|
| 1 | +#define WOLFBOOT_HASH_SHA256 |
| 2 | +#define IMAGE_HEADER_SIZE 256 |
| 3 | +#define UNIT_TEST |
| 4 | +#define WC_NO_HARDEN |
| 5 | +#define MOCK_ADDRESS 0xCC000000 |
| 6 | +#include <stdio.h> |
| 7 | +#include "libwolfboot.c" |
| 8 | +#include <fcntl.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include <sys/mman.h> |
| 11 | +#include <check.h> |
| 12 | + |
| 13 | +static int locked = 0; |
| 14 | +static int erased_boot = 0; |
| 15 | +static int erased_update = 0; |
| 16 | +static int erased_nvm_bank0 = 0; |
| 17 | +static int erased_nvm_bank1 = 0; |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +/* Mocks */ |
| 24 | +void hal_init(void) |
| 25 | +{ |
| 26 | +} |
| 27 | +int hal_flash_write(haladdr_t address, const uint8_t *data, int len) |
| 28 | +{ |
| 29 | + int i; |
| 30 | + uint8_t *a = (uint8_t *)address; |
| 31 | + if ((address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) && |
| 32 | + (address < WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE)) { |
| 33 | + for (i = 0; i < len; i++) { |
| 34 | + a[i] = data[i]; |
| 35 | + } |
| 36 | + } |
| 37 | + return 0; |
| 38 | +} |
| 39 | +int hal_flash_erase(haladdr_t address, int len) |
| 40 | +{ |
| 41 | + if ((address >= WOLFBOOT_PARTITION_BOOT_ADDRESS) && |
| 42 | + (address < WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE)) { |
| 43 | + erased_boot++; |
| 44 | + } else if ((address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) && |
| 45 | + (address < WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE)) { |
| 46 | + erased_update++; |
| 47 | + memset(address, 0xFF, len); |
| 48 | + if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE) { |
| 49 | + erased_nvm_bank0++; |
| 50 | + } else if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 2 * WOLFBOOT_SECTOR_SIZE) { |
| 51 | + erased_nvm_bank1++; |
| 52 | + } |
| 53 | + } else { |
| 54 | + fail("Invalid address\n"); |
| 55 | + return -1; |
| 56 | + } |
| 57 | + return 0; |
| 58 | +} |
| 59 | +void hal_flash_unlock(void) |
| 60 | +{ |
| 61 | + fail_unless(locked, "Double unlock detected\n"); |
| 62 | + locked--; |
| 63 | +} |
| 64 | +void hal_flash_lock(void) |
| 65 | +{ |
| 66 | + fail_if(locked, "Double lock detected\n"); |
| 67 | + locked++; |
| 68 | +} |
| 69 | + |
| 70 | +void hal_prepare_boot(void) |
| 71 | +{ |
| 72 | +} |
| 73 | + |
| 74 | +/* A simple mock memory */ |
| 75 | +static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address) |
| 76 | +{ |
| 77 | + struct stat st = { 0 }; |
| 78 | + uint8_t *mmaped_addr; |
| 79 | + int ret; |
| 80 | + int fd; |
| 81 | + int i; |
| 82 | + |
| 83 | + if (path == NULL) |
| 84 | + return -1; |
| 85 | + |
| 86 | + fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 87 | + if (fd == -1) { |
| 88 | + fprintf(stderr, "can't open %s\n", path); |
| 89 | + return -1; |
| 90 | + } |
| 91 | + fprintf(stderr, "Open file: %s success.\n", path); |
| 92 | + for (i = 0; i < WOLFBOOT_PARTITION_SIZE; i+=4) { |
| 93 | + const uint32_t erased_word = 0xBADBADBA; |
| 94 | + write(fd, &erased_word, 4); |
| 95 | + } |
| 96 | + lseek(fd, SEEK_SET, 0); |
| 97 | + |
| 98 | + mmaped_addr = mmap(address, WOLFBOOT_PARTITION_SIZE, PROT_READ | PROT_WRITE, |
| 99 | + MAP_SHARED, fd, 0); |
| 100 | + if (mmaped_addr == MAP_FAILED) { |
| 101 | + fprintf(stderr, "MMAP failed.\n"); |
| 102 | + return -1; |
| 103 | + } |
| 104 | + |
| 105 | + fprintf(stderr, "Simulator assigned %s to base %p\n", path, mmaped_addr); |
| 106 | + |
| 107 | + if (ret_address) |
| 108 | + *ret_address = mmaped_addr; |
| 109 | + |
| 110 | + close(fd); |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/* End Mocks */ |
| 116 | + |
| 117 | +Suite *wolfboot_suite(void); |
| 118 | + |
| 119 | + |
| 120 | +START_TEST (test_nvm_select_fresh_sector) |
| 121 | +{ |
| 122 | + int ret; |
| 123 | + const char BOOT[] = "BOOT"; |
| 124 | + const uint32_t *boot_word = (const uint32_t *)BOOT; |
| 125 | + uint8_t st; |
| 126 | + uint32_t *magic; |
| 127 | + ret = mmap_file("/tmp/wolfboot-unit-file.bin", MOCK_ADDRESS, NULL); |
| 128 | + |
| 129 | + erased_update = 0; |
| 130 | + wolfBoot_erase_partition(PART_UPDATE); |
| 131 | + fail_if(erased_update != 1); |
| 132 | + /* Erased flag sectors: select '0' by default */ |
| 133 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 134 | + fail_if(ret != 0, "Failed to select default fresh sector\n"); |
| 135 | + |
| 136 | + /* Force a good 'magic' at the end of sector 1 by setting the magic word */ |
| 137 | + wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_NEW); |
| 138 | + magic = get_partition_magic(PART_UPDATE); |
| 139 | + fail_if(*magic != *boot_word, |
| 140 | + "Failed to read back 'BOOT' trailer at the end of the partition"); |
| 141 | + |
| 142 | + /* Current selected should now be 1 */ |
| 143 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 144 | + fail_if(ret != 1, "Failed to select good fresh sector\n"); |
| 145 | + |
| 146 | + erased_nvm_bank1 = 0; |
| 147 | + erased_nvm_bank0 = 0; |
| 148 | + |
| 149 | + /* Calling 'set_partition_state' should change the current sector */ |
| 150 | + wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_UPDATING); |
| 151 | + |
| 152 | + /* Current selected should now be 0 */ |
| 153 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 154 | + fail_if(ret != 0, "Failed to select updating fresh sector\n"); |
| 155 | + fail_if(erased_nvm_bank1 == 0, "Did not erase the non-selected bank"); |
| 156 | + |
| 157 | + erased_nvm_bank1 = 0; |
| 158 | + erased_nvm_bank0 = 0; |
| 159 | + |
| 160 | + /* Check state is read back correctly */ |
| 161 | + ret = wolfBoot_get_partition_state(PART_UPDATE, &st); |
| 162 | + fail_if(ret != 0, "Failed to read back state\n"); |
| 163 | + fail_if(st != IMG_STATE_UPDATING, "Bootloader in the wrong state\n"); |
| 164 | + |
| 165 | + /* Check that reading did not change the current sector */ |
| 166 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 167 | + fail_if(ret != 0, "Failed to select right sector after reading\n"); |
| 168 | + |
| 169 | + /* Update one sector flag, it should change nvm sector */ |
| 170 | + wolfBoot_set_update_sector_flag(0, SECT_FLAG_SWAPPING); |
| 171 | + |
| 172 | + /* Current selected should now be 1 */ |
| 173 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 174 | + fail_if(ret != 1, "Failed to select updating fresh sector\n"); |
| 175 | + fail_if(erased_nvm_bank0 == 0, "Did not erase the non-selected bank"); |
| 176 | + |
| 177 | + /* Check sector state is read back correctly */ |
| 178 | + ret = wolfBoot_get_update_sector_flag(0, &st); |
| 179 | + fail_if (ret != 0, "Failed to read sector flag state\n"); |
| 180 | + fail_if (st != SECT_FLAG_SWAPPING, "Wrong sector flag state\n"); |
| 181 | + |
| 182 | + /* Check that reading did not change the current sector (1) */ |
| 183 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 184 | + fail_if(ret != 1, "Failed to select right sector after reading sector state\n"); |
| 185 | + |
| 186 | + /* Update sector flag, again. it should change nvm sector */ |
| 187 | + erased_nvm_bank1 = 0; |
| 188 | + erased_nvm_bank0 = 0; |
| 189 | + wolfBoot_set_update_sector_flag(0, SECT_FLAG_UPDATED); |
| 190 | + |
| 191 | + /* Current selected should now be 0 */ |
| 192 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 193 | + fail_if(ret != 0, "Failed to select updating fresh sector\n"); |
| 194 | + fail_if(erased_nvm_bank1 == 0, "Did not erase the non-selected bank"); |
| 195 | + |
| 196 | + /* Check sector state is read back correctly */ |
| 197 | + ret = wolfBoot_get_update_sector_flag(0, &st); |
| 198 | + fail_if (ret != 0, "Failed to read sector flag state\n"); |
| 199 | + fail_if (st != SECT_FLAG_UPDATED, "Wrong sector flag state\n"); |
| 200 | + |
| 201 | + /* Check that reading did not change the current sector (0) */ |
| 202 | + ret = nvm_select_fresh_sector(PART_UPDATE); |
| 203 | + fail_if(ret != 0, "Failed to select right sector after reading sector state\n"); |
| 204 | + |
| 205 | +} |
| 206 | +END_TEST |
| 207 | + |
| 208 | + |
| 209 | +Suite *wolfboot_suite(void) |
| 210 | +{ |
| 211 | + |
| 212 | + /* Suite initialization */ |
| 213 | + Suite *s = suite_create("wolfBoot-NVM-workarounds"); |
| 214 | + |
| 215 | + /* Test cases */ |
| 216 | + TCase *nvm_select_fresh_sector = tcase_create("NVM select fresh sector"); |
| 217 | + tcase_add_test(nvm_select_fresh_sector, test_nvm_select_fresh_sector); |
| 218 | + suite_add_tcase(s, nvm_select_fresh_sector); |
| 219 | + |
| 220 | + return s; |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +int main(void) |
| 225 | +{ |
| 226 | + int fails; |
| 227 | + Suite *s = wolfboot_suite(); |
| 228 | + SRunner *sr = srunner_create(s); |
| 229 | + srunner_run_all(sr, CK_NORMAL); |
| 230 | + fails = srunner_ntests_failed(sr); |
| 231 | + srunner_free(sr); |
| 232 | + return fails; |
| 233 | +} |
0 commit comments