blob: 7019b33d9747e278462aae1c0a291c4e87435e2f [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"
Summer Qin0eb7c912020-08-19 16:08:50 +080013#include "tfm_hal_platform.h"
Summer Qin830c5542020-02-14 13:44:20 +080014#include "tfm_nspm.h"
15#include "tfm_spm_hal.h"
Shawn Shanf5471ba2020-09-17 17:34:50 +080016#include "tfm_spm_log.h"
Summer Qin830c5542020-02-14 13:44:20 +080017#include "tfm_version.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000018
Miklos Balint386b8b52017-11-29 13:12:32 +000019/*
20 * Avoids the semihosting issue
21 * FixMe: describe 'semihosting issue'
22 */
23#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
24__asm(" .global __ARM_use_no_argv\n");
25#endif
26
27#ifndef TFM_LVL
28#error TFM_LVL is not defined!
Kevin Peng25b190b2020-10-30 17:10:45 +080029#elif (TFM_LVL != 1) && (TFM_LVL != 2) && (TFM_LVL != 3)
30#error Invalid TFM_LVL value. Only TFM_LVL 1, 2 and 3 are supported in IPC model!
Edison Aicb0ecf62019-07-10 18:43:51 +080031#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000032
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020033REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
34
David Huf07e97d2021-02-15 22:05:40 +080035static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000036{
Summer Qin0eb7c912020-08-19 16:08:50 +080037 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020038 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
David Huf07e97d2021-02-15 22:05:40 +080039#ifdef TFM_FIH_PROFILE_ON
40 fih_int fih_rc = FIH_FAILURE;
41#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020042
Miklos Balint386b8b52017-11-29 13:12:32 +000043 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020044 plat_err = tfm_spm_hal_enable_fault_handlers();
45 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080046 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020047 }
Miklos Balint386b8b52017-11-29 13:12:32 +000048
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010049 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020050 plat_err = tfm_spm_hal_system_reset_cfg();
51 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080052 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020053 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010054
Marc Moreno Berengued584b612018-11-26 11:46:31 +000055 /* Configures debug authentication */
David Huf07e97d2021-02-15 22:05:40 +080056#ifdef TFM_FIH_PROFILE_ON
57 FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
58 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
59 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
60 }
61#else /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020062 plat_err = tfm_spm_hal_init_debug();
63 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
64 return TFM_ERROR_GENERIC;
65 }
David Huf07e97d2021-02-15 22:05:40 +080066#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000067
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053068 /*
69 * Access to any peripheral should be performed after programming
70 * the necessary security components such as PPC/SAU.
71 */
David Huf07e97d2021-02-15 22:05:40 +080072#ifdef TFM_FIH_PROFILE_ON
73 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
74 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
75 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
76 }
77#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080078 hal_status = tfm_hal_set_up_static_boundaries();
79 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053080 return TFM_ERROR_GENERIC;
81 }
David Huf07e97d2021-02-15 22:05:40 +080082#endif /* TFM_FIH_PROFILE_ON */
83
84#ifdef TFM_FIH_PROFILE_ON
85 FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc);
86 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
87 tfm_core_panic();
88 }
89#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053090
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070091 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080092 hal_status = tfm_hal_platform_init();
93 if (hal_status != TFM_HAL_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080094 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070095 }
Miklos Balint386b8b52017-11-29 13:12:32 +000096
Summer Qindea1f2c2021-01-11 14:46:34 +080097 /* Configures architecture */
98 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +010099
Shawn Shanf5471ba2020-09-17 17:34:50 +0800100 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200101
Shawn Shanf5471ba2020-09-17 17:34:50 +0800102 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000103
Tamas Ban9ff535b2018-09-18 08:15:18 +0100104 tfm_core_validate_boot_data();
105
Miklos Balint386b8b52017-11-29 13:12:32 +0000106 configure_ns_code();
107
108 /* Configures all interrupts to retarget NS state, except for
109 * secure peripherals
110 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200111 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
112 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800113 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200114 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200115
Miklos Balint386b8b52017-11-29 13:12:32 +0000116 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200117 plat_err = tfm_spm_hal_nvic_interrupt_enable();
118 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800119 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200120 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000121
David Huf07e97d2021-02-15 22:05:40 +0800122 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000123}
124
125int main(void)
126{
David Huf07e97d2021-02-15 22:05:40 +0800127 fih_int fih_rc = FIH_FAILURE;
128
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200129 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800130 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
David Huf363fe92019-07-02 13:03:30 +0800131 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200132
David Huf07e97d2021-02-15 22:05:40 +0800133 fih_delay_init();
134
135 FIH_CALL(tfm_core_init, fih_rc);
136 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800137 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000138 }
David Huf07e97d2021-02-15 22:05:40 +0800139
Raef Coles0241dc62020-12-22 11:50:02 +0000140 /* All isolation should have been set up at this point */
141 FIH_LABEL_CRITICAL_POINT();
142
Soby Mathewc64adbc2020-03-11 12:33:44 +0000143 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800144 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000145
Edison Ai4d66dc32019-02-18 17:58:49 +0800146 /*
147 * Prioritise secure exceptions to avoid NS being able to pre-empt
148 * secure SVC or SecureFault. Do it before PSA API initialization.
149 */
Ken Liu50e21092020-10-14 16:42:15 +0800150 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800151
152 /* Move to handler mode for further SPM initialization. */
153 tfm_core_handler_mode();
Miklos Balint386b8b52017-11-29 13:12:32 +0000154}