What changed, and why it matters
This commit rewrites the BitBox02 bootloader's firmware-update logic to write 4 KiB chunks without repeatedly erasing and rewriting the surrounding 8 KiB flash block. It also enforces that new chunks arrive in ascending order, only allows re-sending earlier chunks if they match what's already in flash, verifies each destination is erased before writing, and verifies written bytes match afterwards. The change appears to be a performance improvement with defensive hardening, not a fix for a known exploitable bug. There is no vendor statement that this is a security patch, and no independent researcher is credited.
Treat as a normal firmware improvement with embedded hardening. Review the new ordering and verification logic during QA to confirm it does not introduce regressions for legitimate update clients. No urgent security response is indicated from the commit alone.
Security signals we found
New ordering and replay checks on firmware chunks reduce opportunities for out-of-order or malicious re-write attempts during the update protocol.
Explicit pre-write erased-state check and post-write verify add defense-in-depth against writing to non-blank flash or detecting flash failures.
Switch from flash_write to flash_append removes repeated erase/program cycles on unchanged data, reducing flash wear and potential corruption windows.
Word-aligned buffer copy before flash_append addresses an alignment/width assumption in the underlying flash driver.
Single-chunk progress fix removes a divide-by-zero in UI rendering.
Evidence from the diff
The bootloader previously used flash_write for each 4 KiB firmware chunk, which internally erased and reprogrammed an entire 8 KiB erase block for every chunk, rewriting the other half of the block each time. The patch switches to flash_append and explicitly erases each 8 KiB block only when its first chunk arrives, skipping already-erased blocks. It adds _is_erased and _firmware_erase_block helpers, requires chunks to arrive in ascending order starting at zero, permits retries of earlier chunks only if their contents match flash, copies USB payloads into a word-aligned uint32_t buffer before flash_append, checks the destination is erased before programming, and verifies programmed bytes after writing. OP_ERASE now starts a new upload session when given a nonzero chunk count and erases only the unused tail from the last erase-block boundary. A divide-by-zero in progress rendering for single-chunk uploads is also fixed.
Changed components
src/bootloader/bootloader.cBootloader firmware update protocol (OP_ERASE, OP_WRITE_FIRMWARE_CHUNK)Flash programming path (flash_append, flash_erase)Inspect captured patch +76 / −46
### src/bootloader/bootloader.c
@@ -46,16 +46,15 @@
// Bootloader API command op codes
//
-// OP_ERASE - Receives the total number of firmware chunks to write,
-// then pads any left over FLASH that would be empty by 0xFFs;
-// FLASH areas that will contain firmware are erased when writing the
-// firmware chunk to FLASH.
+// OP_ERASE - Erases unused firmware flash starting at an erase-block boundary.
+// Receives the total number of firmware chunks to write. If non-zero, starts
+// a new firmware loading session.
#define OP_ERASE ((uint8_t)'e') /* 0x65 */
// OP_REBOOT - Reboot the MCU and clear the auto_enter flag.
#define OP_REBOOT ((uint8_t)'r') /* 0x72 */
// OP_WRITE - Write a firmware chunk; the binary must be streamed in chunks.
// The command must include the chunk number in order to be written to the
-// correct FLASH location.
+// correct FLASH location. New chunks must arrive in ascending order.
#define OP_WRITE_FIRMWARE_CHUNK ((uint8_t)'w') /* 0x77 */
// OP_WRITE_SIG_DATA - Write the firmware's signature data, which is used
// for firmware verification.
@@ -95,11 +94,15 @@ static struct sha_context _pukcc_sha256_context;
COMPILER_PACK_RESET()
#define FIRMWARE_CHUNK_LEN (8U * FLASH_PAGE_SIZE) // 4kB
+#define FIRMWARE_BLOCK_LEN FLASH_ERASE_MIN_LEN // 8kB
#define FIRMWARE_MAX_NUM_CHUNKS \
(FLASH_APP_LEN / FIRMWARE_CHUNK_LEN) // app len must be a multiple of chunk len
#if (FIRMWARE_MAX_NUM_CHUNKS > UINT8_MAX)
#error "incompatible variable type"
#endif
+#if (FLASH_APP_LEN % FIRMWARE_BLOCK_LEN)
+ #error "app flash len must be a multiple of the firmware block len"
+#endif
// Be sure to not overflow boot data area
static_assert(sizeof(((boot_data_t*)0)->fields) <= FLASH_BOOTDATA_LEN, "boot_data_t too large");
@@ -115,6 +118,7 @@ static_assert(sizeof(((boot_data_t*)0)->fields) <= FLASH_BOOTDATA_LEN, "boot_dat
static bool _loading_ready = false;
static uint8_t _firmware_num_chunks = 0;
+static uint8_t _firmware_next_chunk = 0;
// Indicates whether the whole app flash contains only 0xFF.
// This controls bootloader text messages on the screen.
// The value is computed at bootloader enter.
@@ -386,6 +390,35 @@ static size_t _report_status(uint8_t status, uint8_t* output)
return BOOT_OP_LEN;
}
+static bool _is_erased(const void* addr, size_t len)
+{
+ const uint8_t* bytes = (const uint8_t*)addr;
+ for (size_t i = 0; i < len; i++) {
+ if (bytes[i] != 0xff) {
+ return false;
+ }
+ }
+ return true;
+}
+
+static uint8_t _firmware_erase_block(uint32_t addr)
+{
+ if (addr < FLASH_APP_START || addr + FIRMWARE_BLOCK_LEN > FLASH_APP_START + FLASH_APP_LEN ||
+ addr % FIRMWARE_BLOCK_LEN != 0) {
+ return OP_STATUS_ERR_ERASE;
+ }
+ if (_is_erased((const void*)addr, FIRMWARE_BLOCK_LEN)) {
+ return OP_STATUS_OK;
+ }
+ if (flash_erase(&FLASH_0, addr, FLASH_ERASE_PAGE_NUM) != ERR_NONE) {
+ return OP_STATUS_ERR_ERASE;
+ }
+ if (!_is_erased((const void*)addr, FIRMWARE_BLOCK_LEN)) {
+ return OP_STATUS_ERR_CHECK;
+ }
+ return OP_STATUS_OK;
+}
+
static size_t _api_write_chunk(const uint8_t* buf, uint8_t chunknum, uint8_t* output)
{
if (!_loading_ready) {
@@ -403,48 +436,45 @@ static size_t _api_write_chunk(const uint8_t* buf, uint8_t chunknum, uint8_t* ou
return _report_status(OP_STATUS_ERR_LEN, output);
}
- if (MEMEQ(
- (const void*)(FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN)),
- buf,
- FIRMWARE_CHUNK_LEN)) {
+ const uint32_t addr = FLASH_APP_START + chunknum * FIRMWARE_CHUNK_LEN;
+ if (chunknum < _firmware_next_chunk) {
+ if (!MEMEQ((const void*)addr, buf, FIRMWARE_CHUNK_LEN)) {
+ return _report_status(OP_STATUS_ERR_CHECK, output);
+ }
_loading_ready = true;
return _report_status(OP_STATUS_OK, output);
}
+ if (chunknum != _firmware_next_chunk) {
+ return _report_status(OP_STATUS_ERR_LEN, output);
+ }
- // Erase is handled inside of flash_write
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
- if (flash_write(
- &FLASH_0, FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN), buf, FIRMWARE_CHUNK_LEN) !=
- ERR_NONE) {
+ if (addr % FIRMWARE_BLOCK_LEN == 0) {
+ uint8_t status = _firmware_erase_block(addr);
+ if (status != OP_STATUS_OK) {
+ return _report_status(status, output);
+ }
+ }
+ if (!_is_erased((const void*)addr, FIRMWARE_CHUNK_LEN)) {
+ return _report_status(OP_STATUS_ERR_CHECK, output);
+ }
+
+ // flash_append reads its source as uint32_t words.
+ uint32_t aligned_buf[FIRMWARE_CHUNK_LEN / sizeof(uint32_t)];
+ memcpy(aligned_buf, buf, sizeof(aligned_buf));
+ if (flash_append(&FLASH_0, addr, (uint8_t*)aligned_buf, sizeof(aligned_buf)) != ERR_NONE) {
return _report_status(OP_STATUS_ERR_WRITE, output);
}
-#pragma GCC diagnostic pop
- if (!MEMEQ(
- (const void*)(FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN)),
- buf,
- FIRMWARE_CHUNK_LEN)) {
+ if (!MEMEQ((const void*)addr, buf, FIRMWARE_CHUNK_LEN)) {
return _report_status(OP_STATUS_ERR_CHECK, output);
}
size_t len = _report_status(OP_STATUS_OK, output);
+ _firmware_next_chunk++;
_loading_ready = true;
return len;
}
-static bool _flash_pages_erased(uint32_t addr, uint32_t num_pages)
-{
- const uint32_t* words = (const uint32_t*)addr;
- const uint32_t num_words = num_pages * FLASH_PAGE_SIZE / sizeof(uint32_t);
- for (uint32_t i = 0; i < num_words; i++) {
- if (words[i] != UINT32_MAX) {
- return false;
- }
- }
- return true;
-}
-
/**
* This function erases the padding bytes, if not already erased. Erasing starts
* at an erase-block boundary, so if the firmware ends halfway through a block,
@@ -468,23 +498,19 @@ static size_t _api_firmware_erase(uint8_t firmware_num_chunks, uint8_t* output)
return _report_status(OP_STATUS_ERR_UNLOCK, output);
}
}
- uint16_t firmware_num_pages = firmware_num_chunks * FIRMWARE_CHUNK_LEN / FLASH_PAGE_SIZE;
- uint16_t erase_start_page = firmware_num_pages - (firmware_num_pages % FLASH_ERASE_PAGE_NUM);
- for (uint32_t i = erase_start_page; i < (uint32_t)FLASH_APP_PAGE_NUM;
- i += FLASH_ERASE_PAGE_NUM) {
- const uint32_t addr = FLASH_APP_START + i * FLASH_PAGE_SIZE;
- if (_flash_pages_erased(addr, FLASH_ERASE_PAGE_NUM)) {
- continue;
- }
- if (flash_erase(&FLASH_0, addr, FLASH_ERASE_PAGE_NUM) != ERR_NONE) {
- return _report_status(OP_STATUS_ERR_ERASE, output);
- }
- if (!_flash_pages_erased(addr, FLASH_ERASE_PAGE_NUM)) {
- return _report_status(OP_STATUS_ERR_CHECK, output);
+ const uint32_t firmware_len = firmware_num_chunks * FIRMWARE_CHUNK_LEN;
+ const uint32_t erase_start = FLASH_APP_START + firmware_len - firmware_len % FIRMWARE_BLOCK_LEN;
+ for (uint32_t addr = erase_start; addr < FLASH_APP_START + FLASH_APP_LEN;
+ addr += FIRMWARE_BLOCK_LEN) {
+ uint8_t status = _firmware_erase_block(addr);
+ if (status != OP_STATUS_OK) {
+ return _report_status(status, output);
}
}
+
if (firmware_num_chunks > 0) {
_firmware_num_chunks = firmware_num_chunks;
+ _firmware_next_chunk = 0;
_loading_ready = true;
}
return _report_status(OP_STATUS_OK, output);
@@ -850,7 +876,11 @@ static size_t _api_command(const uint8_t* input, uint8_t* output, const size_t m
if (output[1] != OP_STATUS_OK) {
bootloader_render_default_screen();
} else {
- _render_progress((float)chunk_num / (float)(_firmware_num_chunks - 1));
+ float progress = 1;
+ if (_firmware_num_chunks > 1) {
+ progress = (float)chunk_num / (float)(_firmware_num_chunks - 1);
+ }
+ _render_progress(progress);
}
break;
}Why this scored 32/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.