isometimes/rpi4-osdev: Tutorial: Writing a “naked metallic” working system for Raspberry Pi 4


Introduction

As a tech CEO @RealVNC, I do not write code any extra. And I've not too long ago realised simply how a lot I miss it.

Presently within the throes of a nationwide "lockdown" on account of Covid-19 (and having been spared my ordinary commute), I've discovered myself with extra hours within the day. I've taken this time for myself and used it to fulfil a childhood ambition - to put in writing a naked metallic working system that runs on industrial {hardware}.

What does naked metallic imply?

After we purchase a pc or a pill/smartphone it usually comes with some primary software program pre-installed. You will doubtless be accustomed to watching Microsoft Home windows, Mac OS, iOS, Android or possibly even Linux begin up as you energy the gadget (or boot it) for the primary time. These are all working techniques - software program designed to make pc chips work out of the field for mere mortals such as you and me. They assist us work together with the machine by drawing to a display, processing messages from gadgets like keyboards & mice, working with networking {hardware} to attach you to the Web, permitting us to playback sound and far, way more.

Software program builders all over the world then construct functions (apps) that run on prime of those working techniques. These apps speak to the {hardware} by way of the working system (OS), so this advanced code does not must be written again and again. Because of this, it is potential to be a software program developer with out figuring out a lot about {hardware} in any respect! It is the OS that does the arduous work that enables us to make use of apps like Fb, Instagram, WhatsApp, TikTok and many others.

It is truthful to say that computer systems cannot do something helpful with out an OS. They simply sit there ready to be advised what to do. So, why is it that solely software program giants like Microsoft, Apple and Google get to inform the vast majority of computer systems what to do as they're being switched on? Why cannot we? Effectively, we will, and that's what naked metallic programming is.

Selection of {hardware}

When you're excited by the prospect of telling a pc what to do then you definately want an curiosity in {hardware}. The pc chip that is going to do your bidding is known as a CPU (Central Processing Unit) - it is the beating coronary heart of each pc gadget. A lot of corporations have designed such CPUs through the years, however two - Intel and Arm - are most generally adopted. These every have their benefits and drawbacks. When you personal a smartphone, it is extremely doubtless that it is working on a chip designed by Arm. When you personal a laptop computer working Microsoft Home windows then it is more likely to be working on an Intel chip. You will need to develop an understanding of each architectures finally, however I've chosen an Arm gadget for this venture.

The brand new Raspberry Pi 4 Model B is a low-cost pc that runs on a 1.5 GHz 64-bit quad-core Arm Cortex-A72 processor. It is a gadget that many hundreds of thousands of individuals worldwide use, and so it is thrilling to put in writing naked metallic code for it. Think about that someone else would possibly sooner or later use your OS! The RPi4 additionally has some helpful hooked up {hardware} that may assist us alongside the best way.

{Hardware} stipulations

You will want some {hardware} to get began with writing your OS:

  • An RPi4 with a devoted energy provide and HDMI lead
  • A monitor/TV linked to the RPi4 by way of HDMI
  • A micro-SD card besides the RPi4 from
  • A pc to put in writing your code on e.g. a Home windows/Mac laptop computer (the dev machine)

You will must just be sure you can write to the micro-SD card utilizing your dev machine. For me, that meant shopping for an SD card adapter, as a result of the micro-SD card was too small for the slot in my laptop computer. You could want the identical, or perhaps a USB SD card reader too in case your laptop computer does not have one built-in.

Different extremely helpful {hardware} that you just cannot do with out:

  • A pair of eyebrow tweezers (I borrowed these from my spouse!) - helpful to insert/take away the micro-SD card into the tiny slot on the RPi4
  • A USB to serial TTL cable - helpful to see what your OS is doing lengthy earlier than you'll be able to write data to the display

Software program stipulations

If you cannot get another person's OS working, you doubtless will not be capable of write your personal. So I began by flashing the SD card with Raspbian - Raspberry Pi's really helpful OS. I used the very neat Imager tool that they make out there on their web site to do that.

Hook up your RPi4 and ensure it boots into Raspbian. There are many sources on-line that can assist you obtain/troubleshoot this. Getting Raspbian up will take a look at that your {hardware} setup is working correctly. Observe: as a result of I linked my RPi4 to my (not good) TV, I wanted to make an edit within the config.txt file on the SD card (setting the hdmi_safe parameter to 1) to make sure that I might see the display. With out that, it was simply black.

Do not proceed till you get Raspbian working!


The RPi4 runs on an Arm Cortex-A72 processor. Your dev machine is probably going working on an Intel processor. You will subsequently want some software program that helps you construct code to run on a special structure. That is known as a cross-compiler.

Utilizing Arm's compiler for Linux

Obtain and unpack Arm's gcc compiler. For causes that I will not go into right here, you will want to make use of the "AArch64 ELF bare-metal goal". Since I am utilizing WSL on Home windows 10 to emulate Ubuntu, I downloaded the x86_64 Linux hosted cross-compiler.

I additionally advocate putting in GNU make - you will want it quickly sufficient. As a result of I am utilizing WSL, for me that was merely a matter of typing sudo apt set up make and getting into my password.

Utilizing clang on Mac OS X (Apple Silicon or Intel)

Obtain and set up XCode from the App Retailer. This gives you a raft of developer instruments at no cost, together with make.

I like to recommend utilizing Homebrew to put in LLVM. For me, Homebrew was already put in, so this was a easy case of typing brew set up llvm.

LLVM gives you all you might want to begin constructing for Raspberry Pi naked metallic on Mac. It even works on my M1 MacBook Professional with Apple Silicon, which runs an ARM processor relatively than an Intel processsor.

Constructing immediately on a Raspberry Pi 4

You'll be able to learn extra about learn how to construct on the Pi itself here.

Now you are prepared to begin writing your OS!

Go to part1-bootstrapping >

Acknowledgements

Not the entire code right here is my unique work, however has been collated from and impressed by some implausible contributors.

Thanks go to:

  • Zoltan Baldaszti's "Naked metallic Raspberry Pi 3 tutorials" (github)

Do attain out if you need to be acknowledged right here too!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *