Skip to content

Commit 0700599

Browse files
committed
Improvements to lib-fs status and added keystore. Added some useful logging/debugging
1 parent e48ba6e commit 0700599

File tree

4 files changed

+108
-38
lines changed

4 files changed

+108
-38
lines changed

arch.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ ifeq ($(TARGET),library_fs)
13361336
# Force all partitions to be marked as external
13371337
NO_XIP=1
13381338
NO_SWAP_EXT=
1339+
NO_LOADER=1
1340+
USE_GCC_HEADLESS=0
1341+
CFLAGS+=-DWOLFBOOT_USE_STDLIBC
13391342
endif
13401343

13411344

config/examples/library_fs.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x80000
2222
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x100000
2323
# Location in flash for swap
2424
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x180000
25+
26+
# Optional debugging of flash read/write/erase calls
27+
# CFLAGS_EXTRA+=-DDEBUG_EXT_FLASH

hal/filesystem.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,20 @@ static int setup_file(byte read_only)
9898
fp = XBADFILE;
9999
}
100100
}
101+
else {
102+
wolfBoot_printf("Failed to open file %s\n",
103+
WOLFBOOT_PARTITION_FILENAME);
104+
}
101105
}
102106
return fp != XBADFILE ? 0 : -1;
103107
}
104108

