Commit 2aa6da15 authored by Ederson de Souza's avatar Ederson de Souza Committed by Jamie
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: default avatarEderson de Souza <ederson.desouza@intel.com>
parent b3ed5cc6
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -82,6 +82,17 @@

#endif /* MCUBOOT_BOOT_GO_HOOKS  */

#ifdef MCUBOOT_FLASH_AREA_HOOKS

#define BOOT_HOOK_FLASH_AREA_CALL(f, ret_default, ...) \
    DO_HOOK_CALL(f, ret_default, __VA_ARGS__)

#else

#define BOOT_HOOK_FLASH_AREA_CALL(f, ret_default, ...) \
    HOOK_CALL_NOP(f, ret_default, __VA_ARGS__)

#endif /* MCUBOOT_FLASH_AREA_ID_HOOKS */

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

/**
 * Hook to implement custom action before retrieving flash area ID.
 *
 * @param image_index the index of the image pair
 * @param slot slot number
 * @param area_id the flash area ID to be populated
 *
 * @retval 0 the flash area ID was fetched successfully;
 *         BOOT_HOOK_REGULAR follow the normal execution path to get the flash
 *         area ID;
 *         otherwise an error-code value.
 */
int flash_area_id_from_multi_image_slot_hook(int image_index, int slot,
                                             int *area_id);

/**
 * Hook to implement custom action before retrieving flash area device ID.
 *
 * @param fa the flash area structure
 * @param device_id the device ID to be populated
 *
 * @retval 0 the device ID was fetched successfully;
 *         BOOT_HOOK_REGULAR follow the normal execution path to get the device
 *         ID;
 *         otherwise an error-code value.
 */
int flash_area_get_device_id_hook(const struct flash_area *fa,
                                  uint8_t *device_id);

#define BOOT_RESET_REQUEST_HOOK_BUSY		1
#define BOOT_RESET_REQUEST_HOOK_TIMEOUT		2
#define BOOT_RESET_REQUEST_HOOK_CHECK_FAILED	3
+18 −0
Original line number Diff line number Diff line
@@ -472,6 +472,17 @@ config BOOT_IMAGE_EXECUTABLE_RAM_SIZE
	default $(dt_chosen_reg_size_int,$(DT_CHOSEN_Z_SRAM),0)
endif

config FLASH_RUNTIME_SOURCES
	bool "Images are read from flash partitions defined at runtime"
	depends on SINGLE_APPLICATION_SLOT
	help
	  Instead of using information on the flash slots to decide which images
	  to load/update, the application provides the information from which
	  flash slot to load in runtime. This is useful when the application
	  reads the state for hardware straps or other sources to decide which
	  image to load. Usually, application will provide a boot_go_hook() to
	  decide which image to load.

config BOOT_ENCRYPTION_SUPPORT
	bool
	help
@@ -862,6 +873,13 @@ config BOOT_GO_HOOKS
	  MCUboot's boot_go routine. It is up to the project customization to
	  add required source files to the build.

config BOOT_FLASH_AREA_HOOKS
	bool "Enable hooks for overriding MCUboot's flash area routines"
	help
	  Allow to provide procedures for override or extend native
	  MCUboot's flash area routines. It is up to the project customization to
	  add required source files to the build.

config MCUBOOT_ACTION_HOOKS
	bool "Enable hooks for responding to MCUboot status changes"
	help
+19 −0
Original line number Diff line number Diff line
@@ -14,7 +14,9 @@
#include <flash_map_backend/flash_map_backend.h>
#include <sysflash/sysflash.h>

#include "bootutil/boot_hooks.h"
#include "bootutil/bootutil_log.h"
#include "bootutil/bootutil_public.h"

BOOT_LOG_MODULE_DECLARE(mcuboot);

@@ -58,6 +60,14 @@ int flash_device_base(uint8_t fd_id, uintptr_t *ret)
 */
int flash_area_id_from_multi_image_slot(int image_index, int slot)
{
    int rc, id;

    rc = BOOT_HOOK_FLASH_AREA_CALL(flash_area_id_from_multi_image_slot_hook,
                                   BOOT_HOOK_REGULAR, image_index, slot, &id);
    if (rc != BOOT_HOOK_REGULAR) {
        return id;
    }

    switch (slot) {
    case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
#if !defined(CONFIG_SINGLE_APPLICATION_SLOT)
@@ -138,6 +148,15 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)

uint8_t flash_area_get_device_id(const struct flash_area *fa)
{
    uint8_t device_id;
    int rc;

    rc = BOOT_HOOK_FLASH_AREA_CALL(flash_area_get_device_id_hook,
                                   BOOT_HOOK_REGULAR, fa, &device_id);
    if (rc != BOOT_HOOK_REGULAR) {
        return device_id;
    }

#if defined(CONFIG_ARM)
    return fa->fa_id;
#else
+4 −0
Original line number Diff line number Diff line
@@ -246,6 +246,10 @@
#define MCUBOOT_BOOT_GO_HOOKS
#endif

#ifdef CONFIG_BOOT_FLASH_AREA_HOOKS
#define MCUBOOT_FLASH_AREA_HOOKS
#endif

#ifdef CONFIG_MCUBOOT_VERIFY_IMG_ADDRESS
#define MCUBOOT_VERIFY_IMG_ADDRESS
#endif