Bootloaders and Over-the-Air Updates: the short answer
When a microcontroller resets, it does not start your program directly. A small bootloader runs first and decides what to run.
- Level
- Advanced
- Time
- About 6 min
- Topic
- Microcontrollers
Understand how bootloaders and flash partitions work, and update ESP32 firmware over Wi-Fi safely with rollback.
Before this: Firmware Build, Logs & Fault Isolation, Getting Started with the ESP32, Security, Privacy & Safe Remote Control
When a microcontroller resets, it does not start your program directly. A small bootloader runs first and decides what to run. On an Arduino Uno, the bootloader listens on the serial port for a short time after reset, so the IDE can upload new code without a programmer. On the ESP32, a ROM bootloader in the chip loads a second-stage bootloader from flash, which reads a partition table and starts one of the application partitions.
Over-the-air (OTA) updates use this. The standard ESP32 partition table with OTA has two app slots, ota_0 and ota_1, plus a small otadata partition that records which slot is active. The running firmware downloads a new image into the other slot, checks it, marks it as the boot slot and restarts. If anything fails during download, the old firmware is still intact. ArduinoOTA lets the IDE upload over the local network; HTTPUpdate or esp_https_ota downloads a .bin from a web server, which is how real products update in the field.
Make OTA safe. Serve images over HTTPS and check the server's certificate, or sign images and enable secure boot, so nobody can push their own firmware. Turn on rollback: the new firmware boots in a pending state and must call esp_ota_mark_app_valid_cancel_rollback() after it has checked that it works (for example, after reconnecting to Wi-Fi and the server). If it crashes or never confirms, the bootloader returns to the previous version on the next reset. Keep a version number in the firmware and log it on start-up, and never update a battery device that is about to run flat.
A bootloader chooses what runs
A bootloader is a small program that runs first and decides which application to start. For safe updates, the flash holds two application slots. The new firmware is written into the inactive slot while the old one keeps running, and only after it is complete and checked does the bootloader switch over.
If the new firmware does not mark itself as good, for example because it crashes at start, the bootloader switches back to the old one. That rollback is what makes a remote update safe.
Check what you install
Verify the image before using it: check its size, a checksum, and ideally a digital signature, so only firmware from you can be installed. An update fetched over plain HTTP could be replaced on the way, so use HTTPS and check the signature as well. Keep the signing key off the device.
Make the update fail safe if power is lost halfway. A power cut during writing into the inactive slot leaves the working slot untouched.
Practical care
Test an update on a device you can reach, including a deliberate bad image. Keep enough flash for two slots, and stagger an update across many devices so a fault does not take them all down at once. Include a version number so you can see what each device runs.
- Writing the new firmware over the running one.
- Downloading firmware over plain HTTP with no signature check.
- Updating every device at once before testing on one.
Practise this lesson with Pro
The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.
With Pro you also get: a worked example, a hands-on task, a 3-question check.
