Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image access hooks unavailable in single_loader.c #1928

Open
JPHutchins opened this issue Mar 25, 2024 · 2 comments
Open

Image access hooks unavailable in single_loader.c #1928

JPHutchins opened this issue Mar 25, 2024 · 2 comments

Comments

@JPHutchins
Copy link

Hello! It looks like Zephyr's single_loader.c does not implement the image access hooks. I have applied the simple patch below and would like feedback before submitting a PR.

Cheers,
JP

diff --git a/boot/zephyr/single_loader.c b/boot/zephyr/single_loader.c
index 86d5052..9e91181 100644
--- a/boot/zephyr/single_loader.c
+++ b/boot/zephyr/single_loader.c
@@ -8,6 +8,7 @@
 #include <assert.h>
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
+#include "bootutil/boot_hooks.h"
 #include "bootutil/bootutil_log.h"
 #include "bootutil/fault_injection_hardening.h"
 
@@ -102,7 +103,15 @@ boot_image_load_header(const struct flash_area *fa_p,
                        struct image_header *hdr)
 {
     uint32_t size;
-    int rc = flash_area_read(fa_p, 0, hdr, sizeof *hdr);
+
+    int rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
+                        0, 0, hdr);
+    if (rc != BOOT_HOOK_REGULAR)
+    {
+        return rc;
+    }
+
+    rc = flash_area_read(fa_p, 0, hdr, sizeof *hdr);
 
     if (rc != 0) {
         rc = BOOT_EFLASH;
@@ -453,12 +462,22 @@ boot_go(struct boot_rsp *rsp)
         goto out;
 
 #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
-    FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
+    BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
+                       fih_rc, 0, 0);
+    if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
+    {
+        FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
+    }
     if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
         goto out;
     }
 #elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
-    FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
+    BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
+                       fih_rc, 0, 0);
+    if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
+    {
+        FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
+    }
     if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
         goto out;
     }

Compare to:

rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
if (rc == BOOT_HOOK_REGULAR)
{
rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
}

And:

BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
fih_rc, BOOT_CURR_IMG(state), slot);
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
{
FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
}

@nordicjm
Copy link
Collaborator

Should probably be added to boot/zephyr/firmware_loader.c also, changes look OK

@JPHutchins
Copy link
Author

Should probably be added to boot/zephyr/firmware_loader.c also, changes look OK

Thanks, I'll take a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants