blob: 0a0c874027b2ff2169506fac2a660816eae71483 [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>
David Brown5153bd62017-01-06 11:16:53 -070020#include <zephyr.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060021#include <drivers/gpio.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010022#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060023#include <drivers/flash.h>
Andrzej Puzdrowskif99a4c72019-06-28 15:16:35 +020024#include <drivers/timer/system_timer.h>
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020025#include <usb/usb_device.h>
Evan Gates4632d8d2018-06-28 13:27:40 -070026#include <soc.h>
Rafał Kuźnia505c6e62020-07-17 13:18:15 +020027#include <linker/linker-defs.h>
David Brown5153bd62017-01-06 11:16:53 -070028
Marti Bolivar51181cf2017-03-20 11:03:41 -040029#include "target.h"
30
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050031#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020032#include "bootutil/image.h"
33#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010034#include "bootutil/fault_injection_hardening.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020035#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020036
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020037#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030038#include "boot_serial/boot_serial.h"
39#include "serial_adapter/serial_adapter.h"
40
41const struct boot_uart_funcs boot_funcs = {
42 .read = console_read,
43 .write = console_write
44};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020045#endif
46
Josh Gao837cf882020-11-13 18:51:27 -080047#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053048#include <usb/class/usb_dfu.h>
49#endif
50
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010051#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
52#include <arm_cleanup.h>
53#endif
54
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010055/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
56 * replaced by CONFIG_LOG_MODE_MINIMAL.
57 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000058#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
59#define ZEPHYR_LOG_MODE_MINIMAL 1
60#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010061
Marek Pietafb47d2e2022-03-11 14:24:07 +010062/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
63 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
64 */
65#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
66#define ZEPHYR_LOG_MODE_IMMEDIATE 1
67#endif
68
69#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000070 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010071#ifdef CONFIG_LOG_PROCESS_THREAD
72#warning "The log internal thread for log processing can't transfer the log"\
73 "well for MCUBoot."
74#else
75#include <logging/log_ctrl.h>
76
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010077#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010078
79/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010080K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010081struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010082volatile bool boot_log_stop = false;
83K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010084
85/* log processing need to be initalized by the application */
86#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010087#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010088#endif /* CONFIG_LOG_PROCESS_THREAD */
89#else
90/* synchronous log mode doesn't need to be initalized by the application */
91#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010092#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +010093#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
94 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
95 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010096
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010097#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +010098#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010099
100static inline bool boot_skip_serial_recovery()
101{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100102 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100103
Radoslaw Koppel72006692021-12-06 16:15:21 +0100104 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100105}
106#else
107static inline bool boot_skip_serial_recovery()
108{
109 return false;
110}
111#endif
112
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200113BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100114
Jared Wolff8e4d7912021-01-21 19:34:05 -0500115#ifdef CONFIG_MCUBOOT_INDICATION_LED
116/*
117 * Devicetree helper macro which gets the 'flags' cell from a 'gpios'
118 * property, or returns 0 if the property has no 'flags' cell.
119 */
120#define FLAGS_OR_ZERO(node) \
121 COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \
122 (DT_GPIO_FLAGS(node, gpios)), \
123 (0))
124
125/*
126 * The led0 devicetree alias is optional. If present, we'll use it
127 * to turn on the LED whenever the button is pressed.
128 */
129
130#define LED0_NODE DT_ALIAS(bootloader_led0)
131
132#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
133#define LED0_GPIO_LABEL DT_GPIO_LABEL(LED0_NODE, gpios)
134#define LED0_GPIO_PIN DT_GPIO_PIN(LED0_NODE, gpios)
135#define LED0_GPIO_FLAGS (GPIO_OUTPUT | FLAGS_OR_ZERO(LED0_NODE))
Josh Gao837cf882020-11-13 18:51:27 -0800136#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500137/* A build error here means your board isn't set up to drive an LED. */
138#error "Unsupported board: led0 devicetree alias is not defined"
139#endif
140
141const static struct device *led;
142
143void led_init(void)
144{
Josh Gao837cf882020-11-13 18:51:27 -0800145
Jared Wolff8e4d7912021-01-21 19:34:05 -0500146 led = device_get_binding(LED0_GPIO_LABEL);
Jared Wolffdf8e9742021-02-04 11:17:00 -0500147 if (led == NULL) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500148 BOOT_LOG_ERR("Didn't find LED device %s\n", LED0_GPIO_LABEL);
149 return;
150 }
151
152 gpio_pin_configure(led, LED0_GPIO_PIN, LED0_GPIO_FLAGS);
Jared Wolffdf8e9742021-02-04 11:17:00 -0500153 gpio_pin_set(led, LED0_GPIO_PIN, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500154
155}
156#endif
157
Andrew Boie7238f512017-03-02 13:39:06 -0800158void os_heap_init(void);
159
160#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200161
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200162#ifdef CONFIG_SW_VECTOR_RELAY
163extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200164#endif
165
Andrew Boie7238f512017-03-02 13:39:06 -0800166struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600167 uint32_t msp;
168 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700169};
170
Andrew Boie7238f512017-03-02 13:39:06 -0800171static void do_boot(struct boot_rsp *rsp)
172{
David Brown0d0652a2017-04-11 17:33:30 -0600173 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400174 uintptr_t flash_base;
175 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800176
David Brown0d0652a2017-04-11 17:33:30 -0600177 /* The beginning of the image is the ARM vector table, containing
178 * the initial stack pointer address and the reset vector
179 * consecutively. Manually set the stack pointer and jump into the
180 * reset vector
181 */
Marti Bolivareb940802017-05-01 23:15:29 -0400182 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
183 assert(rc == 0);
184
185 vt = (struct arm_vector_table *)(flash_base +
186 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600187 rsp->br_hdr->ih_hdr_size);
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200188
David Brown0d0652a2017-04-11 17:33:30 -0600189 sys_clock_disable();
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100190
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200191#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200192 /* Disable the USB to prevent it from firing interrupts */
193 usb_disable();
194#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100195#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
196 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200197
198#ifdef CONFIG_CPU_CORTEX_M7
199 /* Disable instruction cache and data cache before chain-load the application */
200 SCB_DisableDCache();
201 SCB_DisableICache();
202#endif
203
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100204#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200205 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100206#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200207
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000208#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
209 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
210 /* Reset limit registers to avoid inflicting stack overflow on image
211 * being booted.
212 */
213 __set_PSPLIM(0);
214 __set_MSPLIM(0);
215#endif
216
Sigvart Hovland9647c462021-08-20 16:33:55 +0200217#else
218 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200219#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
220
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200221#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200222#if defined(CONFIG_SW_VECTOR_RELAY)
223 _vector_table_pointer = vt;
224#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
225 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200226#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200227#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
228 SCB->VTOR = (uint32_t)vt;
229#endif /* CONFIG_SW_VECTOR_RELAY */
230#else /* CONFIG_BOOT_INTR_VEC_RELOC */
231#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
232 _vector_table_pointer = _vector_start;
233 SCB->VTOR = (uint32_t)__vector_relay_table;
234#endif
235#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200236
David Brown57837b92018-03-13 15:56:38 -0600237 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100238#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
239 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100240 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100241#endif
David Brown0d0652a2017-04-11 17:33:30 -0600242 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800243}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530244
245#elif defined(CONFIG_XTENSA)
246#define SRAM_BASE_ADDRESS 0xBE030000
247
248static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
249{
250 const struct flash_area *fap;
251 int area_id;
252 int rc;
253 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
254
255 BOOT_LOG_INF("Copying image to SRAM");
256
257 area_id = flash_area_id_from_image_slot(slot);
258 rc = flash_area_open(area_id, &fap);
259 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300260 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530261 goto done;
262 }
263
264 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
265 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300266 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530267 goto done;
268 }
269
270done:
271 flash_area_close(fap);
272}
273
274/* Entry point (.ResetVector) is at the very beginning of the image.
275 * Simply copy the image to a suitable location and jump there.
276 */
277static void do_boot(struct boot_rsp *rsp)
278{
279 void *start;
280
281 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
282 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
283
284 /* Copy from the flash to HP SRAM */
285 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
286
287 /* Jump to entry point */
288 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
289 ((void (*)(void))start)();
290}
291
Andrew Boie7238f512017-03-02 13:39:06 -0800292#else
293/* Default: Assume entry point is at the very beginning of the image. Simply
294 * lock interrupts and jump there. This is the right thing to do for X86 and
295 * possibly other platforms.
296 */
297static void do_boot(struct boot_rsp *rsp)
298{
Marti Bolivareb940802017-05-01 23:15:29 -0400299 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600300 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400301 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800302
Marti Bolivareb940802017-05-01 23:15:29 -0400303 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
304 assert(rc == 0);
305
306 start = (void *)(flash_base + rsp->br_image_off +
307 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800308
David Brown0d0652a2017-04-11 17:33:30 -0600309 /* Lock interrupts and dive into the entry point */
310 irq_lock();
311 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800312}
313#endif
David Brown5153bd62017-01-06 11:16:53 -0700314
Marek Pietafb47d2e2022-03-11 14:24:07 +0100315#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000316 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100317/* The log internal thread for log processing can't transfer log well as has too
318 * low priority.
319 * Dedicated thread for log processing below uses highest application
320 * priority. This allows to transmit all logs without adding k_sleep/k_yield
321 * anywhere else int the code.
322 */
323
324/* most simple log processing theread */
325void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
326{
327 (void)dummy1;
328 (void)dummy2;
329 (void)dummy3;
330
331 log_init();
332
333 while (1) {
334 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100335 if (boot_log_stop) {
336 break;
337 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100338 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
339 }
340 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100341
342 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100343}
344
345void zephyr_boot_log_start(void)
346{
347 /* start logging thread */
348 k_thread_create(&boot_log_thread, boot_log_stack,
349 K_THREAD_STACK_SIZEOF(boot_log_stack),
350 boot_log_thread_func, NULL, NULL, NULL,
351 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
352 BOOT_LOG_PROCESSING_INTERVAL);
353
354 k_thread_name_set(&boot_log_thread, "logging");
355}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100356
357void zephyr_boot_log_stop(void)
358{
359 boot_log_stop = true;
360
361 /* wait until log procesing thread expired
362 * This can be reworked using a thread_join() API once a such will be
363 * available in zephyr.
364 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
365 */
366 (void)k_sem_take(&boot_log_sem, K_FOREVER);
367}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100368#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
369 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
370 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100371
Josh Gao837cf882020-11-13 18:51:27 -0800372#if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO)
373static bool detect_pin(const char* port, int pin, uint32_t expected, int delay)
374{
375 int rc;
376 int detect_value;
377 struct device const *detect_port;
378
379 detect_port = device_get_binding(port);
380 __ASSERT(detect_port, "Error: Bad port for boot detection.\n");
381
382 /* The default presence value is 0 which would normally be
383 * active-low, but historically the raw value was checked so we'll
384 * use the raw interface.
385 */
386 rc = gpio_pin_configure(detect_port, pin,
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100387 GPIO_INPUT | GPIO_PULL_UP);
Josh Gao837cf882020-11-13 18:51:27 -0800388 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
389
Josh Gao837cf882020-11-13 18:51:27 -0800390 rc = gpio_pin_get_raw(detect_port, pin);
391 detect_value = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100392
Josh Gao837cf882020-11-13 18:51:27 -0800393 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
394
395 if (detect_value == expected) {
396 if (delay > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200397#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800398 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200399#else
400 k_busy_wait(50000);
401#endif
Josh Gao837cf882020-11-13 18:51:27 -0800402
403 /* Get the uptime for debounce purposes. */
404 int64_t timestamp = k_uptime_get();
405
406 for(;;) {
Josh Gao837cf882020-11-13 18:51:27 -0800407 rc = gpio_pin_get_raw(detect_port, pin);
408 detect_value = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800409 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
410
411 /* Get delta from when this started */
412 uint32_t delta = k_uptime_get() - timestamp;
413
414 /* If not pressed OR if pressed > debounce period, stop. */
415 if (delta >= delay || detect_value != expected) {
416 break;
417 }
418
419 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200420#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800421 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200422#else
423 k_busy_wait(1000);
424#endif
Josh Gao837cf882020-11-13 18:51:27 -0800425 }
426 }
427 }
428
429 return detect_value == expected;
430}
431#endif
432
David Brown5153bd62017-01-06 11:16:53 -0700433void main(void)
434{
David Brown0d0652a2017-04-11 17:33:30 -0600435 struct boot_rsp rsp;
436 int rc;
Tamas Banee6615d2020-09-30 07:58:48 +0100437 fih_int fih_rc = FIH_FAILURE;
David Brown5153bd62017-01-06 11:16:53 -0700438
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200439 MCUBOOT_WATCHDOG_FEED();
440
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000441#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600442 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000443#else
444 BOOT_LOG_INF("Starting Direct-XIP bootloader");
445#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200446
Jared Wolff8e4d7912021-01-21 19:34:05 -0500447#ifdef CONFIG_MCUBOOT_INDICATION_LED
448 /* LED init */
449 led_init();
450#endif
451
David Brown0d0652a2017-04-11 17:33:30 -0600452 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700453
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100454 ZEPHYR_BOOT_LOG_START();
455
Tamas Banee6615d2020-09-30 07:58:48 +0100456 (void)rc;
457
Kumar Gala32b61f32020-04-08 12:02:03 -0500458#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
459 if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) {
460 BOOT_LOG_ERR("Flash device %s not found",
461 DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
David Brown0d0652a2017-04-11 17:33:30 -0600462 while (1)
463 ;
464 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500465#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
466 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
467 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530468 while (1)
469 ;
470 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530471#endif
David Brown5153bd62017-01-06 11:16:53 -0700472
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200473#ifdef CONFIG_MCUBOOT_SERIAL
Josh Gao837cf882020-11-13 18:51:27 -0800474 if (detect_pin(CONFIG_BOOT_SERIAL_DETECT_PORT,
475 CONFIG_BOOT_SERIAL_DETECT_PIN,
476 CONFIG_BOOT_SERIAL_DETECT_PIN_VAL,
477 CONFIG_BOOT_SERIAL_DETECT_DELAY) &&
478 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500479#ifdef CONFIG_MCUBOOT_INDICATION_LED
Josh Gao837cf882020-11-13 18:51:27 -0800480 gpio_pin_set(led, LED0_GPIO_PIN, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200481#endif
482
Josh Gao837cf882020-11-13 18:51:27 -0800483 BOOT_LOG_INF("Enter the serial recovery mode");
484 rc = boot_console_init();
485 __ASSERT(rc == 0, "Error initializing boot console.\n");
486 boot_serial_start(&boot_funcs);
487 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
488 }
489#endif
490
491#if defined(CONFIG_BOOT_USB_DFU_GPIO)
492 if (detect_pin(CONFIG_BOOT_USB_DFU_DETECT_PORT,
493 CONFIG_BOOT_USB_DFU_DETECT_PIN,
494 CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL,
495 CONFIG_BOOT_USB_DFU_DETECT_DELAY)) {
496#ifdef CONFIG_MCUBOOT_INDICATION_LED
497 gpio_pin_set(led, LED0_GPIO_PIN, 1);
498#endif
499 rc = usb_enable(NULL);
500 if (rc) {
501 BOOT_LOG_ERR("Cannot enable USB");
502 } else {
503 BOOT_LOG_INF("Waiting for USB DFU");
504 wait_for_usb_dfu(K_FOREVER);
505 BOOT_LOG_INF("USB DFU wait time elapsed");
506 }
507 }
508#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100509 rc = usb_enable(NULL);
510 if (rc) {
511 BOOT_LOG_ERR("Cannot enable USB");
512 } else {
513 BOOT_LOG_INF("Waiting for USB DFU");
Josh Gao837cf882020-11-13 18:51:27 -0800514 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100515 BOOT_LOG_INF("USB DFU wait time elapsed");
516 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530517#endif
518
Wouter Cappellee3822f82022-01-19 15:39:43 +0100519#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
520 /* Initialize the boot console, so we can already fill up our buffers while
521 * waiting for the boot image check to finish. This image check, can take
522 * some time, so it's better to reuse thistime to already receive the
523 * initial mcumgr command(s) into our buffers
524 */
525 rc = boot_console_init();
526 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
527 uint32_t start = k_uptime_get_32();
528#endif
529
Tamas Banee6615d2020-09-30 07:58:48 +0100530 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100531
532#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
533 timeout_in_ms -= (k_uptime_get_32() - start);
534 if( timeout_in_ms <= 0 ) {
535 /* at least one check if time was expired */
536 timeout_in_ms = 1;
537 }
538 boot_serial_check_start(&boot_funcs,timeout_in_ms);
539#endif
540
Tamas Banee6615d2020-09-30 07:58:48 +0100541 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600542 BOOT_LOG_ERR("Unable to find bootable image");
Tamas Banee6615d2020-09-30 07:58:48 +0100543 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600544 }
David Brown5153bd62017-01-06 11:16:53 -0700545
Marti Bolivar88f48d92017-05-01 22:30:02 -0400546 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
547 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200548
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000549#if defined(MCUBOOT_DIRECT_XIP)
550 BOOT_LOG_INF("Jumping to the image slot");
551#else
David Brown0d0652a2017-04-11 17:33:30 -0600552 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000553#endif
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100554 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600555 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700556
David Brown0d0652a2017-04-11 17:33:30 -0600557 BOOT_LOG_ERR("Never should get here");
558 while (1)
559 ;
David Brown5153bd62017-01-06 11:16:53 -0700560}