blob: 072e243ebcba0834f2a35cc391e0c367339cdb8f [file] [log] [blame]
Mate Toth-Pal179a1562019-11-08 11:40:27 +01001/*
Raef Coles249aba92022-06-16 10:20:29 +01002 * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
Mate Toth-Pal179a1562019-11-08 11:40:27 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdbool.h>
9#include "tfm_platform_api.h"
10#include "psa_manifest/sid.h"
11
12enum tfm_platform_err_t tfm_platform_system_reset(void)
13{
14 psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
15 psa_handle_t handle = PSA_NULL_HANDLE;
16
17 handle = psa_connect(TFM_SP_PLATFORM_SYSTEM_RESET_SID,
18 TFM_SP_PLATFORM_SYSTEM_RESET_VERSION);
19 if (handle <= 0) {
20 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
21 }
22
23 status = psa_call(handle, PSA_IPC_CALL,
24 NULL, 0, NULL, 0);
25 psa_close(handle);
26
27 if (status < PSA_SUCCESS) {
28 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
29 } else {
Raef Coles249aba92022-06-16 10:20:29 +010030 return (enum tfm_platform_err_t)status;
Mate Toth-Pal179a1562019-11-08 11:40:27 +010031 }
32
33}
34
35enum tfm_platform_err_t
36tfm_platform_ioctl(tfm_platform_ioctl_req_t request,
37 psa_invec *input, psa_outvec *output)
38{
39 tfm_platform_ioctl_req_t req = request;
40 struct psa_invec in_vec[2] = { {0} };
41 size_t inlen, outlen;
42 psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
43 psa_handle_t handle = PSA_NULL_HANDLE;
44
45 in_vec[0].base = &req;
46 in_vec[0].len = sizeof(req);
47 if (input != NULL) {
48 in_vec[1].base = input->base;
49 in_vec[1].len = input->len;
50 inlen = 2;
51 } else {
52 inlen = 1;
53 }
54
55 if (output != NULL) {
56 outlen = 1;
57 } else {
58 outlen = 0;
59 }
60
61 handle = psa_connect(TFM_SP_PLATFORM_IOCTL_SID,
62 TFM_SP_PLATFORM_IOCTL_VERSION);
63 if (handle <= 0) {
64 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
65 }
66
67 status = psa_call(handle, PSA_IPC_CALL,
68 in_vec, inlen,
69 output, outlen);
70 psa_close(handle);
71
72 if (status < PSA_SUCCESS) {
73 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
74 } else {
Raef Coles249aba92022-06-16 10:20:29 +010075 return (enum tfm_platform_err_t)status;
Mate Toth-Pal179a1562019-11-08 11:40:27 +010076 }
77}
78
Raef Coles249aba92022-06-16 10:20:29 +010079enum tfm_platform_err_t
80tfm_platform_nv_counter_increment(uint32_t counter_id)
81{
82 psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
83 psa_handle_t handle = PSA_NULL_HANDLE;
84 struct psa_invec in_vec[1];
85
86 in_vec[0].base = &counter_id;
87 in_vec[0].len = sizeof(counter_id);
88
89 handle = psa_connect(TFM_SP_PLATFORM_NV_COUNTER_SID,
90 TFM_SP_PLATFORM_NV_COUNTER_VERSION);
91 if (handle <= 0) {
92 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
93 }
94
95 status = psa_call(handle, TFM_PLATFORM_API_ID_NV_INCREMENT,
96 in_vec, 1, (psa_outvec *)NULL, 0);
97
98 psa_close(handle);
99
100 if (status < PSA_SUCCESS) {
101 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
102 } else {
103 return (enum tfm_platform_err_t)status;
104 }
105}
106
107enum tfm_platform_err_t
108tfm_platform_nv_counter_read(uint32_t counter_id,
109 uint32_t size, uint8_t *val)
110{
111 psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
112 psa_handle_t handle = PSA_NULL_HANDLE;
113 struct psa_invec in_vec[1];
114 struct psa_outvec out_vec[1];
115
116 in_vec[0].base = &counter_id;
117 in_vec[0].len = sizeof(counter_id);
118
119 out_vec[0].base = val;
120 out_vec[0].len = size;
121
122 handle = psa_connect(TFM_SP_PLATFORM_NV_COUNTER_SID,
123 TFM_SP_PLATFORM_NV_COUNTER_VERSION);
124 if (handle <= 0) {
125 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
126 }
127
128 status = psa_call(handle, TFM_PLATFORM_API_ID_NV_READ,
129 in_vec, 1, out_vec, 1);
130
131 psa_close(handle);
132
133 if (status < PSA_SUCCESS) {
134 return TFM_PLATFORM_ERR_SYSTEM_ERROR;
135 } else {
136 return (enum tfm_platform_err_t)status;
137 }
138}