What’s the first thing that happens when you turn a computer on? In a PC, the processor is energized, and the first instruction in the computer’s memory is loaded and executed. The program that executes from that point on is known as a bootloader. In PCs, a bootloader is a system that identifies and loads an operating system kernel and relinquishes control to that operating system.
In a typical PC, the boot process begins with a Power On Self Test (POST). Control then flows to the Basic Input Output System (BIOS), which identifies a memory device from which to load an operating system. This could be a bootable CD-ROM, USB flash drive, or hard drive. Other stages follow that identify drive partitions and operating systems to continue executing.
But what a bootloader is and does in an embedded computer are very different.
Bootloaders in Embedded Devices
Embedded computers have much simpler operating systems (or none at all) that don’t require nearly as much memory for storage. They also may be required to start useful operation immediately. For these reasons, embedded computers don’t typically load programs from serial memory peripheral devices (such as hard drives). They begin execution from the processor’s own internal flash memory that doesn’t need to be loaded from anywhere because the flash memory is implemented in the same address space as RAM.1
Someone from the non-embedded computing world may consider an embedded application as actually being a bootloader that never actually loads an operating system. In the embedded world, a bootloader has an altogether different purpose and meaning.
Embedded applications can be sophisticated, and the development of these systems often requires the ability to change the software of a device — either to fix defects or add features. In embedded systems, the program that facilitates updating software is known as a bootloader. An embedded bootloader may also identify, validate, and load an OS, but the main reason for having a bootloader is to have a separate small program that can internally facilitate updating the main application.
How Embedded Computers Update Software
In a PC, updating an application is a matter of quitting that application, then overwriting the copy of that application in hard drive. In an embedded device, things can get tricky for one main reason: embedded processors execute their programs in place (flash, AKA ROM) as opposed to loading them into RAM and executing from RAM.
How do you update this flash memory while you’re executing in it? How do you write to flash memory at all?
There are different types of flash. Today’s embedded processors use NOR flash.2 NOR flash is great for embedded because it features really fast read speeds — and that allows us to pull off that execute-in-place trick. Erasing flash is problematic because we can’t just erase a single byte at a time. NOR flash is erased one block at a time, and you have to carefully make sure you don’t erase your own program while you’re still trying to run it.
Erasing flash blocks and writing to flash cells take time — ages in computer time — often between several milliseconds and several tens of milliseconds. Also, you usually cannot read from flash while writing or erasing flash unless the flash is specifically separated for this purpose.
Differences Between Processors
Embedded processor manufacturers provide very different interfaces and features. For example:
- The ATmega series of chips feature separate Read-While-Write and No-Read-While-Write flash spaces. This enables the Arduino bootloader to execute out of one flash space and write to the other flash space (through a temporary page buffer).
- The STM8A allows you to write to a single byte in flash at a time if you are executing from flash. This stalls (halts) the processor for several milliseconds while the write completes. To speed up the process, you’ll have to relocate a special chunk of code to RAM and execute from RAM to write whole pages at a time. Luckily ST provides some libraries to help.
- The Freescale S08 has one flash space divided into pages. Any erase or write requires jumping to a RAM resident function. Writes to individual bytes are supported.
- The Renesas RX600 series of chips is similar to the ATmega. There are two separate flash spaces, called user mat and data mat. However, this chip doesn’t have built in EEPROM. The data mat is used as a substitute for EEPROM. Writes to the data mat are possible while executing from the user mat, but the data mat can’t be used for executing programs. Again, you’ll have to jump into RAM to write into the user mat. Unfortunately this chip adds some additional complexities including configuring its FCU and writing, through a specific sequence, 256 byte chunks at a time.
It seems like every chip has a unique way of updating its internal flash. I guess this is what makes writing bootloaders so much fun.
So the next time you update your phone, flash a new arduino program, or take your car into the dealer to update its software, remember there’s a small, but important, bootloader making it possible.
Footnotes
- Not all embedded processors have flash; your mileage may vary.
- This is different from the NAND flash in your SD card.