Skip to content

Commit 5b15c11

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 <almir.okato@espressif.com>
1 parent c53e159 commit 5b15c11

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

boot/espressif/port/esp_mcuboot.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
360360
const uint32_t start_addr = fa->fa_off + off;
361361
BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
362362

363+
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
364+
if (esp_flash_encryption_enabled()) {
365+
/* Ensuring flash region has been erased before writing in order to
366+
* avoid inconsistences when hardware flash encryption is enabled.
367+
*/
368+
if (!aligned_flash_erase(start_addr, len)) {
369+
BOOT_LOG_ERR("%s: Flash erase before write failed", __func__);
370+
return -1;
371+
}
372+
}
373+
#endif
363374

364375
if (!aligned_flash_write(start_addr, src, len)) {
365376
BOOT_LOG_ERR("%s: Flash write failed", __func__);
@@ -393,7 +404,36 @@ int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
393404
}
394405
}
395406

396-
#if VALIDATE_PROGRAM_OP
407+
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
408+
uint8_t write_data[FLASH_BUFFER_SIZE];
409+
memset(write_data, flash_area_erased_val(fa), sizeof(write_data));
410+
uint32_t bytes_remaining = len;
411+
uint32_t offset = start_addr;
412+
413+
uint32_t bytes_written = MIN(sizeof(write_data), len);
414+
if (esp_flash_encryption_enabled()) {
415+
/* When hardware flash encryption is enabled, force expected erased
416+
* value (0xFF) into flash when erasing a region.
417+
*
418+
* This is handled on this implementation because MCUboot's state
419+
* machine relies on erased valued data (0xFF) readed from a
420+
* previously erased region that was not written yet, however when
421+
* hardware flash encryption is enabled, the flash read always
422+
* decrypts whats being read from flash, thus a region that was
423+
* erased would not be read as what MCUboot expected (0xFF).
424+
*/
425+
while (bytes_remaining != 0) {
426+
if (!aligned_flash_write(offset, write_data, bytes_written)) {
427+
BOOT_LOG_ERR("%s: Flash erase before write failed", __func__);
428+
return -1;
429+
}
430+
offset += bytes_written;
431+
bytes_remaining -= bytes_written;
432+
}
433+
}
434+
#endif
435+
436+
#if VALIDATE_PROGRAM_OP && !defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
397437
for (size_t i = 0; i < len; i++) {
398438
uint8_t *val = (void *)(start_addr + i);
399439
if (*val != 0xff) {

0 commit comments

Comments
 (0)