Skip to content

Commit 9265fe7

Browse files
committed
loader: panic on TPM init failure
F/2569
1 parent dc7309c commit 9265fe7

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

src/loader.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ int main(void)
124124
uart_send_current_version();
125125
#endif
126126
#ifdef WOLFBOOT_TPM
127-
wolfBoot_tpm2_init();
127+
if (wolfBoot_tpm2_init() != 0) {
128+
wolfBoot_panic();
129+
}
128130
#endif
129131
#ifdef WOLFCRYPT_SECURE_MODE
130132
wcs_Init();

tools/unit-tests/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TESTS:=unit-parser unit-fdt unit-extflash unit-string unit-spi-flash unit-aes128
4949
unit-enc-nvm-flagshome unit-delta unit-update-flash unit-update-flash-delta \
5050
unit-update-flash-self-update \
5151
unit-update-flash-enc unit-update-ram unit-update-ram-nofixed unit-pkcs11_store unit-psa_store unit-disk \
52-
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
52+
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-loader-tpm-init unit-qspi-flash unit-tpm-rsa-exp \
5353
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-store-sbrk \
5454
unit-tpm-blob unit-policy-create unit-policy-sign unit-rot-auth unit-sdhci-response-bits \
5555
unit-sdhci-disk-unaligned unit-sign-encrypted-output
@@ -235,6 +235,12 @@ unit-boot-x86-fsp: ../../include/target.h unit-boot-x86_fsp.c
235235
-DUCODE0_ADDRESS=0 -ffunction-sections -fdata-sections $(LDFLAGS) \
236236
-Wl,--gc-sections
237237

238+
unit-loader-tpm-init: ../../include/target.h unit-loader-tpm-init.c
239+
gcc -o $@ $^ $(CFLAGS) -DWOLFBOOT_LOADER_MAIN -DWOLFBOOT_TPM \
240+
-DWOLFBOOT_HOOK_PANIC -DWOLFBOOT_SIGN_ECC256 \
241+
-DWOLFBOOT_HASH_SHA256 -ffunction-sections -fdata-sections \
242+
$(LDFLAGS) -Wl,--gc-sections
243+
238244
unit-mock-state: ../../include/target.h unit-mock-state.c
239245
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
240246

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <check.h>
2+
#include <setjmp.h>
3+
#include <stdio.h>
4+
#include <stdint.h>
5+
6+
static int mock_tpm_init_rc;
7+
static int start_calls;
8+
static jmp_buf exit_env;
9+
10+
void hal_init(void)
11+
{
12+
}
13+
14+
uint16_t spi_flash_probe(void)
15+
{
16+
return 0;
17+
}
18+
19+
int wolfBoot_tpm2_init(void)
20+
{
21+
return mock_tpm_init_rc;
22+
}
23+
24+
void wolfBoot_start(void)
25+
{
26+
start_calls++;
27+
longjmp(exit_env, 2);
28+
}
29+
30+
void wolfBoot_hook_panic(void)
31+
{
32+
longjmp(exit_env, 1);
33+
}
34+
35+
#include "../../src/loader.c"
36+
37+
static void setup(void)
38+
{
39+
mock_tpm_init_rc = 0;
40+
start_calls = 0;
41+
}
42+
43+
START_TEST(test_loader_panics_when_tpm_init_fails)
44+
{
45+
int exit_reason;
46+
47+
mock_tpm_init_rc = -1;
48+
exit_reason = setjmp(exit_env);
49+
if (exit_reason == 0) {
50+
ck_assert_int_eq(loader_main(), 0);
51+
}
52+
53+
ck_assert_int_eq(exit_reason, 1);
54+
ck_assert_int_eq(start_calls, 0);
55+
}
56+
END_TEST
57+
58+
START_TEST(test_loader_starts_boot_when_tpm_init_succeeds)
59+
{
60+
int exit_reason;
61+
62+
exit_reason = setjmp(exit_env);
63+
if (exit_reason == 0) {
64+
ck_assert_int_eq(loader_main(), 0);
65+
}
66+
67+
ck_assert_int_eq(exit_reason, 2);
68+
ck_assert_int_eq(start_calls, 1);
69+
}
70+
END_TEST
71+
72+
static Suite *loader_suite(void)
73+
{
74+
Suite *s;
75+
TCase *tc;
76+
77+
s = suite_create("loader");
78+
tc = tcase_create("loader_main");
79+
tcase_add_checked_fixture(tc, setup, NULL);
80+
tcase_add_test(tc, test_loader_panics_when_tpm_init_fails);
81+
tcase_add_test(tc, test_loader_starts_boot_when_tpm_init_succeeds);
82+
suite_add_tcase(s, tc);
83+
return s;
84+
}
85+
86+
int main(void)
87+
{
88+
Suite *s;
89+
SRunner *sr;
90+
int failed;
91+
92+
s = loader_suite();
93+
sr = srunner_create(s);
94+
srunner_run_all(sr, CK_NORMAL);
95+
failed = srunner_ntests_failed(sr);
96+
srunner_free(sr);
97+
return failed == 0 ? 0 : 1;
98+
}

0 commit comments

Comments
 (0)