<h1 align="center">nanoflash</h1>
<p align="center">
<strong>Flash ESP32 and Arduino boards from one Rust tool: command line, Cargo runner, serial monitor and browser.</strong><br>
One layout file for the partitions, one manifest for the images, and every image checked before
the first erase.
</p>
<p align="center">
<a href="https://github.com/AlixANNERAUD/nanoflash/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/AlixANNERAUD/nanoflash/actions/workflows/ci.yml/badge.svg?branch=main"></a>
<a href="https://codecov.io/github/AlixANNERAUD/nanoflash"><img alt="Coverage" src="https://codecov.io/github/AlixANNERAUD/nanoflash/graph/badge.svg"></a>
<a href="https://vulnerabilites.anneraud.fr/public/f6b4cbfa68cfc88ebcb7f3b4b9457939"><img alt="Vulnerabilities" src="https://vulnerabilites.anneraud.fr/badges/864dab79c27abd0a2dea60dda6c2755f.svg"></a>
<a href="https://crates.io/crates/nanoflash-cli"><img alt="Crates.io" src="https://img.shields.io/crates/v/nanoflash-cli"></a>
<a href="https://docs.rs/nanoflash"><img alt="docs.rs" src="https://docs.rs/nanoflash/badge.svg"></a>
<a href="https://alixanneraud.github.io/nanoflash/"><img alt="Documentation" src="https://img.shields.io/badge/docs-guide-blue"></a>
<a href="#licence"><img alt="License: MIT OR Apache-2.0" src="https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg"></a>
</p>
---
nanoflash **flashes ESP32, ESP32-S3, ESP32-C3 and the other Espressif chips, the Arduino Uno, Nano,
Pro Mini, Mega 2560 and Leonardo, the Arduino Nano 33 BLE and SAMD21 boards** from one command. It
is an **alternative to esptool, bossac and avrdude, written in Rust**: the same engine runs in your
terminal, as a **Cargo runner** behind `cargo run`, in a **serial monitor with defmt decoding**, and
in the browser as a **Web Serial flasher**. A TOML layout file describes the **partition table**
once: nanoflash computes the offsets, checks them, and writes the Espressif partition table, an MBR,
linker regions or Rust constants from it.
It is for firmware developers who flash boards of several vendors, Rust embedded developers who want
`cargo run` to flash and monitor, and projects that ship firmware to users and want a flash button
on a web page. It replaces a different flashing tool and partition format per vendor with one tool
and one format.
## Why nanoflash
- **One tool for three families.** The Espressif ROM loader, SAM-BA with the Arduino extensions of
bossac, and the STK500v1, STK500v2 and AVR109 bootloaders of Arduino's AVR boards.
- **Nothing erased before every check passes.** Digests, alignment, overlap, partition sizes, flash
size and the limits of the bootloader are checked before the first erase, then again against the
flash that the chip reports.
- **One layout file as the source of truth.** The command, the `build.rs` of your firmware and the
browser read the same file. Partitions can grow with the flash that the chip reports.
- **Made for Rust firmware.** `cargo run` builds, flashes and monitors. ELF files are accepted as
they are. The `build` feature writes your `memory.x` from the layout.
- **The same engine in the browser.** The library compiles to WebAssembly: an npm package, a
`<nanoflash-button>` web component and a hosted flasher page.
- **Made for CI.** No prompts, `--json` output, exit codes that tell an input error from a device
error and a failed verification.
- **Binary, Intel HEX and ELF images.** Existing esp-web-tools manifests work unchanged.
## Quick start
Install the command from crates.io. `cargo binstall` downloads the binary of the release for
your host. `cargo install` builds it with Rust 1.95 or later:
```console
$ cargo binstall nanoflash-cli
$ cargo install --locked nanoflash-cli
```
Alternatively, download the Linux, macOS or Windows binaries of
[GitHub Releases](https://github.com/AlixANNERAUD/nanoflash/releases), or build the command from
the repository with `cargo install --locked --git https://github.com/AlixANNERAUD/nanoflash nanoflash-cli`.
The [install guide](https://alixanneraud.github.io/nanoflash/how-to/install.html) shows how to
check the archives.
Describe the flash of the board in a layout file. It gives the flash size and the partitions.
nanoflash checks their alignment, overlap and size, and places a partition without an `offset`
after the one before it:
```toml
# layouts/esp32-s3-8mb.toml
[flash]
size = "8 MiB"
[[partition]]
name = "bootloader"
role = "bootloader"
offset = 0x0
size = "32 KiB"
[[partition]]
name = "partition-table"
role = "partition-table"
offset = 0x8000
size = "4 KiB"
[[partition]]
name = "application"
role = "firmware"
alignment = "64 KiB" # no offset: the first 64 KiB boundary after the table, 0x10000
size = "3 MiB"
```
Describe the board in a manifest that names its layout and images:
```toml
# boards/esp32-s3.toml
[[board]]
identifier = "esp32-s3-devkitc"
protocol = "espressif"
chip = "esp32-s3"
layout = "../layouts/esp32-s3-8mb.toml"
application-devices = [{ vendor = 0x303a, product = 0x1001 }]
[[board.image]]
partition = "bootloader"
path = "../build/bootloader.bin"
[[board.image]]
partition = "application"
path = "../build/application.bin"
```
Then flash it:
```console
$ nanoflash flash --board boards/esp32-s3.toml
connecting
writing at 0x0
writing at 0x8000
writing at 0x10000
verifying
resetting
flashed esp32-s3-devkitc (esp32-s3, 8 MiB)
0x0 bootloader 21 KiB
0x8000 partition-table 3 KiB
0x10000 application 812 KiB
verified
```
nanoflash found the port, generated the partition table from the layout, wrote and verified every
image. `nanoflash monitor` shows the serial output, and `runner = "nanoflash run"` in
`.cargo/config.toml` makes `cargo run` do both. The [first
tutorial](https://alixanneraud.github.io/nanoflash/tutorials/flash-esp32-s3.html) walks through a
full example.
## Supported boards
| Espressif | ESP32, ESP32-S2, ESP32-S3, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2, ESP32-P4 | ROM loader |
| Arduino SAM-BA | Nano 33 BLE (nRF52840), SAMD21 boards such as the MKR WiFi 1010 | SAM-BA |
| Arduino AVR | Uno, Nano, Pro Mini (ATmega328P) | STK500v1 |
| Arduino AVR | Mega 2560 (ATmega2560) | STK500v2 |
| Arduino AVR | Leonardo, Micro, Pro Micro (ATmega32U4) | AVR109 |
USB DFU, UF2, CMSIS-DAP and Nordic serial DFU come next. See [supported boards and
protocols](https://alixanneraud.github.io/nanoflash/reference/supported-boards.html).
## Flash from the browser
The [flasher page](https://alixanneraud.github.io/nanoflash/flasher/) flashes a board from a
manifest URL or from files on your computer, in Chrome, Edge or Opera, with nothing to install. To
put a flash button on your own page:
```html
<script type="module">import "nanoflash/button";</script>
<nanoflash-button manifest="firmware/manifest.json" label="Install"></nanoflash-button>
```
See [use the browser package](https://alixanneraud.github.io/nanoflash/how-to/use-the-browser-package.html).
## Documentation
The full guide lives at **<https://alixanneraud.github.io/nanoflash/>**:
- **Tutorials**: [flash your first ESP32-S3](https://alixanneraud.github.io/nanoflash/tutorials/flash-esp32-s3.html),
[flash and monitor a Nano 33 BLE with cargo run](https://alixanneraud.github.io/nanoflash/tutorials/cargo-run-nano-33-ble.html),
[add a flash button to a web page](https://alixanneraud.github.io/nanoflash/tutorials/flash-button.html).
- **How-to guides**: [install](https://alixanneraud.github.io/nanoflash/how-to/install.html),
[write a layout](https://alixanneraud.github.io/nanoflash/how-to/write-a-layout.html),
[write a board manifest](https://alixanneraud.github.io/nanoflash/how-to/write-a-board-manifest.html),
[flash a board](https://alixanneraud.github.io/nanoflash/how-to/flash-a-board.html),
[erase](https://alixanneraud.github.io/nanoflash/how-to/erase-the-flash.html),
[monitor](https://alixanneraud.github.io/nanoflash/how-to/monitor-the-serial-output.html),
[decode defmt](https://alixanneraud.github.io/nanoflash/how-to/decode-defmt.html),
[use the Cargo runner](https://alixanneraud.github.io/nanoflash/how-to/use-the-cargo-runner.html),
[generate memory.x](https://alixanneraud.github.io/nanoflash/how-to/generate-memory-x.html),
[migrate from esptool, bossac or avrdude](https://alixanneraud.github.io/nanoflash/how-to/migrate.html).
- **Reference**: [layout file](https://alixanneraud.github.io/nanoflash/reference/layout-file.html),
[board manifest](https://alixanneraud.github.io/nanoflash/reference/board-manifest.html),
[command line](https://alixanneraud.github.io/nanoflash/reference/command-line.html),
[errors](https://alixanneraud.github.io/nanoflash/reference/errors.html),
[library](https://alixanneraud.github.io/nanoflash/reference/library.html),
[browser API](https://alixanneraud.github.io/nanoflash/reference/browser-api.html).
- **Explanation**: [how nanoflash works](https://alixanneraud.github.io/nanoflash/explanation/how-it-works.html),
[checks before the first erase](https://alixanneraud.github.io/nanoflash/explanation/checks-before-erase.html),
[growable partitions](https://alixanneraud.github.io/nanoflash/explanation/growable-partitions.html),
[design decisions](https://alixanneraud.github.io/nanoflash/explanation/design-decisions.html),
[prior art](https://alixanneraud.github.io/nanoflash/explanation/prior-art.html).
Its source is in [`docs/src`](docs/src/SUMMARY.md).
## Status
Layout checks, the table writers and flashing work natively and in the browser. The monitor and
the Cargo runner work in the command. Flashing is tested against recorded transcripts, Espressif's QEMU, arduino/BOSSA's
emulated SAM-BA bootloader and the Arduino bootloaders emulated by simavr. The release workflow
attaches the binaries of the command to its GitHub releases. The crates `nanoflash` and
`nanoflash-cli` are on crates.io, and the browser package `nanoflash` is on npm.
## Contributing
Issues and pull requests are welcome. Commits follow [Conventional
Commits](https://www.conventionalcommits.org). See [develop and
contribute](https://alixanneraud.github.io/nanoflash/how-to/develop.html) and `AGENTS.md`.
## Licence
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without
any additional terms or conditions.