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