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