Merge branch 'nickez-agent/disable-nvm-cache-latest'
What changed, and why it matters
This commit changes how the BitBox02 hardware wallet writes data to its internal flash memory. It disables the microcontroller's flash cache during programming operations, following a manufacturer errata note (Microchip SAM D5x/E5x errata 2.14.1). Without this fix, cached flash reads could interfere with the data being programmed, potentially leading to corrupted writes in firmware updates, bootloader fuses, or the secure EEPROM area that stores device secrets. The change is defensive and addresses a known silicon errata, but the commit itself does not describe a specific exploited vulnerability or security incident.
Verify that all NVMCTRL programming paths in the firmware, including any not shown in this diff, apply the same cache-disable workaround. Review whether the partial fix leaves other flash-write operations exposed to the same errata. Consider requesting or checking for a Microchip errata sheet and any associated security guidance. No immediate user action is indicated by the commit alone.
Security signals we found
Flash cache disabled during NVM page-buffer load per silicon errata workaround
Bootloader fuse write path modified
SmartEEPROM (secure storage) configuration and write paths modified
Manual write mode fuse operations now include cache-disable step
No explicit CVE, advisory, or exploit description in commit or references
Evidence from the diff
The patch modifies the NVMCTRL driver and three call sites to disable the AHB0/AHB1 flash caches (CACHEDIS0/CACHEDIS1) while loading the page buffer and during programming/erase operations, then restores the previous CTRLA state afterward. This implements the workaround for Microchip SAM D5x/E5x errata 2.14.1, which states that flash caches must be disabled during page buffer loading to avoid cache coherency issues. Affected paths include the ASF4 NVMCTRL driver, bootloader fuse writes, SmartEEPROM configuration/writes, and flash programming. The fix is partial in that it does not appear to cover all possible NVMCTRL programming paths in the codebase, though it covers the security-relevant ones shown.
Changed components
external/asf4-drivers/hpl/nvmctrl/hpl_nvmctrl.csrc/bootloader/bootloader.csrc/memory/smarteeprom.cMicrochip SAM D5x/E5x NVMCTRL flash cache / page bufferInspect captured patch +25 / −0
### external/asf4-drivers/Config/hpl_nvmctrl_config.h
@@ -15,6 +15,7 @@
#define CONF_NVM_SLEEPPRM 0
#endif
+// ICM flash reads require the AHB1 cache; disable caches only during programming.
// <q> AHB0 Cache Disable
// <i> Indicate whether AHB0 cache is disable or not
// <id> nvm_arch_cache0
### external/asf4-drivers/hpl/nvmctrl/hpl_nvmctrl.c
@@ -377,12 +377,16 @@ static void _flash_program(void *const hw, const uint32_t dst_addr, const uint8_
{
uint32_t *ptr_read = (uint32_t *)buffer;
uint32_t nvm_address = dst_addr / 4;
+ uint32_t ctrla;
uint16_t i;
while (!hri_nvmctrl_get_STATUS_READY_bit(hw)) {
/* Wait until this module isn't busy */
}
+ /* SAM D5x/E5x errata 2.14.1: disable both caches while loading the page buffer. */
+ ctrla = hri_nvmctrl_read_CTRLA_reg(hw);
+ hri_nvmctrl_set_CTRLA_reg(hw, NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1);
hri_nvmctrl_write_CTRLB_reg(hw, NVMCTRL_CTRLB_CMD_PBC | NVMCTRL_CTRLB_CMDEX_KEY);
while (!hri_nvmctrl_get_STATUS_READY_bit(hw)) {
@@ -402,6 +406,10 @@ static void _flash_program(void *const hw, const uint32_t dst_addr, const uint8_
hri_nvmctrl_write_ADDR_reg(hw, dst_addr);
hri_nvmctrl_write_CTRLB_reg(hw, NVMCTRL_CTRLB_CMD_WP | NVMCTRL_CTRLB_CMDEX_KEY);
+ while (!hri_nvmctrl_get_STATUS_READY_bit(hw)) {
+ /* Wait until this module isn't busy */
+ }
+ hri_nvmctrl_write_CTRLA_reg(hw, ctrla);
}
/**
@@ -588,6 +596,8 @@ static int32_t _user_row_write_exec(const uint32_t *_row)
/* Wait until this module isn't busy */
}
hri_nvmctrl_clear_CTRLA_WMODE_bf(NVMCTRL, NVMCTRL_CTRLA_WMODE_Msk);
+ /* SAM D5x/E5x errata 2.14.1: disable both caches while loading the page buffer. */
+ hri_nvmctrl_set_CTRLA_reg(hw, NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1);
/* - Erase AUX row. */
hri_nvmctrl_write_ADDR_reg(hw, (hri_nvmctrl_addr_reg_t)_NVM_USER_ROW_BASE);
### src/bootloader/bootloader.c
@@ -934,6 +934,8 @@ static void _check_init(boot_data_t* data)
(FLASH_BOOTPROTECTION << NVMCTRL_FUSES_BOOTPROT_Pos);
// Write fuses
NVMCTRL->CTRLA.bit.WMODE = NVMCTRL_CTRLA_WMODE_MAN; // Manual write
+ // SAM D5x/E5x errata 2.14.1: disable both caches while loading the page buffer.
+ NVMCTRL->CTRLA.reg |= NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1;
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_EP); // Erase page
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_PBC); // Clear page buffer
*((uint32_t*)NVMCTRL_FUSES_BOOTPROT_ADDR) = fuses[0];
### src/memory/smarteeprom.c
@@ -34,12 +34,16 @@ void smarteeprom_disable(void)
/* Now set the SBLK value as well. */
config_word = (config_word & ~NVMCTRL_FUSES_SEESBLK_Msk) | (NVMCTRL_FUSES_SEESBLK(0));
NVMCTRL->CTRLA.bit.WMODE = NVMCTRL_CTRLA_WMODE_MAN; // Manual write
+ // SAM D5x/E5x errata 2.14.1: disable both caches while loading the page buffer.
+ const uint16_t ctrla = NVMCTRL->CTRLA.reg;
+ NVMCTRL->CTRLA.reg |= NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1;
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_EP); // Erase page
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_PBC); // Clear page buffer
*((uint32_t*)NVMCTRL_FUSES_SEEPSZ_ADDR) = config_word;
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_WQW); // Write a 128-bit word
while (!NVMCTRL->STATUS.bit.READY);
while (NVMCTRL->SEESTAT.bit.BUSY);
+ NVMCTRL->CTRLA.reg = ctrla;
}
void smarteeprom_setup(void)
@@ -72,12 +76,16 @@ void smarteeprom_setup(void)
config_word = (config_word & ~NVMCTRL_FUSES_SEESBLK_Msk) |
(NVMCTRL_FUSES_SEESBLK(SMARTEEPROM_SBLK_VALUE));
NVMCTRL->CTRLA.bit.WMODE = NVMCTRL_CTRLA_WMODE_MAN; // Manual write
+ // SAM D5x/E5x errata 2.14.1: disable both caches while loading the page buffer.
+ const uint16_t ctrla = NVMCTRL->CTRLA.reg;
+ NVMCTRL->CTRLA.reg |= NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1;
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_EP); // Erase page
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_PBC); // Clear page buffer
*((uint32_t*)NVMCTRL_FUSES_SEEPSZ_ADDR) = config_word;
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_WQW); // Write a 128-bit word
while (!NVMCTRL->STATUS.bit.READY);
while (NVMCTRL->SEESTAT.bit.BUSY);
+ NVMCTRL->CTRLA.reg = ctrla;
}
void smarteeprom_bb02_config(void)
@@ -124,6 +132,9 @@ void smarteeprom_write(size_t address, size_t bytes, const uint8_t* buffer)
}
volatile uint8_t* eeprom = (uint8_t*)SEEPROM_ADDR + address;
while (NVMCTRL->SEESTAT.bit.BUSY);
+ // Disable both NVM caches during buffered writes and flushes.
+ const uint16_t ctrla = NVMCTRL->CTRLA.reg;
+ NVMCTRL->CTRLA.reg |= NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1;
/*
* Buffered write of multiple bytes.
* Note that crossing a 32B page will still result in a partial flush
@@ -137,6 +148,7 @@ void smarteeprom_write(size_t address, size_t bytes, const uint8_t* buffer)
nvmctrl_exec_cmd(NVMCTRL_CTRLB_CMD_SEEFLUSH);
while (NVMCTRL->SEESTAT.bit.LOAD != 0);
while (NVMCTRL->SEESTAT.bit.BUSY != 0);
+ NVMCTRL->CTRLA.reg = ctrla;
/*
* Read back the buffer.
* Check that it matches what we've just written.Why this scored 59/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.