Skip to content

Commit 982fae2

Browse files
committed
boot: zephyr: Add multi-image support in FW loader
Allow to use two images for both the application as well as the firmware loader. This confiugration alows to build the following configurations: - application: 1 image, FW loader: 1 image - application: 1 image, FW loader: 2 images - application: 2 images, FW loader: 1 image - application: 2 images, FW loader: 2 images Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent a999034 commit 982fae2

File tree

1 file changed

+98
-29
lines changed

1 file changed

+98
-29
lines changed

boot/zephyr/firmware_loader.c

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*
44
* Copyright (c) 2020 Arm Limited
5-
* Copyright (c) 2020-2023 Nordic Semiconductor ASA
5+
* Copyright (c) 2020-2025 Nordic Semiconductor ASA
66
*/
77

88
#include <assert.h>
@@ -27,7 +27,30 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
2727
static const struct flash_area *_fa_p;
2828
static struct image_header _hdr = { 0 };
2929

30-
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) || defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
30+
#if DT_NODE_EXISTS(DT_NODELABEL(slot0_partition))
31+
#define SLOT0_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(slot0_partition))
32+
#else
33+
#error "No slot0_partition found in DTS"
34+
#endif
35+
36+
#if DT_NODE_EXISTS(DT_NODELABEL(slot2_partition))
37+
#define SLOT2_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(slot2_partition))
38+
#endif
39+
40+
#if DT_NODE_EXISTS(DT_NODELABEL(fw_loader_partition))
41+
#define FW_LOADER_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(fw_loader_partition))
42+
#elif DT_NODE_EXISTS(DT_NODELABEL(slot1_partition))
43+
#define FW_LOADER_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(slot1_partition))
44+
#else
45+
#error "No firmware loader partition found in DTS"
46+
#endif
47+
48+
#if DT_NODE_EXISTS(DT_NODELABEL(fw_loader_aux_partition))
49+
#define FW_LOADER_AUX_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(fw_loader_aux_partition))
50+
#elif DT_NODE_EXISTS(DT_NODELABEL(slot3_partition))
51+
#define FW_LOADER_AUX_PARTITION_ID DT_FIXED_PARTITION_ID(DT_NODELABEL(slot3_partition))
52+
#endif
53+
3154
/**
3255
* Validate hash of a primary boot image.
3356
*
@@ -65,7 +88,6 @@ boot_image_validate(const struct flash_area *fa_p,
6588

6689
FIH_RET(fih_rc);
6790
}
68-
#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/
6991

7092
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
7193
inline static fih_ret
@@ -103,42 +125,57 @@ boot_image_validate_once(const struct flash_area *fa_p,
103125
#endif
104126

105127
/**
106-
* Validates that an image in a slot is OK to boot.
128+
* Validates that an image in a partition is OK to boot.
107129
*
108-
* @param[in] slot Slot number to check
130+
* @param[in] id Fixed partition ID to check
109131
* @param[out] rsp Parameters for booting image, on success
110132
*
111133
* @return FIH_SUCCESS on success; non-zero on failure.
112134
*/
113-
static fih_ret validate_image_slot(int slot, struct boot_rsp *rsp)
135+
static fih_ret validate_image_id(int id, struct boot_rsp *rsp)
114136
{
115137
int rc = -1;
116138
FIH_DECLARE(fih_rc, FIH_FAILURE);
117139

118-
BOOT_LOG_DBG("validate_image_slot: slot %d", slot);
140+
BOOT_LOG_DBG("validate_image_id: id %d", id);
119141

120-
rc = flash_area_open(slot, &_fa_p);
142+
rc = flash_area_open(id, &_fa_p);
121143
assert(rc == 0);
122144

123145
rc = boot_image_load_header(_fa_p, &_hdr);
124146
if (rc != 0) {
125147
goto other;
126148
}
127149

150+
switch (id) {
151+
case SLOT0_PARTITION_ID:
152+
#ifdef SLOT2_PARTITION_ID
153+
case SLOT2_PARTITION_ID:
154+
#endif /* SLOT2_PARTITION_ID */
128155
#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
129-
FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
130-
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
131-
goto other;
132-
}
156+
FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
157+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
158+
goto other;
159+
}
133160
#elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
134-
FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
135-
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
161+
FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
162+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
163+
goto other;
164+
}
165+
break;
166+
#else
167+
fih_rc = FIH_SUCCESS;
136168
goto other;
169+
#endif /* !MCUBOOT_VALIDATE_PRIMARY_SLOT */
170+
default:
171+
FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
172+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
173+
goto other;
174+
}
175+
break;
137176
}
138-
#else
139-
fih_rc = FIH_SUCCESS;
140-
#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
141177

