blob: d8dadbc31a2865abeeee8408eaee377237cca92b [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Ken Liu55ba01f2021-01-20 17:34:50 +08002 * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "arch.h"
David Hu9804b6a2021-02-15 21:23:06 +08009#include "fih.h"
Ken Liu55ba01f2021-01-20 17:34:50 +080010#include "ffm/tfm_boot_data.h"
TTornblom83d96372019-11-19 12:53:16 +010011#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080012#include "spm_func.h"
Mingyang Sun9763dee2020-12-07 10:45:17 +080013#include "tfm_hal_defs.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080014#include "tfm_hal_platform.h"
Håkon Øye Amundsencf793942021-01-14 10:50:49 +010015#include "tfm_hal_isolation.h"
Summer Qin830c5542020-02-14 13:44:20 +080016#include "tfm_irq_list.h"
17#include "tfm_nspm.h"
18#include "tfm_spm_hal.h"
Shawn Shanf5471ba2020-09-17 17:34:50 +080019#include "tfm_spm_log.h"
Summer Qin830c5542020-02-14 13:44:20 +080020#include "tfm_version.h"
Raef Colesaefbe082021-06-18 08:53:43 +010021#include "tfm_plat_otp.h"
22#include "tfm_plat_provisioning.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000023
Miklos Balint386b8b52017-11-29 13:12:32 +000024/*
25 * Avoids the semihosting issue
26 * FixMe: describe 'semihosting issue'
27 */
28#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
29__asm(" .global __ARM_use_no_argv\n");
30#endif
31
32#ifndef TFM_LVL
33#error TFM_LVL is not defined!
Summer Qinf993cd42020-08-12 16:55:17 +080034#elif (TFM_LVL != 1)
Edison Aicb0ecf62019-07-10 18:43:51 +080035#error Only TFM_LVL 1 is supported for library model!
36#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000037
Kevin Peng300c68d2021-08-12 17:40:17 +080038REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Michel Jaouenf373efb2021-09-17 15:36:19 +020039REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Limit)[];
40REGION_DECLARE(Image$$, ER_INITIAL_PSP, $$ZI$$Limit)[];
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020041
David Hu9804b6a2021-02-15 21:23:06 +080042static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000043{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020044 size_t i;
Summer Qin0eb7c912020-08-19 16:08:50 +080045 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020046 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
47 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
David Hu9804b6a2021-02-15 21:23:06 +080048#ifdef TFM_FIH_PROFILE_ON
49 fih_int fih_rc = FIH_FAILURE;
50#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020051
Miklos Balint386b8b52017-11-29 13:12:32 +000052 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020053 plat_err = tfm_spm_hal_enable_fault_handlers();
54 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +080055 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020056 }
Miklos Balint386b8b52017-11-29 13:12:32 +000057
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010058 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020059 plat_err = tfm_spm_hal_system_reset_cfg();
60 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +080061 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020062 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010063
Marc Moreno Berengued584b612018-11-26 11:46:31 +000064 /* Configures debug authentication */
David Hu9804b6a2021-02-15 21:23:06 +080065#ifdef TFM_FIH_PROFILE_ON
66 FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
67 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
68 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
69 }
70#else /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020071 plat_err = tfm_spm_hal_init_debug();
72 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
73 return TFM_ERROR_GENERIC;
74 }
David Hu9804b6a2021-02-15 21:23:06 +080075#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000076
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053077 /*
78 * Access to any peripheral should be performed after programming
79 * the necessary security components such as PPC/SAU.
80 */
David Hu9804b6a2021-02-15 21:23:06 +080081#ifdef TFM_FIH_PROFILE_ON
82 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
83 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
84 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
85 }
86#else /* TFM_FIH_PROFILE_ON */
Mingyang Sun9763dee2020-12-07 10:45:17 +080087 hal_status = tfm_hal_set_up_static_boundaries();
88 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053089 return TFM_ERROR_GENERIC;
90 }
David Hu9804b6a2021-02-15 21:23:06 +080091#endif /* TFM_FIH_PROFILE_ON */
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053092
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070093 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080094 hal_status = tfm_hal_platform_init();
95 if (hal_status != TFM_HAL_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +080096 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070097 }
Miklos Balint386b8b52017-11-29 13:12:32 +000098
Raef Colesaefbe082021-06-18 08:53:43 +010099 plat_err = tfm_plat_otp_init();
100 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
101 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
102 }
103
104 /* Perform provisioning. */
105 if (tfm_plat_provisioning_is_required()) {
106 plat_err = tfm_plat_provisioning_perform();
107 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
108 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
109 }
110 } else {
111 tfm_plat_provisioning_check_for_dummy_keys();
112 }
113
Summer Qindea1f2c2021-01-11 14:46:34 +0800114 /* Configures architecture */
115 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +0100116
Shawn Shanf5471ba2020-09-17 17:34:50 +0800117 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200118
Shawn Shanf5471ba2020-09-17 17:34:50 +0800119 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000120
Tamas Ban9ff535b2018-09-18 08:15:18 +0100121 tfm_core_validate_boot_data();
122
Miklos Balint386b8b52017-11-29 13:12:32 +0000123 configure_ns_code();
124
125 /* Configures all interrupts to retarget NS state, except for
126 * secure peripherals
127 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200128 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
129 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +0800130 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200131 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200132
133 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200134 plat_err = tfm_spm_hal_set_secure_irq_priority(
Kevin Peng0979b0e2021-06-15 10:54:53 +0800135 tfm_core_irq_signals[i].irq_line);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200136 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +0800137 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200138 }
139 irq_target_state = tfm_spm_hal_set_irq_target_state(
140 tfm_core_irq_signals[i].irq_line,
141 TFM_IRQ_TARGET_STATE_SECURE);
142 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
David Hu9804b6a2021-02-15 21:23:06 +0800143 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200144 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200145 }
146
Miklos Balint386b8b52017-11-29 13:12:32 +0000147 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200148 plat_err = tfm_spm_hal_nvic_interrupt_enable();
149 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Hu9804b6a2021-02-15 21:23:06 +0800150 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200151 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000152
David Hu9804b6a2021-02-15 21:23:06 +0800153 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000154}
155
Kevin Peng300c68d2021-08-12 17:40:17 +0800156__attribute__((naked))
Miklos Balint386b8b52017-11-29 13:12:32 +0000157int main(void)
158{
Kevin Peng300c68d2021-08-12 17:40:17 +0800159 __ASM volatile(
160#if !defined(__ICCARM__)
161 ".syntax unified \n"
162#endif
Michel Jaouenf373efb2021-09-17 15:36:19 +0200163 "msr msp, %0 \n"
164 "msr psp, %1 \n"
Kevin Peng300c68d2021-08-12 17:40:17 +0800165 "mrs r0, control \n"
166 "movs r1, #2 \n"
167 "orrs r0, r0, r1 \n" /* Switch to PSP */
168 "msr control, r0 \n"
169 "bl c_main \n"
Michel Jaouenf373efb2021-09-17 15:36:19 +0200170 :
171 : "r" (REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Limit)),
172 "r" (REGION_NAME(Image$$, ER_INITIAL_PSP, $$ZI$$Limit))
173 : "r0", "memory"
Kevin Peng300c68d2021-08-12 17:40:17 +0800174 );
175}
176
177int c_main(void)
178{
David Hu9804b6a2021-02-15 21:23:06 +0800179 enum spm_err_t spm_err = SPM_ERR_GENERIC_ERR;
180 fih_int fih_rc = FIH_FAILURE;
181
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200182 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800183 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$,
Kevin Peng300c68d2021-08-12 17:40:17 +0800184 ARM_LIB_STACK,
Ken Liu05e13ba2020-07-25 10:31:33 +0800185 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200186
Soby Mathew960521a2020-09-29 12:48:50 +0100187 /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */
188 tfm_spm_seal_psp_stacks();
189
David Hu9804b6a2021-02-15 21:23:06 +0800190 fih_delay_init();
191
192 FIH_CALL(tfm_core_init, fih_rc);
193 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800194 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000195 }
David Hu9804b6a2021-02-15 21:23:06 +0800196
Raef Coles0241dc62020-12-22 11:50:02 +0000197 /* All isolation should have been set up at this point */
198 FIH_LABEL_CRITICAL_POINT();
199
Soby Mathewc64adbc2020-03-11 12:33:44 +0000200 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800201 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000202
David Hu9804b6a2021-02-15 21:23:06 +0800203 spm_err = tfm_spm_db_init();
204 if (spm_err != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800205 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000206 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200207
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100208 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100209
Kevin Peng300c68d2021-08-12 17:40:17 +0800210 REGION_DECLARE(Image$$, ER_INITIAL_PSP, $$ZI$$Base)[];
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200211 uint32_t psp_stack_bottom =
Kevin Peng300c68d2021-08-12 17:40:17 +0800212 (uint32_t)REGION_NAME(Image$$, ER_INITIAL_PSP, $$ZI$$Base);
Miklos Balint386b8b52017-11-29 13:12:32 +0000213
David Hue05b6a62019-06-12 18:45:28 +0800214 tfm_arch_set_psplim(psp_stack_bottom);
Miklos Balint386b8b52017-11-29 13:12:32 +0000215
David Hu9804b6a2021-02-15 21:23:06 +0800216 FIH_CALL(tfm_spm_partition_init, fih_rc);
217 if (fih_not_eq(fih_rc, fih_int_encode(SPM_ERR_OK))) {
Miklos Balint6a139ae2018-04-04 19:44:37 +0200218 /* Certain systems might refuse to boot altogether if partitions fail
219 * to initialize. This is a placeholder for such an error handler
220 */
221 }
222
Ken Liu96714b32019-04-08 15:10:39 +0800223 /*
224 * Prioritise secure exceptions to avoid NS being able to pre-empt
225 * secure SVC or SecureFault. Do it before PSA API initialization.
226 */
Ken Liu50e21092020-10-14 16:42:15 +0800227 tfm_arch_set_secure_exception_priorities();
Ken Liu96714b32019-04-08 15:10:39 +0800228
Edison Ai4d66dc32019-02-18 17:58:49 +0800229 /* We close the TFM_SP_CORE_ID partition, because its only purpose is
230 * to be able to pass the state checks for the tests started from secure.
231 */
232 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED);
233 tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID,
234 SPM_PARTITION_STATE_RUNNING);
Edison Ai4dcae6f2019-03-18 10:13:47 +0800235
David Hu9804b6a2021-02-15 21:23:06 +0800236#ifdef TFM_FIH_PROFILE_ON
237 FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc);
238 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
239 tfm_core_panic();
240 }
241#endif
242
Edison Ai4dcae6f2019-03-18 10:13:47 +0800243#ifdef TFM_CORE_DEBUG
244 /* Jumps to non-secure code */
Shawn Shanf5471ba2020-09-17 17:34:50 +0800245 SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
Edison Ai4dcae6f2019-03-18 10:13:47 +0800246#endif
247
248 jump_to_ns_code();
Kevin Peng300c68d2021-08-12 17:40:17 +0800249
250 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000251}