Skip to content

Commit cf1b8e0

Browse files
Don't overwrite boot sector unless OTA changes it
There is a window where the eboot sector is erased and unwritten/partially written. If there's a power cycle at this time, the chip will brick due to eboot being corrupted. Avoid this by checking if the new eboot 4K sector is identical to the one already in flash, and if so don't rewrite it.
1 parent 1ff927d commit cf1b8e0

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

bootloaders/eboot/eboot.c

+19-7
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ int copy_raw(const uint32_t src_addr,
159159
gzip = true;
160160
}
161161
while (left > 0) {
162-
if (!verify) {
163-
if (SPIEraseSector(daddr/buffer_size)) {
164-
return 2;
165-
}
166-
}
167162
if (!gzip) {
168163
if (SPIRead(saddr, buffer, buffer_size)) {
169164
return 3;
@@ -190,8 +185,25 @@ int copy_raw(const uint32_t src_addr,
190185
return 9;
191186
}
192187
} else {
193-
if (SPIWrite(daddr, buffer, buffer_size)) {
194-
return 4;
188+
// Special treatment for address 0 (bootloader). Only erase and
189+
// rewrite if the data is different (i.e. very rarely).
190+
bool skip = false;
191+
if (daddr == 0) {
192+
if (SPIRead(daddr, buffer2, buffer_size)) {
193+
return 4;
194+
}
195+
if (!memcmp(buffer2, buffer, buffer_size)) {
196+
ets_putc('B'); // Note we skipped the bootloader in output
197+
skip = true; // And skip erase/write
198+
}
199+
}
200+
if (!skip) {
201+
if (SPIEraseSector(daddr/buffer_size)) {
202+
return 2;
203+
}
204+
if (SPIWrite(daddr, buffer, buffer_size)) {
205+
return 4;
206+
}
195207
}
196208
}
197209
saddr += buffer_size;

bootloaders/eboot/eboot.elf

432 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)