Skip to content

Commit 29aed1d

Browse files
de-nordicutzig
authored andcommitted
bootutil: Application of boot_read_swap_state
The commit reorganizes logic of the boot_swap_sectors and the boot_set_pending_multi to use the boot_read_swap_state, instead of previously used boot_read_swap_state_by_id, to utilize the flash area object the functions area obtaining. It also fixes double a flash_area_open that happened implicitly within the boot_swap_sectors, as it has been calling boot_read_swap_state_by_id on the FLASH_AREA_IMAGE_SCRATCH, after it has already opened the area. Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 0ab87b6 commit 29aed1d

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

boot/bootutil/src/bootutil_public.c

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -492,24 +492,23 @@ boot_set_pending_multi(int image_index, int permanent)
492492
uint8_t swap_type;
493493
int rc;
494494

495-
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
496-
&state_secondary_slot);
495+
rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
496+
if (rc != 0) {
497+
return BOOT_EFLASH;
498+
}
499+
500+
rc = boot_read_swap_state(fap, &state_secondary_slot);
497501
if (rc != 0) {
498-
return rc;
502+
goto done;
499503
}
500504

501505
switch (state_secondary_slot.magic) {
502506
case BOOT_MAGIC_GOOD:
503507
/* Swap already scheduled. */
504-
return 0;
508+
break;
505509

506510
case BOOT_MAGIC_UNSET:
507-
rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
508-
if (rc != 0) {
509-
rc = BOOT_EFLASH;
510-
} else {
511-
rc = boot_write_magic(fap);
512-
}
511+
rc = boot_write_magic(fap);
513512

514513
if (rc == 0 && permanent) {
515514
rc = boot_write_image_ok(fap);
@@ -524,26 +523,24 @@ boot_set_pending_multi(int image_index, int permanent)
524523
rc = boot_write_swap_info(fap, swap_type, 0);
525524
}
526525

527-
flash_area_close(fap);
528-
return rc;
526+
break;
529527

530528
case BOOT_MAGIC_BAD:
531529
/* The image slot is corrupt. There is no way to recover, so erase the
532530
* slot to allow future upgrades.
533531
*/
534-
rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
535-
if (rc != 0) {
536-
return BOOT_EFLASH;
537-
}
538-
539532
flash_area_erase(fap, 0, fap->fa_size);
540-
flash_area_close(fap);
541-
return BOOT_EBADIMAGE;
533+
rc = BOOT_EBADIMAGE;
534+
break;
542535

543536
default:
544537
assert(0);
545-
return BOOT_EBADIMAGE;
538+
rc = BOOT_EBADIMAGE;
546539
}
540+
541+
done:
542+
flash_area_close(fap);
543+
return rc;
547544
}
548545

549546
/**
@@ -577,14 +574,18 @@ boot_set_pending(int permanent)
577574
int
578575
boot_set_confirmed_multi(int image_index)
579576
{
580-
const struct flash_area *fap;
577+
const struct flash_area *fap = NULL;
581578
struct boot_swap_state state_primary_slot;
582579
int rc;
583580

584-
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
585-
&state_primary_slot);
581+
rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
582+
if (rc != 0) {
583+
return BOOT_EFLASH;
584+
}
585+
586+
rc = boot_read_swap_state(fap, &state_primary_slot);
586587
if (rc != 0) {
587-
return rc;
588+
goto done;
588589
}
589590

590591
switch (state_primary_slot.magic) {
@@ -594,16 +595,11 @@ boot_set_confirmed_multi(int image_index)
594595

595596
case BOOT_MAGIC_UNSET:
596597
/* Already confirmed. */
597-
return 0;
598+
goto done;
598599

599600
case BOOT_MAGIC_BAD:
600601
/* Unexpected state. */
601-
return BOOT_EBADVECT;
602-
}
603-
604-
rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
605-
if (rc) {
606-
rc = BOOT_EFLASH;
602+
rc = BOOT_EBADVECT;
607603
goto done;
608604
}
609605

boot/bootutil/src/swap_scratch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,7 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
619619
(BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
620620
BOOT_STATUS_ASSERT(rc == 0);
621621

622-
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
623-
&swap_state);
622+
rc = boot_read_swap_state(fap_scratch, &swap_state);
624623
assert(rc == 0);
625624

626625
if (swap_state.image_ok == BOOT_FLAG_SET) {

0 commit comments

Comments
 (0)