diff --git a/Makefile b/Makefile index 48c6cf24..cdb9b4ed 100644 --- a/Makefile +++ b/Makefile @@ -20,20 +20,18 @@ OBJS:= \ ./lib/bootutil/src/loader.o \ ./lib/bootutil/src/image_validate.o \ ./lib/bootutil/src/bootutil_misc.o \ -./src/run.o \ ./src/mem.o \ ./src/keys.o \ ./src/crypto.o \ -./src/startup_bl.o \ +./src/wolfboot.o \ ./src/main.o \ ./lib/wolfssl/wolfcrypt/src/sha256.o \ ./lib/wolfssl/wolfcrypt/src/hash.o \ ./lib/wolfssl/wolfcrypt/src/wolfmath.o \ ./lib/wolfssl/wolfcrypt/src/fe_low_mem.o -CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=1024 -ffreestanding -Wno-unused \ +CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wextra -Wno-main -Wstack-usage=1024 -ffreestanding -Wno-unused \ -Ilib/bootutil/include -Iinclude/ -Ilib/wolfssl -nostartfiles \ - -DBOOT_MAX_IMG_SECTORS=256 -DWOLFBOOT_VALIDATE_SLOT0 -DWOLFBOOT_USE_FLASHAREA_GET_SECTORS \ -nostdlib \ -DWOLFSSL_USER_SETTINGS \ -DPLATFORM_$(TARGET) diff --git a/lib/bootutil/include/bootutil/bootutil.h b/include/bootutil.h similarity index 100% rename from lib/bootutil/include/bootutil/bootutil.h rename to include/bootutil.h diff --git a/include/flash_map.h b/include/flash.h similarity index 92% rename from include/flash_map.h rename to include/flash.h index ca8d6f73..5f159677 100644 --- a/include/flash_map.h +++ b/include/flash.h @@ -24,9 +24,6 @@ #define H_UTIL_FLASH_MAP_ /** - * - * Provides abstraction of flash regions for type of use. - * I.e. dude where's my image? * * System will contain a map which contains flash areas. Every * region will contain flash identifier, offset within flash and length. @@ -130,13 +127,6 @@ uint8_t flash_area_align(const struct flash_area *); int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); -/* - * Similar to flash_area_get_sectors(), but return the values in an - * array of struct flash_area instead. - */ -__attribute__((deprecated)) -int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret); - int flash_area_id_from_image_slot(int slot); int flash_area_id_to_image_slot(int area_id); diff --git a/include/hal/hal_flash.h b/include/hal.h similarity index 100% rename from include/hal/hal_flash.h rename to include/hal.h diff --git a/include/sysflash/sysflash.h b/include/sysflash/sysflash.h deleted file mode 100644 index e08f9eb9..00000000 --- a/include/sysflash/sysflash.h +++ /dev/null @@ -1,10 +0,0 @@ -/* Manual version of auto-generated version. */ - -#ifndef __SYSFLASH_H__ -#define __SYSFLASH_H__ - -#define FLASH_AREA_IMAGE_0 1 -#define FLASH_AREA_IMAGE_1 2 -#define FLASH_AREA_IMAGE_SCRATCH 3 - -#endif /* __SYSFLASH_H__ */ diff --git a/lib/bootutil/include/bootutil/sha256.h b/lib/bootutil/include/bootutil/sha256.h deleted file mode 100644 index c5114ed4..00000000 --- a/lib/bootutil/include/bootutil/sha256.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __BOOTUTIL_CRYPTO_H_ -#define __BOOTUTIL_CRYPTO_H_ - -#include -#include - - -typedef wc_Sha256 bootutil_sha256_context; - -static inline void bootutil_sha256_init(bootutil_sha256_context *ctx) -{ - wc_InitSha256(ctx); -} - -static inline void bootutil_sha256_update(bootutil_sha256_context *ctx, - const void *data, - uint32_t data_len) -{ - wc_Sha256Update(ctx, data, data_len); -} - -static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx, - uint8_t *output) -{ - wc_Sha256Final(ctx, output); -} - -#endif diff --git a/lib/bootutil/src/bootutil_misc.c b/lib/bootutil/src/bootutil_misc.c index c82e0e7a..3c403f13 100644 --- a/lib/bootutil/src/bootutil_misc.c +++ b/lib/bootutil/src/bootutil_misc.c @@ -23,17 +23,83 @@ #include #include -#include "sysflash/sysflash.h" -#include "hal/hal_flash.h" +#include "hal.h" #include "printf.h" -#include "flash_map_backend/flash_map_backend.h" #include "bootutil/image.h" -#include "bootutil/bootutil.h" #include "bootutil_priv.h" +#include "bootutil.h" +#include "target.h" + +struct area { + struct flash_area whole; + struct flash_area *areas; + uint32_t num_areas; + uint8_t id; +}; + + +struct area_desc { + struct area slots[3]; + uint32_t num_slots; +}; + +static struct area_desc flash_areas[1] = { + { + .slots = { + { + .whole = { + .fa_id = FLASH_AREA_IMAGE_0, + .fa_device_id = 0, + .fa_off = FLASH_AREA_IMAGE_0_OFFSET, + .fa_size = FLASH_AREA_IMAGE_0_SIZE, + }, + .id = FLASH_AREA_IMAGE_0, + .num_areas = 1 + }, + { + .whole = { + .fa_id = FLASH_AREA_IMAGE_1, + .fa_device_id = 0, + .fa_off = FLASH_AREA_IMAGE_1_OFFSET, + .fa_size = FLASH_AREA_IMAGE_1_SIZE, + }, + .id = FLASH_AREA_IMAGE_1, + .num_areas = 1 + }, +#ifndef WOLFBOOT_OVERWRITE_ONLY + { + .whole = { + .fa_id = FLASH_AREA_IMAGE_SCRATCH, + .fa_device_id = 0, + .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET, + .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE, + }, + .id = FLASH_AREA_IMAGE_SCRATCH, + .num_areas = 1 + } + }, + .num_slots = 3 +#else + }, + .num_slots = 2 +#endif + } +} ; + +void boot_panic(void) +{ + while(1) + ; +} + +void boot_panic_unless(int x) +{ + if(!x) + while(1); +} -extern void assert(int); int boot_current_slot; @@ -136,7 +202,7 @@ boot_scratch_trailer_sz(uint8_t min_write_sz) static uint32_t boot_magic_off(const struct flash_area *fap) { - assert(offsetof(struct image_trailer, magic) == 16); + boot_panic_unless(offsetof(struct image_trailer, magic) == 16); return fap->fa_size - BOOT_MAGIC_SZ; } @@ -168,22 +234,22 @@ boot_status_off(const struct flash_area *fap) off_from_end = boot_slots_trailer_sz(elem_sz); } - assert(off_from_end <= fap->fa_size); + boot_panic_unless(off_from_end <= fap->fa_size); return fap->fa_size - off_from_end; } static uint32_t boot_copy_done_off(const struct flash_area *fap) { - assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH); - assert(offsetof(struct image_trailer, copy_done) == 0); + boot_panic_unless(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH); + boot_panic_unless(offsetof(struct image_trailer, copy_done) == 0); return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2; } static uint32_t boot_image_ok_off(const struct flash_area *fap) { - assert(offsetof(struct image_trailer, image_ok) == 8); + boot_panic_unless(offsetof(struct image_trailer, image_ok) == 8); return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN; } @@ -307,7 +373,7 @@ boot_read_swap_size(uint32_t *swap_size) goto out; } - assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); + boot_panic_unless(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); } off = boot_swap_size_off(fap); @@ -358,7 +424,7 @@ boot_write_flag(int flag, const struct flash_area *fap) } align = flash_area_align(fap); - assert(align <= BOOT_MAX_ALIGN); + boot_panic_unless(align <= BOOT_MAX_ALIGN); memset(buf, 0xFF, BOOT_MAX_ALIGN); buf[0] = BOOT_FLAG_SET; @@ -392,7 +458,7 @@ boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) off = boot_swap_size_off(fap); align = flash_area_align(fap); - assert(align <= BOOT_MAX_ALIGN); + boot_panic_unless(align <= BOOT_MAX_ALIGN); if (align < sizeof swap_size) { align = sizeof swap_size; } @@ -439,7 +505,7 @@ boot_swap_type(void) table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : "BUG; can't happen"); - assert(table->swap_type == BOOT_SWAP_TYPE_TEST || + boot_panic_unless(table->swap_type == BOOT_SWAP_TYPE_TEST || table->swap_type == BOOT_SWAP_TYPE_PERM || table->swap_type == BOOT_SWAP_TYPE_REVERT); return table->swap_type; @@ -494,8 +560,8 @@ boot_set_pending(int permanent) return rc; default: - /* XXX: Temporary assert. */ - assert(0); + /* XXX: Temporary boot_panic_unless. */ + boot_panic_unless(0); return -1; } } @@ -558,3 +624,145 @@ done: flash_area_close(fap); return rc; } + +uint8_t flash_area_align(const struct flash_area *area) +{ + (void)area; + return 1; +} + +int flash_area_open(uint8_t id, const struct flash_area **area) +{ + uint32_t i; + + for (i = 0; i < flash_areas->num_slots; i++) { + if (flash_areas->slots[i].id == id) + break; + } + if (i == flash_areas->num_slots) { + wolfBoot_printf("Unsupported area\n"); + boot_panic(); + } + + /* Unsure if this is right, just returning the first area. */ + *area = &flash_areas->slots[i].whole; + return 0; +} + +void flash_area_close(const struct flash_area *area) +{ + (void)area; +} + +/* + * Read/write/erase. Offset is relative from beginning of flash area. + */ +int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, + uint32_t len) +{ + unsigned int i; + uint8_t *src8, *dst8; + wolfBoot_printf("%s: area=%d, off=%x, len=%x", + __func__, area->fa_id, off, len); + if (!area) + return -1; + if ((off + len) > (area->fa_size)) + return -1; + src8 = (uint8_t *)(area->fa_off + off); + dst8 = (uint8_t *)dst; + for (i = 0; i < len; i++) { + dst8[i] = src8[i]; + } + return 0; +} + +int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, + uint32_t len) +{ + wolfBoot_printf("%s: area=%d, off=%x, len=%x", __func__, + area->fa_id, off, len); + hal_flash_unlock(); + hal_flash_write(area->fa_off + off, src, len); + hal_flash_lock(); + return 0; +} + +int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) +{ + wolfBoot_printf("%s: area=%d, off=%x, len=%x", __func__, + area->fa_id, off, len); + hal_flash_unlock(); + hal_flash_erase(area->fa_off + off, len); + hal_flash_lock(); + return 0; +} + +int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) +{ + uint32_t i; + struct area *slot; + + for (i = 0; i < flash_areas->num_slots; i++) { + if (flash_areas->slots[i].id == idx) + break; + } + if (i == flash_areas->num_slots) { + wolfBoot_printf("Unsupported area\n"); + boot_panic(); + } + + slot = &flash_areas->slots[i]; + + if (slot->num_areas > (uint32_t)*cnt) { + wolfBoot_printf("Too many areas in slot\n"); + boot_panic(); + } + + *cnt = slot->num_areas; + memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); + + return 0; +} + +int flash_area_get_sectors(int fa_id, uint32_t *count, + struct flash_sector *sectors) +{ + uint32_t i; + struct area *slot; + + for (i = 0; i < flash_areas->num_slots; i++) { + if (flash_areas->slots[i].id == fa_id) + break; + } + if (i == flash_areas->num_slots) { + wolfBoot_printf("Unsupported area\n"); + boot_panic(); + } + + slot = &flash_areas->slots[i]; + + if (slot->num_areas > *count) { + wolfBoot_printf("Too many areas in slot\n"); + boot_panic(); + } + + for (i = 0; i < slot->num_areas; i++) { + sectors[i].fs_off = slot->areas[i].fa_off - + slot->whole.fa_off; + sectors[i].fs_size = slot->areas[i].fa_size; + } + *count = slot->num_areas; + + return 0; +} + +int flash_area_id_from_image_slot(int slot) +{ + return slot + FLASH_AREA_IMAGE_0; +} + +uint8_t flash_area_erased_val(const struct flash_area *fap) +{ + (void)fap; + return 0xff; +} diff --git a/lib/bootutil/src/bootutil_priv.h b/lib/bootutil/src/bootutil_priv.h index f9389809..abc9771e 100644 --- a/lib/bootutil/src/bootutil_priv.h +++ b/lib/bootutil/src/bootutil_priv.h @@ -22,14 +22,22 @@ #ifndef H_BOOTUTIL_PRIV_ #define H_BOOTUTIL_PRIV_ -#include "sysflash/sysflash.h" -#include +#include "flash_map_backend.h" #include "bootutil/image.h" +#ifndef BOOT_MAX_IMG_SECTORS +# define BOOT_MAX_IMG_SECTORS (32) +#endif + +extern void boot_panic(void); +extern void boot_panic_unless(int x); + +#define FLASH_AREA_IMAGE_0 1 +#define FLASH_AREA_IMAGE_1 2 +#define FLASH_AREA_IMAGE_SCRATCH 3 -#define ASSERT assert struct flash_area; @@ -123,11 +131,7 @@ extern const uint32_t BOOT_MAGIC_SZ; * * This can be deleted when flash_area_to_sectors() is removed. */ -#ifdef WOLFBOOT_USE_FLASHAREA_GET_SECTORS typedef struct flash_sector boot_sector_t; -#else -typedef struct flash_area boot_sector_t; -#endif /** Private state maintained during boot. */ struct boot_loader_state { @@ -196,55 +200,6 @@ static inline size_t boot_scratch_area_size(struct boot_loader_state *state) return state->scratch_area->fa_size; } -#ifndef WOLFBOOT_USE_FLASHAREA_GET_SECTORS - -static inline size_t -boot_img_sector_size(struct boot_loader_state *state, - size_t slot, size_t sector) -{ - return state->imgs[slot].sectors[sector].fa_size; -} - -/* - * Offset of the sector from the beginning of the image, NOT the flash - * device. - */ -static inline uint32_t -boot_img_sector_off(struct boot_loader_state *state, size_t slot, - size_t sector) -{ - return state->imgs[slot].sectors[sector].fa_off - - state->imgs[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; - size_t slot; - int rc; - - switch (flash_area) { - case FLASH_AREA_IMAGE_0: - slot = 0; - break; - case FLASH_AREA_IMAGE_1: - slot = 1; - break; - default: - return BOOT_EFLASH; - } - - rc = flash_area_to_sectors(flash_area, &num_sectors, - state->imgs[slot].sectors); - if (rc != 0) { - return rc; - } - state->imgs[slot].num_sectors = (size_t)num_sectors; - return 0; -} - -#else /* defined(WOLFBOOT_USE_FLASHAREA_GET_SECTORS) */ static inline size_t boot_img_sector_size(struct boot_loader_state *state, @@ -292,7 +247,5 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area) return 0; } -#endif /* !defined(WOLFBOOT_USE_FLASHAREA_GET_SECTORS) */ - #endif diff --git a/include/flash_map_backend/flash_map_backend.h b/lib/bootutil/src/flash_map_backend.h similarity index 99% rename from include/flash_map_backend/flash_map_backend.h rename to lib/bootutil/src/flash_map_backend.h index f2df8c72..aa101697 100644 --- a/include/flash_map_backend/flash_map_backend.h +++ b/lib/bootutil/src/flash_map_backend.h @@ -22,7 +22,7 @@ #ifndef __FLASH_MAP_BACKEND_H__ #define __FLASH_MAP_BACKEND_H__ -#include +#include "flash.h" /** diff --git a/lib/bootutil/src/image_validate.c b/lib/bootutil/src/image_validate.c index bfa4f44d..9135db7e 100644 --- a/lib/bootutil/src/image_validate.c +++ b/lib/bootutil/src/image_validate.c @@ -23,13 +23,11 @@ #include #include -#include "hal/hal_flash.h" - -//#include +#include "hal.h" #include "bootutil/image.h" -#include "bootutil/sha256.h" #include "bootutil/sign_key.h" +#include "wolfssl/wolfcrypt/sha256.h" #include "wolfssl/ssl.h" @@ -43,18 +41,18 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result, uint8_t *seed, int seed_len) { - bootutil_sha256_context sha256_ctx; + wc_Sha256 sha256_ctx; uint32_t blk_sz; uint32_t size; uint32_t off; int rc; - bootutil_sha256_init(&sha256_ctx); + wc_InitSha256(&sha256_ctx); /* in some cases (split image) the hash is seeded with data from * the loader image */ if (seed && (seed_len > 0)) { - bootutil_sha256_update(&sha256_ctx, seed, seed_len); + wc_Sha256Update(&sha256_ctx, seed, seed_len); } /* @@ -71,9 +69,9 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, if (rc) { return rc; } - bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); + wc_Sha256Update(&sha256_ctx, tmp_buf, blk_sz); } - bootutil_sha256_finish(&sha256_ctx, hash_result); + wc_Sha256Final(&sha256_ctx, hash_result); return 0; } @@ -105,22 +103,22 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, #endif #ifdef EXPECTED_SIG_TLV -extern void assert(int); +extern void boot_panic_unless(int); static int bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) { - bootutil_sha256_context sha256_ctx; + wc_Sha256 sha256_ctx; int i; const struct bootutil_key *key; uint8_t hash[32]; - assert(keyhash_len <= 32); + boot_panic_unless(keyhash_len <= 32); for (i = 0; i < bootutil_key_cnt; i++) { key = &bootutil_keys[i]; - bootutil_sha256_init(&sha256_ctx); - bootutil_sha256_update(&sha256_ctx, key->key, *key->len); - bootutil_sha256_finish(&sha256_ctx, hash); + wc_InitSha256(&sha256_ctx); + wc_Sha256Update(&sha256_ctx, key->key, *key->len); + wc_Sha256Final(&sha256_ctx, hash); if (!memcmp(hash, keyhash, keyhash_len)) { return i; } diff --git a/lib/bootutil/src/loader.c b/lib/bootutil/src/loader.c index f49f39ed..6d543f11 100644 --- a/lib/bootutil/src/loader.c +++ b/lib/bootutil/src/loader.c @@ -25,21 +25,16 @@ */ #include -#include -#include "bootutil/bootutil.h" +#include +#include "bootutil.h" #include "bootutil/image.h" #include "bootutil_priv.h" #include "printf.h" static struct boot_loader_state boot_data; -void assert(int x) -{ - if(!x) - while(1); -} -#if defined(WOLFBOOT_VALIDATE_SLOT0) && !defined(WOLFBOOT_OVERWRITE_ONLY) +#ifndef WOLFBOOT_OVERWRITE_ONLY static int boot_status_fails = 0; #define BOOT_STATUS_ASSERT(x) \ do { \ @@ -48,7 +43,7 @@ static int boot_status_fails = 0; } \ } while (0) #else -#define BOOT_STATUS_ASSERT(x) ASSERT(x) +#define BOOT_STATUS_ASSERT(x) boot_panic_unless(x) #endif struct boot_status_table { @@ -161,10 +156,10 @@ boot_status_source(void) uint8_t source; rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch); - assert(rc == 0); + boot_panic_unless(rc == 0); BOOT_LOG_SWAP_STATE("Image 0", &state_slot0); BOOT_LOG_SWAP_STATE("Scratch", &state_scratch); @@ -439,13 +434,6 @@ boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs) * swap. Tell user and move on to validation! */ wolfBoot_printf("Detected inconsistent status!"); - -#if !defined(WOLFBOOT_VALIDATE_SLOT0) - /* With validation of slot0 disabled, there is no way to be sure the - * swapped slot0 is OK, so abort! - */ - assert(0); -#endif } if (found) { @@ -495,7 +483,7 @@ boot_read_status(struct boot_status *bs) break; default: - assert(0); + boot_panic_unless(0); return BOOT_EBADARGS; } @@ -830,29 +818,27 @@ boot_status_init_by_id(int flash_area_id, const struct boot_status *bs) int rc; rc = flash_area_open(flash_area_id, &fap); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state); - assert(rc == 0); + boot_panic_unless(rc == 0); if (swap_state.image_ok == BOOT_FLAG_SET) { rc = boot_write_image_ok(fap); - assert(rc == 0); + boot_panic_unless(rc == 0); } rc = boot_write_swap_size(fap, bs->swap_size); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_write_magic(fap); - assert(rc == 0); + boot_panic_unless(rc == 0); flash_area_close(fap); return 0; } -#endif -#ifndef WOLFBOOT_OVERWRITE_ONLY static int boot_erase_last_sector_by_id(int flash_area_id) { @@ -875,11 +861,10 @@ boot_erase_last_sector_by_id(int flash_area_id) rc = boot_erase_sector(flash_area_id, boot_img_sector_off(&boot_data, slot, last_sector), boot_img_sector_size(&boot_data, slot, last_sector)); - assert(rc == 0); + boot_panic_unless(rc == 0); return rc; } -#endif /* !WOLFBOOT_OVERWRITE_ONLY */ /** * Swaps the contents of two flash regions within the two image slots. @@ -892,7 +877,6 @@ boot_erase_last_sector_by_id(int flash_area_id) * * @return 0 on success; nonzero on failure. */ -#ifndef WOLFBOOT_OVERWRITE_ONLY static void boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) { @@ -929,11 +913,11 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) if (bs->state == 0) { rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH, img_off, 0, copy_sz); - assert(rc == 0); + boot_panic_unless(rc == 0); if (bs->idx == 0) { if (bs->use_scratch) { @@ -944,7 +928,7 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) * safe to erase. */ rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0); - assert(rc == 0); + boot_panic_unless(rc == 0); boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs); } @@ -957,18 +941,18 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) if (bs->state == 1) { rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1, img_off, img_off, copy_sz); - assert(rc == 0); + boot_panic_unless(rc == 0); if (bs->idx == 0 && !bs->use_scratch) { /* If not all sectors of the slot are being swapped, * guarantee here that only slot0 will have the state. */ rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1); - assert(rc == 0); + boot_panic_unless(rc == 0); } bs->state = 2; @@ -978,23 +962,23 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) if (bs->state == 2) { rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz); - assert(rc == 0); + boot_panic_unless(rc == 0); /* NOTE: also copy trailer from scratch (has status info) */ rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0, 0, img_off, copy_sz); - assert(rc == 0); + boot_panic_unless(rc == 0); if (bs->use_scratch) { rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); - assert(rc == 0); + boot_panic_unless(rc == 0); scratch_trailer_off = boot_status_off(fap); flash_area_close(fap); rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); - assert(rc == 0); + boot_panic_unless(rc == 0); /* copy current status that is being maintained in scratch */ rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0, @@ -1005,18 +989,18 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &swap_state); - assert(rc == 0); + boot_panic_unless(rc == 0); if (swap_state.image_ok == BOOT_FLAG_SET) { rc = boot_write_image_ok(fap); - assert(rc == 0); + boot_panic_unless(rc == 0); } rc = boot_write_swap_size(fap, bs->swap_size); - assert(rc == 0); + boot_panic_unless(rc == 0); rc = boot_write_magic(fap); - assert(rc == 0); + boot_panic_unless(rc == 0); flash_area_close(fap); } @@ -1030,6 +1014,8 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) } #endif /* !WOLFBOOT_OVERWRITE_ONLY */ + +#ifdef WOLFBOOT_OVERWRITE_ONLY /** * Swaps the two images in flash. If a prior copy operation was interrupted * by a system reset, this function completes that operation. @@ -1042,7 +1028,6 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs) * * @return 0 on success; nonzero on failure. */ -#ifdef WOLFBOOT_OVERWRITE_ONLY static int boot_copy_image(struct boot_status *bs) { @@ -1058,7 +1043,7 @@ boot_copy_image(struct boot_status *bs) #if defined(WOLFBOOT_OVERWRITE_ONLY_FAST) uint32_t src_size = 0; rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size); - assert(rc == 0); + boot_panic_unless(rc == 0); #endif wolfBoot_printf("Image upgrade slot1 -> slot0"); @@ -1070,7 +1055,7 @@ boot_copy_image(struct boot_status *bs) rc = boot_erase_sector(FLASH_AREA_IMAGE_0, size, this_size); - assert(rc == 0); + boot_panic_unless(rc == 0); size += this_size; @@ -1093,17 +1078,18 @@ boot_copy_image(struct boot_status *bs) rc = boot_erase_sector(FLASH_AREA_IMAGE_1, boot_img_sector_off(&boot_data, 1, 0), boot_img_sector_size(&boot_data, 1, 0)); - assert(rc == 0); + boot_panic_unless(rc == 0); last_sector = boot_img_num_sectors(&boot_data, 1) - 1; rc = boot_erase_sector(FLASH_AREA_IMAGE_1, boot_img_sector_off(&boot_data, 1, last_sector), boot_img_sector_size(&boot_data, 1, last_sector)); - assert(rc == 0); + boot_panic_unless(rc == 0); /* TODO: Perhaps verify slot 0's signature again? */ return 0; } + #else static int boot_copy_image(struct boot_status *bs) @@ -1116,11 +1102,7 @@ boot_copy_image(struct boot_status *bs) uint32_t size; uint32_t copy_size; int rc; - - /* FIXME: just do this if asked by user? */ - size = copy_size = 0; - if (bs->idx == 0 && bs->state == 0) { /* * No swap ever happened, so need to find the largest image which @@ -1129,13 +1111,13 @@ boot_copy_image(struct boot_status *bs) hdr = boot_img_hdr(&boot_data, 0); if (hdr->ih_magic == IMAGE_MAGIC) { rc = boot_read_image_size(0, hdr, ©_size); - assert(rc == 0); + boot_panic_unless(rc == 0); } hdr = boot_img_hdr(&boot_data, 1); if (hdr->ih_magic == IMAGE_MAGIC) { rc = boot_read_image_size(1, hdr, &size); - assert(rc == 0); + boot_panic_unless(rc == 0); } if (size > copy_size) { @@ -1149,7 +1131,7 @@ boot_copy_image(struct boot_status *bs) * in the trailer... */ rc = boot_read_swap_size(&bs->swap_size); - assert(rc == 0); + boot_panic_unless(rc == 0); copy_size = bs->swap_size; } @@ -1175,20 +1157,16 @@ boot_copy_image(struct boot_status *bs) swap_idx++; } -#ifdef WOLFBOOT_VALIDATE_SLOT0 if (boot_status_fails > 0) { wolfBoot_printf("%d status write fails performing the swap", boot_status_fails); } -#endif return 0; } -#endif /** * Marks the image in slot 0 as fully copied. */ -#ifndef WOLFBOOT_OVERWRITE_ONLY static int boot_set_copy_done(void) { @@ -1204,7 +1182,6 @@ boot_set_copy_done(void) flash_area_close(fap); return rc; } -#endif /* !WOLFBOOT_OVERWRITE_ONLY */ /** * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure @@ -1215,7 +1192,6 @@ boot_set_copy_done(void) * image installed on slot0 and the new image to be upgrade to has a bad sig, * image_ok would be overwritten. */ -#ifndef WOLFBOOT_OVERWRITE_ONLY static int boot_set_image_ok(void) { @@ -1263,7 +1239,7 @@ boot_swap_if_needed(int *out_swap_type) * operation. */ rc = boot_read_status(&bs); - assert(rc == 0); + boot_panic_unless(rc == 0); if (rc != 0) { return rc; } @@ -1271,7 +1247,7 @@ boot_swap_if_needed(int *out_swap_type) /* If a partial swap was detected, complete it. */ if (bs.idx != 0 || bs.state != 0) { rc = boot_copy_image(&bs); - assert(rc == 0); + boot_panic_unless(rc == 0); /* NOTE: here we have finished a swap resume. The initial request * was either a TEST or PERM swap, which now after the completed @@ -1290,7 +1266,7 @@ boot_swap_if_needed(int *out_swap_type) case BOOT_SWAP_TYPE_PERM: case BOOT_SWAP_TYPE_REVERT: rc = boot_copy_image(&bs); - assert(rc == 0); + boot_panic_unless(rc == 0); break; } } @@ -1310,7 +1286,7 @@ boot_swap_if_needed(int *out_swap_type) int boot_go(struct boot_rsp *rsp) { - int swap_type; + int swap_type = 0; size_t slot; int rc; int fa_id; @@ -1330,12 +1306,12 @@ boot_go(struct boot_rsp *rsp) for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { fa_id = flash_area_id_from_image_slot(slot); rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot)); - assert(rc == 0); + boot_panic_unless(rc == 0); } #ifndef WOLFBOOT_OVERWRITE_ONLY rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &BOOT_SCRATCH_AREA(&boot_data)); - assert(rc == 0); + boot_panic_unless(rc == 0); #endif /* Determine the sector layout of the image slots and scratch area. */ @@ -1357,7 +1333,7 @@ boot_go(struct boot_rsp *rsp) */ if (boot_slots_compatible()) { rc = boot_swap_if_needed(&swap_type); - assert(rc == 0); + boot_panic_unless(rc == 0); if (rc != 0) { goto out; } @@ -1411,7 +1387,7 @@ boot_go(struct boot_rsp *rsp) if (swap_type == BOOT_SWAP_TYPE_PANIC) { wolfBoot_printf("panic!"); - assert(0); + boot_panic_unless(0); /* Loop forever... */ while (1) {} @@ -1430,24 +1406,12 @@ boot_go(struct boot_rsp *rsp) slot = 0; } -#ifdef WOLFBOOT_VALIDATE_SLOT0 rc = boot_validate_slot(0); - ASSERT(rc == 0); + boot_panic_unless(rc == 0); if (rc != 0) { rc = BOOT_EBADIMAGE; goto out; } -#else - /* Even if we're not re-validating slot 0, we could be booting - * onto an empty flash chip. At least do a basic sanity check that - * the magic number on the image is OK. - */ - if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) { - wolfBoot_printf("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic); - rc = BOOT_EBADIMAGE; - goto out; - } -#endif /* Always boot from the primary slot. */ rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id; @@ -1479,11 +1443,11 @@ split_go(int loader_slot, int split_slot, void **entry) loader_flash_id = flash_area_id_from_image_slot(loader_slot); rc = flash_area_open(loader_flash_id, &BOOT_IMG_AREA(&boot_data, split_slot)); - assert(rc == 0); + boot_panic_unless(rc == 0); split_flash_id = flash_area_id_from_image_slot(split_slot); rc = flash_area_open(split_flash_id, &BOOT_IMG_AREA(&boot_data, split_slot)); - assert(rc == 0); + boot_panic_unless(rc == 0); /* Determine the sector layout of the image slots and scratch area. */ rc = boot_read_sectors(); diff --git a/src/crypto.c b/src/crypto.c index 166ecb47..3a3631ef 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/flash_map_extended.c b/src/flash_map_extended.c deleted file mode 100644 index 5849acf5..00000000 --- a/src/flash_map_extended.c +++ /dev/null @@ -1,81 +0,0 @@ -/* flash_map_extended.c - * - * Copyright (C) 2018 wolfSSL Inc. - * - * This file is part of wolfBoot. - * - * wolfBoot is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * wolfBoot is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA - */ - -#include "target.h" - -#include -#include - -#include "bootutil/bootutil_log.h" - -/* - * For now, we only support one flash device. - * - * Pick the SoC Flash driver ID. - */ -#define FLASH_DEVICE_ID SOC_FLASH_0_ID -#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS - -static struct device *flash_dev; - -struct device *flash_device_get_binding(char *dev_name) -{ - if (!flash_dev) { - flash_dev = device_get_binding(dev_name); - } - return flash_dev; -} - -int flash_device_base(uint8_t fd_id, uintptr_t *ret) -{ - if (fd_id != FLASH_DEVICE_ID) { - BOOT_LOG_ERR("invalid flash ID %d; expected %d", - fd_id, FLASH_DEVICE_ID); - return -EINVAL; - } - *ret = FLASH_DEVICE_BASE; - return 0; -} - -/* - * This depends on the mappings defined in sysflash.h, and assumes - * that slot 0, slot 1, and the scratch areas are contiguous. - */ -int flash_area_id_from_image_slot(int slot) -{ - return slot + FLASH_AREA_IMAGE_0; -} - -int flash_area_sector_from_off(off_t off, struct flash_sector *sector) -{ - int rc; - struct flash_pages_info page; - - rc = flash_get_page_info_by_offs(flash_dev, off, &page); - if (rc) { - return rc; - } - - sector->fs_off = page.start_offset; - sector->fs_size = page.size; - - return rc; -} diff --git a/src/main.c b/src/main.c index 10c9fde5..b9839c4d 100644 --- a/src/main.c +++ b/src/main.c @@ -20,12 +20,11 @@ */ #include "target.h" -#include "hal/hal_flash.h" +#include "hal.h" #include "printf.h" #include "bootutil/image.h" -#include "bootutil/bootutil.h" -#include "flash_map_backend/flash_map_backend.h" +#include "bootutil.h" extern void do_boot(void *); diff --git a/src/mem.c b/src/mem.c index 8d9be273..93f4ae57 100644 --- a/src/mem.c +++ b/src/mem.c @@ -33,7 +33,7 @@ void * memset(void *s, int c, size_t n) void *memcpy(void *dst, const void *src, size_t n) { - int i; + size_t i; const char *s = (const char *)src; char *d = (char *)dst; for (i = 0; i < n; i++) { diff --git a/src/run.c b/src/run.c deleted file mode 100644 index e69e1902..00000000 --- a/src/run.c +++ /dev/null @@ -1,271 +0,0 @@ -/* run.c - * - * Copyright (C) 2018 wolfSSL Inc. - * - * This file is part of wolfBoot. - * - * wolfBoot is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * wolfBoot is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA - */ -#include -#include -#include -#include - -#include "target.h" -#include "sysflash/sysflash.h" -#include "hal/hal_flash.h" - -#include - -#define BOOT_LOG_DBG(...) do{}while(0) - -#define print_log(...) do{}while(0) - -void abort(void) -{ - while(1) - ; -} - -struct area { - struct flash_area whole; - struct flash_area *areas; - uint32_t num_areas; - uint8_t id; -}; - - -struct area_desc { - struct area slots[3]; - uint32_t num_slots; -}; - -static struct area_desc flash_areas[1] = { - { - .slots = { - { - .whole = { - .fa_id = FLASH_AREA_IMAGE_0, - .fa_device_id = 0, - .fa_off = FLASH_AREA_IMAGE_0_OFFSET, - .fa_size = FLASH_AREA_IMAGE_0_SIZE, - }, - .id = FLASH_AREA_IMAGE_0, - .num_areas = 1 - }, - { - .whole = { - .fa_id = FLASH_AREA_IMAGE_1, - .fa_device_id = 0, - .fa_off = FLASH_AREA_IMAGE_1_OFFSET, - .fa_size = FLASH_AREA_IMAGE_1_SIZE, - }, - .id = FLASH_AREA_IMAGE_1, - .num_areas = 1 - }, -#ifndef WOLFBOOT_OVERWRITE_ONLY - { - .whole = { - .fa_id = FLASH_AREA_IMAGE_SCRATCH, - .fa_device_id = 0, - .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET, - .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE, - }, - .id = FLASH_AREA_IMAGE_SCRATCH, - .num_areas = 1 - } - }, - .num_slots = 3 -#else - }, - .num_slots = 2 -#endif - } -} ; - -uint8_t flash_area_align(const struct flash_area *area) -{ - (void)area; - return 1; -} - -int flash_area_open(uint8_t id, const struct flash_area **area) -{ - uint32_t i; - - for (i = 0; i < flash_areas->num_slots; i++) { - if (flash_areas->slots[i].id == id) - break; - } - if (i == flash_areas->num_slots) { - print_log("Unsupported area\n"); - abort(); - } - - /* Unsure if this is right, just returning the first area. */ - *area = &flash_areas->slots[i].whole; - return 0; -} - -void flash_area_close(const struct flash_area *area) -{ - (void)area; -} - -/* - * Read/write/erase. Offset is relative from beginning of flash area. - */ -int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, - uint32_t len) -{ - int i; - uint8_t *src8, *dst8; - BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", - __func__, area->fa_id, off, len); - if (!area) - return -1; - if ((off + len) > (area->fa_size)) - return -1; - src8 = (uint8_t *)(area->fa_off + off); - dst8 = (uint8_t *)dst; - for (i = 0; i < len; i++) { - dst8[i] = src8[i]; - } - return 0; -} - -int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, - uint32_t len) -{ - BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, - area->fa_id, off, len); - hal_flash_unlock(); - hal_flash_write(area->fa_off + off, src, len); - hal_flash_lock(); - return 0; -} - -int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) -{ - BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, - area->fa_id, off, len); - hal_flash_unlock(); - hal_flash_erase(area->fa_off + off, len); - hal_flash_lock(); - return 0; -} - -int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) -{ - uint32_t i; - struct area *slot; - - for (i = 0; i < flash_areas->num_slots; i++) { - if (flash_areas->slots[i].id == idx) - break; - } - if (i == flash_areas->num_slots) { - print_log("Unsupported area\n"); - abort(); - } - - slot = &flash_areas->slots[i]; - - if (slot->num_areas > (uint32_t)*cnt) { - print_log("Too many areas in slot\n"); - abort(); - } - - *cnt = slot->num_areas; - memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); - - return 0; -} - -int flash_area_get_sectors(int fa_id, uint32_t *count, - struct flash_sector *sectors) -{ - uint32_t i; - struct area *slot; - - for (i = 0; i < flash_areas->num_slots; i++) { - if (flash_areas->slots[i].id == fa_id) - break; - } - if (i == flash_areas->num_slots) { - print_log("Unsupported area\n"); - abort(); - } - - slot = &flash_areas->slots[i]; - - if (slot->num_areas > *count) { - print_log("Too many areas in slot\n"); - abort(); - } - - for (i = 0; i < slot->num_areas; i++) { - sectors[i].fs_off = slot->areas[i].fa_off - - slot->whole.fa_off; - sectors[i].fs_size = slot->areas[i].fa_size; - } - *count = slot->num_areas; - - return 0; -} - -int flash_area_id_from_image_slot(int slot) -{ - return slot + FLASH_AREA_IMAGE_0; -} - -uint8_t flash_area_erased_val(const struct flash_area *fap) -{ - (void)fap; - return 0xff; -} -/* - -int _close(int fd) -{ - return -1; -} - -int _fstat(int fd) -{ - return -1; -} - -int _lseek(int fd, int whence, int off) -{ - return -1; -} - -int _read(uint8_t *buf, int len) -{ - return -1; -} - -int _isatty(int fd) -{ - return 1; -} - -int _write(void *r, uint8_t *text, int len) -{ - return -1; -} - -*/ diff --git a/src/startup_bl.c b/src/wolfboot.c similarity index 99% rename from src/startup_bl.c rename to src/wolfboot.c index 164191b7..2e676828 100644 --- a/src/startup_bl.c +++ b/src/wolfboot.c @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ #include "bootutil/image.h" -#include "bootutil/bootutil.h" +#include "bootutil.h" #include extern unsigned int _stored_data; extern unsigned int _start_data;