blob: 5ff1dc3d1d79d29eeb18fc88fa1be6129f349912 [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
Ken Liu1f345b02020-05-30 21:11:05 +080010#include "tfm/spm_api.h"
Ken Liu1f345b02020-05-30 21:11:05 +080011#include "tfm/spm_db.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020012
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080013/* Extern SPM variable */
14extern struct spm_partition_db_t g_spm_partition_db;
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020015
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010016uint32_t get_partition_idx(uint32_t partition_id)
17{
Hugues de Valonf704c802019-02-19 14:51:41 +000018 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010019
20 if (partition_id == INVALID_PARTITION_ID) {
21 return SPM_INVALID_PARTITION_IDX;
22 }
23
24 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
Summer Qin423dbef2019-08-22 15:59:35 +080025 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010026 partition_id) {
27 return i;
28 }
29 }
30 return SPM_INVALID_PARTITION_IDX;
31}
32
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010033uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +010034{
Summer Qin423dbef2019-08-22 15:59:35 +080035 return g_spm_partition_db.partitions[partition_idx].static_data->
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010036 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +010037}
38
Mate Toth-Pal59398712018-02-28 17:06:40 +010039uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
40{
Summer Qin423dbef2019-08-22 15:59:35 +080041 return g_spm_partition_db.partitions[partition_idx].static_data->
Mate Toth-Pal59398712018-02-28 17:06:40 +010042 partition_flags;
43}
44
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +010045uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
46{
47 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
48 return TFM_PARTITION_PRIVILEGED_MODE;
49 } else {
50 return TFM_PARTITION_UNPRIVILEGED_MODE;
51 }
52}
53
54bool tfm_is_partition_privileged(uint32_t partition_idx)
55{
56 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
57
58 return tfm_spm_partition_get_privileged_mode(flags) ==
59 TFM_PARTITION_PRIVILEGED_MODE;
60}
61
Edison Aib5571352019-03-22 10:49:52 +080062__attribute__((section("SFN")))
63void tfm_spm_partition_change_privilege(uint32_t privileged)
64{
65 CONTROL_Type ctrl;
66
67 ctrl.w = __get_CONTROL();
68
69 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
70 ctrl.b.nPRIV = 0;
71 } else {
72 ctrl.b.nPRIV = 1;
73 }
74
75 __set_CONTROL(ctrl.w);
76}