diff --git a/contiki-nrf52/Makefile b/contiki-nrf52/Makefile index 370a53b..342cdfc 100644 --- a/contiki-nrf52/Makefile +++ b/contiki-nrf52/Makefile @@ -20,9 +20,14 @@ $(BOOT_ELF): nrf5_iot_sdk_3288530.zip $(WOLFBOOT_BIN) .contiki_patched make -C $(DTLS_OTA) TARGET=nrf52dk NRF52_SDK_ROOT=$(NRF52_SDK_ROOT) SMALL=1 $(WOLFBOOT_BIN): - cp target.h $(WOLFBOOT)/include cp nrf52.ld $(WOLFBOOT)/hal - make -C $(WOLFBOOT) BOOT0_OFFSET=0x10000 VTOR=0 TARGET=nrf52 DEBUG=$(DEBUG) wolfboot.bin + make -C $(WOLFBOOT) \ + WOLFBOOT_PARTITION_BOOT_ADDRESS=0x2f000 \ + WOLFBOOT_SECTOR_SIZE=0x1000 \ + WOLFBOOT_PARTITION_SIZE=0x28000 \ + WOLFBOOT_PARTITION_SWAP_ADDRESS=0x57000 \ + WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x58000 \ + VTOR=0 TARGET=nrf52 DEBUG=$(DEBUG) wolfboot.bin nrf5_iot_sdk_3288530.zip: wget https://developer.nordicsemi.com/nRF5_IoT_SDK/nRF5_IoT_SDK_v0.9.x/nrf5_iot_sdk_3288530.zip diff --git a/freeRTOS-Freescale-K64F-https-TLS1.3/Makefile b/freeRTOS-Freescale-K64F-https-TLS1.3/Makefile index a3288bc..1380916 100644 --- a/freeRTOS-Freescale-K64F-https-TLS1.3/Makefile +++ b/freeRTOS-Freescale-K64F-https-TLS1.3/Makefile @@ -128,20 +128,21 @@ wolfboot-align.bin:CFLAGS+=-I$(KINETIS_DRIVERS)/drivers -I$(KINETIS_DRIVERS) -DC wolfboot-align.bin:LDFLAGS=$(CFLAGS) -Wl,-gc-sections -ffreestanding -nostartfiles -lc -lnosys -specs=nano.specs -Wl,-Map=image.map wolfboot-align.bin: make -C ../wolfBoot TARGET=kinetis NVM_FLASH_WRITEONCE=1 clean - cp -f src/target.h ../wolfBoot/include/ rm -f ../wolfBoot/hal/kinetis.o rm -f ../wolfBoot/src/*.o rm -f $(KINETIS_DRIVERS)/drivers/*.o - make -C ../wolfBoot TARGET=kinetis DEBUG=$(DEBUG) NVM_FLASH_WRITEONCE=1 wolfboot-align.bin + make -C ../wolfBoot TARGET=kinetis DEBUG=$(DEBUG) NVM_FLASH_WRITEONCE=1 \ + WOLFBOOT_SECTOR_SIZE=0x1000 \ + WOLFBOOT_PARTITION_SIZE=0x7A000 \ + WOLFBOOT_PARTITION_BOOT_ADDRESS=0xA000 \ + WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x84000 \ + WOLFBOOT_PARTITION_SWAP_ADDRESS=0xff000 \ + wolfboot-align.bin cp ../wolfBoot/wolfboot-align.bin . - - standalone:LSCRIPT:=k64f_standalone.ld standalone: image.bin - - $(LIBS): picotcp make -C picotcp EXTRA_CFLAGS="-DPICO_PORT_CUSTOM $(CFLAGS) -I../src -I../freeRTOS/include -I../freeRTOS -I../$(FREERTOS_PORT)" \ ARCH=cortexm4-hardfloat CROSS_COMPILE=arm-none-eabi- RTOS=1 \ diff --git a/freeRTOS-Freescale-K64F-https-TLS1.3/picotcp b/freeRTOS-Freescale-K64F-https-TLS1.3/picotcp index 89653f2..789a156 160000 --- a/freeRTOS-Freescale-K64F-https-TLS1.3/picotcp +++ b/freeRTOS-Freescale-K64F-https-TLS1.3/picotcp @@ -1 +1 @@ -Subproject commit 89653f2aa7ad259fabca6caaaf411287ac39f222 +Subproject commit 789a1561b416f4caa29b00325e36f69a9a6b8b4b diff --git a/riotOS-samr21/RIOT b/riotOS-samr21/RIOT index b05f9bc..3d0e3b0 160000 --- a/riotOS-samr21/RIOT +++ b/riotOS-samr21/RIOT @@ -1 +1 @@ -Subproject commit b05f9bc2c20527ffc2a52213be34614fb6f7f005 +Subproject commit 3d0e3b01179cf4dc0d633385274ec8844c4792d4 diff --git a/riotOS-samr21/fw-update/Makefile b/riotOS-samr21/fw-update/Makefile index a146d68..cd5eef1 100644 --- a/riotOS-samr21/fw-update/Makefile +++ b/riotOS-samr21/fw-update/Makefile @@ -9,7 +9,7 @@ IMAGE_VERSION=5 BOARD ?= samr21-xpro # This has to be the absolute path to the RIOT base directory: -RIOTBASE ?= $(CURDIR)/../RIOT/ +RIOTBASE ?= $(abspath $(CURDIR)/../RIOT/) # Comment this out to disable code in RIOT that does safety checking # which is not needed in a production environment but helps in the @@ -33,6 +33,7 @@ USEMODULE += periph_flashpage WOLFBOOT_DIR=$(abspath $(RIOTBASE)/../../wolfBoot) USEMODULE_INCLUDES+=-I$(WOLFBOOT_DIR)/include +CFLAGS+=-Wno-unused-parameter -Wno-unused-variable -Wno-missing-include-dirs include $(RIOTBASE)/Makefile.include include ../wolfboot.mk diff --git a/riotOS-samr21/fw-update/libwolfboot/Makefile b/riotOS-samr21/fw-update/libwolfboot/Makefile index e094359..cef78d2 100644 --- a/riotOS-samr21/fw-update/libwolfboot/Makefile +++ b/riotOS-samr21/fw-update/libwolfboot/Makefile @@ -1,4 +1,4 @@ -WOLFBOOT_DIR=../../../../wolfBoot +WOLFBOOT_DIR=$(abspath $(CURDIR)/../../../wolfBoot) INCLUDES+=-I$(WOLFBOOT_DIR)/include SRC+=libwolfboot.c samr21.c NO_AUTO_SRC = 1 diff --git a/riotOS-samr21/fw-update/libwolfboot/libwolfboot.c b/riotOS-samr21/fw-update/libwolfboot/libwolfboot.c index 47ccfb3..05610ed 100644 --- a/riotOS-samr21/fw-update/libwolfboot/libwolfboot.c +++ b/riotOS-samr21/fw-update/libwolfboot/libwolfboot.c @@ -1,6 +1,6 @@ /* libwolfboot.c * - * Copyright (C) 2018 wolfSSL Inc. + * Copyright (C) 2019 wolfSSL Inc. * * This file is part of wolfBoot. * @@ -18,88 +18,193 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -#include + #include #include -#include -static uint8_t *get_trailer(uint8_t part) +#include "hal.h" +#include "wolfboot/wolfboot.h" +#include "image.h" + +#ifndef NULL +# define NULL (void *)0 +#endif + +uint32_t ext_cache; + +#ifndef TRAILER_SKIP +# define TRAILER_SKIP 0 +#endif +#define PART_BOOT_ENDFLAGS ((WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP) +#define PART_UPDATE_ENDFLAGS ((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP) + +#ifdef NVM_FLASH_WRITEONCE +#include +extern void *memcpy(void *dst, const void *src, size_t n); +static uint8_t NVM_CACHE[WOLFBOOT_SECTOR_SIZE]; +int RAMFUNCTION hal_trailer_write(uint32_t addr, uint8_t val) { + uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1)); + uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1); + int ret = 0; + memcpy(NVM_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE); + ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE); + if (ret != 0) + return ret; + NVM_CACHE[addr_off] = val; + ret = hal_flash_write(addr_align, NVM_CACHE, WOLFBOOT_SECTOR_SIZE); + return ret; +} +#else +# define hal_trailer_write(addr, val) hal_flash_write(addr, (void *)&val, 1) +#endif + +#if defined PART_UPDATE_EXT +static uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at) { if (part == PART_BOOT) - return (void *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE); - else if (part == PART_UPDATE) - return (void *)(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE); - else - return (void *)0; + return (void *)(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at)); + else if (part == PART_UPDATE) { + ext_flash_read(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&ext_cache, sizeof(uint32_t)); + return (uint8_t *)&ext_cache; + } else + return NULL; } -int wolfBoot_set_partition_state(uint8_t part, uint8_t newst) +static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val) +{ + if (part == PART_BOOT) { + hal_trailer_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), val); + } + else if (part == PART_UPDATE) { + ext_flash_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&val, 1); + } +} + +static void RAMFUNCTION set_partition_magic(uint8_t part) +{ + uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; + if (part == PART_BOOT) { + hal_flash_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t)); + } + else if (part == PART_UPDATE) { + ext_flash_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t)); + } +} + +#else +static uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at) +{ + if (part == PART_BOOT) + return (void *)(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at)); + else if (part == PART_UPDATE) { + return (void *)(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at)); + } else + return NULL; +} + +static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val) +{ + if (part == PART_BOOT) { + hal_trailer_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), val); + } + else if (part == PART_UPDATE) { + hal_trailer_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), val); + } +} + +static void RAMFUNCTION set_partition_magic(uint8_t part) +{ + uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; + if (part == PART_BOOT) { + hal_flash_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t)); + } + else if (part == PART_UPDATE) { + hal_flash_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t), (void *)&wolfboot_magic_trail, sizeof(uint32_t)); + } +} +#endif /* PART_UPDATE_EXT */ + + + +static uint32_t* RAMFUNCTION get_partition_magic(uint8_t part) +{ + return (uint32_t *)get_trailer_at(part, 0); +} + +static uint8_t* RAMFUNCTION get_partition_state(uint8_t part) +{ + return (uint8_t *)get_trailer_at(part, 1); +} + +static uint8_t* RAMFUNCTION get_sector_flags(uint8_t part, uint32_t pos) +{ + return (uint8_t *)get_trailer_at(part, 2 + pos); +} + +static void RAMFUNCTION set_partition_state(uint8_t part, uint8_t val) +{ + set_trailer_at(part, 1, val); +} + +static void RAMFUNCTION set_sector_flags(uint8_t part, uint32_t pos, uint8_t val) +{ + set_trailer_at(part, 2 + pos, val); +} + +int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst) { - uint8_t *trailer_end = get_trailer(part); uint32_t *magic; uint8_t *state; - uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; - if (!trailer_end) - return -1; - magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); + magic = get_partition_magic(part); if (*magic != WOLFBOOT_MAGIC_TRAIL) - hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); - state = (trailer_end - sizeof(uint32_t)) - 1; + set_partition_magic(part); + state = get_partition_state(part); if (*state != newst) - hal_flash_write((uint32_t)state, (void *)&newst, 1); + set_partition_state(part, newst); return 0; } -int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag) +int RAMFUNCTION wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag) { - uint8_t *trailer_end = get_trailer(part); uint32_t *magic; uint8_t *flags; uint8_t fl_value; uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; uint8_t pos = sector >> 1; - if (!trailer_end) - return -1; - magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); + magic = get_partition_magic(part); if (*magic != WOLFBOOT_MAGIC_TRAIL) - hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); - flags = (trailer_end - sizeof(uint32_t)) - (2 + pos); + set_partition_magic(part); + flags = get_sector_flags(part, pos); if (sector == (pos << 1)) fl_value = (*flags & 0xF0) | (newflag & 0x0F); else fl_value = ((newflag & 0x0F) << 4) | (*flags & 0x0F); if (fl_value != *flags) - hal_flash_write((uint32_t)flags, &fl_value, 1); + set_sector_flags(part, pos, fl_value); return 0; } -int wolfBoot_get_partition_state(uint8_t part, uint8_t *st) +int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st) { - uint8_t *trailer_end = get_trailer(part); uint32_t *magic; uint8_t *state; - if (!trailer_end) - return -1; - magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); + magic = get_partition_magic(part); if (*magic != WOLFBOOT_MAGIC_TRAIL) return -1; - state = (trailer_end - sizeof(uint32_t)) - 1; + state = get_partition_state(part); *st = *state; return 0; } int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag) { - uint8_t *trailer_end = get_trailer(part); uint32_t *magic; uint8_t *flags; uint8_t pos = sector >> 1; - if (!trailer_end) - return -1; - magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); + magic = get_partition_magic(part); if (*magic != WOLFBOOT_MAGIC_TRAIL) return -1; - flags = (trailer_end - sizeof(uint32_t)) - (2 + pos); + flags = get_sector_flags(part, pos); if (sector == (pos << 1)) *flag = *flags & 0x0F; else @@ -107,28 +212,123 @@ int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag) return 0; } -void wolfBoot_erase_partition(uint8_t part) +void RAMFUNCTION wolfBoot_erase_partition(uint8_t part) { if (part == PART_BOOT) hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE); - if (part == PART_UPDATE) + if (part == PART_UPDATE) { +#ifdef PART_UPDATE_EXT + ext_flash_unlock(); + ext_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE); + ext_flash_lock(); +#else hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE); +#endif + } if (part == PART_SWAP) hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE); } -void wolfBoot_update_trigger(void) +void RAMFUNCTION wolfBoot_update_trigger(void) { uint8_t st = IMG_STATE_UPDATING; +#ifdef PART_UPDATE_EXT + ext_flash_unlock(); + wolfBoot_set_partition_state(PART_UPDATE, st); + ext_flash_lock(); +#else hal_flash_unlock(); wolfBoot_set_partition_state(PART_UPDATE, st); hal_flash_lock(); +#endif } -void wolfBoot_success(void) +void RAMFUNCTION wolfBoot_success(void) { uint8_t st = IMG_STATE_SUCCESS; hal_flash_unlock(); wolfBoot_set_partition_state(PART_BOOT, st); hal_flash_lock(); } + +uint8_t wolfBoot_find_header(uint8_t *haystack, uint8_t type, uint8_t **ptr) +{ + uint8_t *p = haystack; + while (*p != 0) { + if (*p == HDR_PADDING) { + p++; + continue; + } + if (*p == type) { + p++; + *ptr = (p + 1); + return *p; + } + p++; + p += (*p + 1); + } + *ptr = NULL; + return 0; +} + +#ifdef EXT_FLASH +static uint8_t hdr_cpy[IMAGE_HEADER_SIZE]; +static uint32_t hdr_cpy_done = 0; +#endif + +uint32_t wolfBoot_get_image_version(uint8_t part) +{ + uint32_t *version_field = NULL; + uint32_t version = 0; + uint8_t *image = NULL; + uint32_t *magic = NULL; + if(part == PART_UPDATE) { +#ifdef PART_UPDATE_EXT + ext_flash_read((uint32_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS, hdr_cpy, IMAGE_HEADER_SIZE); + hdr_cpy_done = 1; + image = hdr_cpy; +#else + image = (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS; +#endif + } + if (part == PART_BOOT) + image = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS; + + if (image) { + magic = (uint32_t *)image; + if (*magic != WOLFBOOT_MAGIC) + return 0; + wolfBoot_find_header(image + IMAGE_HEADER_OFFSET, HDR_VERSION, (void *)&version_field); + if (version_field) + return *version_field; + } + return 0; +} + +uint16_t wolfBoot_get_image_type(uint8_t part) +{ + uint16_t *type_field = NULL; + uint8_t *image = NULL; + uint32_t *magic = NULL; + if(part == PART_UPDATE) { +#ifdef PART_UPDATE_EXT + ext_flash_read((uint32_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS, hdr_cpy, IMAGE_HEADER_SIZE); + hdr_cpy_done = 1; + image = hdr_cpy; +#else + image = (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS; +#endif + } + if (part == PART_BOOT) + image = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS; + + if (image) { + magic = (uint32_t *)image; + if (*magic != WOLFBOOT_MAGIC) + return 0; + wolfBoot_find_header(image + IMAGE_HEADER_OFFSET, HDR_IMG_TYPE, (void *)&type_field); + if (type_field) + return *type_field; + } + return 0; +} diff --git a/riotOS-samr21/wolfboot.mk b/riotOS-samr21/wolfboot.mk index 6bba0e2..e8d2f96 100644 --- a/riotOS-samr21/wolfboot.mk +++ b/riotOS-samr21/wolfboot.mk @@ -20,8 +20,7 @@ wolfboot-create-key: $(WOLFBOOT_KEYFILE) $(WOLFBOOT_KEYFILE): make -C $(WOLFBOOT) clean make -C $(WOLFBOOT) distclean - cp $(RIOTBASE)/../target.h $(WOLFBOOT)/include - make -C $(WOLFBOOT) TARGET=samr21 TARGET_ARCH= DEBUG=0 BOOT0_OFFSET=$(WOLFBOOT_OFFSET) ed25519.der + make -C $(WOLFBOOT) TARGET=samr21 DEBUG=0 ed25519.der \ wolfboot: wolfboot-create-key link @$(COLOR_ECHO) @@ -46,8 +45,13 @@ $(WOLFBOOT_BIN): @$(COLOR_ECHO) $(SRC) @$(COLOR_ECHO) make -C $(WOLFBOOT) clean - cp $(RIOTBASE)/../target.h $(WOLFBOOT)/include - make -C $(WOLFBOOT) TARGET=samr21 TARGET_ARCH= DEBUG=0 BOOT0_OFFSET=$(WOLFBOOT_OFFSET) wolfboot.bin + make -C $(WOLFBOOT) TARGET=samr21 TARGET_ARCH= DEBUG=0 BOOT0_OFFSET=$(WOLFBOOT_OFFSET) \ + WOLFBOOT_SECTOR_SIZE=0x100 \ + WOLFBOOT_PARTITION_SIZE=0x1B000 \ + WOLFBOOT_PARTITION_BOOT_ADDRESS=0x08000 \ + WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x23000 \ + WOLFBOOT_PARTITION_SWAP_ADDRESS=0x3E000 \ + wolfboot.bin .PHONY: wolfboot-flash-bootloader wolfboot-flash diff --git a/wolfBoot b/wolfBoot index e6723ec..1c6ea8d 160000 --- a/wolfBoot +++ b/wolfBoot @@ -1 +1 @@ -Subproject commit e6723ec831c1095bdbcf50a8cba636c72364e1c7 +Subproject commit 1c6ea8d7325beba386e3c1007720a8e143539e35