Skip to content

Commit 80fa66f

Browse files
committed
Added more tests
1 parent 230174e commit 80fa66f

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

tools/unit-tests/unit-nvm.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ int hal_flash_erase(haladdr_t address, int len)
4444
} else if ((address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) &&
4545
(address < WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE)) {
4646
erased_update++;
47+
memset(address, 0xFF, len);
4748
if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE) {
4849
erased_nvm_bank0++;
4950
} else if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 2 * WOLFBOOT_SECTOR_SIZE) {
@@ -89,10 +90,10 @@ static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
8990
}
9091
fprintf(stderr, "Open file: %s success.\n", path);
9192
for (i = 0; i < WOLFBOOT_PARTITION_SIZE; i+=4) {
92-
const uint32_t erased_word = 0xFFFFFFFF;
93-
write(fd, &erased_word, 4);
93+
const uint32_t erased_word = 0xBADBADBA;
94+
write(fd, &erased_word, 4);
9495
}
95-
lseek(fd, SEEK_SET, 0);
96+
lseek(fd, SEEK_SET, 0);
9697

9798
mmaped_addr = mmap(address, WOLFBOOT_PARTITION_SIZE, PROT_READ | PROT_WRITE,
9899
MAP_SHARED, fd, 0);
@@ -120,16 +121,23 @@ START_TEST (test_nvm_select_fresh_sector)
120121
{
121122
int ret;
122123
const char BOOT[] = "BOOT";
124+
const uint32_t *boot_word = (const uint32_t *)BOOT;
123125
uint8_t st;
126+
uint32_t *magic;
124127
ret = mmap_file("/tmp/wolfboot-unit-file.bin", MOCK_ADDRESS, NULL);
125128

129+
erased_update = 0;
130+
wolfBoot_erase_partition(PART_UPDATE);
131+
fail_if(erased_update != 1);
126132
/* Erased flag sectors: select '0' by default */
127133
ret = nvm_select_fresh_sector(PART_UPDATE);
128134
fail_if(ret != 0, "Failed to select default fresh sector\n");
129135

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);
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");
133141

134142
/* Current selected should now be 1 */
135143
ret = nvm_select_fresh_sector(PART_UPDATE);
@@ -145,7 +153,7 @@ START_TEST (test_nvm_select_fresh_sector)
145153
ret = nvm_select_fresh_sector(PART_UPDATE);
146154
fail_if(ret != 0, "Failed to select updating fresh sector\n");
147155
fail_if(erased_nvm_bank1 == 0, "Did not erase the non-selected bank");
148-
156+
149157
erased_nvm_bank1 = 0;
150158
erased_nvm_bank0 = 0;
151159

@@ -160,7 +168,7 @@ START_TEST (test_nvm_select_fresh_sector)
160168

161169
/* Update one sector flag, it should change nvm sector */
162170
wolfBoot_set_update_sector_flag(0, SECT_FLAG_SWAPPING);
163-
171+
164172
/* Current selected should now be 1 */
165173
ret = nvm_select_fresh_sector(PART_UPDATE);
166174
fail_if(ret != 1, "Failed to select updating fresh sector\n");
@@ -170,16 +178,16 @@ START_TEST (test_nvm_select_fresh_sector)
170178
ret = wolfBoot_get_update_sector_flag(0, &st);
171179
fail_if (ret != 0, "Failed to read sector flag state\n");
172180
fail_if (st != SECT_FLAG_SWAPPING, "Wrong sector flag state\n");
173-
181+
174182
/* Check that reading did not change the current sector (1) */
175183
ret = nvm_select_fresh_sector(PART_UPDATE);
176184
fail_if(ret != 1, "Failed to select right sector after reading sector state\n");
177-
185+
178186
/* Update sector flag, again. it should change nvm sector */
179187
erased_nvm_bank1 = 0;
180188
erased_nvm_bank0 = 0;
181189
wolfBoot_set_update_sector_flag(0, SECT_FLAG_UPDATED);
182-
190+
183191
/* Current selected should now be 0 */
184192
ret = nvm_select_fresh_sector(PART_UPDATE);
185193
fail_if(ret != 0, "Failed to select updating fresh sector\n");
@@ -190,7 +198,9 @@ START_TEST (test_nvm_select_fresh_sector)
190198
fail_if (ret != 0, "Failed to read sector flag state\n");
191199
fail_if (st != SECT_FLAG_UPDATED, "Wrong sector flag state\n");
192200

193-
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");
194204

195205
}
196206
END_TEST

0 commit comments

Comments
 (0)