blob: 5695279ec96f6e5a3781a6c00538a2c4a297c7cb [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Ken Liu55ba01f2021-01-20 17:34:50 +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
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "arch.h"
Ken Liu55ba01f2021-01-20 17:34:50 +08009#include "ffm/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"
Mingyang Sun9763dee2020-12-07 10:45:17 +080012#include "tfm_hal_defs.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080013#include "tfm_hal_platform.h"
Håkon Øye Amundsencf793942021-01-14 10:50:49 +010014#include "tfm_hal_isolation.h"
Summer Qin830c5542020-02-14 13:44:20 +080015#include "tfm_irq_list.h"
16#include "tfm_nspm.h"
17#include "tfm_spm_hal.h"
Shawn Shanf5471ba2020-09-17 17:34:50 +080018#include "tfm_spm_log.h"
Summer Qin830c5542020-02-14 13:44:20 +080019#include "tfm_version.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!
Summer Qinf993cd42020-08-12 16:55:17 +080031#elif (TFM_LVL != 1)
Edison Aicb0ecf62019-07-10 18:43:51 +080032#error Only TFM_LVL 1 is supported for library model!
33#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000034
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020035REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
36
Summer Qin830c5542020-02-14 13:44:20 +080037static int32_t tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000038{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020039 size_t i;
Summer Qin0eb7c912020-08-19 16:08:50 +080040 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020041 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
42 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
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) {
47 return TFM_ERROR_GENERIC;
48 }
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) {
53 return TFM_ERROR_GENERIC;
54 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010055
Marc Moreno Berengued584b612018-11-26 11:46:31 +000056 /* Configures debug authentication */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020057 plat_err = tfm_spm_hal_init_debug();
58 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
59 return TFM_ERROR_GENERIC;
60 }
Miklos Balint386b8b52017-11-29 13:12:32 +000061
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053062 /*
63 * Access to any peripheral should be performed after programming
64 * the necessary security components such as PPC/SAU.
65 */
Mingyang Sun9763dee2020-12-07 10:45:17 +080066 hal_status = tfm_hal_set_up_static_boundaries();
67 if (hal_status != TFM_HAL_SUCCESS) {
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053068 return TFM_ERROR_GENERIC;
69 }
70
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070071 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080072 hal_status = tfm_hal_platform_init();
73 if (hal_status != TFM_HAL_SUCCESS) {
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070074 return TFM_ERROR_GENERIC;
75 }
Miklos Balint386b8b52017-11-29 13:12:32 +000076
Summer Qindea1f2c2021-01-11 14:46:34 +080077 /* Configures architecture */
78 tfm_arch_config_extensions();
Jamie Fox45587672020-08-17 18:31:14 +010079
Shawn Shanf5471ba2020-09-17 17:34:50 +080080 SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020081
Shawn Shanf5471ba2020-09-17 17:34:50 +080082 SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +000083
Tamas Ban9ff535b2018-09-18 08:15:18 +010084 tfm_core_validate_boot_data();
85
Miklos Balint386b8b52017-11-29 13:12:32 +000086 configure_ns_code();
87
88 /* Configures all interrupts to retarget NS state, except for
89 * secure peripherals
90 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020091 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
92 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
93 return TFM_ERROR_GENERIC;
94 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +020095
96 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020097 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +020098 tfm_core_irq_signals[i].irq_line,
99 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200100 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
101 return TFM_ERROR_GENERIC;
102 }
103 irq_target_state = tfm_spm_hal_set_irq_target_state(
104 tfm_core_irq_signals[i].irq_line,
105 TFM_IRQ_TARGET_STATE_SECURE);
106 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
107 return TFM_ERROR_GENERIC;
108 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200109 }
110
Miklos Balint386b8b52017-11-29 13:12:32 +0000111 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200112 plat_err = tfm_spm_hal_nvic_interrupt_enable();
113 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
114 return TFM_ERROR_GENERIC;
115 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000116
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200117 return TFM_SUCCESS;
Miklos Balint386b8b52017-11-29 13:12:32 +0000118}
119
120int main(void)
121{
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200122 /* set Main Stack Pointer limit */
Ken Liu05e13ba2020-07-25 10:31:33 +0800123 tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$,
124 ARM_LIB_STACK_MSP,
125 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200126
Soby Mathew960521a2020-09-29 12:48:50 +0100127 /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */
128 tfm_spm_seal_psp_stacks();
129
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200130 if (tfm_core_init() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800131 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000132 }
Soby Mathewc64adbc2020-03-11 12:33:44 +0000133 /* Print the TF-M version */
Shawn Shan45578e92020-10-19 17:50:02 +0800134 SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
Miklos Balint386b8b52017-11-29 13:12:32 +0000135
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000136 if (tfm_spm_db_init() != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800137 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000138 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200139
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100140 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100141
TTornblomc640e072019-06-14 14:33:51 +0200142 REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200143 uint32_t psp_stack_bottom =
144 (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Miklos Balint386b8b52017-11-29 13:12:32 +0000145
David Hue05b6a62019-06-12 18:45:28 +0800146 tfm_arch_set_psplim(psp_stack_bottom);
Miklos Balint386b8b52017-11-29 13:12:32 +0000147
Miklos Balint6a139ae2018-04-04 19:44:37 +0200148 if (tfm_spm_partition_init() != SPM_ERR_OK) {
149 /* Certain systems might refuse to boot altogether if partitions fail
150 * to initialize. This is a placeholder for such an error handler
151 */
152 }
153
Ken Liu96714b32019-04-08 15:10:39 +0800154 /*
155 * Prioritise secure exceptions to avoid NS being able to pre-empt
156 * secure SVC or SecureFault. Do it before PSA API initialization.
157 */
Ken Liu50e21092020-10-14 16:42:15 +0800158 tfm_arch_set_secure_exception_priorities();
Ken Liu96714b32019-04-08 15:10:39 +0800159
Edison Ai4d66dc32019-02-18 17:58:49 +0800160 /* We close the TFM_SP_CORE_ID partition, because its only purpose is
161 * to be able to pass the state checks for the tests started from secure.
162 */
163 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED);
164 tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID,
165 SPM_PARTITION_STATE_RUNNING);
Edison Ai4dcae6f2019-03-18 10:13:47 +0800166
167#ifdef TFM_CORE_DEBUG
168 /* Jumps to non-secure code */
Shawn Shanf5471ba2020-09-17 17:34:50 +0800169 SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
Edison Ai4dcae6f2019-03-18 10:13:47 +0800170#endif
171
172 jump_to_ns_code();
Miklos Balint386b8b52017-11-29 13:12:32 +0000173}