Skip to content

eboot - keep the copy command in flash, so a power cut resumes the copy - #9354

Open
adamsthws wants to merge 2 commits into
esp8266:masterfrom
adamsthws:eboot-copy-journal
Open

adamsthws wants to merge 2 commits into
esp8266:masterfrom
adamsthws:eboot-copy-journal

Conversation

@adamsthws

Copy link
Copy Markdown

Fixes #905. Related: #6538. Depends on #9353, which frees the room in
sector 0 that this change uses.

I maintain firmware-liberator,
a tool that moves ESP32 and Beken devices off their stock firmware and onto
Tasmota or ESPHome over the air. ESP8266 support is in progress. Its tests
cut the power, in an emulator, at every step of each update. That is how
this gap showed up. The fixes the project has sent to other projects are
listed in its
upstream contributions.

The problem and the fix

Updater gives eboot its copy command in RTC memory, and a power cut
clears RTC memory. If the power fails while eboot copies the update over the
sketch, the device has half a sketch and no command to finish it. It does
not start again until it is flashed over serial.

With this change, eboot writes the command to a journal in flash before it
writes anything else. After it writes each destination sector, it marks that
sector in the journal. If eboot starts with no command in RTC memory but
finds an open journal, it continues the copy from the first unmarked sector.
A watchdog reset during the copy also resumes from the journal, so eboot
does not start the copy again from a source that it may have overwritten.

A raw copy resumes to exactly the result of an uninterrupted copy, even
when the destination overlaps the source. A gzip copy must inflate again
from the start of its stream, so it resumes only while no written sector
lies inside the compressed source. After that point, eboot stops without
writing anything and starts what is there, as master does. eboot decides
once whether the copy is gzip and keeps that in the journal, so a resume
after the 1f 8b signature is overwritten does not copy compressed bytes
raw.

Compatibility

  • The RTC command still starts an update. Updater and
    eboot_command.h do not change.
  • The journal takes no flash from sketches. No linker script, boards.txt
    or FlashMap.h changes.
  • eboot keeps no journal, and copies as master does, when the chip's ID
    gives no size between 512K and 16M, when the image header names a larger
    flash than the ID, when the source reaches the journal's sector, or when
    the journal's command does not read back. If a copy runs past 991
    sectors, or its destination reaches the journal's sector, eboot closes
    the journal and copies the rest without it.

Where the journal is

The SDK keeps its Wi-Fi settings in the last three sectors of flash: two
copies and a flag sector that names the live copy. I disassembled every SDK
the core ships (2.2.1, the 2.2.x builds 190313 to 191122, and 3.0.5). In
each, user_start and system_param_load read only the copy that the flag
names, and the SDK erases the other copy before it saves to it. The journal
goes in that other copy, for example at 0x3FD000 or 0x3FE000 on a 4M chip.
Every layout that boards.txt.py generates keeps EEPROM, RF cal and the
SDK settings in the last 20 KB. eboot gets the flash size from the chip's
JEDEC ID, as FLASH_MAP_SUPPORT does, and not from the image header,
because an autoflash build's header always says 16M. eboot raises the ROM's
flashchip->chip_size to the ID's size, as esptool's flasher stub does, so
that the ROM's SPI calls reach the top of the chip. The SDK sets it again
from the header when the sketch starts.

struct journal {
    struct eboot_command cmd; // the command, magic and crc32 included
    uint32_t closed;          // 0xffffffff while copying, then 0
    uint32_t done[];          // per destination sector: 0xffffffff, 0 once written
};

eboot writes each word of the journal once between erases. It never clears
more bits in a word that is already programmed, which was the concern for
PUYA chips in #6538.

Guards

  • Chip size: when the header names a larger flash than the ID (except
    the 16M autoflash header), eboot keeps no journal. An ID that
    under-reports the size would otherwise put the journal inside the sketch
    or the FS.
  • Read-back: eboot erases the sector, writes the command, reads its CRC
    back, and only then clears the RTC command. If the CRC does not read
    back, the RTC command stays and the copy runs as on master. If the write
    or the read-back reports an error, eboot also closes the journal, so the
    next power-on does not redo a finished copy.
  • Serial upload after a power cut: before a resume writes anything,
    eboot checks that the last marked sector still holds what the copy wrote.
    If an upload changed that sector, eboot closes the journal and the upload
    starts, as on master. If the upload ends below that sector, eboot
    finishes the copy above it and the upload still starts.

Size

