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