Skip to main content
Be Bitwise

Firmware & Bootloaders

Embedded Systems & Firmware · Embedded Systems & Firmware track

6 min read

In this module

The Software That Lives on the Chip

Firmware is the software permanently baked into an embedded device. Desktop applications get loaded from disk by the OS; firmware lives in non-volatile memory (usually flash) and starts executing the moment power hits the chip. The name itself sits halfway between hardware and software, which is exactly where firmware lives — software, yes, but welded to the specific hardware it drives.

Flash Memory

Most modern embedded devices store firmware in NOR flash or NAND flash. NOR flash allows direct code execution: the CPU fetches instructions straight from the flash chip. NAND is cheaper and denser but typically requires code to be copied into RAM before it can run. Flash is erased and rewritten electrically, which is how firmware updates happen, though there's a catch: each cell can only be rewritten a limited number of times (roughly 10,000 to 100,000 erase cycles for NOR and SLC NAND, and only a few thousand or fewer for the TLC and QLC NAND in cheap storage), whereas RAM doesn't wear out.

Flash vs RAM

Connecting to Memory Fundamentals: RAM is volatile (loses its contents without power) and fast to write. Flash is non-volatile (retains contents without power) but slower to write and must be erased in blocks before rewriting. Firmware lives in flash; runtime data lives in RAM. When a microcontroller boots, initialised variables are copied from flash to RAM by startup code.

Flash isn't the only non-volatile store on the chip. Many MCUs also have a little EEPROM, or a flash region used the same way, for settings that change now and then, such as calibration values or a device name. And many have one-time programmable (OTP) fuses, bits that can be blown once and never reset. They're where a manufacturer locks the debug port or stores the hash of the key that firmware must be signed with, which is exactly what Embedded Security builds on.

The Boot Process

When power flows to an embedded device, the CPU looks in a fixed place for its reset vector. Some cores simply start executing at a fixed address; a Cortex-M instead reads a vector table at address 0, taking the initial stack pointer from the first word and the reset handler's address (with its low Thumb bit set) from the second. Either way, execution lands in the bootloader, a small program whose job is to wake the hardware up and hand control to the main firmware.

A typical embedded boot sequence:

  1. Hardware reset: CPU starts at the reset vector address
  2. First-stage bootloader (in ROM or protected flash): initialises the clock, configures memory, and verifies the main firmware's integrity
  3. Second-stage bootloader (optional): provides more complex features like firmware update over USB or network
  4. Main firmware: the application code begins executing
A typical MCU boot path

The check at the diamond is the one secure boot hardens later in the course. If it's missing, or skippable, whatever sits in flash runs.

Worked Example: Reading a Vector Table

Here are the first 8 bytes of a firmware image for an STM32F4, a common Cortex-M4 part. On that family flash starts at 0x08000000, RAM starts at 0x20000000, and when the chip boots from flash the hardware maps flash at address 0 too, so these bytes are what the core reads at reset:

c
00 50 00 20 C1 01 00 08

Cortex-M is little-endian, so each 4-byte word is read lowest byte first.

  1. Word 0, bytes 00 50 00 20, is 0x20005000: the initial stack pointer. It's 20 KB above the start of RAM, so the linker has put the top of the stack there. The stack grows downwards from it.
  2. Word 1, bytes C1 01 00 08, is 0x080001C1: the reset handler. It's odd, and that's deliberate. Bit 0 is the Thumb bit, which tells the core to execute in Thumb state, the only instruction set a Cortex-M has. Clear it and the real code address is 0x080001C0, 448 bytes into flash, a little way past the end of the vector table.

The core loads SP with the first value and jumps to the second. The reset handler copies initialised data from flash to RAM, zeroes .bss, and calls main. In a disassembler you'd start reading at 0x080001C0, not 0x080001C1.

Predict: a bad build puts 0x080001C0 in word 1 instead, with the Thumb bit clear. What happens at reset?

The core faults on its very first instruction. It takes the Thumb state from bit 0 of the reset vector, and with that bit at 0 it's in a state a Cortex-M can't execute in. The result is an invalid-state usage fault, which escalates to a HardFault because usage faults are disabled straight after reset. If the HardFault handler can't fix anything (and at this point it can't), the board just sits there. It's a classic mistake in hand-written vector tables, and in assembly startup files that forget to mark the handler as a Thumb function.

On larger Linux-running embedded devices, the sequence stretches further. A bootloader like U-Boot brings up DRAM, loads the Linux kernel from flash into RAM, and hands off control.

Debugging Interfaces: JTAG and SWD

Desktop software lets you attach a debugger through the OS. Embedded systems give you no such luxury, so they expose hardware debug interfaces instead.

JTAG (Joint Test Action Group) is a standard debug and test interface that wires directly into the CPU through dedicated pins on the circuit board. You can read and write registers and memory, set breakpoints, and single-step through code, all without the firmware noticing or cooperating.

SWD (Serial Wire Debug) is a two-wire alternative to JTAG, common on ARM Cortex-M microcontrollers. Same capabilities, fewer pins.

These interfaces are indispensable during development and a gift to attackers later. Anyone with physical access to a live debug port can dump firmware, pull secrets out of memory, and rewrite how the device behaves. The security lesson goes deeper on this.