How does Raspberry Pi boot?
In my previous article I explained how to boot Raspberry Pi with Arch Linux. In this article I would like to give some insights into raspberry pi's boot process. Booting starts when you plug in the micro-usb cable (Basically by powering up the board).
Raspberry pi boot process starts with the GPU Core enabled , CPU and SDRAM Disabled. So GPU is responsible for the first and second stage of the booting.
In a nutshell :
GPU runs the the Boot ROM code (Which is embedded inside the SoC) and looks for bootcode.bin in all the valid boot sources. If it finds a valid bootcode.bin then loads it into 128K Cache (L2 Cache)memory and jump to it. This enables SDRAM and loads loader.bin, which loads start.elf, Which loads the Linux kernel.
Once the kernel is loaded, It takes the control and starts the init process.
If you have gone through my previous article on how to boot your RaspberryPi with Archlinux, You might have noticed that we created two partitions, One with FAT32 filesystem named boot. If you go and check that partition you will see some binary files, .dat files and .txt files as shown in the figure.
.dtb files or device tree blobs are used to pass the device information to the kernel. We cannot inspect these files directly in a text editor. If you are keen on what is inside it, Then you should install device-tree-compiler and use fdtdump application as shown below.
Files start.elf, start_x.elf, start_db.elf and start_cd are coupled with fixup.dat, fixup_x.dat, fixup_db.dat and fixup_cd.dat respectively (.dat files are linker scripts). These are the files loaded in to the VideoCore GPU after bootcode.bin has executed.
start.elf is the basic version of firmware, start_x.elf version has support for codec and camera drivers, start_db.elf is the debug version of the firmware and start_cd.elf is the cut-down version of the firmware and it's used when gpu_mem=16 is selected in the config.txt. We can configure some of the boot processes using config.txt. More information will be available here : https://www.raspberrypi.org/documentation/configuration/config-txt/boot.md
Boot process begins by reading the OTP (One time programmable) Register. By default SD card is the primary boot source, USB comes second. Bootrom also checks for GPIO Boot Mode bits in the OTP. There are two sets of GPIOs available to select the boot mode. GPIO22-26 for low bank and GPIO39-43 for high bank. This enables the user to decide on which boot mode to use when there are multiple boot sources are available. You can modify connfig.txt file to enable this option.
program_gpio_bootmode=n
n = 1 for low bank and n =2 for high bank.
One think to note here is that once you have enabled this option then GPIOs for the respective bank will not be available for any other purpose and it's a one time process.
The boot process runs as follows:
- VideoCore GPU boots with bootrom code embedded inside the SoC.
- It reads the OTP and check for any valid boot mode is enabled
- If GPIO boot is set then it looks for bootcode.bin in that particular boot source. (Primary SD, Secondary SD, NAND, SPI and USB)
- By default SD card gets the first priority and it looks for bootcode.bin inside it. If It can't find it, then it checks the USB. If USB is not an OTG device then looks for the type of the USB device, If its a HUB then code recurs through the each devices to find MSD(Mass storage device ) or LAN951x and looks for bootcode.bin inside it.
- Once the valid bootcode.bin has found, Its loaded into L2 Cache memory of GPU.
- This reads the config.txt and cmdline.txt and loads start.elf (depends on the option present in config.txt)
- start.elf then loads the linux.img file
We will look into the raspberry pi internals in the next article.
Happy coding!
Englobe | Dalhousie University | Electrical Engineer | Firmware Engineer
1 年Hi, Thanks for the amazing information. Could you kindly add some sources from where you have written the article?