TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "common/tfm_boot_data.h"
9 #include "region.h"
10 #include "spm_ipc.h"
11 #include "tfm_hal_platform.h"
12 #include "tfm_hal_isolation.h"
13 #include "tfm_irq_list.h"
14 #include "tfm_nspm.h"
15 #include "tfm_spm_hal.h"
16 #include "tfm_spm_log.h"
17 #include "tfm_version.h"
18 
19 /*
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!
29 #elif (TFM_LVL != 1) && (TFM_LVL != 2) && (TFM_LVL != 3)
30 #error Invalid TFM_LVL value. Only TFM_LVL 1, 2 and 3 are supported in IPC model!
31 #endif
32 
33 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
34 
35 static int32_t tfm_core_init(void)
36 {
37  size_t i;
38  enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
39  enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
40  enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
41 
42  /* Enables fault handlers */
43  plat_err = tfm_spm_hal_enable_fault_handlers();
44  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
45  return TFM_ERROR_GENERIC;
46  }
47 
48  /* Configures the system reset request properties */
49  plat_err = tfm_spm_hal_system_reset_cfg();
50  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
51  return TFM_ERROR_GENERIC;
52  }
53 
54  /* Configures debug authentication */
55  plat_err = tfm_spm_hal_init_debug();
56  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
57  return TFM_ERROR_GENERIC;
58  }
59 
60  /*
61  * Access to any peripheral should be performed after programming
62  * the necessary security components such as PPC/SAU.
63  */
64  hal_status = tfm_hal_set_up_static_boundaries();
65  if (hal_status != TFM_HAL_SUCCESS) {
66  return TFM_ERROR_GENERIC;
67  }
68 
69  /* Performs platform specific initialization */
70  hal_status = tfm_hal_platform_init();
71  if (hal_status != TFM_HAL_SUCCESS) {
72  return TFM_ERROR_GENERIC;
73  }
74 
75  /* Configures architecture-specific coprocessors */
77 
78  SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
79 
80  SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
81 
83 
85 
86  /* Configures all interrupts to retarget NS state, except for
87  * secure peripherals
88  */
89  plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
90  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
91  return TFM_ERROR_GENERIC;
92  }
93 
94  for (i = 0; i < tfm_core_irq_signals_count; ++i) {
95  plat_err = tfm_spm_hal_set_secure_irq_priority(
96  tfm_core_irq_signals[i].irq_line,
97  tfm_core_irq_signals[i].irq_priority);
98  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
99  return TFM_ERROR_GENERIC;
100  }
101  irq_target_state = tfm_spm_hal_set_irq_target_state(
102  tfm_core_irq_signals[i].irq_line,
103  TFM_IRQ_TARGET_STATE_SECURE);
104  if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
105  return TFM_ERROR_GENERIC;
106  }
107  }
108 
109  /* Enable secure peripherals interrupts */
110  plat_err = tfm_spm_hal_nvic_interrupt_enable();
111  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
112  return TFM_ERROR_GENERIC;
113  }
114 
115  return TFM_SUCCESS;
116 }
117 
118 int main(void)
119 {
120  /* set Main Stack Pointer limit */
121  tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
122  $$ZI$$Base));
123 
124  if (tfm_core_init() != TFM_SUCCESS) {
125  tfm_core_panic();
126  }
127  /* Print the TF-M version */
128  SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
129 
130  if (tfm_spm_db_init() != SPM_ERR_OK) {
131  tfm_core_panic();
132  }
133 
134  /*
135  * Prioritise secure exceptions to avoid NS being able to pre-empt
136  * secure SVC or SecureFault. Do it before PSA API initialization.
137  */
139 
140  /* Move to handler mode for further SPM initialization. */
142 }
enum spm_err_t tfm_spm_db_init(void)
Initialize partition database.
Definition: spm_func.c:1415
const struct tfm_core_irq_signal_data_t tfm_core_irq_signals[]
void tfm_core_validate_boot_data(void)
Validate the content of shared memory area, which stores the shared data between bootloader and runti...
void tfm_core_handler_mode(void)
Move to handler mode by a SVC for specific purpose.
void configure_ns_code(void)
REGION_DECLARE(Image $$, ARM_LIB_STACK_MSP,$$ZI $$Base)
void tfm_arch_set_secure_exception_priorities(void)
__STATIC_INLINE void tfm_arch_init_secure_msp(uint32_t msplim)
Secure the MSP.
void tfm_core_panic(void)
Definition: utilities.c:11
#define SPMLOG_DBGMSGVAL(msg, val)
Definition: tfm_spm_log.h:41
int main(void)
Definition: main.c:118
#define VERSION_FULLSTR
Definition: tfm_version.h:22
const size_t tfm_core_irq_signals_count
#define SPMLOG_INFMSG(msg)
Definition: tfm_spm_log.h:50
void tfm_arch_configure_coprocessors(void)
Configure coprocessors.
Definition: arch.c:271