Skip to content

Commit 8b954d4

Browse files
committed
Fix for update_flash.c error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]. Cleanup the swap logic for EXT_ENCRYPTED. Switch to using a uint32_t directly for checking tail magic.
1 parent f0c4252 commit 8b954d4

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/update_flash.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,51 +195,50 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
195195
}
196196

197197
#ifndef DISABLE_BACKUP
198+
199+
#ifdef EXT_ENCRYPTED
200+
# define TAIL_OFFSET_WORDS \
201+
((ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE) / sizeof(uint32_t))
202+
#else
203+
# define TAIL_OFFSET_WORDS 0
204+
#endif
205+
198206
static int wolfBoot_swap_and_final_erase(int resume)
199207
{
200208
struct wolfBoot_image boot[1];
201209
struct wolfBoot_image update[1];
202210
struct wolfBoot_image swap[1];
203211
uint8_t st;
204-
int eraseLen = WOLFBOOT_SECTOR_SIZE
205-
#ifdef NVM_FLASH_WRITEONCE
206-
/* need to erase the redundant sector too */
212+
int eraseLen = (WOLFBOOT_SECTOR_SIZE
213+
#ifdef NVM_FLASH_WRITEONCE /* need to erase the redundant sector too */
207214
* 2
208215
#endif
209-
;
216+
);
210217
int swapDone = 0;
211218
uintptr_t tmpBootPos = WOLFBOOT_PARTITION_SIZE - eraseLen -
212219
WOLFBOOT_SECTOR_SIZE;
213-
/* final swap and erase flag is WOLFBOOT_MAGIC_TRAIL */
214-
uint8_t tmpBuffer[sizeof(WOLFBOOT_MAGIC_TRAIL)
215-
#ifdef EXT_ENCRYPTED
216-
+ ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE
217-
#endif
218-
];
220+
uint32_t tmpBuffer[TAIL_OFFSET_WORDS + 1];
221+
219222
/* open boot */
220223
wolfBoot_open_image(boot, PART_BOOT);
221224
/* open update */
222225
wolfBoot_open_image(update, PART_UPDATE);
223226
/* open swap */
224227
wolfBoot_open_image(swap, PART_SWAP);
225228
wolfBoot_get_partition_state(PART_UPDATE, &st);
226-
/* read from tmpBootPos */
227-
memcpy((void*)tmpBuffer, (void*)(boot->hdr + tmpBootPos),
228-
sizeof(tmpBuffer));
229-
/* check for TRAIL */
230-
#ifdef EXT_ENCRYPTED
231-
if (*(uint32_t*)(tmpBuffer + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE) ==
232-
WOLFBOOT_MAGIC_TRAIL) {
233-
swapDone = 1;
234-
}
235-
#else
236-
if (((uint32_t*)tmpBuffer)[0] == WOLFBOOT_MAGIC_TRAIL) {
229+
230+
/* read tail */
231+
memcpy(tmpBuffer, boot->hdr + tmpBootPos, sizeof(tmpBuffer));
232+
233+
/* check for trailing magic (BOOT) */
234+
/* final swap and erase flag is WOLFBOOT_MAGIC_TRAIL */
235+
if (tmpBuffer[TAIL_OFFSET_WORDS] == WOLFBOOT_MAGIC_TRAIL) {
237236
swapDone = 1;
238237
}
239-
#endif
240238
/* if resuming, quit if swap isn't done */
241-
if ((resume == 1) && (swapDone == 0) && (st != IMG_STATE_FINAL_FLAGS))
239+
if ((resume == 1) && (swapDone == 0) && (st != IMG_STATE_FINAL_FLAGS)) {
242240
return -1;
241+
}
243242
if (swapDone == 0) {
244243
/* IMG_STATE_FINAL_FLAGS allows re-entry without blowing away swap */
245244
if (st != IMG_STATE_FINAL_FLAGS) {
@@ -250,29 +249,29 @@ static int wolfBoot_swap_and_final_erase(int resume)
250249
}
251250
#ifdef EXT_ENCRYPTED
252251
/* get encryption key and iv if encryption is enabled */
253-
wolfBoot_get_encrypt_key(tmpBuffer, tmpBuffer + ENCRYPT_KEY_SIZE);
252+
wolfBoot_get_encrypt_key((uint8_t*)tmpBuffer,
253+
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE/sizeof(uint32_t)]);
254254
#endif
255255
/* write TRAIL, encryption key and iv if enabled to tmpBootPos*/
256-
#ifdef EXT_ENCRYPTED
257-
*(uint32_t*)(tmpBuffer + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE)
258-
= WOLFBOOT_MAGIC_TRAIL;
259-
#else
260-
((uint32_t*)tmpBuffer)[0] = WOLFBOOT_MAGIC_TRAIL;
261-
#endif
256+
tmpBuffer[TAIL_OFFSET_WORDS] = WOLFBOOT_MAGIC_TRAIL;
257+
262258
wb_flash_erase(boot, tmpBootPos, WOLFBOOT_SECTOR_SIZE);
263259
wb_flash_write(boot, tmpBootPos, (void*)tmpBuffer, sizeof(tmpBuffer));
264260
}
265261
/* erase the last boot sector(s) */
266262
wb_flash_erase(boot, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen);
267263
/* set the encryption key */
268264
#ifdef EXT_ENCRYPTED
269-
wolfBoot_set_encrypt_key(tmpBuffer, tmpBuffer + ENCRYPT_KEY_SIZE);
265+
wolfBoot_set_encrypt_key((uint8_t*)tmpBuffer,
266+
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE/sizeof(uint32_t)]);
270267
#endif
271268
/* write the original contents of tmpBootPos back */
272-
if (tmpBootPos < boot->fw_size + IMAGE_HEADER_SIZE)
269+
if (tmpBootPos < boot->fw_size + IMAGE_HEADER_SIZE) {
273270
wolfBoot_copy_sector(swap, boot, tmpBootPos / WOLFBOOT_SECTOR_SIZE);
274-
else
271+
}
272+
else {
275273
wb_flash_erase(boot, tmpBootPos, WOLFBOOT_SECTOR_SIZE);
274+
}
276275
/* mark boot as TESTING */
277276
wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_TESTING);
278277
/* erase the last sector(s) of update */

0 commit comments

Comments
 (0)