Embedded and IoT

What is firmware?

Firmware is software stored in non-volatile memory on a chip, such as flash, that runs when the device starts and controls its hardware. NIST describes it as code stored in a chip that resides at the processor's reset vector or extends other firmware. On a simple microcontroller, the firmware can be the whole program the device runs.

TL;DR

Firmware is the code a device runs first, and on a microcontroller it is often the whole program. It can be updated after a device ships, and designs with two app slots keep the current image bootable if power fails during an update. Signed updates and secure boot prove where an image came from, not what it does once running.

Learning objectives

After reading this article you will be able to:

  • Explain what firmware is and why it runs first when a device starts
  • Describe the layers of microcontroller firmware, from bootloader to application
  • Explain how two-slot updates and secure boot keep firmware trustworthy

Firmware storage and boot order

Firmware lives in non-volatile memory such as flash or ROM, on the chip or right beside it. Every processor needs code to run the moment power arrives, and that code has to be stored somewhere that survives power-off. That stored code is firmware.

NIST SP 800-193, the US guidance on platform firmware resiliency, defines firmware as a generic term for any code stored in a chip that either resides at the reset vector (or equivalent) of the processor or is provided as an extension to other firmware. The reset vector is where the processor starts executing after it is switched on or reset, so firmware is, by definition, what runs first.

Firmware is everywhere in a computer, not only in small gadgets. NIST lists boot firmware on the main processor (BIOS and UEFI are examples), and firmware in storage and network controllers, graphics processors and service processors. Some power supplies and batteries have their own microcontroller and firmware governing how they charge and discharge.

Firmware vs software vs hardware

Firmware sits between the physical hardware and the software people install, and the three differ in where they live and how often they change:

HardwareFirmwareSoftware
What it isThe physical chips, circuits and componentsCode stored in a chip that runs first and controls that hardwarePrograms and apps that run on top of an operating system or the firmware
Where it livesOn the circuit boardNon-volatile memory such as flash or ROM, on or beside the chipUsually on a disk or in a device’s main storage
How it changesOnly by replacing partsRarely, through a deliberate update the device verifiesOften, by installing and updating apps
ExamplesA processor, a radio, a sensorBIOS or UEFI, a storage controller’s code, a microcontroller programA browser, a phone app

On small devices the firmware and the software are the same thing: the microcontroller’s program is the whole of its software.

Firmware on a microcontroller

On a microcontroller, the firmware is the program: it sets up the clocks and peripherals, then runs the device’s job until the power goes. The line between firmware and the application tends to disappear, because there is no separate operating system on a disk and no apps to install.

The Embedonomicon, the Rust project’s guide to bare-metal programming, puts it this way: a program built without the standard library can be the first and the only code that runs on a system, and it lists firmware, bootloaders and operating system kernels as examples. That is why bare-metal Rust firmware is written in no_std mode.

Inside, microcontroller firmware can have some or all of these layers:

  • A bootloader that runs first, checks what is in flash, and starts the main image. It can also install updates.
  • Drivers for the peripherals: pins, timers, serial buses, radios.
  • Optionally, a real-time operating system (RTOS) kernel for scheduling several tasks. Zephyr, for example, describes itself as a small-footprint kernel for resource-constrained and embedded systems.
  • The application, the code that reads the sensor, drives the motor or talks to the phone.

Simple firmware can do without an RTOS and run a single loop with interrupt handlers.

Firmware updates after shipping

Firmware can be updated after a device ships, so it is not fixed for the life of a device. NIST notes that updates can remove vulnerabilities and correct operational problems, and its core baseline for IoT devices, NISTIR 8259A, includes software update as one of its device capabilities. Its common elements include the ability to update through remote or local means, to verify and authenticate any update before installing it, to roll back to a previous version, and to restrict updates to authorised entities.

An update delivered by radio or network is called over-the-air (OTA). The risk is a power cut halfway through. Espressif’s ESP-IDF handles this with at least two app slots in flash: the new image is written to the slot that is not currently booting, and only once it is verified does the device switch to it on the next boot. If power fails during the write, the chip still boots the current application. MCUboot, an open bootloader project for 32-bit microcontrollers, likewise defines a common flash layout and a secure bootloader for software upgrades, and works with Zephyr, Apache Mynewt, Apache NuttX and other systems.

Firmware integrity and secure boot

Firmware stays trustworthy through protection, detection and recovery, the three principles NIST SP 800-193 rests its guidance on. Because firmware runs first and is highly privileged, an attacker who changes it can undermine everything that runs above it. NIST SP 800-193 warns that a successful attack on platform firmware could render a system inoperable, or install persistent malware that survives in low-level code. The three principles are:

  • Protection: keep firmware and its critical data intact, including by checking the authenticity and integrity of updates.
  • Detection: notice when firmware or critical data has been corrupted or changed from an authorised state.
  • Recovery: restore firmware and critical data to a state of integrity when corruption is found.

Authenticity comes from digital signatures. In NIST’s model, an authenticated update relies on a root of trust for update that holds a signature verification algorithm and a key store with the public key needed to check each new image. NIST also notes that the matching private key could be stolen, so a design needs a way to recover from key compromise.

Secure boot applies the same idea every time the device starts. In ESP-IDF’s Secure Boot v2, the first-stage bootloader in ROM, which cannot be changed, verifies the signature of the second-stage bootloader, and that bootloader verifies the application before running it. Only the public key is stored on the device; the private key that signs releases is kept elsewhere.

Signatures prove where an image came from, not what it does once running. Firmware that handles keys still needs a good entropy source and careful storage, which running encrypted messaging on a microcontroller covers.

Frequently asked questions

What is the difference between firmware and software?

Firmware is software. The word marks where it lives and what it does. Firmware is stored in a chip on the device and controls that hardware directly, while the apps on a phone or computer sit on top of an operating system and can be installed and removed.

Can a firmware update brick a device?

It can if the update is interrupted and the device has nothing valid left to boot. Designs that keep the old image until the new one is verified, such as the two-slot scheme in Espressif's ESP-IDF, are built so that a power cut during the update leaves the current firmware bootable.

Where is firmware stored?

In non-volatile memory such as flash or ROM, on the chip or right beside it, so it survives power-off and can run the moment power arrives.

Is BIOS firmware?

Yes. NIST lists boot firmware on the main processor, with BIOS and UEFI as examples, alongside firmware in storage and network controllers, graphics processors and service processors.

What is secure boot?

Secure boot checks signatures every time the device starts. In ESP-IDF's Secure Boot v2, the first-stage bootloader in ROM, which cannot be changed, verifies the second-stage bootloader, and that bootloader verifies the application before running it.

Does firmware need an operating system?

No. Simple firmware can do without one and run a single loop with interrupt handlers. A real-time operating system kernel such as Zephyr is an option when firmware needs to schedule several tasks.

Sources

Build it with Offline Protocol

The Offline Protocol embedded docs list what firmware must supply to run its constrained leaf component, including radio I/O, a hardware entropy source, time at pairing, atomic storage that survives power loss, and authorisation of peers.

Read the Rust and constrained devices docs