Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 3 | * Copyright (c) 2017-2019 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" |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 23 | #include "Driver_Flash.h" |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 24 | #include "mbedtls/memory_buffer_alloc.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 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" |
Tamas Ban | a9de4a6 | 2018-09-18 08:09:45 +0100 | [diff] [blame] | 30 | #include "bl2/include/boot_record.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 31 | #include "security_cnt.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 32 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 33 | /* Avoids the semihosting issue */ |
| 34 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 35 | __asm(" .global __ARM_use_no_argv\n"); |
| 36 | #endif |
| 37 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 38 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 39 | /* Macros to pick linker symbols */ |
| 40 | #define REGION(a, b, c) a##b##c |
| 41 | #define REGION_NAME(a, b, c) REGION(a, b, c) |
| 42 | #define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c) |
| 43 | |
| 44 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 45 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 46 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 47 | /* Flash device name must be specified by target */ |
| 48 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 49 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 50 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 |
| 51 | /* Static buffer to be used by mbedtls for memory allocation */ |
| 52 | static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 53 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 54 | struct arm_vector_table { |
| 55 | uint32_t msp; |
| 56 | uint32_t reset; |
| 57 | }; |
| 58 | |
| 59 | static void do_boot(struct boot_rsp *rsp) |
| 60 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 61 | /* Clang at O0, stores variables on the stack with SP relative addressing. |
| 62 | * When manually set the SP then the place of reset vector is lost. |
| 63 | * Static variables are stored in 'data' or 'bss' section, change of SP has |
| 64 | * no effect on them. |
| 65 | */ |
| 66 | static struct arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 67 | uintptr_t flash_base; |
| 68 | int rc; |
| 69 | |
| 70 | /* The beginning of the image is the ARM vector table, containing |
| 71 | * the initial stack pointer address and the reset vector |
| 72 | * consecutively. Manually set the stack pointer and jump into the |
| 73 | * reset vector |
| 74 | */ |
| 75 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 76 | assert(rc == 0); |
| 77 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 78 | if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 79 | /* The image has been copied to SRAM, find the vector table |
| 80 | * at the load address instead of image's address in flash |
| 81 | */ |
| 82 | vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + |
| 83 | rsp->br_hdr->ih_hdr_size); |
| 84 | } else { |
| 85 | /* Using the flash address as not executing in SRAM */ |
| 86 | vt = (struct arm_vector_table *)(flash_base + |
| 87 | rsp->br_image_off + |
| 88 | rsp->br_hdr->ih_hdr_size); |
| 89 | } |
| 90 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 91 | rc = FLASH_DEV_NAME.Uninitialize(); |
| 92 | if(rc != ARM_DRIVER_OK) { |
| 93 | BOOT_LOG_ERR("Error while uninitializing Flash Interface"); |
| 94 | } |
| 95 | |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 96 | stdio_uninit(); |
| 97 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 98 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 99 | /* Restore the Main Stack Pointer Limit register's reset value |
| 100 | * before passing execution to runtime firmware to make the |
| 101 | * bootloader transparent to it. |
| 102 | */ |
| 103 | __set_MSPLIM(0); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 104 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 105 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 106 | __set_MSP(vt->msp); |
| 107 | __DSB(); |
| 108 | __ISB(); |
| 109 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 110 | ((void (*)(void))vt->reset)(); |
| 111 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 112 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 113 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 114 | { |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 115 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 116 | uint32_t msp_stack_bottom = |
| 117 | (uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 118 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 119 | struct boot_rsp rsp; |
| 120 | int rc; |
| 121 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 122 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 123 | __set_MSPLIM(msp_stack_bottom); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame^] | 124 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 125 | |
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 126 | stdio_init(); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 127 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 128 | BOOT_LOG_INF("Starting bootloader"); |
| 129 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 130 | /* Initialise the mbedtls static memory allocator so that mbedtls allocates |
| 131 | * memory from the provided static buffer instead of from the heap. |
| 132 | */ |
| 133 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 134 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 135 | rc = FLASH_DEV_NAME.Initialize(NULL); |
| 136 | if(rc != ARM_DRIVER_OK) { |
| 137 | BOOT_LOG_ERR("Error while initializing Flash Interface"); |
| 138 | while (1) |
| 139 | ; |
| 140 | } |
David Vincze | 26e8c8a | 2018-08-28 16:59:41 +0200 | [diff] [blame] | 141 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 142 | rc = boot_nv_security_counter_init(); |
| 143 | if (rc != 0) { |
| 144 | BOOT_LOG_ERR("Error while initializing the security counter"); |
| 145 | while (1) |
| 146 | ; |
| 147 | } |
| 148 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 149 | rc = boot_go(&rsp); |
| 150 | if (rc != 0) { |
| 151 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 152 | while (1) |
| 153 | ; |
| 154 | } |
| 155 | |
| 156 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 157 | rsp.br_image_off); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 158 | flash_area_warn_on_open(); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 159 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 160 | do_boot(&rsp); |
| 161 | |
| 162 | BOOT_LOG_ERR("Never should get here"); |
| 163 | while (1) |
| 164 | ; |
| 165 | } |