Skip to content

Commit 4c0f425

Browse files
committed
Fix WOLFBOOT_MAX_SPACE precedence and test
F/2587
1 parent af24164 commit 4c0f425

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

include/image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ static inline int wb_flash_write_verify_word(struct wolfBoot_image *img,
13951395
#ifndef EXT_ENCRYPTED
13961396
#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - \
13971397
(TRAILER_SKIP + sizeof(uint32_t) + \
1398-
(WOLFBOOT_PARTITION_SIZE + 1 / (WOLFBOOT_SECTOR_SIZE * 8))))
1398+
((WOLFBOOT_PARTITION_SIZE + 1) / (WOLFBOOT_SECTOR_SIZE * 8))))
13991399
#else
14001400
#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - ENCRYPT_TMP_SECRET_OFFSET)
14011401
#endif

tools/unit-tests/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ endif
4545

4646
TESTS:=unit-parser unit-fdt unit-extflash unit-string unit-spi-flash unit-aes128 \
4747
unit-aes256 unit-chacha20 unit-pci unit-mock-state unit-sectorflags \
48+
unit-max-space \
4849
unit-image unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \
4950
unit-enc-nvm-flagshome unit-delta unit-update-flash unit-update-flash-delta \
5051
unit-update-flash-self-update \
@@ -205,6 +206,9 @@ unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c
205206
unit-string: ../../include/target.h unit-string.c
206207
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
207208

209+
unit-max-space: ../../include/target.h unit-max-space.c
210+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
211+
208212
unit-update-flash-self-update: ../../include/target.h unit-update-flash.c
209213
gcc -o $@ unit-update-flash.c ../../src/image.c \
210214
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.c \

tools/unit-tests/unit-max-space.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* unit-max-space.c
2+
*
3+
* Unit test for WOLFBOOT_MAX_SPACE macro arithmetic in image.h
4+
*/
5+
6+
#include <check.h>
7+
#include <stdint.h>
8+
9+
#define TRAILER_SKIP 0
10+
#include "image.h"
11+
12+
START_TEST(test_wolfboot_max_space_formula)
13+
{
14+
uint32_t expected = (uint32_t)(WOLFBOOT_PARTITION_SIZE -
15+
(TRAILER_SKIP + sizeof(uint32_t) +
16+
((WOLFBOOT_PARTITION_SIZE + 1U) / (WOLFBOOT_SECTOR_SIZE * 8U))));
17+
uint32_t actual = (uint32_t)WOLFBOOT_MAX_SPACE;
18+
19+
ck_assert_uint_eq(actual, expected);
20+
}
21+
END_TEST
22+
23+
Suite *wolfboot_suite(void)
24+
{
25+
Suite *s;
26+
TCase *tc_core;
27+
28+
s = suite_create("WOLFBOOT_MAX_SPACE");
29+
tc_core = tcase_create("Core");
30+
tcase_add_test(tc_core, test_wolfboot_max_space_formula);
31+
suite_add_tcase(s, tc_core);
32+
33+
return s;
34+
}
35+
36+
int main(void)
37+
{
38+
int number_failed;
39+
Suite *s;
40+
SRunner *sr;
41+
42+
s = wolfboot_suite();
43+
sr = srunner_create(s);
44+
srunner_run_all(sr, CK_NORMAL);
45+
number_failed = srunner_ntests_failed(sr);
46+
srunner_free(sr);
47+
48+
return (number_failed == 0) ? 0 : 1;
49+
}

0 commit comments

Comments
 (0)