blob: 3f17358024272e77f15e383f4eea39b0cfc34cf4 [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
David Brown5153bd62017-01-06 11:16:53 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Marti Bolivareb940802017-05-01 23:15:29 -040018#include <assert.h>
David Brown5153bd62017-01-06 11:16:53 -070019#include <zephyr.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060020#include <drivers/gpio.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010021#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060022#include <drivers/flash.h>
Andrzej Puzdrowskif99a4c72019-06-28 15:16:35 +020023#include <drivers/timer/system_timer.h>
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020024#include <usb/usb_device.h>
Evan Gates4632d8d2018-06-28 13:27:40 -070025#include <soc.h>
Rafał Kuźnia505c6e62020-07-17 13:18:15 +020026#include <linker/linker-defs.h>
David Brown5153bd62017-01-06 11:16:53 -070027
Marti Bolivar51181cf2017-03-20 11:03:41 -040028#include "target.h"
29
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050030#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020031#include "bootutil/image.h"
32#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010033#include "bootutil/fault_injection_hardening.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020034#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020035
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020036#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030037#include "boot_serial/boot_serial.h"
38#include "serial_adapter/serial_adapter.h"
39
40const struct boot_uart_funcs boot_funcs = {
41 .read = console_read,
42 .write = console_write
43};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020044#endif
45
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053046#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
47#include <usb/class/usb_dfu.h>
48#endif
49
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010050#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
51#include <arm_cleanup.h>
52#endif
53
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010054/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
55 * replaced by CONFIG_LOG_MODE_MINIMAL.
56 */
57#define ZEPHYR_LOG_MODE_MINIMAL (defined(CONFIG_LOG_MODE_MINIMAL) ||\
58 defined(CONFIG_LOG_MINIMAL))
59
Henrik Brix Andersene5121812021-01-12 09:52:51 +010060#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) && \
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010061 !ZEPHYR_LOG_MODE_MINIMAL
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010062#ifdef CONFIG_LOG_PROCESS_THREAD
63#warning "The log internal thread for log processing can't transfer the log"\
64 "well for MCUBoot."
65#else
66#include <logging/log_ctrl.h>
67
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010068#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010069
70/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010071K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010072struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010073volatile bool boot_log_stop = false;
74K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010075
76/* log processing need to be initalized by the application */
77#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010078#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010079#endif /* CONFIG_LOG_PROCESS_THREAD */
80#else
81/* synchronous log mode doesn't need to be initalized by the application */
82#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010083#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010084#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
85
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010086#ifdef CONFIG_SOC_FAMILY_NRF
87#include <hal/nrf_power.h>
88
89static inline bool boot_skip_serial_recovery()
90{
Fabio Utzig63b4eac2019-12-17 09:59:47 -030091#if NRF_POWER_HAS_RESETREAS
Kumar Gala0813efe2020-05-27 12:25:41 -050092 uint32_t rr = nrf_power_resetreas_get(NRF_POWER);
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010093
94 return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
95#else
96 return false;
97#endif
98}
99#else
100static inline bool boot_skip_serial_recovery()
101{
102 return false;
103}
104#endif
105
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100106MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
107
Jared Wolff8e4d7912021-01-21 19:34:05 -0500108#ifdef CONFIG_MCUBOOT_INDICATION_LED
109/*
110 * Devicetree helper macro which gets the 'flags' cell from a 'gpios'
111 * property, or returns 0 if the property has no 'flags' cell.
112 */
113#define FLAGS_OR_ZERO(node) \
114 COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \
115 (DT_GPIO_FLAGS(node, gpios)), \
116 (0))
117
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 */
122
123#define LED0_NODE DT_ALIAS(bootloader_led0)
124
125#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
126#define LED0_GPIO_LABEL DT_GPIO_LABEL(LED0_NODE, gpios)
127#define LED0_GPIO_PIN DT_GPIO_PIN(LED0_NODE, gpios)
128#define LED0_GPIO_FLAGS (GPIO_OUTPUT | FLAGS_OR_ZERO(LED0_NODE))
129#else
130/* A build error here means your board isn't set up to drive an LED. */
131#error "Unsupported board: led0 devicetree alias is not defined"
132#endif
133
134const static struct device *led;
135
136void led_init(void)
137{
138
139 led = device_get_binding(LED0_GPIO_LABEL);
140 if (led == NULL)
141 {
142 BOOT_LOG_ERR("Didn't find LED device %s\n", LED0_GPIO_LABEL);
143 return;
144 }
145
146 gpio_pin_configure(led, LED0_GPIO_PIN, LED0_GPIO_FLAGS);
147
148}
149#endif
150
Andrew Boie7238f512017-03-02 13:39:06 -0800151void os_heap_init(void);
152
153#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200154
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200155#ifdef CONFIG_SW_VECTOR_RELAY
156extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200157#endif
158
Andrew Boie7238f512017-03-02 13:39:06 -0800159struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600160 uint32_t msp;
161 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700162};
163
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100164extern void sys_clock_disable(void);
165
Andrew Boie7238f512017-03-02 13:39:06 -0800166static void do_boot(struct boot_rsp *rsp)
167{
David Brown0d0652a2017-04-11 17:33:30 -0600168 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400169 uintptr_t flash_base;
170 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800171
David Brown0d0652a2017-04-11 17:33:30 -0600172 /* The beginning of the image is the ARM vector table, containing
173 * the initial stack pointer address and the reset vector
174 * consecutively. Manually set the stack pointer and jump into the
175 * reset vector
176 */
Marti Bolivareb940802017-05-01 23:15:29 -0400177 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
178 assert(rc == 0);
179
180 vt = (struct arm_vector_table *)(flash_base +
181 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600182 rsp->br_hdr->ih_hdr_size);
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200183
David Brown0d0652a2017-04-11 17:33:30 -0600184 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200185#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -0600186 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200187#endif
Jun Li01bef712019-05-08 21:55:54 -0700188#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200189 /* Disable the USB to prevent it from firing interrupts */
190 usb_disable();
191#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100192#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
193 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200194
195#ifdef CONFIG_CPU_CORTEX_M7
196 /* Disable instruction cache and data cache before chain-load the application */
197 SCB_DisableDCache();
198 SCB_DisableICache();
199#endif
200
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100201#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200202 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100203#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200204
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000205#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
206 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
207 /* Reset limit registers to avoid inflicting stack overflow on image
208 * being booted.
209 */
210 __set_PSPLIM(0);
211 __set_MSPLIM(0);
212#endif
213
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200214#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
215
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200216#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200217#if defined(CONFIG_SW_VECTOR_RELAY)
218 _vector_table_pointer = vt;
219#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
220 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200221#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200222#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
223 SCB->VTOR = (uint32_t)vt;
224#endif /* CONFIG_SW_VECTOR_RELAY */
225#else /* CONFIG_BOOT_INTR_VEC_RELOC */
226#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
227 _vector_table_pointer = _vector_start;
228 SCB->VTOR = (uint32_t)__vector_relay_table;
229#endif
230#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200231
David Brown57837b92018-03-13 15:56:38 -0600232 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100233#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
234 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100235 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100236#endif
David Brown0d0652a2017-04-11 17:33:30 -0600237 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800238}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530239
240#elif defined(CONFIG_XTENSA)
241#define SRAM_BASE_ADDRESS 0xBE030000
242
243static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
244{
245 const struct flash_area *fap;
246 int area_id;
247 int rc;
248 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
249
250 BOOT_LOG_INF("Copying image to SRAM");
251
252 area_id = flash_area_id_from_image_slot(slot);
253 rc = flash_area_open(area_id, &fap);
254 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300255 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530256 goto done;
257 }
258
259 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
260 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300261 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530262 goto done;
263 }
264
265done:
266 flash_area_close(fap);
267}
268
269/* Entry point (.ResetVector) is at the very beginning of the image.
270 * Simply copy the image to a suitable location and jump there.
271 */
272static void do_boot(struct boot_rsp *rsp)
273{
274 void *start;
275
276 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
277 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
278
279 /* Copy from the flash to HP SRAM */
280 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
281
282 /* Jump to entry point */
283 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
284 ((void (*)(void))start)();
285}
286
Andrew Boie7238f512017-03-02 13:39:06 -0800287#else
288/* Default: Assume entry point is at the very beginning of the image. Simply
289 * lock interrupts and jump there. This is the right thing to do for X86 and
290 * possibly other platforms.
291 */
292static void do_boot(struct boot_rsp *rsp)
293{
Marti Bolivareb940802017-05-01 23:15:29 -0400294 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600295 void *start;
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);
Andrew Boie7238f512017-03-02 13:39:06 -0800303
David Brown0d0652a2017-04-11 17:33:30 -0600304 /* Lock interrupts and dive into the entry point */
305 irq_lock();
306 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800307}
308#endif
David Brown5153bd62017-01-06 11:16:53 -0700309
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100310#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +0100311 !defined(CONFIG_LOG_PROCESS_THREAD) && !ZEPHYR_LOG_MODE_MINIMAL
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100312/* The log internal thread for log processing can't transfer log well as has too
313 * low priority.
314 * Dedicated thread for log processing below uses highest application
315 * priority. This allows to transmit all logs without adding k_sleep/k_yield
316 * anywhere else int the code.
317 */
318
319/* most simple log processing theread */
320void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
321{
322 (void)dummy1;
323 (void)dummy2;
324 (void)dummy3;
325
326 log_init();
327
328 while (1) {
329 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100330 if (boot_log_stop) {
331 break;
332 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100333 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
334 }
335 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100336
337 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100338}
339
340void zephyr_boot_log_start(void)
341{
342 /* start logging thread */
343 k_thread_create(&boot_log_thread, boot_log_stack,
344 K_THREAD_STACK_SIZEOF(boot_log_stack),
345 boot_log_thread_func, NULL, NULL, NULL,
346 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
347 BOOT_LOG_PROCESSING_INTERVAL);
348
349 k_thread_name_set(&boot_log_thread, "logging");
350}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100351
352void zephyr_boot_log_stop(void)
353{
354 boot_log_stop = true;
355
356 /* wait until log procesing thread expired
357 * This can be reworked using a thread_join() API once a such will be
358 * available in zephyr.
359 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
360 */
361 (void)k_sem_take(&boot_log_sem, K_FOREVER);
362}
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100363#endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
364 !defined(CONFIG_LOG_PROCESS_THREAD) */
365
David Brown5153bd62017-01-06 11:16:53 -0700366void main(void)
367{
David Brown0d0652a2017-04-11 17:33:30 -0600368 struct boot_rsp rsp;
369 int rc;
Tamas Banee6615d2020-09-30 07:58:48 +0100370 fih_int fih_rc = FIH_FAILURE;
David Brown5153bd62017-01-06 11:16:53 -0700371
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200372 MCUBOOT_WATCHDOG_FEED();
373
David Brown0d0652a2017-04-11 17:33:30 -0600374 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200375
Jared Wolff8e4d7912021-01-21 19:34:05 -0500376#ifdef CONFIG_MCUBOOT_INDICATION_LED
377 /* LED init */
378 led_init();
379#endif
380
David Brown0d0652a2017-04-11 17:33:30 -0600381 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700382
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100383 ZEPHYR_BOOT_LOG_START();
384
Tamas Banee6615d2020-09-30 07:58:48 +0100385 (void)rc;
386
Kumar Gala32b61f32020-04-08 12:02:03 -0500387#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
388 if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) {
389 BOOT_LOG_ERR("Flash device %s not found",
390 DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
David Brown0d0652a2017-04-11 17:33:30 -0600391 while (1)
392 ;
393 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500394#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
395 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
396 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530397 while (1)
398 ;
399 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530400#endif
David Brown5153bd62017-01-06 11:16:53 -0700401
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200402#ifdef CONFIG_MCUBOOT_SERIAL
403
Dominik Ermel1422b4b2020-09-11 11:31:38 +0000404 struct device const *detect_port;
Kumar Gala0813efe2020-05-27 12:25:41 -0500405 uint32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200406
407 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
408 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
409
Peter Bigot36e90292020-01-30 05:36:47 -0600410 /* The default presence value is 0 which would normally be
411 * active-low, but historically the raw value was checked so we'll
412 * use the raw interface.
413 */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200414 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Peter Bigot36e90292020-01-30 05:36:47 -0600415#ifdef GPIO_INPUT
416 GPIO_INPUT | GPIO_PULL_UP
417#else
418 GPIO_DIR_IN | GPIO_PUD_PULL_UP
419#endif
Andrzej Puzdrowski5d96bd22020-02-20 10:35:33 +0100420 );
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200421 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200422
Peter Bigot36e90292020-01-30 05:36:47 -0600423#ifdef GPIO_INPUT
424 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
425 detect_value = rc;
426#else
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100427 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200428 &detect_value);
Peter Bigot36e90292020-01-30 05:36:47 -0600429#endif
430 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100431 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
432 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500433
434#if CONFIG_BOOT_SERIAL_DETECT_DELAY > 0
435 k_sleep(K_MSEC(50));
436
437 /* Get the uptime for debounce purposes. */
438 int64_t timestamp = k_uptime_get();
439
440 for(;;) {
441
442#ifdef GPIO_INPUT
443 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
444 detect_value = rc;
445#else
446 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
447 &detect_value);
448#endif
449 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
450
451 /* Get delta from when this started */
452 uint32_t delta = k_uptime_get() - timestamp;
453
454 /* If not pressed OR if pressed > debounce period stop loop*/
455 if( delta >= CONFIG_BOOT_SERIAL_DETECT_DELAY ||
456 detect_value != CONFIG_BOOT_SERIAL_DETECT_PIN_VAL ) {
457 break;
458 }
459
460
461 /* Delay 1 ms */
462 k_sleep(K_MSEC(1));
463 }
464#endif
465
466 /* Then run DFU */
467 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
468#ifdef CONFIG_MCUBOOT_INDICATION_LED
469 gpio_pin_set(led, LED0_GPIO_PIN, 1);
470#endif
471 BOOT_LOG_INF("Enter the serial recovery mode");
472 rc = boot_console_init();
473 __ASSERT(rc == 0, "Error initializing boot console.\n");
474 boot_serial_start(&boot_funcs);
475 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
476
477 }
478}
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200479#endif
480
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530481#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100482 rc = usb_enable(NULL);
483 if (rc) {
484 BOOT_LOG_ERR("Cannot enable USB");
485 } else {
486 BOOT_LOG_INF("Waiting for USB DFU");
487 wait_for_usb_dfu();
488 BOOT_LOG_INF("USB DFU wait time elapsed");
489 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530490#endif
491
Tamas Banee6615d2020-09-30 07:58:48 +0100492 FIH_CALL(boot_go, fih_rc, &rsp);
493 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600494 BOOT_LOG_ERR("Unable to find bootable image");
Tamas Banee6615d2020-09-30 07:58:48 +0100495 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600496 }
David Brown5153bd62017-01-06 11:16:53 -0700497
Marti Bolivar88f48d92017-05-01 22:30:02 -0400498 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
499 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200500
David Brown0d0652a2017-04-11 17:33:30 -0600501 BOOT_LOG_INF("Jumping to the first image slot");
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100502 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600503 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700504
David Brown0d0652a2017-04-11 17:33:30 -0600505 BOOT_LOG_ERR("Never should get here");
506 while (1)
507 ;
David Brown5153bd62017-01-06 11:16:53 -0700508}