Monday, 4 September 2017

RPi Boot Sequence

What happens when you apply power to an RPi and start it up?  This is a surprisingly interesting and complex subject.
Assuming we have a screen attached we can see a load of messages displayed as the linux Operating System (OS) starts up, at the end of which, when everything has been loaded, a login prompt is displayed.  GNU/linux has a program called the kernel which is loaded into memory and then executed by the ARM CPU.  The messages we see on the screen show what the kernel does at it sets up the hardware and software to make RPi useful.
So how does the kernel get into memory?  To understand this we need a few details about RPi architecture.  A good summary of different RPi models and their configuration is provided on wikipedia.  I shall assume we are using the first RPi, the model B, which includes a BroadCom BCM2835 SoC (System on a chip).  The BCM2835 includes some ROM, 256MB RAM, an ARM11 CPU and a Videocore IV GPU (graphics processor unit). The reset of the RPi board provides circuitry and connectors for power, SDcard, USB, video output, ethernet etc.
The kernel is stored on an SDcard as part of the image which is copied to the SCcard as part of the installation process.  This image contains two parts a FAT32 partition and an EXT partition containing OS files.  The small FAT32 partition contains a minimal number of files to start up the system.  These are bootcode.bin (2nd stage bootloader), start.elf (3rd stage bootloader), kernel.img (the kernel), config.txt and cmdline.txt (parameter files).
When power is supplied to the RPi board the GPU in the BCM2835 is hard-wired to run the 1st stage bootloader program in the onboard ROM which enables it to read an SDcard containing bootcode.bin into 256kB L2 cache memory.  bootcode.bin enables the main memory 256MB and loads start.elf which loads kernel.img into RAM and enables the ARM processor which then executes the kernel.
Here are a couple of references in a forum and blog which I found when investigating this.
When we start bare metal programming we can use this boot sequence to load our own programs without an OS.  All we have to do is compile our program in the right way to run on a RPi and put the code onto an SDcard with the name kernel.img.  When power is applied our program is run by the bootloader.

No comments:

Post a Comment