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 | |
Balint Matyi | 2fe0492 | 2020-02-18 12:27:38 +0000 | [diff] [blame] | 18 | #include "mcuboot_config/mcuboot_config.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 19 | #include <assert.h> |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 20 | #include "bl2_util.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 21 | #include "target.h" |
Kevin Peng | bc5e5aa | 2019-10-16 10:55:17 +0800 | [diff] [blame] | 22 | #include "tfm_hal_device_header.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 | #include "bootutil/bootutil_log.h" |
| 26 | #include "bootutil/image.h" |
| 27 | #include "bootutil/bootutil.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 28 | #include "flash_map_backend/flash_map_backend.h" |
| 29 | #include "boot_record.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 30 | #include "security_cnt.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 31 | #include "boot_hal.h" |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 32 | #include "region.h" |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 33 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 34 | #include "uart_stdout.h" |
| 35 | #endif |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 36 | #if defined(CRYPTO_HW_ACCELERATOR) || \ |
| 37 | defined(CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING) |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 38 | #include "crypto_hw.h" |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 39 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 40 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 41 | /* Avoids the semihosting issue */ |
| 42 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 43 | __asm(" .global __ARM_use_no_argv\n"); |
| 44 | #endif |
| 45 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 46 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 47 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 48 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 49 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 50 | /* Flash device name must be specified by target */ |
| 51 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 52 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 53 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 |
| 54 | /* Static buffer to be used by mbedtls for memory allocation */ |
| 55 | static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 56 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 57 | static void do_boot(struct boot_rsp *rsp) |
| 58 | { |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 59 | struct boot_arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 60 | uintptr_t flash_base; |
| 61 | int rc; |
| 62 | |
| 63 | /* The beginning of the image is the ARM vector table, containing |
| 64 | * the initial stack pointer address and the reset vector |
| 65 | * consecutively. Manually set the stack pointer and jump into the |
| 66 | * reset vector |
| 67 | */ |
| 68 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 69 | assert(rc == 0); |
| 70 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 71 | if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 72 | /* The image has been copied to SRAM, find the vector table |
| 73 | * at the load address instead of image's address in flash |
| 74 | */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 75 | vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr + |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 76 | rsp->br_hdr->ih_hdr_size); |
| 77 | } else { |
| 78 | /* Using the flash address as not executing in SRAM */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 79 | vt = (struct boot_arm_vector_table *)(flash_base + |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 80 | rsp->br_image_off + |
| 81 | rsp->br_hdr->ih_hdr_size); |
| 82 | } |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 83 | rc = FLASH_DEV_NAME.Uninitialize(); |
| 84 | if(rc != ARM_DRIVER_OK) { |
| 85 | BOOT_LOG_ERR("Error while uninitializing Flash Interface"); |
| 86 | } |
| 87 | |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 88 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 89 | stdio_uninit(); |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 90 | #endif |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 91 | /* This function never returns, because it calls the secure application |
| 92 | * Reset_Handler() |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 93 | */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 94 | boot_platform_quit(vt); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 95 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 96 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 97 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 98 | { |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 99 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 100 | uint32_t msp_stack_bottom = |
| 101 | (uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 102 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 103 | struct boot_rsp rsp; |
| 104 | int rc; |
| 105 | |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 106 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 107 | __set_MSPLIM(msp_stack_bottom); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 108 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 109 | |
Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 110 | /* Perform platform specific initialization */ |
| 111 | if (boot_platform_init() != 0) { |
| 112 | while (1) |
| 113 | ; |
| 114 | } |
| 115 | |
David Vincze | 99f1b36 | 2019-12-12 16:17:35 +0100 | [diff] [blame] | 116 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF |
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 117 | stdio_init(); |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 118 | #endif |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 119 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 120 | BOOT_LOG_INF("Starting bootloader"); |
| 121 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 122 | /* Initialise the mbedtls static memory allocator so that mbedtls allocates |
| 123 | * memory from the provided static buffer instead of from the heap. |
| 124 | */ |
| 125 | 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] | 126 | |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 127 | #ifdef CRYPTO_HW_ACCELERATOR |
| 128 | rc = crypto_hw_accelerator_init(); |
| 129 | if (rc) { |
| 130 | BOOT_LOG_ERR("Error while initializing cryptographic accelerator."); |
| 131 | while (1); |
| 132 | } |
| 133 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 134 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 135 | rc = boot_nv_security_counter_init(); |
| 136 | if (rc != 0) { |
| 137 | BOOT_LOG_ERR("Error while initializing the security counter"); |
| 138 | while (1) |
| 139 | ; |
| 140 | } |
| 141 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 142 | rc = boot_go(&rsp); |
| 143 | if (rc != 0) { |
| 144 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 145 | while (1) |
| 146 | ; |
| 147 | } |
| 148 | |
Raef Coles | 0e82adc | 2019-10-17 15:06:26 +0100 | [diff] [blame] | 149 | #ifdef CRYPTO_HW_ACCELERATOR |
| 150 | rc = crypto_hw_accelerator_finish(); |
| 151 | if (rc) { |
| 152 | BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator."); |
| 153 | while (1); |
| 154 | } |
| 155 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 156 | |
Tamas Ban | f824e74 | 2019-10-25 21:22:26 +0100 | [diff] [blame] | 157 | /* This is a workaround to program the TF-M related cryptographic keys |
| 158 | * to CC312 OTP memory. This functionality is independent from secure boot, |
| 159 | * this is usually done in the factory floor during chip manufacturing. |
| 160 | */ |
| 161 | #ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING |
| 162 | BOOT_LOG_INF("OTP provisioning started."); |
| 163 | rc = crypto_hw_accelerator_otp_provisioning(); |
| 164 | if (rc) { |
| 165 | BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc); |
| 166 | while (1); |
| 167 | } else { |
| 168 | BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded."); |
| 169 | |
| 170 | /* We don't need to boot - the only aim is provisioning. */ |
| 171 | while (1); |
| 172 | } |
| 173 | #endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */ |
| 174 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 175 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 176 | rsp.br_image_off); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 177 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 178 | do_boot(&rsp); |
| 179 | |
| 180 | BOOT_LOG_ERR("Never should get here"); |
| 181 | while (1) |
| 182 | ; |
| 183 | } |