Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2017, Arm Limited. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 19 | #include "bl2_util.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 20 | #include "target.h" |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 21 | #include "cmsis.h" |
| 22 | #include "uart_stdout.h" |
| 23 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 24 | |
| 25 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 26 | #include "bootutil/bootutil_log.h" |
| 27 | #include "bootutil/image.h" |
| 28 | #include "bootutil/bootutil.h" |
| 29 | #include "flash_map/flash_map.h" |
| 30 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 31 | /* Avoids the semihosting issue */ |
| 32 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 33 | __asm(" .global __ARM_use_no_argv\n"); |
| 34 | #endif |
| 35 | |
| 36 | /* Keep these variables to be compatible with flash API */ |
| 37 | struct device tmp; |
| 38 | struct device *boot_flash_device = &tmp; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 39 | |
| 40 | void os_heap_init(void); |
| 41 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 42 | struct arm_vector_table { |
| 43 | uint32_t msp; |
| 44 | uint32_t reset; |
| 45 | }; |
| 46 | |
| 47 | static void do_boot(struct boot_rsp *rsp) |
| 48 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 49 | /* Clang at O0, stores variables on the stack with SP relative addressing. |
| 50 | * When manually set the SP then the place of reset vector is lost. |
| 51 | * Static variables are stored in 'data' or 'bss' section, change of SP has |
| 52 | * no effect on them. |
| 53 | */ |
| 54 | static struct arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 55 | uintptr_t flash_base; |
| 56 | int rc; |
| 57 | |
| 58 | /* The beginning of the image is the ARM vector table, containing |
| 59 | * the initial stack pointer address and the reset vector |
| 60 | * consecutively. Manually set the stack pointer and jump into the |
| 61 | * reset vector |
| 62 | */ |
| 63 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 64 | assert(rc == 0); |
| 65 | |
| 66 | vt = (struct arm_vector_table *)(flash_base + |
| 67 | rsp->br_image_off + |
| 68 | rsp->br_hdr->ih_hdr_size); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 69 | __disable_irq(); |
| 70 | __set_MSP(vt->msp); |
| 71 | __DSB(); |
| 72 | __ISB(); |
| 73 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 74 | ((void (*)(void))vt->reset)(); |
| 75 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 76 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 77 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 78 | { |
| 79 | struct boot_rsp rsp; |
| 80 | int rc; |
| 81 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 82 | uart_init(UART0_CHANNEL); |
| 83 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 84 | BOOT_LOG_INF("Starting bootloader"); |
| 85 | |
| 86 | os_heap_init(); |
| 87 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 88 | rc = boot_go(&rsp); |
| 89 | if (rc != 0) { |
| 90 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 91 | while (1) |
| 92 | ; |
| 93 | } |
| 94 | |
| 95 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 96 | rsp.br_image_off); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 97 | flash_area_warn_on_open(); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 98 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 99 | do_boot(&rsp); |
| 100 | |
| 101 | BOOT_LOG_ERR("Never should get here"); |
| 102 | while (1) |
| 103 | ; |
| 104 | } |