blob: 3bd8fd35e87914b353ea772f8022ee8d6b991e0e [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
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +00004 * Copyright (c) 2021 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
Marti Bolivar51181cf2017-03-20 11:03:41 -040030#include "target.h"
31
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050032#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020033#include "bootutil/image.h"
34#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010035#include "bootutil/fault_injection_hardening.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000036#include "bootutil/mcuboot_status.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020037#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020038
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020039#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030040#include "boot_serial/boot_serial.h"
41#include "serial_adapter/serial_adapter.h"
42
43const struct boot_uart_funcs boot_funcs = {
44 .read = console_read,
45 .write = console_write
46};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020047#endif
48
Josh Gao837cf882020-11-13 18:51:27 -080049#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020050#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053051#endif
52
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010053#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
54#include <arm_cleanup.h>
55#endif
56
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010057/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
58 * replaced by CONFIG_LOG_MODE_MINIMAL.
59 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000060#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
61#define ZEPHYR_LOG_MODE_MINIMAL 1
62#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010063
Marek Pietafb47d2e2022-03-11 14:24:07 +010064/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
65 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
66 */
67#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
68#define ZEPHYR_LOG_MODE_IMMEDIATE 1
69#endif
70
71#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000072 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010073#ifdef CONFIG_LOG_PROCESS_THREAD
74#warning "The log internal thread for log processing can't transfer the log"\
75 "well for MCUBoot."
76#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020077#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010078
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010079#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010080
81/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010082K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010083struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010084volatile bool boot_log_stop = false;
85K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010086
87/* log processing need to be initalized by the application */
88#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010089#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010090#endif /* CONFIG_LOG_PROCESS_THREAD */
91#else
92/* synchronous log mode doesn't need to be initalized by the application */
93#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010094#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +010095#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
96 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
97 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010098
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010099#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +0100100#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100101
102static inline bool boot_skip_serial_recovery()
103{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100104 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100105
Radoslaw Koppel72006692021-12-06 16:15:21 +0100106 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100107}
108#else
109static inline bool boot_skip_serial_recovery()
110{
111 return false;
112}
113#endif
114
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200115BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100116
Jared Wolff8e4d7912021-01-21 19:34:05 -0500117#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500118
119/*
120 * The led0 devicetree alias is optional. If present, we'll use it
121 * to turn on the LED whenever the button is pressed.
122 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200123#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200124#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200125#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
126#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500127#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200128#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500129
130#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200131static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800132#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500133/* A build error here means your board isn't set up to drive an LED. */
134#error "Unsupported board: led0 devicetree alias is not defined"
135#endif
136
Jared Wolff8e4d7912021-01-21 19:34:05 -0500137void led_init(void)
138{
Martin Jäger477ed812022-07-21 11:45:45 +0200139 if (!device_is_ready(led0.port)) {
140 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
141 return;
142 }
Josh Gao837cf882020-11-13 18:51:27 -0800143
Martin Jäger477ed812022-07-21 11:45:45 +0200144 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
145 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500146}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200147#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500148
Andrew Boie7238f512017-03-02 13:39:06 -0800149void os_heap_init(void);
150
151#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200152
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200153#ifdef CONFIG_SW_VECTOR_RELAY
154extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200155#endif
156
Andrew Boie7238f512017-03-02 13:39:06 -0800157struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600158 uint32_t msp;
159 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700160};
161
Andrew Boie7238f512017-03-02 13:39:06 -0800162static void do_boot(struct boot_rsp *rsp)
163{
David Brown0d0652a2017-04-11 17:33:30 -0600164 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400165 uintptr_t flash_base;
166 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800167
David Brown0d0652a2017-04-11 17:33:30 -0600168 /* The beginning of the image is the ARM vector table, containing
169 * the initial stack pointer address and the reset vector
170 * consecutively. Manually set the stack pointer and jump into the
171 * reset vector
172 */
Marti Bolivareb940802017-05-01 23:15:29 -0400173 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
174 assert(rc == 0);
175
176 vt = (struct arm_vector_table *)(flash_base +
177 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600178 rsp->br_hdr->ih_hdr_size);
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200179
David Brown0d0652a2017-04-11 17:33:30 -0600180 sys_clock_disable();
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100181
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200182#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200183 /* Disable the USB to prevent it from firing interrupts */
184 usb_disable();
185#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100186#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
187 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200188
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700189#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200190 /* Disable instruction cache and data cache before chain-load the application */
191 SCB_DisableDCache();
192 SCB_DisableICache();
193#endif
194
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100195#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200196 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100197#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200198
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000199#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
200 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
201 /* Reset limit registers to avoid inflicting stack overflow on image
202 * being booted.
203 */
204 __set_PSPLIM(0);
205 __set_MSPLIM(0);
206#endif
207
Sigvart Hovland9647c462021-08-20 16:33:55 +0200208#else
209 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200210#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
211
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200212#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200213#if defined(CONFIG_SW_VECTOR_RELAY)
214 _vector_table_pointer = vt;
215#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
216 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200217#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200218#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
219 SCB->VTOR = (uint32_t)vt;
220#endif /* CONFIG_SW_VECTOR_RELAY */
221#else /* CONFIG_BOOT_INTR_VEC_RELOC */
222#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
223 _vector_table_pointer = _vector_start;
224 SCB->VTOR = (uint32_t)__vector_relay_table;
225#endif
226#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200227
David Brown57837b92018-03-13 15:56:38 -0600228 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100229#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
230 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100231 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100232#endif
David Brown0d0652a2017-04-11 17:33:30 -0600233 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800234}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530235
236#elif defined(CONFIG_XTENSA)
237#define SRAM_BASE_ADDRESS 0xBE030000
238
239static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
240{
241 const struct flash_area *fap;
242 int area_id;
243 int rc;
244 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
245
246 BOOT_LOG_INF("Copying image to SRAM");
247
248 area_id = flash_area_id_from_image_slot(slot);
249 rc = flash_area_open(area_id, &fap);
250 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300251 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530252 goto done;
253 }
254
255 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
256 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300257 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530258 goto done;
259 }
260
261done:
262 flash_area_close(fap);
263}
264
265/* Entry point (.ResetVector) is at the very beginning of the image.
266 * Simply copy the image to a suitable location and jump there.
267 */
268static void do_boot(struct boot_rsp *rsp)
269{
270 void *start;
271
272 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
273 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
274
275 /* Copy from the flash to HP SRAM */
276 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
277
278 /* Jump to entry point */
279 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
280 ((void (*)(void))start)();
281}
282
Andrew Boie7238f512017-03-02 13:39:06 -0800283#else
284/* Default: Assume entry point is at the very beginning of the image. Simply
285 * lock interrupts and jump there. This is the right thing to do for X86 and
286 * possibly other platforms.
287 */
288static void do_boot(struct boot_rsp *rsp)
289{
David Brown0d0652a2017-04-11 17:33:30 -0600290 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200291
Jim Tanee1b7b92022-04-07 11:26:28 +0800292#if defined(MCUBOOT_RAM_LOAD)
293 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
294#else
295 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400296 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800297
Marti Bolivareb940802017-05-01 23:15:29 -0400298 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
299 assert(rc == 0);
300
301 start = (void *)(flash_base + rsp->br_image_off +
302 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800303#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800304
David Brown0d0652a2017-04-11 17:33:30 -0600305 /* Lock interrupts and dive into the entry point */
306 irq_lock();
307 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800308}
309#endif
David Brown5153bd62017-01-06 11:16:53 -0700310
Marek Pietafb47d2e2022-03-11 14:24:07 +0100311#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000312 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100313/* The log internal thread for log processing can't transfer log well as has too
314 * low priority.
315 * Dedicated thread for log processing below uses highest application
316 * priority. This allows to transmit all logs without adding k_sleep/k_yield
317 * anywhere else int the code.
318 */
319
320/* most simple log processing theread */
321void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
322{
323 (void)dummy1;
324 (void)dummy2;
325 (void)dummy3;
326
Martin Jäger477ed812022-07-21 11:45:45 +0200327 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100328
Martin Jäger477ed812022-07-21 11:45:45 +0200329 while (1) {
330 if (log_process(false) == false) {
331 if (boot_log_stop) {
332 break;
333 }
334 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
335 }
336 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100337
Martin Jäger477ed812022-07-21 11:45:45 +0200338 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100339}
340
341void zephyr_boot_log_start(void)
342{
Martin Jäger477ed812022-07-21 11:45:45 +0200343 /* start logging thread */
344 k_thread_create(&boot_log_thread, boot_log_stack,
345 K_THREAD_STACK_SIZEOF(boot_log_stack),
346 boot_log_thread_func, NULL, NULL, NULL,
347 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
348 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100349
Martin Jäger477ed812022-07-21 11:45:45 +0200350 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100351}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100352
353void zephyr_boot_log_stop(void)
354{
355 boot_log_stop = true;
356
357 /* wait until log procesing thread expired
358 * This can be reworked using a thread_join() API once a such will be
359 * available in zephyr.
360 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
361 */
362 (void)k_sem_take(&boot_log_sem, K_FOREVER);
363}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100364#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
365 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
366 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100367
Josh Gao837cf882020-11-13 18:51:27 -0800368#if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200369
370#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200371#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
372#else
373#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
374#endif
375
376
377#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
378
379#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
380
381static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
382
383#else /* fallback to legacy configuration */
384
385#if defined(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200386
387#define BUTTON_0_GPIO_LABEL CONFIG_BOOT_SERIAL_DETECT_PORT
388#define BUTTON_0_GPIO_PIN CONFIG_BOOT_SERIAL_DETECT_PIN
389#define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) ?\
390 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\
391 (GPIO_ACTIVE_LOW | GPIO_PULL_UP))
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200392
393#elif defined(CONFIG_BOOT_USB_DFU_GPIO)
394
395#define BUTTON_0_GPIO_LABEL CONFIG_BOOT_USB_DFU_DETECT_PORT
396#define BUTTON_0_GPIO_PIN CONFIG_BOOT_USB_DFU_DETECT_PIN
397#define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL) ?\
398 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\
399 (GPIO_ACTIVE_LOW | GPIO_PULL_UP))
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200400
401#endif
402
403#define BUTTON_0_LEGACY 1
404
405static struct gpio_dt_spec button0 = {
406 .port = NULL,
407 .pin = BUTTON_0_GPIO_PIN,
408 .dt_flags = BUTTON_0_GPIO_FLAGS
409};
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200410
411#endif
412
413static bool detect_pin(void)
Josh Gao837cf882020-11-13 18:51:27 -0800414{
415 int rc;
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200416 int pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800417
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200418#ifdef BUTTON_0_LEGACY
419 button0.port = device_get_binding(BUTTON_0_GPIO_LABEL);
420 if (button0.port == NULL) {
421 __ASSERT(false, "Error: Bad port for boot detection.\n");
422 return false;
423 }
424#else
425 if (!device_is_ready(button0.port)) {
426 __ASSERT(false, "GPIO device is not ready.\n");
427 return false;
428 }
429#endif
Josh Gao837cf882020-11-13 18:51:27 -0800430
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200431 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
Josh Gao837cf882020-11-13 18:51:27 -0800432 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
433
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200434 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200435 pin_active = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100436
Josh Gao837cf882020-11-13 18:51:27 -0800437 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
438
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200439 if (pin_active) {
440 if (BUTTON_0_DETECT_DELAY > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200441#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800442 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200443#else
444 k_busy_wait(50000);
445#endif
Josh Gao837cf882020-11-13 18:51:27 -0800446
447 /* Get the uptime for debounce purposes. */
448 int64_t timestamp = k_uptime_get();
449
450 for(;;) {
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200451 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200452 pin_active = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800453 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
454
455 /* Get delta from when this started */
456 uint32_t delta = k_uptime_get() - timestamp;
457
458 /* If not pressed OR if pressed > debounce period, stop. */
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200459 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
Josh Gao837cf882020-11-13 18:51:27 -0800460 break;
461 }
462
463 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200464#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800465 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200466#else
467 k_busy_wait(1000);
468#endif
Josh Gao837cf882020-11-13 18:51:27 -0800469 }
470 }
471 }
472
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200473 return (bool)pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800474}
475#endif
476
David Brown5153bd62017-01-06 11:16:53 -0700477void main(void)
478{
David Brown0d0652a2017-04-11 17:33:30 -0600479 struct boot_rsp rsp;
480 int rc;
Tamas Banee6615d2020-09-30 07:58:48 +0100481 fih_int fih_rc = FIH_FAILURE;
David Brown5153bd62017-01-06 11:16:53 -0700482
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200483 MCUBOOT_WATCHDOG_FEED();
484
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000485#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600486 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000487#else
488 BOOT_LOG_INF("Starting Direct-XIP bootloader");
489#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200490
Jared Wolff8e4d7912021-01-21 19:34:05 -0500491#ifdef CONFIG_MCUBOOT_INDICATION_LED
492 /* LED init */
493 led_init();
494#endif
495
David Brown0d0652a2017-04-11 17:33:30 -0600496 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700497
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100498 ZEPHYR_BOOT_LOG_START();
499
Tamas Banee6615d2020-09-30 07:58:48 +0100500 (void)rc;
501
Jamie McCrae56cb6102022-03-23 11:57:03 +0000502 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
503
Gerard Marull-Paretasaa041a22022-03-25 12:22:29 +0100504#if (!defined(CONFIG_XTENSA) && DT_HAS_CHOSEN(zephyr_flash_controller))
505 if (!flash_device_get_binding(DT_LABEL(DT_CHOSEN(zephyr_flash_controller)))) {
Kumar Gala32b61f32020-04-08 12:02:03 -0500506 BOOT_LOG_ERR("Flash device %s not found",
Gerard Marull-Paretasaa041a22022-03-25 12:22:29 +0100507 DT_LABEL(DT_CHOSEN(zephyr_flash_controller)));
David Brown0d0652a2017-04-11 17:33:30 -0600508 while (1)
509 ;
510 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500511#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
512 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
513 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530514 while (1)
515 ;
516 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530517#endif
David Brown5153bd62017-01-06 11:16:53 -0700518
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200519#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200520 if (detect_pin() &&
Josh Gao837cf882020-11-13 18:51:27 -0800521 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500522#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200523 gpio_pin_set_dt(&led0, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200524#endif
525
Jamie McCrae56cb6102022-03-23 11:57:03 +0000526 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
527
Josh Gao837cf882020-11-13 18:51:27 -0800528 BOOT_LOG_INF("Enter the serial recovery mode");
529 rc = boot_console_init();
530 __ASSERT(rc == 0, "Error initializing boot console.\n");
531 boot_serial_start(&boot_funcs);
532 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
533 }
534#endif
535
536#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200537 if (detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800538#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200539 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800540#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000541
542 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
543
Josh Gao837cf882020-11-13 18:51:27 -0800544 rc = usb_enable(NULL);
545 if (rc) {
546 BOOT_LOG_ERR("Cannot enable USB");
547 } else {
548 BOOT_LOG_INF("Waiting for USB DFU");
549 wait_for_usb_dfu(K_FOREVER);
550 BOOT_LOG_INF("USB DFU wait time elapsed");
551 }
552 }
553#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100554 rc = usb_enable(NULL);
555 if (rc) {
556 BOOT_LOG_ERR("Cannot enable USB");
557 } else {
558 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000559
560 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
561
Josh Gao837cf882020-11-13 18:51:27 -0800562 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100563 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000564
565 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100566 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530567#endif
568
Wouter Cappellee3822f82022-01-19 15:39:43 +0100569#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
570 /* Initialize the boot console, so we can already fill up our buffers while
571 * waiting for the boot image check to finish. This image check, can take
572 * some time, so it's better to reuse thistime to already receive the
573 * initial mcumgr command(s) into our buffers
574 */
575 rc = boot_console_init();
576 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
577 uint32_t start = k_uptime_get_32();
578#endif
579
Tamas Banee6615d2020-09-30 07:58:48 +0100580 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100581
582#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
583 timeout_in_ms -= (k_uptime_get_32() - start);
584 if( timeout_in_ms <= 0 ) {
585 /* at least one check if time was expired */
586 timeout_in_ms = 1;
587 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100588 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100589#endif
590
Tamas Banee6615d2020-09-30 07:58:48 +0100591 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600592 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000593
594 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
595
Tamas Banee6615d2020-09-30 07:58:48 +0100596 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600597 }
David Brown5153bd62017-01-06 11:16:53 -0700598
Marti Bolivar88f48d92017-05-01 22:30:02 -0400599 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
600 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200601
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000602#if defined(MCUBOOT_DIRECT_XIP)
603 BOOT_LOG_INF("Jumping to the image slot");
604#else
David Brown0d0652a2017-04-11 17:33:30 -0600605 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000606#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000607
608 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
609
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100610 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600611 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700612
Jamie McCrae56cb6102022-03-23 11:57:03 +0000613 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
614
David Brown0d0652a2017-04-11 17:33:30 -0600615 BOOT_LOG_ERR("Never should get here");
616 while (1)
617 ;
David Brown5153bd62017-01-06 11:16:53 -0700618}