Error-aware bootloader for Raspberry Pi - c

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.

Related

Embedded Linux C interrupt on USB insertion

I have a small project build with Yocto. When I insert a USB device the kernel prints the info to the console. This tells me something is prompting this and I should be able to capture it in a C program that I write.
My question is, is there a way in a C thread to capture the USB inserted notification, mount, and read a file?
If my application cannot retrieve this notification I can just start a thread and every X seconds run ls /dev/sda. If I get a hit then mount and read my file.

Writing a task in VxWorks

I am trying to understand how can I write tasks for VxWorks. I have VxWorks on a board that is mounted on a development board.
I can access the board through Putty and a serial terminal connected to the UART of the system. How can I write other tasks (maybe to communicate with other UARTs or other Serial Interfaces) ?
And how do I know where to find the documentation describing how I can access those serial interfaces? (for example write/read methods, which file to include etc)*
Thank you
VxWorks is cross development environment, so you need a licensed development system on Windows or Linux desktop to create applications.
Then you load the compiled code on the target reference board as process/RTP or LKM/DKM. How you get it there varies by board: FTP, TFTP, removable USB or SD card....
At the C shell on booted system you can start a task in the kernel context with any public symbol. But if your working with deployed system, it shouldn't have the C shell, that's a serious security risk :)
Try..
-> sp printf, "\nHello VxWorks World!\n"

How to bypass file access in linux kernel

I have a linux kernel (more exactly an Android kernel) that creates a sysfs entry to read data from a connected device. Output yields raw data.
Now I want to create a new file interface in procfs that reads this data and outputs it in another format.
The first idea is to access the sysfs file because it returns exactly what I need as input to my function. Now I read kernels should not do file operations.
But what else is the recommended way? Digging into the device driver and copying all that happens in there would be tedious.

Detect certain connected USB device

I'm working with a USB device in Linux and have written a library to control this device.
Without going in to TOO many details, the device uses a standard UART protocol, so all I have to do is open a serial connection with open, configure the relevant parameters like baud rate, stop bit, parity, etc, etc, and start bit-banging registers.
The library works fine, however, it its hard coded to assume that this device is /dev/ttyUSB0. That is, this is what I pass to open. If open fails, I exit.
What I would like to do is detect that this device is present, regardless if it's /dev/ttyUSB0, /dev/ttyUSB1, etc. And even detect if there are multiple of these devices connected.
I can write code to poll certain registers on the device that will return serial number, product ID, etc, so I can detect that what is on the other end of the USB is indeed my device... but how can I find a list of connected USB devices, again, in native C?
OR is there a more elegant way of doing this, such as interfacing with it's kernel module, or something? I can provide the USB driver it actually uses, but I'm sort of lost when looking through the code.
Thanks for any insight.
The elegant method is to use udev to create a descriptive symlink for your device when it is connected. Add a line something like this to /etc/udev/rules.d
SUBSYSTEM=="tty",ENV{ID_MODEL}=="My_FlowMeter_Model",ENV{ID_USB_INTERFACE_NUM}=="00",SYMLINK+="flowmeter",RUN+="/bin/su pi -c /home/pi/record-flowmeter.sh
That's a very slightly modified version of an actual udev rule my research group uses to collect data from USB devices connected to battery-powered Raspberry Pi boxes. It also runs a script automatically, which has commands like
stty -F /dev/flowmeter 500000 -ixon -echo -icanon
If you want to know the "real" device filename, you could do readlink /dev/flowmeter. But for most uses you can just use the link: fd = open("/dev/flowmeter"); (or pass it as an argument to your program)
Naturally you should replace flowmeter with a short name for your own device, as well as updating the ID_MODEL based on the output from lsusb.
Multiple devices are a bit more complicated, but there are plenty of examples of udev rules out there.
On Linux, the information you are looking for is in the /sys filesystem, specifically under /sys/bus/usb/devices. From there you will need to search the filesystem to find your device.
For example, I just plugged a USB-serial dongle into my Linux (kernel version 2.6.35) and the device appeared under /sys/bus/usb/devices/2.1-8. Here, I am able to find that this is my device by vendorId:deviceId by checking the files idVendor and idProduct. Here, there is a directory named 2.1-8:1:0 which contains a directory named ttyUSB0.
Obviously, to find your device you will need code (or a shell script using find) to scan the directory tree, looking for the right entries.

Get current drive path from kernel

I'm writing a module that needs to read the MBR on the drive of the currently running kernel. But if I hard code /dev/sda it will read the wrong MBR if I install the module in /dev/sdb.
Is there a way to get the current drive path of the currently running kernel?
(I would use filp_open(), vfs_read() and filp_close() to read the first 512 bytes.)
There is no such thing. The kernel doesn't know where the bootloader that loaded it was itself loaded from. The bootloader might not have been loaded from a drive at all (eg. it could have been a PXE network boot, or loaded from ROM by coreboot), and the kernel might have been loaded by another kernel with kexec rather than from a bootloader.
You will need to have the user specify somehow what device to read the MBR from, perhaps with a module parameter.

Resources