blob: 2deef1ef657fa42a5d3b616b930c1a4eac434370 [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
Jamie McCraeb3e3ce32023-02-22 09:11:57 +00004 * Copyright (c) 2021-2023 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
Jamie McCraeb3e3ce32023-02-22 09:11:57 +000055#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
56#include <zephyr/retention/bootmode.h>
57#endif
58
Josh Gao837cf882020-11-13 18:51:27 -080059#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020060#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053061#endif
62
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010063#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
64#include <arm_cleanup.h>
65#endif
66
Jamie McCrae35941fe2023-03-28 10:04:18 +010067#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
68#include <zephyr/drivers/hwinfo.h>
69#endif
70
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010071/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
72 * replaced by CONFIG_LOG_MODE_MINIMAL.
73 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000074#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
75#define ZEPHYR_LOG_MODE_MINIMAL 1
76#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010077
Marek Pietafb47d2e2022-03-11 14:24:07 +010078/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
79 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
80 */
81#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
82#define ZEPHYR_LOG_MODE_IMMEDIATE 1
83#endif
84
85#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000086 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010087#ifdef CONFIG_LOG_PROCESS_THREAD
88#warning "The log internal thread for log processing can't transfer the log"\
89 "well for MCUBoot."
90#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020091#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010092
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010093#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010094
95/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010096K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010097struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010098volatile bool boot_log_stop = false;
99K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100100
101/* log processing need to be initalized by the application */
102#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100103#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100104#endif /* CONFIG_LOG_PROCESS_THREAD */
105#else
106/* synchronous log mode doesn't need to be initalized by the application */
107#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100108#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +0100109#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
110 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
111 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100112
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100113#ifdef CONFIG_SOC_FAMILY_NRF
Radoslaw Koppel72006692021-12-06 16:15:21 +0100114#include <helpers/nrfx_reset_reason.h>
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100115
116static inline bool boot_skip_serial_recovery()
117{
Radoslaw Koppel72006692021-12-06 16:15:21 +0100118 uint32_t rr = nrfx_reset_reason_get();
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100119
Radoslaw Koppel72006692021-12-06 16:15:21 +0100120 return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100121}
122#else
123static inline bool boot_skip_serial_recovery()
124{
125 return false;
126}
127#endif
128
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200129BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100130
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000131/* Validate serial recovery configuration */
132#ifdef CONFIG_MCUBOOT_SERIAL
133#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
134 !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
Jamie McCraefd79db32023-03-10 11:19:36 +0000135 !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
Jamie McCrae35941fe2023-03-28 10:04:18 +0100136 !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
137 !defined(CONFIG_BOOT_SERIAL_PIN_RESET)
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000138#error "Serial recovery selected without an entrance mode set"
139#endif
140#endif
141
Jared Wolff8e4d7912021-01-21 19:34:05 -0500142#ifdef CONFIG_MCUBOOT_INDICATION_LED
Jared Wolff8e4d7912021-01-21 19:34:05 -0500143
144/*
145 * The led0 devicetree alias is optional. If present, we'll use it
146 * to turn on the LED whenever the button is pressed.
147 */
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200148#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200149#define LED0_NODE DT_ALIAS(mcuboot_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200150#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
151#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
Jared Wolff8e4d7912021-01-21 19:34:05 -0500152#define LED0_NODE DT_ALIAS(bootloader_led0)
Andrzej Puzdrowski2f545b82022-05-26 14:28:07 +0200153#endif
Jared Wolff8e4d7912021-01-21 19:34:05 -0500154
155#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200156static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
Josh Gao837cf882020-11-13 18:51:27 -0800157#else
Jared Wolff8e4d7912021-01-21 19:34:05 -0500158/* A build error here means your board isn't set up to drive an LED. */
159#error "Unsupported board: led0 devicetree alias is not defined"
160#endif
161
Jared Wolff8e4d7912021-01-21 19:34:05 -0500162void led_init(void)
163{
Martin Jäger477ed812022-07-21 11:45:45 +0200164 if (!device_is_ready(led0.port)) {
165 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
166 return;
167 }
Josh Gao837cf882020-11-13 18:51:27 -0800168
Martin Jäger477ed812022-07-21 11:45:45 +0200169 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
170 gpio_pin_set_dt(&led0, 0);
Jared Wolff8e4d7912021-01-21 19:34:05 -0500171}
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200172#endif /* CONFIG_MCUBOOT_INDICATION_LED */
Jared Wolff8e4d7912021-01-21 19:34:05 -0500173
Andrew Boie7238f512017-03-02 13:39:06 -0800174void os_heap_init(void);
175
176#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200177
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200178#ifdef CONFIG_SW_VECTOR_RELAY
179extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200180#endif
181
Andrew Boie7238f512017-03-02 13:39:06 -0800182struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600183 uint32_t msp;
184 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700185};
186
Andrew Boie7238f512017-03-02 13:39:06 -0800187static void do_boot(struct boot_rsp *rsp)
188{
David Brown0d0652a2017-04-11 17:33:30 -0600189 struct arm_vector_table *vt;
Andrew Boie7238f512017-03-02 13:39:06 -0800190
David Brown0d0652a2017-04-11 17:33:30 -0600191 /* The beginning of the image is the ARM vector table, containing
192 * the initial stack pointer address and the reset vector
193 * consecutively. Manually set the stack pointer and jump into the
194 * reset vector
195 */
Daniel DeGrassee4574442022-09-01 15:47:01 -0500196#ifdef CONFIG_BOOT_RAM_LOAD
197 /* Get ram address for image */
198 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
199#else
200 uintptr_t flash_base;
201 int rc;
202
203 /* Jump to flash image */
Marti Bolivareb940802017-05-01 23:15:29 -0400204 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
205 assert(rc == 0);
206
207 vt = (struct arm_vector_table *)(flash_base +
208 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600209 rsp->br_hdr->ih_hdr_size);
Daniel DeGrassee4574442022-09-01 15:47:01 -0500210#endif
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200211
Joakim Andersson90b8f692022-12-21 15:52:53 +0100212 if (IS_ENABLED(CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)) {
213 sys_clock_disable();
214 }
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100215
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200216#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200217 /* Disable the USB to prevent it from firing interrupts */
218 usb_disable();
219#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100220#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
221 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200222
Ryan McClelland179d8fa2022-05-20 23:53:35 -0700223#ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200224 /* Disable instruction cache and data cache before chain-load the application */
225 SCB_DisableDCache();
226 SCB_DisableICache();
227#endif
228
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100229#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200230 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100231#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200232
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000233#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
234 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
235 /* Reset limit registers to avoid inflicting stack overflow on image
236 * being booted.
237 */
238 __set_PSPLIM(0);
239 __set_MSPLIM(0);
240#endif
241
Sigvart Hovland9647c462021-08-20 16:33:55 +0200242#else
243 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200244#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
245
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200246#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200247#if defined(CONFIG_SW_VECTOR_RELAY)
248 _vector_table_pointer = vt;
249#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
250 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200251#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200252#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
253 SCB->VTOR = (uint32_t)vt;
254#endif /* CONFIG_SW_VECTOR_RELAY */
255#else /* CONFIG_BOOT_INTR_VEC_RELOC */
256#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
257 _vector_table_pointer = _vector_start;
258 SCB->VTOR = (uint32_t)__vector_relay_table;
259#endif
260#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200261
David Brown57837b92018-03-13 15:56:38 -0600262 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100263#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
264 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100265 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100266#endif
David Brown0d0652a2017-04-11 17:33:30 -0600267 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800268}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530269
270#elif defined(CONFIG_XTENSA)
271#define SRAM_BASE_ADDRESS 0xBE030000
272
273static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
274{
275 const struct flash_area *fap;
276 int area_id;
277 int rc;
278 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
279
280 BOOT_LOG_INF("Copying image to SRAM");
281
282 area_id = flash_area_id_from_image_slot(slot);
283 rc = flash_area_open(area_id, &fap);
284 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300285 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530286 goto done;
287 }
288
289 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
290 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300291 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530292 goto done;
293 }
294
295done:
296 flash_area_close(fap);
297}
298
299/* Entry point (.ResetVector) is at the very beginning of the image.
300 * Simply copy the image to a suitable location and jump there.
301 */
302static void do_boot(struct boot_rsp *rsp)
303{
304 void *start;
305
306 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
307 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
308
309 /* Copy from the flash to HP SRAM */
310 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
311
312 /* Jump to entry point */
313 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
314 ((void (*)(void))start)();
315}
316
Andrew Boie7238f512017-03-02 13:39:06 -0800317#else
318/* Default: Assume entry point is at the very beginning of the image. Simply
319 * lock interrupts and jump there. This is the right thing to do for X86 and
320 * possibly other platforms.
321 */
322static void do_boot(struct boot_rsp *rsp)
323{
David Brown0d0652a2017-04-11 17:33:30 -0600324 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200325
Jim Tanee1b7b92022-04-07 11:26:28 +0800326#if defined(MCUBOOT_RAM_LOAD)
327 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
328#else
329 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400330 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800331
Marti Bolivareb940802017-05-01 23:15:29 -0400332 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
333 assert(rc == 0);
334
335 start = (void *)(flash_base + rsp->br_image_off +
336 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800337#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800338
David Brown0d0652a2017-04-11 17:33:30 -0600339 /* Lock interrupts and dive into the entry point */
340 irq_lock();
341 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800342}
343#endif
David Brown5153bd62017-01-06 11:16:53 -0700344
Marek Pietafb47d2e2022-03-11 14:24:07 +0100345#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000346 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100347/* The log internal thread for log processing can't transfer log well as has too
348 * low priority.
349 * Dedicated thread for log processing below uses highest application
350 * priority. This allows to transmit all logs without adding k_sleep/k_yield
351 * anywhere else int the code.
352 */
353
354/* most simple log processing theread */
355void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
356{
357 (void)dummy1;
358 (void)dummy2;
359 (void)dummy3;
360
Martin Jäger477ed812022-07-21 11:45:45 +0200361 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100362
Martin Jäger477ed812022-07-21 11:45:45 +0200363 while (1) {
Martin Jäger31751822022-07-21 11:49:32 +0200364#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
365 /* support Zephyr legacy logging implementation before commit c5f2cde */
Martin Jäger477ed812022-07-21 11:45:45 +0200366 if (log_process(false) == false) {
Martin Jäger31751822022-07-21 11:49:32 +0200367#else
368 if (log_process() == false) {
369#endif
Martin Jäger477ed812022-07-21 11:45:45 +0200370 if (boot_log_stop) {
371 break;
372 }
373 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
374 }
375 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100376
Martin Jäger477ed812022-07-21 11:45:45 +0200377 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100378}
379
380void zephyr_boot_log_start(void)
381{
Martin Jäger477ed812022-07-21 11:45:45 +0200382 /* start logging thread */
383 k_thread_create(&boot_log_thread, boot_log_stack,
384 K_THREAD_STACK_SIZEOF(boot_log_stack),
385 boot_log_thread_func, NULL, NULL, NULL,
386 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
387 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100388
Martin Jäger477ed812022-07-21 11:45:45 +0200389 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100390}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100391
392void zephyr_boot_log_stop(void)
393{
394 boot_log_stop = true;
395
396 /* wait until log procesing thread expired
397 * This can be reworked using a thread_join() API once a such will be
398 * available in zephyr.
399 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
400 */
401 (void)k_sem_take(&boot_log_sem, K_FOREVER);
402}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100403#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
404 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
405 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100406
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000407#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200408
409#ifdef CONFIG_MCUBOOT_SERIAL
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200410#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
411#else
412#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
413#endif
414
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200415#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
416
417#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200418static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000419#else
Jamie McCrae9551b6e2023-02-28 15:47:42 +0000420#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200421#endif
422
423static bool detect_pin(void)
Josh Gao837cf882020-11-13 18:51:27 -0800424{
425 int rc;
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200426 int pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800427
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200428 if (!device_is_ready(button0.port)) {
429 __ASSERT(false, "GPIO device is not ready.\n");
430 return false;
431 }
Josh Gao837cf882020-11-13 18:51:27 -0800432
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200433 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
Josh Gao837cf882020-11-13 18:51:27 -0800434 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
435
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200436 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200437 pin_active = rc;
Andrzej Puzdrowski8bd30812021-03-22 08:19:41 +0100438
Josh Gao837cf882020-11-13 18:51:27 -0800439 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
440
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200441 if (pin_active) {
442 if (BUTTON_0_DETECT_DELAY > 0) {
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200443#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800444 k_sleep(K_MSEC(50));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200445#else
446 k_busy_wait(50000);
447#endif
Josh Gao837cf882020-11-13 18:51:27 -0800448
449 /* Get the uptime for debounce purposes. */
450 int64_t timestamp = k_uptime_get();
451
452 for(;;) {
Andrzej Puzdrowski7ed0e6d2022-04-09 13:25:35 +0200453 rc = gpio_pin_get_dt(&button0);
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200454 pin_active = rc;
Josh Gao837cf882020-11-13 18:51:27 -0800455 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
456
457 /* Get delta from when this started */
458 uint32_t delta = k_uptime_get() - timestamp;
459
460 /* If not pressed OR if pressed > debounce period, stop. */
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200461 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
Josh Gao837cf882020-11-13 18:51:27 -0800462 break;
463 }
464
465 /* Delay 1 ms */
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200466#ifdef CONFIG_MULTITHREADING
Josh Gao837cf882020-11-13 18:51:27 -0800467 k_sleep(K_MSEC(1));
Andrzej Puzdrowski9b974562021-05-17 11:37:00 +0200468#else
469 k_busy_wait(1000);
470#endif
Josh Gao837cf882020-11-13 18:51:27 -0800471 }
472 }
473 }
474
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200475 return (bool)pin_active;
Josh Gao837cf882020-11-13 18:51:27 -0800476}
477#endif
478
Jamie McCrae6902abb2023-04-04 13:50:43 +0100479#ifdef CONFIG_MCUBOOT_SERIAL
480static void boot_serial_enter()
481{
482 int rc;
483
484#ifdef CONFIG_MCUBOOT_INDICATION_LED
485 gpio_pin_set_dt(&led0, 1);
486#endif
487
488 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
489
490 BOOT_LOG_INF("Enter the serial recovery mode");
491 rc = boot_console_init();
492 __ASSERT(rc == 0, "Error initializing boot console.\n");
493 boot_serial_start(&boot_funcs);
494 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
495}
496#endif
497
David Brown5153bd62017-01-06 11:16:53 -0700498void main(void)
499{
David Brown0d0652a2017-04-11 17:33:30 -0600500 struct boot_rsp rsp;
501 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100502 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Brown5153bd62017-01-06 11:16:53 -0700503
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000504#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
505 int32_t boot_mode;
506#endif
507
Jamie McCrae35941fe2023-03-28 10:04:18 +0100508#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
509 uint32_t reset_cause;
510#endif
511
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200512 MCUBOOT_WATCHDOG_FEED();
513
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000514#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600515 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000516#else
517 BOOT_LOG_INF("Starting Direct-XIP bootloader");
518#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200519
Jared Wolff8e4d7912021-01-21 19:34:05 -0500520#ifdef CONFIG_MCUBOOT_INDICATION_LED
521 /* LED init */
522 led_init();
523#endif
524
David Brown0d0652a2017-04-11 17:33:30 -0600525 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700526
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100527 ZEPHYR_BOOT_LOG_START();
528
Tamas Banee6615d2020-09-30 07:58:48 +0100529 (void)rc;
530
Jamie McCrae56cb6102022-03-23 11:57:03 +0000531 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
532
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000533#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200534 if (detect_pin() &&
Josh Gao837cf882020-11-13 18:51:27 -0800535 !boot_skip_serial_recovery()) {
Jamie McCrae6902abb2023-04-04 13:50:43 +0100536 boot_serial_enter();
Josh Gao837cf882020-11-13 18:51:27 -0800537 }
538#endif
539
Jamie McCrae35941fe2023-03-28 10:04:18 +0100540#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
541 rc = hwinfo_get_reset_cause(&reset_cause);
542
543 if (rc == 0 && reset_cause == RESET_PIN) {
Jamie McCrae35941fe2023-03-28 10:04:18 +0100544 (void)hwinfo_clear_reset_cause();
Jamie McCrae6902abb2023-04-04 13:50:43 +0100545 boot_serial_enter();
Jamie McCrae35941fe2023-03-28 10:04:18 +0100546 }
547#endif
548
Josh Gao837cf882020-11-13 18:51:27 -0800549#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Andrzej Puzdrowski2ab49872022-04-09 13:03:58 +0200550 if (detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800551#ifdef CONFIG_MCUBOOT_INDICATION_LED
Andrzej Puzdrowski5f317e42022-05-26 15:30:14 +0200552 gpio_pin_set_dt(&led0, 1);
Josh Gao837cf882020-11-13 18:51:27 -0800553#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000554
555 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
556
Josh Gao837cf882020-11-13 18:51:27 -0800557 rc = usb_enable(NULL);
558 if (rc) {
559 BOOT_LOG_ERR("Cannot enable USB");
560 } else {
561 BOOT_LOG_INF("Waiting for USB DFU");
562 wait_for_usb_dfu(K_FOREVER);
563 BOOT_LOG_INF("USB DFU wait time elapsed");
564 }
565 }
566#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100567 rc = usb_enable(NULL);
568 if (rc) {
569 BOOT_LOG_ERR("Cannot enable USB");
570 } else {
571 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000572
573 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
574
Josh Gao837cf882020-11-13 18:51:27 -0800575 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100576 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000577
578 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100579 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530580#endif
581
Wouter Cappellee3822f82022-01-19 15:39:43 +0100582#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
583 /* Initialize the boot console, so we can already fill up our buffers while
584 * waiting for the boot image check to finish. This image check, can take
585 * some time, so it's better to reuse thistime to already receive the
586 * initial mcumgr command(s) into our buffers
587 */
588 rc = boot_console_init();
589 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
590 uint32_t start = k_uptime_get_32();
591#endif
592
Tamas Banee6615d2020-09-30 07:58:48 +0100593 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100594
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000595#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
596 boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
597
598 if (boot_mode == 1) {
599 /* Boot mode to stay in bootloader, clear status and enter serial
600 * recovery mode
601 */
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000602 bootmode_clear();
Jamie McCrae6902abb2023-04-04 13:50:43 +0100603 boot_serial_enter();
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000604 }
605#endif
606
Wouter Cappellee3822f82022-01-19 15:39:43 +0100607#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
608 timeout_in_ms -= (k_uptime_get_32() - start);
609 if( timeout_in_ms <= 0 ) {
610 /* at least one check if time was expired */
611 timeout_in_ms = 1;
612 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100613 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100614#endif
615
Michael Grand5047f032022-11-24 16:49:56 +0100616 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600617 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000618
619 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
620
Jamie McCraefd79db32023-03-10 11:19:36 +0000621#ifdef CONFIG_BOOT_SERIAL_NO_APPLICATION
622 /* No bootable image and configuration set to remain in serial
623 * recovery mode
624 */
Jamie McCrae6902abb2023-04-04 13:50:43 +0100625 boot_serial_enter();
Jamie McCraefd79db32023-03-10 11:19:36 +0000626#endif
627
Tamas Banee6615d2020-09-30 07:58:48 +0100628 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600629 }
David Brown5153bd62017-01-06 11:16:53 -0700630
Marti Bolivar88f48d92017-05-01 22:30:02 -0400631 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
632 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200633
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000634#if defined(MCUBOOT_DIRECT_XIP)
635 BOOT_LOG_INF("Jumping to the image slot");
636#else
David Brown0d0652a2017-04-11 17:33:30 -0600637 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000638#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000639
640 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
641
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100642 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600643 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700644
Jamie McCrae56cb6102022-03-23 11:57:03 +0000645 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
646
David Brown0d0652a2017-04-11 17:33:30 -0600647 BOOT_LOG_ERR("Never should get here");
648 while (1)
649 ;
David Brown5153bd62017-01-06 11:16:53 -0700650}