memory: disable NVM caches during programming
What changed, and why it matters
This commit fixes a hardware erratum in the microcontroller used by the BitBox02 hardware wallet. When the device writes to its internal flash memory, a bug in certain chip revisions can corrupt data being read from flash at the same time—including the program instructions being fetched to run the write routine. The patch temporarily disables two flash caches only during programming, then restores them afterward. If left unpatched, this could in theory lead to corrupted firmware writes, fuses, or security-critical configuration bits, but the commit is a preventive fix rather than a confirmed exploited vulnerability.
Treat as a security-relevant hardening fix. Rebuild and deploy updated firmware, bootloader, and factory images. Verify that the cache-disable/restore logic is exercised in programming tests, especially for BOOTPROT and SmartEEPROM paths. No immediate incident response is indicated absent evidence of active exploitation.
Security signals we found
Fixes a documented silicon erratum that can corrupt flash reads during NVM programming
Covers security-sensitive write paths: BOOTPROT fuses and SmartEEPROM configuration
Could affect firmware/bootloader integrity if corrupted instructions or fuses were written
No explicit CVE, exploit, or incident disclosed in the commit or supplied references
Evidence from the diff
The change implements Microchip erratum DS80000748T section 2.14.1 for SAM D5x/E5x revisions A and D. During NVM page-buffer loading, reading flash can return corrupted data. Because the programming routines execute from flash, instruction fetches are exposed to this erratum. The patch disables both NVMCTRL AHB caches (CACHEDIS0 and CACHEDIS1) around explicit page-buffer writes in _flash_program, _user_row_write_exec, bootloader BOOTPROT fuse writes, and SmartEEPROM configuration writes, restoring the original CTRLA value afterward. The caches remain enabled during normal execution to preserve stage0 ICM hashing performance.
Changed components
external/asf4-drivers/hpl/nvmctrl/hpl_nvmctrl.csrc/bootloader/bootloader.csrc/memory/smarteeprom.cBitBox02 firmware/bootloader/factory images on SAMD51 revisions A and DInspect captured patch +21 / −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)Why this scored 57/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.