blob: d586b3c49c18f53c27ec7bbba9accbb8dc8b6ec7 [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{
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020040 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
David Huf07e97d2021-02-15 22:05:40 +080041#ifdef TFM_FIH_PROFILE_ON
42 fih_int fih_rc = FIH_FAILURE;
Kevin Pengc8555732021-09-24 15:15:21 +080043#else
44 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
David Huf07e97d2021-02-15 22:05:40 +080045#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020046
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053047 /*
48 * Access to any peripheral should be performed after programming
49 * the necessary security components such as PPC/SAU.
50 */
David Huf07e97d2021-02-15 22:05:40 +080051#ifdef TFM_FIH_PROFILE_ON
52 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
53 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
54 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
55 }
56#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080057 hal_status = tfm_hal_set_up_static_boundaries();
58 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053059 return TFM_ERROR_GENERIC;
60 }
David Huf07e97d2021-02-15 22:05:40 +080061#endif /* TFM_FIH_PROFILE_ON */
62
63#ifdef TFM_FIH_PROFILE_ON
Kevin Peng38788a12021-09-08 16:23:50 +080064 FIH_CALL(tfm_hal_verify_static_boundaries, fih_rc);
65 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080066 tfm_core_panic();
67 }
68#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053069
Kevin Pengc8555732021-09-24 15:15:21 +080070#ifdef TFM_FIH_PROFILE_ON
71 FIH_CALL(tfm_hal_platform_init, fih_rc);
72 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080073 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070074 }
Kevin Pengc8555732021-09-24 15:15:21 +080075#else /* TFM_FIH_PROFILE_ON */
76 hal_status = tfm_hal_platform_init();
77 if (hal_status != TFM_HAL_SUCCESS) {
78 return TFM_ERROR_GENERIC;
79 }
80#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000081
Raef Colesaefbe082021-06-18 08:53:43 +010082 plat_err = tfm_plat_otp_init();
83 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
84 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
85 }
86
87 /* Perform provisioning. */
88 if (tfm_plat_provisioning_is_required()) {
89 plat_err = tfm_plat_provisioning_perform();
90 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
91 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
92 }
93 } else {
94 tfm_plat_provisioning_check_for_dummy_keys();
95 }
96
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
David Huf07e97d2021-02-15 22:05:40 +0800108 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000109}
110
Kevin Peng300c68d2021-08-12 17:40:17 +0800111__attribute__((naked))
Miklos Balint386b8b52017-11-29 13:12:32 +0000112int main(void)
113{
Kevin Peng300c68d2021-08-12 17:40:17 +0800114 __ASM volatile(
115 "ldr r0, =0xFEF5EDA5 \n" /* Seal Main Stack before using */
116 "ldr r1, =0xFEF5EDA5 \n"
117 "push {r0, r1} \n"
118 "bl c_main \n"
119 );
120}
121
122int c_main(void)
123{
David Huf07e97d2021-02-15 22:05:40 +0800124 fih_int fih_rc = FIH_FAILURE;
125
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200126 /* set Main Stack Pointer limit */
Kevin Peng300c68d2021-08-12 17:40:17 +0800127 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
128 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200129
David Huf07e97d2021-02-15 22:05:40 +0800130 fih_delay_init();
131
132 FIH_CALL(tfm_core_init, fih_rc);
133 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800134 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000135 }
David Huf07e97d2021-02-15 22:05:40 +0800136
Raef Coles0241dc62020-12-22 11:50:02 +0000137 /* All isolation should have been set up at this point */
138 FIH_LABEL_CRITICAL_POINT();
139
Soby Mathewc64adbc2020-03-11 12:33:44 +0000140 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800141 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000142
Edison Ai4d66dc32019-02-18 17:58:49 +0800143 /*
144 * Prioritise secure exceptions to avoid NS being able to pre-empt
145 * secure SVC or SecureFault. Do it before PSA API initialization.
146 */
Ken Liu50e21092020-10-14 16:42:15 +0800147 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800148
Feder Liang42f5b562021-09-10 17:38:36 +0800149#if (CONFIG_TFM_SPE_FP >= 1)
150 tfm_arch_clear_fp_data();
151#endif
152
153 tfm_arch_clear_fp_status();
154
Ken Liu490281d2019-12-30 15:55:26 +0800155 /* Move to handler mode for further SPM initialization. */
156 tfm_core_handler_mode();
Kevin Peng300c68d2021-08-12 17:40:17 +0800157
158 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000159}