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