blob: 5e160d6c9feec356565b77ee1b0655ee4ef6447d [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 Qinf993cd42020-08-12 16:55:17 +08008#include "log/tfm_log.h"
TTornblom83d96372019-11-19 12:53:16 +01009#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080010#include "spm_ipc.h"
Summer Qin830c5542020-02-14 13:44:20 +080011#include "tfm_internal.h"
12#include "tfm_irq_list.h"
13#include "tfm_nspm.h"
14#include "tfm_spm_hal.h"
15#include "tfm_version.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000016
Miklos Balint386b8b52017-11-29 13:12:32 +000017/*
18 * Avoids the semihosting issue
19 * FixMe: describe 'semihosting issue'
20 */
21#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
22__asm(" .global __ARM_use_no_argv\n");
23#endif
24
25#ifndef TFM_LVL
26#error TFM_LVL is not defined!
Summer Qinf993cd42020-08-12 16:55:17 +080027#elif (TFM_LVL != 1) && (TFM_LVL != 2)
Edison Aicb0ecf62019-07-10 18:43:51 +080028#error Only TFM_LVL 1 and 2 are supported for IPC model!
29#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000030
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020031REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
32
Summer Qin830c5542020-02-14 13:44:20 +080033static int32_t tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000034{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020035 size_t i;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020036 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
37 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
Mate Toth-Pal4341de02018-10-02 12:55:47 +020038
Miklos Balint386b8b52017-11-29 13:12:32 +000039 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020040 plat_err = tfm_spm_hal_enable_fault_handlers();
41 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
42 return TFM_ERROR_GENERIC;
43 }
Miklos Balint386b8b52017-11-29 13:12:32 +000044
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010045 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020046 plat_err = tfm_spm_hal_system_reset_cfg();
47 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
48 return TFM_ERROR_GENERIC;
49 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010050
Marc Moreno Berengued584b612018-11-26 11:46:31 +000051 /* Configures debug authentication */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020052 plat_err = tfm_spm_hal_init_debug();
53 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
54 return TFM_ERROR_GENERIC;
55 }
Miklos Balint386b8b52017-11-29 13:12:32 +000056
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053057 /*
58 * Access to any peripheral should be performed after programming
59 * the necessary security components such as PPC/SAU.
60 */
61 plat_err = tfm_spm_hal_init_isolation_hw();
62 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
63 return TFM_ERROR_GENERIC;
64 }
65
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070066 /* Performs platform specific initialization */
67 plat_err = tfm_spm_hal_post_init();
68 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
69 return TFM_ERROR_GENERIC;
70 }
Miklos Balint386b8b52017-11-29 13:12:32 +000071
Ken Liu81f2d5e2019-12-26 11:44:36 +080072 LOG_MSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020073
Miklos Balint386b8b52017-11-29 13:12:32 +000074#ifdef TFM_CORE_DEBUG
Ken Liu81f2d5e2019-12-26 11:44:36 +080075 LOG_MSG("TF-M isolation level is: %d\r\n", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +000076#endif
77
Tamas Ban9ff535b2018-09-18 08:15:18 +010078 tfm_core_validate_boot_data();
79
Miklos Balint386b8b52017-11-29 13:12:32 +000080 configure_ns_code();
81
82 /* Configures all interrupts to retarget NS state, except for
83 * secure peripherals
84 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020085 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
86 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
87 return TFM_ERROR_GENERIC;
88 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +020089
90 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020091 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +020092 tfm_core_irq_signals[i].irq_line,
93 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020094 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
95 return TFM_ERROR_GENERIC;
96 }
97 irq_target_state = tfm_spm_hal_set_irq_target_state(
98 tfm_core_irq_signals[i].irq_line,
99 TFM_IRQ_TARGET_STATE_SECURE);
100 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
101 return TFM_ERROR_GENERIC;
102 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200103 }
104
Miklos Balint386b8b52017-11-29 13:12:32 +0000105 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200106 plat_err = tfm_spm_hal_nvic_interrupt_enable();
107 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
108 return TFM_ERROR_GENERIC;
109 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000110
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200111 return TFM_SUCCESS;
Miklos Balint386b8b52017-11-29 13:12:32 +0000112}
113
Edison Aid87f07b2019-07-22 18:50:24 +0800114static int32_t tfm_core_set_secure_exception_priorities(void)
Miklos Balintace4c3f2018-07-30 12:31:15 +0200115{
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200116 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
117
David Hu4e165602019-06-12 18:38:31 +0800118 tfm_arch_prioritize_secure_exception();
Miklos Balintace4c3f2018-07-30 12:31:15 +0200119
Mate Toth-Pal3e2ebd02019-05-07 14:22:16 +0200120 /* Explicitly set Secure SVC priority to highest */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200121 plat_err = tfm_spm_hal_set_secure_irq_priority(SVCall_IRQn, 0);
122 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
123 return TFM_ERROR_GENERIC;
124 }
Miklos Balintace4c3f2018-07-30 12:31:15 +0200125
Summer Qin2b8ab7e2020-02-18 13:58:58 +0800126 tfm_arch_set_pendsv_priority();
Edison Aie5111d92019-07-22 16:08:27 +0800127
128 return TFM_SUCCESS;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200129}
130
Miklos Balint386b8b52017-11-29 13:12:32 +0000131int main(void)
132{
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200133 /* set Main Stack Pointer limit */
David Huf363fe92019-07-02 13:03:30 +0800134 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
135 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200136
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 */
141 LOG_MSG("\033[1;34mBooting TFM v%d.%d %s\033[0m\r\n",
142 VERSION_MAJOR, VERSION_MINOR, VERSION_STRING);
Miklos Balint386b8b52017-11-29 13:12:32 +0000143
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000144 if (tfm_spm_db_init() != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800145 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000146 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200147
Edison Ai1dfd7b12020-02-23 14:16:08 +0800148#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
Edison Aic1b10902019-08-26 10:34:19 +0800149 if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800150 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200151 }
Edison Ai1dfd7b12020-02-23 14:16:08 +0800152#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200153
Edison Ai4d66dc32019-02-18 17:58:49 +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 */
Edison Aic1b10902019-08-26 10:34:19 +0800158 if (tfm_core_set_secure_exception_priorities() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800159 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200160 }
Ken Liu490281d2019-12-30 15:55:26 +0800161
162 /* Move to handler mode for further SPM initialization. */
163 tfm_core_handler_mode();
Miklos Balint386b8b52017-11-29 13:12:32 +0000164}