I want to write a simple bootloader for the Raspberry Pi. The main purpose of this bootloader is to enable a Raspberry Pi at a remote location, to recover from kernel updates that make the device unbootable as the result of a kernel panic. The procedure for updating the kernel should be like this:
The default Linux kernel is /boot/rpi-kernel.img
and the string 'rpi-kernel.img' is written in /boot/default-kernel.txt
. The kernel parameter 'panic=10' has been added to 'cmdline=' in /boot/config.txt
.
- Connect to the Raspberry Pi over SSH.
- Copy
/boot/rpi-kernel.img
to/boot/rpi-kernel-fallback.img
- Create empty file
/boot/kernel_update
- Download kernel update and move it to
/boot/rpi-kernel.img
- Reboot
This is the design for the bootloader I have thought up in pseudocode:
read kernel parameters passed from start.elf
if empty file '/boot/kernel_update' does not exists
boot kernel defined in '/boot/default-kernel.txt' with kernel parameters passed from start.elf
else
if empty file '/boot/boot_kernel_update' exists
write string 'rpi-kernel-fallback.img' to file '/boot/default-kernel.txt'
delete file '/boot/kernel_update'
delete file '/boot/boot_kernel_update'
create empty file '/boot/kernel_update_failed'
boot kernel 'rpi-kernel-fallback.img' with kernel parameters passed from start.elf
else
create empty file '/boot/boot_kernel_update'
boot kernel '/boot/rpi-kernel.img' with kernel parameters passed from start.elf
As a last step in the boot process, a shell script will check if the /boot/kernel_update
exists and then if network is functional and the environment is sane, before removing /boot/kernel_update
.
I want to make the bootloader as clean and simple as possible, with no support for HDMI, USB, Ethernet, serial etc. The problem is that I'm not sure how to get started with this.
The main challenges are to read from and write to the FAT32 filesystem on the SD card, and booting the Linux kernel. Everything has to happen from bare metal on the Raspberry Pi.
Any code examples, resources, suggestions ect. on doing this would be very helpful.