Some of the fixes requested during review:

- ARCH_FLASH_OFFSET only used in simulation (dynamic 'base' address)
- Fixed the DISABLE_BACKUP case
- renamed define to WOLFBOOT_ELF_SCATTERED
pull/562/head
Daniele Lacamera 2025-04-11 15:43:14 +02:00
parent 9d7084c6f5
commit 9c3e86241a
6 changed files with 49 additions and 27 deletions

View File

@ -43,7 +43,7 @@
#include "target.h" #include "target.h"
#include "printf.h" #include "printf.h"
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
#include "elf.h" #include "elf.h"
#endif #endif
@ -355,7 +355,7 @@ void do_boot(const uint32_t *app_offset)
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff); main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
main(main_argc, main_argv, NULL, NULL); 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); uint8_t *entry_point = (sim_ram_base + (unsigned long)app_offset);
printf("entry point: %p\n", entry_point); printf("entry point: %p\n", entry_point);
printf("app offset: %p\n", app_offset); printf("app offset: %p\n", app_offset);

View File

@ -787,7 +787,7 @@ ifeq ($(ELF),1)
CFLAGS+=-DDEBUG_ELF=$(DEBUG_ELF) CFLAGS+=-DDEBUG_ELF=$(DEBUG_ELF)
endif endif
ifeq ($(ELF_SCATTERED),1) ifeq ($(ELF_SCATTERED),1)
CFLAGS+=-D"ELF_SCATTERED=1" CFLAGS+=-D"WOLFBOOT_ELF_SCATTERED=1"
endif endif
endif endif

View File

@ -33,7 +33,7 @@
#include "hal/nxp_ppc.h" #include "hal/nxp_ppc.h"
#endif #endif
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
#include "image.h" #include "image.h"
#endif #endif
@ -181,7 +181,14 @@ int elf_hdr_size(const unsigned char *ehdr)
} }
return sz; 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) { int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out, int ext_flash) {
const unsigned char *image; const unsigned char *image;
int is_elf32; int is_elf32;
@ -219,16 +226,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
#ifdef EXT_FLASH #ifdef EXT_FLASH
if (ext_flash) { if (ext_flash) {
ext_flash_unlock(); ext_flash_unlock();
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); ext_flash_erase(paddr + BASE_OFF, filesz);
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); ext_flash_write(paddr + BASE_OFF, image + offset, filesz);
ext_flash_lock(); ext_flash_lock();
} }
else else
#endif #endif
{ {
hal_flash_unlock(); hal_flash_unlock();
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); hal_flash_erase(paddr + BASE_OFF, filesz);
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); hal_flash_write(paddr + BASE_OFF, image + offset, filesz);
hal_flash_lock(); hal_flash_lock();
} }
} }
@ -258,16 +265,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
#ifdef EXT_FLASH #ifdef EXT_FLASH
if (ext_flash) { if (ext_flash) {
ext_flash_unlock(); ext_flash_unlock();
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); ext_flash_erase(paddr + BASE_OFF, filesz);
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); ext_flash_write(paddr + BASE_OFF, image + offset, filesz);
ext_flash_lock(); ext_flash_lock();
} }
else else
#endif #endif
{ {
hal_flash_unlock(); hal_flash_unlock();
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz); hal_flash_erase(paddr + BASE_OFF, filesz);
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz); hal_flash_write(paddr + BASE_OFF, image + offset, filesz);
hal_flash_lock(); hal_flash_lock();
} }
} }

View File

@ -1327,11 +1327,17 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img)
return 0; return 0;
} }
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
#include "elf.h" #include "elf.h"
#define PADDING_BLOCK_SIZE 64 #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) int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
{ {
/* Open the partition containing the image */ /* 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); wolfBoot_printf("Feeding stored segment, len %d\n", len);
while (len > 0) { while (len > 0) {
if (len > WOLFBOOT_SHA_BLOCK_SIZE) { 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); WOLFBOOT_SHA_BLOCK_SIZE);
len -= WOLFBOOT_SHA_BLOCK_SIZE; len -= WOLFBOOT_SHA_BLOCK_SIZE;
paddr += WOLFBOOT_SHA_BLOCK_SIZE; paddr += WOLFBOOT_SHA_BLOCK_SIZE;
} else { } else {
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), update_hash(&ctx, (void *)(paddr + BASE_OFF),
len); len);
break; 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); wolfBoot_printf("Feeding stored segment, len %d\n", len);
while (len > 0) { while (len > 0) {
if (len > WOLFBOOT_SHA_BLOCK_SIZE) { 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); WOLFBOOT_SHA_BLOCK_SIZE);
len -= WOLFBOOT_SHA_BLOCK_SIZE; len -= WOLFBOOT_SHA_BLOCK_SIZE;
paddr += WOLFBOOT_SHA_BLOCK_SIZE; paddr += WOLFBOOT_SHA_BLOCK_SIZE;
} else { } else {
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET), update_hash(&ctx, (void *)(paddr + BASE_OFF),
len); len);
break; break;
} }
@ -1598,6 +1604,7 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
wolfBoot_printf("Scattered ELF verified.\n"); wolfBoot_printf("Scattered ELF verified.\n");
return 0; return 0;
} }
#undef BASE_OFF
#endif #endif

View File

@ -39,7 +39,7 @@ int WP11_Library_Init(void);
#endif #endif
/* Support for ELF scatter/gather format */ /* Support for ELF scatter/gather format */
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
#include "elf.h" #include "elf.h"
#endif #endif
@ -815,13 +815,21 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
wolfBoot_swap_and_final_erase(0); wolfBoot_swap_and_final_erase(0);
#else /* DISABLE_BACKUP */ #else /* DISABLE_BACKUP */
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
/* Compute and verify scattered hash */ unsigned long entry;
if (wolfBoot_verify_scattered_hash(&boot) != 0) { void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
wolfBoot_printf("Scattered hash verification failed\n"); wolfBoot_printf("ELF Scattered image digest check\n");
return -1; 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 */ /* Direct Swap without power fail safety */
hal_flash_unlock(); hal_flash_unlock();
@ -1071,7 +1079,7 @@ void RAMFUNCTION wolfBoot_start(void)
} }
PART_SANITY_CHECK(&boot); PART_SANITY_CHECK(&boot);
#ifdef ELF_SCATTERED #ifdef WOLFBOOT_ELF_SCATTERED
unsigned long entry; unsigned long entry;
void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS; void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
wolfBoot_printf("ELF Scattered image digest check\n"); wolfBoot_printf("ELF Scattered image digest check\n");

View File

@ -241,7 +241,7 @@ ifeq ($(TARGET),sim)
ifeq ($(ELF_SCATTERED),1) ifeq ($(ELF_SCATTERED),1)
LSCRIPT_TEMPLATE=sim_scattered.ld LSCRIPT_TEMPLATE=sim_scattered.ld
APP_OBJS=app_sim_scattered.o ../src/string.o 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 LDFLAGS+=-ffreestanding -nostartfiles -static -T$(LSCRIPT) -nostdlib
else else
APP_OBJS=app_sim.o APP_OBJS=app_sim.o