Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 3 | * Copyright (c) 2017-2022 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 | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 22 | #include "mbedtls/memory_buffer_alloc.h" |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 23 | #include "bootutil/security_cnt.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" |
Raef Coles | 8efad88 | 2020-07-10 09:46:00 +0100 | [diff] [blame] | 27 | #include "bootutil/boot_record.h" |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 28 | #include "bootutil/fault_injection_hardening.h" |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 29 | #include "flash_map_backend/flash_map_backend.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 30 | #include "boot_hal.h" |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 31 | #include "uart_stdout.h" |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 32 | #include "tfm_plat_otp.h" |
| 33 | #include "tfm_plat_provisioning.h" |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 34 | #ifdef TEST_BL2 |
| 35 | #include "mcuboot_suites.h" |
| 36 | #endif /* TEST_BL2 */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 37 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 38 | /* Avoids the semihosting issue */ |
| 39 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 40 | __asm(" .global __ARM_use_no_argv\n"); |
| 41 | #endif |
| 42 | |
Balint Matyi | 5c47631 | 2020-03-31 13:15:39 +0100 | [diff] [blame] | 43 | #ifdef MCUBOOT_ENCRYPT_RSA |
Sherry Zhang | c7baf59 | 2021-07-15 14:54:17 +0800 | [diff] [blame] | 44 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x3000 |
Balint Matyi | 5c47631 | 2020-03-31 13:15:39 +0100 | [diff] [blame] | 45 | #else |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 46 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 |
Balint Matyi | 5c47631 | 2020-03-31 13:15:39 +0100 | [diff] [blame] | 47 | #endif |
| 48 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 49 | /* Static buffer to be used by mbedtls for memory allocation */ |
| 50 | static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; |
Raef Coles | 7763a47 | 2022-11-10 17:11:40 +0000 | [diff] [blame] | 51 | struct boot_rsp rsp; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 52 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 53 | static void do_boot(struct boot_rsp *rsp) |
| 54 | { |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 55 | struct boot_arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 56 | uintptr_t flash_base; |
| 57 | int rc; |
| 58 | |
| 59 | /* The beginning of the image is the ARM vector table, containing |
| 60 | * the initial stack pointer address and the reset vector |
| 61 | * consecutively. Manually set the stack pointer and jump into the |
| 62 | * reset vector |
| 63 | */ |
| 64 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 65 | assert(rc == 0); |
| 66 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 67 | if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 68 | /* The image has been copied to SRAM, find the vector table |
| 69 | * at the load address instead of image's address in flash |
| 70 | */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 71 | vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr + |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 72 | rsp->br_hdr->ih_hdr_size); |
| 73 | } else { |
| 74 | /* Using the flash address as not executing in SRAM */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 75 | vt = (struct boot_arm_vector_table *)(flash_base + |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 76 | rsp->br_image_off + |
| 77 | rsp->br_hdr->ih_hdr_size); |
| 78 | } |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame] | 79 | |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 80 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF || TEST_BL2 |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 81 | stdio_uninit(); |
David Vincze | 73dfbc5 | 2019-10-11 13:54:58 +0200 | [diff] [blame] | 82 | #endif |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 83 | |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 84 | /* This function never returns, because it calls the secure application |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 85 | * Reset_Handler(). |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 86 | */ |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame] | 87 | boot_platform_quit(vt); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 88 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 89 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 90 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | { |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 92 | fih_int fih_rc = FIH_FAILURE; |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 93 | enum tfm_plat_err_t plat_err; |
Raef Coles | 5541049 | 2022-02-01 12:34:37 +0000 | [diff] [blame] | 94 | int32_t image_id; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 95 | |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 96 | /* Initialise the mbedtls static memory allocator so that mbedtls allocates |
| 97 | * memory from the provided static buffer instead of from the heap. |
| 98 | */ |
| 99 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN); |
| 100 | |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 101 | #if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF || TEST_BL2 |
David Vincze | 7d591a6 | 2020-09-23 18:25:56 +0200 | [diff] [blame] | 102 | stdio_init(); |
David Hu | 5cc9a3f | 2019-06-14 13:10:40 +0800 | [diff] [blame] | 103 | #endif |
David Vincze | e0a3c2f | 2019-05-15 16:45:14 +0200 | [diff] [blame] | 104 | |
Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 105 | /* Perform platform specific initialization */ |
| 106 | if (boot_platform_init() != 0) { |
Tamas Ban | 37aedb5 | 2020-10-01 10:54:48 +0100 | [diff] [blame] | 107 | BOOT_LOG_ERR("Platform init failed"); |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 108 | FIH_PANIC; |
Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 111 | BOOT_LOG_INF("Starting bootloader"); |
| 112 | |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 113 | plat_err = tfm_plat_otp_init(); |
| 114 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 115 | BOOT_LOG_ERR("OTP system initialization failed"); |
| 116 | FIH_PANIC; |
| 117 | } |
| 118 | |
| 119 | if (tfm_plat_provisioning_is_required()) { |
| 120 | plat_err = tfm_plat_provisioning_perform(); |
| 121 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 122 | BOOT_LOG_ERR("Provisioning failed"); |
| 123 | FIH_PANIC; |
| 124 | } |
| 125 | } else { |
| 126 | tfm_plat_provisioning_check_for_dummy_keys(); |
| 127 | } |
| 128 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 129 | FIH_CALL(boot_nv_security_counter_init, fih_rc); |
| 130 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 131 | BOOT_LOG_ERR("Error while initializing the security counter"); |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 132 | FIH_PANIC; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Raef Coles | 630d0b8 | 2021-04-26 11:08:43 +0100 | [diff] [blame] | 135 | /* Perform platform specific post-initialization */ |
| 136 | if (boot_platform_post_init() != 0) { |
| 137 | BOOT_LOG_ERR("Platform post init failed"); |
| 138 | FIH_PANIC; |
| 139 | } |
| 140 | |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 141 | #ifdef TEST_BL2 |
Raef Coles | 3867593 | 2022-01-18 12:56:40 +0000 | [diff] [blame] | 142 | (void)run_mcuboot_testsuite(); |
Raef Coles | f77cc17 | 2022-01-07 11:05:47 +0000 | [diff] [blame] | 143 | #endif /* TEST_BL2 */ |
| 144 | |
Raef Coles | 5541049 | 2022-02-01 12:34:37 +0000 | [diff] [blame] | 145 | /* Images are loaded in reverse order so that the last image loaded is the |
| 146 | * TF-M image, which means the response is filled correctly. |
| 147 | */ |
| 148 | for (image_id = MCUBOOT_IMAGE_NUMBER - 1; image_id >= 0; image_id--) { |
| 149 | if (boot_platform_pre_load(image_id)) { |
| 150 | BOOT_LOG_ERR("Pre-load step for image %d failed", image_id); |
| 151 | FIH_PANIC; |
| 152 | } |
| 153 | |
| 154 | FIH_CALL(boot_go_for_image_id, fih_rc, &rsp, image_id); |
| 155 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 156 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 157 | FIH_PANIC; |
| 158 | } |
| 159 | |
| 160 | if (boot_platform_post_load(image_id)) { |
| 161 | BOOT_LOG_ERR("Post-load step for image %d failed", image_id); |
| 162 | FIH_PANIC; |
| 163 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 167 | rsp.br_image_off); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 168 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 169 | do_boot(&rsp); |
| 170 | |
| 171 | BOOT_LOG_ERR("Never should get here"); |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame] | 172 | FIH_PANIC; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 173 | } |