blob: ee08e8c2a6133e15462065384c556ec0f4e76b26 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Mate Toth-Pal65291f32018-02-23 14:35:22 +01002 * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mate Toth-Pal349714a2018-02-23 15:30:24 +01008/* This file contains the APIs exported by the SPM to tfm core */
Miklos Balint386b8b52017-11-29 13:12:32 +00009
10#include <stdio.h>
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010011#include <string.h>
Miklos Balint386b8b52017-11-29 13:12:32 +000012#include "spm_api.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020013#include "platform/include/tfm_spm_hal.h"
14#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020015#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010016#include "tfm_api.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000017#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020018#include "platform_retarget.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020019#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020020#include "spm_partition_defs.h"
21
Miklos Balint386b8b52017-11-29 13:12:32 +000022
Mate Toth-Pal349714a2018-02-23 15:30:24 +010023struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000024
Miklos Balint386b8b52017-11-29 13:12:32 +000025typedef enum {
26 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010027} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000028
29/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010030 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010031 * In case of an error in the error handling, a non-zero value have to be
32 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000033 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010034static void tfm_spm_partition_err_handler(
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020035 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010036 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010037 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000038{
Miklos Balint386b8b52017-11-29 13:12:32 +000039#ifdef TFM_CORE_DEBUG
40 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010041 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010042 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000043 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010044 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010045 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000046 }
47#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010048 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010049 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000050}
51
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010052uint32_t get_partition_idx(uint32_t partition_id)
53{
54 int i;
55
56 if (partition_id == INVALID_PARTITION_ID) {
57 return SPM_INVALID_PARTITION_IDX;
58 }
59
60 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
61 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
62 partition_id) {
63 return i;
64 }
65 }
66 return SPM_INVALID_PARTITION_IDX;
67}
68
Miklos Balint386b8b52017-11-29 13:12:32 +000069enum spm_err_t tfm_spm_db_init(void)
70{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020071 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010072
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010073 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
74
Mate Toth-Pal349714a2018-02-23 15:30:24 +010075 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010076 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
77 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000078
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010079 /* There are a few partitions that are used by TF-M internally.
80 * These are explicitly added to the partition db here.
81 */
82
83 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020084#if TFM_LVL != 1
85 extern uint32_t Stack_Mem[];
86 extern uint32_t Stack_top[];
87#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010088 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
89 return SPM_ERR_INVALID_CONFIG;
90 }
91 part_ptr = &(g_spm_partition_db.partitions[
92 g_spm_partition_db.partition_count]);
93 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010094 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +020095
96#if TFM_LVL != 1
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020097 part_ptr->memory_data.stack_bottom = (uint32_t)Stack_Mem;
98 part_ptr->memory_data.stack_top = (uint32_t)Stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +020099 /* Since RW, ZI and stack are configured as one MPU region, configure
100 * RW start address to Stack_Mem to get RW access to stack
101 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200102 part_ptr->memory_data.rw_start = (uint32_t)Stack_Mem;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200103#endif
104
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100105 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
106 ++g_spm_partition_db.partition_count;
107
108 /* For the TF-M core environment itself */
109 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
110 return SPM_ERR_INVALID_CONFIG;
111 }
112 part_ptr = &(g_spm_partition_db.partitions[
113 g_spm_partition_db.partition_count]);
114 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100115 part_ptr->static_data.partition_flags =
116 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100117 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
118 ++g_spm_partition_db.partition_count;
119
120 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200121 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000122
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100123 g_spm_partition_db.is_init = 1;
124
Miklos Balint386b8b52017-11-29 13:12:32 +0000125 return SPM_ERR_OK;
126}
127
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100128enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000129{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200130 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200131 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200132 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000133 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100134 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000135
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100136 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100137 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
138 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200139 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100140 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100141 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200142 tfm_spm_partition_set_caller_partition_idx(idx,
143 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100144 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200145 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200146
147 desc.args = args;
148 desc.exc_num = EXC_NUM_THREAD_MODE;
149 desc.ns_caller = 0;
150 desc.sfn = (sfn_t)part->static_data.partition_init;
151 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200152 res = tfm_core_sfn_request(&desc);
153 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100154 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100155 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200156 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000157 fail_cnt++;
158 }
159 }
160 }
161
Miklos Balint6a139ae2018-04-04 19:44:37 +0200162 tfm_secure_api_init_done();
163
Miklos Balint386b8b52017-11-29 13:12:32 +0000164 if (fail_cnt == 0) {
165 return SPM_ERR_OK;
166 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100167 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000168 }
169}
170
171#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100172enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000173{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200174 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100175 if (!g_spm_partition_db.is_init) {
176 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000177 }
178
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100179 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000180
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200181 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
182 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000183
Miklos Balint386b8b52017-11-29 13:12:32 +0000184}
185
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100186enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000187{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100188 /* This function takes a partition id and disables the
189 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000190 */
191
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200192 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000193
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100194 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000195
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200196 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
197 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000198}
199
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100200uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000201{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100202 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200203 memory_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000204}
205
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100206uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000207{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200208 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000209}
210
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100211void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000212{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100213 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100214 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000215}
216#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100217
Miklos Balintace4c3f2018-07-30 12:31:15 +0200218void tfm_spm_partition_store_context(uint32_t partition_idx,
219 uint32_t stack_ptr, uint32_t lr)
220{
221 g_spm_partition_db.partitions[partition_idx].
222 runtime_data.stack_ptr = stack_ptr;
223 g_spm_partition_db.partitions[partition_idx].
224 runtime_data.lr = lr;
225}
226
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100227uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100228{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100229 return g_spm_partition_db.partitions[partition_idx].static_data.
230 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100231}
232
Mate Toth-Pal59398712018-02-28 17:06:40 +0100233uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
234{
235 return g_spm_partition_db.partitions[partition_idx].static_data.
236 partition_flags;
237}
238
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100239const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100240 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100241{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100242 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
243}
244
245void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
246{
247 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
248 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100249 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100250 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100251 }
252}
253
Miklos Balint6a139ae2018-04-04 19:44:37 +0200254void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
255 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100256{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100257 g_spm_partition_db.partitions[partition_idx].runtime_data.
258 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100259}
260
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100261enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100262 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100263{
264 enum spm_err_t ret = SPM_ERR_OK;
265
266#if TFM_LVL != 1
267 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200268 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100269#endif
270
271 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100272 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100273 }
274 return ret;
275}
276
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100277uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100278{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100279 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100280}
281
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100282void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100283{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200284 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100285 &(g_spm_partition_db.partitions[partition_idx]);
286 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100287 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100288}