diff --git a/bootloaders/eboot/eboot.c b/bootloaders/eboot/eboot.c index c3d0c278f7..00d74322aa 100644 --- a/bootloaders/eboot/eboot.c +++ b/bootloaders/eboot/eboot.c @@ -138,12 +138,89 @@ int uzlib_flash_read_cb(struct uzlib_uncomp *m) } unsigned char gzip_dict[32768]; -uint8_t buffer2[FLASH_SECTOR_SIZE]; // no room for this on the stack +uint8_t buffer[FLASH_SECTOR_SIZE]; // off the stack, like the others: copy_raw()'s +uint8_t buffer2[FLASH_SECTOR_SIZE]; // frame stays small, and so does its code +struct uzlib_uncomp m_uncomp; -int copy_raw(const uint32_t src_addr, - const uint32_t dst_addr, - const uint32_t size, - const bool verify) +/* + The copy journal. + + The command arrives in RTC memory, which a power cut clears. A cut while + copy_raw() overwrites the app would leave half an app and no command to + finish it. So before it writes anything, eboot keeps the command in a + journal in flash, and marks each destination sector there once written. + A start with no RTC command but an open journal carries the copy on. + + Where: the SDK keeps its settings in the last three sectors of the flash, + two copies and a flag whose first byte names the live one (0: the first). + 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 (its + JEDEC id), as FLASH_MAP_SUPPORT finds it, since an autoflash build's + header says 16M whatever the chip. + + Each word is written once between erases, as PUYA chips need. The command + is read back before the RTC command is cleared. A resume first checks + that the last sector marked still holds what the copy wrote there: if + not, the flash was written since (a serial upload), and the journal is + closed unused. +*/ +struct journal { + struct eboot_command cmd; // as it came, magic and crc32 included + uint32_t closed; // 0xffffffff while copying, then 0 + uint32_t done[]; // per destination sector: 0xffffffff, 0 once written +}; + +uint32_t journal; // the journal's flash address while it is open, else 0 + +struct flash_chip { + uint32_t device_id; + uint32_t chip_size; +}; +extern struct flash_chip *flashchip; // the ROM's: its SPI* calls stop at chip_size + +static uint32_t journal_find(void) +{ + volatile uint32_t *spi0 = (volatile uint32_t *)0x60000200; + spi0[0] = 1 << 28; // SPI_FLASH_RDID + while (spi0[0]) {} + const uint32_t bits = (spi0[16] >> 16) & 0xff; // capacity, log2 bytes, in W0 + if (bits < 19 || bits > 24) { // 512K to 16M + return 0; + } + // A header's size past the id's (16M, an autoflash build's, aside): + // the id under-reports, and the journal might lie in the build's flash + static const uint8_t header_bits[16] = {19, 18, 20, 21, 22, 21, 22, 0xff, 23, 0, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + if (header_bits[read_flash_byte(3) >> 4] > bits) { + return 0; + } + const uint32_t end = 1 << bits; + if (flashchip->chip_size < end) { + flashchip->chip_size = end; + } + return end - (read_flash_byte(end - FLASH_SECTOR_SIZE) ? 3 : 2) * FLASH_SECTOR_SIZE; +} + +const uint32_t zero = 0; + +// Clears a journal word: each is written once between erases +static void __attribute__((noinline)) journal_clear(const uint32_t addr) +{ + SPIWrite(addr, (void *)&zero, sizeof(zero)); +} + +static void journal_close(void) +{ + journal_clear(journal + offsetof(struct journal, closed)); + journal = 0; +} + +// Kept out of main(): inlined there, it takes a few more bytes +static int __attribute__((noinline)) copy_raw(const uint32_t src_addr, + const uint32_t dst_addr, + const uint32_t size, + const bool gzip, + const bool verify) { // require regions to be aligned if ((src_addr & 0xfff) != 0 || @@ -152,16 +229,16 @@ int copy_raw(const uint32_t src_addr, } const uint32_t buffer_size = FLASH_SECTOR_SIZE; - uint8_t buffer[buffer_size]; int32_t left = ((size+buffer_size-1) & ~(buffer_size-1)); uint32_t saddr = src_addr; uint32_t daddr = dst_addr; - struct uzlib_uncomp m_uncomp; - bool gzip = false; + // a gzip copy into its own source: a resume must not write there + bool check = gzip && dst_addr < src_addr + size; + uint32_t mark = journal + offsetof(struct journal, done); + int stale = 0; - // Check if we are uncompressing a GZIP upload or not - if ((read_flash_byte(saddr) == 0x1f) && (read_flash_byte(saddr + 1) == 0x8b)) { - // GZIP signature matched. Find real size as encoded at the end + if (gzip) { + // Find real size as encoded at the end left = read_flash_byte(saddr + size - 4); left += read_flash_byte(saddr + size - 3)<<8; left += read_flash_byte(saddr + size - 2)<<16; @@ -180,7 +257,6 @@ int copy_raw(const uint32_t src_addr, if (res != TINF_OK) { return 5; // Error uncompress header read } - gzip = true; } while (left > 0) { if (!gzip) { @@ -209,14 +285,44 @@ int copy_raw(const uint32_t src_addr, return 9; } } else { + uint32_t todo = 0xffffffff; + if (journal) { + if (mark == journal + FLASH_SECTOR_SIZE || daddr == journal) { + journal_close(); // too long to keep, or copying over it + } else { + SPIRead(mark, &todo, sizeof(todo)); + } + } + // A sector the journal has written already is skipped. Where a + // resumed copy starts writing, the last sector skipped must hold + // what the copy wrote there, or the flash is not the journal's. + // A gzip stream starts again from its head, which must be whole: + // so no sector written up to this one may lie in the source. + bool skip = (todo != 0xffffffff); + if (!skip) { + if (stale) { + return 8; + } + if (check) { + check = false; + if (daddr + buffer_size > src_addr) { + return 7; + } + } + } // Special treatment for address 0 (bootloader). Only erase and // rewrite if the data is different (i.e. very rarely). - bool skip = false; - if (daddr == 0) { + if (daddr == 0 || skip) { if (SPIRead(daddr, buffer2, buffer_size)) { return 4; } - if (!memcmp(buffer2, buffer, buffer_size)) { + int differs = memcmp(buffer2, buffer, buffer_size); + if (skip) { + // raw: its source, which the copy has not reached, as + // Updater stages an update above the running sketch; + // gzip: the stream inflated again + stale = differs; + } else if (!differs) { ets_putc('B'); // Note we skipped the bootloader in output skip = true; // And skip erase/write } @@ -229,7 +335,11 @@ int copy_raw(const uint32_t src_addr, return 4; } } + if (journal && todo == 0xffffffff) { + journal_clear(mark); + } } + mark += sizeof(uint32_t); saddr += buffer_size; daddr += buffer_size; left -= buffer_size; @@ -242,7 +352,7 @@ int main() { int res = 9; bool clear_cmd = false; - struct eboot_command cmd; + struct journal jh; // the command, and the journal's closed word after it // BSS init commented out for now to save space. If any static variables set // to 0 are used, need to uncomment it or else the BSS will not be cleared and @@ -256,22 +366,58 @@ int main() print_version(0); - if (eboot_command_read(&cmd) == 0) { + journal = journal_find(); + if (eboot_command_read(&jh.cmd) == 0) { // valid command was passed via RTC_MEM clear_cmd = true; ets_putc('@'); + } else if (journal && + SPIRead(journal, &jh, sizeof(jh)) == 0 && + jh.closed == 0xffffffff && + eboot_command_check(&jh.cmd) == 0) { + // a copy the power was cut during + ets_putc('J'); } else { // no valid command found - cmd.action = ACTION_LOAD_APP; - cmd.args[0] = 0; + jh.cmd.action = ACTION_LOAD_APP; + jh.cmd.args[0] = 0; ets_putc('~'); } - if (cmd.action == ACTION_COPY_RAW) { + if (jh.cmd.action == ACTION_COPY_RAW) { ets_printf("cp:"); ets_wdt_disable(); - res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2], false); + if (clear_cmd) { + // Check if we are uncompressing a GZIP upload or not. The + // journal's copy of the command keeps the answer, as a resumed + // copy may have written over the head of the stream. + jh.cmd.args[3] = (read_flash_byte(jh.cmd.args[0]) == 0x1f) && (read_flash_byte(jh.cmd.args[0] + 1) == 0x8b); + jh.cmd.crc32 = eboot_command_calculate_crc32(&jh.cmd); + // the source must end below the journal, whose writes would spoil it + if (!journal || + jh.cmd.args[0] + jh.cmd.args[2] > journal || + SPIEraseSector(journal / FLASH_SECTOR_SIZE)) { + journal = 0; + } else if (SPIWrite(journal, &jh.cmd, sizeof(jh.cmd)) || + // read back: a write that silently failed (a protected + // sector) leaves the RTC command in charge, as before + SPIRead(journal + offsetof(struct eboot_command, crc32), &jh.closed, sizeof(jh.closed)) || + jh.closed != jh.cmd.crc32) { + // a write that reported an error may still have left a + // valid command: close it, or a later start would redo + // the copy from it + journal_close(); + } else { + // the journal carries the command from here on + eboot_command_clear(); + clear_cmd = false; + } + } + res = copy_raw(jh.cmd.args[0], jh.cmd.args[1], jh.cmd.args[2], jh.cmd.args[3], false); + if (journal) { + journal_close(); + } ets_wdt_enable(4, 12, 12); // WDT about 13 secs. ets_printf("%d\n", res); @@ -283,15 +429,15 @@ int main() ets_printf("cmp:"); if (res == 0) { ets_wdt_disable(); - res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2], true); + res = copy_raw(jh.cmd.args[0], jh.cmd.args[1], jh.cmd.args[2], jh.cmd.args[3], true); ets_wdt_enable(); } ets_printf("%d\n", res); #endif if (res == 0) { - cmd.action = ACTION_LOAD_APP; - cmd.args[0] = cmd.args[1]; + jh.cmd.action = ACTION_LOAD_APP; + jh.cmd.args[0] = jh.cmd.args[1]; } } @@ -299,9 +445,9 @@ int main() eboot_command_clear(); } - if (cmd.action == ACTION_LOAD_APP) { + if (jh.cmd.action == ACTION_LOAD_APP) { ets_printf("ld\n"); - res = load_app_from_flash_raw(cmd.args[0]); + res = load_app_from_flash_raw(jh.cmd.args[0]); // We will get to this only on load fail ets_printf("e:%d\n", res); } diff --git a/bootloaders/eboot/eboot.elf b/bootloaders/eboot/eboot.elf index 0e679ff46c..569e17cd2d 100755 Binary files a/bootloaders/eboot/eboot.elf and b/bootloaders/eboot/eboot.elf differ diff --git a/bootloaders/eboot/eboot_command.c b/bootloaders/eboot/eboot_command.c index 54d4895c94..990803e23e 100644 --- a/bootloaders/eboot/eboot_command.c +++ b/bootloaders/eboot/eboot_command.c @@ -36,6 +36,11 @@ int eboot_command_read(struct eboot_command* cmd) dst[i] = RTC_MEM[i]; } + return eboot_command_check(cmd); +} + +int eboot_command_check(const struct eboot_command* cmd) +{ uint32_t crc32 = eboot_command_calculate_crc32(cmd); if ((cmd->magic & EBOOT_MAGIC_MASK) != EBOOT_MAGIC || cmd->crc32 != crc32) { diff --git a/bootloaders/eboot/eboot_command.h b/bootloaders/eboot/eboot_command.h index ba9c889a2c..da82c2f5b6 100644 --- a/bootloaders/eboot/eboot_command.h +++ b/bootloaders/eboot/eboot_command.h @@ -31,6 +31,8 @@ struct eboot_command { int eboot_command_read(struct eboot_command* cmd); +int eboot_command_check(const struct eboot_command* cmd); +uint32_t eboot_command_calculate_crc32(const struct eboot_command* cmd); void eboot_command_write(struct eboot_command* cmd); void eboot_command_clear(); diff --git a/doc/ota_updates/readme.rst b/doc/ota_updates/readme.rst index 51896afe17..1fe95d13d4 100755 --- a/doc/ota_updates/readme.rst +++ b/doc/ota_updates/readme.rst @@ -735,6 +735,40 @@ Update process - memory view - the new sketch is now copied "over" the old one. - the new sketch is started. +RTC memory does not survive a power cut, so before it copies anything, +eboot keeps the command in flash, in whichever copy of the SDK's Wi-Fi +settings (the last three sectors of the flash) is not in use, and marks +each sector there as it writes it. If the power fails during the copy, +eboot carries it on at the next start, from the first sector not marked. +This does not cover: + +- a compressed update that is copied over the place it was stored in, a + large one on a small flash: once the copy has written there, the + compressed data is no longer whole, and the copy cannot be started + again; +- the bootloader's own sector, rewritten whenever its bytes differ from + the update's: when the update carries a different eboot, and also on the + first update after a serial upload whose image header esptool changed + (``--flash_size detect``, or another flash mode). A power cut between + erasing and writing it leaves nothing to start. The update that brings + this eboot is still copied by the eboot before it; +- the part of a copy past its first 991 sectors (about 3.9 MB). + +A serial upload made after such a power cut, before the device has started +again on its own, leaves the unfinished copy in flash. At the next start, +eboot checks that the last sector it had copied still holds what it wrote +there. If the upload changed that sector, eboot drops the copy and the +upload starts. If the upload ends below that sector, eboot finishes the +copy above the upload, and the upload still starts. eboot cannot tell the +upload from the update where that sector holds the same bytes in both: +when the cut came before the copy had written past the bootloader's +sector, or when the upload matches the update there (the firmware the +update replaced, uploaded again, or a nearly identical build). The device +may then start the update, the upload or, rarely, a mix of the two. To be +sure, erase the flash with that upload: +"Erase Flash: Sketch + WiFi Settings" in the IDE (when the build is for the +chip's full size), or "All Flash" (``esptool.py erase_flash``). + By default, OTA filesystem updates overwrite the target flash directly. This can lead to the file system being corrupted if there is a power outage during the update process. In order to use the same two step process that is used for OTA application firmware updates, set the `ATOMIC_FS_UPDATE` flag. Note that you will need to have enough unused space for the new filesystem image to be stored, hence is why this is not the default behaviour. .. figure:: update_memory_copy.png