Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [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 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 8 | /* All the APIs defined in this file are common for library and IPC model. */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 9 | |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 10 | #include "tfm/spm_api.h" |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 11 | #include "tfm/spm_db.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 12 | |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame^] | 13 | /* Extern SPM variable */ |
| 14 | extern struct spm_partition_db_t g_spm_partition_db; |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 15 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 16 | uint32_t get_partition_idx(uint32_t partition_id) |
| 17 | { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 18 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 19 | |
| 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 Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 25 | if (g_spm_partition_db.partitions[i].static_data->partition_id == |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 26 | partition_id) { |
| 27 | return i; |
| 28 | } |
| 29 | } |
| 30 | return SPM_INVALID_PARTITION_IDX; |
| 31 | } |
| 32 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 33 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 34 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 35 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 36 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 39 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 40 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 41 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 42 | partition_flags; |
| 43 | } |
| 44 | |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [diff] [blame] | 45 | uint32_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 | |
| 54 | bool 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 Ai | b557135 | 2019-03-22 10:49:52 +0800 | [diff] [blame] | 62 | __attribute__((section("SFN"))) |
| 63 | void 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 | } |