blob: 30cae8bf1b8edcbe485c341be80bf9220f2a2302 [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"
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
Kevin Peng300c68d2021-08-12 17:40:17 +080034REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020035
David Huf07e97d2021-02-15 22:05:40 +080036static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000037{
Summer Qin0eb7c912020-08-19 16:08:50 +080038 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020039 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
David Huf07e97d2021-02-15 22:05:40 +080040#ifdef TFM_FIH_PROFILE_ON
41 fih_int fih_rc = FIH_FAILURE;
42#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020043
Miklos Balint386b8b52017-11-29 13:12:32 +000044 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020045 plat_err = tfm_spm_hal_enable_fault_handlers();
46 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080047 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020048 }
Miklos Balint386b8b52017-11-29 13:12:32 +000049
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010050 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020051 plat_err = tfm_spm_hal_system_reset_cfg();
52 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080053 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020054 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010055
Marc Moreno Berengued584b612018-11-26 11:46:31 +000056 /* Configures debug authentication */
David Huf07e97d2021-02-15 22:05:40 +080057#ifdef TFM_FIH_PROFILE_ON
58 FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
59 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
60 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
61 }
62#else /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020063 plat_err = tfm_spm_hal_init_debug();
64 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
65 return TFM_ERROR_GENERIC;
66 }
David Huf07e97d2021-02-15 22:05:40 +080067#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000068
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053069 /*
70 * Access to any peripheral should be performed after programming
71 * the necessary security components such as PPC/SAU.
72 */
David Huf07e97d2021-02-15 22:05:40 +080073#ifdef TFM_FIH_PROFILE_ON
74 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
75 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
76 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
77 }
78#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080079 hal_status = tfm_hal_set_up_static_boundaries();
80 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053081 return TFM_ERROR_GENERIC;
82 }
David Huf07e97d2021-02-15 22:05:40 +080083#endif /* TFM_FIH_PROFILE_ON */
84
85#ifdef TFM_FIH_PROFILE_ON
86 FIH_CALL(tfm_spm_hal_verify_isolation_hw, fih_rc);
87 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
88 tfm_core_panic();
89 }
90#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053091
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070092 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080093 hal_status = tfm_hal_platform_init();
94 if (hal_status != TFM_HAL_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +080095 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070096 }
Miklos Balint386b8b52017-11-29 13:12:32 +000097
Summer Qindea1f2c2021-01-11 14:46:34 +080098 /* Configures architecture */
99 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +0100100
Shawn Shanf5471ba2020-09-17 17:34:50 +0800101 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200102
Shawn Shanf5471ba2020-09-17 17:34:50 +0800103 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000104
Tamas Ban9ff535b2018-09-18 08:15:18 +0100105 tfm_core_validate_boot_data();
106
Miklos Balint386b8b52017-11-29 13:12:32 +0000107 configure_ns_code();
108
109 /* Configures all interrupts to retarget NS state, except for
110 * secure peripherals
111 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200112 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
113 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800114 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200115 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200116
Miklos Balint386b8b52017-11-29 13:12:32 +0000117 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200118 plat_err = tfm_spm_hal_nvic_interrupt_enable();
119 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
David Huf07e97d2021-02-15 22:05:40 +0800120 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200121 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000122
David Huf07e97d2021-02-15 22:05:40 +0800123 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000124}
125
Kevin Peng300c68d2021-08-12 17:40:17 +0800126__attribute__((naked))
Miklos Balint386b8b52017-11-29 13:12:32 +0000127int main(void)
128{
Kevin Peng300c68d2021-08-12 17:40:17 +0800129 __ASM volatile(
130 "ldr r0, =0xFEF5EDA5 \n" /* Seal Main Stack before using */
131 "ldr r1, =0xFEF5EDA5 \n"
132 "push {r0, r1} \n"
133 "bl c_main \n"
134 );
135}
136
137int c_main(void)
138{
David Huf07e97d2021-02-15 22:05:40 +0800139 fih_int fih_rc = FIH_FAILURE;
140
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200141 /* set Main Stack Pointer limit */
Kevin Peng300c68d2021-08-12 17:40:17 +0800142 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
143 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200144
David Huf07e97d2021-02-15 22:05:40 +0800145 fih_delay_init();
146
147 FIH_CALL(tfm_core_init, fih_rc);
148 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800149 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000150 }
David Huf07e97d2021-02-15 22:05:40 +0800151
Raef Coles0241dc62020-12-22 11:50:02 +0000152 /* All isolation should have been set up at this point */
153 FIH_LABEL_CRITICAL_POINT();
154
Soby Mathewc64adbc2020-03-11 12:33:44 +0000155 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800156 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000157
Edison Ai4d66dc32019-02-18 17:58:49 +0800158 /*
159 * Prioritise secure exceptions to avoid NS being able to pre-empt
160 * secure SVC or SecureFault. Do it before PSA API initialization.
161 */
Ken Liu50e21092020-10-14 16:42:15 +0800162 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800163
164 /* Move to handler mode for further SPM initialization. */
165 tfm_core_handler_mode();
Kevin Peng300c68d2021-08-12 17:40:17 +0800166
167 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000168}