Skip to content

Commit 3822a5c

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 95f6eba commit 3822a5c

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-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("boot_write_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) {
@@ -365,6 +370,10 @@ boot_write_trailer(const struct flash_area *fap, uint32_t off,
365370
memcpy(buf, inbuf, inlen);
366371
memset(&buf[inlen], erased_val, align - inlen);
367372

373+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
374+
rc = flash_area_erase(fap, off, align);
375+
#endif
376+
368377
rc = flash_area_write(fap, off, buf, align);
369378
if (rc != 0) {
370379
return BOOT_EFLASH;

boot/bootutil/src/loader.c

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

655+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
656+
rc = flash_area_erase(fap, off, align);
657+
#endif
655658
rc = flash_area_write(fap, off, buf, align);
656659
if (rc != 0) {
657660
rc = BOOT_EFLASH;

boot/bootutil/src/swap_misc.c

Lines changed: 68 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);
@@ -92,6 +126,40 @@ swap_scramble_trailer_sectors(const struct boot_loader_state *state,
92126
return BOOT_EFLASH;
93127
}
94128

129+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
130+
/* MCUboot's state machine relies on erased valued data
131+
* (e.g. 0xFF) readed from this erased region that could
132+
* be not written before, however if the flash device has
133+
* hardware flash encryption and its flash read operation
134+
* always decrypts what is being read from flash, thus a
135+
* region that was erased would not be read as what
136+
* MCUboot expected (after erasing, the region
137+
* physically contains 0xFF, but once reading it, flash
138+
* controller decrypts 0xFF to something else).
139+
* So this configuration force the erased value into the
140+
* region after the erasing.
141+
*/
142+
#ifndef MIN
143+
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
144+
#endif
145+
146+
uint8_t write_data[FLASH_AUX_WRITE_BUFFER_SIZE];
147+
memset(write_data, flash_area_erased_val(fap), sizeof(write_data));
148+
uint32_t bytes_remaining = (flash_area_get_size(fap) - off);
149+
uint32_t offset = off;
150+
151+
uint32_t bytes_written = MIN(sizeof(write_data), (flash_area_get_size(fap) - off));
152+
while (bytes_remaining != 0) {
153+
if (flash_area_write(fap, offset, write_data, bytes_written)) {
154+
BOOT_LOG_ERR("%s: Force write erased value after erasing a trailer region failed", __func__);
155+
rc = -1;
156+
break;
157+
}
158+
offset += bytes_written;
159+
bytes_remaining -= bytes_written;
160+
}
161+
#endif // MCUBOOT_FLASH_HAS_HW_ENCRYPTION
162+
95163
return 0;
96164
}
97165

boot/bootutil/src/swap_scratch.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,15 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs,
943943
swap_idx++;
944944
}
945945

946+
#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION
947+
int rc;
948+
/* Ensure that the trailer from scratch area will have
949+
* unset state after the swap process finishes.
950+
*/
951+
rc = swap_scramble_trailer_sectors(state, state->scratch.area);
952+
assert(rc == 0);
953+
#endif // MCUBOOT_FLASH_HAS_HW_ENCRYPTION
954+
946955
}
947956
#endif /* !MCUBOOT_OVERWRITE_ONLY */
948957

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)