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> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 19 | #include <flash.h> |
| 20 | #include <asm_inline.h> |
Ricardo Salveti | 8e4d44d | 2017-02-27 23:00:31 -0300 | [diff] [blame] | 21 | #include <drivers/system_timer.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 22 | |
Marti Bolivar | 51181cf | 2017-03-20 11:03:41 -0400 | [diff] [blame] | 23 | #include "target.h" |
| 24 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame] | 25 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 26 | #include "bootutil/bootutil_log.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 27 | #include "bootutil/image.h" |
| 28 | #include "bootutil/bootutil.h" |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 29 | #include "flash_map/flash_map.h" |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 30 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 31 | struct device *boot_flash_device; |
| 32 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 33 | void os_heap_init(void); |
| 34 | |
| 35 | #if defined(CONFIG_ARM) |
| 36 | struct arm_vector_table { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 37 | uint32_t msp; |
| 38 | uint32_t reset; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Marti Bolivar | 83a3cef | 2017-05-05 11:59:49 -0400 | [diff] [blame^] | 41 | extern void zephyr_flash_area_warn_on_open(void); |
| 42 | |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 43 | static void do_boot(struct boot_rsp *rsp) |
| 44 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 45 | struct arm_vector_table *vt; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 46 | uintptr_t flash_base; |
| 47 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 48 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 49 | /* The beginning of the image is the ARM vector table, containing |
| 50 | * the initial stack pointer address and the reset vector |
| 51 | * consecutively. Manually set the stack pointer and jump into the |
| 52 | * reset vector |
| 53 | */ |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 54 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 55 | assert(rc == 0); |
| 56 | |
| 57 | vt = (struct arm_vector_table *)(flash_base + |
| 58 | rsp->br_image_off + |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 59 | rsp->br_hdr->ih_hdr_size); |
| 60 | irq_lock(); |
| 61 | sys_clock_disable(); |
| 62 | _MspSet(vt->msp); |
| 63 | ((void (*)(void))vt->reset)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 64 | } |
| 65 | #else |
| 66 | /* Default: Assume entry point is at the very beginning of the image. Simply |
| 67 | * lock interrupts and jump there. This is the right thing to do for X86 and |
| 68 | * possibly other platforms. |
| 69 | */ |
| 70 | static void do_boot(struct boot_rsp *rsp) |
| 71 | { |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 72 | uintptr_t flash_base; |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 73 | void *start; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 74 | int rc; |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 75 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 76 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 77 | assert(rc == 0); |
| 78 | |
| 79 | start = (void *)(flash_base + rsp->br_image_off + |
| 80 | rsp->br_hdr->ih_hdr_size); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 81 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 82 | /* Lock interrupts and dive into the entry point */ |
| 83 | irq_lock(); |
| 84 | ((void (*)(void))start)(); |
Andrew Boie | 7238f51 | 2017-03-02 13:39:06 -0800 | [diff] [blame] | 85 | } |
| 86 | #endif |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 87 | |
| 88 | void main(void) |
| 89 | { |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 90 | struct boot_rsp rsp; |
| 91 | int rc; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 92 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 93 | BOOT_LOG_INF("Starting bootloader"); |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 94 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 95 | os_heap_init(); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 96 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 97 | boot_flash_device = device_get_binding(FLASH_DRIVER_NAME); |
| 98 | if (!boot_flash_device) { |
| 99 | BOOT_LOG_ERR("Flash device not found"); |
| 100 | while (1) |
| 101 | ; |
| 102 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 103 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 104 | rc = boot_go(&rsp); |
| 105 | if (rc != 0) { |
| 106 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 107 | while (1) |
| 108 | ; |
| 109 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 110 | |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 111 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 112 | rsp.br_image_off); |
Marti Bolivar | 83a3cef | 2017-05-05 11:59:49 -0400 | [diff] [blame^] | 113 | zephyr_flash_area_warn_on_open(); |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 114 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 115 | do_boot(&rsp); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 116 | |
David Brown | 0d0652a | 2017-04-11 17:33:30 -0600 | [diff] [blame] | 117 | BOOT_LOG_ERR("Never should get here"); |
| 118 | while (1) |
| 119 | ; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 120 | } |