.text .rodata sector-0 image (+8: elf2bin keeps 8 bytes free) spare of 4096
master 3408 40 3488 (3496) 600
#9353 3204 40 3280 (3288) 808
this PR, on #9353 3716 63 3808 (3816) 280
this PR, on master alone 3940 63 4032 (4040) 56

eboot.elf was rebuilt with the toolchain that tools/get.py installs,
and tests/ci/eboot_test.sh passes.

Testing

All tests ran in an emulator,
bekensim (C++ build,
3afc3a6) in its ESP8266 mode. It boots the flash through the ROM into
eboot, keeps RTC memory across a soft restart, and loses it at power-on.
Each flash has the layout of the matching Arduino board. The old sketch (A)
writes the RTC command as Updater::end() does, and the update (B) is
staged where Updater::begin() puts it. Each case runs the update once and
records every flash erase and program. Then, for each operation in turn, it
cuts the power at that operation, powers on with RTC memory empty, and
compares the flash byte for byte with A and with B. "Neither" means that
the flash held neither sketch whole, so the device does not start.

Case Copy Power cuts B started A started Neither
master, raw, 1M 75 sectors 148 of 148 0 0 148
master, gzip, 1M 160 sectors from about 490 KB, overlapping 318 of 318 0 0 318
raw, 1M 75 sectors 226 of 226 224 2 0
raw, 1M 128 sectors, overlapping 385 of 385 383 2 0
gzip, 1M 75 sectors from about 230 KB 113 of 226 (every 2nd) 112 1 0
gzip, 1M 160 sectors from about 490 KB, overlapping 481 of 481 344 2 135
raw, 512K 48 sectors 145 of 145 143 2 0
raw, 2M 144 sectors, overlapping 433 of 433 431 2 0
gzip, 4M 96 sectors from about 295 KB 145 of 289 (every 2nd) 144 1 0
raw, 16M header on a 1M chip 75 sectors 226 of 226 224 2 0

A starts only when the power cut hits the journal's own erase or command
write. The journal is not valid yet and the destination is untouched, so
the update is lost, as on master. In the overlapping gzip copy, every power
cut up to operation 343 resumed to a whole B, and every one from 344 to 478
did not, because the destination had reached the compressed source. That
leaves 45 of 160 sectors unprotected, where master leaves all 160. The SDK
settings, RF cal, FS and EEPROM stayed byte for byte the same in every case
whose destination does not cover them, with all three flag values (0, 1,
0xFF).

With datasheet timings (Winbond W25Q80DV, about 56 ms per sector), a power
cut on master leaves a dead device anywhere in about 4.1 s of the raw copy
and 8.9 s of the overlapping gzip copy. With this change, no part of the raw
copies is exposed, and about 2.5 s of the overlapping gzip copy remains.

Further runs covered the other paths. Where eboot keeps no journal (source
at the journal's sector, a 4M header on a 1M ID, write-protected journal
sectors), every power cut left what master leaves, and eboot did not write
the journal's sectors. After each power cut, a serial upload started in 226
of 226 cases for a 704 KB upload, 223 of 226 for a 128 KB upload, and 111
of 113 for a gzip copy. In the other cases eboot finished the update over
the upload (see the limits). In 150 double power cuts, one during the copy
and one during the resume, every pair ended with B whole and started,
except 12 gzip pairs whose second cut fell in the unprotected tail.
Test-only shims also covered a soft reset during the copy and a command
write that reports an error; in both, B started. I made the last change
(closing the journal after a write error) after the full run, so I cut
every operation of the main cases again on the final build. Every result
agreed with the earlier run.

Limits

  • No real hardware. The emulator models a cut erase as an all-0xff
    sector and a cut program as half written. Real chips can leave cells
    partly programmed or erased. The write-once journal and the command's CRC
    are meant for that, but they are untested on silicon, as are the RDID
    read and the chip_size change on 8M and 16M chips. The under-reporting
    ID, the write protection, the write error and the soft reset were shims
    around eboot's flash calls.
  • Sector 0. eboot rewrites sector 0 whenever its bytes differ from the
    update's: when the update carries a different eboot, or after a serial
    upload whose header esptool changed. A power cut between its erase and
    its program leaves nothing to start. The eboot before this one copies
    the update that brings this one, so that update is not protected. An
    update that brings master's eboot back is unprotected from the moment
    sector 0 holds it.
  • A gzip copy whose destination grows into its source is unprotected
    from the moment it writes there.
  • A serial upload after a power cut can be overwritten by the update
    when the cut came before the copy marked a sector past sector 0, or when
    the upload holds the same bytes as the update in the last marked sector.
    The device then starts the update, the upload or, rarely, a mix of the
    two. The readme now says to erase the flash with such an upload.
  • A wrong flash ID. A genuine 16M build on a chip whose ID
    under-reports its size is not guarded. On a chip whose ID over-reports
    and that wraps addresses, a copy over the real SDK sectors is not seen to
    reach the journal.

