Skip to content

Commit dec118d

Browse files
committed
feat(srv/flash-updater): Add support for verifying update ELF signature
1 parent db55227 commit dec118d

File tree

6 files changed

+283
-30
lines changed

6 files changed

+283
-30
lines changed

applications/bootloader/app.c

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <main.h>
22

3+
#include <libopencm3/cm3/scb.h>
34
#include <services/chainloader/chainloader.h>
45
#include <interfaces/flash.h>
56

@@ -19,11 +20,14 @@ static const char *bl_states[] = {
1920
"check-signature",
2021
"find-update",
2122
"validate-update",
23+
"flash-update",
24+
"disable-update",
25+
"reset",
2226
};
2327

2428

2529
static void bl_set_state(App *self, enum bl_state state) {
26-
u_log(system_log, LOG_TYPE_DEBUG, U_LOG_MODULE_PREFIX("state '%s' -> '%s'"), bl_states[self->state], bl_states[state]);
30+
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("\x1b[1mstate '%s' -> '%s'"), bl_states[self->state], bl_states[state]);
2731
self->state = state;
2832
}
2933

@@ -78,8 +82,6 @@ static app_ret_t bl_step(App *self) {
7882
}
7983

8084
case BL_STATE_FIND_UPDATE: {
81-
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("Trying to initialize flash-updater"));
82-
8385
Flash *target = NULL;
8486
if (iservicelocator_query_name_type(locator, "app", ISERVICELOCATOR_TYPE_FLASH, (Interface **)&target) != ISERVICELOCATOR_RET_OK) {
8587
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("no update target found, skipping update check"));
@@ -92,26 +94,72 @@ static app_ret_t bl_step(App *self) {
9294
bl_set_state(self, BL_STATE_FIND_APP);
9395
}
9496

95-
if (flash_updater_init(&self->updater, target) == FLASH_UPDATER_RET_OK &&
96-
flash_updater_set_source_flash(&self->updater, update) == FLASH_UPDATER_RET_OK) {
97-
bl_set_state(self, BL_STATE_VALIDATE_UPDATE);
97+
if (flash_updater_init(&self->updater, target) != FLASH_UPDATER_RET_OK ||
98+
flash_updater_set_source_flash(&self->updater, update) != FLASH_UPDATER_RET_OK) {
99+
bl_set_state(self, BL_STATE_FIND_APP);
100+
101+
}
98102

103+
Stream *console = NULL;
104+
if (iservicelocator_query_name_type(locator, "console", ISERVICELOCATOR_TYPE_STREAM, (Interface **)&console) == ISERVICELOCATOR_RET_OK) {
105+
/* Set only if found. */
106+
if (flash_updater_set_console(&self->updater, console) != FLASH_UPDATER_RET_OK) {
107+
bl_set_state(self, BL_STATE_FIND_APP);
108+
}
99109
}
110+
111+
bl_set_state(self, BL_STATE_VALIDATE_UPDATE);
100112
break;
101113
}
102114

103115
case BL_STATE_VALIDATE_UPDATE: {
104-
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("validating update sources"));
105-
if (flash_updater_validate_source(&self->updater) == FLASH_UPDATER_RET_OK) {
116+
const char pubkey_b64[] = CONFIG_BL_PUBKEY;
117+
size_t keylen = 32;
118+
uint8_t pubkey[32] = {0};
119+
base64decode(pubkey_b64, strlen(pubkey_b64), pubkey, &keylen);
120+
if (keylen != 32) {
121+
u_log(system_log, LOG_TYPE_ERROR, U_LOG_MODULE_PREFIX("wrong pubkey size %d"), keylen);
122+
bl_set_state(self, BL_STATE_ALL_FAILED);
123+
break;
124+
}
125+
126+
if (flash_updater_validate_source(&self->updater) == FLASH_UPDATER_RET_OK &&
127+
flash_updater_find_signature(&self->updater) == FLASH_UPDATER_RET_OK &&
128+
flash_updater_check_signature(&self->updater, pubkey) == FLASH_UPDATER_RET_OK) {
106129
/* Continue with the update process. */
107-
bl_set_state(self, BL_STATE_FIND_APP);
130+
bl_set_state(self, BL_STATE_FLASH_UPDATE);
108131
break;
109132
}
110-
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("cannot find update image, continuing boot"));
133+
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("cannot validate update image, continuing boot"));
111134
bl_set_state(self, BL_STATE_FIND_APP);
112135
break;
113136
}
114137

