Commit 9a605b69 authored by Andrzej Puzdrowski's avatar Andrzej Puzdrowski Committed by David Brown
Browse files

zephyr: add cleanup ARM core before boot

This patch is needed as MCUBoot should be able to chain-load any
application, not only these built using zephyr.

Introduced cleanup on ARM core control register.
Might be required as for instance the application assumes
that it starts with thread mode configured as by default, not
according to zephyr-rtos configuration.

MCUBoot disables interrupt before application chain-load used
basepr register. This Patch introduce additional celenup on
NVIC register.

Signed-off-by: default avatarAndrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
parent 05dc5ddb
Loading
Loading
Loading
Loading
+5 −0
Changes for boot/zephyr/CMakeLists.txt: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -228,3 +228,8 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
  zephyr_library_sources(${GENERATED_PUBKEY})
endif()

if(CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
zephyr_library_sources(
  ${BOOT_DIR}/zephyr/arm_cleanup.c
)
endif()
+5 −0
Changes for boot/zephyr/Kconfig: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -120,6 +120,11 @@ config BOOT_SIGNATURE_KEY_FILE
	  with the public key information will be written in a format expected by
	  MCUboot.

config MCUBOOT_CLEANUP_ARM_CORE
	bool "Perform core cleanup before chain-load the application"
	depends on CPU_CORTEX_M
	default y

config MBEDTLS_CFG_FILE
	default "mcuboot-mbedtls-cfg.h"

+22 −0
Changes for boot/zephyr/arm_cleanup.c: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>

void cleanup_arm_nvic(void) {
	/* Allow any pending interrupts to be recognized */
	__ISB();
	__disable_irq();

	/* Disable NVIC interrupts */
	for (u8_t i = 0; i < ARRAY_SIZE(NVIC->ICER); i++) {
		NVIC->ICER[i] = 0xFFFFFFFF;
	}
	/* Clear pending NVIC interrupts */
	for (u8_t i = 0; i < ARRAY_SIZE(NVIC->ICPR); i++) {
		NVIC->ICPR[i] = 0xFFFFFFFF;
	}
}
+15 −0
Changes for boot/zephyr/include/arm_cleanup.h: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef H_ARM_CLEANUP_
#define H_ARM_CLEANUP_

/**
 * Cleanup interrupt priority and interupt enable registers.
 */
void cleanup_arm_nvic(void);
#endif
+10 −0
Changes for boot/zephyr/main.c: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -44,6 +44,10 @@ const struct boot_uart_funcs boot_funcs = {
#include <usb/class/usb_dfu.h>
#endif

#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
#include <arm_cleanup.h>
#endif

#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE)
#ifdef CONFIG_LOG_PROCESS_THREAD
#warning "The log internal thread for log processing can't transfer the log"\
@@ -125,8 +129,14 @@ static void do_boot(struct boot_rsp *rsp)
#ifdef CONFIG_USB
    /* Disable the USB to prevent it from firing interrupts */
    usb_disable();
#endif
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
    cleanup_arm_nvic(); /* cleanup NVIC registers */
#endif
    __set_MSP(vt->msp);
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
    __set_CONTROL(0x00); /* application will configures core on its own */
#endif
    ((void (*)(void))vt->reset)();
}