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> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 19 | #include <gpio.h> |
| 20 | #include <misc/__assert.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 21 | #include <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 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 47 | MCUBOOT_LOG_MODULE_REGISTER(mcuboot); |
| 48 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 49 | void os_heap_init(void); |
| 50 | |
| 51 | #if defined(CONFIG_ARM) |
| 52 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 53 | uint32_t msp; |
| 54 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Emanuele Di Santo | fcfff58 | 2018-11-01 16:06:36 +0100 | [diff] [blame] | 57 | extern void sys_clock_disable(void); |
| 58 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 59 | static void do_boot(struct boot_rsp *rsp) |
| 60 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 61 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 62 | uintptr_t flash_base; |
| 63 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 64 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 65 | /* The beginning of the image is the ARM vector table, containing |
| 66 | * the initial stack pointer address and the reset vector |
| 67 | * consecutively. Manually set the stack pointer and jump into the |
| 68 | * reset vector |
| 69 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 70 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 71 | assert(rc == 0); |
| 72 | |
| 73 | vt = (struct arm_vector_table *)(flash_base + |
| 74 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 75 | rsp->br_hdr->ih_hdr_size); |
| 76 | irq_lock(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame^] | 77 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 78 | sys_clock_disable(); |
Andrzej Puzdrowski | 960a2d6 | 2019-08-12 16:28:26 +0200 | [diff] [blame^] | 79 | #endif |
Jun Li | 01bef71 | 2019-05-08 21:55:54 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_USB |
Emanuele Di Santo | c4bf780 | 2018-07-20 11:39:57 +0200 | [diff] [blame] | 81 | /* Disable the USB to prevent it from firing interrupts */ |
| 82 | usb_disable(); |
| 83 | #endif |
David Brown | 57837b9 | 2018-03-13 15:56:38 -0600 | [diff] [blame] | 84 | __set_MSP(vt->msp); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 85 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 86 | } |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 87 | |
| 88 | #elif defined(CONFIG_XTENSA) |
| 89 | #define SRAM_BASE_ADDRESS 0xBE030000 |
| 90 | |
| 91 | static void copy_img_to_SRAM(int slot, unsigned int hdr_offset) |
| 92 | { |
| 93 | const struct flash_area *fap; |
| 94 | int area_id; |
| 95 | int rc; |
| 96 | unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset); |
| 97 | |
| 98 | BOOT_LOG_INF("Copying image to SRAM"); |
| 99 | |
| 100 | area_id = flash_area_id_from_image_slot(slot); |
| 101 | rc = flash_area_open(area_id, &fap); |
| 102 | if (rc != 0) { |
| 103 | BOOT_LOG_ERR("flash_area_open failed with %d\n", rc); |
| 104 | goto done; |
| 105 | } |
| 106 | |
| 107 | rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset); |
| 108 | if (rc != 0) { |
| 109 | BOOT_LOG_ERR("flash_area_read failed with %d\n", rc); |
| 110 | goto done; |
| 111 | } |
| 112 | |
| 113 | done: |
| 114 | flash_area_close(fap); |
| 115 | } |
| 116 | |
| 117 | /* Entry point (.ResetVector) is at the very beginning of the image. |
| 118 | * Simply copy the image to a suitable location and jump there. |
| 119 | */ |
| 120 | static void do_boot(struct boot_rsp *rsp) |
| 121 | { |
| 122 | void *start; |
| 123 | |
| 124 | BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off); |
| 125 | BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size); |
| 126 | |
| 127 | /* Copy from the flash to HP SRAM */ |
| 128 | copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size); |
| 129 | |
| 130 | /* Jump to entry point */ |
| 131 | start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size); |
| 132 | ((void (*)(void))start)(); |
| 133 | } |
| 134 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 135 | #else |
| 136 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 137 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 138 | * possibly other platforms. |
| 139 | */ |
| 140 | static void do_boot(struct boot_rsp *rsp) |
| 141 | { |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 142 | uintptr_t flash_base; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 143 | void *start; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 144 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 145 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 146 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 147 | assert(rc == 0); |
| 148 | |
| 149 | start = (void *)(flash_base + rsp->br_image_off + |
| 150 | rsp->br_hdr->ih_hdr_size); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 151 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 152 | /* Lock interrupts and dive into the entry point */ |
| 153 | irq_lock(); |
| 154 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 155 | } |
| 156 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 157 | |
| 158 | void main(void) |
| 159 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 160 | struct boot_rsp rsp; |
| 161 | int rc; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 162 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 163 | BOOT_LOG_INF("Starting bootloader"); |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 164 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 165 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 166 | |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 167 | #if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME)) |
Andrzej Puzdrowski | f50054d | 2018-11-14 14:33:19 +0100 | [diff] [blame] | 168 | if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) { |
| 169 | BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 170 | while (1) |
| 171 | ; |
| 172 | } |
Rajavardhan Gundi | 24321c3 | 2019-02-08 12:48:34 +0530 | [diff] [blame] | 173 | #elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL)) |
| 174 | if (!flash_device_get_binding(DT_JEDEC_SPI_NOR_0_LABEL)) { |
| 175 | BOOT_LOG_ERR("Flash device %s not found", DT_JEDEC_SPI_NOR_0_LABEL); |
Rajavardhan Gundi | 40c28e3 | 2018-12-09 13:32:01 +0530 | [diff] [blame] | 176 | while (1) |
| 177 | ; |
| 178 | } |
Rajavardhan Gundi | c3353b2 | 2018-12-06 17:24:10 +0530 | [diff] [blame] | 179 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 180 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 181 | #ifdef CONFIG_MCUBOOT_SERIAL |
| 182 | |
| 183 | struct device *detect_port; |
| 184 | u32_t detect_value; |
| 185 | |
| 186 | detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT); |
| 187 | __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n"); |
| 188 | |
| 189 | rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
| 190 | GPIO_DIR_IN | GPIO_PUD_PULL_UP); |
Andrzej Puzdrowski | 82d7395 | 2018-06-13 14:55:51 +0200 | [diff] [blame] | 191 | __ASSERT(rc == 0, "Error of boot detect pin initialization.\n"); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 192 | |
Emanuele Di Santo | fcfff58 | 2018-11-01 16:06:36 +0100 | [diff] [blame] | 193 | rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 194 | &detect_value); |
Andrzej Puzdrowski | 82d7395 | 2018-06-13 14:55:51 +0200 | [diff] [blame] | 195 | __ASSERT(rc == 0, "Error of the reading the detect pin.\n"); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 196 | |
| 197 | if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) { |
| 198 | BOOT_LOG_INF("Enter the serial recovery mode"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 199 | rc = boot_console_init(); |
Carles Cufi | e2a3612 | 2018-06-15 10:51:02 +0200 | [diff] [blame] | 200 | __ASSERT(rc == 0, "Error initializing boot console.\n"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 201 | boot_serial_start(&boot_funcs); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 202 | __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n"); |
| 203 | } |
| 204 | #endif |
| 205 | |
Rajavardhan Gundi | 51c9d70 | 2019-02-20 14:08:52 +0530 | [diff] [blame] | 206 | #ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU |
| 207 | BOOT_LOG_INF("Waiting for USB DFU"); |
| 208 | wait_for_usb_dfu(); |
| 209 | BOOT_LOG_INF("USB DFU wait time elapsed"); |
| 210 | #endif |
| 211 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 212 | rc = boot_go(&rsp); |
| 213 | if (rc != 0) { |
| 214 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 215 | while (1) |
| 216 | ; |
| 217 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 218 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 219 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 220 | rsp.br_image_off); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 221 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 222 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 223 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 224 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 225 | BOOT_LOG_ERR("Never should get here"); |
| 226 | while (1) |
| 227 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 228 | } |