Skip to content

Commit ed871e2

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 02cb4a6 commit ed871e2

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
@@ -751,6 +751,9 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
751751
flash_area_get_id(fap), (unsigned long)off,
752752
(unsigned long)flash_area_get_off(fap) + off);
753753

754+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
755+
rc = flash_area_erase(fap, off, align);
756+
#endif
754757
rc = flash_area_write(fap, off, buf, align);
755758
if (rc != 0) {
756759
rc = BOOT_EFLASH;

boot/bootutil/src/swap_misc.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ swap_erase_trailer_sectors(const struct boot_loader_state *state,
6464
rc = boot_erase_region(fap, off, sz, false);
6565
assert(rc == 0);
6666

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