Skip to content

Commit 35db4cf

Browse files
authored
Merge pull request #469 from danielinux/nvm-unit-tests
New unit tests for NVM_FLASH_WRITEONCE, fix bug in offset calculation
2 parents b9dc7ee + 8f0c090 commit 35db4cf

File tree

5 files changed

+345
-11
lines changed

5 files changed

+345
-11
lines changed

src/libwolfboot.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,10 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
176176
#include <string.h>
177177
static uint8_t NVM_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(16)));
178178
static int nvm_cached_sector = 0;
179-
180-
#ifdef __GNUC__
181-
#pragma GCC diagnostic push
182-
#pragma GCC diagnostic ignored "-Warray-bounds"
183-
#endif
184179
static uint8_t get_base_offset(uint8_t *base, uintptr_t off)
185180
{
186181
return *(base - off); /* ignore array bounds error */
187182
}
188-
#ifdef __GNUC__
189-
#pragma GCC diagnostic pop
190-
#endif
191183

192184
void WEAKFUNCTION hal_cache_invalidate(void)
193185
{
@@ -308,8 +300,8 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
308300
break;
309301
}
310302
/* Examine previous position one byte ahead */
311-
byte_0 = get_base_offset(base, (1 - off));
312-
byte_1 = get_base_offset(base, (1 - (WOLFBOOT_SECTOR_SIZE + off)));
303+
byte_0 = get_base_offset(base, (off - 1));
304+
byte_1 = get_base_offset(base, ((WOLFBOOT_SECTOR_SIZE + off) - 1));
313305

314306
sel = FLAG_CMP(byte_0, byte_1);
315307
break;

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: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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

Comments
 (0)