diff --git a/.gitignore b/.gitignore index 7758f82f..01c7389f 100644 --- a/.gitignore +++ b/.gitignore @@ -119,3 +119,6 @@ IDE/IAR/Release build/ CMakeFiles/ CMakeCache.txt + +# Stage 1 +stage1/loader_stage1.ld diff --git a/Makefile b/Makefile index d9ea419d..03dd1f08 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,10 @@ include arch.mk # Parse config options include options.mk +OBJS+=$(WOLFCRYPT_OBJS) +OBJS+=$(PUBLIC_KEY_OBJS) +OBJS+=$(UPDATE_OBJS) + CFLAGS+= \ -I"." -I"include/" -I"lib/wolfssl" \ -D"WOLFSSL_USER_SETTINGS" \ @@ -92,7 +96,7 @@ ifeq ($(TARGET),sim) endif ifeq ($(TARGET),nxp_p1021) - MAIN_TARGET:=loader_stage1.bin factory.bin + MAIN_TARGET:=factory_wstage1.bin endif ASFLAGS:=$(CFLAGS) @@ -100,11 +104,10 @@ BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLAS all: $(MAIN_TARGET) -stage1: loader_stage1.bin -loader_stage1: loader_stage1.bin -loader_stage1.bin: +stage1: stage1/loader_stage1.bin +stage1/loader_stage1.bin: @echo "\t[BIN] $@" - $(Q)$(MAKE) -C stage1 $@ + $(Q)$(MAKE) -C $(dir $@) $(notdir $@) test-lib: $(OBJS) $(Q)$(CC) $(CFLAGS) -o $@ $^ @@ -181,7 +184,15 @@ internal_flash.dd: test-app/image_v1_signed.bin wolfboot.elf $(BINASSEMBLE) factory.bin: $(BOOT_IMG) wolfboot.bin $(PRIVATE_KEY) test-app/image_v1_signed.bin $(BINASSEMBLE) @echo "\t[MERGE] $@" - $(Q)$(BINASSEMBLE) $@ $(WOLFBOOT_ORIGIN) wolfboot.bin \ + $(Q)$(BINASSEMBLE) $@ \ + $(WOLFBOOT_ORIGIN) wolfboot.bin \ + $(WOLFBOOT_PARTITION_BOOT_ADDRESS) test-app/image_v1_signed.bin + +factory_wstage1.bin: $(BOOT_IMG) wolfboot.bin $(PRIVATE_KEY) test-app/image_v1_signed.bin $(BINASSEMBLE) stage1/loader_stage1.bin + @echo "\t[MERGE] $@" + $(Q)$(BINASSEMBLE) $@ \ + $(WOLFBOOT_STAGE1_FLASH_ADDR) stage1/loader_stage1.bin \ + $(WOLFBOOT_ORIGIN) wolfboot.bin \ $(WOLFBOOT_PARTITION_BOOT_ADDRESS) test-app/image_v1_signed.bin wolfboot.elf: include/target.h $(OBJS) $(LSCRIPT) FORCE @@ -207,6 +218,7 @@ $(LSCRIPT): FORCE sed -e "s/@WOLFBOOT_PARTITION_SIZE@/$(WOLFBOOT_PARTITION_SIZE)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_UPDATE_ADDRESS@/$(WOLFBOOT_PARTITION_UPDATE_ADDRESS)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_SWAP_ADDRESS@/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" | \ + sed -e "s/@WOLFBOOT_STAGE1_SIZE@/$(WOLFBOOT_STAGE1_SIZE)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_LOAD_ADDR@/$(WOLFBOOT_STAGE1_LOAD_ADDR)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_FLASH_ADDR@/$(WOLFBOOT_STAGE1_FLASH_ADDR)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_BASE_ADDR@/$(WOLFBOOT_STAGE1_BASE_ADDR)/g" \ diff --git a/arch.mk b/arch.mk index b06bc1f0..bde56984 100644 --- a/arch.mk +++ b/arch.mk @@ -222,7 +222,7 @@ ifeq ($(ARCH),PPC) endif # Prune unused functions and data - CFLAGS +=-ffunction-sections -fdata-sections + CFLAGS+=-ffunction-sections -fdata-sections LDFLAGS+=-Wl,--gc-sections OBJS+=src/boot_ppc_start.o src/boot_ppc.o @@ -491,3 +491,5 @@ endif ifeq ($(DEBUG_UART),1) CFLAGS+=-DDEBUG_UART endif + +CFLAGS+=-DWOLFBOOT_ARCH=$(ARCH) diff --git a/config/examples/nxp-p1021.config b/config/examples/nxp-p1021.config index 23d544ab..ee20155e 100644 --- a/config/examples/nxp-p1021.config +++ b/config/examples/nxp-p1021.config @@ -25,6 +25,9 @@ WOLFTPM?=0 # Flash Sector (Block) Size (16KB) WOLFBOOT_SECTOR_SIZE=0x4000 +# Maximum size of wolfBoot stage 1 loader +WOLFBOOT_STAGE1_SIZE=0x1000 + # wolfBoot partition size (128KB) BOOTLOADER_PARTITION_SIZE=0x20000 @@ -34,7 +37,7 @@ WOLFBOOT_PARTITION_SIZE?=0x1000000 ## NAND Flash Offsets ## # Location in Flash for stage 1 loader (XIP from boot ROM) -WOLFBOOT_STAGE1_FLASH_ADDR = 0x0 +WOLFBOOT_STAGE1_FLASH_ADDR=0x0 # Location in Flash for wolfBoot WOLFBOOT_ORIGIN=0x4000 # Location in Flash for Application Partition @@ -44,7 +47,6 @@ WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1200000 # Location in Flash for temporary sector used during updates WOLFBOOT_PARTITION_SWAP_ADDRESS?= 0x2200000 - ## Loader Destinations (DDR) ## # Address in RAM to load stage 1 loader diff --git a/config/examples/nxp-t2080.config b/config/examples/nxp-t2080.config index 8e749353..10d04d34 100644 --- a/config/examples/nxp-t2080.config +++ b/config/examples/nxp-t2080.config @@ -1,8 +1,9 @@ ARCH=PPC TARGET=nxp_t2080 -SIGN?=ED25519 -HASH?=SHA256 -DEBUG?=1 +SIGN?=ECC384 +HASH?=SHA384 +DEBUG?=0 +DEBUG_UART?=1 VTOR?=1 CORTEX_M0?=0 NO_ASM?=0 diff --git a/docs/Loader.md b/docs/Loader.md index 41ba9ca9..7738dc95 100644 --- a/docs/Loader.md +++ b/docs/Loader.md @@ -10,10 +10,13 @@ A first stage loader whose purpose is to load wolfBoot from flash to ram and jum Example: `make WOLFBOOT_STAGE1_LOAD_ADDR=0x1000 stage1` +* `WOLFBOOT_STAGE1_SIZE`: Maximum size of wolfBoot stage 1 loader +* `WOLFBOOT_STAGE1_FLASH_ADDR`: Location in Flash for stage 1 loader (XIP from boot ROM) * `WOLFBOOT_STAGE1_BASE_ADDR`: Address in RAM to load stage 1 loader to * `WOLFBOOT_STAGE1_LOAD_ADDR`: Address in RAM to load wolfBoot to * `WOLFBOOT_LOAD_ADDRESS`: Address in RAM to load application partition + ## update_ram.c Implementation for RAM based updater diff --git a/docs/Targets.md b/docs/Targets.md index 9542658a..9a0848d4 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -1081,22 +1081,27 @@ The NXP QorIQ P1021 is a PPC e500v2 based processor (two cores). This has been t wolfBoot supports loading from external flash using the eLBC FMC (Flash Machine) with NAND. -When each e500 core comes out of reset, its MMU has one 4-Kbyte page defined at `0x0_FFFF_Fnnn`. For NAND boot the first 4KB is loaded to this region with the first offset jump instruction at `0x0_FFFF_FFFC`. The 4KB is mapped to the FCM buffers. +When each e500 core comes out of reset, its MMU has one 4-Kbyte page defined at `0x0_FFFF_Fnnn`. For NAND boot the first 4KB is loaded to this region with the first offset jump instruction at `0x0_FFFF_FFFC`. The 4KB is mapped to the eLBC FCM buffers. This device defines the default boot ROM address range to be 8 Mbytes at address `0x0_FF80_0000` to `0x0_FFFF_FFFF`. -cfg_rom_loc[0:3] = 1000 Local bus FCM-8-bit NAND flash small page -cfg_rom_loc[0:3] = 1010 Local bus FCM-8-bit NAND flash large page +These pin determine if the boot ROM will use small or large flash page: +* `cfg_rom_loc[0:3]` = 1000 Local bus FCM-8-bit NAND flash small page +* `cfg_rom_loc[0:3]` = 1010 Local bus FCM-8-bit NAND flash large page If the boot sequencer is not enabled, the processor cores exit reset and fetches boot code in default configurations. A loader must reside in the 4KB page to handle early startup including DDR and then load wolfBoot into DDR for execution. +### First Stage Loader (stage 1) for NXP P1021 PPC + +A first stage loader is required to load the wolfBoot image into DDR for execution. This is because only 4KB of code space is available on boot. The stage 1 loader must also copy iteslf from the FCM buffer to DDR (or L2SRAM) to allow using of the eLBC to read NAND blocks. + ### Building wolfBoot for NXP P1021 PPC By default wolfBoot will use `powerpc-linux-gnu-` cross-compiler prefix. These tools can be installed with the Debian package `gcc-powerpc-linux-gnu` (`sudo apt install gcc-powerpc-linux-gnu`). -The `make` creates a `factory.bin` image that can be programmed at `0x00000000` +The `make` creates a `factory_wstage1.bin` image that can be programmed at `0x00000000` ``` cp ./config/examples/nxp-p1021.config .config @@ -1104,6 +1109,16 @@ make keytools make clean && make ``` +#### Flash Layout for NXP P1021 PPC (default) + +| File | NAND offset | +| ---------------------------- | ----------- | +| stage1/loader_stage1.bin | 0x00000000 | +| wolfboot.bin | 0x00004000 | +| test-app/image_v1_signed.bin | 0x00200000 | +| update | 0x01200000 | +| swap block | 0x02200000 | + ## NXP QorIQ T2080 PPC diff --git a/hal/nxp_p1021.c b/hal/nxp_p1021.c index ee8385a3..6295e84f 100644 --- a/hal/nxp_p1021.c +++ b/hal/nxp_p1021.c @@ -951,23 +951,6 @@ void hal_init(void) (void)svr; #endif -#ifdef BUILD_LOADER_STAGE1 - #ifdef WOLFBOOT_STAGE1_BASE_ADDR - /* if this is executing from boot 4KB region (FCM buffer) it must - * first be relocated to RAM before the eLBC NAND can be read */ - if (((size_t)&hal_init & BOOT_ROM_ADDR) == BOOT_ROM_ADDR) { - __attribute__((noreturn)) void (*wolfboot_start)(void) = - (void*)BOOT_ROM_ADDR + BOOT_ROM_SIZE - 0x04; - - /* relocate 4KB code to DST and jump */ - memmove((void*)WOLFBOOT_STAGE1_BASE_ADDR, (void*)BOOT_ROM_ADDR, - BOOT_ROM_SIZE); - - wolfboot_start(); /* never returns */ - } - #endif -#endif - #ifdef ENABLE_PCIE hal_pcie_init(); #endif diff --git a/hal/nxp_p1021.ld b/hal/nxp_p1021.ld index b995afcc..4675f0fc 100644 --- a/hal/nxp_p1021.ld +++ b/hal/nxp_p1021.ld @@ -2,17 +2,17 @@ OUTPUT_ARCH( "powerpc" ) ENTRY( _reset ) +HEAP_SIZE = 4K; /* heap not used */ +STACK_SIZE = 128K; + MEMORY { - /* L2 as SRAM (up to 256KB) */ - RAM (rwx) : ORIGIN = 0xF8F80000, LENGTH = 64K - - /* L1 as SRAM (up to 16KB) */ - LRAM (rwx) : ORIGIN = 0xFFD00000, LENGTH = 16K - /* DDR3 - 512MB (offset by destination address and 4KB boot region) */ DRAM (rwx) : ORIGIN = @WOLFBOOT_STAGE1_LOAD_ADDR@, LENGTH = 0x1FFFFFFF - @WOLFBOOT_STAGE1_LOAD_ADDR@ + + /* L1 as SRAM (up to 16KB) */ + RAM (rwx) : ORIGIN = 0xFFD00000, LENGTH = 16K } SECTIONS @@ -77,5 +77,5 @@ SECTIONS } > DRAM } -PROVIDE(_start_heap = ORIGIN(DRAM)); -PROVIDE(_end_stack = ORIGIN(LRAM) + (LENGTH(LRAM)) ); +PROVIDE(_start_heap = _end); +PROVIDE(_end_stack = _end + HEAP_SIZE + STACK_SIZE ); diff --git a/hal/nxp_p1021_stage1.ld b/hal/nxp_p1021_stage1.ld index eb772c93..c72d2ada 100644 --- a/hal/nxp_p1021_stage1.ld +++ b/hal/nxp_p1021_stage1.ld @@ -5,25 +5,21 @@ ENTRY( _reset ) BASE_ADDR = @WOLFBOOT_STAGE1_BASE_ADDR@; /* for flashing to NAND it must be 4KB */ -/* if debugging and running from RAM this can be increased */ -MAX_LOADER_SIZE = 0x1000; /* 4KB */ +LOADER_STAGE1_SIZE = @WOLFBOOT_STAGE1_SIZE@; /* Boot initialization code */ BOOTSTRAP_TLB = BASE_ADDR; /* Entry point where boot executes from (end of region) */ -BOOTSTRAP_ENTRY = BASE_ADDR + MAX_LOADER_SIZE - 4; +BOOTSTRAP_ENTRY = BASE_ADDR + LOADER_STAGE1_SIZE - 4; MEMORY { /* Boot Location */ - FLASH (rx) : ORIGIN = BOOTSTRAP_TLB, LENGTH = MAX_LOADER_SIZE - - /* L2 as SRAM (up to 256KB) */ - RAM (rwx) : ORIGIN = BASE_ADDR + MAX_LOADER_SIZE, LENGTH = 128K - MAX_LOADER_SIZE + FLASH (rx) : ORIGIN = BOOTSTRAP_TLB, LENGTH = LOADER_STAGE1_SIZE /* L1 as SRAM (up to 16KB) */ - L1RAM (rwx) : ORIGIN = 0xffd00000, LENGTH = 16K + RAM (rwx) : ORIGIN = 0xffd00000, LENGTH = 16K } SECTIONS @@ -84,4 +80,4 @@ SECTIONS } PROVIDE(_start_heap = _end); -PROVIDE(_end_stack = ORIGIN(L1RAM) + (LENGTH(L1RAM)) ); +PROVIDE(_end_stack = ORIGIN(RAM) + (LENGTH(RAM)) ); diff --git a/hal/nxp_ppc.h b/hal/nxp_ppc.h index db11257d..d4e9e54e 100644 --- a/hal/nxp_ppc.h +++ b/hal/nxp_ppc.h @@ -35,10 +35,6 @@ /* Relocate CCSRBAR */ #define CCSRBAR 0xFFE00000 -/* boot address */ -#define BOOT_ROM_ADDR 0xFFFFF000UL -#define BOOT_ROM_SIZE (4*1024) - /* Memory used for transferring blocks to/from NAND. * Maps to eLBC FCM internal 8KB region (by hardware) */ #define FLASH_BASE_ADDR 0xFC000000 @@ -65,6 +61,10 @@ #error Please define MMU version and CCSRBAR for platform #endif +/* boot address */ +#define BOOT_ROM_ADDR 0xFFFFF000UL +#define BOOT_ROM_SIZE (4*1024) + #define RESET_VECTOR (BOOT_ROM_ADDR + (BOOT_ROM_SIZE - 4)) #ifndef CCSRBAR_DEF diff --git a/include/image.h b/include/image.h index 8f2563bf..0e970734 100644 --- a/include/image.h +++ b/include/image.h @@ -34,6 +34,15 @@ extern "C" { #include "target.h" #include "wolfboot/wolfboot.h" +#ifdef EXT_FLASH +#include "hal.h" +#endif + +#if defined(EXT_ENCRYPTED) && (defined(__WOLFBOOT) || defined(UNIT_TEST)) +#include "encrypt.h" +#endif + + int wolfBot_get_dts_size(void *dts_addr); @@ -576,11 +585,9 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr); ((pn == PART_UPDATE || pn == PART_DTS_UPDATE)?UPDATE_EXT: \ ((pn == PART_SWAP)?SWAP_EXT:0))) # define PART_IS_EXT(x) PARTN_IS_EXT(((x)->part)) -#include "hal.h" #if defined(EXT_ENCRYPTED) && (defined(__WOLFBOOT) || defined(UNIT_TEST)) -#include "encrypt.h" #define ext_flash_check_write ext_flash_encrypt_write #define ext_flash_check_read ext_flash_decrypt_read #else @@ -588,7 +595,8 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr); #define ext_flash_check_read ext_flash_read #endif -static inline int wb_flash_erase(struct wolfBoot_image *img, uint32_t off, uint32_t size) +static inline int wb_flash_erase(struct wolfBoot_image *img, uint32_t off, + uint32_t size) { if (PART_IS_EXT(img)) return ext_flash_erase((uintptr_t)(img->hdr) + off, size); @@ -596,7 +604,8 @@ static inline int wb_flash_erase(struct wolfBoot_image *img, uint32_t off, uint3 return hal_flash_erase((uintptr_t)(img->hdr) + off, size); } -static inline int wb_flash_write(struct wolfBoot_image *img, uint32_t off, const void *data, uint32_t size) +static inline int wb_flash_write(struct wolfBoot_image *img, uint32_t off, + const void *data, uint32_t size) { if (PART_IS_EXT(img)) return ext_flash_check_write((uintptr_t)(img->hdr) + off, data, size); @@ -604,18 +613,22 @@ static inline int wb_flash_write(struct wolfBoot_image *img, uint32_t off, const return hal_flash_write((uintptr_t)(img->hdr) + off, data, size); } -static inline int wb_flash_write_verify_word(struct wolfBoot_image *img, uint32_t off, uint32_t word) +static inline int wb_flash_write_verify_word(struct wolfBoot_image *img, + uint32_t off, uint32_t word) { int ret; volatile uint32_t copy; if (PART_IS_EXT(img)) { - ext_flash_check_read((uintptr_t)(img->hdr) + off, (void *)©, sizeof(uint32_t)); + ext_flash_check_read((uintptr_t)(img->hdr) + off, (void *)©, + sizeof(uint32_t)); while (copy != word) { - ret = ext_flash_check_write((uintptr_t)(img->hdr) + off, (void *)&word, sizeof(uint32_t)); + ret = ext_flash_check_write((uintptr_t)(img->hdr) + off, + (void *)&word, sizeof(uint32_t)); if (ret < 0) return ret; - ext_flash_check_read((uintptr_t)(img->hdr) + off, (void *)©, sizeof(uint32_t)); + ext_flash_check_read((uintptr_t)(img->hdr) + off, (void *)©, + sizeof(uint32_t)); } } else { volatile uint32_t *pcopy = (volatile uint32_t*)(img->hdr + off); @@ -631,8 +644,10 @@ static inline int wb_flash_write_verify_word(struct wolfBoot_image *img, uint32_ # define PART_IS_EXT(x) (0) # define PARTN_IS_EXT(x) (0) -# define wb_flash_erase(im, of, siz) hal_flash_erase(((uintptr_t)(((im)->hdr)) + of), siz) -# define wb_flash_write(im, of, dat, siz) hal_flash_write(((uintptr_t)((im)->hdr)) + of, dat, siz) +# define wb_flash_erase(im, of, siz) \ + hal_flash_erase(((uintptr_t)(((im)->hdr)) + of), siz) +# define wb_flash_write(im, of, dat, siz) \ + hal_flash_write(((uintptr_t)((im)->hdr)) + of, dat, siz) #endif /* EXT_FLASH */ @@ -645,9 +660,10 @@ static inline int wb_flash_write_verify_word(struct wolfBoot_image *img, uint32_ #define UBOOT_FDT_MAGIC 0xEDFE0DD0UL #ifndef EXT_ENCRYPTED -#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP + sizeof(uint32_t) + (WOLFBOOT_PARTITION_SIZE + 1 / (WOLFBOOT_SECTOR_SIZE * 8)))) +#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - \ + (TRAILER_SKIP + sizeof(uint32_t) + \ + (WOLFBOOT_PARTITION_SIZE + 1 / (WOLFBOOT_SECTOR_SIZE * 8)))) #else -#include "encrypt.h" #define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - ENCRYPT_TMP_SECRET_OFFSET) #endif diff --git a/include/user_settings.h b/include/user_settings.h index 1a97c79d..52a44358 100644 --- a/include/user_settings.h +++ b/include/user_settings.h @@ -34,6 +34,12 @@ #define WOLFCRYPT_ONLY #define SIZEOF_LONG_LONG 8 +#define CTYPE_USER /* don't let wolfCrypt types.h include ctype.h */ +extern int toupper(int c); +extern int tolower(int c); +#define XTOUPPER(c) toupper((c)) +#define XTOLOWER(c) tolower((c)) + #ifdef USE_FAST_MATH # define WC_NO_HARDEN #endif diff --git a/options.mk b/options.mk index f2853262..4fbef934 100644 --- a/options.mk +++ b/options.mk @@ -376,8 +376,9 @@ else endif endif -ifeq ($(V),0) - Q=@ +Q?=@ +ifeq ($(V),1) + Q= endif ifeq ($(NO_MPU),1) @@ -415,9 +416,6 @@ ifeq ($(WOLFBOOT_HUGE_STACK),1) CFLAGS+=-DWOLFBOOT_HUGE_STACK endif -OBJS+=$(PUBLIC_KEY_OBJS) -OBJS+=$(UPDATE_OBJS) - ifeq ($(WOLFTPM),1) OBJS += lib/wolfTPM/src/tpm2.o \ lib/wolfTPM/src/tpm2_packet.o \ @@ -466,7 +464,6 @@ ifeq ($(HASH),SHA3) endif CFLAGS+=-DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE) -OBJS+=$(WOLFCRYPT_OBJS) # check if both encryption and self update are on # diff --git a/src/boot_ppc_start.S b/src/boot_ppc_start.S index 3044ab9e..30b8de64 100644 --- a/src/boot_ppc_start.S +++ b/src/boot_ppc_start.S @@ -412,7 +412,7 @@ setup_l2: stw r4, 0(r6) /* write L2SC0 MMR to flash invalidate L2 cache and locks */ l2loop: sync - lwz r5, 0(6r) /* get current L2SC0 MMR value */ + lwz r5, 0(r6) /* get current L2SC0 MMR value */ and. r5, r5, r4 /* compare to mask to see if complete */ bne l2loop isync diff --git a/src/boot_renesas.c b/src/boot_renesas.c old mode 100755 new mode 100644 diff --git a/src/loader.c b/src/loader.c old mode 100755 new mode 100644 diff --git a/src/string.c b/src/string.c index f3aa7ce1..15b30eee 100644 --- a/src/string.c +++ b/src/string.c @@ -37,6 +37,14 @@ size_t strlen(const char *s); /* forward declaration */ #endif #endif +#if !defined(__IAR_SYSTEMS_ICC__) && !defined(PLATFORM_X86_64_EFI) +/* for RAMFUNCTION */ +#include "image.h" +#endif + +#if !defined(BUILD_LOADER_STAGE1) || \ + (defined(PRINTF_ENABLED) && defined(DEBUG_UART)) + int islower(int c) { return (c >= 'a' && c <= 'z'); @@ -137,16 +145,6 @@ int strncasecmp(const char *s1, const char *s2, size_t n) return diff; } -size_t strlen(const char *s) -{ - size_t i = 0; - - while (s[i] != 0) - i++; - - return i; -} - char *strncat(char *dest, const char *src, size_t n) { size_t i = 0; @@ -179,22 +177,6 @@ int strncmp(const char *s1, const char *s2, size_t n) return diff; } -#if !defined(__IAR_SYSTEMS_ICC__) && !defined(PLATFORM_X86_64_EFI) -#include "image.h" -void RAMFUNCTION *memcpy(void *dst, const void *src, size_t n) -{ - size_t i; - const char *s = (const char *)src; - char *d = (char *)dst; - - for (i = 0; i < n; i++) { - d[i] = s[i]; - } - - return dst; -} -#endif /* IAR */ - char *strncpy(char *dst, const char *src, size_t n) { size_t i; @@ -222,7 +204,6 @@ char *strcpy(char *dst, const char *src) return dst; } - int memcmp(const void *_s1, const void *_s2, size_t n) { int diff = 0; @@ -239,6 +220,35 @@ int memcmp(const void *_s1, const void *_s2, size_t n) return diff; } +#endif /* !BUILD_LOADER_STAGE1 || (PRINTF_ENABLED && DEBUG_UART) */ + +#if !defined(BUILD_LOADER_STAGE1) || defined(DEBUG_UART) +size_t strlen(const char *s) +{ + size_t i = 0; + + while (s[i] != 0) + i++; + + return i; +} +#endif + +#if !defined(__IAR_SYSTEMS_ICC__) && !defined(PLATFORM_X86_64_EFI) +void RAMFUNCTION *memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + const char *s = (const char *)src; + char *d = (char *)dst; + + for (i = 0; i < n; i++) { + d[i] = s[i]; + } + + return dst; +} +#endif /* IAR */ + #ifndef __IAR_SYSTEMS_ICC__ void *memmove(void *dst, const void *src, size_t n) { @@ -258,6 +268,7 @@ void *memmove(void *dst, const void *src, size_t n) } #endif + #if defined(PRINTF_ENABLED) && defined(DEBUG_UART) void uart_writenum(int num, int base) { diff --git a/stage1/Makefile b/stage1/Makefile old mode 100755 new mode 100644 index d9dc4b69..395bf63a --- a/stage1/Makefile +++ b/stage1/Makefile @@ -12,7 +12,7 @@ WOLFBOOT_ROOT?=$(PWD) CFLAGS:=-D"__WOLFBOOT" CFLAGS+=-Werror -Wextra LDFLAGS:= -LSCRIPT:=../config/loader_stage1.ld +LSCRIPT:=loader_stage1.ld LSCRIPT_IN:=../hal/$(TARGET)_stage1.ld LSCRIPT_FLAGS:= LD_START_GROUP:=-Wl,--start-group @@ -24,7 +24,7 @@ DEBUG_UART?=0 OBJS:= \ ./hal/$(TARGET).o \ ./src/string.o \ - ./src/loader_stage1.o + loader_stage1.o WOLFCRYPT_OBJS:= PUBLIC_KEY_OBJS:= @@ -55,12 +55,21 @@ TARGET_H_TEMPLATE:=../include/target.h.in ASFLAGS:=$(CFLAGS) BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) +ifeq ($(WOLFBOOT_STAGE1_SIZE),) + # default is end of 4KB region (0x0FFC) + WOLFBOOT_STAGE1_SIZE=0x1000 +endif + CFLAGS+=\ - -DBUILD_LOADER_STAGE1 -DNO_PRINTF_UART \ + -DBUILD_LOADER_STAGE1 \ + -DWOLFBOOT_STAGE1_SIZE=$(WOLFBOOT_STAGE1_SIZE) \ -DWOLFBOOT_STAGE1_LOAD_ADDR=$(WOLFBOOT_STAGE1_LOAD_ADDR) \ -DWOLFBOOT_STAGE1_FLASH_ADDR=$(WOLFBOOT_STAGE1_FLASH_ADDR) \ -DWOLFBOOT_STAGE1_BASE_ADDR=$(WOLFBOOT_STAGE1_BASE_ADDR) +# For printf support (disable NO_PRINTF_UART) and increase WOLFBOOT_STAGE1_SIZE +CFLAGS+=-DNO_PRINTF_UART + BUILD_DIR=. LS1_OBJS=$(addprefix $(BUILD_DIR)/, $(notdir $(OBJS))) vpath %.c $(dir ../src) @@ -103,6 +112,7 @@ $(LSCRIPT): FORCE sed -e "s/@WOLFBOOT_PARTITION_SIZE@/$(WOLFBOOT_PARTITION_SIZE)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_UPDATE_ADDRESS@/$(WOLFBOOT_PARTITION_UPDATE_ADDRESS)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_SWAP_ADDRESS@/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" | \ + sed -e "s/@WOLFBOOT_STAGE1_SIZE@/$(WOLFBOOT_STAGE1_SIZE)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_LOAD_ADDR@/$(WOLFBOOT_STAGE1_LOAD_ADDR)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_FLASH_ADDR@/$(WOLFBOOT_STAGE1_FLASH_ADDR)/g" | \ sed -e "s/@WOLFBOOT_STAGE1_BASE_ADDR@/$(WOLFBOOT_STAGE1_BASE_ADDR)/g" \ @@ -110,7 +120,7 @@ $(LSCRIPT): FORCE %.hex:%.elf @echo "\t[ELF2HEX] $@" - @$(OBJCOPY) -O ihex $^ $@ + $(Q)@$(OBJCOPY) -O ihex $^ $@ ../include/target.h: $(TARGET_H_TEMPLATE) FORCE @cat $(TARGET_H_TEMPLATE) | \ @@ -150,7 +160,7 @@ $(BUILD_DIR)/%.o: ../hal/%.S clean: $(Q)rm -f *.o - $(Q)rm -f loader_stage1.bin loader_stage1.elf loader_stage1.map ../config/loader_stage1.ld + $(Q)rm -f loader_stage1.bin loader_stage1.elf loader_stage1.map loader_stage1.ld FORCE: diff --git a/src/loader_stage1.c b/stage1/loader_stage1.c similarity index 58% rename from src/loader_stage1.c rename to stage1/loader_stage1.c index fd2ff593..d7e577f7 100644 --- a/src/loader_stage1.c +++ b/stage1/loader_stage1.c @@ -28,20 +28,29 @@ #include "spi_flash.h" #include "printf.h" #include "wolfboot/wolfboot.h" +#include "string.h" + +#include #ifndef EXT_FLASH #error The stage1 loader only supports use with external flash #endif -#ifndef LOADER_STAGE1_SIZE - #define LOADER_STAGE1_SIZE (4*1024) -#endif -#ifndef LOADER_STAGE1_START_ADDR - /* default is end of 4KB region (0x0FFC) */ - #define LOADER_STAGE1_START_ADDR \ - (WOLFBOOT_STAGE1_LOAD_ADDR + LOADER_STAGE1_SIZE - 0x4) +#ifndef WOLFBOOT_STAGE1_SIZE + #define WOLFBOOT_STAGE1_SIZE (4*1024) #endif +#ifndef WOLFBOOT_STAGE1_START_ADDR + /* default is end of 4KB region (0x0FFC) */ + #define WOLFBOOT_STAGE1_START_ADDR \ + (WOLFBOOT_STAGE1_LOAD_ADDR + WOLFBOOT_STAGE1_SIZE - 0x4) +#endif + +#ifdef BUILD_LOADER_STAGE1 + +#if defined(WOLFBOOT_ARCH) && WOLFBOOT_ARCH == PPC +#include "hal/nxp_ppc.h" +#endif int main(void) { @@ -58,15 +67,31 @@ int main(void) uart_write("Loader Stage 1\r\n", 16); #endif +#ifdef BOOT_ROM_ADDR + /* if this is executing from boot 4KB region (FCM buffer) it must + * first be relocated to RAM before the eLBC NAND can be read */ + if (((uintptr_t)&hal_init & BOOT_ROM_ADDR) == BOOT_ROM_ADDR) { + wolfboot_start = (void*)WOLFBOOT_STAGE1_BASE_ADDR + BOOT_ROM_SIZE - 0x04; + + /* relocate 4KB code to DST and jump */ + memmove((void*)WOLFBOOT_STAGE1_BASE_ADDR, (void*)BOOT_ROM_ADDR, + BOOT_ROM_SIZE); + + wolfboot_start(); /* never returns */ + } +#endif + ret = ext_flash_read( - (uintptr_t)WOLFBOOT_ORIGIN, /* flash offset */ - (uint8_t*)WOLFBOOT_STAGE1_LOAD_ADDR, /* ram destination */ - BOOTLOADER_PARTITION_SIZE /* boot-loader partition (entire) */ + (uintptr_t)WOLFBOOT_ORIGIN, /* flash offset */ + (uint8_t*)WOLFBOOT_STAGE1_LOAD_ADDR,/* ram destination */ + BOOTLOADER_PARTITION_SIZE /* boot-loader partition (entire) */ ); if (ret >= 0) { - wolfboot_start = (void*)LOADER_STAGE1_START_ADDR; + wolfboot_start = (void*)WOLFBOOT_STAGE1_START_ADDR; wolfboot_start(); /* never returns */ } return 0; } + +#endif /* BUILD_LOADER_STAGE1 */ diff --git a/test-app/Makefile b/test-app/Makefile index de4c4ec0..4cdc2f86 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -68,10 +68,6 @@ ifeq ($(ARCH),ARM_BE) CFLAGS+=-DSTACK_PAINTING=0 endif -ifeq ($(V),0) - Q=@ -endif - ifeq ($(ENCRYPT),1) CFLAGS+=-D"EXT_ENCRYPTED=1" endif @@ -264,26 +260,26 @@ standalone:CFLAGS+=-D"TEST_APP_STANDALONE" standalone:LDFLAGS:=-T standalone.ld -Wl,-gc-sections -Wl,-Map=image.map image.bin: image.elf - $(Q)echo "\t[BIN] $@" + @echo "\t[BIN] $@" $(Q)$(OBJCOPY) --gap-fill $(FILL_BYTE) -O binary $^ $@ image.elf: $(APP_OBJS) $(LSCRIPT) - $(Q)echo "\t[LD] $@" + @echo "\t[LD] $@" $(Q)$(LD) $(LDFLAGS) $(APP_OBJS) $(OUTPUT_FLAG) $@ standalone: image.bin ../src/libwolfboot.o: ../src/libwolfboot.c FORCE - $(Q)echo "\t[CC-$(ARCH)] $@" + @echo "\t[CC-$(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ ../src/libwolfboot.c %.o:%.c - $(Q)echo "\t[CC-$(ARCH)] $@" + @echo "\t[CC-$(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $^ %.o:%.S - $(Q)echo "\t[AS-$(ARCH)] $@" + @echo "\t[AS-$(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $^ clean: diff --git a/tools/bin-assemble/Makefile b/tools/bin-assemble/Makefile index 99645a10..2fda4157 100644 --- a/tools/bin-assemble/Makefile +++ b/tools/bin-assemble/Makefile @@ -8,11 +8,6 @@ EXE=bin-assemble LIBS= -Q?=@ -ifeq ($(V),1) - Q= -endif - $(EXE): $(EXE).o $(Q)$(CC) -o $@ $^ $(CFLAGS) $(LIBS) diff --git a/tools/uart-flash-server/Makefile b/tools/uart-flash-server/Makefile index 1d7ec762..fe0b5b1f 100644 --- a/tools/uart-flash-server/Makefile +++ b/tools/uart-flash-server/Makefile @@ -4,10 +4,6 @@ CC=gcc CFLAGS+=-DBUILD_TOOL -Wall -g -ggdb -I../../include -I../../hal -Wextra -Q?=@ -ifeq ($(V),1) - Q= -endif EXE=ufserver