Skip to content

Commit b3b01c5

Browse files
committed
espressif: flash: fix image wrong state after swap-scratch when flash encryption is enabled
When hardware flash encryption is enabled, force expected erased value (0xFF) into flash when erasing a region, and also always do a real erase before writing data into flash. This is handled on this implementation because MCUboot's state machine relies on erased valued data (0xFF) readed from a previously erased region that was not written yet, however when hardware flash encryption is enabled, the flash read always decrypts whats being read from flash, thus a region that was erased would not be read as what MCUboot expected (0xFF). Signed-off-by: Almir Okato <[email protected]>
1 parent b41b6b3 commit b3b01c5

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

boot/espressif/port/esp_mcuboot.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,18 @@ int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
387387
const uint32_t start_addr = fa->fa_off + off;
388388
BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
389389

390+
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
391+
if (esp_flash_encryption_enabled()) {
392+
/* Ensuring flash region has been erased before writing in order to
393+
* avoid inconsistences when hardware flash encryption is enabled.
394+
*/
395+
if (!aligned_flash_erase(start_addr, len)) {
396+
BOOT_LOG_ERR("%s: Flash erase before write failed", __func__);
397+
return -1;
398+
}
399+
}
400+
#endif
401+
390402
if (!aligned_flash_write(start_addr, src, len)) {
391403
BOOT_LOG_ERR("%s: Flash write failed", __func__);
392404
return -1;
@@ -415,9 +427,36 @@ int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
415427

416428
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
417429
flush_cache(start_addr, len);
430+
431+
uint8_t write_data[FLASH_BUFFER_SIZE];
432+
memset(write_data, flash_area_erased_val(fa), sizeof(write_data));
433+
uint32_t bytes_remaining = len;
434+
uint32_t offset = start_addr;
435+
436+
uint32_t bytes_written = MIN(sizeof(write_data), len);
437+
if (esp_flash_encryption_enabled()) {
438+
/* When hardware flash encryption is enabled, force expected erased
439+
* value (0xFF) into flash when erasing a region.
440+
*
441+
* This is handled on this implementation because MCUboot's state
442+
* machine relies on erased valued data (0xFF) readed from a
443+
* previously erased region that was not written yet, however when
444+
* hardware flash encryption is enabled, the flash read always
445+
* decrypts whats being read from flash, thus a region that was
446+
* erased would not be read as what MCUboot expected (0xFF).
447+
*/
448+
while (bytes_remaining != 0) {
449+
if (!aligned_flash_write(offset, write_data, bytes_written)) {
450+
BOOT_LOG_ERR("%s: Flash erase before write failed", __func__);
451+
return -1;
452+
}
453+
offset += bytes_written;
454+
bytes_remaining -= bytes_written;
455+
}
456+
}
418457
#endif
419458

420-
#if VALIDATE_PROGRAM_OP
459+
#if VALIDATE_PROGRAM_OP && !defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
421460
for (size_t i = 0; i < len; i++) {
422461
uint8_t *val = (void *)(start_addr + i);
423462
if (*val != 0xff) {

0 commit comments

Comments
 (0)