How nanoflash works
Every microcontroller family ships its own flashing tool (esptool or espflash, dfu-util, bossac, avrdude, picotool, probe-rs), each with its own partition format. Web flashing exists only per vendor. nanoflash lays out flash and writes firmware for boards of any vendor through one API, natively (a Rust library and a command) and in the browser (WebAssembly, an npm package and a web component).
nanoflash is project-agnostic. It knows nothing about an operating system or a signing scheme. It reads the image formats that flashing needs (binary, Intel HEX and ELF) and nothing else. Callers bring signing and other formats through hooks.
Two input files
- A layout file (TOML) describes the flash of a board: its size and its named partitions, each with a role. nanoflash computes the offsets, checks alignment, overlap and size, reports the headroom of every partition, and writes the table that each family needs. See Layout file.
- A board manifest (TOML or JSON) lists the boards of a project: protocol, chip, layout, images, reset sequence and optional SHA-256 digests. An esp-web-tools manifest is accepted as it is. See Board manifest.
One engine per protocol
Each protocol has one asynchronous engine, a backend. The same code runs natively and in the browser, over a transport trait:
- The native transport wraps the blocking calls of the
serialportcrate inasync fns. The command runs one flash at a time on a single-threaded executor, where this is harmless. A program that embeds nanoflash in a multi-threaded runtime runs it on a dedicated thread. - The browser transport speaks Web Serial through hand-written
wasm-bindgenbindings. - The test transports replay and record transcripts.
Futures are not Send: browser APIs are single-threaded, so every trait is designed for a
single-threaded executor. The library has no clock: every read takes a timeout, so a no_std
backend needs no timer of its own. See Library for the traits.
The backends are:
| Backend | Boards |
|---|---|
| Espressif ROM loader | ESP32, ESP32-S2, ESP32-S3, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2, ESP32-P4 |
| SAM-BA, with the Arduino extensions of bossac | Arduino Nano 33 BLE (nRF52840), Arduino SAMD21 boards |
| STK500v1 | Arduino Uno, Nano, Pro Mini (Optiboot, ATmegaBOOT) |
| STK500v2 | Arduino Mega 2560 (the wiring bootloader) |
| AVR109 | Arduino Leonardo, Micro, Pro Micro (Caterina) |
USB DFU, UF2, CMSIS-DAP and Nordic serial DFU come later. See Design decisions for the order.
What a flash does
- Select the board. The protocol comes from the manifest. See Choosing a backend before detection.
- Prepare the images. nanoflash reads every image, checks its SHA-256 digest, calls the verifier of the caller, decodes Intel HEX and ELF files, and places each image at its offset. It generates the partition table from the layout.
- Check the plan. Alignment, overlap, partition sizes, flash size and the address range of the protocol are checked before connecting.
- Connect. The backend resets the board into its bootloader and detects the chip. It reports the flash size and the part of the flash that the bootloader writes.
- Check again. With a layout whose flash size is detected, the layout is resolved again for the reported flash. Every image and erase entry must fit what the chip reports, and the backend checks the order of the whole flash.
- Erase, write and verify. Verification uses MD5 on Espressif, CRC-16 through the
Zcommand on SAM-BA, and a read-back on AVR. - Reset the board into its application.
Nothing is erased before steps 2 to 5 pass. Checks before the first erase explains why and what each bootloader erases.
Choosing a backend before detection
- The protocol comes from the manifest. One manifest may mix protocols, for example an ESP32-S3
and an nRF52840 variant of the same product. The USB identity of the selected port, matched
against the
application-devicesandbootloader-devicesof each board, picks the board first. Chip detection then picks among the boards of that protocol. - A port that identifies nothing (a USB to UART bridge such as CP210x or CH34x) is matched against
the boards whose reset sequence fits a bridge (
espressif-classic). When several protocols remain,flashstops with an error that lists them, and--board-identifiersettles it. detectwithout a manifest uses a built-in table of known USB identities to pick the backend: the Arduino AVR boards of Arduino’sboards.txtfirst, then the vendor0x303a(Espressif) and the vendor0x2341(Arduino, SAM-BA). Otherwise it requires--protocol. It never probes several protocols on the same port, because each probe resets the board differently.- Arduino’s AVR boards share the vendors of its SAM-BA boards, so the table lists their products
before the vendor rule. A Nano or a Pro Mini sits behind a serial bridge (FTDI, CH340) that
tells nothing: the board of the manifest without devices, or
--protocol, decides. USB identities thatdetectknows lists the identities. - In the browser, only the module of the chosen backend is loaded, for
detecttoo.
The crates
Three crates in one workspace, plus the npm package. Fewer crates means one version to release, no glue between crates, and features instead of crate boundaries for what a firmware or a web page leaves out.
| Crate | Role |
|---|---|
nanoflash (crates/nanoflash) | The library, no_std with alloc. Every feature only adds code. See Library. |
nanoflash-cli (crates/nanoflash-cli) | The nanoflash command and its twin cargo-nanoflash. Both binaries call main_with of the library of the crate, which holds all the code. It is #[doc(hidden)]: the command is the interface. |
nanoflash-web (crates/nanoflash-web) | The wasm-bindgen bindings, built once per backend into separate WebAssembly modules. It is not published. |
The npm package (web/) holds the TypeScript API, the backend loader and the <nanoflash-button>
web component. See Browser API.
The web layer
nanoflash-webbuilds one WebAssembly module per backend withwasm-bindgen-cli:core(layouts),espressif,samba, andavrfor the three AVR protocols. Each flashing module contains the core and its backend only.- The npm package exposes
prepare(manifestUrl), whoseflash(options)asks for the port inside the click,detect(), the layout functions, andloadBackend(protocol), which imports the right module dynamically once the board is known. <nanoflash-button manifest="manifest.json">is a custom element with progress, error and unsupported-browser states, styled through CSS custom properties and parts.- Every module is at most 256000 bytes once compressed with
gzip -9. CI measures each module and fails above it. - The flasher page is a page around
<nanoflash-button>. See Browser support and security.