blob: e885fb05138e5c3728bdeefa973cc855f6d58593 [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>
Gerard Marull-Paretasaa041a22022-03-25 12:22:29 +010021#include <devicetree.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060022#include <drivers/gpio.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010023#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060024#include <drivers/flash.h>
Andrzej Puzdrowskif99a4c72019-06-28 15:16:35 +020025#include <drivers/timer/system_timer.h>
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020026#include <usb/usb_device.h>
Evan Gates4632d8d2018-06-28 13:27:40 -070027#include <soc.h>
Rafał Kuźnia505c6e62020-07-17 13:18:15 +020028#include <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"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020036#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020037
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020038#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030039#include "boot_serial/boot_serial.h"
40#include "serial_adapter/serial_adapter.h"
41
42const struct boot_uart_funcs boot_funcs = {
43 .read = console_read,
44 .write = console_write
45};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020046#endif
47
Josh Gao837cf882020-11-13 18:51:27 -080048#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053049#include <usb/class/usb_dfu.h>
50#endif
51
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010052#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
53#include <arm_cleanup.h>
54#endif
55
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010056/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
57 * replaced by CONFIG_LOG_MODE_MINIMAL.
58 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000059#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
60#define ZEPHYR_LOG_MODE_MINIMAL 1
61#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010062
Marek Pietafb47d2e2022-03-11 14:24:07 +010063/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
64 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
65 */
66#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
67#define ZEPHYR_LOG_MODE_IMMEDIATE 1
68#endif
69
70#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000071 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010072#ifdef CONFIG_LOG_PROCESS_THREAD
73#warning "The log internal thread for log processing can't transfer the log"\
74 "well for MCUBoot."
75#else
76#include <logging/log_ctrl.h>
77
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010078#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010079
80/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010081K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010082struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010083volatile bool boot_log_stop = false;
84K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010085
86/* log processing need to be initalized by the application */
87#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010088#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010089#endif /* CONFIG_LOG_PROCESS_THREAD */
90#else
91/* synchronous log mode doesn't need to be initalized by the application */
92#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010093#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +010094#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
95 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
96 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010097
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010098#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +010099#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100100
101static inline bool boot_skip_serial_recovery()
102{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100103 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100104
Radoslaw Koppel72006692021-12-06 16:15:21 +0100105 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100106}
107#else
108static inline bool boot_skip_serial_recovery()
109{
110 return false;
111}
112#endif
113
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200114BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100115
Jared Wolff8e4d7912021-01-21 19:34:05 -0500116#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500117
118/*
119 * The led0 devicetree alias is optional. If present, we'll use it
120 * to turn on the LED whenever the button is pressed.
121 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200122#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200123#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200124#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
125#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500126#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200127#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500128
129#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200130static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800131#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500132/* A build error here means your board isn't set up to drive an LED. */
133#error "Unsupported board: led0 devicetree alias is not defined"
134#endif
135
Jared Wolff8e4d7912021-01-21 19:34:05 -0500136void led_init(void)
137{
Josh Gao837cf882020-11-13 18:51:27 -0800138
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200139 if (!device_is_ready(led0.port)) {
140 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
Jared Wolff8e4d7912021-01-21 19:34:05 -0500141 return;
142 }
143
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200144 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
145 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500146
147}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200148#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500149
Andrew Boie7238f512017-03-02 13:39:06 -0800150void os_heap_init(void);
151
152#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200153
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200154#ifdef CONFIG_SW_VECTOR_RELAY
155extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200156#endif
157
Andrew Boie7238f512017-03-02 13:39:06 -0800158struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600159 uint32_t msp;
160 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700161};
162
Andrew Boie7238f512017-03-02 13:39:06 -0800163static void do_boot(struct boot_rsp *rsp)
164{
David Brown0d0652a2017-04-11 17:33:30 -0600165 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400166 uintptr_t flash_base;
167 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800168
David Brown0d0652a2017-04-11 17:33:30 -0600169 /* The beginning of the image is the ARM vector table, containing
170 * the initial stack pointer address and the reset vector
171 * consecutively. Manually set the stack pointer and jump into the
172 * reset vector
173 */
Marti Bolivareb940802017-05-01 23:15:29 -0400174 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
175 assert(rc == 0);
176
177 vt = (struct arm_vector_table *)(flash_base +
178 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600179 rsp->br_hdr->ih_hdr_size);
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200180
David Brown0d0652a2017-04-11 17:33:30 -0600181 sys_clock_disable();
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100182
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200183#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200184 /* Disable the USB to prevent it from firing interrupts */
185 usb_disable();
186#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100187#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
188 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200189
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700190#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200191 /* Disable instruction cache and data cache before chain-load the application */
192 SCB_DisableDCache();
193 SCB_DisableICache();
194#endif
195
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100196#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200197 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100198#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200199
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000200#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
201 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
202 /* Reset limit registers to avoid inflicting stack overflow on image
203 * being booted.
204 */
205 __set_PSPLIM(0);
206 __set_MSPLIM(0);
207#endif
208
Sigvart Hovland9647c462021-08-20 16:33:55 +0200209#else
210 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200211#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
212
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200213#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200214#if defined(CONFIG_SW_VECTOR_RELAY)
215 _vector_table_pointer = vt;
216#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
217 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200218#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200219#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
220 SCB->VTOR = (uint32_t)vt;
221#endif /* CONFIG_SW_VECTOR_RELAY */
222#else /* CONFIG_BOOT_INTR_VEC_RELOC */
223#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
224 _vector_table_pointer = _vector_start;
225 SCB->VTOR = (uint32_t)__vector_relay_table;
226#endif
227#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200228
David Brown57837b92018-03-13 15:56:38 -0600229 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100230#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
231 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100232 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100233#endif
David Brown0d0652a2017-04-11 17:33:30 -0600234 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800235}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530236
237#elif defined(CONFIG_XTENSA)
238#define SRAM_BASE_ADDRESS 0xBE030000
239
240static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
241{
242 const struct flash_area *fap;
243 int area_id;
244 int rc;
245 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
246
247 BOOT_LOG_INF("Copying image to SRAM");
248
249 area_id = flash_area_id_from_image_slot(slot);
250 rc = flash_area_open(area_id, &fap);
251 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300252 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530253 goto done;
254 }
255
256 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
257 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300258 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530259 goto done;
260 }
261
262done:
263 flash_area_close(fap);
264}
265
266/* Entry point (.ResetVector) is at the very beginning of the image.
267 * Simply copy the image to a suitable location and jump there.
268 */
269static void do_boot(struct boot_rsp *rsp)
270{
271 void *start;
272
273 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
274 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
275
276 /* Copy from the flash to HP SRAM */
277 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
278
279 /* Jump to entry point */
280 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
281 ((void (*)(void))start)();
282}
283
Andrew Boie7238f512017-03-02 13:39:06 -0800284#else
285/* Default: Assume entry point is at the very beginning of the image. Simply
286 * lock interrupts and jump there. This is the right thing to do for X86 and
287 * possibly other platforms.
288 */
289static void do_boot(struct boot_rsp *rsp)
290{
David Brown0d0652a2017-04-11 17:33:30 -0600291 void *start;
Jim Tanee1b7b92022-04-07 11:26:28 +0800292
293#if defined(MCUBOOT_RAM_LOAD)
294 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
295#else
296 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400297 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800298
Marti Bolivareb940802017-05-01 23:15:29 -0400299 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
300 assert(rc == 0);
301
302 start = (void *)(flash_base + rsp->br_image_off +
303 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800304#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800305
David Brown0d0652a2017-04-11 17:33:30 -0600306 /* Lock interrupts and dive into the entry point */
307 irq_lock();
308 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800309}
310#endif
David Brown5153bd62017-01-06 11:16:53 -0700311
Marek Pietafb47d2e2022-03-11 14:24:07 +0100312#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000313 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100314/* The log internal thread for log processing can't transfer log well as has too
315 * low priority.
316 * Dedicated thread for log processing below uses highest application
317 * priority. This allows to transmit all logs without adding k_sleep/k_yield
318 * anywhere else int the code.
319 */
320
321/* most simple log processing theread */
322void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
323{
324 (void)dummy1;
325 (void)dummy2;
326 (void)dummy3;
327
328 log_init();
329
330 while (1) {
331 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100332 if (boot_log_stop) {
333 break;
334 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100335 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
336 }
337 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100338
339 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100340}
341
342void zephyr_boot_log_start(void)
343{
344 /* start logging thread */
345 k_thread_create(&boot_log_thread, boot_log_stack,
346 K_THREAD_STACK_SIZEOF(boot_log_stack),
347 boot_log_thread_func, NULL, NULL, NULL,
348 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
349 BOOT_LOG_PROCESSING_INTERVAL);
350
351 k_thread_name_set(&boot_log_thread, "logging");
352}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100353
354void zephyr_boot_log_stop(void)
355{
356 boot_log_stop = true;
357
358 /* wait until log procesing thread expired
359 * This can be reworked using a thread_join() API once a such will be
360 * available in zephyr.
361 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
362 */
363 (void)k_sem_take(&boot_log_sem, K_FOREVER);
364}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100365#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
366 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
367 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100368
Josh Gao837cf882020-11-13 18:51:27 -0800369#if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO)
370static bool detect_pin(const char* port, int pin, uint32_t expected, int delay)
371{
372 int rc;
373 int detect_value;
374 struct device const *detect_port;
375
376 detect_port = device_get_binding(port);
377 __ASSERT(detect_port, "Error: Bad port for boot detection.\n");
378
379 /* The default presence value is 0 which would normally be
380 * active-low, but historically the raw value was checked so we'll
381 * use the raw interface.
382 */
383 rc = gpio_pin_configure(detect_port, pin,
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100384 GPIO_INPUT | GPIO_PULL_UP);
Josh Gao837cf882020-11-13 18:51:27 -0800385 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
386
Josh Gao837cf882020-11-13 18:51:27 -0800387 rc = gpio_pin_get_raw(detect_port, pin);
388 detect_value = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100389
Josh Gao837cf882020-11-13 18:51:27 -0800390 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
391
392 if (detect_value == expected) {
393 if (delay > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200394#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800395 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200396#else
397 k_busy_wait(50000);
398#endif
Josh Gao837cf882020-11-13 18:51:27 -0800399
400 /* Get the uptime for debounce purposes. */
401 int64_t timestamp = k_uptime_get();
402
403 for(;;) {
Josh Gao837cf882020-11-13 18:51:27 -0800404 rc = gpio_pin_get_raw(detect_port, pin);
405 detect_value = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800406 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
407
408 /* Get delta from when this started */
409 uint32_t delta = k_uptime_get() - timestamp;
410
411 /* If not pressed OR if pressed > debounce period, stop. */
412 if (delta >= delay || detect_value != expected) {
413 break;
414 }
415
416 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200417#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800418 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200419#else
420 k_busy_wait(1000);
421#endif
Josh Gao837cf882020-11-13 18:51:27 -0800422 }
423 }
424 }
425
426 return detect_value == expected;
427}
428#endif
429
David Brown5153bd62017-01-06 11:16:53 -0700430void main(void)
431{
David Brown0d0652a2017-04-11 17:33:30 -0600432 struct boot_rsp rsp;
433 int rc;
Tamas Banee6615d2020-09-30 07:58:48 +0100434 fih_int fih_rc = FIH_FAILURE;
David Brown5153bd62017-01-06 11:16:53 -0700435
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200436 MCUBOOT_WATCHDOG_FEED();
437
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000438#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600439 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000440#else
441 BOOT_LOG_INF("Starting Direct-XIP bootloader");
442#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200443
Jared Wolff8e4d7912021-01-21 19:34:05 -0500444#ifdef CONFIG_MCUBOOT_INDICATION_LED
445 /* LED init */
446 led_init();
447#endif
448
David Brown0d0652a2017-04-11 17:33:30 -0600449 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700450
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100451 ZEPHYR_BOOT_LOG_START();
452
Tamas Banee6615d2020-09-30 07:58:48 +0100453 (void)rc;
454
Gerard Marull-Paretasaa041a22022-03-25 12:22:29 +0100455#if (!defined(CONFIG_XTENSA) && DT_HAS_CHOSEN(zephyr_flash_controller))
456 if (!flash_device_get_binding(DT_LABEL(DT_CHOSEN(zephyr_flash_controller)))) {
Kumar Gala32b61f32020-04-08 12:02:03 -0500457 BOOT_LOG_ERR("Flash device %s not found",
Gerard Marull-Paretasaa041a22022-03-25 12:22:29 +0100458 DT_LABEL(DT_CHOSEN(zephyr_flash_controller)));
David Brown0d0652a2017-04-11 17:33:30 -0600459 while (1)
460 ;
461 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500462#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
463 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
464 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530465 while (1)
466 ;
467 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530468#endif
David Brown5153bd62017-01-06 11:16:53 -0700469
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200470#ifdef CONFIG_MCUBOOT_SERIAL
Josh Gao837cf882020-11-13 18:51:27 -0800471 if (detect_pin(CONFIG_BOOT_SERIAL_DETECT_PORT,
472 CONFIG_BOOT_SERIAL_DETECT_PIN,
473 CONFIG_BOOT_SERIAL_DETECT_PIN_VAL,
474 CONFIG_BOOT_SERIAL_DETECT_DELAY) &&
475 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500476#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200477 gpio_pin_set_dt(&led0, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200478#endif
479
Josh Gao837cf882020-11-13 18:51:27 -0800480 BOOT_LOG_INF("Enter the serial recovery mode");
481 rc = boot_console_init();
482 __ASSERT(rc == 0, "Error initializing boot console.\n");
483 boot_serial_start(&boot_funcs);
484 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
485 }
486#endif
487
488#if defined(CONFIG_BOOT_USB_DFU_GPIO)
489 if (detect_pin(CONFIG_BOOT_USB_DFU_DETECT_PORT,
490 CONFIG_BOOT_USB_DFU_DETECT_PIN,
491 CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL,
492 CONFIG_BOOT_USB_DFU_DETECT_DELAY)) {
493#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200494 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800495#endif
496 rc = usb_enable(NULL);
497 if (rc) {
498 BOOT_LOG_ERR("Cannot enable USB");
499 } else {
500 BOOT_LOG_INF("Waiting for USB DFU");
501 wait_for_usb_dfu(K_FOREVER);
502 BOOT_LOG_INF("USB DFU wait time elapsed");
503 }
504 }
505#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100506 rc = usb_enable(NULL);
507 if (rc) {
508 BOOT_LOG_ERR("Cannot enable USB");
509 } else {
510 BOOT_LOG_INF("Waiting for USB DFU");
Josh Gao837cf882020-11-13 18:51:27 -0800511 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100512 BOOT_LOG_INF("USB DFU wait time elapsed");
513 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530514#endif
515
Wouter Cappellee3822f82022-01-19 15:39:43 +0100516#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
517 /* Initialize the boot console, so we can already fill up our buffers while
518 * waiting for the boot image check to finish. This image check, can take
519 * some time, so it's better to reuse thistime to already receive the
520 * initial mcumgr command(s) into our buffers
521 */
522 rc = boot_console_init();
523 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
524 uint32_t start = k_uptime_get_32();
525#endif
526
Tamas Banee6615d2020-09-30 07:58:48 +0100527 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100528
529#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
530 timeout_in_ms -= (k_uptime_get_32() - start);
531 if( timeout_in_ms <= 0 ) {
532 /* at least one check if time was expired */
533 timeout_in_ms = 1;
534 }
535 boot_serial_check_start(&boot_funcs,timeout_in_ms);
536#endif
537
Tamas Banee6615d2020-09-30 07:58:48 +0100538 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600539 BOOT_LOG_ERR("Unable to find bootable image");
Tamas Banee6615d2020-09-30 07:58:48 +0100540 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600541 }
David Brown5153bd62017-01-06 11:16:53 -0700542
Marti Bolivar88f48d92017-05-01 22:30:02 -0400543 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
544 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200545
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000546#if defined(MCUBOOT_DIRECT_XIP)
547 BOOT_LOG_INF("Jumping to the image slot");
548#else
David Brown0d0652a2017-04-11 17:33:30 -0600549 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000550#endif
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100551 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600552 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700553
David Brown0d0652a2017-04-11 17:33:30 -0600554 BOOT_LOG_ERR("Never should get here");
555 while (1)
556 ;
David Brown5153bd62017-01-06 11:16:53 -0700557}