Skip to content

Commit 7c4ec9a

Browse files
tomchyde-nordic
authored andcommitted
loader: Add boot hook for slot selection
Add a bootloader hook to alter the logic of the active slot selection in Direct XIP modes. Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 94d85f9 commit 7c4ec9a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

boot/bootutil/include/bootutil/boot_hooks.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282

8383
#endif /* MCUBOOT_BOOT_GO_HOOKS */
8484

85+
#ifdef MCUBOOT_FIND_NEXT_SLOT_HOOKS
86+
87+
#define BOOT_HOOK_FIND_SLOT_CALL(f, ret_default, ...) \
88+
DO_HOOK_CALL(f, ret_default, __VA_ARGS__)
89+
90+
#else
91+
92+
#define BOOT_HOOK_FIND_SLOT_CALL(f, ret_default, ...) \
93+
HOOK_CALL_NOP(f, ret_default, __VA_ARGS__)
94+
95+
#endif /* MCUBOOT_FIND_NEXT_SLOT_HOOKS */
96+
8597
#ifdef MCUBOOT_FLASH_AREA_HOOKS
8698

8799
#define BOOT_HOOK_FLASH_AREA_CALL(f, ret_default, ...) \
@@ -260,4 +272,16 @@ int flash_area_get_device_id_hook(const struct flash_area *fa,
260272
#define BOOT_RESET_REQUEST_HOOK_CHECK_FAILED 3
261273
#define BOOT_RESET_REQUEST_HOOK_INTERNAL_ERROR 4
262274

275+
/**
276+
* Finds the preferred slot containing the image.
277+
*
278+
* @param[in] state Boot loader status information.
279+
* @param[in] image Image, for which the slot should be found.
280+
* @param[out] active_slot Number of the preferred slot.
281+
*
282+
* @return 0 if a slot was requested;
283+
* BOOT_HOOK_REGULAR follow the normal execution path.
284+
*/
285+
int boot_find_next_slot_hook(struct boot_loader_state *state, uint8_t image, uint32_t *active_slot);
286+
263287
#endif /*H_BOOTUTIL_HOOKS*/

boot/bootutil/src/loader.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,7 +2818,12 @@ boot_load_and_validate_images(struct boot_loader_state *state)
28182818
break;
28192819
}
28202820

2821-
active_slot = find_slot_with_highest_version(state);
2821+
rc = BOOT_HOOK_FIND_SLOT_CALL(boot_find_next_slot_hook, BOOT_HOOK_REGULAR,
2822+
state, BOOT_CURR_IMG(state), &active_slot);
2823+
if (rc == BOOT_HOOK_REGULAR) {
2824+
active_slot = find_slot_with_highest_version(state);
2825+
}
2826+
28222827
if (active_slot == NO_ACTIVE_SLOT) {
28232828
BOOT_LOG_INF("No slot to load for image %d",
28242829
BOOT_CURR_IMG(state));
@@ -2865,7 +2870,12 @@ boot_load_and_validate_images(struct boot_loader_state *state)
28652870
break;
28662871
}
28672872

2868-
active_slot = find_slot_with_highest_version(state);
2873+
rc = BOOT_HOOK_FIND_SLOT_CALL(boot_find_next_slot_hook, BOOT_HOOK_REGULAR,
2874+
state, BOOT_CURR_IMG(state), &active_slot);
2875+
if (rc == BOOT_HOOK_REGULAR) {
2876+
active_slot = find_slot_with_highest_version(state);
2877+
}
2878+
28692879
if (active_slot == NO_ACTIVE_SLOT) {
28702880
BOOT_LOG_INF("No slot to load for image %d",
28712881
BOOT_CURR_IMG(state));

0 commit comments

Comments
 (0)