diff --git a/hal/sim.c b/hal/sim.c index 1adba014..9473a41d 100644 --- a/hal/sim.c +++ b/hal/sim.c @@ -43,7 +43,7 @@ #include "target.h" #include "printf.h" -#ifdef ELF_SCATTERED +#ifdef WOLFBOOT_ELF_SCATTERED #include "elf.h" #endif @@ -355,7 +355,7 @@ void do_boot(const uint32_t *app_offset) main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff); main(main_argc, main_argv, NULL, NULL); -#elif defined (ELF_SCATTERED) +#elif defined (WOLFBOOT_ELF_SCATTERED) uint8_t *entry_point = (sim_ram_base + (unsigned long)app_offset); printf("entry point: %p\n", entry_point); printf("app offset: %p\n", app_offset); diff --git a/options.mk b/options.mk index 52ac384e..5b7238ca 100644 --- a/options.mk +++ b/options.mk @@ -787,7 +787,7 @@ ifeq ($(ELF),1) CFLAGS+=-DDEBUG_ELF=$(DEBUG_ELF) endif ifeq ($(ELF_SCATTERED),1) - CFLAGS+=-D"ELF_SCATTERED=1" + CFLAGS+=-D"WOLFBOOT_ELF_SCATTERED=1" endif endif diff --git a/src/elf.c b/src/elf.c index d2709829..905e383b 100644 --- a/src/elf.c +++ b/src/elf.c @@ -33,7 +33,7 @@ #include "hal/nxp_ppc.h" #endif -#ifdef ELF_SCATTERED +#ifdef WOLFBOOT_ELF_SCATTERED #include "image.h" #endif @@ -181,7 +181,14 @@ int elf_hdr_size(const unsigned char *ehdr) } return sz; } -#if !defined(MMU) && !defined(WOLFBOOT_FSP) && !defined(ARCH_PPC) +#if !defined(MMU) && !defined(WOLFBOOT_FSP) && !defined(ARCH_PPC) && defined (WOLFBOOT_ELF_SCATTERED) + +#ifdef ARCH_SIM +# define BASE_OFF ARCH_FLASH_OFFSET +#else +# define BASE_OFF 0 +#endif + int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out, int ext_flash) { const unsigned char *image; int is_elf32; @@ -219,16 +226,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out #ifdef EXT_FLASH if (ext_flash) { ext_flash_unlock(); - ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); - ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); + ext_flash_erase(paddr + BASE_OFF, filesz); + ext_flash_write(paddr + BASE_OFF, image + offset, filesz); ext_flash_lock(); } else #endif { hal_flash_unlock(); - hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); - hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); + hal_flash_erase(paddr + BASE_OFF, filesz); + hal_flash_write(paddr + BASE_OFF, image + offset, filesz); hal_flash_lock(); } } @@ -258,16 +265,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out #ifdef EXT_FLASH if (ext_flash) { ext_flash_unlock(); - ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); - ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); + ext_flash_erase(paddr + BASE_OFF, filesz); + ext_flash_write(paddr + BASE_OFF, image + offset, filesz); ext_flash_lock(); } else #endif { hal_flash_unlock(); - hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); - hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); + hal_flash_erase(paddr + BASE_OFF, filesz); + hal_flash_write(paddr + BASE_OFF, image + offset, filesz); hal_flash_lock(); } } diff --git a/src/image.c b/src/image.c index 0814de8f..67e8fe26 100644 --- a/src/image.c +++ b/src/image.c @@ -1327,11 +1327,17 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img) return 0; } -#ifdef ELF_SCATTERED +#ifdef WOLFBOOT_ELF_SCATTERED #include "elf.h" #define PADDING_BLOCK_SIZE 64 +#ifdef ARCH_SIM +#define BASE_OFF ARCH_FLASH_OFFSET +#else +#define BASE_OFF 0 +#endif + int elf_check_image_scattered(uint8_t part, unsigned long *entry_out) { /* Open the partition containing the image */ @@ -1448,12 +1454,12 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out) wolfBoot_printf("Feeding stored segment, len %d\n", len); while (len > 0) { if (len > WOLFBOOT_SHA_BLOCK_SIZE) { - update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), + update_hash(&ctx, (void *)(paddr + BASE_OFF), WOLFBOOT_SHA_BLOCK_SIZE); len -= WOLFBOOT_SHA_BLOCK_SIZE; paddr += WOLFBOOT_SHA_BLOCK_SIZE; } else { - update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), + update_hash(&ctx, (void *)(paddr + BASE_OFF), len); break; } @@ -1532,12 +1538,12 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out) wolfBoot_printf("Feeding stored segment, len %d\n", len); while (len > 0) { if (len > WOLFBOOT_SHA_BLOCK_SIZE) { - update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), + update_hash(&ctx, (void *)(paddr + BASE_OFF), WOLFBOOT_SHA_BLOCK_SIZE); len -= WOLFBOOT_SHA_BLOCK_SIZE; paddr += WOLFBOOT_SHA_BLOCK_SIZE; } else { - update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), + update_hash(&ctx, (void *)(paddr + BASE_OFF), len); break; } @@ -1598,6 +1604,7 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out) wolfBoot_printf("Scattered ELF verified.\n"); return 0; } +#undef BASE_OFF #endif diff --git a/src/update_flash.c b/src/update_flash.c index 2cdc37fb..8caa5241 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -39,7 +39,7 @@ int WP11_Library_Init(void); #endif /* Support for ELF scatter/gather format */ -#ifdef ELF_SCATTERED +#ifdef WOLFBOOT_ELF_SCATTERED #include "elf.h" #endif @@ -815,13 +815,21 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed) wolfBoot_swap_and_final_erase(0); #else /* DISABLE_BACKUP */ - #ifdef ELF_SCATTERED - /* Compute and verify scattered hash */ - if (wolfBoot_verify_scattered_hash(&boot) != 0) { - wolfBoot_printf("Scattered hash verification failed\n"); - return -1; +#ifdef WOLFBOOT_ELF_SCATTERED + unsigned long entry; + void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS; + wolfBoot_printf("ELF Scattered image digest check\n"); + if (elf_check_image_scattered(PART_BOOT, &entry) < 0) { + wolfBoot_printf("ELF Scattered image digest check: failed. Restoring scattered image...\n"); + elf_store_image_scattered(base, &entry, PART_IS_EXT(boot)); + if (elf_check_image_scattered(PART_BOOT, &entry) < 0) { + wolfBoot_printf("Fatal: Could not verify digest after scattering. Panic().\n"); + wolfBoot_panic(); + } } - #endif + wolfBoot_printf("Scattered image correctly verified. Setting entry point to %p\n", entry); + boot.fw_base = (void *)entry; +#endif /* Direct Swap without power fail safety */ hal_flash_unlock(); @@ -1071,7 +1079,7 @@ void RAMFUNCTION wolfBoot_start(void) } PART_SANITY_CHECK(&boot); -#ifdef ELF_SCATTERED +#ifdef WOLFBOOT_ELF_SCATTERED unsigned long entry; void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS; wolfBoot_printf("ELF Scattered image digest check\n"); diff --git a/test-app/Makefile b/test-app/Makefile index 0a0fd137..b2975669 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -241,7 +241,7 @@ ifeq ($(TARGET),sim) ifeq ($(ELF_SCATTERED),1) LSCRIPT_TEMPLATE=sim_scattered.ld APP_OBJS=app_sim_scattered.o ../src/string.o - CFLAGS+=-D"ELF_SCATTERED=1" -nostartfiles -ffreestanding -static -nostdlib + CFLAGS+=-D"WOLFBOOT_ELF_SCATTERED=1" -nostartfiles -ffreestanding -static -nostdlib LDFLAGS+=-ffreestanding -nostartfiles -static -T$(LSCRIPT) -nostdlib else APP_OBJS=app_sim.o