138+
case BL_STATE_FLASH_UPDATE: {
139+
/* If something failed, we cannot do more. */
140+
flash_updater_write(&self->updater);
141+
bl_set_state(self, BL_STATE_DISABLE_UPDATE);
142+
143+
break;
144+
}
145+
146+
case BL_STATE_DISABLE_UPDATE: {
147+
flash_updater_disable_update(&self->updater);
148+
bl_set_state(self, BL_STATE_FIND_APP);
149+
150+
break;
151+
}
152+
153+
154+
case BL_STATE_RESET: {
155+
SCB_AIRCR = (SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ);
156+
while (true) {
157+
;
158+
}
159+
160+
break;
161+
}
162+
115163
case BL_STATE_INIT:
116164
default:
117165
bl_set_state(self, BL_STATE_FIND_UPDATE);

applications/bootloader/app.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ enum bl_state {
3535

3636
BL_STATE_FIND_UPDATE,
3737
BL_STATE_VALIDATE_UPDATE,
38+
BL_STATE_FLASH_UPDATE,
39+
BL_STATE_DISABLE_UPDATE,
40+
BL_STATE_RESET,
3841
};
3942

4043
typedef struct {

ports/nwdaq-s2-main-g4/port.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,29 @@ Stm32Flash iflash;
145145
FlashVolStatic pv_iflash;
146146

147147
Flash *lv_bl;
148+
Flash *lv_conf;
149+
Flash *lv_mib;
148150
Flash *lv_app;
149151
Flash *lv_update;
150152

151153
static void port_flash_init(void) {
152154
stm32_flash_init(&iflash);
153155

154156
flash_vol_static_init(&pv_iflash, &iflash.flash);
155-
flash_vol_static_create(&pv_iflash, "bootloader", 0, 64 * 1024, &lv_bl);
156-
flash_vol_static_create(&pv_iflash, "app", 64 * 1024, 128 * 1024, &lv_app);
157-
flash_vol_static_create(&pv_iflash, "update", 192 * 1024, 64 * 1024, &lv_update);
157+
flash_vol_static_create(&pv_iflash, "bootloader", 0, 60 * 1024, &lv_bl);
158+
iservicelocator_add(locator, ISERVICELOCATOR_TYPE_FLASH, (Interface *)lv_bl, "bootloader");
159+
160+
flash_vol_static_create(&pv_iflash, "bootconf", 60 * 1024, 2 * 1024, &lv_conf);
161+
iservicelocator_add(locator, ISERVICELOCATOR_TYPE_FLASH, (Interface *)lv_conf, "bootconf");
162+
163+
flash_vol_static_create(&pv_iflash, "mib", 62 * 1024, 2 * 1024, &lv_mib);
164+
iservicelocator_add(locator, ISERVICELOCATOR_TYPE_FLASH, (Interface *)lv_mib, "mib");
165+
166+
flash_vol_static_create(&pv_iflash, "app", 64 * 1024, 128 * 1024, &lv_app);
167+
iservicelocator_add(locator, ISERVICELOCATOR_TYPE_FLASH, (Interface *)lv_app, "app");
168+
169+
flash_vol_static_create(&pv_iflash, "update", 192 * 1024, 64 * 1024, &lv_update);
170+
iservicelocator_add(locator, ISERVICELOCATOR_TYPE_FLASH, (Interface *)lv_update, "update");
158171
}
159172

160173

@@ -236,7 +249,7 @@ int32_t port_init(void) {
236249
port_setup_default_gpio();
237250
console_init();
238251
port_flash_init();
239-
xz_test();
252+
//xz_test();
240253

241254
#if !defined(CONFIG_APP_BL)
242255
gpio_set(GPIOA, GPIO5);

0 commit comments

Comments
 (0)