From 61bbfef35ce0f874654f48e01e8abe688659f4c3 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 22 Nov 2018 16:22:30 +0100 Subject: [PATCH] Separated library functions for in-app use --- Makefile | 1 + include/image.h | 4 -- include/wolfboot/wolfboot.h | 5 ++ src/image.c | 113 ------------------------------ src/libwolfboot.c | 136 ++++++++++++++++++++++++++++++++++++ src/wolfboot.c | 1 + 6 files changed, 143 insertions(+), 117 deletions(-) create mode 100644 src/libwolfboot.c diff --git a/Makefile b/Makefile index 45d4c104..7d8e447d 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ OBJS:= \ ./src/crypto.o \ ./src/wolfboot.o \ ./src/image.o \ +./src/libwolfboot.o \ ./lib/wolfssl/wolfcrypt/src/sha256.o \ ./lib/wolfssl/wolfcrypt/src/hash.o \ ./lib/wolfssl/wolfcrypt/src/wolfmath.o \ diff --git a/include/image.h b/include/image.h index 5c9f7776..941170e0 100644 --- a/include/image.h +++ b/include/image.h @@ -4,10 +4,6 @@ #include #include -#define IMG_STATE_NEW 0xFF -#define IMG_STATE_UPDATING 0x70 -#define IMG_STATE_TESTING 0x10 -#define IMG_STATE_SUCCESS 0x00 #define SECT_FLAG_NEW 0x0F #define SECT_FLAG_SWAPPING 0x07 diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index 76c67d53..2fcbbee9 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -21,6 +21,11 @@ #define PART_UPDATE 1 #define PART_SWAP 2 +#define IMG_STATE_NEW 0xFF +#define IMG_STATE_UPDATING 0x70 +#define IMG_STATE_TESTING 0x10 +#define IMG_STATE_SUCCESS 0x00 + void wolfBoot_erase_partition(uint8_t part); void wolfBoot_update_trigger(void); void wolfBoot_success(void); diff --git a/src/image.c b/src/image.c index a50c553d..eedaee64 100644 --- a/src/image.c +++ b/src/image.c @@ -192,91 +192,6 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img) return 0; } -static uint8_t *get_trailer(uint8_t part) -{ - 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 NULL; -} - -int 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)); - 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; - if (*state != newst) - hal_flash_write((uint32_t)state, (void *)&newst, 1); - return 0; -} - -int 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)); - if (*magic != WOLFBOOT_MAGIC_TRAIL) - hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); - flags = (trailer_end - sizeof(uint32_t)) - 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); - return 0; -} - -int 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)); - if (*magic != WOLFBOOT_MAGIC_TRAIL) - return -1; - state = (trailer_end - sizeof(uint32_t)) - 1; - *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 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)); - if (*magic != WOLFBOOT_MAGIC_TRAIL) - return -1; - flags = (trailer_end - sizeof(uint32_t)) - pos; - if (sector == (pos << 1)) - *flag = *flags & 0x0F; - else - *flag = (*flags & 0xF0) >> 4; - return 0; -} int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size) { @@ -293,31 +208,3 @@ int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size) } return pos; } - -void wolfBoot_erase_partition(uint8_t part) -{ - if (part == PART_BOOT) - hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE); - if (part == PART_UPDATE) - hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE); -#ifdef WOLFBOOT_PARTITION_SWAP_ADDRESS - if (part == PART_SWAP) - hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE); -#endif -} - -void wolfBoot_update_trigger(void) -{ - uint8_t st = IMG_STATE_UPDATING; - hal_flash_unlock(); - wolfBoot_set_partition_state(PART_UPDATE, st); - hal_flash_lock(); -} - -void wolfBoot_success(void) -{ - uint8_t st = IMG_STATE_SUCCESS; - hal_flash_unlock(); - wolfBoot_set_partition_state(PART_BOOT, st); - hal_flash_lock(); -} diff --git a/src/libwolfboot.c b/src/libwolfboot.c new file mode 100644 index 00000000..e72c2f66 --- /dev/null +++ b/src/libwolfboot.c @@ -0,0 +1,136 @@ +/* libwolfboot.c + * + * Copyright (C) 2018 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * 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) +{ + 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; +} + +int 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)); + 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; + if (*state != newst) + hal_flash_write((uint32_t)state, (void *)&newst, 1); + return 0; +} + +int 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)); + if (*magic != WOLFBOOT_MAGIC_TRAIL) + hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); + flags = (trailer_end - sizeof(uint32_t)) - 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); + return 0; +} + +int 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)); + if (*magic != WOLFBOOT_MAGIC_TRAIL) + return -1; + state = (trailer_end - sizeof(uint32_t)) - 1; + *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 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)); + if (*magic != WOLFBOOT_MAGIC_TRAIL) + return -1; + flags = (trailer_end - sizeof(uint32_t)) - pos; + if (sector == (pos << 1)) + *flag = *flags & 0x0F; + else + *flag = (*flags & 0xF0) >> 4; + return 0; +} + +void wolfBoot_erase_partition(uint8_t part) +{ + if (part == PART_BOOT) + hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE); + if (part == PART_UPDATE) + hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE); + if (part == PART_SWAP) + hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE); +} + +void wolfBoot_update_trigger(void) +{ + uint8_t st = IMG_STATE_UPDATING; + hal_flash_unlock(); + wolfBoot_set_partition_state(PART_UPDATE, st); + hal_flash_lock(); +} + +void wolfBoot_success(void) +{ + uint8_t st = IMG_STATE_SUCCESS; + hal_flash_unlock(); + wolfBoot_set_partition_state(PART_BOOT, st); + hal_flash_lock(); +} diff --git a/src/wolfboot.c b/src/wolfboot.c index 74d25da1..33deb708 100644 --- a/src/wolfboot.c +++ b/src/wolfboot.c @@ -20,6 +20,7 @@ */ #include #include +#include extern unsigned int _stored_data; extern unsigned int _start_data; extern unsigned int _end_data;