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
|
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>
|
2017-01-07 02:16:53 +08:00
|
|
|
#include <zephyr.h>
|
2020-01-25 19:50:12 +08:00
|
|
|
#include <drivers/gpio.h>
|
2019-12-12 16:34:11 +08:00
|
|
|
#include <sys/__assert.h>
|
2020-01-25 19:50:12 +08:00
|
|
|
#include <drivers/flash.h>
|
2019-06-28 21:16:35 +08:00
|
|
|
#include <drivers/timer/system_timer.h>
|
2018-07-20 17:39:57 +08:00
|
|
|
#include <usb/usb_device.h>
|
2018-06-29 04:27:40 +08:00
|
|
|
#include <soc.h>
|
2020-07-17 19:18:15 +08:00
|
|
|
#include <linker/linker-defs.h>
|
2017-01-07 02:16:53 +08:00
|
|
|
|
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"
|
2018-04-12 18:42:49 +08:00
|
|
|
#include "flash_map_backend/flash_map_backend.h"
|
2017-01-19 20:22:35 +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
|
|
|
|
|
2019-02-20 16:38:52 +08:00
|
|
|
#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
|
|
|
|
#include <usb/class/usb_dfu.h>
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
#define ZEPHYR_LOG_MODE_MINIMAL (defined(CONFIG_LOG_MODE_MINIMAL) ||\
|
|
|
|
defined(CONFIG_LOG_MINIMAL))
|
|
|
|
|
2021-01-12 16:52:51 +08:00
|
|
|
#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) && \
|
2021-01-27 05:50:38 +08:00
|
|
|
!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
|
|
|
|
#include <logging/log_ctrl.h>
|
|
|
|
|
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)
|
2020-02-17 20:25:32 +08:00
|
|
|
#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
|
|
|
|
|
2019-11-25 22:59:52 +08:00
|
|
|
#ifdef CONFIG_SOC_FAMILY_NRF
|
|
|
|
#include <hal/nrf_power.h>
|
|
|
|
|
|
|
|
static inline bool boot_skip_serial_recovery()
|
|
|
|
{
|
2019-12-17 20:59:47 +08:00
|
|
|
#if NRF_POWER_HAS_RESETREAS
|
2020-05-28 01:25:41 +08:00
|
|
|
uint32_t rr = nrf_power_resetreas_get(NRF_POWER);
|
2019-11-25 22:59:52 +08:00
|
|
|
|
|
|
|
return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool boot_skip_serial_recovery()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-20 17:59:59 +08:00
|
|
|
MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
|
|
|
|
|
2021-01-22 08:34:05 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
|
|
|
/*
|
|
|
|
* Devicetree helper macro which gets the 'flags' cell from a 'gpios'
|
|
|
|
* property, or returns 0 if the property has no 'flags' cell.
|
|
|
|
*/
|
|
|
|
#define FLAGS_OR_ZERO(node) \
|
|
|
|
COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \
|
|
|
|
(DT_GPIO_FLAGS(node, gpios)), \
|
|
|
|
(0))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The led0 devicetree alias is optional. If present, we'll use it
|
|
|
|
* to turn on the LED whenever the button is pressed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LED0_NODE DT_ALIAS(bootloader_led0)
|
|
|
|
|
|
|
|
#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
|
|
|
|
#define LED0_GPIO_LABEL DT_GPIO_LABEL(LED0_NODE, gpios)
|
|
|
|
#define LED0_GPIO_PIN DT_GPIO_PIN(LED0_NODE, gpios)
|
|
|
|
#define LED0_GPIO_FLAGS (GPIO_OUTPUT | FLAGS_OR_ZERO(LED0_NODE))
|
|
|
|
#else
|
|
|
|
/* A build error here means your board isn't set up to drive an LED. */
|
|
|
|
#error "Unsupported board: led0 devicetree alias is not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const static struct device *led;
|
|
|
|
|
|
|
|
void led_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
led = device_get_binding(LED0_GPIO_LABEL);
|
2021-02-05 00:17:00 +08:00
|
|
|
if (led == NULL) {
|
2021-01-22 08:34:05 +08:00
|
|
|
BOOT_LOG_ERR("Didn't find LED device %s\n", LED0_GPIO_LABEL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_configure(led, LED0_GPIO_PIN, LED0_GPIO_FLAGS);
|
2021-02-05 00:17:00 +08:00
|
|
|
gpio_pin_set(led, LED0_GPIO_PIN, 0);
|
2021-01-22 08:34:05 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-11-01 23:06:36 +08:00
|
|
|
extern void sys_clock_disable(void);
|
|
|
|
|
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;
|
2017-05-02 11:15:29 +08:00
|
|
|
uintptr_t flash_base;
|
|
|
|
int rc;
|
2017-04-12 07:33:30 +08:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2017-05-02 11:15:29 +08:00
|
|
|
rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
|
|
|
|
assert(rc == 0);
|
|
|
|
|
|
|
|
vt = (struct arm_vector_table *)(flash_base +
|
|
|
|
rsp->br_image_off +
|
2017-04-12 07:33:30 +08:00
|
|
|
rsp->br_hdr->ih_hdr_size);
|
2020-08-19 14:57:11 +08:00
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
irq_lock();
|
2019-08-12 22:28:26 +08:00
|
|
|
#ifdef CONFIG_SYS_CLOCK_EXISTS
|
2017-04-12 07:33:30 +08:00
|
|
|
sys_clock_disable();
|
2019-08-12 22:28:26 +08:00
|
|
|
#endif
|
2019-05-09 12:55:54 +08:00
|
|
|
#ifdef CONFIG_USB
|
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
|
|
|
|
|
|
|
#ifdef CONFIG_CPU_CORTEX_M7
|
|
|
|
/* Disable instruction cache and data cache before chain-load the application */
|
|
|
|
SCB_DisableDCache();
|
|
|
|
SCB_DisableICache();
|
|
|
|
#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
|
|
|
|
|
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
|
|
|
|
|
|
|
#elif defined(CONFIG_XTENSA)
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* 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)();
|
|
|
|
}
|
|
|
|
|
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-05-02 11:15:29 +08:00
|
|
|
uintptr_t flash_base;
|
2017-04-12 07:33:30 +08:00
|
|
|
void *start;
|
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);
|
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
|
|
|
|
2020-02-17 20:25:32 +08:00
|
|
|
#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
|
2021-01-27 05:50:38 +08:00
|
|
|
!defined(CONFIG_LOG_PROCESS_THREAD) && !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;
|
|
|
|
|
|
|
|
log_init();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (log_process(false) == false) {
|
2020-02-24 18:50:19 +08:00
|
|
|
if (boot_log_stop) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-17 20:25:32 +08:00
|
|
|
k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 18:50:19 +08:00
|
|
|
|
|
|
|
k_sem_give(&boot_log_sem);
|
2020-02-17 20:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void zephyr_boot_log_start(void)
|
|
|
|
{
|
|
|
|
/* 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-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);
|
|
|
|
}
|
2020-02-17 20:25:32 +08:00
|
|
|
#endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
|
|
|
|
!defined(CONFIG_LOG_PROCESS_THREAD) */
|
|
|
|
|
2017-01-07 02:16:53 +08:00
|
|
|
void main(void)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
struct boot_rsp rsp;
|
|
|
|
int rc;
|
2020-09-30 14:58:48 +08:00
|
|
|
fih_int fih_rc = FIH_FAILURE;
|
2017-04-12 07:33:30 +08:00
|
|
|
|
2020-10-13 19:52:15 +08:00
|
|
|
MCUBOOT_WATCHDOG_FEED();
|
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_INF("Starting bootloader");
|
|
|
|
|
2021-01-22 08:34:05 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
|
|
|
/* LED init */
|
|
|
|
led_init();
|
|
|
|
#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;
|
|
|
|
|
2020-04-09 01:02:03 +08:00
|
|
|
#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
|
|
|
|
if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) {
|
|
|
|
BOOT_LOG_ERR("Flash device %s not found",
|
|
|
|
DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
2017-04-12 07:33:30 +08:00
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
2020-04-09 01:06:21 +08:00
|
|
|
#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
|
|
|
|
if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
|
|
|
|
BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
|
2018-12-09 16:02:01 +08:00
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
2018-12-06 19:54:10 +08:00
|
|
|
#endif
|
2017-04-12 07:33:30 +08:00
|
|
|
|
2017-09-08 22:49:14 +08:00
|
|
|
#ifdef CONFIG_MCUBOOT_SERIAL
|
|
|
|
|
2020-09-11 19:31:38 +08:00
|
|
|
struct device const *detect_port;
|
2020-05-28 01:25:41 +08:00
|
|
|
uint32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
|
2017-09-08 22:49:14 +08:00
|
|
|
|
|
|
|
detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
|
|
|
|
__ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
|
|
|
|
|
2020-01-30 19:36:47 +08:00
|
|
|
/* The default presence value is 0 which would normally be
|
|
|
|
* active-low, but historically the raw value was checked so we'll
|
|
|
|
* use the raw interface.
|
|
|
|
*/
|
2017-09-08 22:49:14 +08:00
|
|
|
rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
|
2020-01-30 19:36:47 +08:00
|
|
|
#ifdef GPIO_INPUT
|
|
|
|
GPIO_INPUT | GPIO_PULL_UP
|
|
|
|
#else
|
|
|
|
GPIO_DIR_IN | GPIO_PUD_PULL_UP
|
|
|
|
#endif
|
2020-02-20 17:35:33 +08:00
|
|
|
);
|
2018-06-13 20:55:51 +08:00
|
|
|
__ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
|
2017-09-08 22:49:14 +08:00
|
|
|
|
2020-01-30 19:36:47 +08:00
|
|
|
#ifdef GPIO_INPUT
|
|
|
|
rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
|
|
|
|
detect_value = rc;
|
|
|
|
#else
|
2018-11-01 23:06:36 +08:00
|
|
|
rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
|
2017-09-08 22:49:14 +08:00
|
|
|
&detect_value);
|
2020-01-30 19:36:47 +08:00
|
|
|
#endif
|
|
|
|
__ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
|
2019-11-25 22:59:52 +08:00
|
|
|
if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
|
|
|
|
!boot_skip_serial_recovery()) {
|
2021-01-22 08:34:05 +08:00
|
|
|
|
|
|
|
#if CONFIG_BOOT_SERIAL_DETECT_DELAY > 0
|
|
|
|
k_sleep(K_MSEC(50));
|
|
|
|
|
|
|
|
/* Get the uptime for debounce purposes. */
|
|
|
|
int64_t timestamp = k_uptime_get();
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
|
|
|
|
#ifdef GPIO_INPUT
|
|
|
|
rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
|
|
|
|
detect_value = rc;
|
|
|
|
#else
|
|
|
|
rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
|
|
|
|
&detect_value);
|
|
|
|
#endif
|
|
|
|
__ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
|
|
|
|
|
|
|
|
/* Get delta from when this started */
|
|
|
|
uint32_t delta = k_uptime_get() - timestamp;
|
|
|
|
|
|
|
|
/* If not pressed OR if pressed > debounce period stop loop*/
|
|
|
|
if( delta >= CONFIG_BOOT_SERIAL_DETECT_DELAY ||
|
|
|
|
detect_value != CONFIG_BOOT_SERIAL_DETECT_PIN_VAL ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Delay 1 ms */
|
|
|
|
k_sleep(K_MSEC(1));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then run DFU */
|
|
|
|
if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
|
|
|
|
#ifdef CONFIG_MCUBOOT_INDICATION_LED
|
|
|
|
gpio_pin_set(led, LED0_GPIO_PIN, 1);
|
|
|
|
#endif
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 22:49:14 +08:00
|
|
|
#endif
|
|
|
|
|
2019-02-20 16:38:52 +08:00
|
|
|
#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
|
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");
|
|
|
|
wait_for_usb_dfu();
|
|
|
|
BOOT_LOG_INF("USB DFU wait time elapsed");
|
|
|
|
}
|
2019-02-20 16:38:52 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-30 14:58:48 +08:00
|
|
|
FIH_CALL(boot_go, fih_rc, &rsp);
|
|
|
|
if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_ERR("Unable to find bootable image");
|
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
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_INF("Jumping to the first image slot");
|
2020-02-24 18:50:19 +08:00
|
|
|
ZEPHYR_BOOT_LOG_STOP();
|
2017-04-12 07:33:30 +08:00
|
|
|
do_boot(&rsp);
|
|
|
|
|
|
|
|
BOOT_LOG_ERR("Never should get here");
|
|
|
|
while (1)
|
|
|
|
;
|
2017-01-07 02:16:53 +08:00
|
|
|
}
|