blob: 686e02eb9189b53c7aef8b8d7039434c3f805fd2 [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
Jamie McCrae24ac8cc2024-03-19 08:18:44 +000030#if defined(CONFIG_BOOT_DISABLE_CACHES)
31#include <zephyr/cache.h>
32#endif
33
Lucas Tamborrino9bf7ce82023-09-05 11:20:50 -030034#if defined(CONFIG_ARM)
Gerard Marull-Paretas11ecbf62023-06-30 09:58:41 +020035#include <cmsis_core.h>
Lucas Tamborrino9bf7ce82023-09-05 11:20:50 -030036#endif
Yonatan Schachterb22eb6a2022-07-29 18:16:09 +030037
Jamie McCrae433b8482023-08-16 07:33:24 +010038#include "io/io.h"
Marti Bolivar51181cf2017-03-20 11:03:41 -040039#include "target.h"
40
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050041#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020042#include "bootutil/image.h"
43#include "bootutil/bootutil.h"
Tamas Banee6615d2020-09-30 07:58:48 +010044#include "bootutil/fault_injection_hardening.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000045#include "bootutil/mcuboot_status.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020046#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020047
Marek Matej67693442023-01-23 17:54:36 +010048/* Check if Espressif target is supported */
Sylvio Alvesb794d332024-03-04 10:08:30 -030049#ifdef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
Marek Matej67693442023-01-23 17:54:36 +010050
51#include <bootloader_init.h>
52#include <esp_loader.h>
53
54#define IMAGE_INDEX_0 0
55#define IMAGE_INDEX_1 1
56
57#define PRIMARY_SLOT 0
58#define SECONDARY_SLOT 1
59
60#define IMAGE0_PRIMARY_START_ADDRESS \
61 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 0)
62#define IMAGE0_PRIMARY_SIZE \
63 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_0), reg, 1)
64
65#define IMAGE1_PRIMARY_START_ADDRESS \
66 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 0)
67#define IMAGE1_PRIMARY_SIZE \
68 DT_PROP_BY_IDX(DT_NODE_BY_FIXED_PARTITION_LABEL(image_1), reg, 1)
69
Sylvio Alvesb794d332024-03-04 10:08:30 -030070#endif /* CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
Marek Matej67693442023-01-23 17:54:36 +010071
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020072#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030073#include "boot_serial/boot_serial.h"
74#include "serial_adapter/serial_adapter.h"
75
76const struct boot_uart_funcs boot_funcs = {
77 .read = console_read,
78 .write = console_write
79};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020080#endif
81
Josh Gao837cf882020-11-13 18:51:27 -080082#if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +020083#include <zephyr/usb/class/usb_dfu.h>
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053084#endif
85
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010086#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
87#include <arm_cleanup.h>
88#endif
89
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010090/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
91 * replaced by CONFIG_LOG_MODE_MINIMAL.
92 */
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +000093#if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL))
94#define ZEPHYR_LOG_MODE_MINIMAL 1
95#endif
Gerard Marull-Paretasa513b8e2021-01-26 22:50:38 +010096
Marek Pietafb47d2e2022-03-11 14:24:07 +010097/* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property,
98 * replaced by CONFIG_LOG_MODE_IMMEDIATE.
99 */
100#if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE))
101#define ZEPHYR_LOG_MODE_IMMEDIATE 1
102#endif
103
104#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000105 !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100106#ifdef CONFIG_LOG_PROCESS_THREAD
107#warning "The log internal thread for log processing can't transfer the log"\
108 "well for MCUBoot."
109#else
Gerard Marull-Paretas3cd2cec2022-05-09 12:10:05 +0200110#include <zephyr/logging/log_ctrl.h>
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100111
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +0100112#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100113
114/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +0100115K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100116struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100117volatile bool boot_log_stop = false;
118K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100119
120/* log processing need to be initalized by the application */
121#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100122#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100123#endif /* CONFIG_LOG_PROCESS_THREAD */
124#else
125/* synchronous log mode doesn't need to be initalized by the application */
126#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100127#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Marek Pietafb47d2e2022-03-11 14:24:07 +0100128#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
129 * !defined(ZEPHYR_LOG_MODE_MINIMAL)
130 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100131
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +0200132BOOT_LOG_MODULE_REGISTER(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100133
Andrew Boie7238f512017-03-02 13:39:06 -0800134void os_heap_init(void);
135
136#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200137
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200138#ifdef CONFIG_SW_VECTOR_RELAY
139extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200140#endif
141
Andrew Boie7238f512017-03-02 13:39:06 -0800142struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600143 uint32_t msp;
144 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700145};
146
Andrew Boie7238f512017-03-02 13:39:06 -0800147static void do_boot(struct boot_rsp *rsp)
148{
David Brown0d0652a2017-04-11 17:33:30 -0600149 struct arm_vector_table *vt;
Andrew Boie7238f512017-03-02 13:39:06 -0800150
David Brown0d0652a2017-04-11 17:33:30 -0600151 /* The beginning of the image is the ARM vector table, containing
152 * the initial stack pointer address and the reset vector
153 * consecutively. Manually set the stack pointer and jump into the
154 * reset vector
155 */
Daniel DeGrassee4574442022-09-01 15:47:01 -0500156#ifdef CONFIG_BOOT_RAM_LOAD
157 /* Get ram address for image */
158 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
159#else
160 uintptr_t flash_base;
161 int rc;
162
163 /* Jump to flash image */
Marti Bolivareb940802017-05-01 23:15:29 -0400164 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
165 assert(rc == 0);
166
167 vt = (struct arm_vector_table *)(flash_base +
168 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600169 rsp->br_hdr->ih_hdr_size);
Daniel DeGrassee4574442022-09-01 15:47:01 -0500170#endif
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200171
Joakim Andersson90b8f692022-12-21 15:52:53 +0100172 if (IS_ENABLED(CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)) {
173 sys_clock_disable();
174 }
Andrzej Puzdrowskie9c6b4d2021-12-14 14:09:02 +0100175
Johann Fischerd2e87aa2021-08-27 13:30:07 +0200176#ifdef CONFIG_USB_DEVICE_STACK
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200177 /* Disable the USB to prevent it from firing interrupts */
178 usb_disable();
179#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100180#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
181 cleanup_arm_nvic(); /* cleanup NVIC registers */
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200182
Jamie McCrae24ac8cc2024-03-19 08:18:44 +0000183#if defined(CONFIG_BOOT_DISABLE_CACHES)
184 /* Flush and disable instruction/data caches before chain-loading the application */
185 (void)sys_cache_instr_flush_all();
186 (void)sys_cache_data_flush_all();
187 sys_cache_instr_disable();
188 sys_cache_data_disable();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200189#endif
190
Henrik Brix Andersen008f4a72020-12-08 14:40:19 +0100191#if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200192 z_arm_clear_arm_mpu_config();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100193#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200194
Håkon Øye Amundsen6a8dbba2020-10-01 12:52:38 +0000195#if defined(CONFIG_BUILTIN_STACK_GUARD) && \
196 defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM)
197 /* Reset limit registers to avoid inflicting stack overflow on image
198 * being booted.
199 */
200 __set_PSPLIM(0);
201 __set_MSPLIM(0);
202#endif
203
Sigvart Hovland9647c462021-08-20 16:33:55 +0200204#else
205 irq_lock();
Ioannis Glaropoulos70af7082020-10-22 15:14:48 +0200206#endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */
207
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200208#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200209#if defined(CONFIG_SW_VECTOR_RELAY)
210 _vector_table_pointer = vt;
211#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
212 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200213#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200214#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
215 SCB->VTOR = (uint32_t)vt;
216#endif /* CONFIG_SW_VECTOR_RELAY */
217#else /* CONFIG_BOOT_INTR_VEC_RELOC */
218#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
219 _vector_table_pointer = _vector_start;
220 SCB->VTOR = (uint32_t)__vector_relay_table;
221#endif
222#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200223
David Brown57837b92018-03-13 15:56:38 -0600224 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100225#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
226 __set_CONTROL(0x00); /* application will configures core on its own */
Andrzej Puzdrowski56c15e72020-10-29 09:16:50 +0100227 __ISB();
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100228#endif
David Brown0d0652a2017-04-11 17:33:30 -0600229 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800230}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530231
Marek Matej67693442023-01-23 17:54:36 +0100232#elif defined(CONFIG_XTENSA) || defined(CONFIG_RISCV)
233
Sylvio Alvesb794d332024-03-04 10:08:30 -0300234#ifndef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
Marek Matej67693442023-01-23 17:54:36 +0100235
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530236#define SRAM_BASE_ADDRESS 0xBE030000
237
238static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
239{
240 const struct flash_area *fap;
241 int area_id;
242 int rc;
243 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
244
245 BOOT_LOG_INF("Copying image to SRAM");
246
247 area_id = flash_area_id_from_image_slot(slot);
248 rc = flash_area_open(area_id, &fap);
249 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300250 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530251 goto done;
252 }
253
254 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
255 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300256 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530257 goto done;
258 }
259
260done:
261 flash_area_close(fap);
262}
Sylvio Alvesb794d332024-03-04 10:08:30 -0300263#endif /* !CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530264
265/* Entry point (.ResetVector) is at the very beginning of the image.
266 * Simply copy the image to a suitable location and jump there.
267 */
268static void do_boot(struct boot_rsp *rsp)
269{
270 void *start;
271
272 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
273 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
274
Sylvio Alvesb794d332024-03-04 10:08:30 -0300275#ifdef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
Marek Matej67693442023-01-23 17:54:36 +0100276 int slot = (rsp->br_image_off == IMAGE0_PRIMARY_START_ADDRESS) ?
277 PRIMARY_SLOT : SECONDARY_SLOT;
278 /* Load memory segments and start from entry point */
279 start_cpu0_image(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size);
280#else
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530281 /* Copy from the flash to HP SRAM */
282 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
283
284 /* Jump to entry point */
285 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
286 ((void (*)(void))start)();
Sylvio Alvesb794d332024-03-04 10:08:30 -0300287#endif /* CONFIG_SOC_FAMILY_ESPRESSIF_ESP32 */
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530288}
289
Andrew Boie7238f512017-03-02 13:39:06 -0800290#else
291/* Default: Assume entry point is at the very beginning of the image. Simply
292 * lock interrupts and jump there. This is the right thing to do for X86 and
293 * possibly other platforms.
294 */
295static void do_boot(struct boot_rsp *rsp)
296{
David Brown0d0652a2017-04-11 17:33:30 -0600297 void *start;
Martin Jäger477ed812022-07-21 11:45:45 +0200298
Jim Tanee1b7b92022-04-07 11:26:28 +0800299#if defined(MCUBOOT_RAM_LOAD)
300 start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size);
301#else
302 uintptr_t flash_base;
Marti Bolivareb940802017-05-01 23:15:29 -0400303 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800304
Marti Bolivareb940802017-05-01 23:15:29 -0400305 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
306 assert(rc == 0);
307
308 start = (void *)(flash_base + rsp->br_image_off +
309 rsp->br_hdr->ih_hdr_size);
Jim Tanee1b7b92022-04-07 11:26:28 +0800310#endif
Andrew Boie7238f512017-03-02 13:39:06 -0800311
David Brown0d0652a2017-04-11 17:33:30 -0600312 /* Lock interrupts and dive into the entry point */
313 irq_lock();
314 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800315}
316#endif
David Brown5153bd62017-01-06 11:16:53 -0700317
Marek Pietafb47d2e2022-03-11 14:24:07 +0100318#if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
Dominik Ermel5b7ed6a2021-02-26 14:26:48 +0000319 !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100320/* The log internal thread for log processing can't transfer log well as has too
321 * low priority.
322 * Dedicated thread for log processing below uses highest application
323 * priority. This allows to transmit all logs without adding k_sleep/k_yield
324 * anywhere else int the code.
325 */
326
327/* most simple log processing theread */
328void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
329{
330 (void)dummy1;
331 (void)dummy2;
332 (void)dummy3;
333
Martin Jäger477ed812022-07-21 11:45:45 +0200334 log_init();
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100335
Martin Jäger477ed812022-07-21 11:45:45 +0200336 while (1) {
Martin Jäger31751822022-07-21 11:49:32 +0200337#if defined(CONFIG_LOG1) || defined(CONFIG_LOG2)
338 /* support Zephyr legacy logging implementation before commit c5f2cde */
Martin Jäger477ed812022-07-21 11:45:45 +0200339 if (log_process(false) == false) {
Martin Jäger31751822022-07-21 11:49:32 +0200340#else
341 if (log_process() == false) {
342#endif
Martin Jäger477ed812022-07-21 11:45:45 +0200343 if (boot_log_stop) {
344 break;
345 }
346 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
347 }
348 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100349
Martin Jäger477ed812022-07-21 11:45:45 +0200350 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100351}
352
353void zephyr_boot_log_start(void)
354{
Martin Jäger477ed812022-07-21 11:45:45 +0200355 /* start logging thread */
356 k_thread_create(&boot_log_thread, boot_log_stack,
357 K_THREAD_STACK_SIZEOF(boot_log_stack),
358 boot_log_thread_func, NULL, NULL, NULL,
359 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
360 BOOT_LOG_PROCESSING_INTERVAL);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100361
Martin Jäger477ed812022-07-21 11:45:45 +0200362 k_thread_name_set(&boot_log_thread, "logging");
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100363}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100364
365void zephyr_boot_log_stop(void)
366{
367 boot_log_stop = true;
368
369 /* wait until log procesing thread expired
370 * This can be reworked using a thread_join() API once a such will be
371 * available in zephyr.
372 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
373 */
374 (void)k_sem_take(&boot_log_sem, K_FOREVER);
375}
Marek Pietafb47d2e2022-03-11 14:24:07 +0100376#endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \
377 * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
378 */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100379
Jamie McCrae6902abb2023-04-04 13:50:43 +0100380#ifdef CONFIG_MCUBOOT_SERIAL
381static void boot_serial_enter()
382{
383 int rc;
384
385#ifdef CONFIG_MCUBOOT_INDICATION_LED
Piotr Dymacz2a74a2b2023-12-05 14:26:22 +0100386 io_led_set(1);
Jamie McCrae6902abb2023-04-04 13:50:43 +0100387#endif
388
389 mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED);
390
391 BOOT_LOG_INF("Enter the serial recovery mode");
392 rc = boot_console_init();
393 __ASSERT(rc == 0, "Error initializing boot console.\n");
394 boot_serial_start(&boot_funcs);
395 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
396}
397#endif
398
Keith Packardec2ac822023-04-27 09:56:10 -0700399int main(void)
David Brown5153bd62017-01-06 11:16:53 -0700400{
David Brown0d0652a2017-04-11 17:33:30 -0600401 struct boot_rsp rsp;
402 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100403 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Brown5153bd62017-01-06 11:16:53 -0700404
Jeppe Odgaard4420bb62023-04-11 09:43:10 +0200405 MCUBOOT_WATCHDOG_SETUP();
Andrzej Puzdrowski210b3182020-10-13 13:52:15 +0200406 MCUBOOT_WATCHDOG_FEED();
407
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000408#if !defined(MCUBOOT_DIRECT_XIP)
David Brown0d0652a2017-04-11 17:33:30 -0600409 BOOT_LOG_INF("Starting bootloader");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000410#else
411 BOOT_LOG_INF("Starting Direct-XIP bootloader");
412#endif
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200413
Jared Wolff8e4d7912021-01-21 19:34:05 -0500414#ifdef CONFIG_MCUBOOT_INDICATION_LED
415 /* LED init */
Piotr Dymaczd99154f2023-12-05 13:41:59 +0100416 io_led_init();
Jared Wolff8e4d7912021-01-21 19:34:05 -0500417#endif
418
David Brown0d0652a2017-04-11 17:33:30 -0600419 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700420
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100421 ZEPHYR_BOOT_LOG_START();
422
Tamas Banee6615d2020-09-30 07:58:48 +0100423 (void)rc;
424
Jamie McCrae56cb6102022-03-23 11:57:03 +0000425 mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
426
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000427#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
Jamie McCrae433b8482023-08-16 07:33:24 +0100428 if (io_detect_pin() &&
429 !io_boot_skip_serial_recovery()) {
Jamie McCrae6902abb2023-04-04 13:50:43 +0100430 boot_serial_enter();
Josh Gao837cf882020-11-13 18:51:27 -0800431 }
432#endif
433
Jamie McCrae35941fe2023-03-28 10:04:18 +0100434#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
Jamie McCrae433b8482023-08-16 07:33:24 +0100435 if (io_detect_pin_reset()) {
Jamie McCrae6902abb2023-04-04 13:50:43 +0100436 boot_serial_enter();
Jamie McCrae35941fe2023-03-28 10:04:18 +0100437 }
438#endif
439
Josh Gao837cf882020-11-13 18:51:27 -0800440#if defined(CONFIG_BOOT_USB_DFU_GPIO)
Jamie McCrae433b8482023-08-16 07:33:24 +0100441 if (io_detect_pin()) {
Josh Gao837cf882020-11-13 18:51:27 -0800442#ifdef CONFIG_MCUBOOT_INDICATION_LED
Piotr Dymacz2a74a2b2023-12-05 14:26:22 +0100443 io_led_set(1);
Josh Gao837cf882020-11-13 18:51:27 -0800444#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000445
446 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
447
Josh Gao837cf882020-11-13 18:51:27 -0800448 rc = usb_enable(NULL);
449 if (rc) {
450 BOOT_LOG_ERR("Cannot enable USB");
451 } else {
452 BOOT_LOG_INF("Waiting for USB DFU");
453 wait_for_usb_dfu(K_FOREVER);
454 BOOT_LOG_INF("USB DFU wait time elapsed");
455 }
456 }
457#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100458 rc = usb_enable(NULL);
459 if (rc) {
460 BOOT_LOG_ERR("Cannot enable USB");
461 } else {
462 BOOT_LOG_INF("Waiting for USB DFU");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000463
464 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
465
Josh Gao837cf882020-11-13 18:51:27 -0800466 wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100467 BOOT_LOG_INF("USB DFU wait time elapsed");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000468
469 mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100470 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530471#endif
472
Wouter Cappellee3822f82022-01-19 15:39:43 +0100473#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
474 /* Initialize the boot console, so we can already fill up our buffers while
475 * waiting for the boot image check to finish. This image check, can take
476 * some time, so it's better to reuse thistime to already receive the
477 * initial mcumgr command(s) into our buffers
478 */
479 rc = boot_console_init();
480 int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT;
481 uint32_t start = k_uptime_get_32();
Piotr Dymacz3790f5f2022-12-15 14:58:45 +0100482
483#ifdef CONFIG_MCUBOOT_INDICATION_LED
Piotr Dymacz2a74a2b2023-12-05 14:26:22 +0100484 io_led_set(1);
Piotr Dymacz3790f5f2022-12-15 14:58:45 +0100485#endif
Wouter Cappellee3822f82022-01-19 15:39:43 +0100486#endif
487
Tamas Banee6615d2020-09-30 07:58:48 +0100488 FIH_CALL(boot_go, fih_rc, &rsp);
Wouter Cappellee3822f82022-01-19 15:39:43 +0100489
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000490#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
Jamie McCrae433b8482023-08-16 07:33:24 +0100491 if (io_detect_boot_mode()) {
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000492 /* Boot mode to stay in bootloader, clear status and enter serial
493 * recovery mode
494 */
Jamie McCrae6902abb2023-04-04 13:50:43 +0100495 boot_serial_enter();
Jamie McCraeb3e3ce32023-02-22 09:11:57 +0000496 }
497#endif
498
Wouter Cappellee3822f82022-01-19 15:39:43 +0100499#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
500 timeout_in_ms -= (k_uptime_get_32() - start);
501 if( timeout_in_ms <= 0 ) {
502 /* at least one check if time was expired */
503 timeout_in_ms = 1;
504 }
Jamie McCrae254714b2022-04-07 09:00:31 +0100505 boot_serial_check_start(&boot_funcs,timeout_in_ms);
Piotr Dymacz3790f5f2022-12-15 14:58:45 +0100506
507#ifdef CONFIG_MCUBOOT_INDICATION_LED
Piotr Dymacz2a74a2b2023-12-05 14:26:22 +0100508 io_led_set(0);
Piotr Dymacz3790f5f2022-12-15 14:58:45 +0100509#endif
Wouter Cappellee3822f82022-01-19 15:39:43 +0100510#endif
511
Michael Grand5047f032022-11-24 16:49:56 +0100512 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
David Brown0d0652a2017-04-11 17:33:30 -0600513 BOOT_LOG_ERR("Unable to find bootable image");
Jamie McCrae56cb6102022-03-23 11:57:03 +0000514
515 mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND);
516
Jamie McCraefd79db32023-03-10 11:19:36 +0000517#ifdef CONFIG_BOOT_SERIAL_NO_APPLICATION
518 /* No bootable image and configuration set to remain in serial
519 * recovery mode
520 */
Jamie McCrae6902abb2023-04-04 13:50:43 +0100521 boot_serial_enter();
Jamie McCraefd79db32023-03-10 11:19:36 +0000522#endif
523
Tamas Banee6615d2020-09-30 07:58:48 +0100524 FIH_PANIC;
David Brown0d0652a2017-04-11 17:33:30 -0600525 }
David Brown5153bd62017-01-06 11:16:53 -0700526
Marti Bolivar88f48d92017-05-01 22:30:02 -0400527 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
528 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200529
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000530#if defined(MCUBOOT_DIRECT_XIP)
531 BOOT_LOG_INF("Jumping to the image slot");
532#else
David Brown0d0652a2017-04-11 17:33:30 -0600533 BOOT_LOG_INF("Jumping to the first image slot");
Dominik Ermelcd07ed32021-02-17 15:18:01 +0000534#endif
Jamie McCrae56cb6102022-03-23 11:57:03 +0000535
536 mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
537
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100538 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600539 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700540
Jamie McCrae56cb6102022-03-23 11:57:03 +0000541 mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);
542
David Brown0d0652a2017-04-11 17:33:30 -0600543 BOOT_LOG_ERR("Never should get here");
544 while (1)
545 ;
David Brown5153bd62017-01-06 11:16:53 -0700546}