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