diff --git a/Makefile b/Makefile index 0ddfaa71..9ad4f3c6 100644 --- a/Makefile +++ b/Makefile @@ -16,10 +16,6 @@ LDFLAGS:= LD_START_GROUP:=-Wl,--start-group LD_END_GROUP:=-Wl,--end-group - - - - V?=0 OBJS:= \ diff --git a/config/examples/raspi3.config b/config/examples/raspi3.config index 705d1773..f4afe233 100644 --- a/config/examples/raspi3.config +++ b/config/examples/raspi3.config @@ -4,26 +4,10 @@ SIGN?=RSA4096 HASH?=SHA3 DEBUG?=1 VTOR?=1 -CORTEX_M0?=0 -NO_ASM?=0 -EXT_FLASH?=0 -SPI_FLASH?=0 -ALLOW_DOWNGRADE?=0 -NVM_FLASH_WRITEONCE?=0 -WOLFBOOT_VERSION?=0 -V?=0 SPMATH?=1 -RAM_CODE?=0 -DUALBANK_SWAP?=0 IMAGE_HEADER_SIZE?=1024 PKA?=1 WOLFTPM?=0 WOLFBOOT_NO_PARTITIONS=1 -WOLFBOOT_PARTITION_SIZE=0 -WOLFBOOT_SECTOR_SIZE=0 -WOLFBOOT_PARTITION_BOOT_ADDRESS?=0 -WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0 -WOLFBOOT_PARTITION_SWAP_ADDRESS?=0 -WOLFBOOT_LOAD_ADDRESS?=0x140000 -WOLFBOOT_LOAD_DTS_ADDRESS?=0xa0000 -WOLFBOOT_DTS_BOOT_ADDRESS?=0xa0000 +WOLFBOOT_LOAD_ADDRESS?=0x3080000 +WOLFBOOT_LOAD_DTS_ADDRESS?=0x400000 diff --git a/docs/Targets.md b/docs/Targets.md index 0c612086..08c8997f 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -801,6 +801,7 @@ git clone https://github.com/raspberrypi/linux linux-rpi -b rpi-4.19.y --depth=1 ``` export wolfboot_dir=`pwd` cd linux-rpi +patch -p1 < $wolfboot_dir/tools/wolfboot-rpi-devicetree.diff make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- ``` @@ -822,7 +823,7 @@ make clean make wolfboot.bin CROSS_COMPILE=aarch64-linux-gnu- ``` -* Sign Image +* Sign Linux kernel image ``` make keytools ./tools/keytools/sign --rsa4096 --sha3 Image wolfboot_signing_private_key.der 1 @@ -839,7 +840,7 @@ dd if=bcm2710-rpi-3-b.dtb of=wolfboot_linux_raspi.bin bs=1 seek=128K conv=notrun * Test boot using qemu ``` -qemu-system-aarch64 -M raspi3 -m 512 -serial stdio -kernel wolfboot_linux_raspi.bin -append "terminal=ttyS0 rootwait" -dtb ./bcm2710-rpi-3-b.dtb -cpu cortex-a53 +qemu-system-aarch64 -M raspi3 -m 512 -serial stdio -kernel wolfboot_linux_raspi.bin -append "terminal=ttyS0 rootwait" -cpu cortex-a53 ``` diff --git a/hal/raspi3.c b/hal/raspi3.c index f913b213..0a105f61 100644 --- a/hal/raspi3.c +++ b/hal/raspi3.c @@ -31,12 +31,10 @@ #define CORTEXA53_0_CPU_CLK_FREQ_HZ 1099989014 #define CORTEXA53_0_TIMESTAMP_CLK_FREQ 99998999 -#ifndef WOLFBOOT_UPDATE_ADDRESS -# define WOLFBOOT_UPDATE_ADDRESS 0x00000 -#endif - -static const void* kernel_addr = (void*)0x0140000; -static const void* update_addr = (void*)0x1140000; +/* Fixed addresses */ +static const void* kernel_addr = (void*)0x0140000; +static const void* update_addr = (void*)0x1140000; +static const void* dts_addr = (void*)0x00a0000; void* hal_get_primary_address(void) { @@ -48,7 +46,15 @@ void* hal_get_update_address(void) return (void*)update_addr; } +void* hal_get_dts_address(void) +{ + return (void*)dts_addr; +} +void* hal_get_dts_update_address(void) +{ + return NULL; /* Not yet supported */ +} /* QSPI functions */ void qspi_init(uint32_t cpu_clock, uint32_t flash_freq) diff --git a/hal/x86_64_efi.c b/hal/x86_64_efi.c index f01240d9..aaadf36f 100644 --- a/hal/x86_64_efi.c +++ b/hal/x86_64_efi.c @@ -80,6 +80,15 @@ void* hal_get_update_address(void) return (void*)update_addr; } +void *hal_get_dts_address(void) +{ + return NULL; +} + +void *hal_get_dts_update_address(void) +{ + return NULL; +} static void panic() { diff --git a/include/hal.h b/include/hal.h index 1e4d902b..5ba809c6 100644 --- a/include/hal.h +++ b/include/hal.h @@ -52,6 +52,11 @@ void hal_prepare_boot(void); void* hal_get_update_address(void); #endif +#ifdef MMU + void *hal_get_dts_address(void); + void *hal_get_dts_update_address(void); +#endif + #ifndef SPI_FLASH /* user supplied external flash interfaces */ int ext_flash_write(uintptr_t address, const uint8_t *data, int len); diff --git a/include/image.h b/include/image.h index 6244665f..7a462d9d 100644 --- a/include/image.h +++ b/include/image.h @@ -29,6 +29,8 @@ #include "target.h" #include "wolfboot/wolfboot.h" +int wolfBot_get_dts_size(void *dts_addr); + #ifndef RAMFUNCTION #if defined(__WOLFBOOT) && defined(RAM_CODE) @@ -538,7 +540,9 @@ int wolfBoot_set_partition_state(uint8_t part, uint8_t newst); int wolfBoot_get_update_sector_flag(uint16_t sector, uint8_t *flag); int wolfBoot_set_update_sector_flag(uint16_t sector, uint8_t newflag); -uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset, uint32_t* sz); +uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset, + uint32_t* sz); + /* Defined in libwolfboot */ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr); diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index 1ee17628..37d3a977 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -266,6 +266,6 @@ int wolfBoot_get_diffbase_hdr(uint8_t part, uint8_t **ptr); int wolfBoot_set_encrypt_key(const uint8_t *key, const uint8_t *nonce); int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce); int wolfBoot_erase_encrypt_key(void); - +uint32_t wb_reverse_word32(uint32_t x); #endif /* !WOLFBOOT_H */ diff --git a/src/image.c b/src/image.c index afb0c699..fd76cc5a 100644 --- a/src/image.c +++ b/src/image.c @@ -808,10 +808,36 @@ int wolfBoot_open_image_address(struct wolfBoot_image* img, uint8_t* image) return 0; } +#ifdef MMU +/* misc.c included via libwolfboot.c */ +extern uint32_t wb_reverse_word32(uint32_t); + +int wolfBoot_get_dts_size(void *dts_addr) +{ + uint32_t hdr[2]; + uint32_t magic; + uint32_t size; + memcpy(hdr, dts_addr, 2 * sizeof(uint32_t)); + +#ifdef BIG_ENDIAN_ORDER + magic = wb_reverse_word32(hdr[0]); + size = hdr[1]; +#else + magic = hdr[0]; + size = wb_reverse_word32(hdr[1]); +#endif + if (magic != UBOOT_FDT_MAGIC) + return -1; + else + return (int)size; +} +#endif + #ifdef WOLFBOOT_FIXED_PARTITIONS int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part) { - uint32_t *size; + int ret; + uint32_t size; uint8_t *image; if (!img) return -1; @@ -836,16 +862,12 @@ int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part) image = fetch_hdr_cpy(img); else image = (uint8_t*)img->hdr; - if (*((uint32_t*)image) != UBOOT_FDT_MAGIC) + ret = wolfBoot_get_dts_size(image); + if (ret < 0) return -1; img->hdr_ok = 1; img->fw_base = img->hdr; - /* DTS data is big endian */ - size = (uint32_t*)(image + sizeof(uint32_t)); - img->fw_size = (((*size & 0x000000FF) << 24) | - ((*size & 0x0000FF00) << 8) | - ((*size & 0x00FF0000) >> 8) | - ((*size & 0xFF000000) >> 24)); + img->fw_size = (uint32_t)size; return 0; } #endif diff --git a/src/libwolfboot.c b/src/libwolfboot.c index fe19f5fd..b27ef302 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -54,7 +54,6 @@ /* MAGIC + PART_FLAG (1B) + (N_SECTORS / 2) */ #define START_FLAGS_OFFSET (ENCRYPT_TMP_SECRET_OFFSET - TRAILER_OVERHEAD) #else - #define XMEMCPY memcpy #define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP)) #endif @@ -70,6 +69,15 @@ static uint32_t ext_cache; #endif +/* Inline use of ByteReverseWord32 */ +#define WOLFSSL_MISC_INCLUDED +#include +uint32_t wb_reverse_word32(uint32_t x) +{ + return ByteReverseWord32(x); +} + + static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; /* Top addresses for FLAGS field * - PART_BOOT_ENDFLAGS = top of flags for BOOT partition @@ -834,10 +842,6 @@ int chacha_init(void) #elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256) -/* Inline use of ByteReverseWord32 */ -#define WOLFSSL_MISC_INCLUDED -#include - Aes aes_dec, aes_enc; int aes_init(void) @@ -878,7 +882,7 @@ void aes_set_iv(uint8_t *nonce, uint32_t iv_ctr) XMEMCPY(iv_buf, nonce, ENCRYPT_NONCE_SIZE); #ifndef BIG_ENDIAN_ORDER for (i = 0; i < 4; i++) { - iv_buf[i] = ByteReverseWord32(iv_buf[i]); + iv_buf[i] = wb_reverse_word32(iv_buf[i]); } #endif iv_buf[3] += iv_ctr; @@ -891,7 +895,7 @@ void aes_set_iv(uint8_t *nonce, uint32_t iv_ctr) } #ifndef BIG_ENDIAN_ORDER for (i = 0; i < 4; i++) { - iv_buf[i] = ByteReverseWord32(iv_buf[i]); + iv_buf[i] = wb_reverse_word32(iv_buf[i]); } #endif wc_AesSetIV(&aes_enc, (byte *)iv_buf); diff --git a/src/update_ram.c b/src/update_ram.c index 4981facb..34a54c76 100644 --- a/src/update_ram.c +++ b/src/update_ram.c @@ -22,22 +22,16 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -#include "loader.h" #include "image.h" +#include "loader.h" #include "hal.h" #include "spi_flash.h" #include "printf.h" #include "wolfboot/wolfboot.h" #include -#ifdef PLATFORM_X86_64_EFI - #include "efi/efi.h" - #include "efi/efilib.h" - extern EFI_PHYSICAL_ADDRESS kernel_addr; - extern EFI_PHYSICAL_ADDRESS update_addr; -#endif - extern void hal_flash_dualbank_swap(void); +extern int wolfBoot_get_dts_size(void *dts_addr); static inline void boot_panic(void) { @@ -45,35 +39,30 @@ static inline void boot_panic(void) ; } + void RAMFUNCTION wolfBoot_start(void) { int active, ret = 0; struct wolfBoot_image os_image; - uint32_t *dtb_load_address = (void*)0x4000000; -#ifdef WOLFBOOT_FIXED_PARTITIONS - uint32_t* load_address = (uint32_t*)WOLFBOOT_LOAD_ADDRESS; -#else - uint32_t* load_address = hal_get_primary_address(); -#endif - - uint8_t* image_ptr; + uint8_t *image_ptr; uint8_t p_state; -#ifdef MMU - uint32_t* dts_address = (void *)WOLFBOOT_LOAD_DTS_ADDRESS; -#endif + uint32_t *load_address; + uint8_t *dts_buf = NULL; + uint32_t dts_size = 0; #ifdef WOLFBOOT_FIXED_PARTITIONS active = wolfBoot_dualboot_candidate(); + if (active == PART_UPDATE) + load_address = (uint32_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS; + else + load_address = (uint32_t*)WOLFBOOT_PARTITION_UPDATE_ADDRESS; #else active = wolfBoot_dualboot_candidate_addr((void**)&load_address); #endif - - wolfBoot_printf("Active Part %d\n", active); - if (active < 0) /* panic if no images available */ wolfBoot_panic(); - wolfBoot_printf("Active Part %x\n", load_address); + #ifdef WOLFBOOT_FIXED_PARTITIONS /* Check current status for failure (image still in TESTING), and fall-back * if an alternative is available @@ -85,8 +74,9 @@ void RAMFUNCTION wolfBoot_start(void) active ^= 1; /* switch to other partition if available */ } #endif - wolfBoot_printf("Active Part %d %x\n", active, load_address); + wolfBoot_printf("Active Partition: %c\n", active?'B':'A'); + wolfBoot_printf("Active Partition start address: %x\n", load_address); for (;;) { if (((ret = wolfBoot_open_image_address(&os_image, (uint8_t*)load_address)) < 0) || ((ret = wolfBoot_verify_integrity(&os_image) < 0)) || @@ -140,40 +130,51 @@ void RAMFUNCTION wolfBoot_start(void) #ifdef EXT_FLASH /* Load image to RAM */ if (PART_IS_EXT(&os_image)) { - wolfBoot_printf("Loading %d to RAM at %08lx\n", os_image.fw_size, load_address); + wolfBoot_printf("Loading %d bytes to RAM at %08lx\n", + os_image.fw_size, WOLFBOOT_LOAD_ADDRESS); ext_flash_read((uintptr_t)os_image.fw_base, - (uint8_t*)load_address, + (uint8_t*)WOLFBOOT_LOAD_ADDRESS, os_image.fw_size); } -#endif - -#if defined(WOLFBOOT_FIXED_PARTITIONS) && defined (MMU) - /* Device Tree Blob (DTB) Handling */ - if (wolfBoot_open_image(&os_image, PART_DTS_BOOT) >= 0) { - dts_address = (uint32_t*)WOLFBOOT_LOAD_DTS_ADDRESS; - - #ifdef EXT_FLASH - /* Load DTS to RAM */ - if (PART_IS_EXT(&os_image)) { - wolfBoot_printf("Loading DTS %d to RAM at %08lx\n", os_image.fw_size, dts_address); - - ext_flash_read((uintptr_t)os_image.fw_base, - (uint8_t*)dts_address, - os_image.fw_size); - } - #endif + #ifdef MMU + /* Load DTS to RAM */ + if (PART_IS_EXT(&os_image)) { + if (wolfBoot_open_image(&os_image, PART_DTS_BOOT) >= 0) { + dts_address = (uint32_t*)WOLFBOOT_LOAD_DTS_ADDRESS; + wolfBoot_printf("Loading DTS (size %lu) to RAM at %08lx\n", + os_image.fw_size, WOLFBOOT_LOAD_DTS_ADDRESS); + ext_flash_read((uintptr_t)os_image.fw_base, + (uint8_t*)WOLFBOOT_LOAD_DTS_ADDRESS, + os_image.fw_size); } -#endif -// load_address = (void *)0x200000 + (*(((uint64_t *)(os_image.fw_base)) + 1)); - load_address = (void *)0x3000000 + (*(((uint64_t *)(os_image.fw_base)) + 1)); - memcpy((void*)load_address, os_image.fw_base, os_image.fw_size); - memcpy((void*)dtb_load_address, dts_address, 26524); - wolfBoot_printf("Booting at %08lx\n", load_address); + #endif /* MMU */ +#else + wolfBoot_printf("Loading %d bytes to RAM at %08lx\n", + os_image.fw_size, WOLFBOOT_LOAD_ADDRESS); + memcpy((void*)WOLFBOOT_LOAD_ADDRESS, os_image.fw_base, os_image.fw_size); + #ifdef MMU + dts_buf = hal_get_dts_address(); + if (dts_buf) { + ret = wolfBoot_get_dts_size(dts_buf); + if (ret < 0) { + wolfBoot_printf("Failed parsing DTB to load.\n"); + } else { + dts_size = (uint32_t)ret; + wolfBoot_printf("Loading DTB (size %d) to RAM at %08lx\n", + dts_size, dts_address); + memcpy((void*)WOLFBOOT_LOAD_DTS_ADDRESS, dts_buf, dts_size); + } + } + #endif /* MMU */ + +#endif /* WOLFBOOT_FIXED_PARTITIONS */ + wolfBoot_printf("Booting at %08lx\n", WOLFBOOT_LOAD_ADDRESS); hal_prepare_boot(); #if defined MMU - do_boot((uint32_t*)load_address, (uint32_t*)dtb_load_address); + do_boot((uint32_t*)WOLFBOOT_LOAD_ADDRESS, + (uint32_t*)WOLFBOOT_LOAD_DTS_ADDRESS); #else do_boot((uint32_t*)os_image.fw_base); #endif diff --git a/tools/wolfboot-rpi-devicetree.diff b/tools/wolfboot-rpi-devicetree.diff new file mode 100644 index 00000000..390ae2ea --- /dev/null +++ b/tools/wolfboot-rpi-devicetree.diff @@ -0,0 +1,15 @@ +diff --git a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts +index 261827cdb957..13613f965a10 100644 +--- a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts ++++ b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts +@@ -18,6 +18,10 @@ + serial1 = &uart0; + mmc1 = &mmcnr; + }; ++ memory@0 { ++ reg = <0x00 0x3c000000>; ++ device_type = "memory"; ++ }; + }; + + &gpio {