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