Skip to content

Commit dd4bd8e

Browse files
committed
Added new unit tests for NVM_FLASH_WRITEONCE
1 parent b9dc7ee commit dd4bd8e

File tree

3 files changed

+236
-1
lines changed

3 files changed

+236
-1
lines changed

tools/unit-tests/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ WOLFCRYPT=../../lib/wolfssl/
1717

1818

1919
TESTS:=unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20 unit-pci \
20-
unit-mock-state unit-sectorflags unit-image
20+
unit-mock-state unit-sectorflags unit-image unit-nvm
2121

2222
all: $(TESTS)
2323

@@ -39,6 +39,7 @@ unit-aes128:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES128
3939
unit-aes256:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES256
4040
unit-chacha20:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_CHACHA
4141
unit-parser:CFLAGS+=-DNVM_FLASH_WRITEONCE
42+
unit-nvm:CFLAGS+=-DNVM_FLASH_WRITEONCE -DMOCK_PARTITIONS
4243

4344

4445
WOLFCRYPT_SRC:=$(WOLFCRYPT)/wolfcrypt/src/sha.c \
@@ -86,6 +87,9 @@ unit-sectorflags: ../../include/target.h unit-sectorflags.c
8687
unit-image: unit-image.c unit-common.c $(WOLFCRYPT_SRC)
8788
gcc -o $@ $^ $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)
8889

90+
unit-nvm: ../../include/target.h unit-nvm.c
91+
gcc -o $@ unit-nvm.c $(CFLAGS) $(LDFLAGS)
92+
8993
%.o:%.c
9094
gcc -c -o $@ $^ $(CFLAGS)
9195

tools/unit-tests/target.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434

3535
#define WOLFBOOT_SECTOR_SIZE 0x400
3636

37+
#ifdef MOCK_PARTITIONS
38+
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0xCD000000
39+
#define WOLFBOOT_PARTITION_SIZE 0x8000
40+
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0xCC000000
41+
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0xCE000000
42+
#else
43+
3744
#ifdef WOLFBOOT_FIXED_PARTITIONS
3845

3946
#ifdef PULL_LINKER_DEFINES
@@ -66,6 +73,7 @@
6673
/* Load address in RAM for staged OS (update_ram only) */
6774
#define WOLFBOOT_LOAD_ADDRESS 0x200000
6875
#define WOLFBOOT_LOAD_DTS_ADDRESS 0x400000
76+
#endif /* MOCK_PARTITIONS */
6977

7078

7179
#endif /* !H_TARGETS_TARGET_ */

