Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Ken Liu | 55ba01f | 2021-01-20 17:34:50 +0800 | [diff] [blame^] | 8 | #include "ffm/tfm_boot_data.h" |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 9 | #include "region.h" |
Summer Qin | f993cd4 | 2020-08-12 16:55:17 +0800 | [diff] [blame] | 10 | #include "spm_ipc.h" |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 11 | #include "tfm_hal_platform.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 12 | #include "tfm_hal_isolation.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 13 | #include "tfm_irq_list.h" |
| 14 | #include "tfm_nspm.h" |
| 15 | #include "tfm_spm_hal.h" |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 16 | #include "tfm_spm_log.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 17 | #include "tfm_version.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 18 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 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! |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 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! |
Edison Ai | cb0ecf6 | 2019-07-10 18:43:51 +0800 | [diff] [blame] | 31 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 32 | |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 33 | REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base); |
| 34 | |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 35 | static int32_t tfm_core_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 36 | { |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 37 | size_t i; |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 38 | enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC; |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 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; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 41 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 42 | /* Enables fault handlers */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 43 | plat_err = tfm_spm_hal_enable_fault_handlers(); |
| 44 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 45 | return TFM_ERROR_GENERIC; |
| 46 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 47 | |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 48 | /* Configures the system reset request properties */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 49 | plat_err = tfm_spm_hal_system_reset_cfg(); |
| 50 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 51 | return TFM_ERROR_GENERIC; |
| 52 | } |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 53 | |
Marc Moreno Berengue | d584b61 | 2018-11-26 11:46:31 +0000 | [diff] [blame] | 54 | /* Configures debug authentication */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 55 | plat_err = tfm_spm_hal_init_debug(); |
| 56 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 57 | return TFM_ERROR_GENERIC; |
| 58 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 59 | |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 60 | /* |
| 61 | * Access to any peripheral should be performed after programming |
| 62 | * the necessary security components such as PPC/SAU. |
| 63 | */ |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 64 | hal_status = tfm_hal_set_up_static_boundaries(); |
| 65 | if (hal_status != TFM_HAL_SUCCESS) { |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 66 | return TFM_ERROR_GENERIC; |
| 67 | } |
| 68 | |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 69 | /* Performs platform specific initialization */ |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 70 | hal_status = tfm_hal_platform_init(); |
| 71 | if (hal_status != TFM_HAL_SUCCESS) { |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 72 | return TFM_ERROR_GENERIC; |
| 73 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 74 | |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 75 | /* Configures architecture-specific coprocessors */ |
| 76 | tfm_arch_configure_coprocessors(); |
| 77 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 78 | SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n"); |
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 79 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 80 | SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 81 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 82 | tfm_core_validate_boot_data(); |
| 83 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 84 | configure_ns_code(); |
| 85 | |
| 86 | /* Configures all interrupts to retarget NS state, except for |
| 87 | * secure peripherals |
| 88 | */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 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 | } |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 93 | |
| 94 | for (i = 0; i < tfm_core_irq_signals_count; ++i) { |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 95 | plat_err = tfm_spm_hal_set_secure_irq_priority( |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 96 | tfm_core_irq_signals[i].irq_line, |
| 97 | tfm_core_irq_signals[i].irq_priority); |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 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 | } |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 109 | /* Enable secure peripherals interrupts */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 110 | plat_err = tfm_spm_hal_nvic_interrupt_enable(); |
| 111 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 112 | return TFM_ERROR_GENERIC; |
| 113 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 114 | |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 115 | return TFM_SUCCESS; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int main(void) |
| 119 | { |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 120 | /* set Main Stack Pointer limit */ |
Ken Liu | 05e13ba | 2020-07-25 10:31:33 +0800 | [diff] [blame] | 121 | tfm_arch_init_secure_msp((uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK_MSP, |
David Hu | f363fe9 | 2019-07-02 13:03:30 +0800 | [diff] [blame] | 122 | $$ZI$$Base)); |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 123 | |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 124 | if (tfm_core_init() != TFM_SUCCESS) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 125 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 126 | } |
Soby Mathew | c64adbc | 2020-03-11 12:33:44 +0000 | [diff] [blame] | 127 | /* Print the TF-M version */ |
Shawn Shan | 45578e9 | 2020-10-19 17:50:02 +0800 | [diff] [blame] | 128 | SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n"); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 129 | |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 130 | if (tfm_spm_db_init() != SPM_ERR_OK) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 131 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 132 | } |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 133 | |
Edison Ai | 4d66dc3 | 2019-02-18 17:58:49 +0800 | [diff] [blame] | 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 | */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 138 | tfm_arch_set_secure_exception_priorities(); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 139 | |
| 140 | /* Move to handler mode for further SPM initialization. */ |
| 141 | tfm_core_handler_mode(); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 142 | } |