David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 3 | * Copyright (c) 2020 Arm Limited |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2021 Nordic Semiconductor ASA |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 5 | * |
| 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 Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 19 | #include <assert.h> |
Gerard Marull-Paretas | 34dd9e7 | 2022-05-09 12:13:12 +0200 | [diff] [blame^] | 20 | #include <zephyr/kernel.h> |
Gerard Marull-Paretas | 3cd2cec | 2022-05-09 12:10:05 +0200 | [diff] [blame] | 21 | #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 Gates | 4632d8d | 2018-06-28 13:27:40 -0700 | [diff] [blame] | 27 | #include <soc.h> |
Gerard Marull-Paretas | 3cd2cec | 2022-05-09 12:10:05 +0200 | [diff] [blame] | 28 | #include <zephyr/linker/linker-defs.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 29 | |
Marti Bolivar | 51181cf | 2017-03-20 11:03:41 -0400 | [diff] [blame] | 30 | #include "target.h" |
| 31 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame] | 32 | #include "bootutil/bootutil_log.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 33 | #include "bootutil/image.h" |
| 34 | #include "bootutil/bootutil.h" |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 35 | #include "bootutil/fault_injection_hardening.h" |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 36 | #include "bootutil/mcuboot_status.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 37 | #include "flash_map_backend/flash_map_backend.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 38 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_MCUBOOT_SERIAL |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 40 | #include "boot_serial/boot_serial.h" |
| 41 | #include "serial_adapter/serial_adapter.h" |
| 42 | |
| 43 | const struct boot_uart_funcs boot_funcs = { |
| 44 | .read = console_read, |
| 45 | .write = console_write |
| 46 | }; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 47 | #endif |
| 48 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 49 | #if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO) |
Gerard Marull-Paretas | 3cd2cec | 2022-05-09 12:10:05 +0200 | [diff] [blame] | 50 | #include <zephyr/usb/class/usb_dfu.h> |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 51 | #endif |
| 52 | |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 53 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 54 | #include <arm_cleanup.h> |
| 55 | #endif |
| 56 | |
Gerard Marull-Paretas | a513b8e | 2021-01-26 22:50:38 +0100 | [diff] [blame] | 57 | /* CONFIG_LOG_MINIMAL is the legacy Kconfig property, |
| 58 | * replaced by CONFIG_LOG_MODE_MINIMAL. |
| 59 | */ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 60 | #if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL)) |
| 61 | #define ZEPHYR_LOG_MODE_MINIMAL 1 |
| 62 | #endif |
Gerard Marull-Paretas | a513b8e | 2021-01-26 22:50:38 +0100 | [diff] [blame] | 63 | |
Marek Pieta | fb47d2e | 2022-03-11 14:24:07 +0100 | [diff] [blame] | 64 | /* CONFIG_LOG_IMMEDIATE is the legacy Kconfig property, |
| 65 | * replaced by CONFIG_LOG_MODE_IMMEDIATE. |
| 66 | */ |
| 67 | #if (defined(CONFIG_LOG_MODE_IMMEDIATE) || defined(CONFIG_LOG_IMMEDIATE)) |
| 68 | #define ZEPHYR_LOG_MODE_IMMEDIATE 1 |
| 69 | #endif |
| 70 | |
| 71 | #if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 72 | !defined(ZEPHYR_LOG_MODE_MINIMAL) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 73 | #ifdef CONFIG_LOG_PROCESS_THREAD |
| 74 | #warning "The log internal thread for log processing can't transfer the log"\ |
| 75 | "well for MCUBoot." |
| 76 | #else |
Gerard Marull-Paretas | 3cd2cec | 2022-05-09 12:10:05 +0200 | [diff] [blame] | 77 | #include <zephyr/logging/log_ctrl.h> |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 78 | |
Krzysztof Chruscinski | 821214e | 2020-03-24 07:50:32 +0100 | [diff] [blame] | 79 | #define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */ |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 80 | |
| 81 | /* log are processing in custom routine */ |
Andrzej Puzdrowski | af14853 | 2020-02-25 12:51:26 +0100 | [diff] [blame] | 82 | K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 83 | struct k_thread boot_log_thread; |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 84 | volatile bool boot_log_stop = false; |
| 85 | K_SEM_DEFINE(boot_log_sem, 1, 1); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 86 | |
| 87 | /* log processing need to be initalized by the application */ |
| 88 | #define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start() |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 89 | #define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop() |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 90 | #endif /* CONFIG_LOG_PROCESS_THREAD */ |
| 91 | #else |
| 92 | /* synchronous log mode doesn't need to be initalized by the application */ |
| 93 | #define ZEPHYR_BOOT_LOG_START() do { } while (false) |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 94 | #define ZEPHYR_BOOT_LOG_STOP() do { } while (false) |
Marek Pieta | fb47d2e | 2022-03-11 14:24:07 +0100 | [diff] [blame] | 95 | #endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \ |
| 96 | * !defined(ZEPHYR_LOG_MODE_MINIMAL) |
| 97 | */ |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 98 | |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 99 | #ifdef CONFIG_SOC_FAMILY_NRF |
Radoslaw Koppel | 7200669 | 2021-12-06 16:15:21 +0100 | [diff] [blame] | 100 | #include <helpers/nrfx_reset_reason.h> |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 101 | |
| 102 | static inline bool boot_skip_serial_recovery() |
| 103 | { |
Radoslaw Koppel | 7200669 | 2021-12-06 16:15:21 +0100 | [diff] [blame] | 104 | uint32_t rr = nrfx_reset_reason_get(); |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 105 | |
Radoslaw Koppel | 7200669 | 2021-12-06 16:15:21 +0100 | [diff] [blame] | 106 | return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK)); |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 107 | } |
| 108 | #else |
| 109 | static inline bool boot_skip_serial_recovery() |
| 110 | { |
| 111 | return false; |
| 112 | } |
| 113 | #endif |
| 114 | |
Carlos Falgueras García | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 115 | BOOT_LOG_MODULE_REGISTER(mcuboot); |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 116 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 117 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * The led0 devicetree alias is optional. If present, we'll use it |
| 121 | * to turn on the LED whenever the button is pressed. |
| 122 | */ |
Andrzej Puzdrowski | 2f545b8 | 2022-05-26 14:28:07 +0200 | [diff] [blame] | 123 | #if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0)) |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 124 | #define LED0_NODE DT_ALIAS(mcuboot_led0) |
Andrzej Puzdrowski | 2f545b8 | 2022-05-26 14:28:07 +0200 | [diff] [blame] | 125 | #elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0)) |
| 126 | #warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead" |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 127 | #define LED0_NODE DT_ALIAS(bootloader_led0) |
Andrzej Puzdrowski | 2f545b8 | 2022-05-26 14:28:07 +0200 | [diff] [blame] | 128 | #endif |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 129 | |
| 130 | #if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios) |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 131 | static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios); |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 132 | #else |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 133 | /* A build error here means your board isn't set up to drive an LED. */ |
| 134 | #error "Unsupported board: led0 devicetree alias is not defined" |
| 135 | #endif |
| 136 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 137 | void led_init(void) |
| 138 | { |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 139 | |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 140 | if (!device_is_ready(led0.port)) { |
| 141 | BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n"); |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 145 | gpio_pin_configure_dt(&led0, GPIO_OUTPUT); |
| 146 | gpio_pin_set_dt(&led0, 0); |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 147 | |
| 148 | } |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 149 | #endif /* CONFIG_MCUBOOT_INDICATION_LED */ |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 150 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 151 | void os_heap_init(void); |
| 152 | |
| 153 | #if defined(CONFIG_ARM) |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 154 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 155 | #ifdef CONFIG_SW_VECTOR_RELAY |
| 156 | extern void *_vector_table_pointer; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 159 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 160 | uint32_t msp; |
| 161 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 164 | static void do_boot(struct boot_rsp *rsp) |
| 165 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 166 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 167 | uintptr_t flash_base; |
| 168 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 169 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 170 | /* The beginning of the image is the ARM vector table, containing |
| 171 | * the initial stack pointer address and the reset vector |
| 172 | * consecutively. Manually set the stack pointer and jump into the |
| 173 | * reset vector |
| 174 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 175 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 176 | assert(rc == 0); |
| 177 | |
| 178 | vt = (struct arm_vector_table *)(flash_base + |
| 179 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 180 | rsp->br_hdr->ih_hdr_size); |
Arvid Rosén | 9ed399c | 2020-08-19 08:57:11 +0200 | [diff] [blame] | 181 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 182 | sys_clock_disable(); |
Andrzej Puzdrowski | e9c6b4d | 2021-12-14 14:09:02 +0100 | [diff] [blame] | 183 | |
Johann Fischer | d2e87aa | 2021-08-27 13:30:07 +0200 | [diff] [blame] | 184 | #ifdef CONFIG_USB_DEVICE_STACK |
Emanuele Di Santo | c4bf780 | 2018-07-20 11:39:57 +0200 | [diff] [blame] | 185 | /* Disable the USB to prevent it from firing interrupts */ |
| 186 | usb_disable(); |
| 187 | #endif |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 188 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 189 | cleanup_arm_nvic(); /* cleanup NVIC registers */ |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 190 | |
Ryan McClelland | 179d8fa | 2022-05-20 23:53:35 -0700 | [diff] [blame] | 191 | #ifdef CONFIG_CPU_CORTEX_M_HAS_CACHE |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 192 | /* Disable instruction cache and data cache before chain-load the application */ |
| 193 | SCB_DisableDCache(); |
| 194 | SCB_DisableICache(); |
| 195 | #endif |
| 196 | |
Henrik Brix Andersen | 008f4a7 | 2020-12-08 14:40:19 +0100 | [diff] [blame] | 197 | #if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 198 | z_arm_clear_arm_mpu_config(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 199 | #endif |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 200 | |
Håkon Øye Amundsen | 6a8dbba | 2020-10-01 12:52:38 +0000 | [diff] [blame] | 201 | #if defined(CONFIG_BUILTIN_STACK_GUARD) && \ |
| 202 | defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM) |
| 203 | /* Reset limit registers to avoid inflicting stack overflow on image |
| 204 | * being booted. |
| 205 | */ |
| 206 | __set_PSPLIM(0); |
| 207 | __set_MSPLIM(0); |
| 208 | #endif |
| 209 | |
Sigvart Hovland | 9647c46 | 2021-08-20 16:33:55 +0200 | [diff] [blame] | 210 | #else |
| 211 | irq_lock(); |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 212 | #endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */ |
| 213 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_BOOT_INTR_VEC_RELOC |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 215 | #if defined(CONFIG_SW_VECTOR_RELAY) |
| 216 | _vector_table_pointer = vt; |
| 217 | #ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR |
| 218 | SCB->VTOR = (uint32_t)__vector_relay_table; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 219 | #endif |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 220 | #elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) |
| 221 | SCB->VTOR = (uint32_t)vt; |
| 222 | #endif /* CONFIG_SW_VECTOR_RELAY */ |
| 223 | #else /* CONFIG_BOOT_INTR_VEC_RELOC */ |
| 224 | #if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY) |
| 225 | _vector_table_pointer = _vector_start; |
| 226 | SCB->VTOR = (uint32_t)__vector_relay_table; |
| 227 | #endif |
| 228 | #endif /* CONFIG_BOOT_INTR_VEC_RELOC */ |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 229 | |
David Brown | 57837b9 | 2018-03-13 15:56:38 -0600 | [diff] [blame] | 230 | __set_MSP(vt->msp); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 231 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 232 | __set_CONTROL(0x00); /* application will configures core on its own */ |
Andrzej Puzdrowski | 56c15e7 | 2020-10-29 09:16:50 +0100 | [diff] [blame] | 233 | __ISB(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 234 | #endif |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 235 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 236 | } |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 237 | |
| 238 | #elif defined(CONFIG_XTENSA) |
| 239 | #define SRAM_BASE_ADDRESS 0xBE030000 |
| 240 | |
| 241 | static void copy_img_to_SRAM(int slot, unsigned int hdr_offset) |
| 242 | { |
| 243 | const struct flash_area *fap; |
| 244 | int area_id; |
| 245 | int rc; |
| 246 | unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset); |
| 247 | |
| 248 | BOOT_LOG_INF("Copying image to SRAM"); |
| 249 | |
| 250 | area_id = flash_area_id_from_image_slot(slot); |
| 251 | rc = flash_area_open(area_id, &fap); |
| 252 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 253 | BOOT_LOG_ERR("flash_area_open failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 254 | goto done; |
| 255 | } |
| 256 | |
| 257 | rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset); |
| 258 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 259 | BOOT_LOG_ERR("flash_area_read failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 260 | goto done; |
| 261 | } |
| 262 | |
| 263 | done: |
| 264 | flash_area_close(fap); |
| 265 | } |
| 266 | |
| 267 | /* Entry point (.ResetVector) is at the very beginning of the image. |
| 268 | * Simply copy the image to a suitable location and jump there. |
| 269 | */ |
| 270 | static void do_boot(struct boot_rsp *rsp) |
| 271 | { |
| 272 | void *start; |
| 273 | |
| 274 | BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off); |
| 275 | BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size); |
| 276 | |
| 277 | /* Copy from the flash to HP SRAM */ |
| 278 | copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size); |
| 279 | |
| 280 | /* Jump to entry point */ |
| 281 | start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size); |
| 282 | ((void (*)(void))start)(); |
| 283 | } |
| 284 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 285 | #else |
| 286 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 287 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 288 | * possibly other platforms. |
| 289 | */ |
| 290 | static void do_boot(struct boot_rsp *rsp) |
| 291 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 292 | void *start; |
Jim Tan | ee1b7b9 | 2022-04-07 11:26:28 +0800 | [diff] [blame] | 293 | |
| 294 | #if defined(MCUBOOT_RAM_LOAD) |
| 295 | start = (void *)(rsp->br_hdr->ih_load_addr + rsp->br_hdr->ih_hdr_size); |
| 296 | #else |
| 297 | uintptr_t flash_base; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 298 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 299 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 300 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 301 | assert(rc == 0); |
| 302 | |
| 303 | start = (void *)(flash_base + rsp->br_image_off + |
| 304 | rsp->br_hdr->ih_hdr_size); |
Jim Tan | ee1b7b9 | 2022-04-07 11:26:28 +0800 | [diff] [blame] | 305 | #endif |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 306 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 307 | /* Lock interrupts and dive into the entry point */ |
| 308 | irq_lock(); |
| 309 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 310 | } |
| 311 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 312 | |
Marek Pieta | fb47d2e | 2022-03-11 14:24:07 +0100 | [diff] [blame] | 313 | #if defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 314 | !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 315 | /* The log internal thread for log processing can't transfer log well as has too |
| 316 | * low priority. |
| 317 | * Dedicated thread for log processing below uses highest application |
| 318 | * priority. This allows to transmit all logs without adding k_sleep/k_yield |
| 319 | * anywhere else int the code. |
| 320 | */ |
| 321 | |
| 322 | /* most simple log processing theread */ |
| 323 | void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3) |
| 324 | { |
| 325 | (void)dummy1; |
| 326 | (void)dummy2; |
| 327 | (void)dummy3; |
| 328 | |
| 329 | log_init(); |
| 330 | |
| 331 | while (1) { |
| 332 | if (log_process(false) == false) { |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 333 | if (boot_log_stop) { |
| 334 | break; |
| 335 | } |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 336 | k_sleep(BOOT_LOG_PROCESSING_INTERVAL); |
| 337 | } |
| 338 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 339 | |
| 340 | k_sem_give(&boot_log_sem); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void zephyr_boot_log_start(void) |
| 344 | { |
| 345 | /* start logging thread */ |
| 346 | k_thread_create(&boot_log_thread, boot_log_stack, |
| 347 | K_THREAD_STACK_SIZEOF(boot_log_stack), |
| 348 | boot_log_thread_func, NULL, NULL, NULL, |
| 349 | K_HIGHEST_APPLICATION_THREAD_PRIO, 0, |
| 350 | BOOT_LOG_PROCESSING_INTERVAL); |
| 351 | |
| 352 | k_thread_name_set(&boot_log_thread, "logging"); |
| 353 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 354 | |
| 355 | void zephyr_boot_log_stop(void) |
| 356 | { |
| 357 | boot_log_stop = true; |
| 358 | |
| 359 | /* wait until log procesing thread expired |
| 360 | * This can be reworked using a thread_join() API once a such will be |
| 361 | * available in zephyr. |
| 362 | * see https://github.com/zephyrproject-rtos/zephyr/issues/21500 |
| 363 | */ |
| 364 | (void)k_sem_take(&boot_log_sem, K_FOREVER); |
| 365 | } |
Marek Pieta | fb47d2e | 2022-03-11 14:24:07 +0100 | [diff] [blame] | 366 | #endif /* defined(CONFIG_LOG) && !defined(ZEPHYR_LOG_MODE_IMMEDIATE) && \ |
| 367 | * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL) |
| 368 | */ |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 369 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 370 | #if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO) |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 371 | |
| 372 | #ifdef CONFIG_MCUBOOT_SERIAL |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 373 | #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY |
| 374 | #else |
| 375 | #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY |
| 376 | #endif |
| 377 | |
| 378 | |
| 379 | #define BUTTON_0_NODE DT_ALIAS(mcuboot_button0) |
| 380 | |
| 381 | #if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios) |
| 382 | |
| 383 | static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios); |
| 384 | |
| 385 | #else /* fallback to legacy configuration */ |
| 386 | |
| 387 | #if defined(CONFIG_MCUBOOT_SERIAL) |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 388 | |
| 389 | #define BUTTON_0_GPIO_LABEL CONFIG_BOOT_SERIAL_DETECT_PORT |
| 390 | #define BUTTON_0_GPIO_PIN CONFIG_BOOT_SERIAL_DETECT_PIN |
| 391 | #define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) ?\ |
| 392 | (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\ |
| 393 | (GPIO_ACTIVE_LOW | GPIO_PULL_UP)) |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 394 | |
| 395 | #elif defined(CONFIG_BOOT_USB_DFU_GPIO) |
| 396 | |
| 397 | #define BUTTON_0_GPIO_LABEL CONFIG_BOOT_USB_DFU_DETECT_PORT |
| 398 | #define BUTTON_0_GPIO_PIN CONFIG_BOOT_USB_DFU_DETECT_PIN |
| 399 | #define BUTTON_0_GPIO_FLAGS ((CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL) ?\ |
| 400 | (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) :\ |
| 401 | (GPIO_ACTIVE_LOW | GPIO_PULL_UP)) |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 402 | |
| 403 | #endif |
| 404 | |
| 405 | #define BUTTON_0_LEGACY 1 |
| 406 | |
| 407 | static struct gpio_dt_spec button0 = { |
| 408 | .port = NULL, |
| 409 | .pin = BUTTON_0_GPIO_PIN, |
| 410 | .dt_flags = BUTTON_0_GPIO_FLAGS |
| 411 | }; |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 412 | |
| 413 | #endif |
| 414 | |
| 415 | static bool detect_pin(void) |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 416 | { |
| 417 | int rc; |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 418 | int pin_active; |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 419 | |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 420 | #ifdef BUTTON_0_LEGACY |
| 421 | button0.port = device_get_binding(BUTTON_0_GPIO_LABEL); |
| 422 | if (button0.port == NULL) { |
| 423 | __ASSERT(false, "Error: Bad port for boot detection.\n"); |
| 424 | return false; |
| 425 | } |
| 426 | #else |
| 427 | if (!device_is_ready(button0.port)) { |
| 428 | __ASSERT(false, "GPIO device is not ready.\n"); |
| 429 | return false; |
| 430 | } |
| 431 | #endif |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 432 | |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 433 | rc = gpio_pin_configure_dt(&button0, GPIO_INPUT); |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 434 | __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n"); |
| 435 | |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 436 | rc = gpio_pin_get_dt(&button0); |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 437 | pin_active = rc; |
Andrzej Puzdrowski | 8bd3081 | 2021-03-22 08:19:41 +0100 | [diff] [blame] | 438 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 439 | __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 440 | |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 441 | if (pin_active) { |
| 442 | if (BUTTON_0_DETECT_DELAY > 0) { |
Andrzej Puzdrowski | 9b97456 | 2021-05-17 11:37:00 +0200 | [diff] [blame] | 443 | #ifdef CONFIG_MULTITHREADING |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 444 | k_sleep(K_MSEC(50)); |
Andrzej Puzdrowski | 9b97456 | 2021-05-17 11:37:00 +0200 | [diff] [blame] | 445 | #else |
| 446 | k_busy_wait(50000); |
| 447 | #endif |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 448 | |
| 449 | /* Get the uptime for debounce purposes. */ |
| 450 | int64_t timestamp = k_uptime_get(); |
| 451 | |
| 452 | for(;;) { |
Andrzej Puzdrowski | 7ed0e6d | 2022-04-09 13:25:35 +0200 | [diff] [blame] | 453 | rc = gpio_pin_get_dt(&button0); |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 454 | pin_active = rc; |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 455 | __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 Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 461 | if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) { |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | |
| 465 | /* Delay 1 ms */ |
Andrzej Puzdrowski | 9b97456 | 2021-05-17 11:37:00 +0200 | [diff] [blame] | 466 | #ifdef CONFIG_MULTITHREADING |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 467 | k_sleep(K_MSEC(1)); |
Andrzej Puzdrowski | 9b97456 | 2021-05-17 11:37:00 +0200 | [diff] [blame] | 468 | #else |
| 469 | k_busy_wait(1000); |
| 470 | #endif |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 475 | return (bool)pin_active; |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 476 | } |
| 477 | #endif |
| 478 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 479 | void main(void) |
| 480 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 481 | struct boot_rsp rsp; |
| 482 | int rc; |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 483 | fih_int fih_rc = FIH_FAILURE; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 484 | |
Andrzej Puzdrowski | 210b318 | 2020-10-13 13:52:15 +0200 | [diff] [blame] | 485 | MCUBOOT_WATCHDOG_FEED(); |
| 486 | |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 487 | #if !defined(MCUBOOT_DIRECT_XIP) |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 488 | BOOT_LOG_INF("Starting bootloader"); |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 489 | #else |
| 490 | BOOT_LOG_INF("Starting Direct-XIP bootloader"); |
| 491 | #endif |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 492 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 493 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 494 | /* LED init */ |
| 495 | led_init(); |
| 496 | #endif |
| 497 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 498 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 499 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 500 | ZEPHYR_BOOT_LOG_START(); |
| 501 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 502 | (void)rc; |
| 503 | |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 504 | mcuboot_status_change(MCUBOOT_STATUS_STARTUP); |
| 505 | |
Gerard Marull-Paretas | aa041a2 | 2022-03-25 12:22:29 +0100 | [diff] [blame] | 506 | #if (!defined(CONFIG_XTENSA) && DT_HAS_CHOSEN(zephyr_flash_controller)) |
| 507 | if (!flash_device_get_binding(DT_LABEL(DT_CHOSEN(zephyr_flash_controller)))) { |
Kumar Gala | 32b61f3 | 2020-04-08 12:02:03 -0500 | [diff] [blame] | 508 | BOOT_LOG_ERR("Flash device %s not found", |
Gerard Marull-Paretas | aa041a2 | 2022-03-25 12:22:29 +0100 | [diff] [blame] | 509 | DT_LABEL(DT_CHOSEN(zephyr_flash_controller))); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 510 | while (1) |
| 511 | ; |
| 512 | } |
Kumar Gala | 9a5b951 | 2020-04-08 12:06:21 -0500 | [diff] [blame] | 513 | #elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL)) |
| 514 | if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) { |
| 515 | BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 516 | while (1) |
| 517 | ; |
| 518 | } |
Rajavardhan Gundi | c3353b2 | 2018-12-06 17:24:10 +0530 | [diff] [blame] | 519 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 520 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 521 | #ifdef CONFIG_MCUBOOT_SERIAL |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 522 | if (detect_pin() && |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 523 | !boot_skip_serial_recovery()) { |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 524 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 525 | gpio_pin_set_dt(&led0, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 526 | #endif |
| 527 | |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 528 | mcuboot_status_change(MCUBOOT_STATUS_SERIAL_DFU_ENTERED); |
| 529 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 530 | BOOT_LOG_INF("Enter the serial recovery mode"); |
| 531 | rc = boot_console_init(); |
| 532 | __ASSERT(rc == 0, "Error initializing boot console.\n"); |
| 533 | boot_serial_start(&boot_funcs); |
| 534 | __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n"); |
| 535 | } |
| 536 | #endif |
| 537 | |
| 538 | #if defined(CONFIG_BOOT_USB_DFU_GPIO) |
Andrzej Puzdrowski | 2ab4987 | 2022-04-09 13:03:58 +0200 | [diff] [blame] | 539 | if (detect_pin()) { |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 540 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
Andrzej Puzdrowski | 5f317e4 | 2022-05-26 15:30:14 +0200 | [diff] [blame] | 541 | gpio_pin_set_dt(&led0, 1); |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 542 | #endif |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 543 | |
| 544 | mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED); |
| 545 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 546 | rc = usb_enable(NULL); |
| 547 | if (rc) { |
| 548 | BOOT_LOG_ERR("Cannot enable USB"); |
| 549 | } else { |
| 550 | BOOT_LOG_INF("Waiting for USB DFU"); |
| 551 | wait_for_usb_dfu(K_FOREVER); |
| 552 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
| 553 | } |
| 554 | } |
| 555 | #elif defined(CONFIG_BOOT_USB_DFU_WAIT) |
Andrzej Puzdrowski | ac1f1ff | 2020-02-05 08:40:12 +0100 | [diff] [blame] | 556 | rc = usb_enable(NULL); |
| 557 | if (rc) { |
| 558 | BOOT_LOG_ERR("Cannot enable USB"); |
| 559 | } else { |
| 560 | BOOT_LOG_INF("Waiting for USB DFU"); |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 561 | |
| 562 | mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING); |
| 563 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 564 | wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS)); |
Andrzej Puzdrowski | ac1f1ff | 2020-02-05 08:40:12 +0100 | [diff] [blame] | 565 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 566 | |
| 567 | mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT); |
Andrzej Puzdrowski | ac1f1ff | 2020-02-05 08:40:12 +0100 | [diff] [blame] | 568 | } |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 569 | #endif |
| 570 | |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 571 | #ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU |
| 572 | /* Initialize the boot console, so we can already fill up our buffers while |
| 573 | * waiting for the boot image check to finish. This image check, can take |
| 574 | * some time, so it's better to reuse thistime to already receive the |
| 575 | * initial mcumgr command(s) into our buffers |
| 576 | */ |
| 577 | rc = boot_console_init(); |
| 578 | int timeout_in_ms = CONFIG_BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT; |
| 579 | uint32_t start = k_uptime_get_32(); |
| 580 | #endif |
| 581 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 582 | FIH_CALL(boot_go, fih_rc, &rsp); |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 583 | |
| 584 | #ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU |
| 585 | timeout_in_ms -= (k_uptime_get_32() - start); |
| 586 | if( timeout_in_ms <= 0 ) { |
| 587 | /* at least one check if time was expired */ |
| 588 | timeout_in_ms = 1; |
| 589 | } |
Jamie McCrae | 254714b | 2022-04-07 09:00:31 +0100 | [diff] [blame] | 590 | boot_serial_check_start(&boot_funcs,timeout_in_ms); |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 591 | #endif |
| 592 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 593 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 594 | BOOT_LOG_ERR("Unable to find bootable image"); |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 595 | |
| 596 | mcuboot_status_change(MCUBOOT_STATUS_NO_BOOTABLE_IMAGE_FOUND); |
| 597 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 598 | FIH_PANIC; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 599 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 600 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 601 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 602 | rsp.br_image_off); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 603 | |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 604 | #if defined(MCUBOOT_DIRECT_XIP) |
| 605 | BOOT_LOG_INF("Jumping to the image slot"); |
| 606 | #else |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 607 | BOOT_LOG_INF("Jumping to the first image slot"); |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 608 | #endif |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 609 | |
| 610 | mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND); |
| 611 | |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 612 | ZEPHYR_BOOT_LOG_STOP(); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 613 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 614 | |
Jamie McCrae | 56cb610 | 2022-03-23 11:57:03 +0000 | [diff] [blame] | 615 | mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED); |
| 616 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 617 | BOOT_LOG_ERR("Never should get here"); |
| 618 | while (1) |
| 619 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 620 | } |