blob: f8eb68108d92e4aab656f089fd1c10adbb260227 [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"
Mingyang Sund1ed6732020-08-26 15:52:21 +080014#include "tfm_hal_isolation.h"
Summer Qin830c5542020-02-14 13:44:20 +080015#include "tfm_irq_list.h"
16#include "tfm_nspm.h"
17#include "tfm_spm_hal.h"
Shawn Shanf5471ba2020-09-17 17:34:50 +080018#include "tfm_spm_log.h"
Summer Qin830c5542020-02-14 13:44:20 +080019#include "tfm_version.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000020
Miklos Balint386b8b52017-11-29 13:12:32 +000021/*
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 Peng25b190b2020-10-30 17:10:45 +080031#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 Aicb0ecf62019-07-10 18:43:51 +080033#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000034
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020035REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
36
David Huf07e97d2021-02-15 22:05:40 +080037static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000038{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020039 size_t i;
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;
42 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
David Huf07e97d2021-02-15 22:05:40 +080043#ifdef TFM_FIH_PROFILE_ON
44 fih_int fih_rc = FIH_FAILURE;
45#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020046
Miklos Balint386b8b52017-11-29 13:12:32 +000047 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020048 plat_err = tfm_spm_hal_enable_fault_handlers();
49 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080050 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020051 }
Miklos Balint386b8b52017-11-29 13:12:32 +000052
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010053 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020054 plat_err = tfm_spm_hal_system_reset_cfg();
55 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080056 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020057 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010058
Marc Moreno Berengued584b612018-11-26 11:46:31 +000059 /* Configures debug authentication */
David Huf07e97d2021-02-15 22:05:40 +080060#ifdef TFM_FIH_PROFILE_ON
61 FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
62 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
63 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
64 }
65#else /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020066 plat_err = tfm_spm_hal_init_debug();
67 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
68 return TFM_ERROR_GENERIC;
69 }
David Huf07e97d2021-02-15 22:05:40 +080070#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000071
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053072 /*
73 * Access to any peripheral should be performed after programming
74 * the necessary security components such as PPC/SAU.
75 */
David Huf07e97d2021-02-15 22:05:40 +080076#ifdef TFM_FIH_PROFILE_ON
77 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
78 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
79 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
80 }
81#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080082 hal_status = tfm_hal_set_up_static_boundaries();
83 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053084 return TFM_ERROR_GENERIC;
85 }
David Huf07e97d2021-02-15 22:05:40 +080086#endif /* TFM_FIH_PROFILE_ON */
87
88#ifdef TFM_FIH_PROFILE_ON
89 FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc);
90 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
91 tfm_core_panic();
92 }
93#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053094
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070095 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080096 hal_status = tfm_hal_platform_init();
97 if (hal_status != TFM_HAL_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080098 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070099 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000100
Summer Qindea1f2c2021-01-11 14:46:34 +0800101 /* Configures architecture */
102 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +0100103
Shawn Shanf5471ba2020-09-17 17:34:50 +0800104 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200105
Shawn Shanf5471ba2020-09-17 17:34:50 +0800106 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000107
Tamas Ban9ff535b2018-09-18 08:15:18 +0100108 tfm_core_validate_boot_data();
109
Miklos Balint386b8b52017-11-29 13:12:32 +0000110 configure_ns_code();
111
112 /* Configures all interrupts to retarget NS state, except for
113 * secure peripherals
114 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200115 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
116 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800117 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200118 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200119
120 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200121 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200122 tfm_core_irq_signals[i].irq_line,
123 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200124 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800125 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200126 }
127 irq_target_state = tfm_spm_hal_set_irq_target_state(
128 tfm_core_irq_signals[i].irq_line,
129 TFM_IRQ_TARGET_STATE_SECURE);
130 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
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 }
134
Miklos Balint386b8b52017-11-29 13:12:32 +0000135 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200136 plat_err = tfm_spm_hal_nvic_interrupt_enable();
137 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800138 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200139 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000140
David Huf07e97d2021-02-15 22:05:40 +0800141 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000142}
143
144int main(void)
145{
David Huf07e97d2021-02-15 22:05:40 +0800146 fih_int fih_rc = FIH_FAILURE;
147
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200148 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800149 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
David Huf363fe92019-07-02 13:03:30 +0800150 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200151
David Huf07e97d2021-02-15 22:05:40 +0800152 fih_delay_init();
153
154 FIH_CALL(tfm_core_init, fih_rc);
155 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800156 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000157 }
David Huf07e97d2021-02-15 22:05:40 +0800158
Raef Coles0241dc62020-12-22 11:50:02 +0000159 /* All isolation should have been set up at this point */
160 FIH_LABEL_CRITICAL_POINT();
161
Soby Mathewc64adbc2020-03-11 12:33:44 +0000162 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800163 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000164
Edison Ai4d66dc32019-02-18 17:58:49 +0800165 /*
166 * Prioritise secure exceptions to avoid NS being able to pre-empt
167 * secure SVC or SecureFault. Do it before PSA API initialization.
168 */
Ken Liu50e21092020-10-14 16:42:15 +0800169 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800170
171 /* Move to handler mode for further SPM initialization. */
172 tfm_core_handler_mode();
Miklos Balint386b8b52017-11-29 13:12:32 +0000173}