Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Updatergives eboot its copy command in RTC memory, and a power cutclears 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 8bsignature is overwritten does not copy compressed bytesraw.
Compatibility
Updaterandeboot_command.hdo not change.boards.txtor
FlashMap.hchanges.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_startandsystem_param_loadread only the copy that the flagnames, 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.pygenerates keeps EEPROM, RF cal and theSDK settings in the last 20 KB. eboot gets the flash size from the chip's
JEDEC ID, as
FLASH_MAP_SUPPORTdoes, and not from the image header,because an autoflash build's header always says 16M. eboot raises the ROM's
flashchip->chip_sizeto the ID's size, as esptool's flasher stub does, sothat the ROM's SPI calls reach the top of the chip. The SDK sets it again
from the header when the sketch starts.
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
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.
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.
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.rodataeboot.elfwas rebuilt with the toolchain thattools/get.pyinstalls,and
tests/ci/eboot_test.shpasses.Testing
All tests ran in an emulator,
bekensim (C++ build,
3afc3a6) in its ESP8266 mode. It boots the flash through the ROM intoeboot, 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) isstaged where
Updater::begin()puts it. Each case runs the update once andrecords 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.
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
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_sizechange on 8M and 16M chips. The under-reportingID, the write protection, the write error and the soft reset were shims
around eboot's flash calls.
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.
from the moment it writes there.
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.
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 copyends, 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.