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 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 21 | #define SYS_LOG_DOMAIN "BOOTLOADER" |
| 22 | #define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO |
| 23 | #include <logging/sys_log.h> |
| 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 | |
| 36 | struct vector_table { |
| 37 | uint32_t msp; |
| 38 | uint32_t reset; |
| 39 | }; |
| 40 | |
| 41 | void os_heap_init(void); |
| 42 | |
| 43 | void main(void) |
| 44 | { |
| 45 | struct boot_rsp rsp; |
| 46 | struct vector_table *vt; |
| 47 | int rc; |
| 48 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 49 | SYS_LOG_INF("Starting bootloader"); |
| 50 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 51 | os_heap_init(); |
| 52 | |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 53 | boot_flash_device = device_get_binding(FLASH_DRIVER_NAME); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 54 | if (!boot_flash_device) { |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 55 | SYS_LOG_ERR("Flash device not found"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 56 | while (1) |
| 57 | ; |
| 58 | } |
| 59 | |
| 60 | rc = boot_go(&rsp); |
| 61 | if (rc != 0) { |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 62 | SYS_LOG_ERR("Unable to find bootable image"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 63 | while (1) |
| 64 | ; |
| 65 | } |
| 66 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 67 | SYS_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 68 | vt = (struct vector_table *)(rsp.br_image_addr + |
| 69 | rsp.br_hdr->ih_hdr_size); |
| 70 | irq_lock(); |
| 71 | _MspSet(vt->msp); |
| 72 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 73 | SYS_LOG_INF("Setting vector table to %p", vt); |
| 74 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 75 | /* Not all targets set the VTOR, so just set it. */ |
| 76 | _scs_relocate_vector_table((void *) vt); |
| 77 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 78 | SYS_LOG_INF("Jumping to the first image slot"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 79 | ((void (*)(void))vt->reset)(); |
| 80 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame^] | 81 | SYS_LOG_ERR("Never should get here"); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 82 | while (1) |
| 83 | ; |
| 84 | } |