Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Checks before the first erase

A flash that stops halfway leaves a board that does not boot. Most failures come from the inputs: an image that is too large, a wrong offset, a partition that overlaps another, a file that is not the expected one. nanoflash therefore reads and checks every input before it erases a byte. A mistake in a manifest costs an error message, not a board in its bootloader.

The order of the checks

  1. Before connecting, when the board is known: every image and erase entry is read and checked. The checks are the digest, the alignment, the overlap, the partition, the layout and the address range of the protocol.
  2. After connect, before the first erase: everything is checked again against the flash that the chip reports. Every image and erase entry must start at or after writable_from and end at or before writable_to. When the bootloader erases to the end of flash, every erase entry must end at the end of flash. The backend’s check_plan must accept the whole flash in the order in which it is sent (the erase order of the Mega 2560).

Every error message names the partitions and the addresses involved. Addresses print as 0x hexadecimal. See Errors and warnings.

What flash writes

  • It writes exactly the images that the manifest lists, plus the partition table generated from the layout when the layout has a partition-table partition and no manifest image targets it.
  • A manifest image that targets that partition wins, and layout check warns when it differs from the generated table. In a layout whose partitions grow or move with the flash, such an image is refused (partition-table-image-for-detected-flash): no fixed table fits every chip.
  • It does not initialise otadata and does not patch the Espressif bootloader header (flash size, mode, frequency). Both are image formats, which nanoflash leaves to the caller. A manifest that wants otadata reset lists an image for it, or an erase entry for its partition.

What flash erases

It erases only the regions that it writes, rounded to the erase unit, except on the SAMD21 (see below). --erase-all (and eraseAll: true in the browser API) erases the flash first, from the lowest offset that the bootloader writes (writable_from, which connect reports) to the end. A bootloader that the chip protects, such as SAM-BA’s, is never erased. Data partitions such as NVS or storage are not touched unless an image or an erase entry targets them, again except on the SAMD21.

SAM-BA

Arduino’s X command depends on the chip:

  • nRF52840 (Nano 33 BLE): X erases nothing. Each write erases the 4 KiB pages that it programs. An erase entry or --erase-all writes 0xFF over its pages.
  • SAMD21: X erases from its address to the end of flash. A write erases from the lowest written address to the end of flash, so everything stored after the lowest image is gone, data partitions included. nanoflash says so in its progress messages and adds an erases-to-end warning to the report, which names the partitions of the layout that it erased and that no image is written to. Erase entries must end at the end of flash, and images are written in order of offset.

AVR

The STK500v1, STK500v2 and AVR109 bootloaders have no usable chip erase: Optiboot ignores it, and Arduino never sends it. On AVR, “erased” means “programmed with 0xFF”.

  • Optiboot, ATmegaBOOT and Caterina erase each page as they program it. An erase entry or --erase-all programs 0xFF over its pages.
  • The bootloader sits at the top of the flash, which connect reports as writable_to (0x7E00 on an Uno with Optiboot). Nothing at or above it is written. --erase-all stops at writable_to, or at the bootloader partition of the layout above the application when that partition starts lower. The layout of the Nano keeps 2 KiB for the old bootloader, while Optiboot takes 512 bytes.
  • The wiring bootloader of the Mega 2560 (STK500v2) erases one page per page programmed. Its erase address starts at 0x0 and moves on by one page each time, wherever the programmed page goes. It erases nothing from its APP_END, 0x3C000 (16 KiB under the end of flash, twice its 8 KiB). So writable_to is 0x3C000. Before the first erase or write, the backend replays that address over the whole flash (--erase-all, the erase entries, then the images in order of offset). Images must start at 0x0 and follow each other, and an erase entry must start at 0x0, unless --erase-all erased every page first. A flash that breaks the order is refused before anything is written.

Layouts that detect the flash size

A layout with size = "detect" describes partitions that grow or move with the flash of the chip. Growable partitions explains the rules. For flashing, they add these steps:

  • Resolution at connect. Before connecting, the layout is resolved for its minimum, and every input is checked against it, image sizes included. An image larger than its partition on the minimum is refused, even for a chip with more flash. Every prepared image remembers how it was placed (by partition, by offset, or the generated table), and every erase entry the partition that it names. Right after connect, before any check against the chip and any erase, the layout is resolved again for the flash that the chip reports:

    • a chip below the minimum fails with flash-too-small;
    • a chip that reports no size (secure download mode, an unknown flash identifier) keeps the minimum with a flash-size-not-detected warning;
    • a chip of exactly the minimum changes nothing;
    • a larger chip moves the images and erase entries that name a partition to where it now is, generates the partition table again for that size, and runs the checks of the plan again.

    Then come the checks against the chip (writable_from, writable_to, erases to the end of flash) and the backend’s check_plan, on the new places.

  • Inputs tied to an offset. An image or an erase entry given by offset, an Intel HEX image, an ELF image decoded into flat segments and a manifest image of the partition table cannot follow the chip. The addresses of Intel HEX and ELF images are in the file, even when the manifest names their partition. Such an input may not overlap a partition that moves with the flash (fixed-offset-on-moving-partition): it would stay where the partition was on the minimum. This is checked before connecting against the minimum, and again after the resolution for the chip, because a partition that moves by a whole step can cover what was the unallocated end of the flash on the minimum. Images and erase entries that name a partition follow it.

  • Warnings. partitions-grew names each partition that is larger than on the minimum, with both sizes. On the Espressif protocol it adds that the bootloader image must be built for that flash size or with flash size detection, because ESP-IDF applications use the flash size of the bootloader header, which nanoflash does not patch. The chip refuses a bootloader header that names more flash than it has. partitions-moved names each partition that starts elsewhere than on the minimum: data that a firmware wrote for another flash size is not where the layout now puts it. flash-size-not-detected says that the minimum was kept. Each warning is also sent as a progress message when it happens. The report lists the warnings of the image preparation, then these, then erases-to-end, then not-verified-in-secure-download-mode.

  • The bootloaders. The layout describes the whole flash that the chip reports. On AVR that size includes the bootloader at the top, so a partition that grows to the end of the flash covers it. An erase entry or an image that reaches into it is refused by the writable_to check, before any erase. On the SAMD21, erase entries must end at the end of flash. A partition that grows in steps or up to a maximum may stop short of it on a larger chip, and an erase entry for it is then refused before any erase. A lone remaining partition at the end of the layout fills to the end of the flash on every chip.