blob: 4a672fd19bae42067cfe5b0aade3e3ba103e1db9 [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
Yonatan Schachterb22eb6a2022-07-29 18:16:09 +030030#if defined(CONFIG_CPU_AARCH32_CORTEX_A) || defined(CONFIG_CPU_AARCH32_CORTEX_R)
31#include <zephyr/arch/arm/aarch32/cortex_a_r/cmsis.h>
32#elif defined(CONFIG_CPU_CORTEX_M)
33#include <zephyr/arch/arm/aarch32/cortex_m/cmsis.h>
34#endif
35
Marti Bolivar51181cf2017-03-20 11:03:41 -040036#include "target.h"
37
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050038#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020039#include "bootutil/image.h"
40#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010041#include "bootutil/fault_injection_hardening.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000042#include "bootutil/mcuboot_status.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020043#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020044
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020045#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030046#include "boot_serial/boot_serial.h"
47#include "serial_adapter/serial_adapter.h"
48
49const struct boot_uart_funcs boot_funcs = {
50 .read = console_read,
51 .write = console_write
52};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020053#endif
54
Jamie McCraeb3e3ce32023-02-22 09:11:57 +000055#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
56#include <zephyr/retention/bootmode.h>
57#endif
58
Josh Gao837cf882020-11-13 18:51:27 -080059#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020060#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053061#endif
62
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010063#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
64#include <arm_cleanup.h>
65#endif
66
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010067/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
68 * replaced by CONFIG_LOG_MODE_MINIMAL.
69 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000070#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
71#define ZEPHYR_LOG_MODE_MINIMAL 1
72#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010073
Marek Pietafb47d2e2022-03-11 14:24:07 +010074/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
75 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
76 */
77#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
78#define ZEPHYR_LOG_MODE_IMMEDIATE 1
79#endif
80
81#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000082 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010083#ifdef CONFIG_LOG_PROCESS_THREAD
84#warning "The log internal thread for log processing can't transfer the log"\
85 "well for MCUBoot."
86#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020087#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010088
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010089#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010090
91/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010092K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010093struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010094volatile bool boot_log_stop = false;
95K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010096
97/* log processing need to be initalized by the application */
98#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010099#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100100#endif /* CONFIG_LOG_PROCESS_THREAD */
101#else
102/* synchronous log mode doesn't need to be initalized by the application */
103#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100104#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +0100105#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
106 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
107 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100108
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100109#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +0100110#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100111
112static inline bool boot_skip_serial_recovery()
113{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100114 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100115
Radoslaw Koppel72006692021-12-06 16:15:21 +0100116 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100117}
118#else
119static inline bool boot_skip_serial_recovery()
120{
121 return false;
122}
123#endif
124
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200125BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100126
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000127/* Validate serial recovery configuration */
128#ifdef CONFIG_MCUBOOT_SERIAL
129#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
130 !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
Jamie McCraefd79db32023-03-10 11:19:36 +0000131 !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
132 !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION)
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000133#error "Serial recovery selected without an entrance mode set"
134#endif
135#endif
136
Jared Wolff8e4d7912021-01-21 19:34:05 -0500137#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500138
139/*
140 * The led0 devicetree alias is optional. If present, we'll use it
141 * to turn on the LED whenever the button is pressed.
142 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200143#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200144#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200145#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
146#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500147#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200148#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500149
150#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200151static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800152#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500153/* A build error here means your board isn't set up to drive an LED. */
154#error "Unsupported board: led0 devicetree alias is not defined"
155#endif
156
Jared Wolff8e4d7912021-01-21 19:34:05 -0500157void led_init(void)
158{
Martin Jäger477ed812022-07-21 11:45:45 +0200159 if (!device_is_ready(led0.port)) {
160 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
161 return;
162 }
Josh Gao837cf882020-11-13 18:51:27 -0800163
Martin Jäger477ed812022-07-21 11:45:45 +0200164 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
165 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500166}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200167#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500168
Andrew Boie7238f512017-03-02 13:39:06 -0800169void os_heap_init(void);
170
171#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200172
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200173#ifdef CONFIG_SW_VECTOR_RELAY
174extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200175#endif
176
Andrew Boie7238f512017-03-02 13:39:06 -0800177struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600178 uint32_t msp;
179 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700180};
181
Andrew Boie7238f512017-03-02 13:39:06 -0800182static void do_boot(struct boot_rsp *rsp)
183{
David Brown0d0652a2017-04-11 17:33:30 -0600184 struct arm_vector_table *vt;
Andrew Boie7238f512017-03-02 13:39:06 -0800185
David Brown0d0652a2017-04-11 17:33:30 -0600186 /* The beginning of the image is the ARM vector table, containing
187 * the initial stack pointer address and the reset vector
188 * consecutively. Manually set the stack pointer and jump into the
189 * reset vector
190 */
Daniel DeGrassee4574442022-09-01 15:47:01 -0500191#ifdef CONFIG_BOOT_RAM_LOAD
192 /* Get ram address for image */
193 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
194#else
195 uintptr_t flash_base;
196 int rc;
197
198 /* Jump to flash image */
Marti Bolivareb940802017-05-01 23:15:29 -0400199 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
200 assert(rc == 0);
201
202 vt = (struct arm_vector_table *)(flash_base +
203 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600204 rsp->br_hdr->ih_hdr_size);
Daniel DeGrassee4574442022-09-01 15:47:01 -0500205#endif
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200206
Joakim Andersson90b8f692022-12-21 15:52:53 +0100207 if (IS_ENABLED(CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)) {
208 sys_clock_disable();
209 }
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100210
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200211#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200212 /* Disable the USB to prevent it from firing interrupts */
213 usb_disable();
214#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100215#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
216 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200217
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700218#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200219 /* Disable instruction cache and data cache before chain-load the application */
220 SCB_DisableDCache();
221 SCB_DisableICache();
222#endif
223
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100224#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200225 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100226#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200227
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000228#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
229 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
230 /* Reset limit registers to avoid inflicting stack overflow on image
231 * being booted.
232 */
233 __set_PSPLIM(0);
234 __set_MSPLIM(0);
235#endif
236
Sigvart Hovland9647c462021-08-20 16:33:55 +0200237#else
238 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200239#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
240
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200241#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200242#if defined(CONFIG_SW_VECTOR_RELAY)
243 _vector_table_pointer = vt;
244#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
245 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200246#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200247#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
248 SCB->VTOR = (uint32_t)vt;
249#endif /* CONFIG_SW_VECTOR_RELAY */
250#else /* CONFIG_BOOT_INTR_VEC_RELOC */
251#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
252 _vector_table_pointer = _vector_start;
253 SCB->VTOR = (uint32_t)__vector_relay_table;
254#endif
255#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200256
David Brown57837b92018-03-13 15:56:38 -0600257 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100258#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
259 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100260 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100261#endif
David Brown0d0652a2017-04-11 17:33:30 -0600262 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800263}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530264
265#elif defined(CONFIG_XTENSA)
266#define SRAM_BASE_ADDRESS 0xBE030000
267
268static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
269{
270 const struct flash_area *fap;
271 int area_id;
272 int rc;
273 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
274
275 BOOT_LOG_INF("Copying image to SRAM");
276
277 area_id = flash_area_id_from_image_slot(slot);
278 rc = flash_area_open(area_id, &fap);
279 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300280 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530281 goto done;
282 }
283
284 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
285 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300286 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530287 goto done;
288 }
289
290done:
291 flash_area_close(fap);
292}
293
294/* Entry point (.ResetVector) is at the very beginning of the image.
295 * Simply copy the image to a suitable location and jump there.
296 */
297static void do_boot(struct boot_rsp *rsp)
298{
299 void *start;
300
301 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
302 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
303
304 /* Copy from the flash to HP SRAM */
305 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
306
307 /* Jump to entry point */
308 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
309 ((void (*)(void))start)();
310}
311
Andrew Boie7238f512017-03-02 13:39:06 -0800312#else
313/* Default: Assume entry point is at the very beginning of the image. Simply
314 * lock interrupts and jump there. This is the right thing to do for X86 and
315 * possibly other platforms.
316 */
317static void do_boot(struct boot_rsp *rsp)
318{
David Brown0d0652a2017-04-11 17:33:30 -0600319 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200320
Jim Tanee1b7b92022-04-07 11:26:28 +0800321#if defined(MCUBOOT_RAM_LOAD)
322 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
323#else
324 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400325 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800326
Marti Bolivareb940802017-05-01 23:15:29 -0400327 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
328 assert(rc == 0);
329
330 start = (void *)(flash_base + rsp->br_image_off +
331 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800332#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800333
David Brown0d0652a2017-04-11 17:33:30 -0600334 /* Lock interrupts and dive into the entry point */
335 irq_lock();
336 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800337}
338#endif
David Brown5153bd62017-01-06 11:16:53 -0700339
Marek Pietafb47d2e2022-03-11 14:24:07 +0100340#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000341 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100342/* The log internal thread for log processing can't transfer log well as has too
343 * low priority.
344 * Dedicated thread for log processing below uses highest application
345 * priority. This allows to transmit all logs without adding k_sleep/k_yield
346 * anywhere else int the code.
347 */
348
349/* most simple log processing theread */
350void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
351{
352 (void)dummy1;
353 (void)dummy2;
354 (void)dummy3;
355
Martin Jäger477ed812022-07-21 11:45:45 +0200356 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100357
Martin Jäger477ed812022-07-21 11:45:45 +0200358 while (1) {
Martin Jäger31751822022-07-21 11:49:32 +0200359#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
360 /* support Zephyr legacy logging implementation before commit c5f2cde */
Martin Jäger477ed812022-07-21 11:45:45 +0200361 if (log_process(false) == false) {
Martin Jäger31751822022-07-21 11:49:32 +0200362#else
363 if (log_process() == false) {
364#endif
Martin Jäger477ed812022-07-21 11:45:45 +0200365 if (boot_log_stop) {
366 break;
367 }
368 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
369 }
370 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100371
Martin Jäger477ed812022-07-21 11:45:45 +0200372 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100373}
374
375void zephyr_boot_log_start(void)
376{
Martin Jäger477ed812022-07-21 11:45:45 +0200377 /* start logging thread */
378 k_thread_create(&boot_log_thread, boot_log_stack,
379 K_THREAD_STACK_SIZEOF(boot_log_stack),
380 boot_log_thread_func, NULL, NULL, NULL,
381 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
382 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100383
Martin Jäger477ed812022-07-21 11:45:45 +0200384 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100385}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100386
387void zephyr_boot_log_stop(void)
388{
389 boot_log_stop = true;
390
391 /* wait until log procesing thread expired
392 * This can be reworked using a thread_join() API once a such will be
393 * available in zephyr.
394 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
395 */
396 (void)k_sem_take(&boot_log_sem, K_FOREVER);
397}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100398#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
399 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
400 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100401
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000402#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200403
404#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200405#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
406#else
407#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
408#endif
409
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200410#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
411
412#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200413static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000414#else
Jamie McCrae9551b6e2023-02-28 15:47:42 +0000415#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200416#endif
417
418static bool detect_pin(void)
Josh Gao837cf882020-11-13 18:51:27 -0800419{
420 int rc;
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200421 int pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800422
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200423 if (!device_is_ready(button0.port)) {
424 __ASSERT(false, "GPIO device is not ready.\n");
425 return false;
426 }
Josh Gao837cf882020-11-13 18:51:27 -0800427
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200428 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
Josh Gao837cf882020-11-13 18:51:27 -0800429 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
430
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200431 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200432 pin_active = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100433
Josh Gao837cf882020-11-13 18:51:27 -0800434 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
435
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200436 if (pin_active) {
437 if (BUTTON_0_DETECT_DELAY > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200438#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800439 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200440#else
441 k_busy_wait(50000);
442#endif
Josh Gao837cf882020-11-13 18:51:27 -0800443
444 /* Get the uptime for debounce purposes. */
445 int64_t timestamp = k_uptime_get();
446
447 for(;;) {
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200448 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200449 pin_active = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800450 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
451
452 /* Get delta from when this started */
453 uint32_t delta = k_uptime_get() - timestamp;
454
455 /* If not pressed OR if pressed > debounce period, stop. */
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200456 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
Josh Gao837cf882020-11-13 18:51:27 -0800457 break;
458 }
459
460 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200461#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800462 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200463#else
464 k_busy_wait(1000);
465#endif
Josh Gao837cf882020-11-13 18:51:27 -0800466 }
467 }
468 }
469
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200470 return (bool)pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800471}
472#endif
473
David Brown5153bd62017-01-06 11:16:53 -0700474void main(void)
475{
David Brown0d0652a2017-04-11 17:33:30 -0600476 struct boot_rsp rsp;
477 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100478 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Brown5153bd62017-01-06 11:16:53 -0700479
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000480#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
481 int32_t boot_mode;
482#endif
483
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200484 MCUBOOT_WATCHDOG_FEED();
485
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000486#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600487 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000488#else
489 BOOT_LOG_INF("Starting Direct-XIP bootloader");
490#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200491
Jared Wolff8e4d7912021-01-21 19:34:05 -0500492#ifdef CONFIG_MCUBOOT_INDICATION_LED
493 /* LED init */
494 led_init();
495#endif
496
David Brown0d0652a2017-04-11 17:33:30 -0600497 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700498
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100499 ZEPHYR_BOOT_LOG_START();
500
Tamas Banee6615d2020-09-30 07:58:48 +0100501 (void)rc;
502
Jamie McCrae56cb6102022-03-23 11:57:03 +0000503 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
504
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000505#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200506 if (detect_pin() &&
Josh Gao837cf882020-11-13 18:51:27 -0800507 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500508#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200509 gpio_pin_set_dt(&led0, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200510#endif
511
Jamie McCrae56cb6102022-03-23 11:57:03 +0000512 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
513
Josh Gao837cf882020-11-13 18:51:27 -0800514 BOOT_LOG_INF("Enter the serial recovery mode");
515 rc = boot_console_init();
516 __ASSERT(rc == 0, "Error initializing boot console.\n");
517 boot_serial_start(&boot_funcs);
518 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
519 }
520#endif
521
522#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200523 if (detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800524#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200525 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800526#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000527
528 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
529
Josh Gao837cf882020-11-13 18:51:27 -0800530 rc = usb_enable(NULL);
531 if (rc) {
532 BOOT_LOG_ERR("Cannot enable USB");
533 } else {
534 BOOT_LOG_INF("Waiting for USB DFU");
535 wait_for_usb_dfu(K_FOREVER);
536 BOOT_LOG_INF("USB DFU wait time elapsed");
537 }
538 }
539#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100540 rc = usb_enable(NULL);
541 if (rc) {
542 BOOT_LOG_ERR("Cannot enable USB");
543 } else {
544 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000545
546 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
547
Josh Gao837cf882020-11-13 18:51:27 -0800548 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100549 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000550
551 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100552 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530553#endif
554
Wouter Cappellee3822f82022-01-19 15:39:43 +0100555#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
556 /* Initialize the boot console, so we can already fill up our buffers while
557 * waiting for the boot image check to finish. This image check, can take
558 * some time, so it's better to reuse thistime to already receive the
559 * initial mcumgr command(s) into our buffers
560 */
561 rc = boot_console_init();
562 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
563 uint32_t start = k_uptime_get_32();
564#endif
565
Tamas Banee6615d2020-09-30 07:58:48 +0100566 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100567
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000568#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
569 boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
570
571 if (boot_mode == 1) {
572 /* Boot mode to stay in bootloader, clear status and enter serial
573 * recovery mode
574 */
575#ifdef CONFIG_MCUBOOT_INDICATION_LED
576 gpio_pin_set_dt(&led0, 1);
577#endif
578
579 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
580 rc = boot_console_init();
581 bootmode_clear();
582 boot_serial_start(&boot_funcs);
583 }
584#endif
585
Wouter Cappellee3822f82022-01-19 15:39:43 +0100586#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
587 timeout_in_ms -= (k_uptime_get_32() - start);
588 if( timeout_in_ms <= 0 ) {
589 /* at least one check if time was expired */
590 timeout_in_ms = 1;
591 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100592 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100593#endif
594
Michael Grand5047f032022-11-24 16:49:56 +0100595 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600596 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000597
598 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
599
Jamie McCraefd79db32023-03-10 11:19:36 +0000600#ifdef CONFIG_BOOT_SERIAL_NO_APPLICATION
601 /* No bootable image and configuration set to remain in serial
602 * recovery mode
603 */
604#ifdef CONFIG_MCUBOOT_INDICATION_LED
605 gpio_pin_set_dt(&led0, 1);
606#endif
607
608 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
609 rc = boot_console_init();
610 boot_serial_start(&boot_funcs);
611#endif
612
Tamas Banee6615d2020-09-30 07:58:48 +0100613 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600614 }
David Brown5153bd62017-01-06 11:16:53 -0700615
Marti Bolivar88f48d92017-05-01 22:30:02 -0400616 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
617 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200618
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000619#if defined(MCUBOOT_DIRECT_XIP)
620 BOOT_LOG_INF("Jumping to the image slot");
621#else
David Brown0d0652a2017-04-11 17:33:30 -0600622 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000623#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000624
625 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
626
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100627 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600628 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700629
Jamie McCrae56cb6102022-03-23 11:57:03 +0000630 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
631
David Brown0d0652a2017-04-11 17:33:30 -0600632 BOOT_LOG_ERR("Never should get here");
633 while (1)
634 ;
David Brown5153bd62017-01-06 11:16:53 -0700635}