From 904d0c4608764fed128ed9077b860d2c83f26ad6 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 12 Apr 2023 17:11:32 +0000 Subject: [PATCH] bootutil: Add DirectXIP version of boot_set_next The commit adds DirectXIP version of bootutil boot_set_next function. The function is enabled by configuration option: MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP. Signed-off-by: Dominik Ermel --- boot/bootutil/src/bootutil_misc.c | 12 --- boot/bootutil/src/bootutil_public.c | 80 +++++++++++++++++++ .../include/mcuboot_config/mcuboot_config.h | 5 ++ 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c index e8007666..0caad7ff 100644 --- a/boot/bootutil/src/bootutil_misc.c +++ b/boot/bootutil/src/bootutil_misc.c @@ -294,18 +294,6 @@ boot_read_enc_key(const struct flash_area *fap, uint8_t slot, struct boot_status } #endif -int -boot_write_copy_done(const struct flash_area *fap) -{ - uint32_t off; - - off = boot_copy_done_off(fap); - BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)", - flash_area_get_id(fap), (unsigned long)off, - (unsigned long)(flash_area_get_off(fap) + off)); - return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); -} - int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) { diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index f54dd221..1a50c82e 100644 --- a/boot/bootutil/src/bootutil_public.c +++ b/boot/bootutil/src/bootutil_public.c @@ -476,6 +476,20 @@ static int flash_area_id_to_image(int id) return 0; } +int +boot_write_copy_done(const struct flash_area *fap) +{ + uint32_t off; + + off = boot_copy_done_off(fap); + BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)", + flash_area_get_id(fap), (unsigned long)off, + (unsigned long)(flash_area_get_off(fap) + off)); + return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); +} + + +#ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP int boot_set_next(const struct flash_area *fa, bool active, bool confirm) { @@ -547,6 +561,72 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm) return rc; } +#else +int +boot_set_next(const struct flash_area *fa, bool active, bool confirm) +{ + struct boot_swap_state slot_state; + int rc; + + if (active) { + /* The only way to set active slot for next boot is to confirm it, + * as DirectXIP will conclude that, since slot has not been confirmed + * last boot, it is bad and will remove it. + */ + confirm = true; + } + + rc = boot_read_swap_state(fa, &slot_state); + if (rc != 0) { + return rc; + } + + switch (slot_state.magic) { + case BOOT_MAGIC_UNSET: + /* Magic is needed for MCUboot to even consider booting an image */ + rc = boot_write_magic(fa); + if (rc != 0) { + break; + } + /* Pass */ + + case BOOT_MAGIC_GOOD: + if (confirm) { + if (slot_state.copy_done == BOOT_FLAG_UNSET) { + /* Magic is needed for DirectXIP to even try to boot application. + * DirectXIP will set copy-done flag before attempting to boot + * application. Next boot, application that has copy-done flag + * is expected to already have ok flag, otherwise it will be removed. + */ + rc = boot_write_copy_done(fa); + if (rc != 0) { + break; + } + } + + if (slot_state.image_ok == BOOT_FLAG_UNSET) { + rc = boot_write_image_ok(fa); + if (rc != 0) { + break; + } + } + } + break; + + case BOOT_MAGIC_BAD: + /* This image will not be boot next time anyway */ + rc = BOOT_EBADIMAGE; + break; + + default: + /* Something is not OK, this should never happen */ + assert(0); + rc = BOOT_EBADSTATUS; + } + + return rc; +} +#endif /* * This function is not used by the bootloader itself, but its required API diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index e20003bb..483d7a59 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -2,6 +2,7 @@ * Copyright (c) 2018 Open Source Foundries Limited * Copyright (c) 2019-2020 Arm Limited * Copyright (c) 2019-2020 Linaro Limited + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ @@ -273,6 +274,10 @@ #endif #endif +#ifdef CONFIG_MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP +#define MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP 1 +#endif + #if CONFIG_BOOT_WATCHDOG_FEED #if CONFIG_NRFX_WDT #include