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