Skip to content

Commit

Permalink
zephyr: update XIP REVERT for stm32h7
Browse files Browse the repository at this point in the history
Add compatibility of XIP REVERT for stm32h7 soc
Add option to configure flash alignment of device

Signed-off-by: Nicolas Blondel <[email protected]>
  • Loading branch information
nblondelwitekio authored and lbonhomme-vossloh committed Dec 28, 2022
1 parent 13f6397 commit 1fb66a1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
6 changes: 6 additions & 0 deletions boot/bootutil/include/bootutil/bootutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ struct image_trailer {
uint8_t magic[BOOT_MAGIC_SZ];
};

/* Confirm a complete slot by writing the trailer flag
* copy_done, which must be done after a reboot for some board
* using XIP_REVERT.
*/
void confirm_slot_complete(uint32_t slot);

/* you must have pre-allocated all the entries within this structure */
fih_int boot_go(struct boot_rsp *rsp);
fih_int boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id);
Expand Down
38 changes: 34 additions & 4 deletions boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,37 @@ boot_select_or_erase(struct boot_loader_state *state)
BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
"value was neither 'set' nor 'unset', but 'bad'.");
}
}
flash_area_close(fap);
}

return rc;
}

void confirm_slot_complete(uint32_t slot)
{
struct boot_swap_state state;
const struct flash_area *fap;
int fa_id;
int rc;

fa_id = flash_area_id_from_image_slot(slot);
rc = flash_area_open(fa_id, &fap);
assert(rc == 0);

memset(&state, 0, sizeof(struct boot_swap_state));
rc = boot_read_swap_state(fap, &state);
assert(rc == 0);

if (state.magic == BOOT_MAGIC_GOOD &&
(state.copy_done != BOOT_FLAG_SET ||
state.image_ok == BOOT_FLAG_SET)) {

if (state.copy_done != BOOT_FLAG_SET) {
if (state.copy_done == BOOT_FLAG_BAD) {
BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
"value was neither 'set' nor 'unset', but 'bad'.");
}
/*
* Set the copy_done flag, indicating that the image has been
* selected to boot. It can be set in advance, before even
Expand All @@ -2403,16 +2434,15 @@ boot_select_or_erase(struct boot_loader_state *state)
rc = boot_write_copy_done(fap);
if (rc != 0) {
BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
"the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
"the %s slot.", (slot == BOOT_PRIMARY_SLOT) ?
"primary" : "secondary");
rc = 0;
}
}
flash_area_close(fap);
}

return rc;
flash_area_close(fap);
}

#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */

#ifdef MCUBOOT_RAM_LOAD
Expand Down
11 changes: 11 additions & 0 deletions boot/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ config SINGLE_APPLICATION_SLOT
uploading a new application overwrites the one that previously
occupied the area.

config BOOT_MAX_ALIGN
int "Flash write alignement in bytes"
default 8
help
Bytes alignment when performing writes into flash memory.

config BOOT_SOC_STM32H7XX
bool "Use booloader for STM32H7xx SOC"
default n
select USE_STM32_HAL_FLASH

choice BOOT_SIGNATURE_TYPE
prompt "Signature type"
default BOOT_SIGNATURE_TYPE_RSA
Expand Down
6 changes: 6 additions & 0 deletions boot/zephyr/include/mcuboot_config/mcuboot_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@

#endif /* CONFIG_SINGLE_APPLICATION_SLOT */

#ifdef CONFIG_BOOT_MAX_ALIGN
#define MCUBOOT_BOOT_MAX_ALIGN CONFIG_BOOT_MAX_ALIGN
#elif defined(CONFIG_MCUBOOT_BOOT_MAX_ALIGN)
#define MCUBOOT_BOOT_MAX_ALIGN CONFIG_MCUBOOT_BOOT_MAX_ALIGN
#endif

#ifdef CONFIG_LOG
#define MCUBOOT_HAVE_LOGGING 1
#endif
Expand Down
50 changes: 50 additions & 0 deletions boot/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const struct boot_uart_funcs boot_funcs = {
#include <arm_cleanup.h>
#endif

#ifdef CONFIG_SOC_STM32H743XX
#include <stm32h7xx.h>
#define STM32H743XX_SECONDARY_SLOT 0x00120000
#endif

/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
* replaced by CONFIG_LOG_MODE_MINIMAL.
*/
Expand Down Expand Up @@ -308,6 +313,33 @@ static void do_boot(struct boot_rsp *rsp)
}
#endif

#ifdef CONFIG_SOC_STM32H743XX
static void bank_swap(void)
{
BOOT_LOG_INF("Starting Bank-Swap");
HAL_StatusTypeDef status;

/* Unlock flash options writing */
status = HAL_FLASH_OB_Unlock();
__ASSERT(status == HAL_OK,
"Error: Can't unlock flash options writing.\n");

if(READ_BIT(FLASH->OPTSR_PRG, FLASH_OPTSR_SWAP_BANK_OPT) == 0) {
SET_BIT(FLASH->OPTSR_PRG, FLASH_OPTSR_SWAP_BANK_OPT);
} else {
CLEAR_BIT(FLASH->OPTSR_PRG, FLASH_OPTSR_SWAP_BANK_OPT);
}

/* Start the option byte change sequence */
status = HAL_FLASH_OB_Launch();
__ASSERT(status == HAL_OK,
"Error: Can't start option byte change sequence.\n");

BOOT_LOG_INF("Swap requested: %d",
READ_BIT(FLASH->OPTSR_PRG, FLASH_OPTSR_SWAP_BANK_OPT) == FLASH_OPTSR_SWAP_BANK_OPT);
}
#endif

#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
!defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
/* The log internal thread for log processing can't transfer log well as has too
Expand Down Expand Up @@ -571,6 +603,11 @@ void main(void)
rc = boot_console_init();
int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
uint32_t start = k_uptime_get_32();

#endif
#if defined(MCUBOOT_DIRECT_XIP_REVERT) || defined(MCUBOOT_DIRECT_XIP)
BOOT_LOG_INF("Bank-Swap bit: %d",
READ_BIT(FLASH->OPTCR, FLASH_OPTCR_SWAP_BANK) == FLASH_OPTCR_SWAP_BANK);
#endif

FIH_CALL(boot_go, fih_rc, &rsp);
Expand Down Expand Up @@ -604,7 +641,20 @@ void main(void)
mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);

ZEPHYR_BOOT_LOG_STOP();

#ifdef CONFIG_SOC_STM32H743XX
/* If requested to start on secondary slot, do a bank-swap */
if(rsp.br_image_off == STM32H743XX_SECONDARY_SLOT) {
bank_swap();
HAL_NVIC_SystemReset();
} else {
/* The primary image shall be indicated as complete */
confirm_slot_complete(0);
do_boot(&rsp);
}
#else
do_boot(&rsp);
#endif

mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);

Expand Down

0 comments on commit 1fb66a1

Please sign in to comment.