---
title: 'OTA firmware updates: designing for the update that fails, on an ESP32'
description: >-
  Design an OTA firmware update for the failure cases: flash budget for A/B
  slots on ESP32, the battery one update costs, and ESP-IDF's rollback rules.
author: elroot_labs
date: '2026-11-18'
categories:
  - iot
---
Design an OTA firmware update around the update that fails. Write the new image into a second app slot, switch to it only after the image checks out, make the new firmware prove itself before the device keeps it, and limit how often a device retries. On an ESP32, ESP-IDF supplies each mechanism: two app slots, a switch that survives a power cut, automatic rollback, signed images and anti-rollback ([ESP-IDF OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)). The choices that decide whether a bad update strands a device are still yours: how big the slots are, what the self-test checks, and how a device behaves when an update keeps failing. We worked the numbers for an ESP32 from Espressif's documentation. On a 4 MB part, an A/B layout caps each image at 1,984 KB, and a failing 1 MB update retried every hour would cut an estimated battery life from 758 days to 105.

This is a worked calculation from Espressif's published rules and our own stated assumptions, in the same form as the battery estimate in our [IoT prototype guide](/blog/iot-prototype-development-india). We have not measured an update on a shipped fleet for this post, and the assumptions are marked so you can replace them with your own measurements.

## Power can fail at four points, and each one needs its own answer

An update on an ESP32 goes through four steps, and a power cut means something different at each.

1. Writing the new image. ESP-IDF writes it "to whichever OTA app slot that is currently not selected for booting" ([OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)). If power fails here, the device restarts into the old firmware, because nothing points at the half-written slot yet.
1. Checking the image. `esp_ota_end()` validates the newly written image before anything switches, so a truncated or corrupt download is rejected and the old firmware stays in charge.
1. Switching slots. The choice of slot lives in a small OTA data partition that is two flash sectors long. ESP-IDF writes both sectors with matching data and, if a power cut leaves them disagreeing, uses a counter to pick the one written more recently.
1. The first boot of the new firmware. The image starts in a pending state. With rollback enabled (`CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE`), a reset before the new firmware confirms itself sends the device back to the previous version. A power cut at this point counts as such a reset.

Steps 1 to 3 are handled for you as long as you keep the two-sector OTA data partition and call the functions in order. Step 4 is where your design decides the outcome, and the next sections come back to it. One exception to note: if you keep a factory app, it is never rolled back, so it has to be a version you trust for the life of the device.

## On a 4 MB ESP32, two app slots cap each image at 1,984 KB

A/B updates need room for two complete images, so the slot size limits how large your firmware can ever grow. The [ESP-IDF partition table rules](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/partition-tables.html) put app partitions on 64 KB boundaries, and the built-in OTA table keeps the first 64 KB of flash for the bootloader, the partition table and three small data partitions, including the 8 KB OTA data partition. We took that first 64 KB as fixed, rounded each slot down to a multiple of 64 KB, and left no room for file storage, logs or crash dumps.

| Flash size | One app, no OTA | Two OTA slots | Factory app plus two OTA slots |
| ---------- | --------------- | ------------- | ------------------------------ |
| 4 MB       | 4,032 KB        | 1,984 KB      | 1,344 KB                       |
| 8 MB       | 8,128 KB        | 4,032 KB      | 2,688 KB                       |
| 16 MB      | 16,320 KB       | 8,128 KB      | 5,440 KB                       |

![Bar chart of the largest app image an OTA firmware update can install on an ESP32, by flash size: with two OTA slots, 1,984 KB on 4 MB, 4,032 KB on 8 MB and 8,128 KB on 16 MB; with a factory app as well, 1,344, 2,688 and 5,440 KB.](/images/blog/elroot/figures/ota-firmware-update-esp32-largest-image.png)

The factory app costs a third of the app space, and on a 4 MB part that takes the ceiling from 1,984 KB to 1,344 KB. The built-in "Factory app, two OTA definitions" table is more conservative still: it uses 1 MB slots and ends at 0x310000, which leaves 960 KB of a 4 MB chip unused.

The OTA functions write app slots, not the partition table, so plan the slot size for the firmware you expect to ship in two years. If today's image is already near the ceiling, choosing a larger flash part now is easier than living with a slot that is too small later. Run `idf.py size` on every release and treat the slot size as a budget, the way you would treat RAM.

## One update costs hours of battery, and a retry loop costs months

The download keeps the radio on, so on a battery device an update has a price. We used the figures behind our ESP32 battery estimate: the [ESP32 datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf) gives 95 to 100 mA while receiving, and we assumed an average of 120 mA while awake, a 2,000 mAh battery and a device that reports once an hour, which works out at 0.110 mAh an hour, about 758 days. For the update we assumed an effective rate of 50 KB/s (slow) or 200 KB/s (fast) for downloading and writing together. Flash erase and write current is not counted separately, so read these as lower bounds.

