pureboot: every libavr chip — 37 loaders, the m48 class, the 644 geometry
The chip table becomes family blocks covering all 37 targets. The m48s are a new deployment class: no boot section, so the tiny profile spoken over the hardware USART — host-patched reset vector, trampoline hand-over, a 510-byte budget (474 B built), no fuse preflight — while their RWWSRE store stays the buffer discard (Atmel-8271 §26.2); the device keys the patch flag and the CPU-halt waits on the curated boot-section capability and the discard on the RWWSRE bit itself. The 644s' 64 KiB is exactly the 16-bit byte space: plain LPM, byte wire addresses, 498 B in a 512-byte slot — and their 1 KiB minimum boot section holds the resident and staging slots together, so self-update needs no fuse step (the update test's slot pick now keys word-flash on base >= 64 KiB; base + slot merely touching the boundary stays byte-addressed). The 1284 joins the 1284P's word-addressed 1 KiB slot at 558 B. BOOT_FUSE gains every boot-sectioned family's ladder and fuse byte; the planner exercises them all. The sim scaffolding keys patch-vector-ness instead of the atmega name prefix, the fixture app picks its clock by family (the tiny25/45/13 builds surfaced the 16 MHz fallthrough as garbled banners), and the runner's wrapped flash ioctl performs the m48 discard simavr's no-RWW cores turn into a stray buffer fill. Sizes across the fleet: 466-504 B megas, 474 B m48s, 498 B 644s, 488-502 B tinies, 558 B 1284s — every chip passing size/pi/planner/protocol/reloc/update. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,14 @@
|
||||
|
||||
A serial bootloader on [libavr](https://git.blackmark.me/avr/libavr), pure by
|
||||
constraint: one C++ source, no inline assembly, no global register variables
|
||||
(attributes allowed), built for every chip libavr targets, **fitting each
|
||||
chip's smallest boot sector**: 512 bytes everywhere — 488 B on the
|
||||
ATtiny13A, 502 B on the ATtiny85, 466–504 B across the megas — except the
|
||||
ATmega1284P, whose smallest boot sector is 1 KiB and whose far-flash
|
||||
(attributes allowed), built for **every chip libavr targets — all 37 —
|
||||
fitting each chip's smallest boot sector**: 512 bytes everywhere — 488 B on
|
||||
the tiny13s, 498–502 B on the tiny25/45/85, 466–504 B across the megas
|
||||
(474 B on the boot-section-less m48s, 498 B on the 644s) — except the
|
||||
ATmega1284/1284P, whose smallest boot sector is 1 KiB and whose far-flash
|
||||
machinery (ELPM reads, RAMPZ page commands, word-addressed wire) lands at
|
||||
558 B in a 1 KiB slot: the 512-byte figure is a hardware boundary that chip
|
||||
simply does not have, and no implementation of this feature set fits it
|
||||
558 B in a 1 KiB slot: the 512-byte figure is a hardware boundary those
|
||||
chips simply do not have, and no implementation of this feature set fits it
|
||||
there. The device speaks primitives; every composite — verify, erase,
|
||||
reset-vector surgery, updating the loader itself — lives in the host tool
|
||||
(`pureboot.py`).
|
||||
@@ -29,19 +30,20 @@ belongs to the host-managed trampoline (below).
|
||||
|
||||
| Chip | Serial | Baud | Clock assumed |
|
||||
|---|---|---|---|
|
||||
| every ATmega (8/8A, 16, 32/32A, 168A, 328P, 1284P) | the hardware USART (USART0), RXD/TXD per pinout | 115200 8N1 | 16 MHz crystal |
|
||||
| ATtiny85 | software UART, RX = PB0, TX = PB1 | 57600 8N1 | 8 MHz internal RC |
|
||||
| ATtiny13A | software UART, RX = PB0, TX = PB1 | 57600 8N1 | 9.6 MHz internal RC |
|
||||
| every ATmega | the hardware USART (USART0), RXD/TXD per pinout | 115200 8N1 | 16 MHz crystal |
|
||||
| ATtiny25/45/85 | software UART, RX = PB0, TX = PB1 | 57600 8N1 | 8 MHz internal RC |
|
||||
| ATtiny13/13A | software UART, RX = PB0, TX = PB1 | 57600 8N1 | 9.6 MHz internal RC |
|
||||
|
||||
The tiny RX pin has its pull-up enabled; TX idles high. All multi-byte
|
||||
quantities on the wire are little-endian.
|
||||
|
||||
## Activation
|
||||
|
||||
Reset enters the loader (BOOTRST on the mega, the patched reset vector on the
|
||||
tinies) — except a watchdog reset, which hands straight to the application
|
||||
(the application owns its watchdog; it must clear WDRF itself, which also
|
||||
releases the WDRF-forced WDE).
|
||||
Reset enters the loader (BOOTRST on the boot-sectioned megas; the patched
|
||||
reset vector on the tinies and the boot-section-less m48s) — except a
|
||||
watchdog reset, which hands straight to the application (the application
|
||||
owns its watchdog; it must clear WDRF itself, which also releases the
|
||||
WDRF-forced WDE).
|
||||
|
||||
The host then has one activation window per awaited byte to knock: `p` then
|
||||
`b`. Each awaited byte gets a fresh window; any other byte is discarded and
|
||||
@@ -61,9 +63,10 @@ write to finish and sends the prompt `+` (0x2b) — the prompt is therefore
|
||||
also the completion ack of the previous command. A session is: await `+`,
|
||||
send a command, read its reply, repeat.
|
||||
|
||||
On chips whose flash exceeds 64 KiB (the 1284P — info-block flag bit 1) the
|
||||
On chips whose flash exceeds 64 KiB (the 1284s — info-block flag bit 1) the
|
||||
`R`/`W` flash addresses are **word** addresses; everywhere else they are byte
|
||||
addresses. EEPROM addresses are always bytes, counts always bytes.
|
||||
addresses (the 644s' 64 KiB is exactly the 16-bit byte space and stays
|
||||
byte-addressed). EEPROM addresses are always bytes, counts always bytes.
|
||||
|
||||
| Cmd | Arguments | Reply |
|
||||
|---|---|---|
|
||||
@@ -108,16 +111,23 @@ write `0xff` (per page for flash, per byte for EEPROM).
|
||||
|
||||
## Deployment
|
||||
|
||||
**Megas**: program the loader at `flash − slot` with an external programmer.
|
||||
Every mega has a BOOTSZ step whose boot section is exactly the loader slot —
|
||||
512 B, the m8/16/168A's second-smallest step, the m32/328P's smallest; on
|
||||
the m1284P that step is its smallest, 512 words, which is why its slot is
|
||||
1 KiB — so the ATmega328P profiles below apply to every mega with its own
|
||||
addresses and slot size; the per-chip BOOTSZ ladders live in the host tool
|
||||
(`BOOT_FUSE`). The 1284P's numbers: standalone = BOOTSZ 512 words (reset at
|
||||
the loader base 0x1fc00); self-update = 1024 words, covering both 1 KiB
|
||||
slots, the loader-first reset landing at 0x1f800 — the staging slot, walked
|
||||
across when erased.
|
||||
**Boot-sectioned megas**: program the loader at `flash − slot` with an
|
||||
external programmer. Every such mega has a BOOTSZ step whose boot section
|
||||
is exactly the loader slot — 512 B, the second-smallest step on the 8 KiB
|
||||
and 16 KiB chips (m8, m88, m16, m168, m164), the smallest on the 32 KiB
|
||||
ones (m32, m328, m324); on the 1284s that step is the smallest, 512 words,
|
||||
which is why their slot is 1 KiB — so the ATmega328P profiles below apply
|
||||
to every one of them with its own addresses and slot size; the per-chip
|
||||
BOOTSZ ladders live in the host tool (`BOOT_FUSE`). The 1284s' numbers:
|
||||
standalone = BOOTSZ 512 words (reset at the loader base 0x1fc00);
|
||||
self-update = 1024 words, covering both 1 KiB slots, the loader-first
|
||||
reset landing at 0x1f800 — the staging slot, walked across when erased.
|
||||
|
||||
The **644s** are the geometry's sweet spot: their smallest boot section
|
||||
(512 words = 1 KiB) is exactly *two* 512-byte slots, so the resident and
|
||||
its staging slot both live inside the minimum section — self-update needs
|
||||
no fuse step up, and the standalone profile does not exist (reset lands at
|
||||
0xfc00, one erased slot below the loader: the loader-first walk built in).
|
||||
|
||||
ATmega328P profiles (addresses for its 32 KiB):
|
||||
|
||||
@@ -130,15 +140,18 @@ ATmega328P profiles (addresses for its 32 KiB):
|
||||
Applications are flashed unmodified — word 0 stays the application's own
|
||||
reset vector, and the hand-over jumps to 0.
|
||||
|
||||
**Tinies** (no boot section): program the loader at `flash − 512`; erased
|
||||
flash below it walks up into the loader, so a virgin chip activates. When
|
||||
flashing an application the host performs reset-vector surgery: word 0 is
|
||||
rewritten to `rjmp` to the loader base, and the application's own entry is
|
||||
re-encoded as a trampoline `rjmp` in the word just below the loader
|
||||
(`base − 2`, where the hand-over jumps). Every other vector stays the
|
||||
application's. The patched page 0 and the trampoline page are written
|
||||
*first*, so from the first write on an interrupted flash still resets into
|
||||
the loader; an erase runs top-down for the same reason.
|
||||
**Patched-vector chips — the tinies and the m48s** (no boot section; the
|
||||
m48s' SPM runs from the entire flash, Atmel-8271 §26): program the loader
|
||||
at `flash − 512`; erased flash below it walks up into the loader, so a
|
||||
virgin chip activates. When flashing an application the host performs
|
||||
reset-vector surgery: word 0 is rewritten to `rjmp` to the loader base, and
|
||||
the application's own entry is re-encoded as a trampoline `rjmp` in the
|
||||
word just below the loader (`base − 2`, where the hand-over jumps). Every
|
||||
other vector stays the application's. The patched page 0 and the trampoline
|
||||
page are written *first*, so from the first write on an interrupted flash
|
||||
still resets into the loader; an erase runs top-down for the same reason.
|
||||
The m48s speak this profile over their hardware USART — no fuse preflight,
|
||||
BOOTRST does not exist there.
|
||||
|
||||
## Updating the loader
|
||||
|
||||
@@ -148,16 +161,18 @@ loader itself as its own staging loader. The image is the loader's own 512
|
||||
bytes as a raw binary, or the Intel HEX the build emits beside it, which
|
||||
links the loader at its base inside an otherwise blank flash image:
|
||||
|
||||
1. The staging slot `[base−512, base)` is saved to a host-side state file
|
||||
(on the 1 KB tiny13A that is the whole application, vectors included).
|
||||
2. The resident installs the identical update image there. On the tinies the
|
||||
host composes the slot's last word — the same address as the resident's
|
||||
trampoline — as a jump to the resident base, so even an abandoned staging
|
||||
copy times out into a loader, never into garbage.
|
||||
1. The staging slot `[base−slot, base)` is saved to a host-side state file
|
||||
(on the 1 KB tiny13s that is the whole application, vectors included).
|
||||
2. The resident installs the identical update image there. On the
|
||||
patched-vector chips the host composes the slot's last word — the same
|
||||
address as the resident's trampoline — as a jump to the resident base,
|
||||
so even an abandoned staging copy times out into a loader, never into
|
||||
garbage.
|
||||
3. `J` enters the staging copy, which rewrites the resident slot. On the
|
||||
t85 the host first re-aims word 0 at the staging copy, so a power loss
|
||||
mid-rewrite still resets into a loader; on the t13a the staging slot
|
||||
carries the reset vector itself.
|
||||
patched-vector chips whose staging slot sits away from page 0 the host
|
||||
first re-aims word 0 at the staging copy, so a power loss mid-rewrite
|
||||
still resets into a loader; on the tiny13s the staging slot carries the
|
||||
reset vector itself.
|
||||
4. `J` enters the new resident, which restores the staging slot's saved
|
||||
content (word 0 and the trampoline with it) and the state file is
|
||||
discarded.
|
||||
@@ -166,9 +181,10 @@ Every phase is idempotent and keyed off the actual flash state: re-running
|
||||
the same command after any interruption resumes and completes. The state
|
||||
file carries the only bytes not recoverable from the device; if it is lost
|
||||
mid-update the update still completes, and the staging region is restored by
|
||||
reflashing the application. The mega needs its fuses for the preflight
|
||||
(BOOTSZ gate, profile notes) — read from the device, or supplied with
|
||||
`--assume-fuses` where reading is impossible (simulators).
|
||||
reflashing the application. A boot-sectioned mega needs its fuses for the
|
||||
preflight (BOOTSZ gate, profile notes) — read from the device, or supplied
|
||||
with `--assume-fuses` where reading is impossible (simulators); the
|
||||
patched-vector chips need none.
|
||||
|
||||
## Host tool
|
||||
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
// resident — how pureboot updates itself, host-driven, with no other
|
||||
// firmware involved.
|
||||
//
|
||||
// Entry: reset lands in avr::startup::entry below (BOOTRST on the mega; the
|
||||
// patched reset vector — or erased flash walking up into the loader — on the
|
||||
// tinies). A watchdog reset hands straight to the application. Otherwise the
|
||||
// Entry: reset lands in avr::startup::entry below (BOOTRST on the
|
||||
// boot-sectioned megas; the patched reset vector — or erased flash walking
|
||||
// up into the loader — on the tinies and the boot-section-less m48s). A
|
||||
// watchdog reset hands straight to the application. Otherwise the
|
||||
// host has one activation window per awaited knock byte ("pb"); an idle line
|
||||
// boots the application. A session then stays in the command loop until 'J'
|
||||
// jumps away or the chip resets.
|
||||
@@ -44,9 +45,10 @@ constexpr std::uint8_t ack = '+';
|
||||
// from code.
|
||||
consteval avr::hertz_t clock()
|
||||
{
|
||||
if (avr::hw::db.name == "ATtiny13A")
|
||||
auto name = std::string_view{avr::hw::db.name};
|
||||
if (name.starts_with("ATtiny13"))
|
||||
return 9.6_MHz;
|
||||
if (avr::hw::db.name == "ATtiny85")
|
||||
if (name.starts_with("ATtiny"))
|
||||
return 8_MHz;
|
||||
return 16_MHz;
|
||||
}
|
||||
@@ -62,21 +64,19 @@ consteval std::int16_t wdrf_field()
|
||||
|
||||
// Geometry: the resident loader owns the top slot of flash — 512 bytes,
|
||||
// except on the >64 KiB chips whose own smallest boot sector is 1 KiB (the
|
||||
// 1284P): there the slot is 1 KiB, matching the hardware boundary the
|
||||
// 1284s): there the slot is 1 KiB, matching the hardware boundary the
|
||||
// 512-byte figure comes from everywhere else. The word below the slot is
|
||||
// the trampoline (the application's relocated reset vector) on chips
|
||||
// without a hardware boot section. The RWWSRE bit marks a separate boot
|
||||
// section — on classic AVR the two capabilities coincide (the m8/m32 packs
|
||||
// spell its register SPMCR).
|
||||
// without a hardware boot section — the tinies and the m48s, whose SPM
|
||||
// runs from anywhere (Atmel-8271 §26). A boot section also means the CPU
|
||||
// runs on while the RWW section programs; everywhere else it halts through
|
||||
// the operation. The m48s still carry RWWSRE as their temporary-buffer
|
||||
// discard (§26.2), so the discard picks by that bit, not by the section.
|
||||
constexpr std::uint16_t slot_bytes = spm::flash_bytes > 65536 ? 1024 : 512;
|
||||
constexpr std::uint32_t base = spm::flash_bytes - slot_bytes;
|
||||
constexpr std::uint16_t page = spm::page_bytes;
|
||||
constexpr bool boot_section = [] {
|
||||
for (auto reg : {"SPMCSR", "SPMCR"})
|
||||
if (avr::hw::db.field_index(reg, "RWWSRE") >= 0)
|
||||
return true;
|
||||
return false;
|
||||
}();
|
||||
constexpr bool boot_section = avr::hw::curated::has_boot_section();
|
||||
constexpr bool rww_discard = spm::detail::has_rww();
|
||||
|
||||
// Past 64 KiB a byte address no longer fits the wire's 16 bits, so on the
|
||||
// large chips every flash address on the wire — and all slot arithmetic —
|
||||
@@ -85,7 +85,8 @@ constexpr bool boot_section = [] {
|
||||
// 2 x 256 words), so the slot index is the high byte with its low bit
|
||||
// dropped everywhere.
|
||||
constexpr bool word_flash = spm::flash_bytes > 65536;
|
||||
constexpr std::uint16_t wire_base = word_flash ? static_cast<std::uint16_t>(base / 2) : static_cast<std::uint16_t>(base);
|
||||
constexpr std::uint16_t wire_base =
|
||||
word_flash ? static_cast<std::uint16_t>(base / 2) : static_cast<std::uint16_t>(base);
|
||||
constexpr std::uint16_t wire_page_mask = word_flash ? (page / 2 - 1) : (page - 1);
|
||||
|
||||
// The activation window, in seconds, is a compile-time constant (the build
|
||||
@@ -332,9 +333,10 @@ void program_flash(std::uint16_t wire_address, std::uint8_t slot_high)
|
||||
{
|
||||
// A buffer word cannot be loaded twice without an erase (§26.2.1), so a
|
||||
// refused page's drained data must not linger for the next write:
|
||||
// discard the buffer up front — CTPB on the tinies; on the mega writing
|
||||
// RWWSRE aborts a pending load (§26.2.2).
|
||||
if constexpr (boot_section)
|
||||
// discard the buffer up front — CTPB on the tinies; on the megas
|
||||
// writing RWWSRE aborts a pending load (§26.2.2 — on the m48s that
|
||||
// flush is the bit's whole documented job).
|
||||
if constexpr (rww_discard)
|
||||
spm::rww_enable<off>();
|
||||
else
|
||||
spm::clear_buffer<off>();
|
||||
@@ -376,8 +378,9 @@ void program_flash(std::uint16_t wire_address, std::uint8_t slot_high)
|
||||
page_high = static_cast<std::uint8_t>(address >> 8) & 0xfe;
|
||||
}
|
||||
if (page_high != slot_high) {
|
||||
// The tinies halt the CPU through the erase and the write, so only
|
||||
// the megas — running on while their RWW section programs — wait.
|
||||
// The tinies and the m48s halt the CPU through the erase and the
|
||||
// write, so only the boot-sectioned megas — running on while their
|
||||
// RWW section programs — wait.
|
||||
spm::erase_page<off>(address);
|
||||
if constexpr (boot_section)
|
||||
spm::wait();
|
||||
@@ -416,8 +419,8 @@ void send_fuses()
|
||||
// program_flash refuses this one slot and the info block is addressed
|
||||
// from it, so both follow wherever the code was flashed.
|
||||
const std::uint16_t ra_words = reinterpret_cast<std::uint16_t>(__builtin_return_address(0));
|
||||
const std::uint8_t slot_high = word_flash ? static_cast<std::uint8_t>(ra_words >> 8) & 0xfe
|
||||
: static_cast<std::uint8_t>((ra_words >> 8) << 1);
|
||||
const std::uint8_t slot_high =
|
||||
word_flash ? static_cast<std::uint8_t>(ra_words >> 8) & 0xfe : static_cast<std::uint8_t>((ra_words >> 8) << 1);
|
||||
|
||||
// The knock: 'p' then 'b', each under a fresh window; any other byte is
|
||||
// line noise and waits again. Falling out of a window runs the app.
|
||||
|
||||
@@ -518,15 +518,34 @@ def covered(pages, info, skip_blank):
|
||||
|
||||
# Per-chip boot fuse geometry, keyed by the signature's family/part bytes:
|
||||
# which byte of the 'F' reply (low, lock, extended, high) carries BOOTSZ/
|
||||
# BOOTRST, and the BOOTSZ->words ladder. Sources: Atmel-2486/2466/2503
|
||||
# (HIGH fuse), Atmel-8271 (m168A: EXTENDED; m328P: HIGH), Atmel-42719.
|
||||
# BOOTRST, and the BOOTSZ->words ladder. A die revision shares its base
|
||||
# signature, so one row covers it. The m48s have no boot section and no
|
||||
# row — their info block says patch-vector and this table is never
|
||||
# consulted. Sources: Atmel-2486/2466/2503 (HIGH fuse), Atmel-2545/8271/
|
||||
# DS40002065 (x8: EXTENDED, except the m328s' HIGH), Atmel-8272/8011/2593/
|
||||
# 42719 (x4: HIGH).
|
||||
_LADDER_128 = {0b11: 128, 0b10: 256, 0b01: 512, 0b00: 1024}
|
||||
_LADDER_256 = {0b11: 256, 0b10: 512, 0b01: 1024, 0b00: 2048}
|
||||
_LADDER_512 = {0b11: 512, 0b10: 1024, 0b01: 2048, 0b00: 4096}
|
||||
BOOT_FUSE = {
|
||||
bytes((0x93, 0x07)): (3, {0b11: 128, 0b10: 256, 0b01: 512, 0b00: 1024}), # m8/8A
|
||||
bytes((0x94, 0x03)): (3, {0b11: 128, 0b10: 256, 0b01: 512, 0b00: 1024}), # m16
|
||||
bytes((0x95, 0x02)): (3, {0b11: 256, 0b10: 512, 0b01: 1024, 0b00: 2048}), # m32/32A
|
||||
bytes((0x94, 0x06)): (2, {0b11: 128, 0b10: 256, 0b01: 512, 0b00: 1024}), # m168A
|
||||
bytes((0x95, 0x0F)): (3, {0b11: 256, 0b10: 512, 0b01: 1024, 0b00: 2048}), # m328P
|
||||
bytes((0x97, 0x05)): (3, {0b11: 512, 0b10: 1024, 0b01: 2048, 0b00: 4096}), # 1284P
|
||||
bytes((0x93, 0x07)): (3, _LADDER_128), # m8/8A
|
||||
bytes((0x94, 0x03)): (3, _LADDER_128), # m16/16A
|
||||
bytes((0x95, 0x02)): (3, _LADDER_256), # m32/32A
|
||||
bytes((0x93, 0x0A)): (2, _LADDER_128), # m88/88A
|
||||
bytes((0x93, 0x0F)): (2, _LADDER_128), # m88P/88PA
|
||||
bytes((0x94, 0x06)): (2, _LADDER_128), # m168/168A
|
||||
bytes((0x94, 0x0B)): (2, _LADDER_128), # m168P/168PA
|
||||
bytes((0x95, 0x14)): (3, _LADDER_256), # m328
|
||||
bytes((0x95, 0x0F)): (3, _LADDER_256), # m328P
|
||||
bytes((0x94, 0x0F)): (3, _LADDER_128), # m164A
|
||||
bytes((0x94, 0x0A)): (3, _LADDER_128), # m164P/164PA
|
||||
bytes((0x95, 0x15)): (3, _LADDER_256), # m324A
|
||||
bytes((0x95, 0x08)): (3, _LADDER_256), # m324P
|
||||
bytes((0x95, 0x11)): (3, _LADDER_256), # m324PA
|
||||
bytes((0x96, 0x09)): (3, _LADDER_512), # m644/644A
|
||||
bytes((0x96, 0x0A)): (3, _LADDER_512), # m644P/644PA
|
||||
bytes((0x97, 0x06)): (3, _LADDER_512), # m1284
|
||||
bytes((0x97, 0x05)): (3, _LADDER_512), # m1284P
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user