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> |
Evan Gates | 4632d8d | 2018-06-28 13:27:40 -0700 | [diff] [blame^] | 23 | #include <soc.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 24 | |
Marti Bolivar | 51181cf | 2017-03-20 11:03:41 -0400 | [diff] [blame] | 25 | #include "target.h" |
| 26 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame] | 27 | #include "bootutil/bootutil_log.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 28 | #include "bootutil/image.h" |
| 29 | #include "bootutil/bootutil.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 30 | #include "flash_map_backend/flash_map_backend.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 31 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_MCUBOOT_SERIAL |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 33 | #include "boot_serial/boot_serial.h" |
| 34 | #include "serial_adapter/serial_adapter.h" |
| 35 | |
| 36 | const struct boot_uart_funcs boot_funcs = { |
| 37 | .read = console_read, |
| 38 | .write = console_write |
| 39 | }; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 42 | struct device *boot_flash_device; |
| 43 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 44 | void os_heap_init(void); |
| 45 | |
| 46 | #if defined(CONFIG_ARM) |
| 47 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 48 | uint32_t msp; |
| 49 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 52 | static void do_boot(struct boot_rsp *rsp) |
| 53 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 54 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 55 | uintptr_t flash_base; |
| 56 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 57 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 58 | /* The beginning of the image is the ARM vector table, containing |
| 59 | * the initial stack pointer address and the reset vector |
| 60 | * consecutively. Manually set the stack pointer and jump into the |
| 61 | * reset vector |
| 62 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 63 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 64 | assert(rc == 0); |
| 65 | |
| 66 | vt = (struct arm_vector_table *)(flash_base + |
| 67 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 68 | rsp->br_hdr->ih_hdr_size); |
| 69 | irq_lock(); |
| 70 | sys_clock_disable(); |
David Brown | 57837b9 | 2018-03-13 15:56:38 -0600 | [diff] [blame] | 71 | __set_MSP(vt->msp); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 72 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 73 | } |
| 74 | #else |
| 75 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 76 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 77 | * possibly other platforms. |
| 78 | */ |
| 79 | static void do_boot(struct boot_rsp *rsp) |
| 80 | { |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 81 | uintptr_t flash_base; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 82 | void *start; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 83 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 84 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 85 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 86 | assert(rc == 0); |
| 87 | |
| 88 | start = (void *)(flash_base + rsp->br_image_off + |
| 89 | rsp->br_hdr->ih_hdr_size); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 90 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 91 | /* Lock interrupts and dive into the entry point */ |
| 92 | irq_lock(); |
| 93 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 94 | } |
| 95 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 96 | |
| 97 | void main(void) |
| 98 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 99 | struct boot_rsp rsp; |
| 100 | int rc; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 101 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 102 | BOOT_LOG_INF("Starting bootloader"); |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 103 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 104 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 105 | |
Marti Bolivar | 310445b | 2018-04-10 10:23:47 -0400 | [diff] [blame] | 106 | boot_flash_device = device_get_binding(FLASH_DEV_NAME); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 107 | if (!boot_flash_device) { |
Marti Bolivar | 310445b | 2018-04-10 10:23:47 -0400 | [diff] [blame] | 108 | BOOT_LOG_ERR("Flash device %s not found", FLASH_DEV_NAME); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 109 | while (1) |
| 110 | ; |
| 111 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 112 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 113 | #ifdef CONFIG_MCUBOOT_SERIAL |
| 114 | |
| 115 | struct device *detect_port; |
| 116 | u32_t detect_value; |
| 117 | |
| 118 | detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT); |
| 119 | __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n"); |
| 120 | |
| 121 | rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
| 122 | GPIO_DIR_IN | GPIO_PUD_PULL_UP); |
Andrzej Puzdrowski | 82d7395 | 2018-06-13 14:55:51 +0200 | [diff] [blame] | 123 | __ASSERT(rc == 0, "Error of boot detect pin initialization.\n"); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 124 | |
| 125 | rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, |
| 126 | &detect_value); |
Andrzej Puzdrowski | 82d7395 | 2018-06-13 14:55:51 +0200 | [diff] [blame] | 127 | __ASSERT(rc == 0, "Error of the reading the detect pin.\n"); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 128 | |
| 129 | if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) { |
| 130 | BOOT_LOG_INF("Enter the serial recovery mode"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 131 | rc = boot_console_init(); |
Carles Cufi | e2a3612 | 2018-06-15 10:51:02 +0200 | [diff] [blame] | 132 | __ASSERT(rc == 0, "Error initializing boot console.\n"); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 133 | boot_serial_start(&boot_funcs); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 134 | __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n"); |
| 135 | } |
| 136 | #endif |
| 137 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 138 | rc = boot_go(&rsp); |
| 139 | if (rc != 0) { |
| 140 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 141 | while (1) |
| 142 | ; |
| 143 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 144 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 145 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 146 | rsp.br_image_off); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 147 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 148 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 149 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 150 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 151 | BOOT_LOG_ERR("Never should get here"); |
| 152 | while (1) |
| 153 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 154 | } |