Commit 29aed1d8 authored by Dominik Ermel's avatar Dominik Ermel Committed by Fabio Utzig
Browse files

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: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent 0ab87b61
Loading
Loading
Loading
Loading
+26 −30
Changes for boot/bootutil/src/bootutil_public.c: 26 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -492,24 +492,23 @@ boot_set_pending_multi(int image_index, int permanent)
    uint8_t swap_type;
    int rc;

    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
                                    &state_secondary_slot);
    rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
    if (rc != 0) {
        return rc;
        return BOOT_EFLASH;
    }

    rc = boot_read_swap_state(fap, &state_secondary_slot);
    if (rc != 0) {
        goto done;
    }

    switch (state_secondary_slot.magic) {
    case BOOT_MAGIC_GOOD:
        /* Swap already scheduled. */
        return 0;
        break;

    case BOOT_MAGIC_UNSET:
        rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
        if (rc != 0) {
            rc = BOOT_EFLASH;
        } else {
        rc = boot_write_magic(fap);
        }

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

        flash_area_close(fap);
        return rc;
        break;

    case BOOT_MAGIC_BAD:
        /* The image slot is corrupt.  There is no way to recover, so erase the
         * slot to allow future upgrades.
         */
        rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
        if (rc != 0) {
            return BOOT_EFLASH;
        }

        flash_area_erase(fap, 0, fap->fa_size);
        flash_area_close(fap);
        return BOOT_EBADIMAGE;
        rc = BOOT_EBADIMAGE;
        break;

    default:
        assert(0);
        return BOOT_EBADIMAGE;
        rc = BOOT_EBADIMAGE;
    }

done:
    flash_area_close(fap);
    return rc;
}

/**
@@ -577,14 +574,18 @@ boot_set_pending(int permanent)
int
boot_set_confirmed_multi(int image_index)
{
    const struct flash_area *fap;
    const struct flash_area *fap = NULL;
    struct boot_swap_state state_primary_slot;
    int rc;

    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
                                    &state_primary_slot);
    rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
    if (rc != 0) {
        return rc;
        return BOOT_EFLASH;
    }

    rc = boot_read_swap_state(fap, &state_primary_slot);
    if (rc != 0) {
        goto done;
    }

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

    case BOOT_MAGIC_UNSET:
        /* Already confirmed. */
        return 0;
        goto done;

    case BOOT_MAGIC_BAD:
        /* Unexpected state. */
        return BOOT_EBADVECT;
    }

    rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
    if (rc) {
        rc = BOOT_EFLASH;
        rc = BOOT_EBADVECT;
        goto done;
    }

+1 −2
Changes for boot/bootutil/src/swap_scratch.c: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -619,8 +619,7 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
                        (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
            BOOT_STATUS_ASSERT(rc == 0);

            rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
                                            &swap_state);
            rc = boot_read_swap_state(fap_scratch, &swap_state);
            assert(rc == 0);

            if (swap_state.image_ok == BOOT_FLAG_SET) {