1 /*
2  * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdbool.h>
9 #include "tfm_platform_api.h"
10 #include "psa/client.h"
11 #include "psa_manifest/sid.h"
12 
tfm_platform_system_reset(void)13 enum tfm_platform_err_t tfm_platform_system_reset(void)
14 {
15     psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
16 
17     status = psa_call(TFM_PLATFORM_SERVICE_HANDLE,
18                       TFM_PLATFORM_API_ID_SYSTEM_RESET,
19                       NULL, 0, NULL, 0);
20 
21     if (status < PSA_SUCCESS) {
22         return TFM_PLATFORM_ERR_SYSTEM_ERROR;
23     } else {
24         return (enum tfm_platform_err_t)status;
25     }
26 
27 }
28 
29 enum tfm_platform_err_t
tfm_platform_ioctl(tfm_platform_ioctl_req_t request,psa_invec * input,psa_outvec * output)30 tfm_platform_ioctl(tfm_platform_ioctl_req_t request,
31                    psa_invec *input, psa_outvec *output)
32 {
33     tfm_platform_ioctl_req_t req = request;
34     struct psa_invec in_vec[2] = { {0} };
35     size_t inlen, outlen;
36     psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
37 
38     in_vec[0].base = &req;
39     in_vec[0].len = sizeof(req);
40     if (input != NULL) {
41         in_vec[1].base = input->base;
42         in_vec[1].len = input->len;
43         inlen = 2;
44     } else {
45         inlen = 1;
46     }
47 
48     if (output != NULL) {
49         outlen = 1;
50     } else {
51         outlen = 0;
52     }
53 
54     status = psa_call(TFM_PLATFORM_SERVICE_HANDLE,
55                       TFM_PLATFORM_API_ID_IOCTL,
56                       in_vec, inlen,
57                       output, outlen);
58 
59     if (status < PSA_SUCCESS) {
60         return TFM_PLATFORM_ERR_SYSTEM_ERROR;
61     } else {
62         return (enum tfm_platform_err_t)status;
63     }
64 }
65 
66 enum tfm_platform_err_t
tfm_platform_nv_counter_increment(uint32_t counter_id)67 tfm_platform_nv_counter_increment(uint32_t counter_id)
68 {
69     psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
70     struct psa_invec in_vec[1];
71 
72     in_vec[0].base = &counter_id;
73     in_vec[0].len = sizeof(counter_id);
74 
75     status = psa_call(TFM_PLATFORM_SERVICE_HANDLE,
76                       TFM_PLATFORM_API_ID_NV_INCREMENT,
77                       in_vec, 1, (psa_outvec *)NULL, 0);
78 
79     if (status < PSA_SUCCESS) {
80         return TFM_PLATFORM_ERR_SYSTEM_ERROR;
81     } else {
82         return (enum tfm_platform_err_t)status;
83     }
84 }
85 
86 enum tfm_platform_err_t
tfm_platform_nv_counter_read(uint32_t counter_id,uint32_t size,uint8_t * val)87 tfm_platform_nv_counter_read(uint32_t counter_id,
88                              uint32_t size, uint8_t *val)
89 {
90     psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
91     struct psa_invec in_vec[1];
92     struct psa_outvec out_vec[1];
93 
94     in_vec[0].base = &counter_id;
95     in_vec[0].len = sizeof(counter_id);
96 
97     out_vec[0].base = val;
98     out_vec[0].len = size;
99 
100     status = psa_call(TFM_PLATFORM_SERVICE_HANDLE,
101                       TFM_PLATFORM_API_ID_NV_READ,
102                       in_vec, 1, out_vec, 1);
103 
104     if (status < PSA_SUCCESS) {
105         return TFM_PLATFORM_ERR_SYSTEM_ERROR;
106     } else {
107         return (enum tfm_platform_err_t)status;
108     }
109 }
110