Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 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 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 8 | #include "fih.h" |
Ken Liu | 55ba01f | 2021-01-20 17:34:50 +0800 | [diff] [blame] | 9 | #include "ffm/tfm_boot_data.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 10 | #include "compile_check_defs.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_ipc.h" |
Kevin Peng | 54d47fb | 2021-06-15 16:40:08 +0800 | [diff] [blame] | 13 | #include "tfm_hal_isolation.h" |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 14 | #include "tfm_hal_platform.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 15 | #include "tfm_nspm.h" |
| 16 | #include "tfm_spm_hal.h" |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 17 | #include "tfm_spm_log.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 18 | #include "tfm_version.h" |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 19 | #include "tfm_plat_otp.h" |
| 20 | #include "tfm_plat_provisioning.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 21 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 22 | /* |
| 23 | * Avoids the semihosting issue |
| 24 | * FixMe: describe 'semihosting issue' |
| 25 | */ |
| 26 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 27 | __asm(" .global __ARM_use_no_argv\n"); |
| 28 | #endif |
| 29 | |
| 30 | #ifndef TFM_LVL |
| 31 | #error TFM_LVL is not defined! |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 32 | #elif (TFM_LVL != 1) && (TFM_LVL != 2) && (TFM_LVL != 3) |
| 33 | #error Invalid TFM_LVL value. Only TFM_LVL 1, 2 and 3 are supported in IPC model! |
Edison Ai | cb0ecf6 | 2019-07-10 18:43:51 +0800 | [diff] [blame] | 34 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 35 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 36 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 37 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 38 | static fih_int tfm_core_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 39 | { |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 40 | enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR; |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 41 | #ifdef TFM_FIH_PROFILE_ON |
| 42 | fih_int fih_rc = FIH_FAILURE; |
Kevin Peng | c855573 | 2021-09-24 15:15:21 +0800 | [diff] [blame^] | 43 | #else |
| 44 | enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC; |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 45 | #endif |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 46 | |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 47 | /* |
| 48 | * Access to any peripheral should be performed after programming |
| 49 | * the necessary security components such as PPC/SAU. |
| 50 | */ |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 51 | #ifdef TFM_FIH_PROFILE_ON |
| 52 | FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc); |
| 53 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 54 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 55 | } |
| 56 | #else /* TFM_FIH_PROFILE_ON */ |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 57 | hal_status = tfm_hal_set_up_static_boundaries(); |
| 58 | if (hal_status != TFM_HAL_SUCCESS) { |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 59 | return TFM_ERROR_GENERIC; |
| 60 | } |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 61 | #endif /* TFM_FIH_PROFILE_ON */ |
| 62 | |
| 63 | #ifdef TFM_FIH_PROFILE_ON |
| 64 | FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc); |
| 65 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) { |
| 66 | tfm_core_panic(); |
| 67 | } |
| 68 | #endif |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 69 | |
Kevin Peng | c855573 | 2021-09-24 15:15:21 +0800 | [diff] [blame^] | 70 | #ifdef TFM_FIH_PROFILE_ON |
| 71 | FIH_CALL(tfm_hal_platform_init, fih_rc); |
| 72 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 73 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 74 | } |
Kevin Peng | c855573 | 2021-09-24 15:15:21 +0800 | [diff] [blame^] | 75 | #else /* TFM_FIH_PROFILE_ON */ |
| 76 | hal_status = tfm_hal_platform_init(); |
| 77 | if (hal_status != TFM_HAL_SUCCESS) { |
| 78 | return TFM_ERROR_GENERIC; |
| 79 | } |
| 80 | #endif /* TFM_FIH_PROFILE_ON */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 81 | |
Raef Coles | aefbe08 | 2021-06-18 08:53:43 +0100 | [diff] [blame] | 82 | plat_err = tfm_plat_otp_init(); |
| 83 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 84 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 85 | } |
| 86 | |
| 87 | /* Perform provisioning. */ |
| 88 | if (tfm_plat_provisioning_is_required()) { |
| 89 | plat_err = tfm_plat_provisioning_perform(); |
| 90 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 91 | FIH_RET(fih_int_encode(TFM_ERROR_GENERIC)); |
| 92 | } |
| 93 | } else { |
| 94 | tfm_plat_provisioning_check_for_dummy_keys(); |
| 95 | } |
| 96 | |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 97 | /* Configures architecture */ |
| 98 | tfm_arch_config_extensions(); |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 99 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 100 | 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] | 101 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 102 | SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 103 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 104 | tfm_core_validate_boot_data(); |
| 105 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 106 | configure_ns_code(); |
| 107 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 108 | FIH_RET(fih_int_encode(TFM_SUCCESS)); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 111 | __attribute__((naked)) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 112 | int main(void) |
| 113 | { |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 114 | __ASM volatile( |
| 115 | "ldr r0, =0xFEF5EDA5 \n" /* Seal Main Stack before using */ |
| 116 | "ldr r1, =0xFEF5EDA5 \n" |
| 117 | "push {r0, r1} \n" |
| 118 | "bl c_main \n" |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | int c_main(void) |
| 123 | { |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 124 | fih_int fih_rc = FIH_FAILURE; |
| 125 | |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 126 | /* set Main Stack Pointer limit */ |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 127 | tfm_arch_set_msplim((uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK, |
| 128 | $$ZI$$Base)); |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 129 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 130 | fih_delay_init(); |
| 131 | |
| 132 | FIH_CALL(tfm_core_init, fih_rc); |
| 133 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 134 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 135 | } |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 136 | |
Raef Coles | 0241dc6 | 2020-12-22 11:50:02 +0000 | [diff] [blame] | 137 | /* All isolation should have been set up at this point */ |
| 138 | FIH_LABEL_CRITICAL_POINT(); |
| 139 | |
Soby Mathew | c64adbc | 2020-03-11 12:33:44 +0000 | [diff] [blame] | 140 | /* Print the TF-M version */ |
Shawn Shan | 45578e9 | 2020-10-19 17:50:02 +0800 | [diff] [blame] | 141 | SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n"); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 142 | |
Edison Ai | 4d66dc3 | 2019-02-18 17:58:49 +0800 | [diff] [blame] | 143 | /* |
| 144 | * Prioritise secure exceptions to avoid NS being able to pre-empt |
| 145 | * secure SVC or SecureFault. Do it before PSA API initialization. |
| 146 | */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 147 | tfm_arch_set_secure_exception_priorities(); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 148 | |
| 149 | /* Move to handler mode for further SPM initialization. */ |
| 150 | tfm_core_handler_mode(); |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 151 | |
| 152 | return 0; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 153 | } |