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 | |
| 35 | struct vector_table { |
| 36 | uint32_t msp; |
| 37 | uint32_t reset; |
| 38 | }; |
| 39 | |
| 40 | void os_heap_init(void); |
| 41 | |
| 42 | void main(void) |
| 43 | { |
| 44 | struct boot_rsp rsp; |
| 45 | struct vector_table *vt; |
| 46 | int rc; |
| 47 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 48 | BOOT_LOG_INF("Starting bootloader"); |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 49 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 50 | os_heap_init(); |
| 51 | |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 52 | boot_flash_device = device_get_binding(FLASH_DRIVER_NAME); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 53 | if (!boot_flash_device) { |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 54 | BOOT_LOG_ERR("Flash device not found"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 55 | while (1) |
| 56 | ; |
| 57 | } |
| 58 | |
| 59 | rc = boot_go(&rsp); |
| 60 | if (rc != 0) { |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 61 | BOOT_LOG_ERR("Unable to find bootable image"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 62 | while (1) |
| 63 | ; |
| 64 | } |
| 65 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 66 | BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 67 | vt = (struct vector_table *)(rsp.br_image_addr + |
| 68 | rsp.br_hdr->ih_hdr_size); |
| 69 | irq_lock(); |
| 70 | _MspSet(vt->msp); |
| 71 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 72 | BOOT_LOG_INF("Jumping to the first image slot"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 73 | ((void (*)(void))vt->reset)(); |
| 74 | |
Marti Bolivar | 4a97b4c | 2017-01-31 18:20:02 -0500 | [diff] [blame^] | 75 | BOOT_LOG_ERR("Never should get here"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 76 | while (1) |
| 77 | ; |
| 78 | } |