Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Summer Qin | 9c1fba1 | 2020-08-12 15:49:12 +0800 | [diff] [blame] | 8 | #include "arch.h" |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 9 | #include "fih.h" |
Ken Liu | 55ba01f | 2021-01-20 17:34:50 +0800 | [diff] [blame] | 10 | #include "ffm/tfm_boot_data.h" |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 11 | #include "region.h" |
Summer Qin | f993cd4 | 2020-08-12 16:55:17 +0800 | [diff] [blame] | 12 | #include "spm_func.h" |
Mingyang Sun | 9763dee | 2020-12-07 10:45:17 +0800 | [diff] [blame] | 13 | #include "tfm_hal_defs.h" |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 14 | #include "tfm_hal_platform.h" |
Håkon Øye Amundsen | cf79394 | 2021-01-14 10:50:49 +0100 | [diff] [blame] | 15 | #include "tfm_hal_isolation.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 16 | #include "tfm_irq_list.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 17 | #include "tfm_spm_hal.h" |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 18 | #include "tfm_spm_log.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 19 | #include "tfm_version.h" |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 20 | #include "tfm_plat_otp.h" |
| 21 | #include "tfm_plat_provisioning.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 22 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Avoids the semihosting issue |
| 25 | * FixMe: describe 'semihosting issue' |
| 26 | */ |
| 27 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 28 | __asm(" .global __ARM_use_no_argv\n"); |
| 29 | #endif |
| 30 | |
| 31 | #ifndef TFM_LVL |
| 32 | #error TFM_LVL is not defined! |
Summer Qin | f993cd4 | 2020-08-12 16:55:17 +0800 | [diff] [blame] | 33 | #elif (TFM_LVL != 1) |
Edison Ai | cb0ecf6 | 2019-07-10 18:43:51 +0800 | [diff] [blame] | 34 | #error Only TFM_LVL 1 is supported for library model! |
| 35 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 36 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 37 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
Michel Jaouen | f373efb | 2021-09-17 15:36:19 +0200 | [diff] [blame] | 38 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Limit)[]; |
| 39 | REGION_DECLARE(Image$$, ER_INITIAL_PSP, $$ZI$$Limit)[]; |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 40 | |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 41 | static void configure_ns_code(void) |
| 42 | { |
| 43 | /* SCB_NS.VTOR points to the Non-secure vector table base address */ |
| 44 | SCB_NS->VTOR = tfm_spm_hal_get_ns_VTOR(); |
| 45 | |
| 46 | /* Setups Main stack pointer of the non-secure code */ |
| 47 | uint32_t ns_msp = tfm_spm_hal_get_ns_MSP(); |
| 48 | |
| 49 | __TZ_set_MSP_NS(ns_msp); |
| 50 | |
| 51 | /* Get the address of non-secure code entry point to jump there */ |
| 52 | uint32_t entry_ptr = tfm_spm_hal_get_ns_entry_point(); |
| 53 | |
| 54 | /* Clears LSB of the function address to indicate the function-call |
| 55 | * will perform the switch from secure to non-secure |
| 56 | */ |
| 57 | ns_entry = (nsfptr_t)cmse_nsfptr_create(entry_ptr); |
| 58 | } |
| 59 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 60 | static fih_int tfm_core_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 61 | { |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 62 | size_t i; |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 63 | enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC; |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 64 | enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR; |
| 65 | enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE; |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 66 | #ifdef TFM_FIH_PROFILE_ON |
| 67 | fih_int fih_rc = FIH_FAILURE; |
| 68 | #endif |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 69 | |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 70 | /* |
| 71 | * Access to any peripheral should be performed after programming |
| 72 | * the necessary security components such as PPC/SAU. |
| 73 | */ |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 74 | #ifdef TFM_FIH_PROFILE_ON |
| 75 | FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc); |
| 76 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 77 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 78 | } |
| 79 | #else /* TFM_FIH_PROFILE_ON */ |
Mingyang Sun | 9763dee | 2020-12-07 10:45:17 +0800 | [diff] [blame] | 80 | hal_status = tfm_hal_set_up_static_boundaries(); |
| 81 | if (hal_status != TFM_HAL_SUCCESS) { |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 82 | return TFM_ERROR_GENERIC; |
| 83 | } |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 84 | #endif /* TFM_FIH_PROFILE_ON */ |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 85 | |
Kevin Peng | c855573 | 2021-09-24 15:15:21 +0800 | [diff] [blame] | 86 | #ifdef TFM_FIH_PROFILE_ON |
| 87 | FIH_CALL(tfm_hal_platform_init, fih_rc); |
| 88 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 89 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 90 | } |
Kevin Peng | c855573 | 2021-09-24 15:15:21 +0800 | [diff] [blame] | 91 | #else /* TFM_FIH_PROFILE_ON */ |
| 92 | hal_status = tfm_hal_platform_init(); |
| 93 | if (hal_status != TFM_HAL_SUCCESS) { |
| 94 | return TFM_ERROR_GENERIC; |
| 95 | } |
| 96 | #endif /* TFM_FIH_PROFILE_ON */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 97 | |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 98 | plat_err = tfm_plat_otp_init(); |
| 99 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 100 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 101 | } |
| 102 | |
| 103 | /* Perform provisioning. */ |
| 104 | if (tfm_plat_provisioning_is_required()) { |
| 105 | plat_err = tfm_plat_provisioning_perform(); |
| 106 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 107 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 108 | } |
| 109 | } else { |
| 110 | tfm_plat_provisioning_check_for_dummy_keys(); |
| 111 | } |
| 112 | |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 113 | /* Configures architecture */ |
| 114 | tfm_arch_config_extensions(); |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 115 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 116 | SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n"); |
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 117 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 118 | SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 119 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 120 | tfm_core_validate_boot_data(); |
| 121 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 122 | configure_ns_code(); |
| 123 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 124 | for (i = 0; i < tfm_core_irq_signals_count; ++i) { |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 125 | plat_err = tfm_spm_hal_set_secure_irq_priority( |
Kevin Peng | 0979b0e | 2021-06-15 10:54:53 +0800 | [diff] [blame] | 126 | tfm_core_irq_signals[i].irq_line); |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 127 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 128 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 129 | } |
| 130 | irq_target_state = tfm_spm_hal_set_irq_target_state( |
| 131 | tfm_core_irq_signals[i].irq_line, |
| 132 | TFM_IRQ_TARGET_STATE_SECURE); |
| 133 | if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) { |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 134 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 135 | } |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 136 | } |
| 137 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 138 | FIH_RET(fih_int_encode(TFM_SUCCESS)); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 141 | __attribute__((naked)) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 142 | int main(void) |
| 143 | { |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 144 | __ASM volatile( |
| 145 | #if !defined(__ICCARM__) |
| 146 | ".syntax unified \n" |
| 147 | #endif |
Michel Jaouen | f373efb | 2021-09-17 15:36:19 +0200 | [diff] [blame] | 148 | "msr msp, %0 \n" |
| 149 | "msr psp, %1 \n" |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 150 | "mrs r0, control \n" |
| 151 | "movs r1, #2 \n" |
| 152 | "orrs r0, r0, r1 \n" /* Switch to PSP */ |
| 153 | "msr control, r0 \n" |
| 154 | "bl c_main \n" |
Michel Jaouen | f373efb | 2021-09-17 15:36:19 +0200 | [diff] [blame] | 155 | : |
| 156 | : "r" (REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Limit)), |
| 157 | "r" (REGION_NAME(Image$$, ER_INITIAL_PSP, $$ZI$$Limit)) |
| 158 | : "r0", "memory" |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 159 | ); |
| 160 | } |
| 161 | |
| 162 | int c_main(void) |
| 163 | { |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 164 | enum spm_err_t spm_err = SPM_ERR_GENERIC_ERR; |
| 165 | fih_int fih_rc = FIH_FAILURE; |
| 166 | |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 167 | /* set Main Stack Pointer limit */ |
Ken Liu | 05e13ba | 2020-07-25 10:31:33 +0800 | [diff] [blame] | 168 | tfm_arch_init_secure_msp((uint32_t)®ION_NAME(Image$$, |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 169 | ARM_LIB_STACK, |
Ken Liu | 05e13ba | 2020-07-25 10:31:33 +0800 | [diff] [blame] | 170 | $$ZI$$Base)); |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 171 | |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 172 | /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */ |
| 173 | tfm_spm_seal_psp_stacks(); |
| 174 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 175 | fih_delay_init(); |
| 176 | |
| 177 | FIH_CALL(tfm_core_init, fih_rc); |
| 178 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 179 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 180 | } |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 181 | |
Raef Coles | 0241dc6 | 2020-12-22 11:50:02 +0000 | [diff] [blame] | 182 | /* All isolation should have been set up at this point */ |
| 183 | FIH_LABEL_CRITICAL_POINT(); |
| 184 | |
Soby Mathew | c64adbc | 2020-03-11 12:33:44 +0000 | [diff] [blame] | 185 | /* Print the TF-M version */ |
Anton Komlev | 2d2a6fc | 2022-02-20 15:47:53 +0000 | [diff] [blame^] | 186 | SPMLOG_INFMSG("\033[1;34mBooting TF-M "VERSION_FULLSTR"\033[0m\r\n"); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 187 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 188 | spm_err = tfm_spm_db_init(); |
| 189 | if (spm_err != SPM_ERR_OK) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 190 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 191 | } |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 192 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 193 | tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 194 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 195 | REGION_DECLARE(Image$$, ER_INITIAL_PSP, $$ZI$$Base)[]; |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 196 | uint32_t psp_stack_bottom = |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 197 | (uint32_t)REGION_NAME(Image$$, ER_INITIAL_PSP, $$ZI$$Base); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 198 | |
David Hu | e05b6a6 | 2019-06-12 18:45:28 +0800 | [diff] [blame] | 199 | tfm_arch_set_psplim(psp_stack_bottom); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 200 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 201 | FIH_CALL(tfm_spm_partition_init, fih_rc); |
| 202 | if (fih_not_eq(fih_rc, fih_int_encode(SPM_ERR_OK))) { |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 203 | /* Certain systems might refuse to boot altogether if partitions fail |
| 204 | * to initialize. This is a placeholder for such an error handler |
| 205 | */ |
| 206 | } |
| 207 | |
Ken Liu | 96714b3 | 2019-04-08 15:10:39 +0800 | [diff] [blame] | 208 | /* |
| 209 | * Prioritise secure exceptions to avoid NS being able to pre-empt |
| 210 | * secure SVC or SecureFault. Do it before PSA API initialization. |
| 211 | */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 212 | tfm_arch_set_secure_exception_priorities(); |
Ken Liu | 96714b3 | 2019-04-08 15:10:39 +0800 | [diff] [blame] | 213 | |
Edison Ai | 4d66dc3 | 2019-02-18 17:58:49 +0800 | [diff] [blame] | 214 | /* We close the TFM_SP_CORE_ID partition, because its only purpose is |
| 215 | * to be able to pass the state checks for the tests started from secure. |
| 216 | */ |
| 217 | tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED); |
| 218 | tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID, |
| 219 | SPM_PARTITION_STATE_RUNNING); |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 220 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 221 | #ifdef TFM_FIH_PROFILE_ON |
Kevin Peng | 38788a1 | 2021-09-08 16:23:50 +0800 | [diff] [blame] | 222 | FIH_CALL(tfm_hal_verify_static_boundaries, fih_rc); |
| 223 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 224 | tfm_core_panic(); |
| 225 | } |
| 226 | #endif |
| 227 | |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 228 | #ifdef TFM_CORE_DEBUG |
| 229 | /* Jumps to non-secure code */ |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 230 | SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n"); |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 231 | #endif |
| 232 | |
| 233 | jump_to_ns_code(); |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 234 | |
| 235 | return 0; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 236 | } |