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