Skip to content

Commit b3ed5cc

Browse files
edersondisouzanordicjm
authored andcommitted
boot: New boot_go hook
This new hook allows external modules to override the choice of which image to boot - it can be used, for instance, to load an image based on some GPIO state. If the hook returns FIH_BOOT_HOOK_REGULAR, then normal platform boot_go() will be run to choose image to boot. This hook is shielded by a different configuration than other hooks - MCUBOOT_BOOT_GO_HOOKS, so that it can be enabled independently from other available hooks. Support for this new hook was added to Zephyr port. Signed-off-by: Ederson de Souza <[email protected]>
1 parent c9aa7fd commit b3ed5cc

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

boot/bootutil/include/bootutil/boot_hooks.h

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,54 @@
3434
#ifndef H_BOOTUTIL_HOOKS
3535
#define H_BOOTUTIL_HOOKS
3636

37-
#ifdef MCUBOOT_IMAGE_ACCESS_HOOKS
37+
#include "bootutil/bootutil.h"
38+
#include "bootutil/fault_injection_hardening.h"
3839

39-
#define BOOT_HOOK_CALL(f, ret_default, ...) f(__VA_ARGS__)
40+
#define DO_HOOK_CALL(f, ret_default, ...) \
41+
f(__VA_ARGS__)
4042

41-
#define BOOT_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
43+
#define DO_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
4244
do { \
4345
FIH_CALL(f, fih_rc, __VA_ARGS__); \
4446
} while(0);
4547

46-
#else
47-
48-
#define BOOT_HOOK_CALL(f, ret_default, ...) ret_default
48+
#define HOOK_CALL_NOP(f, ret_default, ...) ret_default
4949

50-
#define BOOT_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
50+
#define HOOK_CALL_FIH_NOP(f, fih_ret_default, fih_rc, ...) \
5151
do { \
5252
fih_rc = fih_ret_default; \
5353
} while(0);
5454

55-
#endif
55+
#ifdef MCUBOOT_IMAGE_ACCESS_HOOKS
56+
57+
#define BOOT_HOOK_CALL(f, ret_default, ...) \
58+
DO_HOOK_CALL(f, ret_default, __VA_ARGS__)
59+
60+
#define BOOT_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
61+
DO_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, __VA_ARGS__)
62+
63+
#else
64+
65+
#define BOOT_HOOK_CALL(f, ret_default, ...) \
66+
HOOK_CALL_NOP(f, ret_default, __VA_ARGS__)
67+
68+
#define BOOT_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
69+
HOOK_CALL_FIH_NOP(f, fih_ret_default, fih_rc, __VA_ARGS__)
70+
71+
#endif /* MCUBOOT_IMAGE_ACCESS_HOOKS */
72+
73+
#ifdef MCUBOOT_BOOT_GO_HOOKS
74+
75+
#define BOOT_HOOK_GO_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
76+
DO_HOOK_CALL_FIH(f, fih_ret_default, fih_rc, __VA_ARGS__);
77+
78+
#else
79+
80+
#define BOOT_HOOK_GO_CALL_FIH(f, fih_ret_default, fih_rc, ...) \
81+
HOOK_CALL_FIH_NOP(f, fih_ret_default, fih_rc, __VA_ARGS__)
82+
83+
#endif /* MCUBOOT_BOOT_GO_HOOKS */
84+
5685

5786
/** Hook for provide image header data.
5887
*
@@ -173,6 +202,19 @@ int boot_img_install_stat_hook(int image_index, int slot,
173202
*/
174203
int boot_reset_request_hook(bool force);
175204

205+
/**
206+
* Hook to implement custom action before boot_go() function.
207+
*
208+
* @param rsp boot response structure.
209+
*
210+
* @retval FIH_SUCCESS: boot_go() should be skipped, boot response is already
211+
* filled.
212+
* FIH_FAILURE: boot_go() should be skipped, boot response is already
213+
* filled with error.
214+
* FIH_BOOT_HOOK_REGULAR: follow the normal execution path.
215+
*/
216+
fih_ret boot_go_hook(struct boot_rsp *rsp);
217+
176218
#define BOOT_RESET_REQUEST_HOOK_BUSY 1
177219
#define BOOT_RESET_REQUEST_HOOK_TIMEOUT 2
178220
#define BOOT_RESET_REQUEST_HOOK_CHECK_FAILED 3

boot/zephyr/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,13 @@ config BOOT_IMAGE_ACCESS_HOOKS
855855
update. It is up to the project customization to add required source
856856
files to the build.
857857

858+
config BOOT_GO_HOOKS
859+
bool "Enable hooks for overriding MCUBOOT's boot_go routine"
860+
help
861+
Allow to provide procedures for override or extend native
862+
MCUboot's boot_go routine. It is up to the project customization to
863+
add required source files to the build.
864+
858865
config MCUBOOT_ACTION_HOOKS
859866
bool "Enable hooks for responding to MCUboot status changes"
860867
help

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@
242242
#define MCUBOOT_IMAGE_ACCESS_HOOKS
243243
#endif
244244

245+
#ifdef CONFIG_BOOT_GO_HOOKS
246+
#define MCUBOOT_BOOT_GO_HOOKS
247+
#endif
248+
245249
#ifdef CONFIG_MCUBOOT_VERIFY_IMG_ADDRESS
246250
#define MCUBOOT_VERIFY_IMG_ADDRESS
247251
#endif

boot/zephyr/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "bootutil/bootutil_log.h"
4242
#include "bootutil/image.h"
4343
#include "bootutil/bootutil.h"
44+
#include "bootutil/boot_hooks.h"
4445
#include "bootutil/fault_injection_hardening.h"
4546
#include "bootutil/mcuboot_status.h"
4647
#include "flash_map_backend/flash_map_backend.h"
@@ -525,7 +526,10 @@ int main(void)
525526
#endif
526527
#endif
527528

528-
FIH_CALL(boot_go, fih_rc, &rsp);
529+
BOOT_HOOK_GO_CALL_FIH(boot_go_hook, FIH_BOOT_HOOK_REGULAR, fih_rc, &rsp);
530+
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR)) {
531+
FIH_CALL(boot_go, fih_rc, &rsp);
532+
}
529533

530534
#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
531535
if (io_detect_boot_mode()) {

0 commit comments

Comments
 (0)