Commit 10ee6481 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

Update bootutil to reduce amount of global state

The boot loader state currently exists as global variable (boot_data)
which is accessed by all routines; this updates all routines that
require access to the state to receive it by parameter.

Variables that are declared as "static" (globals) were #ifdef'ed to
only use static when building a native bootloader, to avoid stack
allocation. When bootutil is built to run in the simulator they are
stack allocated to avoid thread share.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent b0f04733
Loading
Loading
Loading
Loading
+1 −1
Changes for boot/boot_serial/src/boot_serial.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -178,7 +178,7 @@ bs_list(char *buf, int len)
        flash_area_read(fap, 0, &hdr, sizeof(hdr));

        if (hdr.ih_magic != IMAGE_MAGIC ||
          bootutil_img_validate(0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
          bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
                                NULL, 0, NULL)) {
            flash_area_close(fap);
            continue;
+12 −8
Changes for boot/bootutil/include/bootutil/enc_key.h: 12 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -49,14 +49,18 @@ struct enc_key_data {

extern const struct bootutil_key bootutil_enc_key;

int boot_enc_set_key(uint8_t slot, uint8_t *enckey);
int boot_enc_load(int image_index, const struct image_header *hdr,
        const struct flash_area *fap, uint8_t *enckey);
bool boot_enc_valid(int image_index, const struct flash_area *fap);
void boot_enc_mark_keys_invalid(void);
void boot_encrypt(int image_index, const struct flash_area *fap, uint32_t off,
        uint32_t sz, uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(void);
int boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
        uint8_t *enckey);
int boot_enc_load(struct enc_key_data *enc_state, int image_index,
        const struct image_header *hdr, const struct flash_area *fap,
        uint8_t *enckey);
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
        const struct flash_area *fap);
void boot_enc_mark_keys_invalid(struct enc_key_data *enc_state);
void boot_encrypt(struct enc_key_data *enc_state, int image_index,
        const struct flash_area *fap, uint32_t off, uint32_t sz,
        uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(struct enc_key_data *enc_state);

#ifdef __cplusplus
}
+4 −1
Changes for boot/bootutil/include/bootutil/image.h: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#define H_IMAGE_

#include <inttypes.h>
#include <stdbool.h>

#ifdef __ZEPHYR__
#include <toolchain/gcc.h>
@@ -134,7 +135,9 @@ _Static_assert(sizeof(struct image_header) == IMAGE_HEADER_SIZE,
               "struct image_header not required size");
#endif

int bootutil_img_validate(int image_index, struct image_header *hdr,
struct enc_key_data;
int bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
                          struct image_header *hdr,
                          const struct flash_area *fap,
                          uint8_t *tmp_buf, uint32_t tmp_buf_sz,
                          uint8_t *seed, int seed_len, uint8_t *out_hash);
+1
Changes for boot/bootutil/src/bootutil_misc.c: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@

MCUBOOT_LOG_MODULE_DECLARE(mcuboot);

/* Currently only used by imgmgr */
int boot_current_slot;

const uint32_t boot_img_magic[] = {
+15 −64
Changes for boot/bootutil/src/bootutil_priv.h: 15 added lines, 64 removed lines.
Original line number Diff line number Diff line
@@ -215,6 +215,15 @@ struct boot_loader_state {

    uint8_t swap_type[BOOT_IMAGE_NUMBER];
    uint8_t write_sz;

#if defined(MCUBOOT_ENC_IMAGES)
    /*
     * TODO: This could later be expanded to use a different set of keys
     * per image.
     */
    struct enc_key_data enc[BOOT_NUM_SLOTS];
#endif

#if (BOOT_IMAGE_NUMBER > 1)
    uint8_t curr_img_idx;
#endif
@@ -233,7 +242,7 @@ int boot_read_swap_state(const struct flash_area *fap,
int boot_read_swap_state_by_id(int flash_area_id,
                               struct boot_swap_state *state);
int boot_write_magic(const struct flash_area *fap);
int boot_write_status(struct boot_status *bs);
int boot_write_status(struct boot_loader_state *state, struct boot_status *bs);
int boot_schedule_test_swap(void);
int boot_write_copy_done(const struct flash_area *fap);
int boot_write_image_ok(const struct flash_area *fap);
@@ -274,7 +283,7 @@ boot_img_hdr(struct boot_loader_state *state, size_t slot)
}

static inline size_t
boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
{
    return BOOT_IMG(state, slot).num_sectors;
}
@@ -302,7 +311,7 @@ static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS

static inline size_t
boot_img_sector_size(struct boot_loader_state *state,
boot_img_sector_size(const struct boot_loader_state *state,
                     size_t slot, size_t sector)
{
    return BOOT_IMG(state, slot).sectors[sector].fa_size;
@@ -313,88 +322,30 @@ boot_img_sector_size(struct boot_loader_state *state,
 * device.
 */
static inline uint32_t
boot_img_sector_off(struct boot_loader_state *state, size_t slot,
boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
                    size_t sector)
{
    return BOOT_IMG(state, slot).sectors[sector].fa_off -
           BOOT_IMG(state, slot).sectors[0].fa_off;
}

static inline int
boot_initialize_area(struct boot_loader_state *state, int flash_area)
{
    int num_sectors = BOOT_MAX_IMG_SECTORS;
    int rc;

    if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
        rc = flash_area_to_sectors(flash_area, &num_sectors,
                                   BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
        BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;

    } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
        rc = flash_area_to_sectors(flash_area, &num_sectors,
                                 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
        BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;

    } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
        rc = flash_area_to_sectors(flash_area, &num_sectors,
                                   state->scratch.sectors);
        state->scratch.num_sectors = (size_t)num_sectors;
    } else {
        return BOOT_EFLASH;
    }

    return rc;
}

#else  /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */

static inline size_t
boot_img_sector_size(struct boot_loader_state *state,
boot_img_sector_size(const struct boot_loader_state *state,
                     size_t slot, size_t sector)
{
    return BOOT_IMG(state, slot).sectors[sector].fs_size;
}

static inline uint32_t
boot_img_sector_off(struct boot_loader_state *state, size_t slot,
boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
                    size_t sector)
{
    return BOOT_IMG(state, slot).sectors[sector].fs_off -
           BOOT_IMG(state, slot).sectors[0].fs_off;
}

static inline int
boot_initialize_area(struct boot_loader_state *state, int flash_area)
{
    uint32_t num_sectors;
    struct flash_sector *out_sectors;
    size_t *out_num_sectors;
    int rc;

    num_sectors = BOOT_MAX_IMG_SECTORS;

    if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
        out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
        out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
    } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
        out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
        out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
    } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
        out_sectors = state->scratch.sectors;
        out_num_sectors = &state->scratch.num_sectors;
    } else {
        return BOOT_EFLASH;
    }

    rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
    if (rc != 0) {
        return rc;
    }
    *out_num_sectors = num_sectors;
    return 0;
}

#endif  /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */

#ifdef __cplusplus
Loading