Commit 4f9c7304 authored by Andrzej Puzdrowski's avatar Andrzej Puzdrowski Committed by David Brown
Browse files

boot_serial: added hooks for mcuboot image access operations



Added hook for: read image header, validate the image and hook
which is called after image was uploaded completely.

Signed-off-by: default avatarAndrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
parent dea293ad
Loading
Loading
Loading
Loading
+32 −8
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@
#endif

#include "serial_recovery_cbor.h"
#include "bootutil/boot_hooks.h"

BOOT_LOG_MODULE_DECLARE(mcuboot);

@@ -187,15 +188,32 @@ bs_list(char *buf, int len)
                continue;
            }

            int rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
                                    BOOT_HOOK_REGULAR, image_index, slot, &hdr);
            if (rc == BOOT_HOOK_REGULAR)
            {
                flash_area_read(fap, 0, &hdr, sizeof(hdr));
            }

            fih_int fih_rc = FIH_FAILURE;

            if (hdr.ih_magic == IMAGE_MAGIC)
            {
                BOOT_HOOK_CALL_FIH(boot_image_check_hook,
                                   fih_int_encode(BOOT_HOOK_REGULAR),
                                   fih_rc, image_index, slot);
                if (fih_eq(fih_rc, BOOT_HOOK_REGULAR))
                {
                    FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
                                    NULL, 0, NULL);
                }
            }

            if (hdr.ih_magic != IMAGE_MAGIC ||
              bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
                                    NULL, 0, NULL)) {
            flash_area_close(fap);

            if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
                continue;
            }
            flash_area_close(fap);

            map_start_encode(&cbor_state, 20);

@@ -376,8 +394,8 @@ bs_upload(char *buf, int len)

    if (rc == 0) {
        curr_off += img_blen;
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
        if (curr_off == img_size) {
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
            /* get the last sector offset */
            rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
            if (rc) {
@@ -397,8 +415,14 @@ bs_upload(char *buf, int len)
                    goto out;
                }
            }
        }
#endif
            rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
                                img_size);
            if (rc) {
                BOOT_LOG_ERR("Error %d post upload hook", rc);
                goto out;
            }
        }
    } else {
    out_invalid_data:
        rc = MGMT_ERR_EINVAL;
+18 −0
Original line number Diff line number Diff line
@@ -119,4 +119,22 @@ int boot_perform_update_hook(int img_index, struct image_header *img_head,
int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
                               size_t size);

/** Hook for implement image's post recovery upload action
 *
 * This hook is for implement action which might be done right after image was
 * copied to the primary slot. This hook is called in serial recovery upload
 * operation.
 *
 * @param img_index the index of the image pair
 * @param area the flash area of the primary image.
 * @param size size of copied image.
 *
 * @retval 0: success, mcuboot will follow normal code execution flow after
 *            execution of this call.
 *         non-zero: an error, will be transferred as part of comand response
 *            as "rc" entry.
 */
int boot_serial_uploaded_hook(int img_index, const struct flash_area *area,
                              size_t size);

#endif /*H_BOOTUTIL_HOOKS*/