Compared with #6538

#6538 (last commit December 2020, now in conflict) keeps commands in slots
in a sector reserved between the sketch and the FS, which needs changes to
the linker scripts and boards.txt.py. A slot stays pending until its copy
ends, so a cut copy starts again from the beginning. Its last comments
(April 2022) say that the author meant to redo it. This PR keeps the RTC
interface and the flash layout, records progress per sector, and fits in
sector 0 alongside gzip, with 280 bytes to spare on top of #9353, or 56
on master alone.

copy_raw() kept its 4 KB sector buffer and the uzlib state on its stack.
Addressing them there, past the reach of the stack-relative loads and
stores, takes more code than addressing a global. They are now globals,
as buffer2 and gzip_dict already are. Both are written before they are
read, so the uncleared BSS does not matter.

The sector-0 image goes from 3488 to 3280 bytes (3288 of its 4096 with
elf2bin's trailer).
Updater leaves eboot its command in RTC memory, which a power cut
clears. A cut while eboot copies the staged update over the sketch left
half a sketch and no command to finish it: a device that does not start
(esp8266#905).

Before it writes anything, eboot now keeps the command in a journal in
flash, and marks each destination sector there once it is written. A
start with no RTC command but an open journal carries the copy on from
the first sector not marked. The RTC command stays the way in: a sketch
that writes it as before is copied as before, and the journal only adds
the resume.

- Where: the SDK keeps its Wi-Fi settings in the last three sectors, two
  copies and a flag whose first byte names the live one (0: the first),
  in every NONOS SDK the core ships (their user_start and
  system_param_load). The journal takes the other copy, which the SDK
  never reads and erases before it next saves there. The end of the
  flash is the chip's own, from its JEDEC id as FLASH_MAP_SUPPORT finds
  it, since an autoflash build's header says 16M whatever the chip. The
  ROM's flash descriptor is raised to that size, as esptool's stub does,
  so its SPI calls reach the top of a large chip; it is never lowered.
- Each journal word is written once between erases, as PUYA chips need:
  the command, a word per destination sector, and a closed word. The
  command's CRC is read back, and only then is the RTC command cleared: a
  write that silently failed leaves the copy to the RTC command, as
  before. If the write or the read-back reports an error once the sector
  is erased, the closed word is written, so a command that may have
  landed whole is never left open for a later start to redo.
- A raw copy resumes where it stopped and writes what the uninterrupted
  copy would have, overlapping regions included: every source sector a
  resumed sector reads was overwritten, or not, just as it would have
  been. A gzip copy inflates again from the head of its stream, so it
  resumes only while no sector written so far lies in the compressed
  source; past that it stops (5, 6 or 7) before writing anything, and
  the device starts what is there, as before. Whether a copy inflates is
  now decided once, before the journal is written, and kept in its copy
  of the command (args[3]): a resumed copy may have written over the
  gzip signature, and would otherwise copy the compressed bytes raw.
- Before a resume writes anything, the last sector the journal marked
  must still hold what the copy wrote there. If a serial upload made
  since the cut changed it, the journal is closed (8) and the upload
  starts, as it would have without the journal; if the upload ends below
  it, the copy is finished above the upload, and the upload still
  starts. An upload that holds the same bytes there as the update (a cut
  before the copy passed sector 0, say) is not told apart.
- No journal is kept (the copy goes on as before) on a chip whose id
  gives no size from 512K to 16M, when the image header names a larger
  flash than the id (16M, an autoflash build's, aside), or when the
  source reaches the journal's sector; the journal is closed and the copy
  goes on unjournalled past 991 sectors, or if it reaches the journal's
  sector.
- A watchdog reset during the copy (RTC kept) now carries the copy on
  from the journal too, rather than starting it again over an overlapping
  source.

copy_raw() is now static, so the compiler drops its unused verify path,
and out of line, as inlining it into main() costs bytes. The sector-0
image grows from 3280 to 3808 bytes (3816 of its 4096, as elf2bin keeps
8 bytes free): 280 bytes spare.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OTA which survives power failure

1 participant