blob: a247f37970c38f4bfacf3b4a07e812dcf932d0f5 [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
Ken Liu60fcf1c2022-05-18 14:58:48 +08008#include "build_config_check.h"
David Huf07e97d2021-02-15 22:05:40 +08009#include "fih.h"
Ken Liu55ba01f2021-01-20 17:34:50 +080010#include "ffm/tfm_boot_data.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
Kevin Peng300c68d2021-08-12 17:40:17 +080021REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020022
David Huf07e97d2021-02-15 22:05:40 +080023static fih_int tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000024{
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020025 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
David Huf07e97d2021-02-15 22:05:40 +080026#ifdef TFM_FIH_PROFILE_ON
27 fih_int fih_rc = FIH_FAILURE;
Kevin Pengc8555732021-09-24 15:15:21 +080028#else
29 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
David Huf07e97d2021-02-15 22:05:40 +080030#endif
Mate Toth-Pal4341de02018-10-02 12:55:47 +020031
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053032 /*
33 * Access to any peripheral should be performed after programming
34 * the necessary security components such as PPC/SAU.
35 */
David Huf07e97d2021-02-15 22:05:40 +080036#ifdef TFM_FIH_PROFILE_ON
37 FIH_CALL(tfm_hal_set_up_static_boundaries, fih_rc);
38 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
39 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
40 }
41#else /* TFM_FIH_PROFILE_ON */
Mingyang Sund1ed6732020-08-26 15:52:21 +080042 hal_status = tfm_hal_set_up_static_boundaries();
43 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053044 return TFM_ERROR_GENERIC;
45 }
David Huf07e97d2021-02-15 22:05:40 +080046#endif /* TFM_FIH_PROFILE_ON */
47
48#ifdef TFM_FIH_PROFILE_ON
Kevin Peng38788a12021-09-08 16:23:50 +080049 FIH_CALL(tfm_hal_verify_static_boundaries, fih_rc);
50 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080051 tfm_core_panic();
52 }
53#endif
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053054
Kevin Pengc8555732021-09-24 15:15:21 +080055#ifdef TFM_FIH_PROFILE_ON
56 FIH_CALL(tfm_hal_platform_init, fih_rc);
57 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
David Huf07e97d2021-02-15 22:05:40 +080058 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070059 }
Kevin Pengc8555732021-09-24 15:15:21 +080060#else /* TFM_FIH_PROFILE_ON */
61 hal_status = tfm_hal_platform_init();
62 if (hal_status != TFM_HAL_SUCCESS) {
63 return TFM_ERROR_GENERIC;
64 }
65#endif /* TFM_FIH_PROFILE_ON */
Miklos Balint386b8b52017-11-29 13:12:32 +000066
Raef Colesaefbe082021-06-18 08:53:43 +010067 plat_err = tfm_plat_otp_init();
68 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
69 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
70 }
71
72 /* Perform provisioning. */
73 if (tfm_plat_provisioning_is_required()) {
74 plat_err = tfm_plat_provisioning_perform();
75 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
76 FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
77 }
78 } else {
79 tfm_plat_provisioning_check_for_dummy_keys();
80 }
81
Summer Qindea1f2c2021-01-11 14:46:34 +080082 /* Configures architecture */
83 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +010084
Shawn Shanf5471ba2020-09-17 17:34:50 +080085 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020086
Shawn Shanf5471ba2020-09-17 17:34:50 +080087 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +000088
Feder Liang55194382021-11-22 16:45:33 +080089#if (CONFIG_TFM_FP == 2)
David Hu91ea6aa2021-11-17 14:34:48 +080090 SPMLOG_INFMSG("TF-M FP mode: Hardware\r\n");
Feder Liang55194382021-11-22 16:45:33 +080091#ifdef CONFIG_TFM_LAZY_STACKING
David Hu91ea6aa2021-11-17 14:34:48 +080092 SPMLOG_INFMSG("Lazy stacking enabled\r\n");
Feder Liangd4dbaa92021-09-07 15:34:46 +080093#else
David Hu91ea6aa2021-11-17 14:34:48 +080094 SPMLOG_INFMSG("Lazy stacking disabled\r\n");
Feder Liangd4dbaa92021-09-07 15:34:46 +080095#endif
96#endif
97
Tamas Ban9ff535b2018-09-18 08:15:18 +010098 tfm_core_validate_boot_data();
99
David Huf07e97d2021-02-15 22:05:40 +0800100 FIH_RET(fih_int_encode(TFM_SUCCESS));
Miklos Balint386b8b52017-11-29 13:12:32 +0000101}
102
103int main(void)
104{
David Huf07e97d2021-02-15 22:05:40 +0800105 fih_int fih_rc = FIH_FAILURE;
106
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200107 /* set Main Stack Pointer limit */
Kevin Peng300c68d2021-08-12 17:40:17 +0800108 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
109 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200110
David Huf07e97d2021-02-15 22:05:40 +0800111 fih_delay_init();
112
113 FIH_CALL(tfm_core_init, fih_rc);
114 if (fih_not_eq(fih_rc, fih_int_encode(TFM_SUCCESS))) {
Edison Ai9059ea02019-11-28 13:46:14 +0800115 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000116 }
David Huf07e97d2021-02-15 22:05:40 +0800117
Raef Coles0241dc62020-12-22 11:50:02 +0000118 /* All isolation should have been set up at this point */
119 FIH_LABEL_CRITICAL_POINT();
120
Soby Mathewc64adbc2020-03-11 12:33:44 +0000121 /* Print the TF-M version */
Anton Komlev2d2a6fc2022-02-20 15:47:53 +0000122 SPMLOG_INFMSG("\033[1;34mBooting TF-M "VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000123
Edison Ai4d66dc32019-02-18 17:58:49 +0800124 /*
125 * Prioritise secure exceptions to avoid NS being able to pre-empt
126 * secure SVC or SecureFault. Do it before PSA API initialization.
127 */
Ken Liu50e21092020-10-14 16:42:15 +0800128 tfm_arch_set_secure_exception_priorities();
Ken Liu490281d2019-12-30 15:55:26 +0800129
Feder Liang55194382021-11-22 16:45:33 +0800130#if (CONFIG_TFM_FP >= 1)
Feder Liang42f5b562021-09-10 17:38:36 +0800131 tfm_arch_clear_fp_data();
132#endif
133
134 tfm_arch_clear_fp_status();
135
Ken Liu490281d2019-12-30 15:55:26 +0800136 /* Move to handler mode for further SPM initialization. */
137 tfm_core_handler_mode();
Kevin Peng300c68d2021-08-12 17:40:17 +0800138
139 return 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000140}