Skip to content

Commit 9c3e862

Browse files
committed
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
1 parent 9d7084c commit 9c3e862

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

hal/sim.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include "target.h"
4444
#include "printf.h"
4545

46-
#ifdef ELF_SCATTERED
46+
#ifdef WOLFBOOT_ELF_SCATTERED
4747
#include "elf.h"
4848
#endif
4949

@@ -355,7 +355,7 @@ void do_boot(const uint32_t *app_offset)
355355
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
356356
main(main_argc, main_argv, NULL, NULL);
357357

358-
#elif defined (ELF_SCATTERED)
358+
#elif defined (WOLFBOOT_ELF_SCATTERED)
359359
uint8_t *entry_point = (sim_ram_base + (unsigned long)app_offset);
360360
printf("entry point: %p\n", entry_point);
361361
printf("app offset: %p\n", app_offset);

options.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ ifeq ($(ELF),1)
787787
CFLAGS+=-DDEBUG_ELF=$(DEBUG_ELF)
788788
endif
789789
ifeq ($(ELF_SCATTERED),1)
790-
CFLAGS+=-D"ELF_SCATTERED=1"
790+
CFLAGS+=-D"WOLFBOOT_ELF_SCATTERED=1"
791791
endif
792792

793793
endif

src/elf.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "hal/nxp_ppc.h"
3434
#endif
3535

36-
#ifdef ELF_SCATTERED
36+
#ifdef WOLFBOOT_ELF_SCATTERED
3737
#include "image.h"
3838
#endif
3939

@@ -181,7 +181,14 @@ int elf_hdr_size(const unsigned char *ehdr)
181181
}
182182
return sz;
183183
}
184-
#if !defined(MMU) && !defined(WOLFBOOT_FSP) && !defined(ARCH_PPC)
184+
#if !defined(MMU) && !defined(WOLFBOOT_FSP) && !defined(ARCH_PPC) && defined (WOLFBOOT_ELF_SCATTERED)
185+
186+
#ifdef ARCH_SIM
187+
# define BASE_OFF ARCH_FLASH_OFFSET
188+
#else
189+
# define BASE_OFF 0
190+
#endif
191+
185192
int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out, int ext_flash) {
186193
const unsigned char *image;
187194
int is_elf32;
@@ -219,16 +226,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
219226
#ifdef EXT_FLASH
220227
if (ext_flash) {
221228
ext_flash_unlock();
222-
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
223-
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
229+
ext_flash_erase(paddr + BASE_OFF, filesz);
230+
ext_flash_write(paddr + BASE_OFF, image + offset, filesz);
224231
ext_flash_lock();
225232
}
226233
else
227234
#endif
228235
{
229236
hal_flash_unlock();
230-
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
231-
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
237+
hal_flash_erase(paddr + BASE_OFF, filesz);
238+
hal_flash_write(paddr + BASE_OFF, image + offset, filesz);
232239
hal_flash_lock();
233240
}
234241
}
@@ -258,16 +265,16 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
258265
#ifdef EXT_FLASH
259266
if (ext_flash) {
260267
ext_flash_unlock();
261-
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
262-
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
268+
ext_flash_erase(paddr + BASE_OFF, filesz);
269+
ext_flash_write(paddr + BASE_OFF, image + offset, filesz);
263270
ext_flash_lock();
264271
}
265272
else
266273
#endif
267274
{
268275
hal_flash_unlock();
269-
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
270-
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
276+
hal_flash_erase(paddr + BASE_OFF, filesz);
277+
hal_flash_write(paddr + BASE_OFF, image + offset, filesz);
271278
hal_flash_lock();
272279
}
273280
}

src/image.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,11 +1327,17 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img)
13271327
return 0;
13281328
}
13291329

1330-
#ifdef ELF_SCATTERED
1330+
#ifdef WOLFBOOT_ELF_SCATTERED
13311331
#include "elf.h"
13321332

13331333
#define PADDING_BLOCK_SIZE 64
13341334

