|
| 1 | +/* libwolfboot.c |
| 2 | + * |
| 3 | + * Copyright (C) 2018 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfBoot. |
| 6 | + * |
| 7 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfBoot is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | +#include <hal.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <inttypes.h> |
| 24 | +#include <wolfboot/wolfboot.h> |
| 25 | + |
| 26 | +static uint8_t *get_trailer(uint8_t part) |
| 27 | +{ |
| 28 | + if (part == PART_BOOT) |
| 29 | + return (void *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE); |
| 30 | + else if (part == PART_UPDATE) |
| 31 | + return (void *)(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE); |
| 32 | + else |
| 33 | + return (void *)0; |
| 34 | +} |
| 35 | + |
| 36 | +int wolfBoot_set_partition_state(uint8_t part, uint8_t newst) |
| 37 | +{ |
| 38 | + uint8_t *trailer_end = get_trailer(part); |
| 39 | + uint32_t *magic; |
| 40 | + uint8_t *state; |
| 41 | + uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; |
| 42 | + if (!trailer_end) |
| 43 | + return -1; |
| 44 | + magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); |
| 45 | + if (*magic != WOLFBOOT_MAGIC_TRAIL) |
| 46 | + hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); |
| 47 | + state = (trailer_end - sizeof(uint32_t)) - 1; |
| 48 | + if (*state != newst) |
| 49 | + hal_flash_write((uint32_t)state, (void *)&newst, 1); |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag) |
| 54 | +{ |
| 55 | + uint8_t *trailer_end = get_trailer(part); |
| 56 | + uint32_t *magic; |
| 57 | + uint8_t *flags; |
| 58 | + uint8_t fl_value; |
| 59 | + uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; |
| 60 | + uint8_t pos = sector >> 1; |
| 61 | + if (!trailer_end) |
| 62 | + return -1; |
| 63 | + magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); |
| 64 | + if (*magic != WOLFBOOT_MAGIC_TRAIL) |
| 65 | + hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t)); |
| 66 | + flags = (trailer_end - sizeof(uint32_t)) - pos; |
| 67 | + if (sector == (pos << 1)) |
| 68 | + fl_value = (*flags & 0xF0) | (newflag & 0x0F); |
| 69 | + else |
| 70 | + fl_value = ((newflag & 0x0F) << 4) | (*flags & 0x0F); |
| 71 | + if (fl_value != *flags) |
| 72 | + hal_flash_write((uint32_t)flags, &fl_value, 1); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +int wolfBoot_get_partition_state(uint8_t part, uint8_t *st) |
| 77 | +{ |
| 78 | + uint8_t *trailer_end = get_trailer(part); |
| 79 | + uint32_t *magic; |
| 80 | + uint8_t *state; |
| 81 | + if (trailer_end) |
| 82 | + return -1; |
| 83 | + magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); |
| 84 | + if (*magic != WOLFBOOT_MAGIC_TRAIL) |
| 85 | + return -1; |
| 86 | + state = (trailer_end - sizeof(uint32_t)) - 1; |
| 87 | + *st = *state; |
| 88 | + return 0; |
| 89 | +} |
| 90 | + |
| 91 | +int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag) |
| 92 | +{ |
| 93 | + uint8_t *trailer_end = get_trailer(part); |
| 94 | + uint32_t *magic; |
| 95 | + uint8_t *flags; |
| 96 | + uint8_t fl_value; |
| 97 | + uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL; |
| 98 | + uint8_t pos = sector >> 1; |
| 99 | + if (!trailer_end) |
| 100 | + return -1; |
| 101 | + magic = (uint32_t *)(trailer_end - sizeof(uint32_t)); |
| 102 | + if (*magic != WOLFBOOT_MAGIC_TRAIL) |
| 103 | + return -1; |
| 104 | + flags = (trailer_end - sizeof(uint32_t)) - pos; |
| 105 | + if (sector == (pos << 1)) |
| 106 | + *flag = *flags & 0x0F; |
| 107 | + else |
| 108 | + *flag = (*flags & 0xF0) >> 4; |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +void wolfBoot_erase_partition(uint8_t part) |
| 113 | +{ |
| 114 | + if (part == PART_BOOT) |
| 115 | + hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE); |
| 116 | + if (part == PART_UPDATE) |
| 117 | + hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE); |
| 118 | + if (part == PART_SWAP) |
| 119 | + hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE); |
| 120 | +} |
| 121 | + |
| 122 | +void wolfBoot_update_trigger(void) |
| 123 | +{ |
| 124 | + uint8_t st = IMG_STATE_UPDATING; |
| 125 | + hal_flash_unlock(); |
| 126 | + wolfBoot_set_partition_state(PART_UPDATE, st); |
| 127 | + hal_flash_lock(); |
| 128 | +} |
| 129 | + |
| 130 | +void wolfBoot_success(void) |
| 131 | +{ |
| 132 | + uint8_t st = IMG_STATE_SUCCESS; |
| 133 | + hal_flash_unlock(); |
| 134 | + wolfBoot_set_partition_state(PART_BOOT, st); |
| 135 | + hal_flash_lock(); |
| 136 | +} |
0 commit comments