Skip to content

Commit 665641d

Browse files
jpbland1danielinux
authored andcommitted
update tests to properly simulate flash locks
add hal_flash_unlock after setting the key since setting the key locks flash
1 parent b52c938 commit 665641d

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

hal/sim.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static uint8_t *flash_base;
5454

5555
int forceEmergency = 0;
5656
uint32_t erasefail_address = 0xFFFFFFFF;
57+
int flashLocked = 1;
58+
int extFlashLocked = 1;
5759

5860
#define INTERNAL_FLASH_FILE "./internal_flash.dd"
5961
#define EXTERNAL_FLASH_FILE "./external_flash.dd"
@@ -134,12 +136,12 @@ static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
134136

135137
void hal_flash_unlock(void)
136138
{
137-
/* no op */
139+
flashLocked = 0;
138140
}
139141

140142
void hal_flash_lock(void)
141143
{
142-
/* no op */
144+
flashLocked = 1;
143145
}
144146

145147
void hal_prepare_boot(void)
@@ -150,6 +152,10 @@ void hal_prepare_boot(void)
150152
int hal_flash_write(uintptr_t address, const uint8_t *data, int len)
151153
{
152154
int i;
155+
if (flashLocked == 1) {
156+
wolfBoot_printf("FLASH IS BEING WRITTEN TO WHILE LOCKED\n");
157+
return -1;
158+
}
153159
if (forceEmergency == 1 && address == WOLFBOOT_PARTITION_BOOT_ADDRESS) {
154160
/* implicit cast abide compiler warning */
155161
memset((void*)address, 0, len);
@@ -179,6 +185,10 @@ int hal_flash_write(uintptr_t address, const uint8_t *data, int len)
179185

180186
int hal_flash_erase(uintptr_t address, int len)
181187
{
188+
if (flashLocked == 1) {
189+
wolfBoot_printf("FLASH IS BEING ERASED WHILE LOCKED\n");
190+
return -1;
191+
}
182192
/* implicit cast abide compiler warning */
183193
wolfBoot_printf( "hal_flash_erase addr %p len %d\n", (void*)address, len);
184194
if (address == erasefail_address + WOLFBOOT_PARTITION_BOOT_ADDRESS) {
@@ -227,16 +237,20 @@ void hal_init(void)
227237

228238
void ext_flash_lock(void)
229239
{
230-
/* no op */
240+
extFlashLocked = 1;
231241
}
232242

233243
void ext_flash_unlock(void)
234244
{
235-
/* no op */
245+
extFlashLocked = 0;
236246
}
237247

238248
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
239249
{
250+
if (extFlashLocked == 1) {
251+
wolfBoot_printf("EXT FLASH IS BEING WRITTEN TO WHILE LOCKED\n");
252+
return -1;
253+
}
240254
memcpy(flash_base + address, data, len);
241255
return 0;
242256
}
@@ -249,6 +263,10 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
249263

250264
int ext_flash_erase(uintptr_t address, int len)
251265
{
266+
if (extFlashLocked == 1) {
267+
wolfBoot_printf("EXT FLASH IS BEING ERASED WHILE LOCKED\n");
268+
return -1;
269+
}
252270
memset(flash_base + address, FLASH_BYTE_ERASED, len);
253271
return 0;
254272
}
@@ -287,6 +305,14 @@ void do_boot(const uint32_t *app_offset)
287305
int ret;
288306
size_t app_size = WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE;
289307

308+
if (flashLocked == 0) {
309+
wolfBoot_printf("WARNING FLASH IS UNLOCKED AT BOOT");
310+
}
311+
312+
if (extFlashLocked == 0) {
313+
wolfBoot_printf("WARNING EXT FLASH IS UNLOCKED AT BOOT");
314+
}
315+
290316
#ifdef __APPLE__
291317
typedef int (*main_entry)(int, char**, char**, char**);
292318
NSObjectFileImage fileImage = NULL;

src/update_flash.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,36 +245,38 @@ static int wolfBoot_swap_and_final_erase(int resume)
245245
if ((resume == 1) && (swapDone == 0) && (st != IMG_STATE_FINAL_FLAGS)) {
246246
return -1;
247247
}
248+
248249
hal_flash_unlock();
249-
#ifdef EXT_FLASH
250+
251+
/* IMG_STATE_FINAL_FLAGS allows re-entry without blowing away swap */
252+
if (st != IMG_STATE_FINAL_FLAGS) {
253+
/* store the sector at tmpBootPos into swap */
254+
wolfBoot_copy_sector(boot, swap, tmpBootPos / WOLFBOOT_SECTOR_SIZE);
255+
/* set FINAL_SWAP for re-entry */
256+
wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_FINAL_FLAGS);
257+
}
258+
#ifdef EXT_ENCRYPTED
250259
ext_flash_unlock();
251-
#endif
252260

253261
if (swapDone == 0) {
254-
/* IMG_STATE_FINAL_FLAGS allows re-entry without blowing away swap */
255-
if (st != IMG_STATE_FINAL_FLAGS) {
256-
/* store the sector at tmpBootPos into swap */
257-
wolfBoot_copy_sector(boot, swap, tmpBootPos / WOLFBOOT_SECTOR_SIZE);
258-
/* set FINAL_SWAP for re-entry */
259-
wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_FINAL_FLAGS);
260-
}
261-
#ifdef EXT_ENCRYPTED
262262
/* get encryption key and iv if encryption is enabled */
263263
wolfBoot_get_encrypt_key((uint8_t*)tmpBuffer,
264264
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE/sizeof(uint32_t)]);
265-
#endif
266265
/* write TRAIL, encryption key and iv if enabled to tmpBootPos*/
267266
tmpBuffer[TRAILER_OFFSET_WORDS] = WOLFBOOT_MAGIC_TRAIL;
268267

269268
wb_flash_erase(boot, tmpBootPos, WOLFBOOT_SECTOR_SIZE);
270269
wb_flash_write(boot, tmpBootPos, (void*)tmpBuffer, sizeof(tmpBuffer));
271270
}
271+
#endif
272272
/* erase the last boot sector(s) */
273273
wb_flash_erase(boot, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen);
274274
/* set the encryption key */
275275
#ifdef EXT_ENCRYPTED
276276
wolfBoot_set_encrypt_key((uint8_t*)tmpBuffer,
277277
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE/sizeof(uint32_t)]);
278+
/* wolfBoot_set_encrypt_key calls hal_flash_unlock, need to unlock again */
279+
hal_flash_unlock();
278280
#endif
279281
/* write the original contents of tmpBootPos back */
280282
if (tmpBootPos < boot->fw_size + IMAGE_HEADER_SIZE) {

0 commit comments

Comments
 (0)