Commit 73c38c6f authored by Kristine Jassmann's avatar Kristine Jassmann Committed by David Brown
Browse files

bootutil: Allow larger minimum flash write



[kristine.jassmann@renesas.com: Allow larger minimum flash write]
[michael.thomas@renesas.com: Add changes for 1.8]
[michael.thomas@renesas.com: Add magic alignment fix]
[gustavo.nihei@espressif.com: bootutil: Address issues from PR 949]

Co-authored-by: default avatarKristine Jassmann <kristine.jassmann@renesas.com>
Co-authored-by: default avatarMichael Thomas <michael.thomas@renesas.com>
Co-authored-by: default avatarGustavo Henrique Nihei <gustavo.nihei@espressif.com>
Signed-off-by: default avatarKristine Jassmann <kristine.jassmann@renesas.com>
Signed-off-by: default avatarMichael Thomas <michael.thomas@renesas.com>
Signed-off-by: default avatarGustavo Henrique Nihei <gustavo.nihei@espressif.com>
parent 1eedec3e
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -71,7 +71,10 @@ struct image_trailer {
    uint8_t pad2[BOOT_MAX_ALIGN - 1];
    uint8_t image_ok;
    uint8_t pad3[BOOT_MAX_ALIGN - 1];
    uint8_t magic[16];
#if BOOT_MAX_ALIGN > BOOT_MAGIC_SZ
    uint8_t pad4[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ];
#endif
    uint8_t magic[BOOT_MAGIC_SZ];
};

/* you must have pre-allocated all the entries within this structure */
+20 −2
Original line number Diff line number Diff line
@@ -47,6 +47,14 @@
extern "C" {
#endif

#ifndef ALIGN_UP
#define ALIGN_UP(num, align)    (((num) + ((align) - 1)) & ~((align) - 1))
#endif

#ifndef ALIGN_DOWN
#define ALIGN_DOWN(num, align)  ((num) & ~((align) - 1))
#endif

/** Attempt to boot the contents of the primary slot. */
#define BOOT_SWAP_TYPE_NONE     1

@@ -71,7 +79,19 @@ extern "C" {
/** Swapping encountered an unrecoverable error */
#define BOOT_SWAP_TYPE_PANIC    0xff

#define BOOT_MAGIC_SZ           16

#ifdef MCUBOOT_BOOT_MAX_ALIGN

_Static_assert(MCUBOOT_BOOT_MAX_ALIGN >= 8 && MCUBOOT_BOOT_MAX_ALIGN <= 32,
               "Unsupported value for MCUBOOT_BOOT_MAX_ALIGN");

#define BOOT_MAX_ALIGN          MCUBOOT_BOOT_MAX_ALIGN
#define BOOT_MAGIC_ALIGN_SIZE   ALIGN_UP(BOOT_MAGIC_SZ, BOOT_MAX_ALIGN)
#else
#define BOOT_MAX_ALIGN          8
#define BOOT_MAGIC_ALIGN_SIZE   BOOT_MAGIC_SZ
#endif

#define BOOT_MAGIC_GOOD     1
#define BOOT_MAGIC_BAD      2
@@ -87,8 +107,6 @@ extern "C" {
#define BOOT_FLAG_UNSET     3
#define BOOT_FLAG_ANY       4  /* NOTE: control only, not dependent on sector */

#define BOOT_MAGIC_SZ (sizeof boot_img_magic)

#define BOOT_EFLASH      1
#define BOOT_EFILE       2
#define BOOT_EBADIMAGE   3
+1 −4
Original line number Diff line number Diff line
@@ -38,10 +38,7 @@
extern "C" {
#endif

#define BOOT_ENC_KEY_SIZE_BITS  (BOOT_ENC_KEY_SIZE * 8)

#define BOOT_ENC_TLV_ALIGN_SIZE \
    ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN)
#define BOOT_ENC_TLV_ALIGN_SIZE ALIGN_UP(BOOT_ENC_TLV_SIZE, BOOT_MAX_ALIGN)

struct enc_key_data {
    uint8_t valid;
+7 −1
Original line number Diff line number Diff line
@@ -32,14 +32,20 @@
extern "C" {
#endif

#ifndef ALIGN_UP
#define ALIGN_UP(num, align)    (((num) + ((align) - 1)) & ~((align) - 1))
#endif

#ifdef MCUBOOT_AES_256
#define BOOT_ENC_KEY_SIZE       32
#else
#define BOOT_ENC_KEY_SIZE       16
#endif

#define BOOT_ENC_KEY_ALIGN_SIZE ALIGN_UP(BOOT_ENC_KEY_SIZE, BOOT_MAX_ALIGN)

#define TLV_ENC_RSA_SZ    256
#define TLV_ENC_KW_SZ     BOOT_ENC_KEY_SIZE + 8
#define TLV_ENC_KW_SZ     (BOOT_ENC_KEY_SIZE + 8)
#define TLV_ENC_EC256_SZ  (65 + 32 + BOOT_ENC_KEY_SIZE)
#define TLV_ENC_X25519_SZ (32 + 32 + BOOT_ENC_KEY_SIZE)

+17 −9
Original line number Diff line number Diff line
@@ -108,12 +108,12 @@ boot_trailer_info_sz(void)
#  if MCUBOOT_SWAP_SAVE_ENCTLV
           BOOT_ENC_TLV_ALIGN_SIZE * 2            +
#  else
           BOOT_ENC_KEY_SIZE * 2                  +
           BOOT_ENC_KEY_ALIGN_SIZE * 2            +
#  endif
#endif
           /* swap_type + copy_done + image_ok + swap_size */
           BOOT_MAX_ALIGN * 4                     +
           BOOT_MAGIC_SZ
           BOOT_MAGIC_ALIGN_SIZE
           );
}

@@ -190,6 +190,15 @@ boot_status_off(const struct flash_area *fap)
    return flash_area_get_size(fap) - off_from_end;
}

static int
boot_magic_decode(const uint32_t *magic)
{
    if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
        return BOOT_MAGIC_GOOD;
    }
    return BOOT_MAGIC_BAD;
}

static inline uint32_t
boot_magic_off(const struct flash_area *fap)
{
@@ -199,7 +208,7 @@ boot_magic_off(const struct flash_area *fap)
static inline uint32_t
boot_image_ok_off(const struct flash_area *fap)
{
    return boot_magic_off(fap) - BOOT_MAX_ALIGN;
    return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
}

static inline uint32_t
@@ -219,10 +228,9 @@ static inline uint32_t
boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
{
#if MCUBOOT_SWAP_SAVE_ENCTLV
    return boot_swap_size_off(fap) - ((slot + 1) *
            ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
    return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_TLV_ALIGN_SIZE);
#else
    return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
    return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_ALIGN_SIZE);
#endif
}
#endif
@@ -272,7 +280,7 @@ boot_find_status(int image_index, const struct flash_area **fap)
            return rc;
        }

        if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
        if (BOOT_MAGIC_GOOD == boot_magic_decode(magic)) {
            return 0;
        }

@@ -327,7 +335,7 @@ boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
            }
        }
#else
        rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
        rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE);
#endif
        flash_area_close(fap);
    }
@@ -375,7 +383,7 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
#if MCUBOOT_SWAP_SAVE_ENCTLV
    rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
#else
    rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
    rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE);
#endif
    if (rc != 0) {
        return BOOT_EFLASH;
Loading