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 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 54 | #if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) |
| 55 | #ifdef CONFIG_LOG_PROCESS_THREAD |
| 56 | #warning "The log internal thread for log processing can't transfer the log"\ |
| 57 | "well for MCUBoot." |
| 58 | #else |
| 59 | #include <logging/log_ctrl.h> |
| 60 | |
Krzysztof Chruscinski | 821214e | 2020-03-24 07:50:32 +0100 | [diff] [blame] | 61 | #define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */ |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 62 | |
| 63 | /* log are processing in custom routine */ |
Andrzej Puzdrowski | af14853 | 2020-02-25 12:51:26 +0100 | [diff] [blame] | 64 | 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] | 65 | struct k_thread boot_log_thread; |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 66 | volatile bool boot_log_stop = false; |
| 67 | K_SEM_DEFINE(boot_log_sem, 1, 1); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 68 | |
| 69 | /* log processing need to be initalized by the application */ |
| 70 | #define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start() |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 71 | #define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop() |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 72 | #endif /* CONFIG_LOG_PROCESS_THREAD */ |
| 73 | #else |
| 74 | /* synchronous log mode doesn't need to be initalized by the application */ |
| 75 | #define ZEPHYR_BOOT_LOG_START() do { } while (false) |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 76 | #define ZEPHYR_BOOT_LOG_STOP() do { } while (false) |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 77 | #endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */ |
| 78 | |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_SOC_FAMILY_NRF |
| 80 | #include <hal/nrf_power.h> |
| 81 | |
| 82 | static inline bool boot_skip_serial_recovery() |
| 83 | { |
Fabio Utzig | 63b4eac | 2019-12-17 09:59:47 -0300 | [diff] [blame] | 84 | #if NRF_POWER_HAS_RESETREAS |
Kumar Gala | 0813efe | 2020-05-27 12:25:41 -0500 | [diff] [blame] | 85 | uint32_t rr = nrf_power_resetreas_get(NRF_POWER); |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 86 | |
| 87 | return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK)); |
| 88 | #else |
| 89 | return false; |
| 90 | #endif |
| 91 | } |
| 92 | #else |
| 93 | static inline bool boot_skip_serial_recovery() |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | #endif |
| 98 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 99 | MCUBOOT_LOG_MODULE_REGISTER(mcuboot); |
| 100 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 101 | void os_heap_init(void); |
| 102 | |
| 103 | #if defined(CONFIG_ARM) |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 104 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_SW_VECTOR_RELAY |
| 106 | extern void *_vector_table_pointer; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 107 | #endif |
| 108 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 109 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 110 | uint32_t msp; |
| 111 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Emanuele Di Santo | fcfff58 | 2018-11-01 16:06:36 +0100 | [diff] [blame] | 114 | extern void sys_clock_disable(void); |
| 115 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 116 | static void do_boot(struct boot_rsp *rsp) |
| 117 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 118 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 119 | uintptr_t flash_base; |
| 120 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 121 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 122 | /* The beginning of the image is the ARM vector table, containing |
| 123 | * the initial stack pointer address and the reset vector |
| 124 | * consecutively. Manually set the stack pointer and jump into the |
| 125 | * reset vector |
| 126 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 127 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 128 | assert(rc == 0); |
| 129 | |
| 130 | vt = (struct arm_vector_table *)(flash_base + |
| 131 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 132 | rsp->br_hdr->ih_hdr_size); |
Arvid Rosén | 9ed399c | 2020-08-19 08:57:11 +0200 | [diff] [blame] | 133 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 134 | irq_lock(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 136 | sys_clock_disable(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame] | 137 | #endif |
Jun Li | 01bef71 | 2019-05-08 21:55:54 -0700 | [diff] [blame] | 138 | #ifdef CONFIG_USB |
Emanuele Di Santo | c4bf780 | 2018-07-20 11:39:57 +0200 | [diff] [blame] | 139 | /* Disable the USB to prevent it from firing interrupts */ |
| 140 | usb_disable(); |
| 141 | #endif |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 142 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 143 | cleanup_arm_nvic(); /* cleanup NVIC registers */ |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 144 | |
| 145 | #ifdef CONFIG_CPU_CORTEX_M7 |
| 146 | /* Disable instruction cache and data cache before chain-load the application */ |
| 147 | SCB_DisableDCache(); |
| 148 | SCB_DisableICache(); |
| 149 | #endif |
| 150 | |
| 151 | #if CONFIG_CPU_HAS_ARM_MPU |
| 152 | z_arm_clear_arm_mpu_config(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 153 | #endif |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 154 | |
Håkon Øye Amundsen | 6a8dbba | 2020-10-01 12:52:38 +0000 | [diff] [blame] | 155 | #if defined(CONFIG_BUILTIN_STACK_GUARD) && \ |
| 156 | defined(CONFIG_CPU_CORTEX_M_HAS_SPLIM) |
| 157 | /* Reset limit registers to avoid inflicting stack overflow on image |
| 158 | * being booted. |
| 159 | */ |
| 160 | __set_PSPLIM(0); |
| 161 | __set_MSPLIM(0); |
| 162 | #endif |
| 163 | |
Ioannis Glaropoulos | 70af708 | 2020-10-22 15:14:48 +0200 | [diff] [blame] | 164 | #endif /* CONFIG_MCUBOOT_CLEANUP_ARM_CORE */ |
| 165 | |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_BOOT_INTR_VEC_RELOC |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 167 | #if defined(CONFIG_SW_VECTOR_RELAY) |
| 168 | _vector_table_pointer = vt; |
| 169 | #ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR |
| 170 | SCB->VTOR = (uint32_t)__vector_relay_table; |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 171 | #endif |
Rafał Kuźnia | 505c6e6 | 2020-07-17 13:18:15 +0200 | [diff] [blame] | 172 | #elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) |
| 173 | SCB->VTOR = (uint32_t)vt; |
| 174 | #endif /* CONFIG_SW_VECTOR_RELAY */ |
| 175 | #else /* CONFIG_BOOT_INTR_VEC_RELOC */ |
| 176 | #if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY) |
| 177 | _vector_table_pointer = _vector_start; |
| 178 | SCB->VTOR = (uint32_t)__vector_relay_table; |
| 179 | #endif |
| 180 | #endif /* CONFIG_BOOT_INTR_VEC_RELOC */ |
Rafał Kuźnia | d854bb6 | 2020-06-17 15:06:47 +0200 | [diff] [blame] | 181 | |
David Brown | 57837b9 | 2018-03-13 15:56:38 -0600 | [diff] [blame] | 182 | __set_MSP(vt->msp); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 183 | #if CONFIG_MCUBOOT_CLEANUP_ARM_CORE |
| 184 | __set_CONTROL(0x00); /* application will configures core on its own */ |
Andrzej Puzdrowski | 56c15e7 | 2020-10-29 09:16:50 +0100 | [diff] [blame^] | 185 | __ISB(); |
Andrzej Puzdrowski | 9a605b6 | 2020-03-16 13:34:30 +0100 | [diff] [blame] | 186 | #endif |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 187 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 188 | } |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 189 | |
| 190 | #elif defined(CONFIG_XTENSA) |
| 191 | #define SRAM_BASE_ADDRESS 0xBE030000 |
| 192 | |
| 193 | static void copy_img_to_SRAM(int slot, unsigned int hdr_offset) |
| 194 | { |
| 195 | const struct flash_area *fap; |
| 196 | int area_id; |
| 197 | int rc; |
| 198 | unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset); |
| 199 | |
| 200 | BOOT_LOG_INF("Copying image to SRAM"); |
| 201 | |
| 202 | area_id = flash_area_id_from_image_slot(slot); |
| 203 | rc = flash_area_open(area_id, &fap); |
| 204 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 205 | BOOT_LOG_ERR("flash_area_open failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 206 | goto done; |
| 207 | } |
| 208 | |
| 209 | rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset); |
| 210 | if (rc != 0) { |
Fabio Utzig | cac58f6 | 2019-09-06 08:52:35 -0300 | [diff] [blame] | 211 | BOOT_LOG_ERR("flash_area_read failed with %d\n", rc); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 212 | goto done; |
| 213 | } |
| 214 | |
| 215 | done: |
| 216 | flash_area_close(fap); |
| 217 | } |
| 218 | |
| 219 | /* Entry point (.ResetVector) is at the very beginning of the image. |
| 220 | * Simply copy the image to a suitable location and jump there. |
| 221 | */ |
| 222 | static void do_boot(struct boot_rsp *rsp) |
| 223 | { |
| 224 | void *start; |
| 225 | |
| 226 | BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off); |
| 227 | BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size); |
| 228 | |
| 229 | /* Copy from the flash to HP SRAM */ |
| 230 | copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size); |
| 231 | |
| 232 | /* Jump to entry point */ |
| 233 | start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size); |
| 234 | ((void (*)(void))start)(); |
| 235 | } |
| 236 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 237 | #else |
| 238 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 239 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 240 | * possibly other platforms. |
| 241 | */ |
| 242 | static void do_boot(struct boot_rsp *rsp) |
| 243 | { |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 244 | uintptr_t flash_base; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 245 | void *start; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 246 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 247 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 248 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 249 | assert(rc == 0); |
| 250 | |
| 251 | start = (void *)(flash_base + rsp->br_image_off + |
| 252 | rsp->br_hdr->ih_hdr_size); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 253 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 254 | /* Lock interrupts and dive into the entry point */ |
| 255 | irq_lock(); |
| 256 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 257 | } |
| 258 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 259 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 260 | #if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\ |
| 261 | !defined(CONFIG_LOG_PROCESS_THREAD) |
| 262 | /* The log internal thread for log processing can't transfer log well as has too |
| 263 | * low priority. |
| 264 | * Dedicated thread for log processing below uses highest application |
| 265 | * priority. This allows to transmit all logs without adding k_sleep/k_yield |
| 266 | * anywhere else int the code. |
| 267 | */ |
| 268 | |
| 269 | /* most simple log processing theread */ |
| 270 | void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3) |
| 271 | { |
| 272 | (void)dummy1; |
| 273 | (void)dummy2; |
| 274 | (void)dummy3; |
| 275 | |
| 276 | log_init(); |
| 277 | |
| 278 | while (1) { |
| 279 | if (log_process(false) == false) { |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 280 | if (boot_log_stop) { |
| 281 | break; |
| 282 | } |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 283 | k_sleep(BOOT_LOG_PROCESSING_INTERVAL); |
| 284 | } |
| 285 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 286 | |
| 287 | k_sem_give(&boot_log_sem); |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void zephyr_boot_log_start(void) |
| 291 | { |
| 292 | /* start logging thread */ |
| 293 | k_thread_create(&boot_log_thread, boot_log_stack, |
| 294 | K_THREAD_STACK_SIZEOF(boot_log_stack), |
| 295 | boot_log_thread_func, NULL, NULL, NULL, |
| 296 | K_HIGHEST_APPLICATION_THREAD_PRIO, 0, |
| 297 | BOOT_LOG_PROCESSING_INTERVAL); |
| 298 | |
| 299 | k_thread_name_set(&boot_log_thread, "logging"); |
| 300 | } |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 301 | |
| 302 | void zephyr_boot_log_stop(void) |
| 303 | { |
| 304 | boot_log_stop = true; |
| 305 | |
| 306 | /* wait until log procesing thread expired |
| 307 | * This can be reworked using a thread_join() API once a such will be |
| 308 | * available in zephyr. |
| 309 | * see https://github.com/zephyrproject-rtos/zephyr/issues/21500 |
| 310 | */ |
| 311 | (void)k_sem_take(&boot_log_sem, K_FOREVER); |
| 312 | } |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 313 | #endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\ |
| 314 | !defined(CONFIG_LOG_PROCESS_THREAD) */ |
| 315 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 316 | void main(void) |
| 317 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 318 | struct boot_rsp rsp; |
| 319 | int rc; |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 320 | fih_int fih_rc = FIH_FAILURE; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 321 | |
Andrzej Puzdrowski | 210b318 | 2020-10-13 13:52:15 +0200 | [diff] [blame] | 322 | MCUBOOT_WATCHDOG_FEED(); |
| 323 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 324 | BOOT_LOG_INF("Starting bootloader"); |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 325 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 326 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 327 | |
Andrzej Puzdrowski | 3f092bd | 2020-02-17 13:25:32 +0100 | [diff] [blame] | 328 | ZEPHYR_BOOT_LOG_START(); |
| 329 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 330 | (void)rc; |
| 331 | |
Kumar Gala | 32b61f3 | 2020-04-08 12:02:03 -0500 | [diff] [blame] | 332 | #if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) |
| 333 | if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) { |
| 334 | BOOT_LOG_ERR("Flash device %s not found", |
| 335 | DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 336 | while (1) |
| 337 | ; |
| 338 | } |
Kumar Gala | 9a5b951 | 2020-04-08 12:06:21 -0500 | [diff] [blame] | 339 | #elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL)) |
| 340 | if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) { |
| 341 | 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] | 342 | while (1) |
| 343 | ; |
| 344 | } |
Rajavardhan Gundi | c3353b2 | 2018-12-06 17:24:10 +0530 | [diff] [blame] | 345 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 346 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 347 | #ifdef CONFIG_MCUBOOT_SERIAL |
| 348 | |
Dominik Ermel | 1422b4b | 2020-09-11 11:31:38 +0000 | [diff] [blame] | 349 | struct device const *detect_port; |
Kumar Gala | 0813efe | 2020-05-27 12:25:41 -0500 | [diff] [blame] | 350 | uint32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 351 | |
| 352 | detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT); |
| 353 | __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n"); |
| 354 | |
Peter Bigot | 36e9029 | 2020-01-30 05:36:47 -0600 | [diff] [blame] | 355 | /* The default presence value is 0 which would normally be |
| 356 | * active-low, but historically the raw value was checked so we'll |
| 357 | * use the raw interface. |
| 358 | */ |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 359 | rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
Peter Bigot | 36e9029 | 2020-01-30 05:36:47 -0600 | [diff] [blame] | 360 | #ifdef GPIO_INPUT |
| 361 | GPIO_INPUT | GPIO_PULL_UP |
| 362 | #else |
| 363 | GPIO_DIR_IN | GPIO_PUD_PULL_UP |
| 364 | #endif |
Andrzej Puzdrowski | 5d96bd2 | 2020-02-20 10:35:33 +0100 | [diff] [blame] | 365 | ); |
Andrzej Puzdrowski | 82d7395 | 2018-06-13 14:55:51 +0200 | [diff] [blame] | 366 | __ASSERT(rc == 0, "Error of boot detect pin initialization.\n"); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 367 | |
Peter Bigot | 36e9029 | 2020-01-30 05:36:47 -0600 | [diff] [blame] | 368 | #ifdef GPIO_INPUT |
| 369 | rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN); |
| 370 | detect_value = rc; |
| 371 | #else |
Emanuele Di Santo | fcfff58 | 2018-11-01 16:06:36 +0100 | [diff] [blame] | 372 | rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 373 | &detect_value); |
Peter Bigot | 36e9029 | 2020-01-30 05:36:47 -0600 | [diff] [blame] | 374 | #endif |
| 375 | __ASSERT(rc >= 0, "Error of the reading the detect pin.\n"); |
Jon Helge Nistad | e58f9bd | 2019-11-25 15:59:52 +0100 | [diff] [blame] | 376 | if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL && |
| 377 | !boot_skip_serial_recovery()) { |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 378 | BOOT_LOG_INF("Enter the serial recovery mode"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 379 | rc = boot_console_init(); |
Carles Cufi | e2a3612 | 2018-06-15 10:51:02 +0200 | [diff] [blame] | 380 | __ASSERT(rc == 0, "Error initializing boot console.\n"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 381 | boot_serial_start(&boot_funcs); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 382 | __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n"); |
| 383 | } |
| 384 | #endif |
| 385 | |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 386 | #ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU |
Andrzej Puzdrowski | ac1f1ff | 2020-02-05 08:40:12 +0100 | [diff] [blame] | 387 | rc = usb_enable(NULL); |
| 388 | if (rc) { |
| 389 | BOOT_LOG_ERR("Cannot enable USB"); |
| 390 | } else { |
| 391 | BOOT_LOG_INF("Waiting for USB DFU"); |
| 392 | wait_for_usb_dfu(); |
| 393 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
| 394 | } |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 395 | #endif |
| 396 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 397 | FIH_CALL(boot_go, fih_rc, &rsp); |
| 398 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 399 | BOOT_LOG_ERR("Unable to find bootable image"); |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 400 | FIH_PANIC; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 401 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 402 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 403 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 404 | rsp.br_image_off); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 405 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 406 | BOOT_LOG_INF("Jumping to the first image slot"); |
Andrzej Puzdrowski | 8459163 | 2020-02-24 11:50:19 +0100 | [diff] [blame] | 407 | ZEPHYR_BOOT_LOG_STOP(); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 408 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 409 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 410 | BOOT_LOG_ERR("Never should get here"); |
| 411 | while (1) |
| 412 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 413 | } |