178+
BOOT_LOG_INF("validate_image_id: id %d is valid.", id);
142179
rsp->br_flash_dev_id = flash_area_get_device_id(_fa_p);
143180
rsp->br_image_off = flash_area_get_off(_fa_p);
144181
rsp->br_hdr = &_hdr;
@@ -168,46 +205,78 @@ boot_go(struct boot_rsp *rsp)
168205
BOOT_LOG_DBG("boot_go: firmware loader");
169206

170207
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO
171-
if (io_detect_pin() &&
172-
!io_boot_skip_serial_recovery()) {
208+
if (io_detect_pin() && !io_boot_skip_serial_recovery()) {
209+
BOOT_LOG_INF("Button press detected - enter firmware loader.");
173210
boot_firmware_loader = true;
174211
}
175212
#endif
176213

177214
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET
178215
if (io_detect_pin_reset()) {
216+
BOOT_LOG_INF("Pin reset detected - enter firmware loader.");
179217
boot_firmware_loader = true;
180218
}
181219
#endif
182220

183221
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE
184222
if (io_detect_boot_mode()) {
223+
BOOT_LOG_INF("Boot mode detected - enter firmware loader.");
185224
boot_firmware_loader = true;
186225
}
187226
#endif
188227

189228
#ifdef CONFIG_NRF_BOOT_FIRMWARE_LOADER_BOOT_REQ
190229
if (boot_request_detect_firmware_loader()) {
230+
BOOT_LOG_INF("Boot request detected - enter firmware loader.");
191231
boot_firmware_loader = true;
192232
}
193233
#endif
194234

195-
/* Check if firmware loader button is pressed. TODO: check all entrance methods */
196-
if (boot_firmware_loader == true) {
197-
FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_SECONDARY(0), rsp);
198-
199-
if (FIH_EQ(fih_rc, FIH_SUCCESS)) {
235+
while (boot_firmware_loader == false) {
236+
BOOT_LOG_DBG("Validating main image(s)...");
237+
#ifdef SLOT2_PARTITION_ID
238+
FIH_CALL(validate_image_id, fih_rc, SLOT2_PARTITION_ID, rsp);
239+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
240+
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_NO_APPLICATION
241+
BOOT_LOG_WRN("Failed to validate slot2_partition. Enter firmware loader.");
242+
boot_firmware_loader = true;
243+
break;
244+
#else
245+
BOOT_LOG_ERR("Failed to validate slot2_partition.");
200246
FIH_RET(fih_rc);
247+
#endif
201248
}
202-
}
203-
204-
FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_PRIMARY(0), rsp);
249+
#endif /* slot2_partition */
205250

251+
FIH_CALL(validate_image_id, fih_rc, SLOT0_PARTITION_ID, rsp);
206252
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_NO_APPLICATION
207-
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
208-
FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_SECONDARY(0), rsp);
253+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
254+
BOOT_LOG_WRN("Failed to validate slot0_partition. Enter firmware loader.");
255+
boot_firmware_loader = true;
256+
break;
257+
}
258+
#endif
259+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
260+
BOOT_LOG_ERR("Failed to validate slot0_partition.");
261+
}
262+
FIH_RET(fih_rc);
209263
}
264+
265+
/* Check if firmware loader button is pressed. TODO: check all entrance methods */
266+
if (boot_firmware_loader == true) {
267+
BOOT_LOG_DBG("Validating firmware loader image(s)...");
268+
#ifdef FW_LOADER_AUX_PARTITION_ID
269+
FIH_CALL(validate_image_id, fih_rc, FW_LOADER_AUX_PARTITION_ID, rsp);
270+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
271+
BOOT_LOG_ERR("Failed to validate auxiliary firmware loader image.");
272+
FIH_RET(fih_rc);
273+
}
210274
#endif
275+
FIH_CALL(validate_image_id, fih_rc, FW_LOADER_PARTITION_ID, rsp);
276+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
277+
BOOT_LOG_ERR("Failed to validate firmware loader image.");
278+
}
279+
}
211280

212281
FIH_RET(fih_rc);
213282
}

0 commit comments

Comments
 (0)