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