blob: 044e33651a7dbdb25e21a3afb4b3e2d552460fd3 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +01002 * 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
Mingyang Sunda01a972019-07-12 17:32:59 +08008/* All the APIs defined in this file are common for library and IPC model. */
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"
Mingyang Sunf3d29892019-07-10 17:50:23 +080013#include "tfm_spm_hal.h"
Tamas Ban8bd24b72019-02-19 12:13:13 +000014#include "tfm_memory_utils.h"
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020015#include "spm_db.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"
David Hu49a28eb2019-08-14 18:18:15 +080019#include "tfm_core.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020020#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020021#include "spm_partition_defs.h"
Shawn Shan6e7be072019-11-29 17:56:29 +080022#include "psa/lifecycle.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020023
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020024#define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0
25#define TFM_CORE_INTERNAL_PARTITION_DB_IDX 1
Miklos Balint386b8b52017-11-29 13:12:32 +000026
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020027/* Define SPM DB structure */
Edison Aia8e751e2019-10-12 14:08:15 +080028#include "tfm_spm_db.inc"
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020029
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010030uint32_t get_partition_idx(uint32_t partition_id)
31{
Hugues de Valonf704c802019-02-19 14:51:41 +000032 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010033
34 if (partition_id == INVALID_PARTITION_ID) {
35 return SPM_INVALID_PARTITION_IDX;
36 }
37
38 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
Summer Qin423dbef2019-08-22 15:59:35 +080039 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010040 partition_id) {
41 return i;
42 }
43 }
44 return SPM_INVALID_PARTITION_IDX;
45}
46
Miklos Balint386b8b52017-11-29 13:12:32 +000047enum spm_err_t tfm_spm_db_init(void)
48{
Summer Qin423dbef2019-08-22 15:59:35 +080049 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010050
Mate Toth-Pal349714a2018-02-23 15:30:24 +010051 /* This function initialises partition db */
Miklos Balint386b8b52017-11-29 13:12:32 +000052
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010053 /* For the non secure Execution environment */
Edison Ai66fbdf12019-07-08 16:05:07 +080054#ifndef TFM_PSA_API
Miklos Balint12735bc2018-08-01 15:45:18 +020055 tfm_nspm_configure_clients();
Summer Qin423dbef2019-08-22 15:59:35 +080056#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010057
Summer Qin423dbef2019-08-22 15:59:35 +080058 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
59#ifndef TFM_PSA_API
60 g_spm_partition_db.partitions[i].runtime_data.partition_state =
61 SPM_PARTITION_STATE_UNINIT;
62 g_spm_partition_db.partitions[i].runtime_data.caller_partition_idx =
63 SPM_INVALID_PARTITION_IDX;
64 g_spm_partition_db.partitions[i].runtime_data.caller_client_id =
65 TFM_INVALID_CLIENT_ID;
Summer Qin423dbef2019-08-22 15:59:35 +080066 g_spm_partition_db.partitions[i].runtime_data.ctx_stack_ptr =
67 ctx_stack_list[i];
Edison Ai66fbdf12019-07-08 16:05:07 +080068#endif /* !defined(TFM_PSA_API) */
Summer Qin423dbef2019-08-22 15:59:35 +080069 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +010070 g_spm_partition_db.partitions[i].platform_data_list =
71 platform_data_list_list[i];
Summer Qin423dbef2019-08-22 15:59:35 +080072#ifdef TFM_PSA_API
73 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
74#endif
75 }
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010076 g_spm_partition_db.is_init = 1;
77
Miklos Balint386b8b52017-11-29 13:12:32 +000078 return SPM_ERR_OK;
79}
80
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010081uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +010082{
Summer Qin423dbef2019-08-22 15:59:35 +080083 return g_spm_partition_db.partitions[partition_idx].static_data->
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010084 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +010085}
86
Mate Toth-Pal59398712018-02-28 17:06:40 +010087uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
88{
Summer Qin423dbef2019-08-22 15:59:35 +080089 return g_spm_partition_db.partitions[partition_idx].static_data->
Mate Toth-Pal59398712018-02-28 17:06:40 +010090 partition_flags;
91}
92
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +010093uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
94{
95 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
96 return TFM_PARTITION_PRIVILEGED_MODE;
97 } else {
98 return TFM_PARTITION_UNPRIVILEGED_MODE;
99 }
100}
101
102bool tfm_is_partition_privileged(uint32_t partition_idx)
103{
104 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
105
106 return tfm_spm_partition_get_privileged_mode(flags) ==
107 TFM_PARTITION_PRIVILEGED_MODE;
108}
109
Shawn Shan6e7be072019-11-29 17:56:29 +0800110uint32_t tfm_spm_get_lifecycle_state(void)
111{
112 /*
113 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
114 * implemented in the future.
115 */
116 return PSA_LIFECYCLE_UNKNOWN;
117}
118
Edison Aib5571352019-03-22 10:49:52 +0800119__attribute__((section("SFN")))
120void tfm_spm_partition_change_privilege(uint32_t privileged)
121{
122 CONTROL_Type ctrl;
123
124 ctrl.w = __get_CONTROL();
125
126 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
127 ctrl.b.nPRIV = 0;
128 } else {
129 ctrl.b.nPRIV = 1;
130 }
131
132 __set_CONTROL(ctrl.w);
133}