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