Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 3 | * Copyright (c) 2017-2020 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" |
Kevin Peng | bc5e5aa | 2019-10-16 10:55:17 +0800 | [diff] [blame] | 21 | #include "tfm_hal_device_header.h" |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 22 | #include "Driver_Flash.h" |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 23 | #include "mbedtls/memory_buffer_alloc.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 24 | #include "bootutil/bootutil_log.h" |
| 25 | #include "bootutil/image.h" |
| 26 | #include "bootutil/bootutil.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 27 | #include "flash_map_backend/flash_map_backend.h" |
| 28 | #include "boot_record.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 29 | #include "security_cnt.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 30 | #include "boot_hal.h" |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 31 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 32 | #include "uart_stdout.h" |
| 33 | #endif |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 34 | #if defined(CRYPTO_HW_ACCELERATOR) || \ |
| 35 | defined(CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING) |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 36 | #include "crypto_hw.h" |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 37 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 38 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 39 | /* Avoids the semihosting issue */ |
| 40 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 41 | __asm(" .global __ARM_use_no_argv\n"); |
| 42 | #endif |
| 43 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 44 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 45 | /* Macros to pick linker symbols */ |
| 46 | #define REGION(a, b, c) a##b##c |
| 47 | #define REGION_NAME(a, b, c) REGION(a, b, c) |
| 48 | #define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c) |
| 49 | |
| 50 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 51 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 52 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 53 | /* Flash device name must be specified by target */ |
| 54 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 55 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 56 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 |
| 57 | /* Static buffer to be used by mbedtls for memory allocation */ |
| 58 | static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 59 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 60 | struct arm_vector_table { |
| 61 | uint32_t msp; |
| 62 | uint32_t reset; |
| 63 | }; |
| 64 | |
Tamas Ban | d4bf347 | 2019-09-06 12:59:56 +0100 | [diff] [blame] | 65 | /*! |
| 66 | * \brief Chain-loading the next image in the boot sequence. |
| 67 | * |
| 68 | * This function calls the Reset_Handler of the next image in the boot sequence, |
| 69 | * usually it is the secure firmware. Before passing the execution to next image |
| 70 | * there is conditional rule to remove the secrets from the memory. This must be |
| 71 | * done if the following conditions are satisfied: |
| 72 | * - Memory is shared between SW components at different stages of the trusted |
| 73 | * boot process. |
| 74 | * - There are secrets in the memory: KDF parameter, symmetric key, |
| 75 | * manufacturer sensitive code/data, etc. |
| 76 | */ |
| 77 | __attribute__((naked)) void boot_jump_to_next_image(uint32_t reset_handler_addr) |
| 78 | { |
| 79 | __ASM volatile( |
| 80 | ".syntax unified \n" |
| 81 | "mov r7, r0 \n" |
| 82 | "bl boot_clear_bl2_ram_area \n" /* Clear RAM before jump */ |
| 83 | "movs r0, #0 \n" /* Clear registers: R0-R12, */ |
| 84 | "mov r1, r0 \n" /* except R7 */ |
| 85 | "mov r2, r0 \n" |
| 86 | "mov r3, r0 \n" |
| 87 | "mov r4, r0 \n" |
| 88 | "mov r5, r0 \n" |
| 89 | "mov r6, r0 \n" |
| 90 | "mov r8, r0 \n" |
| 91 | "mov r9, r0 \n" |
| 92 | "mov r10, r0 \n" |
| 93 | "mov r11, r0 \n" |
| 94 | "mov r12, r0 \n" |
| 95 | "mov lr, r0 \n" |
| 96 | "bx r7 \n" /* Jump to Reset_handler */ |
| 97 | ); |
| 98 | } |
| 99 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 100 | static void do_boot(struct boot_rsp *rsp) |
| 101 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 102 | /* Clang at O0, stores variables on the stack with SP relative addressing. |
| 103 | * When manually set the SP then the place of reset vector is lost. |
| 104 | * Static variables are stored in 'data' or 'bss' section, change of SP has |
| 105 | * no effect on them. |
| 106 | */ |
| 107 | static struct arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 108 | uintptr_t flash_base; |
| 109 | int rc; |
| 110 | |
| 111 | /* The beginning of the image is the ARM vector table, containing |
| 112 | * the initial stack pointer address and the reset vector |
| 113 | * consecutively. Manually set the stack pointer and jump into the |
| 114 | * reset vector |
| 115 | */ |
| 116 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 117 | assert(rc == 0); |
| 118 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 119 | if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 120 | /* The image has been copied to SRAM, find the vector table |
| 121 | * at the load address instead of image's address in flash |
| 122 | */ |
| 123 | vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + |
| 124 | rsp->br_hdr->ih_hdr_size); |
| 125 | } else { |
| 126 | /* Using the flash address as not executing in SRAM */ |
| 127 | vt = (struct arm_vector_table *)(flash_base + |
| 128 | rsp->br_image_off + |
| 129 | rsp->br_hdr->ih_hdr_size); |
| 130 | } |
| 131 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 132 | rc = FLASH_DEV_NAME.Uninitialize(); |
| 133 | if(rc != ARM_DRIVER_OK) { |
| 134 | BOOT_LOG_ERR("Error while uninitializing Flash Interface"); |
| 135 | } |
| 136 | |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 137 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 138 | stdio_uninit(); |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 139 | #endif |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 140 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 141 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 142 | /* Restore the Main Stack Pointer Limit register's reset value |
| 143 | * before passing execution to runtime firmware to make the |
| 144 | * bootloader transparent to it. |
| 145 | */ |
| 146 | __set_MSPLIM(0); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 147 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 148 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 149 | __set_MSP(vt->msp); |
| 150 | __DSB(); |
| 151 | __ISB(); |
| 152 | |
Tamas Ban | d4bf347 | 2019-09-06 12:59:56 +0100 | [diff] [blame] | 153 | boot_jump_to_next_image(vt->reset); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 154 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 155 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 156 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 157 | { |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 158 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 159 | uint32_t msp_stack_bottom = |
| 160 | (uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 161 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 162 | struct boot_rsp rsp; |
| 163 | int rc; |
| 164 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 165 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 166 | __set_MSPLIM(msp_stack_bottom); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 167 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 168 | |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 169 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 170 | stdio_init(); |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 171 | #endif |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 172 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 173 | BOOT_LOG_INF("Starting bootloader"); |
| 174 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 175 | /* Initialise the mbedtls static memory allocator so that mbedtls allocates |
| 176 | * memory from the provided static buffer instead of from the heap. |
| 177 | */ |
| 178 | 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] | 179 | |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 180 | #ifdef CRYPTO_HW_ACCELERATOR |
| 181 | rc = crypto_hw_accelerator_init(); |
| 182 | if (rc) { |
| 183 | BOOT_LOG_ERR("Error while initializing cryptographic accelerator."); |
| 184 | while (1); |
| 185 | } |
| 186 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 187 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 188 | rc = FLASH_DEV_NAME.Initialize(NULL); |
| 189 | if(rc != ARM_DRIVER_OK) { |
| 190 | BOOT_LOG_ERR("Error while initializing Flash Interface"); |
| 191 | while (1) |
| 192 | ; |
| 193 | } |
David Vincze | 26e8c8a | 2018-08-28 16:59:41 +0200 | [diff] [blame] | 194 | |
David Vincze | c3e313a | 2020-01-06 17:31:11 +0100 | [diff] [blame^] | 195 | #ifndef MCUBOOT_USE_UPSTREAM |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 196 | rc = boot_nv_security_counter_init(); |
| 197 | if (rc != 0) { |
| 198 | BOOT_LOG_ERR("Error while initializing the security counter"); |
| 199 | while (1) |
| 200 | ; |
| 201 | } |
David Vincze | c3e313a | 2020-01-06 17:31:11 +0100 | [diff] [blame^] | 202 | #endif /* !MCUBOOT_USE_UPSTREAM */ |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 203 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 204 | rc = boot_go(&rsp); |
| 205 | if (rc != 0) { |
| 206 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 207 | while (1) |
| 208 | ; |
| 209 | } |
| 210 | |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 211 | #ifdef CRYPTO_HW_ACCELERATOR |
| 212 | rc = crypto_hw_accelerator_finish(); |
| 213 | if (rc) { |
| 214 | BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator."); |
| 215 | while (1); |
| 216 | } |
| 217 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 218 | |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 219 | /* This is a workaround to program the TF-M related cryptographic keys |
| 220 | * to CC312 OTP memory. This functionality is independent from secure boot, |
| 221 | * this is usually done in the factory floor during chip manufacturing. |
| 222 | */ |
| 223 | #ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING |
| 224 | BOOT_LOG_INF("OTP provisioning started."); |
| 225 | rc = crypto_hw_accelerator_otp_provisioning(); |
| 226 | if (rc) { |
| 227 | BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc); |
| 228 | while (1); |
| 229 | } else { |
| 230 | BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded."); |
| 231 | |
| 232 | /* We don't need to boot - the only aim is provisioning. */ |
| 233 | while (1); |
| 234 | } |
| 235 | #endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */ |
| 236 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 237 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 238 | rsp.br_image_off); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 239 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 240 | do_boot(&rsp); |
| 241 | |
| 242 | BOOT_LOG_ERR("Never should get here"); |
| 243 | while (1) |
| 244 | ; |
| 245 | } |