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> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 20 | #include <zephyr.h> |
Peter Bigot | 54c1e3f | 2020-01-25 05:50:12 -0600 | [diff] [blame] | 21 | #include <drivers/gpio.h> |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 22 | #include <sys/__assert.h> |
Peter Bigot | 54c1e3f | 2020-01-25 05:50:12 -0600 | [diff] [blame] | 23 | #include <drivers/flash.h> |
Andrzej Puzdrowski | f99a4c7 | 2019-06-28 15:16:35 +0200 | [diff] [blame] | 24 | #include <drivers/timer/system_timer.h> |
Emanuele Di Santo | c4bf780 | 2018-07-20 11:39:57 +0200 | [diff] [blame] | 25 | #include <usb/usb_device.h> |
Evan Gates | 4632d8d | 2018-06-28 13:27:40 -0700 | [diff] [blame] | 26 | #include <soc.h> |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 27 | #include <linker/linker-defs.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 28 | |
Marti Bolivar | 51181cf | 2017-03-20 11:03:41 -0400 | [diff] [blame] | 29 | #include "target.h" |
| 30 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame] | 31 | #include "bootutil/bootutil_log.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 32 | #include "bootutil/image.h" |
| 33 | #include "bootutil/bootutil.h" |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 34 | #include "bootutil/fault_injection_hardening.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 35 | #include "flash_map_backend/flash_map_backend.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 36 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 37 | #ifdef CONFIG_MCUBOOT_SERIAL |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 38 | #include "boot_serial/boot_serial.h" |
| 39 | #include "serial_adapter/serial_adapter.h" |
| 40 | |
| 41 | const struct boot_uart_funcs boot_funcs = { |
| 42 | .read = console_read, |
| 43 | .write = console_write |
| 44 | }; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 47 | #if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO) |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 48 | #include <usb/class/usb_dfu.h> |
| 49 | #endif |
| 50 | |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 51 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 52 | #include <arm_cleanup.h> |
| 53 | #endif |
| 54 | |
Gerard Marull-Paretas | a513b8e | 2021-01-26 22:50:38 +0100 | [diff] [blame] | 55 | /* CONFIG_LOG_MINIMAL is the legacy Kconfig property, |
| 56 | * replaced by CONFIG_LOG_MODE_MINIMAL. |
| 57 | */ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 58 | #if (defined(CONFIG_LOG_MODE_MINIMAL) || defined(CONFIG_LOG_MINIMAL)) |
| 59 | #define ZEPHYR_LOG_MODE_MINIMAL 1 |
| 60 | #endif |
Gerard Marull-Paretas | a513b8e | 2021-01-26 22:50:38 +0100 | [diff] [blame] | 61 | |
Henrik Brix Andersen | e512181 | 2021-01-12 09:52:51 +0100 | [diff] [blame] | 62 | #if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) && \ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 63 | !defined(ZEPHYR_LOG_MODE_MINIMAL) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 64 | #ifdef CONFIG_LOG_PROCESS_THREAD |
| 65 | #warning "The log internal thread for log processing can't transfer the log"\ |
| 66 | "well for MCUBoot." |
| 67 | #else |
| 68 | #include <logging/log_ctrl.h> |
| 69 | |
Krzysztof Chruscinski | 821214e | 2020-03-24 07:50:32 +0100 | [diff] [blame] | 70 | #define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */ |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 71 | |
| 72 | /* log are processing in custom routine */ |
Andrzej Puzdrowski | af14853 | 2020-02-25 12:51:26 +0100 | [diff] [blame] | 73 | 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] | 74 | struct k_thread boot_log_thread; |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 75 | volatile bool boot_log_stop = false; |
| 76 | K_SEM_DEFINE(boot_log_sem, 1, 1); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 77 | |
| 78 | /* log processing need to be initalized by the application */ |
| 79 | #define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start() |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 80 | #define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop() |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 81 | #endif /* CONFIG_LOG_PROCESS_THREAD */ |
| 82 | #else |
| 83 | /* synchronous log mode doesn't need to be initalized by the application */ |
| 84 | #define ZEPHYR_BOOT_LOG_START() do { } while (false) |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 85 | #define ZEPHYR_BOOT_LOG_STOP() do { } while (false) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 86 | #endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */ |
| 87 | |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 88 | #ifdef CONFIG_SOC_FAMILY_NRF |
| 89 | #include <hal/nrf_power.h> |
| 90 | |
| 91 | static inline bool boot_skip_serial_recovery() |
| 92 | { |
Fabio Utzig | 63b4eac | 2019-12-17 09:59:47 -0300 | [diff] [blame] | 93 | #if NRF_POWER_HAS_RESETREAS |
Kumar Gala | 0813efe | 2020-05-27 12:25:41 -0500 | [diff] [blame] | 94 | uint32_t rr = nrf_power_resetreas_get(NRF_POWER); |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 95 | |
| 96 | return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK)); |
| 97 | #else |
| 98 | return false; |
| 99 | #endif |
| 100 | } |
| 101 | #else |
| 102 | static inline bool boot_skip_serial_recovery() |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | #endif |
| 107 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 108 | MCUBOOT_LOG_MODULE_REGISTER(mcuboot); |
| 109 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 110 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 111 | /* |
| 112 | * Devicetree helper macro which gets the 'flags' cell from a 'gpios' |
| 113 | * property, or returns 0 if the property has no 'flags' cell. |
| 114 | */ |
| 115 | #define FLAGS_OR_ZERO(node) \ |
| 116 | COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \ |
| 117 | (DT_GPIO_FLAGS(node, gpios)), \ |
| 118 | (0)) |
| 119 | |
| 120 | /* |
| 121 | * The led0 devicetree alias is optional. If present, we'll use it |
| 122 | * to turn on the LED whenever the button is pressed. |
| 123 | */ |
| 124 | |
| 125 | #define LED0_NODE DT_ALIAS(bootloader_led0) |
| 126 | |
| 127 | #if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios) |
| 128 | #define LED0_GPIO_LABEL DT_GPIO_LABEL(LED0_NODE, gpios) |
| 129 | #define LED0_GPIO_PIN DT_GPIO_PIN(LED0_NODE, gpios) |
| 130 | #define LED0_GPIO_FLAGS (GPIO_OUTPUT | FLAGS_OR_ZERO(LED0_NODE)) |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 131 | #else |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 132 | /* A build error here means your board isn't set up to drive an LED. */ |
| 133 | #error "Unsupported board: led0 devicetree alias is not defined" |
| 134 | #endif |
| 135 | |
| 136 | const static struct device *led; |
| 137 | |
| 138 | void led_init(void) |
| 139 | { |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 140 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 141 | led = device_get_binding(LED0_GPIO_LABEL); |
Jared Wolff | df8e974 | 2021-02-04 11:17:00 -0500 | [diff] [blame] | 142 | if (led == NULL) { |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 143 | BOOT_LOG_ERR("Didn't find LED device %s\n", LED0_GPIO_LABEL); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | gpio_pin_configure(led, LED0_GPIO_PIN, LED0_GPIO_FLAGS); |
Jared Wolff | df8e974 | 2021-02-04 11:17:00 -0500 | [diff] [blame] | 148 | gpio_pin_set(led, LED0_GPIO_PIN, 0); |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 149 | |
| 150 | } |
| 151 | #endif |
| 152 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 153 | void os_heap_init(void); |
| 154 | |
| 155 | #if defined(CONFIG_ARM) |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 156 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_SW_VECTOR_RELAY |
| 158 | extern void *_vector_table_pointer; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 159 | #endif |
| 160 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 161 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 162 | uint32_t msp; |
| 163 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Emanuele Di Santo | fcfff58 | 2018-11-01 16:06:36 +0100 | [diff] [blame] | 166 | extern void sys_clock_disable(void); |
| 167 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 168 | static void do_boot(struct boot_rsp *rsp) |
| 169 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 170 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 171 | uintptr_t flash_base; |
| 172 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 173 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 174 | /* The beginning of the image is the ARM vector table, containing |
| 175 | * the initial stack pointer address and the reset vector |
| 176 | * consecutively. Manually set the stack pointer and jump into the |
| 177 | * reset vector |
| 178 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 179 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 180 | assert(rc == 0); |
| 181 | |
| 182 | vt = (struct arm_vector_table *)(flash_base + |
| 183 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 184 | rsp->br_hdr->ih_hdr_size); |
Arvid Rosén | 9ed399c | 2020-08-19 08:57:11 +0200 | [diff] [blame] | 185 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 186 | irq_lock(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 188 | sys_clock_disable(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame] | 189 | #endif |
Jun Li | 01bef71 | 2019-05-08 21:55:54 -0700 | [diff] [blame] | 190 | #ifdef CONFIG_USB |
Emanuele Di Santo | c4bf780 | 2018-07-20 11:39:57 +0200 | [diff] [blame] | 191 | /* Disable the USB to prevent it from firing interrupts */ |
| 192 | usb_disable(); |
| 193 | #endif |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 194 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 195 | cleanup_arm_nvic(); /* cleanup NVIC registers */ |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 196 | |
| 197 | #ifdef CONFIG_CPU_CORTEX_M7 |
| 198 | /* Disable instruction cache and data cache before chain-load the application */ |
| 199 | SCB_DisableDCache(); |
| 200 | SCB_DisableICache(); |
| 201 | #endif |
| 202 | |
Henrik Brix Andersen | 008f4a7 | 2020-12-08 14:40:19 +0100 | [diff] [blame] | 203 | #if CONFIG_CPU_HAS_ARM_MPU || CONFIG_CPU_HAS_NXP_MPU |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 204 | z_arm_clear_arm_mpu_config(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 205 | #endif |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 206 | |
Håkon Øye Amundsen | 6a8dbba | 2020-10-01 12:52:38 +0000 | [diff] [blame] | 207 | #if defined(CONFIG_BUILTIN_STACK_GUARD) && \ |
| 208 | defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM) |
| 209 | /* Reset limit registers to avoid inflicting stack overflow on image |
| 210 | * being booted. |
| 211 | */ |
| 212 | __set_PSPLIM(0); |
| 213 | __set_MSPLIM(0); |
| 214 | #endif |
| 215 | |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 216 | #endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */ |
| 217 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 218 | #ifdef CONFIG_BOOT_INTR_VEC_RELOC |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 219 | #if defined(CONFIG_SW_VECTOR_RELAY) |
| 220 | _vector_table_pointer = vt; |
| 221 | #ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR |
| 222 | SCB->VTOR = (uint32_t)__vector_relay_table; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 223 | #endif |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 224 | #elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) |
| 225 | SCB->VTOR = (uint32_t)vt; |
| 226 | #endif /* CONFIG_SW_VECTOR_RELAY */ |
| 227 | #else /* CONFIG_BOOT_INTR_VEC_RELOC */ |
| 228 | #if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY) |
| 229 | _vector_table_pointer = _vector_start; |
| 230 | SCB->VTOR = (uint32_t)__vector_relay_table; |
| 231 | #endif |
| 232 | #endif /* CONFIG_BOOT_INTR_VEC_RELOC */ |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 233 | |
David Brown | 57837b9 | 2018-03-13 15:56:38 -0600 | [diff] [blame] | 234 | __set_MSP(vt->msp); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 235 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 236 | __set_CONTROL(0x00); /* application will configures core on its own */ |
Andrzej Puzdrowski | 56c15e7 | 2020-10-29 09:16:50 +0100 | [diff] [blame] | 237 | __ISB(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 238 | #endif |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 239 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 240 | } |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 241 | |
| 242 | #elif defined(CONFIG_XTENSA) |
| 243 | #define SRAM_BASE_ADDRESS 0xBE030000 |
| 244 | |
| 245 | static void copy_img_to_SRAM(int slot, unsigned int hdr_offset) |
| 246 | { |
| 247 | const struct flash_area *fap; |
| 248 | int area_id; |
| 249 | int rc; |
| 250 | unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset); |
| 251 | |
| 252 | BOOT_LOG_INF("Copying image to SRAM"); |
| 253 | |
| 254 | area_id = flash_area_id_from_image_slot(slot); |
| 255 | rc = flash_area_open(area_id, &fap); |
| 256 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 257 | BOOT_LOG_ERR("flash_area_open failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 258 | goto done; |
| 259 | } |
| 260 | |
| 261 | rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset); |
| 262 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 263 | BOOT_LOG_ERR("flash_area_read failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 264 | goto done; |
| 265 | } |
| 266 | |
| 267 | done: |
| 268 | flash_area_close(fap); |
| 269 | } |
| 270 | |
| 271 | /* Entry point (.ResetVector) is at the very beginning of the image. |
| 272 | * Simply copy the image to a suitable location and jump there. |
| 273 | */ |
| 274 | static void do_boot(struct boot_rsp *rsp) |
| 275 | { |
| 276 | void *start; |
| 277 | |
| 278 | BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off); |
| 279 | BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size); |
| 280 | |
| 281 | /* 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)(); |
| 287 | } |
| 288 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 289 | #else |
| 290 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 291 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 292 | * possibly other platforms. |
| 293 | */ |
| 294 | static void do_boot(struct boot_rsp *rsp) |
| 295 | { |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 296 | uintptr_t flash_base; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 297 | void *start; |
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); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 305 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 306 | /* Lock interrupts and dive into the entry point */ |
| 307 | irq_lock(); |
| 308 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 309 | } |
| 310 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 311 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 312 | #if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\ |
Dominik Ermel | 5b7ed6a | 2021-02-26 14:26:48 +0000 | [diff] [blame] | 313 | !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 314 | /* The log internal thread for log processing can't transfer log well as has too |
| 315 | * low priority. |
| 316 | * Dedicated thread for log processing below uses highest application |
| 317 | * priority. This allows to transmit all logs without adding k_sleep/k_yield |
| 318 | * anywhere else int the code. |
| 319 | */ |
| 320 | |
| 321 | /* most simple log processing theread */ |
| 322 | void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3) |
| 323 | { |
| 324 | (void)dummy1; |
| 325 | (void)dummy2; |
| 326 | (void)dummy3; |
| 327 | |
| 328 | log_init(); |
| 329 | |
| 330 | while (1) { |
| 331 | if (log_process(false) == false) { |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 332 | if (boot_log_stop) { |
| 333 | break; |
| 334 | } |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 335 | k_sleep(BOOT_LOG_PROCESSING_INTERVAL); |
| 336 | } |
| 337 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 338 | |
| 339 | k_sem_give(&boot_log_sem); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void zephyr_boot_log_start(void) |
| 343 | { |
| 344 | /* start logging thread */ |
| 345 | k_thread_create(&boot_log_thread, boot_log_stack, |
| 346 | K_THREAD_STACK_SIZEOF(boot_log_stack), |
| 347 | boot_log_thread_func, NULL, NULL, NULL, |
| 348 | K_HIGHEST_APPLICATION_THREAD_PRIO, 0, |
| 349 | BOOT_LOG_PROCESSING_INTERVAL); |
| 350 | |
| 351 | k_thread_name_set(&boot_log_thread, "logging"); |
| 352 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 353 | |
| 354 | void zephyr_boot_log_stop(void) |
| 355 | { |
| 356 | boot_log_stop = true; |
| 357 | |
| 358 | /* wait until log procesing thread expired |
| 359 | * This can be reworked using a thread_join() API once a such will be |
| 360 | * available in zephyr. |
| 361 | * see https://github.com/zephyrproject-rtos/zephyr/issues/21500 |
| 362 | */ |
| 363 | (void)k_sem_take(&boot_log_sem, K_FOREVER); |
| 364 | } |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 365 | #endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\ |
| 366 | !defined(CONFIG_LOG_PROCESS_THREAD) */ |
| 367 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 368 | #if defined(CONFIG_MCUBOOT_SERIAL) || defined(CONFIG_BOOT_USB_DFU_GPIO) |
| 369 | static bool detect_pin(const char* port, int pin, uint32_t expected, int delay) |
| 370 | { |
| 371 | int rc; |
| 372 | int detect_value; |
| 373 | struct device const *detect_port; |
| 374 | |
| 375 | detect_port = device_get_binding(port); |
| 376 | __ASSERT(detect_port, "Error: Bad port for boot detection.\n"); |
| 377 | |
| 378 | /* The default presence value is 0 which would normally be |
| 379 | * active-low, but historically the raw value was checked so we'll |
| 380 | * use the raw interface. |
| 381 | */ |
| 382 | rc = gpio_pin_configure(detect_port, pin, |
Andrzej Puzdrowski | 8bd3081 | 2021-03-22 08:19:41 +0100 | [diff] [blame] | 383 | GPIO_INPUT | GPIO_PULL_UP); |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 384 | __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n"); |
| 385 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 386 | rc = gpio_pin_get_raw(detect_port, pin); |
| 387 | detect_value = rc; |
Andrzej Puzdrowski | 8bd3081 | 2021-03-22 08:19:41 +0100 | [diff] [blame] | 388 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 389 | __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 390 | |
| 391 | if (detect_value == expected) { |
| 392 | if (delay > 0) { |
| 393 | k_sleep(K_MSEC(50)); |
| 394 | |
| 395 | /* Get the uptime for debounce purposes. */ |
| 396 | int64_t timestamp = k_uptime_get(); |
| 397 | |
| 398 | for(;;) { |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 399 | rc = gpio_pin_get_raw(detect_port, pin); |
| 400 | detect_value = rc; |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 401 | __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 402 | |
| 403 | /* Get delta from when this started */ |
| 404 | uint32_t delta = k_uptime_get() - timestamp; |
| 405 | |
| 406 | /* If not pressed OR if pressed > debounce period, stop. */ |
| 407 | if (delta >= delay || detect_value != expected) { |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | /* Delay 1 ms */ |
| 412 | k_sleep(K_MSEC(1)); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return detect_value == expected; |
| 418 | } |
| 419 | #endif |
| 420 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 421 | void main(void) |
| 422 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 423 | struct boot_rsp rsp; |
| 424 | int rc; |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 425 | fih_int fih_rc = FIH_FAILURE; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 426 | |
Andrzej Puzdrowski | 210b318 | 2020-10-13 13:52:15 +0200 | [diff] [blame] | 427 | MCUBOOT_WATCHDOG_FEED(); |
| 428 | |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 429 | #if !defined(MCUBOOT_DIRECT_XIP) |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 430 | BOOT_LOG_INF("Starting bootloader"); |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 431 | #else |
| 432 | BOOT_LOG_INF("Starting Direct-XIP bootloader"); |
| 433 | #endif |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 434 | |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 435 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 436 | /* LED init */ |
| 437 | led_init(); |
| 438 | #endif |
| 439 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 440 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 441 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 442 | ZEPHYR_BOOT_LOG_START(); |
| 443 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 444 | (void)rc; |
| 445 | |
Kumar Gala | 32b61f3 | 2020-04-08 12:02:03 -0500 | [diff] [blame] | 446 | #if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) |
| 447 | if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) { |
| 448 | BOOT_LOG_ERR("Flash device %s not found", |
| 449 | DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 450 | while (1) |
| 451 | ; |
| 452 | } |
Kumar Gala | 9a5b951 | 2020-04-08 12:06:21 -0500 | [diff] [blame] | 453 | #elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL)) |
| 454 | if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) { |
| 455 | 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] | 456 | while (1) |
| 457 | ; |
| 458 | } |
Rajavardhan Gundi | c3353b2 | 2018-12-06 17:24:10 +0530 | [diff] [blame] | 459 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 460 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 461 | #ifdef CONFIG_MCUBOOT_SERIAL |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 462 | if (detect_pin(CONFIG_BOOT_SERIAL_DETECT_PORT, |
| 463 | CONFIG_BOOT_SERIAL_DETECT_PIN, |
| 464 | CONFIG_BOOT_SERIAL_DETECT_PIN_VAL, |
| 465 | CONFIG_BOOT_SERIAL_DETECT_DELAY) && |
| 466 | !boot_skip_serial_recovery()) { |
Jared Wolff | 8e4d791 | 2021-01-21 19:34:05 -0500 | [diff] [blame] | 467 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 468 | gpio_pin_set(led, LED0_GPIO_PIN, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 469 | #endif |
| 470 | |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 471 | BOOT_LOG_INF("Enter the serial recovery mode"); |
| 472 | rc = boot_console_init(); |
| 473 | __ASSERT(rc == 0, "Error initializing boot console.\n"); |
| 474 | boot_serial_start(&boot_funcs); |
| 475 | __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n"); |
| 476 | } |
| 477 | #endif |
| 478 | |
| 479 | #if defined(CONFIG_BOOT_USB_DFU_GPIO) |
| 480 | if (detect_pin(CONFIG_BOOT_USB_DFU_DETECT_PORT, |
| 481 | CONFIG_BOOT_USB_DFU_DETECT_PIN, |
| 482 | CONFIG_BOOT_USB_DFU_DETECT_PIN_VAL, |
| 483 | CONFIG_BOOT_USB_DFU_DETECT_DELAY)) { |
| 484 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 485 | gpio_pin_set(led, LED0_GPIO_PIN, 1); |
| 486 | #endif |
| 487 | rc = usb_enable(NULL); |
| 488 | if (rc) { |
| 489 | BOOT_LOG_ERR("Cannot enable USB"); |
| 490 | } else { |
| 491 | BOOT_LOG_INF("Waiting for USB DFU"); |
| 492 | wait_for_usb_dfu(K_FOREVER); |
| 493 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
| 494 | } |
| 495 | } |
| 496 | #elif defined(CONFIG_BOOT_USB_DFU_WAIT) |
Andrzej Puzdrowski | ac1f1ff | 2020-02-05 08:40:12 +0100 | [diff] [blame] | 497 | rc = usb_enable(NULL); |
| 498 | if (rc) { |
| 499 | BOOT_LOG_ERR("Cannot enable USB"); |
| 500 | } else { |
| 501 | BOOT_LOG_INF("Waiting for USB DFU"); |
Josh Gao | 837cf88 | 2020-11-13 18:51:27 -0800 | [diff] [blame] | 502 | 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] | 503 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
| 504 | } |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 505 | #endif |
| 506 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 507 | FIH_CALL(boot_go, fih_rc, &rsp); |
| 508 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 509 | BOOT_LOG_ERR("Unable to find bootable image"); |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 510 | FIH_PANIC; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 511 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 512 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 513 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 514 | rsp.br_image_off); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 515 | |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 516 | #if defined(MCUBOOT_DIRECT_XIP) |
| 517 | BOOT_LOG_INF("Jumping to the image slot"); |
| 518 | #else |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 519 | BOOT_LOG_INF("Jumping to the first image slot"); |
Dominik Ermel | cd07ed3 | 2021-02-17 15:18:01 +0000 | [diff] [blame] | 520 | #endif |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 521 | ZEPHYR_BOOT_LOG_STOP(); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 522 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 523 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 524 | BOOT_LOG_ERR("Never should get here"); |
| 525 | while (1) |
| 526 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 527 | } |