blob: 41bd91ab31a5ca02d746a9e7ff15ebd7b1807864 [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"
TTornblom83d96372019-11-19 12:53:16 +010010#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080011#include "spm_ipc.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080012#include "tfm_hal_platform.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080013#include "tfm_hal_isolation.h"
Summer Qin830c5542020-02-14 13:44:20 +080014#include "tfm_irq_list.h"
15#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"
Miklos Balint386b8b52017-11-29 13:12:32 +000019
Miklos Balint386b8b52017-11-29 13:12:32 +000020/*
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 Peng25b190b2020-10-30 17:10:45 +080030#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 Aicb0ecf62019-07-10 18:43:51 +080032#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000033
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020034REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
35
David Huf07e97d2021-02-15 22:05:40 +080036static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000037{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020038 size_t i;
Summer Qin0eb7c912020-08-19 16:08:50 +080039 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020040 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
41 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
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
Summer Qindea1f2c2021-01-11 14:46:34 +0800100 /* Configures architecture */
101 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +0100102
Shawn Shanf5471ba2020-09-17 17:34:50 +0800103 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200104
Shawn Shanf5471ba2020-09-17 17:34:50 +0800105 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000106
Tamas Ban9ff535b2018-09-18 08:15:18 +0100107 tfm_core_validate_boot_data();
108
Miklos Balint386b8b52017-11-29 13:12:32 +0000109 configure_ns_code();
110
111 /* Configures all interrupts to retarget NS state, except for
112 * secure peripherals
113 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200114 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
115 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800116 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200117 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200118
119 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200120 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200121 tfm_core_irq_signals[i].irq_line,
122 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200123 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800124 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200125 }
126 irq_target_state = tfm_spm_hal_set_irq_target_state(
127 tfm_core_irq_signals[i].irq_line,
128 TFM_IRQ_TARGET_STATE_SECURE);
129 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
David Huf07e97d2021-02-15 22:05:40 +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
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
143int main(void)
144{
David Huf07e97d2021-02-15 22:05:40 +0800145 fih_int fih_rc = FIH_FAILURE;
146
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200147 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800148 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
David Huf363fe92019-07-02 13:03:30 +0800149 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200150
David Huf07e97d2021-02-15 22:05:40 +0800151 fih_delay_init();
152
153 FIH_CALL(tfm_core_init, fih_rc);
154 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800155 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000156 }
David Huf07e97d2021-02-15 22:05:40 +0800157
Soby Mathewc64adbc2020-03-11 12:33:44 +0000158 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800159 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000160
Edison Ai4d66dc32019-02-18 17:58:49 +0800161 /*
162 * Prioritise secure exceptions to avoid NS being able to pre-empt
163 * secure SVC or SecureFault. Do it before PSA API initialization.
164 */
Ken Liu50e21092020-10-14 16:42:15 +0800165 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800166
167 /* Move to handler mode for further SPM initialization. */
168 tfm_core_handler_mode();
Miklos Balint386b8b52017-11-29 13:12:32 +0000169}