1335+
#ifdef ARCH_SIM
1336+
#define BASE_OFF ARCH_FLASH_OFFSET
1337+
#else
1338+
#define BASE_OFF 0
1339+
#endif
1340+
13351341
int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
13361342
{
13371343
/* Open the partition containing the image */
@@ -1448,12 +1454,12 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
14481454
wolfBoot_printf("Feeding stored segment, len %d\n", len);
14491455
while (len > 0) {
14501456
if (len > WOLFBOOT_SHA_BLOCK_SIZE) {
1451-
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET),
1457+
update_hash(&ctx, (void *)(paddr + BASE_OFF),
14521458
WOLFBOOT_SHA_BLOCK_SIZE);
14531459
len -= WOLFBOOT_SHA_BLOCK_SIZE;
14541460
paddr += WOLFBOOT_SHA_BLOCK_SIZE;
14551461
} else {
1456-
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET),
1462+
update_hash(&ctx, (void *)(paddr + BASE_OFF),
14571463
len);
14581464
break;
14591465
}
@@ -1532,12 +1538,12 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
15321538
wolfBoot_printf("Feeding stored segment, len %d\n", len);
15331539
while (len > 0) {
15341540
if (len > WOLFBOOT_SHA_BLOCK_SIZE) {
1535-
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET),
1541+
update_hash(&ctx, (void *)(paddr + BASE_OFF),
15361542
WOLFBOOT_SHA_BLOCK_SIZE);
15371543
len -= WOLFBOOT_SHA_BLOCK_SIZE;
15381544
paddr += WOLFBOOT_SHA_BLOCK_SIZE;
15391545
} else {
1540-
update_hash(&ctx, (void *)(paddr + ARCH_FLASH_OFFSET),
1546+
update_hash(&ctx, (void *)(paddr + BASE_OFF),
15411547
len);
15421548
break;
15431549
}
@@ -1598,6 +1604,7 @@ int elf_check_image_scattered(uint8_t part, unsigned long *entry_out)
15981604
wolfBoot_printf("Scattered ELF verified.\n");
15991605
return 0;
16001606
}
1607+
#undef BASE_OFF
16011608

16021609
#endif
16031610

src/update_flash.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int WP11_Library_Init(void);
3939
#endif
4040

4141
/* Support for ELF scatter/gather format */
42-
#ifdef ELF_SCATTERED
42+
#ifdef WOLFBOOT_ELF_SCATTERED
4343
#include "elf.h"
4444
#endif
4545

@@ -815,13 +815,21 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
815815
wolfBoot_swap_and_final_erase(0);
816816

817817
#else /* DISABLE_BACKUP */
818-
#ifdef ELF_SCATTERED
819-
/* Compute and verify scattered hash */
820-
if (wolfBoot_verify_scattered_hash(&boot) != 0) {
821-
wolfBoot_printf("Scattered hash verification failed\n");
822-
return -1;
818+
#ifdef WOLFBOOT_ELF_SCATTERED
819+
unsigned long entry;
820+
void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
821+
wolfBoot_printf("ELF Scattered image digest check\n");
822+
if (elf_check_image_scattered(PART_BOOT, &entry) < 0) {
823+
wolfBoot_printf("ELF Scattered image digest check: failed. Restoring scattered image...\n");
824+
elf_store_image_scattered(base, &entry, PART_IS_EXT(boot));
825+
if (elf_check_image_scattered(PART_BOOT, &entry) < 0) {
826+
wolfBoot_printf("Fatal: Could not verify digest after scattering. Panic().\n");
827+
wolfBoot_panic();
828+
}
823829
}
824-
#endif
830+
wolfBoot_printf("Scattered image correctly verified. Setting entry point to %p\n", entry);
831+
boot.fw_base = (void *)entry;
832+
#endif
825833
/* Direct Swap without power fail safety */
826834

827835
hal_flash_unlock();
@@ -1071,7 +1079,7 @@ void RAMFUNCTION wolfBoot_start(void)
10711079
}
10721080
PART_SANITY_CHECK(&boot);
10731081

1074-
#ifdef ELF_SCATTERED
1082+
#ifdef WOLFBOOT_ELF_SCATTERED
10751083
unsigned long entry;
10761084
void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
10771085
wolfBoot_printf("ELF Scattered image digest check\n");

test-app/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ ifeq ($(TARGET),sim)
241241
ifeq ($(ELF_SCATTERED),1)
242242
LSCRIPT_TEMPLATE=sim_scattered.ld
243243
APP_OBJS=app_sim_scattered.o ../src/string.o
244-
CFLAGS+=-D"ELF_SCATTERED=1" -nostartfiles -ffreestanding -static -nostdlib
244+
CFLAGS+=-D"WOLFBOOT_ELF_SCATTERED=1" -nostartfiles -ffreestanding -static -nostdlib
245245
LDFLAGS+=-ffreestanding -nostartfiles -static -T$(LSCRIPT) -nostdlib
246246
else
247247
APP_OBJS=app_sim.o

0 commit comments

Comments
 (0)