blob: 4e886bb2d6fa6d2254ebcd63727c8fa79872584b [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Feder Liang55194382021-11-22 16:45:33 +08002 * Copyright (c) 2017-2022, 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_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"
Raef Colesaefbe082021-06-18 08:53:43 +010018#include "tfm_plat_otp.h"
19#include "tfm_plat_provisioning.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
Kevin Peng300c68d2021-08-12 17:40:17 +080035REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020036
David Huf07e97d2021-02-15 22:05:40 +080037static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000038{
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;
Kevin Pengc8555732021-09-24 15:15:21 +080042#else
43 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
David Huf07e97d2021-02-15 22:05:40 +080044#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020045
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053046 /*
47 * Access to any peripheral should be performed after programming
48 * the necessary security components such as PPC/SAU.
49 */
David Huf07e97d2021-02-15 22:05:40 +080050#ifdef TFM_FIH_PROFILE_ON
51 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
52 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
53 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
54 }
55#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080056 hal_status = tfm_hal_set_up_static_boundaries();
57 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053058 return TFM_ERROR_GENERIC;
59 }
David Huf07e97d2021-02-15 22:05:40 +080060#endif /* TFM_FIH_PROFILE_ON */
61
62#ifdef TFM_FIH_PROFILE_ON
Kevin Peng38788a12021-09-08 16:23:50 +080063 FIH_CALL(tfm_hal_verify_static_boundaries, fih_rc);
64 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080065 tfm_core_panic();
66 }
67#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053068
Kevin Pengc8555732021-09-24 15:15:21 +080069#ifdef TFM_FIH_PROFILE_ON
70 FIH_CALL(tfm_hal_platform_init, fih_rc);
71 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080072 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070073 }
Kevin Pengc8555732021-09-24 15:15:21 +080074#else /* TFM_FIH_PROFILE_ON */
75 hal_status = tfm_hal_platform_init();
76 if (hal_status != TFM_HAL_SUCCESS) {
77 return TFM_ERROR_GENERIC;
78 }
79#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000080
Raef Colesaefbe082021-06-18 08:53:43 +010081 plat_err = tfm_plat_otp_init();
82 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
83 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
84 }
85
86 /* Perform provisioning. */
87 if (tfm_plat_provisioning_is_required()) {
88 plat_err = tfm_plat_provisioning_perform();
89 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
90 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
91 }
92 } else {
93 tfm_plat_provisioning_check_for_dummy_keys();
94 }
95
Summer Qindea1f2c2021-01-11 14:46:34 +080096 /* Configures architecture */
97 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +010098
Shawn Shanf5471ba2020-09-17 17:34:50 +080099 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +0200100
Shawn Shanf5471ba2020-09-17 17:34:50 +0800101 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +0000102
Feder Liang55194382021-11-22 16:45:33 +0800103#if (CONFIG_TFM_FP == 2)
David Hu91ea6aa2021-11-17 14:34:48 +0800104 SPMLOG_INFMSG("TF-M FP mode: Hardware\r\n");
Feder Liang55194382021-11-22 16:45:33 +0800105#ifdef CONFIG_TFM_LAZY_STACKING
David Hu91ea6aa2021-11-17 14:34:48 +0800106 SPMLOG_INFMSG("Lazy stacking enabled\r\n");
Feder Liangd4dbaa92021-09-07 15:34:46 +0800107#else
David Hu91ea6aa2021-11-17 14:34:48 +0800108 SPMLOG_INFMSG("Lazy stacking disabled\r\n");
Feder Liangd4dbaa92021-09-07 15:34:46 +0800109#endif
110#endif
111
Tamas Ban9ff535b2018-09-18 08:15:18 +0100112 tfm_core_validate_boot_data();
113
David Huf07e97d2021-02-15 22:05:40 +0800114 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000115}
116
117int main(void)
118{
David Huf07e97d2021-02-15 22:05:40 +0800119 fih_int fih_rc = FIH_FAILURE;
120
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200121 /* set Main Stack Pointer limit */
Kevin Peng300c68d2021-08-12 17:40:17 +0800122 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
123 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200124
David Huf07e97d2021-02-15 22:05:40 +0800125 fih_delay_init();
126
127 FIH_CALL(tfm_core_init, fih_rc);
128 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800129 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000130 }
David Huf07e97d2021-02-15 22:05:40 +0800131
Raef Coles0241dc62020-12-22 11:50:02 +0000132 /* All isolation should have been set up at this point */
133 FIH_LABEL_CRITICAL_POINT();
134
Soby Mathewc64adbc2020-03-11 12:33:44 +0000135 /* Print the TF-M version */
Anton Komlev2d2a6fc2022-02-20 15:47:53 +0000136 SPMLOG_INFMSG("\033[1;34mBooting TF-M "VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000137
Edison Ai4d66dc32019-02-18 17:58:49 +0800138 /*
139 * Prioritise secure exceptions to avoid NS being able to pre-empt
140 * secure SVC or SecureFault. Do it before PSA API initialization.
141 */
Ken Liu50e21092020-10-14 16:42:15 +0800142 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800143
Feder Liang55194382021-11-22 16:45:33 +0800144#if (CONFIG_TFM_FP >= 1)
Feder Liang42f5b562021-09-10 17:38:36 +0800145 tfm_arch_clear_fp_data();
146#endif
147
148 tfm_arch_clear_fp_status();
149
Ken Liu490281d2019-12-30 15:55:26 +0800150 /* Move to handler mode for further SPM initialization. */
151 tfm_core_handler_mode();
Kevin Peng300c68d2021-08-12 17:40:17 +0800152
153 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000154}