blob: bfc11cdeb7228ba27843a75af3816afd41496c20 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +05302 * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "arch.h"
9#include "common/tfm_boot_data.h"
TTornblom83d96372019-11-19 12:53:16 +010010#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080011#include "spm_func.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080012#include "tfm_hal_platform.h"
Summer Qin830c5542020-02-14 13:44:20 +080013#include "tfm_irq_list.h"
14#include "tfm_nspm.h"
15#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"
Miklos Balint386b8b52017-11-29 13:12:32 +000018
Miklos Balint386b8b52017-11-29 13:12:32 +000019/*
20 * Avoids the semihosting issue
21 * FixMe: describe 'semihosting issue'
22 */
23#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
24__asm(" .global __ARM_use_no_argv\n");
25#endif
26
27#ifndef TFM_LVL
28#error TFM_LVL is not defined!
Summer Qinf993cd42020-08-12 16:55:17 +080029#elif (TFM_LVL != 1)
Edison Aicb0ecf62019-07-10 18:43:51 +080030#error Only TFM_LVL 1 is supported for library model!
31#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000032
Shawn Shanf5471ba2020-09-17 17:34:50 +080033#define PRINT_TFM_VERSION SPMLOG_INFMSGVAL("Booting TFM v", VERSION_MAJOR); \
34 SPMLOG_INFMSGVAL(".", VERSION_MINOR); \
35 SPMLOG_INFMSG(" "); \
36 SPMLOG_INFMSG(VERSION_STRING); \
37 SPMLOG_INFMSG("\r\n")
38
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020039REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
40
Summer Qin830c5542020-02-14 13:44:20 +080041static int32_t tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000042{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020043 size_t i;
Summer Qin0eb7c912020-08-19 16:08:50 +080044 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020045 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
46 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
Mate Toth-Pal4341de02018-10-02 12:55:47 +020047
Miklos Balint386b8b52017-11-29 13:12:32 +000048 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020049 plat_err = tfm_spm_hal_enable_fault_handlers();
50 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
51 return TFM_ERROR_GENERIC;
52 }
Miklos Balint386b8b52017-11-29 13:12:32 +000053
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010054 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020055 plat_err = tfm_spm_hal_system_reset_cfg();
56 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
57 return TFM_ERROR_GENERIC;
58 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010059
Marc Moreno Berengued584b612018-11-26 11:46:31 +000060 /* Configures debug authentication */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020061 plat_err = tfm_spm_hal_init_debug();
62 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
63 return TFM_ERROR_GENERIC;
64 }
Miklos Balint386b8b52017-11-29 13:12:32 +000065
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053066 /*
67 * Access to any peripheral should be performed after programming
68 * the necessary security components such as PPC/SAU.
69 */
70 plat_err = tfm_spm_hal_init_isolation_hw();
71 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
72 return TFM_ERROR_GENERIC;
73 }
74
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070075 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080076 hal_status = tfm_hal_platform_init();
77 if (hal_status != TFM_HAL_SUCCESS) {
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070078 return TFM_ERROR_GENERIC;
79 }
Miklos Balint386b8b52017-11-29 13:12:32 +000080
Jamie Fox45587672020-08-17 18:31:14 +010081 /* Configures architecture-specific coprocessors */
82 tfm_arch_configure_coprocessors();
83
Shawn Shanf5471ba2020-09-17 17:34:50 +080084 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020085
Miklos Balint386b8b52017-11-29 13:12:32 +000086#ifdef TFM_CORE_DEBUG
Shawn Shanf5471ba2020-09-17 17:34:50 +080087 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
88 SPMLOG_DBGMSG("\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +000089#endif
90
Tamas Ban9ff535b2018-09-18 08:15:18 +010091 tfm_core_validate_boot_data();
92
Miklos Balint386b8b52017-11-29 13:12:32 +000093 configure_ns_code();
94
95 /* Configures all interrupts to retarget NS state, except for
96 * secure peripherals
97 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020098 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
99 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
100 return TFM_ERROR_GENERIC;
101 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200102
103 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200104 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200105 tfm_core_irq_signals[i].irq_line,
106 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200107 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
108 return TFM_ERROR_GENERIC;
109 }
110 irq_target_state = tfm_spm_hal_set_irq_target_state(
111 tfm_core_irq_signals[i].irq_line,
112 TFM_IRQ_TARGET_STATE_SECURE);
113 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
114 return TFM_ERROR_GENERIC;
115 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200116 }
117
Miklos Balint386b8b52017-11-29 13:12:32 +0000118 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200119 plat_err = tfm_spm_hal_nvic_interrupt_enable();
120 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
121 return TFM_ERROR_GENERIC;
122 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000123
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200124 return TFM_SUCCESS;
Miklos Balint386b8b52017-11-29 13:12:32 +0000125}
126
127int main(void)
128{
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200129 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800130 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$,
131 ARM_LIB_STACK_MSP,
132 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200133
Soby Mathew960521a2020-09-29 12:48:50 +0100134 /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */
135 tfm_spm_seal_psp_stacks();
136
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200137 if (tfm_core_init() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800138 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000139 }
Soby Mathewc64adbc2020-03-11 12:33:44 +0000140 /* Print the TF-M version */
Shawn Shanf5471ba2020-09-17 17:34:50 +0800141 PRINT_TFM_VERSION;
Miklos Balint386b8b52017-11-29 13:12:32 +0000142
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000143 if (tfm_spm_db_init() != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800144 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000145 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200146
Edison Ai1dfd7b12020-02-23 14:16:08 +0800147#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
Edison Aic1b10902019-08-26 10:34:19 +0800148 if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800149 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200150 }
Edison Ai1dfd7b12020-02-23 14:16:08 +0800151#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200152
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100153 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100154
TTornblomc640e072019-06-14 14:33:51 +0200155 REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200156 uint32_t psp_stack_bottom =
157 (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Miklos Balint386b8b52017-11-29 13:12:32 +0000158
David Hue05b6a62019-06-12 18:45:28 +0800159 tfm_arch_set_psplim(psp_stack_bottom);
Miklos Balint386b8b52017-11-29 13:12:32 +0000160
Miklos Balint6a139ae2018-04-04 19:44:37 +0200161 if (tfm_spm_partition_init() != SPM_ERR_OK) {
162 /* Certain systems might refuse to boot altogether if partitions fail
163 * to initialize. This is a placeholder for such an error handler
164 */
165 }
166
Ken Liu96714b32019-04-08 15:10:39 +0800167 /*
168 * Prioritise secure exceptions to avoid NS being able to pre-empt
169 * secure SVC or SecureFault. Do it before PSA API initialization.
170 */
Ken Liu50e21092020-10-14 16:42:15 +0800171 tfm_arch_set_secure_exception_priorities();
Ken Liu96714b32019-04-08 15:10:39 +0800172
Edison Ai4d66dc32019-02-18 17:58:49 +0800173 /* We close the TFM_SP_CORE_ID partition, because its only purpose is
174 * to be able to pass the state checks for the tests started from secure.
175 */
176 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED);
177 tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID,
178 SPM_PARTITION_STATE_RUNNING);
Edison Ai4dcae6f2019-03-18 10:13:47 +0800179
180#ifdef TFM_CORE_DEBUG
181 /* Jumps to non-secure code */
Shawn Shanf5471ba2020-09-17 17:34:50 +0800182 SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
Edison Ai4dcae6f2019-03-18 10:13:47 +0800183#endif
184
185 jump_to_ns_code();
Miklos Balint386b8b52017-11-29 13:12:32 +0000186}