blob: 3167e5a2bb8ae06c6083bfb79192a85a99bacfe9 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Edison Aibb614aa2018-11-21 15:15:00 +08002 * Copyright (c) 2017-2019, 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"
Tamas Ban997aeb32018-11-19 13:28:32 +000014#include "secure_utilities.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020015#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020016#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010017#include "tfm_api.h"
Mate Toth-Palce61afa2018-08-03 13:51:01 +020018#include "tfm_nspm.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000019#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020020#include "platform_retarget.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020021#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020022#include "spm_partition_defs.h"
23
Miklos Balint386b8b52017-11-29 13:12:32 +000024
Mate Toth-Pal349714a2018-02-23 15:30:24 +010025struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000026
Miklos Balint386b8b52017-11-29 13:12:32 +000027typedef enum {
28 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010029} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000030
31/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010032 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010033 * In case of an error in the error handling, a non-zero value have to be
34 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000035 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010036static void tfm_spm_partition_err_handler(
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020037 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010038 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010039 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000040{
Miklos Balint386b8b52017-11-29 13:12:32 +000041#ifdef TFM_CORE_DEBUG
42 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010043 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010044 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000045 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010046 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010047 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000048 }
49#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010050 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010051 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000052}
53
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010054uint32_t get_partition_idx(uint32_t partition_id)
55{
56 int i;
57
58 if (partition_id == INVALID_PARTITION_ID) {
59 return SPM_INVALID_PARTITION_IDX;
60 }
61
62 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
63 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
64 partition_id) {
65 return i;
66 }
67 }
68 return SPM_INVALID_PARTITION_IDX;
69}
70
Miklos Balint386b8b52017-11-29 13:12:32 +000071enum spm_err_t tfm_spm_db_init(void)
72{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020073 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010074
Tamas Ban997aeb32018-11-19 13:28:32 +000075 tfm_memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010076
Mate Toth-Pal349714a2018-02-23 15:30:24 +010077 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010078 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
79 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000080
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010081 /* There are a few partitions that are used by TF-M internally.
82 * These are explicitly added to the partition db here.
83 */
84
85 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020086#if TFM_LVL != 1
Tamas Ban56ef3022018-09-13 23:49:16 +010087 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
88 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
89 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
90 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balint6a139ae2018-04-04 19:44:37 +020091#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010092 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
93 return SPM_ERR_INVALID_CONFIG;
94 }
95 part_ptr = &(g_spm_partition_db.partitions[
96 g_spm_partition_db.partition_count]);
97 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010098 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +020099
100#if TFM_LVL != 1
Tamas Ban56ef3022018-09-13 23:49:16 +0100101 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
102 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200103 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100104 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200105 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100106 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200107#endif
108
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100109 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Miklos Balint12735bc2018-08-01 15:45:18 +0200110 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100111 ++g_spm_partition_db.partition_count;
112
113 /* For the TF-M core environment itself */
114 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
115 return SPM_ERR_INVALID_CONFIG;
116 }
117 part_ptr = &(g_spm_partition_db.partitions[
118 g_spm_partition_db.partition_count]);
119 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100120 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800121 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100122 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
123 ++g_spm_partition_db.partition_count;
124
125 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200126 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000127
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100128 g_spm_partition_db.is_init = 1;
129
Miklos Balint386b8b52017-11-29 13:12:32 +0000130 return SPM_ERR_OK;
131}
132
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100133enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000134{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200135 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200136 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200137 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000138 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100139 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000140
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100141 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100142 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
143 part = &g_spm_partition_db.partitions[idx];
Edison Aibb614aa2018-11-21 15:15:00 +0800144#ifdef TFM_PSA_API
145 if (part->static_data.partition_flags & SPM_PART_FLAG_IPC) {
146 continue;
147 }
148#endif
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200149 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100150 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100151 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200152 tfm_spm_partition_set_caller_partition_idx(idx,
153 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100154 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200155 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200156
157 desc.args = args;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200158 desc.ns_caller = 0;
159 desc.sfn = (sfn_t)part->static_data.partition_init;
160 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200161 res = tfm_core_sfn_request(&desc);
162 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100163 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100164 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200165 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000166 fail_cnt++;
167 }
168 }
169 }
170
Miklos Balint6a139ae2018-04-04 19:44:37 +0200171 tfm_secure_api_init_done();
172
Miklos Balint386b8b52017-11-29 13:12:32 +0000173 if (fail_cnt == 0) {
174 return SPM_ERR_OK;
175 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100176 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000177 }
178}
179
180#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100181enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000182{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200183 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100184 if (!g_spm_partition_db.is_init) {
185 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000186 }
187
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100188 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000189
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200190 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
191 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000192
Miklos Balint386b8b52017-11-29 13:12:32 +0000193}
194
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100195enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000196{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100197 /* This function takes a partition id and disables the
198 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000199 */
200
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200201 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000202
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100203 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000204
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200205 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
206 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000207}
208
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100209uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000210{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100211 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200212 memory_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000213}
214
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100215uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000216{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200217 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000218}
219
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200220uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
221{
222 return g_spm_partition_db.partitions[partition_idx].
223 memory_data.zi_start;
224}
225
226uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
227{
228 return g_spm_partition_db.partitions[partition_idx].
229 memory_data.zi_limit;
230}
231
232uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
233{
234 return g_spm_partition_db.partitions[partition_idx].
235 memory_data.rw_start;
236}
237
238uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
239{
240 return g_spm_partition_db.partitions[partition_idx].
241 memory_data.rw_limit;
242}
243
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100244void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000245{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100246 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100247 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000248}
249#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100250
Miklos Balintace4c3f2018-07-30 12:31:15 +0200251void tfm_spm_partition_store_context(uint32_t partition_idx,
252 uint32_t stack_ptr, uint32_t lr)
253{
254 g_spm_partition_db.partitions[partition_idx].
255 runtime_data.stack_ptr = stack_ptr;
256 g_spm_partition_db.partitions[partition_idx].
257 runtime_data.lr = lr;
258}
259
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100260uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100261{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100262 return g_spm_partition_db.partitions[partition_idx].static_data.
263 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100264}
265
Mate Toth-Pal59398712018-02-28 17:06:40 +0100266uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
267{
268 return g_spm_partition_db.partitions[partition_idx].static_data.
269 partition_flags;
270}
271
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100272const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100273 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100274{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100275 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
276}
277
278void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
279{
280 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
281 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100282 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100283 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100284 }
285}
286
Miklos Balint6a139ae2018-04-04 19:44:37 +0200287void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
288 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100289{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100290 g_spm_partition_db.partitions[partition_idx].runtime_data.
291 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100292}
293
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200294void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
295 int32_t caller_client_id)
296{
297 g_spm_partition_db.partitions[partition_idx].runtime_data.
298 caller_client_id = caller_client_id;
299}
300
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100301enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100302 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100303{
304 enum spm_err_t ret = SPM_ERR_OK;
305
306#if TFM_LVL != 1
307 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200308 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100309#endif
310
311 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100312 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100313 }
314 return ret;
315}
316
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100317uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100318{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100319 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100320}
321
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100322void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100323{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200324 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100325 &(g_spm_partition_db.partitions[partition_idx]);
326 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100327 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100328}