| Image    | Rate     | Time   | Charge used | Same as this many hours of hourly reporting |
| -------- | -------- | ------ | ----------- | ------------------------------------------- |
| 1,024 KB | 50 KB/s  | 20.5 s | 0.683 mAh   | 6.2                                         |
| 1,024 KB | 200 KB/s | 5.1 s  | 0.171 mAh   | 1.6                                         |
| 1,984 KB | 50 KB/s  | 39.7 s | 1.323 mAh   | 12.0                                        |
| 1,984 KB | 200 KB/s | 9.9 s  | 0.331 mAh   | 3.0                                         |

A successful update is cheap: at worst about half a day of normal running. The danger is an update that fails at the end and starts again. If that hourly device downloaded a 1 MB image at 50 KB/s every hour and failed each time, its draw would rise from 0.110 to 0.793 mAh an hour and the estimate would fall from about 758 days to about 105.

Mobile data follows the same pattern. One 1 MB image sent to 1,000 devices is 1.05 GB before protocol overhead. In our [IoT SIM measurement](/blog/iot-sim-card-india-data-per-device), the same fleet reporting every five minutes on connections kept open used 8.9 GB a month, so one fleet-wide update equals about three and a half days of reporting. A single device retrying that update every hour downloads 25.2 MB a day, against 8.9 MB a month for its reports.

So give every device a retry policy before it ships. Wait longer after each failed attempt, stop after a fixed number of attempts and report the failure, and skip the update while the battery is low. A device that gives up and tells you is easier to fix than one that drains its battery in silence.

## Let the new firmware prove itself before the device keeps it

With rollback enabled, the new firmware must call `esp_ota_mark_app_valid_cancel_rollback()` once its self-test passes, or `esp_ota_mark_app_invalid_rollback_and_reboot()` if it fails ([OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)). Everything therefore depends on what the self-test checks.

We would make the self-test prove that the firmware can still receive the next update. A version that boots, reads its sensors and then can never reach the update server again passes a shallow test and leaves the device stuck for good. So we would check, in order: the firmware starts and its main tasks run, the sensors answer, the device joins its network, and it can reach the update server.

Espressif recommends running the self-test as quickly as possible, because a power cut before confirmation also rolls the device back. The two goals pull against each other: a test that waits for the server takes longer and so rolls back more often after power cuts. We would accept that cost. A needless rollback costs one more download later; confirming a firmware that cannot update costs a site visit.

## Sign every image, and spend anti-rollback steps on security fixes only

Anyone who can get a device to download an image should not be able to make it run one. ESP-IDF can check signatures on updates even without hardware secure boot (`CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT` and `CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT`), and with secure boot enabled `esp_ota_end()` rejects an image whose signature does not verify. Keep the signing key off the build machine that compiles every branch, because whoever holds it can ship firmware to the fleet.

Anti-rollback stops a device from accepting an older, vulnerable version. ESP-IDF compares a security version in the image with one burned into the chip's eFuse, and on the ESP32 that field is 32 bits, so "only 32 times you can do an anti-rollback" ([OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)). Raise it on every monthly release and the steps run out in 32 months, about 2.7 years. Raise it only when an older version has a security flaw worth blocking, and the 32 steps last the life of the device.

## The failure cases and what handles each

| What goes wrong                                    | What handles it on an ESP32                                                      | What you still decide                                 |
| -------------------------------------------------- | -------------------------------------------------------------------------------- | ----------------------------------------------------- |
| Power fails while the new image is written         | The image goes to the slot that is not booting, so the old firmware still starts | How long to wait before retrying                      |
| The download is cut short or arrives corrupt       | `esp_ota_end()` validates the image before the switch                            | How many attempts before the device stops and reports |
| Power fails while the device switches slots        | Two-sector OTA data partition with a counter                                     | Keep the 8 KB OTA data partition in your table        |
| The new firmware crashes or cannot connect         | Rollback on a reset before the firmware confirms itself                          | What the self-test checks, and how quickly            |
| Someone serves a forged image                      | Signature check, with or without hardware secure boot                            | Where the signing key lives and who can use it        |
| An old, vulnerable version is installed on purpose | Anti-rollback security version in eFuse, 32 steps on the ESP32                   | When to raise the security version                    |
| The firmware outgrows its slot                     | Nothing in the OTA functions, which write app slots only                         | Slot size, and flash size, at design time             |

## Frequently asked questions

**What happens if power fails during an OTA firmware update on an ESP32?** It depends on the step. While the image is being written or checked, the device restarts into its old firmware. While the slot is being switched, the two-sector OTA data partition keeps one good copy. During the first boot of the new firmware, a device with rollback enabled goes back to the previous version ([ESP-IDF OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)).

**How much flash does OTA need on an ESP32?** Room for two full images plus 64 KB at the start of flash for the bootloader, partition table and small data partitions. On a 4 MB part that allows images up to 1,984 KB with two slots, or 1,344 KB if you also keep a factory app, by our calculation from the [partition table rules](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/partition-tables.html).

**How many times can ESP32 anti-rollback be used?** 32 times, because the security version field in eFuse is 32 bits on the ESP32 ([OTA documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/ota.html)). Save the steps for releases that fix a security flaw.
