Skip to content

Commit 6bf8a0b

Browse files
boot: Add flash area ID/device ID retrieval hooks
For users that customise (for instance, via boot_go() hook) from where images are being loaded from, having those hardcoded to `slot_partitionX` (when using Zephyr) can be problematic. New hooks, `flash_area_id_from_multi_image_slot_hook` and `flash_area_get_device_id_hook` allow some customisation around these processes. Support for these hooks is shielded by a different configuration than other hooks - MCUBOOT_FLASH_AREA_HOOKS, so that it can be enabled independently from other available hooks. Support for this new hook was added to the Zephyr port. Signed-off-by: Ederson de Souza <[email protected]>
1 parent e0ed465 commit 6bf8a0b

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

boot/bootutil/include/bootutil/boot_hooks.h

+40
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@
8282

8383
#endif /* MCUBOOT_BOOT_GO_HOOKS */
8484

85+
#ifdef MCUBOOT_FLASH_AREA_HOOKS
86+
87+
#define BOOT_HOOK_FLASH_AREA_CALL(f, ret_default, ...) \
88+
DO_HOOK_CALL(f, ret_default, __VA_ARGS__)
89+
90+
#else
91+
92+
#define BOOT_HOOK_FLASH_AREA_CALL(f, ret_default, ...) \
93+
HOOK_CALL_NOP(f, ret_default, __VA_ARGS__)
94+
95+
#endif /* MCUBOOT_FLASH_AREA_ID_HOOKS */
8596

8697
/** Hook for provide image header data.
8798
*
@@ -215,6 +226,35 @@ int boot_reset_request_hook(bool force);
215226
*/
216227
fih_ret boot_go_hook(struct boot_rsp *rsp);
217228

229+
/**
230+
* Hook to implement custom action before retrieving flash area ID.
231+
*
232+
* @param image_index the index of the image pair
233+
* @param slot slot number
234+
* @param area_id the flash area ID to be populated
235+
*
236+
* @retval 0 the flash area ID was fetched successfully;
237+
* BOOT_HOOK_REGULAR follow the normal execution path to get the flash
238+
* area ID;
239+
* otherwise an error-code value.
240+
*/
241+
int flash_area_id_from_multi_image_slot_hook(int image_index, int slot,
242+
int *area_id);
243+
244+
/**
245+
* Hook to implement custom action before retrieving flash area device ID.
246+
*
247+
* @param fa the flash area structure
248+
* @param device_id the device ID to be populated
249+
*
250+
* @retval 0 the device ID was fetched successfully;
251+
* BOOT_HOOK_REGULAR follow the normal execution path to get the device
252+
* ID;
253+
* otherwise an error-code value.
254+
*/
255+
int flash_area_get_device_id_hook(const struct flash_area *fa,
256+
uint8_t *device_id);
257+
218258
#define BOOT_RESET_REQUEST_HOOK_BUSY 1
219259
#define BOOT_RESET_REQUEST_HOOK_TIMEOUT 2
220260
#define BOOT_RESET_REQUEST_HOOK_CHECK_FAILED 3

boot/zephyr/Kconfig

+18
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,17 @@ config BOOT_IMAGE_EXECUTABLE_RAM_SIZE
389389
default $(dt_chosen_reg_size_int,$(DT_CHOSEN_Z_SRAM),0)
390390
endif
391391

392+
config FLASH_RUNTIME_SOURCES
393+
bool "Images are read from flash partitions defined at runtime"
394+
select SINGLE_APPLICATION_SLOT
395+
help
396+
Instead of using information on the flash slots to decide which images
397+
to load/update, the application provides the information from which
398+
flash slot to load in runtime. This is useful when the application
399+
reads the state for hardware straps or other sources to decide which
400+
image to load. Usually, application will provide a boot_go_hook() to
401+
decide which image to load.
402+
392403
config BOOT_ENCRYPTION_SUPPORT
393404
bool
394405
help
@@ -779,6 +790,13 @@ config BOOT_GO_HOOKS
779790
MCUboot's boot_go routine. It is up to the project customization to
780791
add required source files to the build.
781792

793+
config BOOT_FLASH_AREA_HOOKS
794+
bool "Enable hooks for overriding MCUboot's flash area routines"
795+
help
796+
Allow to provide procedures for override or extend native
797+
MCUboot's flash area routines. It is up to the project customization to
798+
add required source files to the build.
799+
782800
config MCUBOOT_ACTION_HOOKS
783801
bool "Enable hooks for responding to MCUboot status changes"
784802
help

boot/zephyr/flash_map_extended.c

+19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include <flash_map_backend/flash_map_backend.h>
1515
#include <sysflash/sysflash.h>
1616

17+
#include "bootutil/boot_hooks.h"
1718
#include "bootutil/bootutil_log.h"
19+
#include "bootutil/bootutil_public.h"
1820

1921
BOOT_LOG_MODULE_DECLARE(mcuboot);
2022

@@ -58,6 +60,14 @@ int flash_device_base(uint8_t fd_id, uintptr_t *ret)
5860
*/
5961
int flash_area_id_from_multi_image_slot(int image_index, int slot)
6062
{
63+
int rc, id;
64+
65+
rc = BOOT_HOOK_FLASH_AREA_CALL(flash_area_id_from_multi_image_slot_hook,
66+
BOOT_HOOK_REGULAR, image_index, slot, &id);
67+
if (rc != BOOT_HOOK_REGULAR) {
68+
return id;
69+
}
70+
6171
switch (slot) {
6272
case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
6373
#if !defined(CONFIG_SINGLE_APPLICATION_SLOT)
@@ -141,6 +151,15 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
141151

142152
uint8_t flash_area_get_device_id(const struct flash_area *fa)
143153
{
154+
uint8_t device_id;
155+
int rc;
156+
157+
rc = BOOT_HOOK_FLASH_AREA_CALL(flash_area_get_device_id_hook,
158+
BOOT_HOOK_REGULAR, fa, &device_id);
159+
if (rc != BOOT_HOOK_REGULAR) {
160+
return device_id;
161+
}
162+
144163
#if defined(CONFIG_ARM)
145164
return fa->fa_id;
146165
#else

boot/zephyr/include/mcuboot_config/mcuboot_config.h

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@
231231
#define MCUBOOT_BOOT_GO_HOOKS
232232
#endif
233233

234+
#ifdef CONFIG_BOOT_FLASH_AREA_HOOKS
235+
#define MCUBOOT_FLASH_AREA_HOOKS
236+
#endif
237+
234238
#ifdef CONFIG_MCUBOOT_VERIFY_IMG_ADDRESS
235239
#define MCUBOOT_VERIFY_IMG_ADDRESS
236240
#endif

0 commit comments

Comments
 (0)