105109
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
106110
{
111+
#ifdef DEBUG_EXT_FLASH
112+
wolfBoot_printf("ext_flash_write: addr %p data %p len %d\n",
113+
(void*)address, data, len);
114+
#endif
107115
if (setup_file(0) != 0)
108116
return -1;
109117
if (address + len > (uintptr_t)fp_size)
@@ -119,19 +127,32 @@ int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
119127

120128
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
121129
{
130+
#ifdef DEBUG_EXT_FLASH
131+
wolfBoot_printf("ext_flash_read: addr %p data %p len %d\n",
132+
(void*)address, data, len);
133+
#endif
122134
if (setup_file(1) != 0)
123135
return -1;
124136
if (XFSEEK(fp, address, XSEEK_SET) < 0)
125137
return -1;
126138
return (int)XFREAD(data, 1, len, fp);
127139
}
140+
128141
int ext_flash_erase(uintptr_t address, int len)
129142
{
130-
byte zeros[256];
131-
XMEMSET(zeros, 0, sizeof(zeros));
132-
for (; len > 0; len -= sizeof(zeros), address += sizeof(zeros)) {
133-
if (ext_flash_write(address, zeros, (int)MIN((int)sizeof(zeros), len)) != 0)
143+
byte erase_data[256];
144+
#ifdef DEBUG_EXT_FLASH
145+
wolfBoot_printf("ext_flash_erase: addr %p len %d\n",
146+
(void*)address, len);
147+
#endif
148+
XMEMSET(erase_data, 0xFF, sizeof(erase_data));
149+
while (len > 0) {
150+
int erase_len = (int)MIN((int)sizeof(erase_data), len);
151+
if (ext_flash_write(address, erase_data, erase_len) != 0) {
134152
return -1;
153+
}
154+
len -= erase_len;
155+
address += erase_len;
135156
}
136157
return 0;
137158
}

hal/library_fs.c

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "printf.h"
2828
#include "wolfboot/wolfboot.h"
2929

30+
3031
/* Helper function to convert partition ID to string */
3132
static const char* partition_name(uint8_t part)
3233
{
@@ -41,52 +42,95 @@ static const char* partition_name(uint8_t part)
4142
}
4243

4344
/* Helper function to convert state value to string */
44-
static const char* state_name(uint8_t state)
45+
static const char* partition_state_name(uint8_t state)
4546
{
4647
switch (state) {
4748
case IMG_STATE_NEW:
4849
return "NEW";
4950
case IMG_STATE_UPDATING:
5051
return "UPDATING";
52+
case IMG_STATE_FINAL_FLAGS:
53+
return "FFLAGS";
54+
case IMG_STATE_TESTING:
55+
return "TESTING";
5156
case IMG_STATE_SUCCESS:
5257
return "SUCCESS";
5358
default:
5459
return "UNKNOWN";
5560
}
5661
}
5762

58-
/* Print partition state */
59-
static int cmd_get_state(uint8_t part)
63+
/* Print all partition states */
64+
static int cmd_get_all_states(void)
6065
{
61-
uint8_t state;
62-
int ret;
66+
uint32_t cur_fw_version, update_fw_version;
67+
uint16_t hdrSz;
68+
uint8_t boot_part_state = IMG_STATE_NEW, update_part_state = IMG_STATE_NEW;
6369

64-
ret = wolfBoot_get_partition_state(part, &state);
65-
if (ret != 0) {
66-
wolfBoot_printf("Error: Failed to get state for %s partition (error: %d)\n",
67-
partition_name(part), ret);
68-
return -1;
70+
cur_fw_version = wolfBoot_current_firmware_version();
71+
update_fw_version = wolfBoot_update_firmware_version();
72+
73+
wolfBoot_get_partition_state(PART_BOOT, &boot_part_state);
74+
wolfBoot_get_partition_state(PART_UPDATE, &update_part_state);
75+
76+
wolfBoot_printf("\n");
77+
wolfBoot_printf("System information\n");
78+
wolfBoot_printf("====================================\n");
79+
wolfBoot_printf("Firmware version : 0x%lx\n",
80+
(unsigned long)wolfBoot_current_firmware_version());
81+
wolfBoot_printf("Current firmware state: %s\n",
82+
partition_state_name(boot_part_state));
83+
if (update_fw_version != 0) {
84+
if (update_part_state == IMG_STATE_UPDATING) {
85+
wolfBoot_printf("Candidate firmware version : 0x%lx\n",
86+
(unsigned long)update_fw_version);
87+
} else {
88+
wolfBoot_printf("Backup firmware version : 0x%lx\n",
89+
(unsigned long)update_fw_version);
90+
}
91+
wolfBoot_printf("Update state: %s\n",
92+
partition_state_name(update_part_state));
93+
if (update_fw_version > cur_fw_version) {
94+
wolfBoot_printf("'reboot' to initiate update.\n");
95+
} else {
96+
wolfBoot_printf("Update image older than current.\n");
97+
}
98+
} else {
99+
wolfBoot_printf("No image in update partition.\n");
69100
}
70101

71-
wolfBoot_printf("%s partition state: %s (0x%02X)\n",
72-
partition_name(part), state_name(state), state);
73102
return 0;
74103
}
75104

76-
/* Print all partition states */
77-
static int cmd_get_all_states(void)
105+
static int cmd_get_keystore(void)
78106
{
79-
int ret = 0;
80-
81-
wolfBoot_printf("=== Partition States ===\n");
107+
int i, j;
108+
uint32_t n_keys;
82109

83-
if (cmd_get_state(PART_BOOT) != 0)
84-
ret = -1;
85-
86-
if (cmd_get_state(PART_UPDATE) != 0)
87-
ret = -1;
110+
wolfBoot_printf("\n");
111+
wolfBoot_printf("Bootloader keystore information\n");
112+
wolfBoot_printf("====================================\n");
113+
n_keys = keystore_num_pubkeys();
114+
wolfBoot_printf("Number of public keys: %lu\n", (unsigned long)n_keys);
115+
for (i = 0; i < (int)n_keys; i++) {
116+
uint32_t size = keystore_get_size(i);
117+
uint32_t type = keystore_get_key_type(i);
118+
uint32_t mask = keystore_get_mask(i);
119+
uint8_t *keybuf = keystore_get_buffer(i);
88120

89-
return ret;
121+
wolfBoot_printf("\n");
122+
wolfBoot_printf(" Public Key #%d: size %lu, type %lx, mask %08lx\n", i,
123+
(unsigned long)size, (unsigned long)type, (unsigned long)mask);
124+
wolfBoot_printf(" ====================================\n ");
125+
for (j = 0; j < (int)size; j++) {
126+
wolfBoot_printf("%02X ", keybuf[j]);
127+
if (j % 16 == 15) {
128+
wolfBoot_printf("\n ");
129+
}
130+
}
131+
wolfBoot_printf("\n");
132+
}
133+
return 0;
90134
}
91135

92136
/* Trigger an update */
@@ -114,8 +158,7 @@ static void print_usage(const char* prog_name)
114158
wolfBoot_printf("\nUsage: %s <command> [options]\n\n", prog_name);
115159
wolfBoot_printf("Commands:\n");
116160
wolfBoot_printf(" status - Show state of all partitions\n");
117-
wolfBoot_printf(" get-boot - Get BOOT partition state\n");
118-
wolfBoot_printf(" get-update - Get UPDATE partition state\n");
161+
wolfBoot_printf(" keystore - Show keystore information\n");
119162
wolfBoot_printf(" update-trigger - Trigger an update (sets UPDATE partition to UPDATING)\n");
120163
wolfBoot_printf(" success - Mark BOOT partition as SUCCESS\n");
121164
wolfBoot_printf(" verify-boot - Verify integrity and authenticity of BOOT partition\n");
@@ -140,24 +183,27 @@ static int cmd_verify(uint8_t part)
140183

141184
ret = wolfBoot_open_image(&img, part);
142185
if (ret < 0) {
143-
wolfBoot_printf("Error: Failed to open image header for %s partition (error: %d)\n",
186+
wolfBoot_printf("Failed to open image header for %s partition (error: %d)\n",
144187
partition_name(part), ret);
145188
return -1;
146189
}
147190

148191
ret = wolfBoot_verify_integrity(&img);
149192
if (ret < 0) {
150-
wolfBoot_printf("Integrity check failed for %s partition\n", partition_name(part));
193+
wolfBoot_printf("Integrity check failed for %s partition\n",
194+
partition_name(part));
151195
return -1;
152196
}
153197

154198
ret = wolfBoot_verify_authenticity(&img);
155199
if (ret < 0) {
156-
wolfBoot_printf("Authenticity check failed for %s partition\n", partition_name(part));
200+
wolfBoot_printf("Authenticity check failed for %s partition\n",
201+
partition_name(part));
157202
return -1;
158203
}
159204

160-
wolfBoot_printf("%s partition: Integrity and authenticity verified.\n", partition_name(part));
205+
wolfBoot_printf("%s partition: Integrity and authenticity verified.\n",
206+
partition_name(part));
161207
return ret;
162208
}
163209

@@ -183,11 +229,8 @@ int main(int argc, const char* argv[])
183229
if (strcmp(command, "status") == 0) {
184230
ret = cmd_get_all_states();
185231
}
186-
else if (strcmp(command, "get-boot") == 0) {
187-
ret = cmd_get_state(PART_BOOT);
188-
}
189-
else if (strcmp(command, "get-update") == 0) {
190-
ret = cmd_get_state(PART_UPDATE);
232+
else if (strcmp(command, "keystore") == 0) {
233+
ret = cmd_get_keystore();
191234
}
192235
else if (strcmp(command, "update-trigger") == 0) {
193236
ret = cmd_update_trigger();

0 commit comments

Comments
 (0)