2017-01-07 02:16:53 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012-2014 Wind River Systems, Inc.
|
2020-09-30 14:58:48 +08:00
|
|
|
* Copyright (c) 2020 Arm Limited
|
2023-02-22 17:11:57 +08:00
|
|
|
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
|
2017-01-07 02:16:53 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 11:15:29 +08:00
|
|
|
#include <assert.h>
|
2022-05-09 18:13:12 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-09 18:10:05 +08:00
|
|
|
#include <zephyr/devicetree.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/sys/__assert.h>
|
|
|
|
#include <zephyr/drivers/flash.h>
|
|
|
|
#include <zephyr/drivers/timer/system_timer.h>
|
|
|
|
#include <zephyr/usb/usb_device.h>
|
2018-06-29 04:27:40 +08:00
|
|
|
#include <soc.h>
|
2022-05-09 18:10:05 +08:00
|
|
|
#include <zephyr/linker/linker-defs.h>
|
2017-01-07 02:16:53 +08:00
|
|
|
|
2024-03-19 16:18:44 +08:00
|
|
|
#if defined(CONFIG_BOOT_DISABLE_CACHES)
|
|
|
|
#include <zephyr/cache.h>
|
|
|
|
#endif
|
|
|
|
|
2023-09-05 22:20:50 +08:00
|
|
|
#if defined(CONFIG_ARM)
|
2023-06-30 15:58:41 +08:00
|
|
|
#include <cmsis_core.h>
|
2023-09-05 22:20:50 +08:00
|
|
|
#endif
|
2022-07-29 23:16:09 +08:00
|
|
|
|
2023-08-16 14:33:24 +08:00
|
|
|
#include "io/io.h"
|
2017-03-20 23:03:41 +08:00
|
|
|
#include "target.h"
|
|
|
|
|
2017-02-01 07:20:02 +08:00
|
|
|
#include "bootutil/bootutil_log.h"
|
2017-01-19 20:22:35 +08:00
|
|
|
#include "bootutil/image.h"
|
|
|
|
#include "bootutil/bootutil.h"
|
2020-09-30 14:58:48 +08:00
|
|
|
#include "bootutil/fault_injection_hardening.h"
|
2022-03-23 19:57:03 +08:00
|
|
|
#include "bootutil/mcuboot_status.h"
|
2018-04-12 18:42:49 +08:00
|
|
|
#include "flash_map_backend/flash_map_backend.h"
|
2017-01-19 20:22:35 +08:00
|
|
|
|
2023-01-24 00:54:36 +08:00
|
|
|
/* Check if Espressif target is supported */
|
2024-03-04 21:08:30 +08:00
|
|
|
#ifdef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
|
2023-01-24 00:54:36 +08:00
|
|
|
|
|
|
|
#include <bootloader_init.h>
|
|
|
|
#include <esp_loader.h>
|
|
|
|
|
|
|
|
#define IMAGE_INDEX_0 0
|
|
|
|
#define IMAGE_INDEX_1 1
|
|
|
|
|
|
|
|
#define PRIMARY_SLOT 0
|
|
|
|
#define SECONDARY_SLOT 1
|
|
|
|
|
|
|
|
#define IMAGE0_PRIMARY_START_ADDRESS \
|
|
|
|
DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 0)
|
|
|
|
#define IMAGE0_PRIMARY_SIZE \
|
|
|
|
DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 1)
|
|
|
|
|
|
|
|
#define IMAGE1_PRIMARY_START_ADDRESS \
|
|
|
|
DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 0)
|
|
|
|
#define IMAGE1_PRIMARY_SIZE \
|
|
|
|
DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 1)
|
|
|
|
|
2024-03-04 21:08:30 +08:00
|
|
|
#endif /* CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
|
2023-01-24 00:54:36 +08:00
|
|
|
|
2017-09-08 22:49:14 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_SERIAL
|
2018-06-06 19:18:54 +08:00
|
|
|
#include "boot_serial/boot_serial.h"
|
|
|
|
#include "serial_adapter/serial_adapter.h"
|
|
|
|
|
|
|
|
const struct boot_uart_funcs boot_funcs = {
|
|
|
|
.read = console_read,
|
|
|
|
.write = console_write
|
|
|
|
};
|
2017-09-08 22:49:14 +08:00
|
|
|
#endif
|
|
|
|
|
2020-11-14 10:51:27 +08:00
|
|
|
#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
|
2022-05-09 18:10:05 +08:00
|
|
|
#include <zephyr/usb/class/usb_dfu.h>
|
2019-02-20 16:38:52 +08:00
|
|
|
#endif
|
|
|
|
|
2020-03-16 20:34:30 +08:00
|
|
|
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
|
|
|
|
#include <arm_cleanup.h>
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 05:50:38 +08:00
|
|
|
/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
|
|
|
|
* replaced by CONFIG_LOG_MODE_MINIMAL.
|
|
|
|
*/
|
2021-02-26 22:26:48 +08:00
|
|
|
#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
|
|
|
|
#define ZEPHYR_LOG_MODE_MINIMAL 1
|
|
|
|
#endif
|
2021-01-27 05:50:38 +08:00
|
|
|
|
2022-03-11 21:24:07 +08:00
|
|
|
/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
|
|
|
|
* replaced by CONFIG_LOG_MODE_IMMEDIATE.
|
|
|
|
*/
|
|
|
|
#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
|
|
|
|
#define ZEPHYR_LOG_MODE_IMMEDIATE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
|
2021-02-26 22:26:48 +08:00
|
|
|
!defined(ZEPHYR_LOG_MODE_MINIMAL)
|
2020-02-17 20:25:32 +08:00
|
|
|
#ifdef CONFIG_LOG_PROCESS_THREAD
|
|
|
|
#warning "The log internal thread for log processing can't transfer the log"\
|
|
|
|
"well for MCUBoot."
|
|
|
|
#else
|
2022-05-09 18:10:05 +08:00
|
|
|
#include <zephyr/logging/log_ctrl.h>
|
2020-02-17 20:25:32 +08:00
|
|
|
|
2020-03-24 14:50:32 +08:00
|
|
|
#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
|
2020-02-17 20:25:32 +08:00
|
|
|
|
|
|
|
/* log are processing in custom routine */
|
2020-02-25 19:51:26 +08:00
|
|
|
K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
|
2020-02-17 20:25:32 +08:00
|
|
|
struct k_thread boot_log_thread;
|
2020-02-24 18:50:19 +08:00
|
|
|
volatile bool boot_log_stop = false;
|
|
|
|
K_SEM_DEFINE(boot_log_sem, 1, 1);
|
2020-02-17 20:25:32 +08:00
|
|
|
|
|
|
|
/* log processing need to be initalized by the application */
|
|
|
|
#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
|
2020-02-24 18:50:19 +08:00
|
|
|
#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
|
2020-02-17 20:25:32 +08:00
|
|
|
#endif /* CONFIG_LOG_PROCESS_THREAD */
|
|
|
|
#else
|
|
|
|
/* synchronous log mode doesn't need to be initalized by the application */
|
|
|
|
#define ZEPHYR_BOOT_LOG_START() do { } while (false)
|
2020-02-24 18:50:19 +08:00
|
|
|
#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
|
2022-03-11 21:24:07 +08:00
|
|
|
#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
|
|
|
|
* !defined(ZEPHYR_LOG_MODE_MINIMAL)
|
|
|
|
*/
|
2020-02-17 20:25:32 +08:00
|
|
|
|
2021-06-22 16:00:22 +08:00
|
|
|
BOOT_LOG_MODULE_REGISTER(mcuboot);
|
2018-11-20 17:59:59 +08:00
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
void os_heap_init(void);
|
|
|
|
|
|
|
|
#if defined(CONFIG_ARM)
|
2020-06-17 21:06:47 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SW_VECTOR_RELAY
|
|
|
|
extern void *_vector_table_pointer;
|
|
|
|
#endif
|
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
struct arm_vector_table {
|
2017-04-12 07:33:30 +08:00
|
|
|
uint32_t msp;
|
|
|
|
uint32_t reset;
|
2017-01-07 02:16:53 +08:00
|
|
|
};
|
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
static void do_boot(struct boot_rsp *rsp)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
struct arm_vector_table *vt;
|
|
|
|
|
|
|
|
/* The beginning of the image is the ARM vector table, containing
|
|
|
|
* the initial stack pointer address and the reset vector
|
|
|
|
* consecutively. Manually set the stack pointer and jump into the
|
|
|
|
* reset vector
|
|
|
|
*/
|
2022-09-02 04:47:01 +08:00
|
|
|
#ifdef CONFIG_BOOT_RAM_LOAD
|
|
|
|
/* Get ram address for image */
|
|
|
|
vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
|
|
|
|
#else
|
|
|
|
int rc;
|
2024-02-07 11:06:54 +08:00
|
|
|
const struct flash_area *fap;
|
|
|
|
static uint32_t dst[2];
|
2022-09-02 04:47:01 +08:00
|
|
|
|
|
|
|
/* Jump to flash image */
|
2024-02-07 11:06:54 +08:00
|
|
|
rc = flash_area_open(rsp->br_flash_dev_id, &fap);
|
2017-05-02 11:15:29 +08:00
|
|
|
assert(rc == 0);
|
|
|
|
|
2024-02-07 11:06:54 +08:00
|
|
|
rc = flash_area_read(fap, rsp->br_hdr->ih_hdr_size, dst, sizeof(dst));
|
|
|
|
assert(rc == 0);
|
|
|
|
#ifndef CONFIG_ASSERT
|
|
|
|
/* Enter a lock up as asserts are disabled */
|
|
|
|
if (rc != 0) {
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
flash_area_close(fap);
|
|
|
|
|
|
|
|
vt = (struct arm_vector_table *)dst;
|
2022-09-02 04:47:01 +08:00
|
|
|
#endif
|
2020-08-19 14:57:11 +08:00
|
|
|
|
2022-12-21 22:52:53 +08:00
|
|
|
if (IS_ENABLED(CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)) {
|
|
|
|
sys_clock_disable();
|
|
|
|
}
|
2021-12-14 21:09:02 +08:00
|
|
|
|
2021-08-27 19:30:07 +08:00
|
|
|
#ifdef CONFIG_USB_DEVICE_STACK
|
2018-07-20 17:39:57 +08:00
|
|
|
/* Disable the USB to prevent it from firing interrupts */
|
|
|
|
usb_disable();
|
2020-03-16 20:34:30 +08:00
|
|
|
#endif
|
|
|
|
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
|
|
|
|
cleanup_arm_nvic(); /* cleanup NVIC registers */
|
2020-10-22 21:14:48 +08:00
|
|
|
|
2024-03-19 16:18:44 +08:00
|
|
|
#if defined(CONFIG_BOOT_DISABLE_CACHES)
|
|
|
|
/* Flush and disable instruction/data caches before chain-loading the application */
|
|
|
|
(void)sys_cache_instr_flush_all();
|
|
|
|
(void)sys_cache_data_flush_all();
|
|
|
|
sys_cache_instr_disable();
|
|
|
|
sys_cache_data_disable();
|
2020-10-22 21:14:48 +08:00
|
|
|
#endif
|
|
|
|
|
2020-12-08 21:40:19 +08:00
|
|
|
#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
|
2020-10-22 21:14:48 +08:00
|
|
|
z_arm_clear_arm_mpu_config();
|
2018-07-20 17:39:57 +08:00
|
|
|
#endif
|
2020-06-17 21:06:47 +08:00
|
|
|
|
2020-10-01 20:52:38 +08:00
|
|
|
#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
|
|
|
|
defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
|
|
|
|
/* Reset limit registers to avoid inflicting stack overflow on image
|
|
|
|
* being booted.
|
|
|
|
*/
|
|
|
|
__set_PSPLIM(0);
|
|
|
|
__set_MSPLIM(0);
|
|
|
|
#endif
|
|
|
|
|
2021-08-20 22:33:55 +08:00
|
|
|
#else
|
|
|
|
irq_lock();
|
2020-10-22 21:14:48 +08:00
|
|
|
#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
|
|
|
|
|
2020-06-17 21:06:47 +08:00
|
|
|
#ifdef CONFIG_BOOT_INTR_VEC_RELOC
|
2020-07-17 19:18:15 +08:00
|
|
|
#if defined(CONFIG_SW_VECTOR_RELAY)
|
|
|
|
_vector_table_pointer = vt;
|
|
|
|
#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
|
|
|
|
SCB->VTOR = (uint32_t)__vector_relay_table;
|
|
|
|
#endif
|
|
|
|
#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
|
|
|
|
SCB->VTOR = (uint32_t)vt;
|
|
|
|
#endif /* CONFIG_SW_VECTOR_RELAY */
|
|
|
|
#else /* CONFIG_BOOT_INTR_VEC_RELOC */
|
|
|
|
#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
|
|
|
|
_vector_table_pointer = _vector_start;
|
|
|
|
SCB->VTOR = (uint32_t)__vector_relay_table;
|
2020-06-17 21:06:47 +08:00
|
|
|
#endif
|
2020-07-17 19:18:15 +08:00
|
|
|
#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
|
2020-06-17 21:06:47 +08:00
|
|
|
|
2018-03-14 05:56:38 +08:00
|
|
|
__set_MSP(vt->msp);
|
2020-03-16 20:34:30 +08:00
|
|
|
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
|
|
|
|
__set_CONTROL(0x00); /* application will configures core on its own */
|
2020-10-29 16:16:50 +08:00
|
|
|
__ISB();
|
2020-03-16 20:34:30 +08:00
|
|
|
#endif
|
2017-04-12 07:33:30 +08:00
|
|
|
((void (*)(void))vt->reset)();
|
2017-03-03 05:39:06 +08:00
|
|
|
}
|
2018-12-09 16:02:01 +08:00
|
|
|
|
2023-01-24 00:54:36 +08:00
|
|
|
#elif defined(CONFIG_XTENSA) || defined(CONFIG_RISCV)
|
|
|
|
|
2024-03-04 21:08:30 +08:00
|
|
|
#ifndef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
|
2023-01-24 00:54:36 +08:00
|
|
|
|
2018-12-09 16:02:01 +08:00
|
|
|
#define SRAM_BASE_ADDRESS 0xBE030000
|
|
|
|
|
|
|
|
static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
|
|
|
|
{
|
|
|
|
const struct flash_area *fap;
|
|
|
|
int area_id;
|
|
|
|
int rc;
|
|
|
|
unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
|
|
|
|
|
|
|
|
BOOT_LOG_INF("Copying image to SRAM");
|
|
|
|
|
|
|
|
area_id = flash_area_id_from_image_slot(slot);
|
|
|
|
rc = flash_area_open(area_id, &fap);
|
|
|
|
if (rc != 0) {
|
2019-09-06 19:52:35 +08:00
|
|
|
BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
|
2018-12-09 16:02:01 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
|
|
|
|
if (rc != 0) {
|
2019-09-06 19:52:35 +08:00
|
|
|
BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
|
2018-12-09 16:02:01 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
flash_area_close(fap);
|
|
|
|
}
|
2024-03-04 21:08:30 +08:00
|
|
|
#endif /* !CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
|
2018-12-09 16:02:01 +08:00
|
|
|
|
|
|
|
/* Entry point (.ResetVector) is at the very beginning of the image.
|
|
|
|
* Simply copy the image to a suitable location and jump there.
|
|
|
|
*/
|
|
|
|
static void do_boot(struct boot_rsp *rsp)
|
|
|
|
{
|
|
|
|
void *start;
|
|
|
|
|
|
|
|
BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
|
|
|
|
BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
|
|
|
|
|
2024-03-04 21:08:30 +08:00
|
|
|
#ifdef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
|
2023-01-24 00:54:36 +08:00
|
|
|
int slot = (rsp->br_image_off == IMAGE0_PRIMARY_START_ADDRESS) ?
|
|
|
|
PRIMARY_SLOT : SECONDARY_SLOT;
|
|
|
|
/* Load memory segments and start from entry point */
|
|
|
|
start_cpu0_image(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size);
|
|
|
|
#else
|
2018-12-09 16:02:01 +08:00
|
|
|
/* Copy from the flash to HP SRAM */
|
|
|
|
copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
|
|
|
|
|
|
|
|
/* Jump to entry point */
|
|
|
|
start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
|
|
|
|
((void (*)(void))start)();
|
2024-03-04 21:08:30 +08:00
|
|
|
#endif /* CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
|
2018-12-09 16:02:01 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
#else
|
|
|
|
/* Default: Assume entry point is at the very beginning of the image. Simply
|
|
|
|
* lock interrupts and jump there. This is the right thing to do for X86 and
|
|
|
|
* possibly other platforms.
|
|
|
|
*/
|
|
|
|
static void do_boot(struct boot_rsp *rsp)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
void *start;
|
2022-07-21 17:45:45 +08:00
|
|
|
|
2022-04-07 11:26:28 +08:00
|
|
|
#if defined(MCUBOOT_RAM_LOAD)
|
|
|
|
start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
|
|
|
|
#else
|
|
|
|
uintptr_t flash_base;
|
2017-05-02 11:15:29 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
|
|
|
|
assert(rc == 0);
|
2017-03-03 05:39:06 +08:00
|
|
|
|
2017-05-02 11:15:29 +08:00
|
|
|
start = (void *)(flash_base + rsp->br_image_off +
|
|
|
|
rsp->br_hdr->ih_hdr_size);
|
2022-04-07 11:26:28 +08:00
|
|
|
#endif
|
2017-03-03 05:39:06 +08:00
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
/* Lock interrupts and dive into the entry point */
|
|
|
|
irq_lock();
|
|
|
|
((void (*)(void))start)();
|
2017-03-03 05:39:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
2017-01-07 02:16:53 +08:00
|
|
|
|
2022-03-11 21:24:07 +08:00
|
|
|
#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
|
2021-02-26 22:26:48 +08:00
|
|
|
!defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
|
2020-02-17 20:25:32 +08:00
|
|
|
/* The log internal thread for log processing can't transfer log well as has too
|
|
|
|
* low priority.
|
|
|
|
* Dedicated thread for log processing below uses highest application
|
|
|
|
* priority. This allows to transmit all logs without adding k_sleep/k_yield
|
|
|
|
* anywhere else int the code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* most simple log processing theread */
|
|
|
|
void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
|
|
|
|
{
|
|
|
|
(void)dummy1;
|
|
|
|
(void)dummy2;
|
|
|
|
(void)dummy3;
|
|
|
|
|
2022-07-21 17:45:45 +08:00
|
|
|
log_init();
|
2020-02-17 20:25:32 +08:00
|
|
|
|
2022-07-21 17:45:45 +08:00
|
|
|
while (1) {
|
2022-07-21 17:49:32 +08:00
|
|
|
#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
|
|
|
|
/* support Zephyr legacy logging implementation before commit c5f2cde */
|
2022-07-21 17:45:45 +08:00
|
|
|
if (log_process(false) == false) {
|
2022-07-21 17:49:32 +08:00
|
|
|
#else
|
|
|
|
if (log_process() == false) {
|
|
|
|
#endif
|
2022-07-21 17:45:45 +08:00
|
|
|
if (boot_log_stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 18:50:19 +08:00
|
|
|
|
2022-07-21 17:45:45 +08:00
|
|
|
k_sem_give(&boot_log_sem);
|
2020-02-17 20:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void zephyr_boot_log_start(void)
|
|
|
|
{
|
2022-07-21 17:45:45 +08:00
|
|
|
/* start logging thread */
|
|
|
|
k_thread_create(&boot_log_thread, boot_log_stack,
|
|
|
|
K_THREAD_STACK_SIZEOF(boot_log_stack),
|
|
|
|
boot_log_thread_func, NULL, NULL, NULL,
|
|
|
|
K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
|
|
|
|
BOOT_LOG_PROCESSING_INTERVAL);
|
|
|
|
|
|
|
|
k_thread_name_set(&boot_log_thread, "logging");
|
2020-02-17 20:25:32 +08:00
|
|
|
}
|
2020-02-24 18:50:19 +08:00
|
|
|
|
|
|
|
void zephyr_boot_log_stop(void)
|
|
|
|
{
|
|
|
|
boot_log_stop = true;
|
|
|
|
|
|
|
|
/* wait until log procesing thread expired
|
|
|
|
* This can be reworked using a thread_join() API once a such will be
|
|
|
|
* available in zephyr.
|
|
|
|
* see https://github.com/zephyrproject-rtos/zephyr/issues/21500
|
|
|
|
*/
|
|
|
|
(void)k_sem_take(&boot_log_sem, K_FOREVER);
|
|
|
|
}
|
2022-03-11 21:24:07 +08:00
|
|
|
#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
|
|
|
|
* !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
|
|
|
|
*/
|
2020-02-17 20:25:32 +08:00
|
|
|
|
2023-04-04 20:50:43 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_SERIAL
|
|
|
|
static void boot_serial_enter()
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
2023-12-05 21:26:22 +08:00
|
|
|
io_led_set(1);
|
2023-04-04 20:50:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
|
|
|
|
|
|
|
|
BOOT_LOG_INF("Enter the serial recovery mode");
|
|
|
|
rc = boot_console_init();
|
|
|
|
__ASSERT(rc == 0, "Error initializing boot console.\n");
|
|
|
|
boot_serial_start(&boot_funcs);
|
|
|
|
__ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-28 00:56:10 +08:00
|
|
|
int main(void)
|
2017-01-07 02:16:53 +08:00
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
struct boot_rsp rsp;
|
|
|
|
int rc;
|
2022-11-24 23:49:56 +08:00
|
|
|
FIH_DECLARE(fih_rc, FIH_FAILURE);
|
2017-04-12 07:33:30 +08:00
|
|
|
|
2023-04-11 15:43:10 +08:00
|
|
|
MCUBOOT_WATCHDOG_SETUP();
|
2020-10-13 19:52:15 +08:00
|
|
|
MCUBOOT_WATCHDOG_FEED();
|
|
|
|
|
2021-02-17 23:18:01 +08:00
|
|
|
#if !defined(MCUBOOT_DIRECT_XIP)
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_INF("Starting bootloader");
|
2021-02-17 23:18:01 +08:00
|
|
|
#else
|
|
|
|
BOOT_LOG_INF("Starting Direct-XIP bootloader");
|
|
|
|
#endif
|
2017-04-12 07:33:30 +08:00
|
|
|
|
2021-01-22 08:34:05 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
|
|
|
/* LED init */
|
2023-12-05 20:41:59 +08:00
|
|
|
io_led_init();
|
2021-01-22 08:34:05 +08:00
|
|
|
#endif
|
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
os_heap_init();
|
|
|
|
|
2020-02-17 20:25:32 +08:00
|
|
|
ZEPHYR_BOOT_LOG_START();
|
|
|
|
|
2020-09-30 14:58:48 +08:00
|
|
|
(void)rc;
|
|
|
|
|
2022-03-23 19:57:03 +08:00
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
|
|
|
|
|
2023-02-22 17:11:57 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
|
2023-08-16 14:33:24 +08:00
|
|
|
if (io_detect_pin() &&
|
|
|
|
!io_boot_skip_serial_recovery()) {
|
2023-04-04 20:50:43 +08:00
|
|
|
boot_serial_enter();
|
2020-11-14 10:51:27 +08:00
|
|
|
}
|
2021-01-22 08:34:05 +08:00
|
|
|
#endif
|
|
|
|
|
2023-03-28 17:04:18 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
|
2023-08-16 14:33:24 +08:00
|
|
|
if (io_detect_pin_reset()) {
|
2023-04-04 20:50:43 +08:00
|
|
|
boot_serial_enter();
|
2023-03-28 17:04:18 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-14 10:51:27 +08:00
|
|
|
#if defined(CONFIG_BOOT_USB_DFU_GPIO)
|
2023-08-16 14:33:24 +08:00
|
|
|
if (io_detect_pin()) {
|
2021-01-22 08:34:05 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
2023-12-05 21:26:22 +08:00
|
|
|
io_led_set(1);
|
2021-01-22 08:34:05 +08:00
|
|
|
#endif
|
2022-03-23 19:57:03 +08:00
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
|
|
|
|
|
2020-11-14 10:51:27 +08:00
|
|
|
rc = usb_enable(NULL);
|
|
|
|
if (rc) {
|
|
|
|
BOOT_LOG_ERR("Cannot enable USB");
|
|
|
|
} else {
|
|
|
|
BOOT_LOG_INF("Waiting for USB DFU");
|
|
|
|
wait_for_usb_dfu(K_FOREVER);
|
|
|
|
BOOT_LOG_INF("USB DFU wait time elapsed");
|
2021-01-22 08:34:05 +08:00
|
|
|
}
|
2020-11-14 10:51:27 +08:00
|
|
|
}
|
|
|
|
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
|
2020-02-05 15:40:12 +08:00
|
|
|
rc = usb_enable(NULL);
|
|
|
|
if (rc) {
|
|
|
|
BOOT_LOG_ERR("Cannot enable USB");
|
|
|
|
} else {
|
|
|
|
BOOT_LOG_INF("Waiting for USB DFU");
|
2022-03-23 19:57:03 +08:00
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
|
|
|
|
|
2020-11-14 10:51:27 +08:00
|
|
|
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
|
2020-02-05 15:40:12 +08:00
|
|
|
BOOT_LOG_INF("USB DFU wait time elapsed");
|
2022-03-23 19:57:03 +08:00
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
|
2020-02-05 15:40:12 +08:00
|
|
|
}
|
2019-02-20 16:38:52 +08:00
|
|
|
#endif
|
|
|
|
|
2022-01-19 22:39:43 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
|
|
|
|
/* Initialize the boot console, so we can already fill up our buffers while
|
|
|
|
* waiting for the boot image check to finish. This image check, can take
|
|
|
|
* some time, so it's better to reuse thistime to already receive the
|
|
|
|
* initial mcumgr command(s) into our buffers
|
|
|
|
*/
|
|
|
|
rc = boot_console_init();
|
|
|
|
int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
|
|
|
|
uint32_t start = k_uptime_get_32();
|
2022-12-15 21:58:45 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
2023-12-05 21:26:22 +08:00
|
|
|
io_led_set(1);
|
2022-12-15 21:58:45 +08:00
|
|
|
#endif
|
2022-01-19 22:39:43 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-30 14:58:48 +08:00
|
|
|
FIH_CALL(boot_go, fih_rc, &rsp);
|
2022-01-19 22:39:43 +08:00
|
|
|
|
2023-02-22 17:11:57 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
|
2023-08-16 14:33:24 +08:00
|
|
|
if (io_detect_boot_mode()) {
|
2023-02-22 17:11:57 +08:00
|
|
|
/* Boot mode to stay in bootloader, clear status and enter serial
|
|
|
|
* recovery mode
|
|
|
|
*/
|
2023-04-04 20:50:43 +08:00
|
|
|
boot_serial_enter();
|
2023-02-22 17:11:57 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-19 22:39:43 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
|
|
|
|
timeout_in_ms -= (k_uptime_get_32() - start);
|
|
|
|
if( timeout_in_ms <= 0 ) {
|
|
|
|
/* at least one check if time was expired */
|
|
|
|
timeout_in_ms = 1;
|
|
|
|
}
|
2022-04-07 16:00:31 +08:00
|
|
|
boot_serial_check_start(&boot_funcs,timeout_in_ms);
|
2022-12-15 21:58:45 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
2023-12-05 21:26:22 +08:00
|
|
|
io_led_set(0);
|
2022-12-15 21:58:45 +08:00
|
|
|
#endif
|
2022-01-19 22:39:43 +08:00
|
|
|
#endif
|
|
|
|
|
2022-11-24 23:49:56 +08:00
|
|
|
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_ERR("Unable to find bootable image");
|
2022-03-23 19:57:03 +08:00
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
|
|
|
|
|
2023-03-10 19:19:36 +08:00
|
|
|
#ifdef CONFIG_BOOT_SERIAL_NO_APPLICATION
|
|
|
|
/* No bootable image and configuration set to remain in serial
|
|
|
|
* recovery mode
|
|
|
|
*/
|
2023-04-04 20:50:43 +08:00
|
|
|
boot_serial_enter();
|
2023-03-10 19:19:36 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-30 14:58:48 +08:00
|
|
|
FIH_PANIC;
|
2017-04-12 07:33:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:30:02 +08:00
|
|
|
BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
|
|
|
|
rsp.br_image_off);
|
2018-04-12 18:42:49 +08:00
|
|
|
|
2021-02-17 23:18:01 +08:00
|
|
|
#if defined(MCUBOOT_DIRECT_XIP)
|
|
|
|
BOOT_LOG_INF("Jumping to the image slot");
|
|
|
|
#else
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_INF("Jumping to the first image slot");
|
2021-02-17 23:18:01 +08:00
|
|
|
#endif
|
2022-03-23 19:57:03 +08:00
|
|
|
|
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
|
|
|
|
|
2020-02-24 18:50:19 +08:00
|
|
|
ZEPHYR_BOOT_LOG_STOP();
|
2017-04-12 07:33:30 +08:00
|
|
|
do_boot(&rsp);
|
|
|
|
|
2022-03-23 19:57:03 +08:00
|
|
|
mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
|
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_ERR("Never should get here");
|
|
|
|
while (1)
|
|
|
|
;
|
2017-01-07 02:16:53 +08:00
|
|
|
}
|