tools/unit-tests/unit-nvm.c

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE) {
48+
erased_nvm_bank0++;
49+
} else if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 2 * WOLFBOOT_SECTOR_SIZE) {
50+
erased_nvm_bank1++;
51+
}
52+
} else {
53+
fail("Invalid address\n");
54+
return -1;
55+
}
56+
return 0;
57+
}
58+
void hal_flash_unlock(void)
59+
{
60+
fail_unless(locked, "Double unlock detected\n");
61+
locked--;
62+
}
63+
void hal_flash_lock(void)
64+
{
65+
fail_if(locked, "Double lock detected\n");
66+
locked++;
67+
}
68+
69+
void hal_prepare_boot(void)
70+
{
71+
}
72+
73+
/* A simple mock memory */
74+
static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
75+
{
76+
struct stat st = { 0 };
77+
uint8_t *mmaped_addr;
78+
int ret;
79+
int fd;
80+
int i;
81+
82+
if (path == NULL)
83+
return -1;
84+
85+
fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0666);
86+
if (fd == -1) {
87+
fprintf(stderr, "can't open %s\n", path);
88+
return -1;
89+
}
90+
fprintf(stderr, "Open file: %s success.\n", path);
91+
for (i = 0; i < WOLFBOOT_PARTITION_SIZE; i+=4) {
92+
const uint32_t erased_word = 0xFFFFFFFF;
93+
write(fd, &erased_word, 4);
94+
}
95+
lseek(fd, SEEK_SET, 0);
96+
97+
mmaped_addr = mmap(address, WOLFBOOT_PARTITION_SIZE, PROT_READ | PROT_WRITE,
98+
MAP_SHARED, fd, 0);
99+
if (mmaped_addr == MAP_FAILED) {
100+
fprintf(stderr, "MMAP failed.\n");
101+
return -1;
102+
}
103+
104+
fprintf(stderr, "Simulator assigned %s to base %p\n", path, mmaped_addr);
105+
106+
if (ret_address)
107+
*ret_address = mmaped_addr;
108+
109+
close(fd);
110+
return 0;
111+
}
112+
113+
114+
/* End Mocks */
115+
116+
Suite *wolfboot_suite(void);
117+
118+
119+
START_TEST (test_nvm_select_fresh_sector)
120+
{
121+
int ret;
122+
const char BOOT[] = "BOOT";
123+
uint8_t st;
124+
ret = mmap_file("/tmp/wolfboot-unit-file.bin", MOCK_ADDRESS, NULL);
125+
126+
/* Erased flag sectors: select '0' by default */
127+
ret = nvm_select_fresh_sector(PART_UPDATE);
128+
fail_if(ret != 0, "Failed to select default fresh sector\n");
129+
130+
/* Force a good 'magic' at the end of sector 1 */
131+
hal_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE -
132+
(WOLFBOOT_SECTOR_SIZE + 4), BOOT, 4);
133+
134+
/* Current selected should now be 1 */
135+
ret = nvm_select_fresh_sector(PART_UPDATE);
136+
fail_if(ret != 1, "Failed to select good fresh sector\n");
137+
138+
erased_nvm_bank1 = 0;
139+
erased_nvm_bank0 = 0;
140+
141+
/* Calling 'set_partition_state' should change the current sector */
142+
wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_UPDATING);
143+
144+
/* Current selected should now be 0 */
145+
ret = nvm_select_fresh_sector(PART_UPDATE);
146+
fail_if(ret != 0, "Failed to select updating fresh sector\n");
147+
fail_if(erased_nvm_bank1 == 0, "Did not erase the non-selected bank");
148+
149+
erased_nvm_bank1 = 0;
150+
erased_nvm_bank0 = 0;
151+
152+
/* Check state is read back correctly */
153+
ret = wolfBoot_get_partition_state(PART_UPDATE, &st);
154+
fail_if(ret != 0, "Failed to read back state\n");
155+
fail_if(st != IMG_STATE_UPDATING, "Bootloader in the wrong state\n");
156+
157+
/* Check that reading did not change the current sector */
158+
ret = nvm_select_fresh_sector(PART_UPDATE);
159+
fail_if(ret != 0, "Failed to select right sector after reading\n");
160+
161+
/* Update one sector flag, it should change nvm sector */
162+
wolfBoot_set_update_sector_flag(0, SECT_FLAG_SWAPPING);
163+
164+
/* Current selected should now be 1 */
165+
ret = nvm_select_fresh_sector(PART_UPDATE);
166+
fail_if(ret != 1, "Failed to select updating fresh sector\n");
167+
fail_if(erased_nvm_bank0 == 0, "Did not erase the non-selected bank");
168+
169+
/* Check sector state is read back correctly */
170+
ret = wolfBoot_get_update_sector_flag(0, &st);
171+
fail_if (ret != 0, "Failed to read sector flag state\n");
172+
fail_if (st != SECT_FLAG_SWAPPING, "Wrong sector flag state\n");
173+
174+
/* Check that reading did not change the current sector (1) */
175+
ret = nvm_select_fresh_sector(PART_UPDATE);
176+
fail_if(ret != 1, "Failed to select right sector after reading sector state\n");
177+
178+
/* Update sector flag, again. it should change nvm sector */
179+
erased_nvm_bank1 = 0;
180+
erased_nvm_bank0 = 0;
181+
wolfBoot_set_update_sector_flag(0, SECT_FLAG_UPDATED);
182+
183+
/* Current selected should now be 0 */
184+
ret = nvm_select_fresh_sector(PART_UPDATE);
185+
fail_if(ret != 0, "Failed to select updating fresh sector\n");
186+
fail_if(erased_nvm_bank1 == 0, "Did not erase the non-selected bank");
187+
188+
/* Check sector state is read back correctly */
189+
ret = wolfBoot_get_update_sector_flag(0, &st);
190+
fail_if (ret != 0, "Failed to read sector flag state\n");
191+
fail_if (st != SECT_FLAG_UPDATED, "Wrong sector flag state\n");
192+
193+
194+
195+
}
196+
END_TEST
197+
198+
199+
Suite *wolfboot_suite(void)
200+
{
201+
202+
/* Suite initialization */
203+
Suite *s = suite_create("wolfBoot-NVM-workarounds");
204+
205+
/* Test cases */
206+
TCase *nvm_select_fresh_sector = tcase_create("NVM select fresh sector");
207+
tcase_add_test(nvm_select_fresh_sector, test_nvm_select_fresh_sector);
208+
suite_add_tcase(s, nvm_select_fresh_sector);
209+
210+
return s;
211+
}
212+
213+
214+
int main(void)
215+
{
216+
int fails;
217+
Suite *s = wolfboot_suite();
218+
SRunner *sr = srunner_create(s);
219+
srunner_run_all(sr, CK_NORMAL);
220+
fails = srunner_ntests_failed(sr);
221+
srunner_free(sr);
222+
return fails;
223+
}

0 commit comments

Comments
 (0)