66#include <bootutil/boot_request.h>
77
88#include "bootutil/bootutil_log.h"
9- #include <zephyr/retention/retention.h>
109
1110/** Special value of image number, indicating a request to the bootloader. */
1211#define BOOT_REQUEST_IMG_BOOTLOADER 0xFF
1312
1413/** Additional memory used by the retention subsystem (2B - prefix, 4B - CRC).*/
1514#define BOOT_REQUEST_ENTRY_METADATA_SIZE (2 + 4)
1615
17- BOOT_LOG_MODULE_REGISTER (bootloader_request );
16+ /** Number of images supported by the bootloader requests. */
17+ #define BOOT_REQUEST_IMG_NUM 2
1818
19- static const struct device * bootloader_request_dev =
20- DEVICE_DT_GET (DT_CHOSEN (nrf_bootloader_request ));
19+ BOOT_LOG_MODULE_REGISTER (bootloader_request );
2120
2221enum boot_request_type {
2322 /** Invalid request. */
@@ -143,16 +142,26 @@ static int boot_request_entry_find(enum boot_request_type type, boot_request_img
143142
144143int boot_request_init (void )
145144{
146- if (!device_is_ready (bootloader_request_dev )) {
147- return - EIO ;
148- }
149-
150- return 0 ;
145+ return boot_request_mem_init ();
151146}
152147
153148int boot_request_clear (void )
154149{
155- return retention_clear (bootloader_request_dev );
150+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
151+ size_t nv_indexes [BOOT_REQUEST_IMG_NUM ];
152+ uint8_t image ;
153+
154+ for (image = 0 ; image < BOOT_REQUEST_IMG_NUM ; image ++ ) {
155+ if (boot_request_entry_find (BOOT_REQUEST_IMG_PREFERENCE , image ,
156+ & nv_indexes [image ]) != 0 ) {
157+ return - EINVAL ;
158+ }
159+ }
160+
161+ return boot_request_mem_selective_erase (nv_indexes , BOOT_REQUEST_IMG_NUM );
162+ #else
163+ return boot_request_mem_selective_erase (NULL , 0 );
164+ #endif
156165}
157166
158167int boot_request_confirm_slot (uint8_t image , enum boot_slot slot )
@@ -161,6 +170,14 @@ int boot_request_confirm_slot(uint8_t image, enum boot_slot slot)
161170 size_t req_entry ;
162171 int ret ;
163172
173+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
174+ if (!boot_request_updateable ()) {
175+ BOOT_LOG_ERR ("Unable to confirm slot - area not updateable." );
176+ /* Cannot update area if it is corrupted. */
177+ return - EIO ;
178+ }
179+ #endif
180+
164181 ret = boot_request_entry_find (BOOT_REQUEST_IMG_CONFIRM , image , & req_entry );
165182 if (ret != 0 ) {
166183 return ret ;
@@ -177,8 +194,7 @@ int boot_request_confirm_slot(uint8_t image, enum boot_slot slot)
177194 return - EINVAL ;
178195 }
179196
180- return retention_write (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
181- sizeof (value ));
197+ return boot_request_mem_write (req_entry * sizeof (value ), & value );
182198}
183199
184200bool boot_request_check_confirmed_slot (uint8_t image , enum boot_slot slot )
@@ -192,8 +208,7 @@ bool boot_request_check_confirmed_slot(uint8_t image, enum boot_slot slot)
192208 return false;
193209 }
194210
195- ret = retention_read (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
196- sizeof (value ));
211+ ret = boot_request_mem_read (req_entry * sizeof (uint8_t ), & value );
197212 if (ret != 0 ) {
198213 return false;
199214 }
@@ -216,6 +231,14 @@ int boot_request_set_preferred_slot(uint8_t image, enum boot_slot slot)
216231 size_t req_entry ;
217232 int ret ;
218233
234+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
235+ if (!boot_request_updateable ()) {
236+ BOOT_LOG_ERR ("Unable to select slot - area not updateable." );
237+ /* Cannot update area if it is corrupted. */
238+ return - EIO ;
239+ }
240+ #endif
241+
219242 ret = boot_request_entry_find (BOOT_REQUEST_IMG_PREFERENCE , image , & req_entry );
220243 if (ret != 0 ) {
221244 return ret ;
@@ -232,8 +255,7 @@ int boot_request_set_preferred_slot(uint8_t image, enum boot_slot slot)
232255 return - EINVAL ;
233256 }
234257
235- return retention_write (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
236- sizeof (value ));
258+ return boot_request_mem_write (req_entry * sizeof (value ), & value );
237259}
238260
239261enum boot_slot boot_request_get_preferred_slot (uint8_t image )
@@ -247,8 +269,7 @@ enum boot_slot boot_request_get_preferred_slot(uint8_t image)
247269 return BOOT_SLOT_NONE ;
248270 }
249271
250- ret = retention_read (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
251- sizeof (value ));
272+ ret = boot_request_mem_read (req_entry * sizeof (uint8_t ), & value );
252273 if (ret != 0 ) {
253274 return BOOT_SLOT_NONE ;
254275 }
@@ -271,14 +292,21 @@ int boot_request_enter_recovery(void)
271292 size_t req_entry ;
272293 int ret ;
273294
295+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
296+ if (!boot_request_updateable ()) {
297+ BOOT_LOG_ERR ("Unable to enter recovery - area not updateable." );
298+ /* Cannot update area if it is corrupted. */
299+ return - EIO ;
300+ }
301+ #endif
302+
274303 ret = boot_request_entry_find (BOOT_REQUEST_BOOT_MODE , BOOT_REQUEST_IMG_BOOTLOADER ,
275304 & req_entry );
276305 if (ret != 0 ) {
277306 return ret ;
278307 }
279308
280- return retention_write (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
281- sizeof (value ));
309+ return boot_request_mem_write (req_entry * sizeof (value ), & value );
282310}
283311
284312#ifdef CONFIG_NRF_BOOT_SERIAL_BOOT_REQ
@@ -294,8 +322,7 @@ bool boot_request_detect_recovery(void)
294322 return false;
295323 }
296324
297- ret = retention_read (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
298- sizeof (value ));
325+ ret = boot_request_mem_read (req_entry * sizeof (uint8_t ), & value );
299326 if ((ret == 0 ) && (value == BOOT_REQUEST_MODE_RECOVERY )) {
300327 return true;
301328 }
@@ -310,14 +337,21 @@ int boot_request_enter_firmware_loader(void)
310337 size_t req_entry ;
311338 int ret ;
312339
340+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
341+ if (!boot_request_updateable ()) {
342+ BOOT_LOG_ERR ("Unable to enter FW loader - area not updateable." );
343+ /* Cannot update area if it is corrupted. */
344+ return - EIO ;
345+ }
346+ #endif
347+
313348 ret = boot_request_entry_find (BOOT_REQUEST_BOOT_MODE , BOOT_REQUEST_IMG_BOOTLOADER ,
314349 & req_entry );
315350 if (ret != 0 ) {
316351 return ret ;
317352 }
318353
319- return retention_write (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
320- sizeof (value ));
354+ return boot_request_mem_write (req_entry * sizeof (value ), & value );
321355}
322356
323357#ifdef CONFIG_NRF_BOOT_FIRMWARE_LOADER_BOOT_REQ
@@ -333,8 +367,7 @@ bool boot_request_detect_firmware_loader(void)
333367 return false;
334368 }
335369
336- ret = retention_read (bootloader_request_dev , req_entry * sizeof (value ), (void * )& value ,
337- sizeof (value ));
370+ ret = boot_request_mem_read (req_entry * sizeof (uint8_t ), & value );
338371 if ((ret == 0 ) && (value == BOOT_REQUEST_MODE_FIRMWARE_LOADER )) {
339372 return true;
340373 }
0 commit comments