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