blob: b8e4ccc98badd5ec640a9f64beb1d029eb908a55 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Summer Qindea1f2c2021-01-11 14:46:34 +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
David Huf07e97d2021-02-15 22:05:40 +08008#include "fih.h"
Ken Liu55ba01f2021-01-20 17:34:50 +08009#include "ffm/tfm_boot_data.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080010#include "compile_check_defs.h"
TTornblom83d96372019-11-19 12:53:16 +010011#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080012#include "spm_ipc.h"
Kevin Peng54d47fb2021-06-15 16:40:08 +080013#include "tfm_hal_isolation.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080014#include "tfm_hal_platform.h"
Summer Qin830c5542020-02-14 13:44:20 +080015#include "tfm_nspm.h"
16#include "tfm_spm_hal.h"
Shawn Shanf5471ba2020-09-17 17:34:50 +080017#include "tfm_spm_log.h"
Summer Qin830c5542020-02-14 13:44:20 +080018#include "tfm_version.h"
Raef Colesaefbe082021-06-18 08:53:43 +010019#include "tfm_plat_otp.h"
20#include "tfm_plat_provisioning.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000021
Miklos Balint386b8b52017-11-29 13:12:32 +000022/*
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 Peng25b190b2020-10-30 17:10:45 +080032#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 Aicb0ecf62019-07-10 18:43:51 +080034#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000035
Kevin Peng300c68d2021-08-12 17:40:17 +080036REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020037
David Huf07e97d2021-02-15 22:05:40 +080038static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000039{
Summer Qin0eb7c912020-08-19 16:08:50 +080040 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020041 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
David Huf07e97d2021-02-15 22:05:40 +080042#ifdef TFM_FIH_PROFILE_ON
43 fih_int fih_rc = FIH_FAILURE;
44#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020045
Miklos Balint386b8b52017-11-29 13:12:32 +000046 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020047 plat_err = tfm_spm_hal_enable_fault_handlers();
48 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080049 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020050 }
Miklos Balint386b8b52017-11-29 13:12:32 +000051
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010052 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020053 plat_err = tfm_spm_hal_system_reset_cfg();
54 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080055 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020056 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010057
Marc Moreno Berengued584b612018-11-26 11:46:31 +000058 /* Configures debug authentication */
David Huf07e97d2021-02-15 22:05:40 +080059#ifdef TFM_FIH_PROFILE_ON
60 FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
61 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
62 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
63 }
64#else /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020065 plat_err = tfm_spm_hal_init_debug();
66 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
67 return TFM_ERROR_GENERIC;
68 }
David Huf07e97d2021-02-15 22:05:40 +080069#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000070
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053071 /*
72 * Access to any peripheral should be performed after programming
73 * the necessary security components such as PPC/SAU.
74 */
David Huf07e97d2021-02-15 22:05:40 +080075#ifdef TFM_FIH_PROFILE_ON
76 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
77 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
78 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
79 }
80#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080081 hal_status = tfm_hal_set_up_static_boundaries();
82 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053083 return TFM_ERROR_GENERIC;
84 }
David Huf07e97d2021-02-15 22:05:40 +080085#endif /* TFM_FIH_PROFILE_ON */
86
87#ifdef TFM_FIH_PROFILE_ON
88 FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc);
89 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
90 tfm_core_panic();
91 }
92#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053093
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070094 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080095 hal_status = tfm_hal_platform_init();
96 if (hal_status != TFM_HAL_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080097 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070098 }
Miklos Balint386b8b52017-11-29 13:12:32 +000099
Raef Colesaefbe082021-06-18 08:53:43 +0100100 plat_err = tfm_plat_otp_init();
101 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
102 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
103 }
104
105 /* Perform provisioning. */
106 if (tfm_plat_provisioning_is_required()) {
107 plat_err = tfm_plat_provisioning_perform();
108 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
109 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
110 }
111 } else {
112 tfm_plat_provisioning_check_for_dummy_keys();
113 }
114
Summer Qindea1f2c2021-01-11 14:46:34 +0800115 /* Configures architecture */
116 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +0100117
Shawn Shanf5471ba2020-09-17 17:34:50 +0800118 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200119
Shawn Shanf5471ba2020-09-17 17:34:50 +0800120 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000121
Tamas Ban9ff535b2018-09-18 08:15:18 +0100122 tfm_core_validate_boot_data();
123
Miklos Balint386b8b52017-11-29 13:12:32 +0000124 configure_ns_code();
125
126 /* Configures all interrupts to retarget NS state, except for
127 * secure peripherals
128 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200129 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
130 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800131 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200132 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200133
Miklos Balint386b8b52017-11-29 13:12:32 +0000134 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200135 plat_err = tfm_spm_hal_nvic_interrupt_enable();
136 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800137 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200138 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000139
David Huf07e97d2021-02-15 22:05:40 +0800140 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000141}
142
Kevin Peng300c68d2021-08-12 17:40:17 +0800143__attribute__((naked))
Miklos Balint386b8b52017-11-29 13:12:32 +0000144int main(void)
145{
Kevin Peng300c68d2021-08-12 17:40:17 +0800146 __ASM volatile(
147 "ldr r0, =0xFEF5EDA5 \n" /* Seal Main Stack before using */
148 "ldr r1, =0xFEF5EDA5 \n"
149 "push {r0, r1} \n"
150 "bl c_main \n"
151 );
152}
153
154int c_main(void)
155{
David Huf07e97d2021-02-15 22:05:40 +0800156 fih_int fih_rc = FIH_FAILURE;
157
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200158 /* set Main Stack Pointer limit */
Kevin Peng300c68d2021-08-12 17:40:17 +0800159 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
160 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200161
David Huf07e97d2021-02-15 22:05:40 +0800162 fih_delay_init();
163
164 FIH_CALL(tfm_core_init, fih_rc);
165 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800166 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000167 }
David Huf07e97d2021-02-15 22:05:40 +0800168
Raef Coles0241dc62020-12-22 11:50:02 +0000169 /* All isolation should have been set up at this point */
170 FIH_LABEL_CRITICAL_POINT();
171
Soby Mathewc64adbc2020-03-11 12:33:44 +0000172 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800173 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000174
Edison Ai4d66dc32019-02-18 17:58:49 +0800175 /*
176 * Prioritise secure exceptions to avoid NS being able to pre-empt
177 * secure SVC or SecureFault. Do it before PSA API initialization.
178 */
Ken Liu50e21092020-10-14 16:42:15 +0800179 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800180
181 /* Move to handler mode for further SPM initialization. */
182 tfm_core_handler_mode();
Kevin Peng300c68d2021-08-12 17:40:17 +0800183
184 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000185}