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