2
2
* SPDX-License-Identifier: Apache-2.0
3
3
*
4
4
* Copyright (c) 2020 Arm Limited
5
- * Copyright (c) 2020-2023 Nordic Semiconductor ASA
5
+ * Copyright (c) 2020-2025 Nordic Semiconductor ASA
6
6
*/
7
7
8
8
#include <assert.h>
@@ -27,7 +27,30 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
27
27
static const struct flash_area * _fa_p ;
28
28
static struct image_header _hdr = { 0 };
29
29
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
+
31
54
/**
32
55
* Validate hash of a primary boot image.
33
56
*
@@ -65,7 +88,6 @@ boot_image_validate(const struct flash_area *fa_p,
65
88
66
89
FIH_RET (fih_rc );
67
90
}
68
- #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/
69
91
70
92
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE )
71
93
inline static fih_ret
@@ -103,42 +125,57 @@ boot_image_validate_once(const struct flash_area *fa_p,
103
125
#endif
104
126
105
127
/**
106
- * Validates that an image in a slot is OK to boot.
128
+ * Validates that an image in a partition is OK to boot.
107
129
*
108
- * @param[in] slot Slot number to check
130
+ * @param[in] id Fixed partition ID to check
109
131
* @param[out] rsp Parameters for booting image, on success
110
132
*
111
133
* @return FIH_SUCCESS on success; non-zero on failure.
112
134
*/
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 )
114
136
{
115
137
int rc = -1 ;
116
138
FIH_DECLARE (fih_rc , FIH_FAILURE );
117
139
118
- BOOT_LOG_DBG ("validate_image_slot: slot %d" , slot );
140
+ BOOT_LOG_DBG ("validate_image_id: id %d" , id );
119
141
120
- rc = flash_area_open (slot , & _fa_p );
142
+ rc = flash_area_open (id , & _fa_p );
121
143
assert (rc == 0 );
122
144
123
145
rc = boot_image_load_header (_fa_p , & _hdr );
124
146
if (rc != 0 ) {
125
147
goto other ;
126
148
}
127
149
150
+ switch (id ) {
151
+ case SLOT0_PARTITION_ID :
152
+ #ifdef SLOT2_PARTITION_ID
153
+ case SLOT2_PARTITION_ID :
154
+ #endif /* SLOT2_PARTITION_ID */
128
155
#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
+ }
133
160
#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 ;
136
168
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 ;
137
176
}
138
- #else
139
- fih_rc = FIH_SUCCESS ;
140
- #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
141
177
178
+ BOOT_LOG_INF ("validate_image_id: id %d is valid." , id );
142
179
rsp -> br_flash_dev_id = flash_area_get_device_id (_fa_p );
143
180
rsp -> br_image_off = flash_area_get_off (_fa_p );
144
181
rsp -> br_hdr = & _hdr ;
@@ -168,46 +205,78 @@ boot_go(struct boot_rsp *rsp)
168
205
BOOT_LOG_DBG ("boot_go: firmware loader" );
169
206
170
207
#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." );
173
210
boot_firmware_loader = true;
174
211
}
175
212
#endif
176
213
177
214
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET
178
215
if (io_detect_pin_reset ()) {
216
+ BOOT_LOG_INF ("Pin reset detected - enter firmware loader." );
179
217
boot_firmware_loader = true;
180
218
}
181
219
#endif
182
220
183
221
#ifdef CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE
184
222
if (io_detect_boot_mode ()) {
223
+ BOOT_LOG_INF ("Boot mode detected - enter firmware loader." );
185
224
boot_firmware_loader = true;
186
225
}
187
226
#endif
188
227
189
228
#ifdef CONFIG_NRF_BOOT_FIRMWARE_LOADER_BOOT_REQ
190
229
if (boot_request_detect_firmware_loader ()) {
230
+ BOOT_LOG_INF ("Boot request detected - enter firmware loader." );
191
231
boot_firmware_loader = true;
192
232
}
193
233
#endif
194
234
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." );
200
246
FIH_RET (fih_rc );
247
+ #endif
201
248
}
202
- }
203
-
204
- FIH_CALL (validate_image_slot , fih_rc , FLASH_AREA_IMAGE_PRIMARY (0 ), rsp );
249
+ #endif /* slot2_partition */
205
250
251
+ FIH_CALL (validate_image_id , fih_rc , SLOT0_PARTITION_ID , rsp );
206
252
#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 );
209
263
}
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
+ }
210
274
#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
+ }
211
280
212
281
FIH_RET (fih_rc );
213
282
}
0 commit comments