Skip to content

Commit fd2bc2a

Browse files
committed
bootutil: add MCUBOOT_FLASH_HAS_HW_ENCRYPTION config-condition
MCUboot's state machine relies on erased valued data (e.g. 0xFF) readed from this erased region that could be not written before, however if the flash device has hardware flash encryption and its flash read operation always decrypts what is being read from flash, thus a region that was erased would not be read as what MCUboot expected (after erasing, the region physically contains 0xFF, but once reading it, flash controller decrypts 0xFF to something else). So this configuration force the erased value into the region after the erasing the trailer regions, and also make an erase operation before writing trailers. Signed-off-by: Almir Okato <[email protected]>
1 parent c53e159 commit fd2bc2a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

boot/bootutil/src/bootutil_public.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ boot_write_magic(const struct flash_area *fap)
327327
BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
328328
flash_area_get_id(fap), (unsigned long)off,
329329
(unsigned long)(flash_area_get_off(fap) + off));
330+
331+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
332+
rc = flash_area_erase(fap, pad_off, BOOT_MAGIC_ALIGN_SIZE);
333+
#endif
334+
330335
rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
331336

332337
if (rc != 0) {
@@ -360,6 +365,10 @@ boot_write_trailer(const struct flash_area *fap, uint32_t off,
360365
memcpy(buf, inbuf, inlen);
361366
memset(&buf[inlen], erased_val, align - inlen);
362367

368+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
369+
rc = flash_area_erase(fap, off, align);
370+
#endif
371+
363372
rc = flash_area_write(fap, off, buf, align);
364373
if (rc != 0) {
365374
return BOOT_EFLASH;

boot/bootutil/src/loader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,9 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
757757
flash_area_get_id(fap), (unsigned long)off,
758758
(unsigned long)flash_area_get_off(fap) + off);
759759

760+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
761+
rc = flash_area_erase(fap, off, align);
762+
#endif
760763
rc = flash_area_write(fap, off, buf, align);
761764
if (rc != 0) {
762765
rc = BOOT_EFLASH;

boot/bootutil/src/swap_misc.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,40 @@ swap_erase_trailer_sectors(const struct boot_loader_state *state,
7272
rc = boot_erase_region(fap, off, sz);
7373
assert(rc == 0);
7474

75+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
76+
/* MCUboot's state machine relies on erased valued data
77+
* (e.g. 0xFF) readed from this erased region that could
78+
* be not written before, however if the flash device has
79+
* hardware flash encryption and its flash read operation
80+
* always decrypts what is being read from flash, thus a
81+
* region that was erased would not be read as what
82+
* MCUboot expected (after erasing, the region
83+
* physically contains 0xFF, but once reading it, flash
84+
* controller decrypts 0xFF to something else).
85+
* So this configuration force the erased value into the
86+
* region after the erasing.
87+
*/
88+
#ifndef MIN
89+
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
90+
#endif
91+
92+
uint8_t write_data[FLASH_AUX_WRITE_BUFFER_SIZE];
93+
memset(write_data, flash_area_erased_val(fap), sizeof(write_data));
94+
uint32_t bytes_remaining = sz;
95+
uint32_t offset = off;
96+
97+
uint32_t bytes_written = MIN(sizeof(write_data), sz);
98+
while (bytes_remaining != 0) {
99+
if (flash_area_write(fap, offset, write_data, bytes_written)) {
100+
BOOT_LOG_ERR("%s: Force write erased value after erasing a trailer region failed", __func__);
101+
rc = -1;
102+
break;
103+
}
104+
offset += bytes_written;
105+
bytes_remaining -= bytes_written;
106+
}
107+
#endif // MCUBOOT_FLASH_HAS_HW_ENCRYPTION
108+
75109
sector--;
76110
total_sz += sz;
77111
} while (total_sz < trailer_sz);

boot/espressif/hal/include/mcuboot_config/mcuboot_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
#define MCUBOOT_BOOT_MAX_ALIGN 32
3434
#endif
3535

36+
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
37+
#define MCUBOOT_FLASH_HAS_HW_ENCRYPTION 1
38+
#endif
39+
40+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
41+
#define FLASH_AUX_WRITE_BUFFER_SIZE 0x100
42+
#endif
43+
3644
/*
3745
* Upgrade mode
3846
*

0 commit comments

Comments
 (0)