blob: 8551649152aec7cb891177cc672717f99026fe1f [file] [log] [blame]
David Brown5153bd62017-01-06 11:16:53 -07001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
Tamas Banee6615d2020-09-30 07:58:48 +01003 * Copyright (c) 2020 Arm Limited
Jamie McCraeb3e3ce32023-02-22 09:11:57 +00004 * Copyright (c) 2021-2023 Nordic Semiconductor ASA
David Brown5153bd62017-01-06 11:16:53 -07005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Marti Bolivareb940802017-05-01 23:15:29 -040019#include <assert.h>
Gerard Marull-Paretas34dd9e72022-05-09 12:13:12 +020020#include <zephyr/kernel.h>
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020021#include <zephyr/devicetree.h>
22#include <zephyr/drivers/gpio.h>
23#include <zephyr/sys/__assert.h>
24#include <zephyr/drivers/flash.h>
25#include <zephyr/drivers/timer/system_timer.h>
26#include <zephyr/usb/usb_device.h>
Evan Gates4632d8d2018-06-28 13:27:40 -070027#include <soc.h>
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020028#include <zephyr/linker/linker-defs.h>
David Brown5153bd62017-01-06 11:16:53 -070029
Gerard Marull-Paretas11ecbf62023-06-30 09:58:41 +020030#include <cmsis_core.h>
Yonatan Schachterb22eb6a2022-07-29 18:16:09 +030031
Marti Bolivar51181cf2017-03-20 11:03:41 -040032#include "target.h"
33
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050034#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020035#include "bootutil/image.h"
36#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010037#include "bootutil/fault_injection_hardening.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000038#include "bootutil/mcuboot_status.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020039#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020040
Marek Matej67693442023-01-23 17:54:36 +010041/* Check if Espressif target is supported */
42#ifdef CONFIG_SOC_FAMILY_ESP32
43
44#include <bootloader_init.h>
45#include <esp_loader.h>
46
47#define IMAGE_INDEX_0 0
48#define IMAGE_INDEX_1 1
49
50#define PRIMARY_SLOT 0
51#define SECONDARY_SLOT 1
52
53#define IMAGE0_PRIMARY_START_ADDRESS \
54 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 0)
55#define IMAGE0_PRIMARY_SIZE \
56 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 1)
57
58#define IMAGE1_PRIMARY_START_ADDRESS \
59 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 0)
60#define IMAGE1_PRIMARY_SIZE \
61 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 1)
62
63#endif /* CONFIG_SOC_FAMILY_ESP32 */
64
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020065#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030066#include "boot_serial/boot_serial.h"
67#include "serial_adapter/serial_adapter.h"
68
69const struct boot_uart_funcs boot_funcs = {
70 .read = console_read,
71 .write = console_write
72};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020073#endif
74
Jamie McCraeb3e3ce32023-02-22 09:11:57 +000075#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
76#include <zephyr/retention/bootmode.h>
77#endif
78
Josh Gao837cf882020-11-13 18:51:27 -080079#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020080#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053081#endif
82
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010083#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
84#include <arm_cleanup.h>
85#endif
86
Jamie McCrae35941fe2023-03-28 10:04:18 +010087#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
88#include <zephyr/drivers/hwinfo.h>
89#endif
90
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010091/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
92 * replaced by CONFIG_LOG_MODE_MINIMAL.
93 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000094#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
95#define ZEPHYR_LOG_MODE_MINIMAL 1
96#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010097
Marek Pietafb47d2e2022-03-11 14:24:07 +010098/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
99 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
100 */
101#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
102#define ZEPHYR_LOG_MODE_IMMEDIATE 1
103#endif
104
105#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000106 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100107#ifdef CONFIG_LOG_PROCESS_THREAD
108#warning "The log internal thread for log processing can't transfer the log"\
109 "well for MCUBoot."
110#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +0200111#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100112
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +0100113#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100114
115/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +0100116K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100117struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100118volatile bool boot_log_stop = false;
119K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100120
121/* log processing need to be initalized by the application */
122#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100123#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100124#endif /* CONFIG_LOG_PROCESS_THREAD */
125#else
126/* synchronous log mode doesn't need to be initalized by the application */
127#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100128#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +0100129#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
130 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
131 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100132
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100133#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +0100134#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100135
136static inline bool boot_skip_serial_recovery()
137{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100138 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100139
Radoslaw Koppel72006692021-12-06 16:15:21 +0100140 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100141}
142#else
143static inline bool boot_skip_serial_recovery()
144{
145 return false;
146}
147#endif
148
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200149BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100150
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000151/* Validate serial recovery configuration */
152#ifdef CONFIG_MCUBOOT_SERIAL
153#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
154 !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
Jamie McCraefd79db32023-03-10 11:19:36 +0000155 !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
Jamie McCrae35941fe2023-03-28 10:04:18 +0100156 !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
157 !defined(CONFIG_BOOT_SERIAL_PIN_RESET)
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000158#error "Serial recovery selected without an entrance mode set"
159#endif
160#endif
161
Jared Wolff8e4d7912021-01-21 19:34:05 -0500162#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500163
164/*
165 * The led0 devicetree alias is optional. If present, we'll use it
166 * to turn on the LED whenever the button is pressed.
167 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200168#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200169#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200170#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
171#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500172#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200173#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500174
175#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200176static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800177#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500178/* A build error here means your board isn't set up to drive an LED. */
179#error "Unsupported board: led0 devicetree alias is not defined"
180#endif
181
Jared Wolff8e4d7912021-01-21 19:34:05 -0500182void led_init(void)
183{
Martin Jäger477ed812022-07-21 11:45:45 +0200184 if (!device_is_ready(led0.port)) {
185 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
186 return;
187 }
Josh Gao837cf882020-11-13 18:51:27 -0800188
Martin Jäger477ed812022-07-21 11:45:45 +0200189 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
190 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500191}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200192#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500193
Andrew Boie7238f512017-03-02 13:39:06 -0800194void os_heap_init(void);
195
196#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200197
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200198#ifdef CONFIG_SW_VECTOR_RELAY
199extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200200#endif
201
Andrew Boie7238f512017-03-02 13:39:06 -0800202struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600203 uint32_t msp;
204 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700205};
206
Andrew Boie7238f512017-03-02 13:39:06 -0800207static void do_boot(struct boot_rsp *rsp)
208{
David Brown0d0652a2017-04-11 17:33:30 -0600209 struct arm_vector_table *vt;
Andrew Boie7238f512017-03-02 13:39:06 -0800210
David Brown0d0652a2017-04-11 17:33:30 -0600211 /* The beginning of the image is the ARM vector table, containing
212 * the initial stack pointer address and the reset vector
213 * consecutively. Manually set the stack pointer and jump into the
214 * reset vector
215 */
Daniel DeGrassee4574442022-09-01 15:47:01 -0500216#ifdef CONFIG_BOOT_RAM_LOAD
217 /* Get ram address for image */
218 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
219#else
220 uintptr_t flash_base;
221 int rc;
222
223 /* Jump to flash image */
Marti Bolivareb940802017-05-01 23:15:29 -0400224 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
225 assert(rc == 0);
226
227 vt = (struct arm_vector_table *)(flash_base +
228 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600229 rsp->br_hdr->ih_hdr_size);
Daniel DeGrassee4574442022-09-01 15:47:01 -0500230#endif
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200231
Joakim Andersson90b8f692022-12-21 15:52:53 +0100232 if (IS_ENABLED(CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)) {
233 sys_clock_disable();
234 }
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100235
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200236#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200237 /* Disable the USB to prevent it from firing interrupts */
238 usb_disable();
239#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100240#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
241 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200242
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700243#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200244 /* Disable instruction cache and data cache before chain-load the application */
245 SCB_DisableDCache();
246 SCB_DisableICache();
247#endif
248
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100249#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200250 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100251#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200252
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000253#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
254 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
255 /* Reset limit registers to avoid inflicting stack overflow on image
256 * being booted.
257 */
258 __set_PSPLIM(0);
259 __set_MSPLIM(0);
260#endif
261
Sigvart Hovland9647c462021-08-20 16:33:55 +0200262#else
263 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200264#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
265
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200266#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200267#if defined(CONFIG_SW_VECTOR_RELAY)
268 _vector_table_pointer = vt;
269#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
270 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200271#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200272#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
273 SCB->VTOR = (uint32_t)vt;
274#endif /* CONFIG_SW_VECTOR_RELAY */
275#else /* CONFIG_BOOT_INTR_VEC_RELOC */
276#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
277 _vector_table_pointer = _vector_start;
278 SCB->VTOR = (uint32_t)__vector_relay_table;
279#endif
280#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200281
David Brown57837b92018-03-13 15:56:38 -0600282 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100283#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
284 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100285 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100286#endif
David Brown0d0652a2017-04-11 17:33:30 -0600287 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800288}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530289
Marek Matej67693442023-01-23 17:54:36 +0100290#elif defined(CONFIG_XTENSA) || defined(CONFIG_RISCV)
291
292#ifndef CONFIG_SOC_FAMILY_ESP32
293
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530294#define SRAM_BASE_ADDRESS 0xBE030000
295
296static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
297{
298 const struct flash_area *fap;
299 int area_id;
300 int rc;
301 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
302
303 BOOT_LOG_INF("Copying image to SRAM");
304
305 area_id = flash_area_id_from_image_slot(slot);
306 rc = flash_area_open(area_id, &fap);
307 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300308 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530309 goto done;
310 }
311
312 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
313 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300314 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530315 goto done;
316 }
317
318done:
319 flash_area_close(fap);
320}
Marek Matej67693442023-01-23 17:54:36 +0100321#endif /* !CONFIG_SOC_FAMILY_ESP32 */
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530322
323/* Entry point (.ResetVector) is at the very beginning of the image.
324 * Simply copy the image to a suitable location and jump there.
325 */
326static void do_boot(struct boot_rsp *rsp)
327{
328 void *start;
329
330 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
331 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
332
Marek Matej67693442023-01-23 17:54:36 +0100333#ifdef CONFIG_SOC_FAMILY_ESP32
334 int slot = (rsp->br_image_off == IMAGE0_PRIMARY_START_ADDRESS) ?
335 PRIMARY_SLOT : SECONDARY_SLOT;
336 /* Load memory segments and start from entry point */
337 start_cpu0_image(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size);
338#else
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530339 /* Copy from the flash to HP SRAM */
340 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
341
342 /* Jump to entry point */
343 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
344 ((void (*)(void))start)();
Marek Matej67693442023-01-23 17:54:36 +0100345#endif /* CONFIG_SOC_FAMILY_ESP32 */
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530346}
347
Andrew Boie7238f512017-03-02 13:39:06 -0800348#else
349/* Default: Assume entry point is at the very beginning of the image. Simply
350 * lock interrupts and jump there. This is the right thing to do for X86 and
351 * possibly other platforms.
352 */
353static void do_boot(struct boot_rsp *rsp)
354{
David Brown0d0652a2017-04-11 17:33:30 -0600355 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200356
Jim Tanee1b7b92022-04-07 11:26:28 +0800357#if defined(MCUBOOT_RAM_LOAD)
358 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
359#else
360 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400361 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800362
Marti Bolivareb940802017-05-01 23:15:29 -0400363 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
364 assert(rc == 0);
365
366 start = (void *)(flash_base + rsp->br_image_off +
367 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800368#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800369
David Brown0d0652a2017-04-11 17:33:30 -0600370 /* Lock interrupts and dive into the entry point */
371 irq_lock();
372 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800373}
374#endif
David Brown5153bd62017-01-06 11:16:53 -0700375
Marek Pietafb47d2e2022-03-11 14:24:07 +0100376#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000377 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100378/* The log internal thread for log processing can't transfer log well as has too
379 * low priority.
380 * Dedicated thread for log processing below uses highest application
381 * priority. This allows to transmit all logs without adding k_sleep/k_yield
382 * anywhere else int the code.
383 */
384
385/* most simple log processing theread */
386void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
387{
388 (void)dummy1;
389 (void)dummy2;
390 (void)dummy3;
391
Martin Jäger477ed812022-07-21 11:45:45 +0200392 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100393
Martin Jäger477ed812022-07-21 11:45:45 +0200394 while (1) {
Martin Jäger31751822022-07-21 11:49:32 +0200395#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
396 /* support Zephyr legacy logging implementation before commit c5f2cde */
Martin Jäger477ed812022-07-21 11:45:45 +0200397 if (log_process(false) == false) {
Martin Jäger31751822022-07-21 11:49:32 +0200398#else
399 if (log_process() == false) {
400#endif
Martin Jäger477ed812022-07-21 11:45:45 +0200401 if (boot_log_stop) {
402 break;
403 }
404 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
405 }
406 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100407
Martin Jäger477ed812022-07-21 11:45:45 +0200408 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100409}
410
411void zephyr_boot_log_start(void)
412{
Martin Jäger477ed812022-07-21 11:45:45 +0200413 /* start logging thread */
414 k_thread_create(&boot_log_thread, boot_log_stack,
415 K_THREAD_STACK_SIZEOF(boot_log_stack),
416 boot_log_thread_func, NULL, NULL, NULL,
417 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
418 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100419
Martin Jäger477ed812022-07-21 11:45:45 +0200420 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100421}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100422
423void zephyr_boot_log_stop(void)
424{
425 boot_log_stop = true;
426
427 /* wait until log procesing thread expired
428 * This can be reworked using a thread_join() API once a such will be
429 * available in zephyr.
430 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
431 */
432 (void)k_sem_take(&boot_log_sem, K_FOREVER);
433}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100434#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
435 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
436 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100437
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000438#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200439
440#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200441#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
442#else
443#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
444#endif
445
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200446#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
447
448#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200449static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000450#else
Jamie McCrae9551b6e2023-02-28 15:47:42 +0000451#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200452#endif
453
454static bool detect_pin(void)
Josh Gao837cf882020-11-13 18:51:27 -0800455{
456 int rc;
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200457 int pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800458
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200459 if (!device_is_ready(button0.port)) {
460 __ASSERT(false, "GPIO device is not ready.\n");
461 return false;
462 }
Josh Gao837cf882020-11-13 18:51:27 -0800463
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200464 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
Josh Gao837cf882020-11-13 18:51:27 -0800465 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
466
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200467 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200468 pin_active = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100469
Josh Gao837cf882020-11-13 18:51:27 -0800470 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
471
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200472 if (pin_active) {
473 if (BUTTON_0_DETECT_DELAY > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200474#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800475 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200476#else
477 k_busy_wait(50000);
478#endif
Josh Gao837cf882020-11-13 18:51:27 -0800479
480 /* Get the uptime for debounce purposes. */
481 int64_t timestamp = k_uptime_get();
482
483 for(;;) {
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200484 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200485 pin_active = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800486 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
487
488 /* Get delta from when this started */
489 uint32_t delta = k_uptime_get() - timestamp;
490
491 /* If not pressed OR if pressed > debounce period, stop. */
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200492 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
Josh Gao837cf882020-11-13 18:51:27 -0800493 break;
494 }
495
496 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200497#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800498 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200499#else
500 k_busy_wait(1000);
501#endif
Josh Gao837cf882020-11-13 18:51:27 -0800502 }
503 }
504 }
505
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200506 return (bool)pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800507}
508#endif
509
Jamie McCrae6902abb2023-04-04 13:50:43 +0100510#ifdef CONFIG_MCUBOOT_SERIAL
511static void boot_serial_enter()
512{
513 int rc;
514
515#ifdef CONFIG_MCUBOOT_INDICATION_LED
516 gpio_pin_set_dt(&led0, 1);
517#endif
518
519 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
520
521 BOOT_LOG_INF("Enter the serial recovery mode");
522 rc = boot_console_init();
523 __ASSERT(rc == 0, "Error initializing boot console.\n");
524 boot_serial_start(&boot_funcs);
525 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
526}
527#endif
528
Keith Packardec2ac822023-04-27 09:56:10 -0700529int main(void)
David Brown5153bd62017-01-06 11:16:53 -0700530{
David Brown0d0652a2017-04-11 17:33:30 -0600531 struct boot_rsp rsp;
532 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100533 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Brown5153bd62017-01-06 11:16:53 -0700534
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000535#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
536 int32_t boot_mode;
537#endif
538
Jamie McCrae35941fe2023-03-28 10:04:18 +0100539#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
540 uint32_t reset_cause;
541#endif
542
Jeppe Odgaard4420bb62023-04-11 09:43:10 +0200543 MCUBOOT_WATCHDOG_SETUP();
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200544 MCUBOOT_WATCHDOG_FEED();
545
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000546#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600547 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000548#else
549 BOOT_LOG_INF("Starting Direct-XIP bootloader");
550#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200551
Jared Wolff8e4d7912021-01-21 19:34:05 -0500552#ifdef CONFIG_MCUBOOT_INDICATION_LED
553 /* LED init */
554 led_init();
555#endif
556
David Brown0d0652a2017-04-11 17:33:30 -0600557 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700558
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100559 ZEPHYR_BOOT_LOG_START();
560
Tamas Banee6615d2020-09-30 07:58:48 +0100561 (void)rc;
562
Jamie McCrae56cb6102022-03-23 11:57:03 +0000563 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
564
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000565#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200566 if (detect_pin() &&
Josh Gao837cf882020-11-13 18:51:27 -0800567 !boot_skip_serial_recovery()) {
Jamie McCrae6902abb2023-04-04 13:50:43 +0100568 boot_serial_enter();
Josh Gao837cf882020-11-13 18:51:27 -0800569 }
570#endif
571
Jamie McCrae35941fe2023-03-28 10:04:18 +0100572#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
573 rc = hwinfo_get_reset_cause(&reset_cause);
574
575 if (rc == 0 && reset_cause == RESET_PIN) {
Jamie McCrae35941fe2023-03-28 10:04:18 +0100576 (void)hwinfo_clear_reset_cause();
Jamie McCrae6902abb2023-04-04 13:50:43 +0100577 boot_serial_enter();
Jamie McCrae35941fe2023-03-28 10:04:18 +0100578 }
579#endif
580
Josh Gao837cf882020-11-13 18:51:27 -0800581#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200582 if (detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800583#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200584 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800585#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000586
587 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
588
Josh Gao837cf882020-11-13 18:51:27 -0800589 rc = usb_enable(NULL);
590 if (rc) {
591 BOOT_LOG_ERR("Cannot enable USB");
592 } else {
593 BOOT_LOG_INF("Waiting for USB DFU");
594 wait_for_usb_dfu(K_FOREVER);
595 BOOT_LOG_INF("USB DFU wait time elapsed");
596 }
597 }
598#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100599 rc = usb_enable(NULL);
600 if (rc) {
601 BOOT_LOG_ERR("Cannot enable USB");
602 } else {
603 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000604
605 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
606
Josh Gao837cf882020-11-13 18:51:27 -0800607 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100608 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000609
610 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100611 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530612#endif
613
Wouter Cappellee3822f82022-01-19 15:39:43 +0100614#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
615 /* Initialize the boot console, so we can already fill up our buffers while
616 * waiting for the boot image check to finish. This image check, can take
617 * some time, so it's better to reuse thistime to already receive the
618 * initial mcumgr command(s) into our buffers
619 */
620 rc = boot_console_init();
621 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
622 uint32_t start = k_uptime_get_32();
623#endif
624
Tamas Banee6615d2020-09-30 07:58:48 +0100625 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100626
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000627#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
628 boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
629
630 if (boot_mode == 1) {
631 /* Boot mode to stay in bootloader, clear status and enter serial
632 * recovery mode
633 */
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000634 bootmode_clear();
Jamie McCrae6902abb2023-04-04 13:50:43 +0100635 boot_serial_enter();
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000636 }
637#endif
638
Wouter Cappellee3822f82022-01-19 15:39:43 +0100639#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
640 timeout_in_ms -= (k_uptime_get_32() - start);
641 if( timeout_in_ms <= 0 ) {
642 /* at least one check if time was expired */
643 timeout_in_ms = 1;
644 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100645 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100646#endif
647
Michael Grand5047f032022-11-24 16:49:56 +0100648 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600649 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000650
651 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
652
Jamie McCraefd79db32023-03-10 11:19:36 +0000653#ifdef CONFIG_BOOT_SERIAL_NO_APPLICATION
654 /* No bootable image and configuration set to remain in serial
655 * recovery mode
656 */
Jamie McCrae6902abb2023-04-04 13:50:43 +0100657 boot_serial_enter();
Jamie McCraefd79db32023-03-10 11:19:36 +0000658#endif
659
Tamas Banee6615d2020-09-30 07:58:48 +0100660 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600661 }
David Brown5153bd62017-01-06 11:16:53 -0700662
Marti Bolivar88f48d92017-05-01 22:30:02 -0400663 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
664 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200665
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000666#if defined(MCUBOOT_DIRECT_XIP)
667 BOOT_LOG_INF("Jumping to the image slot");
668#else
David Brown0d0652a2017-04-11 17:33:30 -0600669 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000670#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000671
672 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
673
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100674 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600675 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700676
Jamie McCrae56cb6102022-03-23 11:57:03 +0000677 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
678
David Brown0d0652a2017-04-11 17:33:30 -0600679 BOOT_LOG_ERR("Never should get here");
680 while (1)
681 ;
David Brown5153bd62017-01-06 11:16:53 -0700682}