blob: 148f06e1483564e386a799228c4eeaaea03da2e2 [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
Yonatan Schachterb22eb6a2022-07-29 18:16:09 +030030#if defined(CONFIG_CPU_AARCH32_CORTEX_A) || defined(CONFIG_CPU_AARCH32_CORTEX_R)
31#include <zephyr/arch/arm/aarch32/cortex_a_r/cmsis.h>
32#elif defined(CONFIG_CPU_CORTEX_M)
33#include <zephyr/arch/arm/aarch32/cortex_m/cmsis.h>
34#endif
35
Marti Bolivar51181cf2017-03-20 11:03:41 -040036#include "target.h"
37
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050038#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020039#include "bootutil/image.h"
40#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010041#include "bootutil/fault_injection_hardening.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000042#include "bootutil/mcuboot_status.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020043#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020044
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020045#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030046#include "boot_serial/boot_serial.h"
47#include "serial_adapter/serial_adapter.h"
48
49const struct boot_uart_funcs boot_funcs = {
50 .read = console_read,
51 .write = console_write
52};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020053#endif
54
Josh Gao837cf882020-11-13 18:51:27 -080055#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020056#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053057#endif
58
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010059#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
60#include <arm_cleanup.h>
61#endif
62
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010063/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
64 * replaced by CONFIG_LOG_MODE_MINIMAL.
65 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000066#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
67#define ZEPHYR_LOG_MODE_MINIMAL 1
68#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010069
Marek Pietafb47d2e2022-03-11 14:24:07 +010070/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
71 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
72 */
73#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
74#define ZEPHYR_LOG_MODE_IMMEDIATE 1
75#endif
76
77#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000078 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010079#ifdef CONFIG_LOG_PROCESS_THREAD
80#warning "The log internal thread for log processing can't transfer the log"\
81 "well for MCUBoot."
82#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020083#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010084
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010085#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010086
87/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010088K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010089struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010090volatile bool boot_log_stop = false;
91K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010092
93/* log processing need to be initalized by the application */
94#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010095#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010096#endif /* CONFIG_LOG_PROCESS_THREAD */
97#else
98/* synchronous log mode doesn't need to be initalized by the application */
99#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100100#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +0100101#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
102 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
103 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100104
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100105#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +0100106#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100107
108static inline bool boot_skip_serial_recovery()
109{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100110 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100111
Radoslaw Koppel72006692021-12-06 16:15:21 +0100112 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100113}
114#else
115static inline bool boot_skip_serial_recovery()
116{
117 return false;
118}
119#endif
120
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200121BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100122
Jared Wolff8e4d7912021-01-21 19:34:05 -0500123#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500124
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 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200129#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200130#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200131#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
132#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500133#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200134#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500135
136#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200137static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800138#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500139/* A build error here means your board isn't set up to drive an LED. */
140#error "Unsupported board: led0 devicetree alias is not defined"
141#endif
142
Jared Wolff8e4d7912021-01-21 19:34:05 -0500143void led_init(void)
144{
Martin Jäger477ed812022-07-21 11:45:45 +0200145 if (!device_is_ready(led0.port)) {
146 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
147 return;
148 }
Josh Gao837cf882020-11-13 18:51:27 -0800149
Martin Jäger477ed812022-07-21 11:45:45 +0200150 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
151 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500152}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200153#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500154
Andrew Boie7238f512017-03-02 13:39:06 -0800155void os_heap_init(void);
156
157#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200158
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200159#ifdef CONFIG_SW_VECTOR_RELAY
160extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200161#endif
162
Andrew Boie7238f512017-03-02 13:39:06 -0800163struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600164 uint32_t msp;
165 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700166};
167
Andrew Boie7238f512017-03-02 13:39:06 -0800168static void do_boot(struct boot_rsp *rsp)
169{
David Brown0d0652a2017-04-11 17:33:30 -0600170 struct arm_vector_table *vt;
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 */
Daniel DeGrassee4574442022-09-01 15:47:01 -0500177#ifdef CONFIG_BOOT_RAM_LOAD
178 /* Get ram address for image */
179 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
180#else
181 uintptr_t flash_base;
182 int rc;
183
184 /* Jump to flash image */
Marti Bolivareb940802017-05-01 23:15:29 -0400185 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
186 assert(rc == 0);
187
188 vt = (struct arm_vector_table *)(flash_base +
189 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600190 rsp->br_hdr->ih_hdr_size);
Daniel DeGrassee4574442022-09-01 15:47:01 -0500191#endif
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200192
David Brown0d0652a2017-04-11 17:33:30 -0600193 sys_clock_disable();
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100194
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200195#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200196 /* Disable the USB to prevent it from firing interrupts */
197 usb_disable();
198#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100199#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
200 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200201
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700202#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200203 /* Disable instruction cache and data cache before chain-load the application */
204 SCB_DisableDCache();
205 SCB_DisableICache();
206#endif
207
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100208#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200209 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100210#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200211
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000212#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
213 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
214 /* Reset limit registers to avoid inflicting stack overflow on image
215 * being booted.
216 */
217 __set_PSPLIM(0);
218 __set_MSPLIM(0);
219#endif
220
Sigvart Hovland9647c462021-08-20 16:33:55 +0200221#else
222 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200223#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
224
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200225#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200226#if defined(CONFIG_SW_VECTOR_RELAY)
227 _vector_table_pointer = vt;
228#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
229 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200230#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200231#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
232 SCB->VTOR = (uint32_t)vt;
233#endif /* CONFIG_SW_VECTOR_RELAY */
234#else /* CONFIG_BOOT_INTR_VEC_RELOC */
235#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
236 _vector_table_pointer = _vector_start;
237 SCB->VTOR = (uint32_t)__vector_relay_table;
238#endif
239#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200240
David Brown57837b92018-03-13 15:56:38 -0600241 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100242#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
243 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100244 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100245#endif
David Brown0d0652a2017-04-11 17:33:30 -0600246 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800247}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530248
249#elif defined(CONFIG_XTENSA)
250#define SRAM_BASE_ADDRESS 0xBE030000
251
252static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
253{
254 const struct flash_area *fap;
255 int area_id;
256 int rc;
257 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
258
259 BOOT_LOG_INF("Copying image to SRAM");
260
261 area_id = flash_area_id_from_image_slot(slot);
262 rc = flash_area_open(area_id, &fap);
263 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300264 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530265 goto done;
266 }
267
268 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
269 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300270 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530271 goto done;
272 }
273
274done:
275 flash_area_close(fap);
276}
277
278/* Entry point (.ResetVector) is at the very beginning of the image.
279 * Simply copy the image to a suitable location and jump there.
280 */
281static void do_boot(struct boot_rsp *rsp)
282{
283 void *start;
284
285 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
286 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
287
288 /* Copy from the flash to HP SRAM */
289 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
290
291 /* Jump to entry point */
292 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
293 ((void (*)(void))start)();
294}
295
Andrew Boie7238f512017-03-02 13:39:06 -0800296#else
297/* Default: Assume entry point is at the very beginning of the image. Simply
298 * lock interrupts and jump there. This is the right thing to do for X86 and
299 * possibly other platforms.
300 */
301static void do_boot(struct boot_rsp *rsp)
302{
David Brown0d0652a2017-04-11 17:33:30 -0600303 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200304
Jim Tanee1b7b92022-04-07 11:26:28 +0800305#if defined(MCUBOOT_RAM_LOAD)
306 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
307#else
308 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400309 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800310
Marti Bolivareb940802017-05-01 23:15:29 -0400311 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
312 assert(rc == 0);
313
314 start = (void *)(flash_base + rsp->br_image_off +
315 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800316#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800317
David Brown0d0652a2017-04-11 17:33:30 -0600318 /* Lock interrupts and dive into the entry point */
319 irq_lock();
320 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800321}
322#endif
David Brown5153bd62017-01-06 11:16:53 -0700323
Marek Pietafb47d2e2022-03-11 14:24:07 +0100324#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000325 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100326/* The log internal thread for log processing can't transfer log well as has too
327 * low priority.
328 * Dedicated thread for log processing below uses highest application
329 * priority. This allows to transmit all logs without adding k_sleep/k_yield
330 * anywhere else int the code.
331 */
332
333/* most simple log processing theread */
334void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
335{
336 (void)dummy1;
337 (void)dummy2;
338 (void)dummy3;
339
Martin Jäger477ed812022-07-21 11:45:45 +0200340 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100341
Martin Jäger477ed812022-07-21 11:45:45 +0200342 while (1) {
Martin Jäger31751822022-07-21 11:49:32 +0200343#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
344 /* support Zephyr legacy logging implementation before commit c5f2cde */
Martin Jäger477ed812022-07-21 11:45:45 +0200345 if (log_process(false) == false) {
Martin Jäger31751822022-07-21 11:49:32 +0200346#else
347 if (log_process() == false) {
348#endif
Martin Jäger477ed812022-07-21 11:45:45 +0200349 if (boot_log_stop) {
350 break;
351 }
352 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
353 }
354 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100355
Martin Jäger477ed812022-07-21 11:45:45 +0200356 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100357}
358
359void zephyr_boot_log_start(void)
360{
Martin Jäger477ed812022-07-21 11:45:45 +0200361 /* start logging thread */
362 k_thread_create(&boot_log_thread, boot_log_stack,
363 K_THREAD_STACK_SIZEOF(boot_log_stack),
364 boot_log_thread_func, NULL, NULL, NULL,
365 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
366 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100367
Martin Jäger477ed812022-07-21 11:45:45 +0200368 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100369}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100370
371void zephyr_boot_log_stop(void)
372{
373 boot_log_stop = true;
374
375 /* wait until log procesing thread expired
376 * This can be reworked using a thread_join() API once a such will be
377 * available in zephyr.
378 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
379 */
380 (void)k_sem_take(&boot_log_sem, K_FOREVER);
381}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100382#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
383 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
384 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100385
Josh Gao837cf882020-11-13 18:51:27 -0800386#if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200387
388#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200389#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
390#else
391#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
392#endif
393
394
395#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
396
397#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
398
399static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
400
401#else /* fallback to legacy configuration */
402
403#if defined(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200404
Marek Pieta2fa11902022-09-13 11:31:30 +0200405/* The value of -1 is used by default. It must be properly specified for a board before used. */
406BUILD_ASSERT(CONFIG_BOOT_SERIAL_DETECT_PIN != -1);
407
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200408#define BUTTON_0_GPIO_LABEL CONFIG_BOOT_SERIAL_DETECT_PORT
409#define BUTTON_0_GPIO_PIN CONFIG_BOOT_SERIAL_DETECT_PIN
410#define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) ?\
411 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\
412 (GPIO_ACTIVE_LOW | GPIO_PULL_UP))
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200413
414#elif defined(CONFIG_BOOT_USB_DFU_GPIO)
415
Marek Pieta2fa11902022-09-13 11:31:30 +0200416/* The value of -1 is used by default. It must be properly specified for a board before used. */
417BUILD_ASSERT(CONFIG_BOOT_USB_DFU_DETECT_PIN != -1);
418
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200419#define BUTTON_0_GPIO_LABEL CONFIG_BOOT_USB_DFU_DETECT_PORT
420#define BUTTON_0_GPIO_PIN CONFIG_BOOT_USB_DFU_DETECT_PIN
421#define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL) ?\
422 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\
423 (GPIO_ACTIVE_LOW | GPIO_PULL_UP))
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200424
425#endif
426
427#define BUTTON_0_LEGACY 1
428
429static struct gpio_dt_spec button0 = {
430 .port = NULL,
431 .pin = BUTTON_0_GPIO_PIN,
432 .dt_flags = BUTTON_0_GPIO_FLAGS
433};
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200434
435#endif
436
437static bool detect_pin(void)
Josh Gao837cf882020-11-13 18:51:27 -0800438{
439 int rc;
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200440 int pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800441
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200442#ifdef BUTTON_0_LEGACY
443 button0.port = device_get_binding(BUTTON_0_GPIO_LABEL);
444 if (button0.port == NULL) {
445 __ASSERT(false, "Error: Bad port for boot detection.\n");
446 return false;
447 }
448#else
449 if (!device_is_ready(button0.port)) {
450 __ASSERT(false, "GPIO device is not ready.\n");
451 return false;
452 }
453#endif
Josh Gao837cf882020-11-13 18:51:27 -0800454
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200455 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
Josh Gao837cf882020-11-13 18:51:27 -0800456 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
457
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200458 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200459 pin_active = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100460
Josh Gao837cf882020-11-13 18:51:27 -0800461 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
462
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200463 if (pin_active) {
464 if (BUTTON_0_DETECT_DELAY > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200465#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800466 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200467#else
468 k_busy_wait(50000);
469#endif
Josh Gao837cf882020-11-13 18:51:27 -0800470
471 /* Get the uptime for debounce purposes. */
472 int64_t timestamp = k_uptime_get();
473
474 for(;;) {
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200475 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200476 pin_active = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800477 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
478
479 /* Get delta from when this started */
480 uint32_t delta = k_uptime_get() - timestamp;
481
482 /* If not pressed OR if pressed > debounce period, stop. */
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200483 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
Josh Gao837cf882020-11-13 18:51:27 -0800484 break;
485 }
486
487 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200488#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800489 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200490#else
491 k_busy_wait(1000);
492#endif
Josh Gao837cf882020-11-13 18:51:27 -0800493 }
494 }
495 }
496
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200497 return (bool)pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800498}
499#endif
500
David Brown5153bd62017-01-06 11:16:53 -0700501void main(void)
502{
David Brown0d0652a2017-04-11 17:33:30 -0600503 struct boot_rsp rsp;
504 int rc;
Tamas Banee6615d2020-09-30 07:58:48 +0100505 fih_int fih_rc = FIH_FAILURE;
David Brown5153bd62017-01-06 11:16:53 -0700506
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200507 MCUBOOT_WATCHDOG_FEED();
508
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000509#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600510 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000511#else
512 BOOT_LOG_INF("Starting Direct-XIP bootloader");
513#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200514
Jared Wolff8e4d7912021-01-21 19:34:05 -0500515#ifdef CONFIG_MCUBOOT_INDICATION_LED
516 /* LED init */
517 led_init();
518#endif
519
David Brown0d0652a2017-04-11 17:33:30 -0600520 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700521
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100522 ZEPHYR_BOOT_LOG_START();
523
Tamas Banee6615d2020-09-30 07:58:48 +0100524 (void)rc;
525
Jamie McCrae56cb6102022-03-23 11:57:03 +0000526 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
527
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200528#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200529 if (detect_pin() &&
Josh Gao837cf882020-11-13 18:51:27 -0800530 !boot_skip_serial_recovery()) {
Jared Wolff8e4d7912021-01-21 19:34:05 -0500531#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200532 gpio_pin_set_dt(&led0, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200533#endif
534
Jamie McCrae56cb6102022-03-23 11:57:03 +0000535 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
536
Josh Gao837cf882020-11-13 18:51:27 -0800537 BOOT_LOG_INF("Enter the serial recovery mode");
538 rc = boot_console_init();
539 __ASSERT(rc == 0, "Error initializing boot console.\n");
540 boot_serial_start(&boot_funcs);
541 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
542 }
543#endif
544
545#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200546 if (detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800547#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200548 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800549#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000550
551 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
552
Josh Gao837cf882020-11-13 18:51:27 -0800553 rc = usb_enable(NULL);
554 if (rc) {
555 BOOT_LOG_ERR("Cannot enable USB");
556 } else {
557 BOOT_LOG_INF("Waiting for USB DFU");
558 wait_for_usb_dfu(K_FOREVER);
559 BOOT_LOG_INF("USB DFU wait time elapsed");
560 }
561 }
562#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100563 rc = usb_enable(NULL);
564 if (rc) {
565 BOOT_LOG_ERR("Cannot enable USB");
566 } else {
567 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000568
569 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
570
Josh Gao837cf882020-11-13 18:51:27 -0800571 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100572 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000573
574 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100575 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530576#endif
577
Wouter Cappellee3822f82022-01-19 15:39:43 +0100578#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
579 /* Initialize the boot console, so we can already fill up our buffers while
580 * waiting for the boot image check to finish. This image check, can take
581 * some time, so it's better to reuse thistime to already receive the
582 * initial mcumgr command(s) into our buffers
583 */
584 rc = boot_console_init();
585 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
586 uint32_t start = k_uptime_get_32();
587#endif
588
Tamas Banee6615d2020-09-30 07:58:48 +0100589 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100590
591#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
592 timeout_in_ms -= (k_uptime_get_32() - start);
593 if( timeout_in_ms <= 0 ) {
594 /* at least one check if time was expired */
595 timeout_in_ms = 1;
596 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100597 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100598#endif
599
Tamas Banee6615d2020-09-30 07:58:48 +0100600 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600601 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000602
603 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
604
Tamas Banee6615d2020-09-30 07:58:48 +0100605 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600606 }
David Brown5153bd62017-01-06 11:16:53 -0700607
Marti Bolivar88f48d92017-05-01 22:30:02 -0400608 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
609 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200610
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000611#if defined(MCUBOOT_DIRECT_XIP)
612 BOOT_LOG_INF("Jumping to the image slot");
613#else
David Brown0d0652a2017-04-11 17:33:30 -0600614 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000615#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000616
617 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
618
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100619 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600620 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700621
Jamie McCrae56cb6102022-03-23 11:57:03 +0000622 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
623
David Brown0d0652a2017-04-11 17:33:30 -0600624 BOOT_LOG_ERR("Never should get here");
625 while (1)
626 ;
David Brown5153bd62017-01-06 11:16:53 -0700627}