Skip to content

Commit 2d71713

Browse files
committed
Add ioctl platform service
Define weak functions that the platforms can overwrite Change-Id: I3d6f3e59a9c5b88456875a495fa6decc1337683e Signed-off-by: Devaraj Ranganna <[email protected]> Signed-off-by: Gabor Abonyi <[email protected]>
1 parent c12b433 commit 2d71713

File tree

11 files changed

+253
-6
lines changed

11 files changed

+253
-6
lines changed

components/TARGET_PSA/TARGET_MBED_SPM/COMPONENT_SPE/psa_setup.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ spm_rot_service_t platform_rot_services[] = {
395395
.tail = NULL
396396
}
397397
},
398+
{
399+
.sid = PSA_PLATFORM_IOCTL,
400+
.mask = PSA_PLATFORM_IOCTL_MSK,
401+
.partition = NULL,
402+
.min_version = 1,
403+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
404+
.allow_nspe = true,
405+
.queue = {
406+
.head = NULL,
407+
.tail = NULL
408+
}
409+
},
398410
};
399411

400412
/* External SIDs used by PLATFORM */

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/inc/tfm_service_list.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
{"PSA_PLATFORM_LC_GET", PLATFORM_ID, PSA_PLATFORM_LC_GET_MSK, 0x00011000, true, 1, TFM_VERSION_POLICY_RELAXED},
4646
{"PSA_PLATFORM_LC_SET", PLATFORM_ID, PSA_PLATFORM_LC_SET_MSK, 0x00011001, true, 1, TFM_VERSION_POLICY_RELAXED},
4747
{"PSA_PLATFORM_SYSTEM_RESET", PLATFORM_ID, PSA_PLATFORM_SYSTEM_RESET_MSK, 0x00011002, true, 1, TFM_VERSION_POLICY_RELAXED},
48+
{"PSA_PLATFORM_IOCTL", PLATFORM_ID, PSA_PLATFORM_IOCTL_MSK, 0x00011003, true, 1, TFM_VERSION_POLICY_RELAXED},
4849

4950
/* -----------------------------------------------------------------------------
5051
* ITS Services

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/inc/tfm_spm_signal_defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#define PSA_PLATFORM_LC_SET_MSK (1UL << PSA_PLATFORM_LC_SET_MSK_POS)
6363
#define PSA_PLATFORM_SYSTEM_RESET_MSK_POS (6UL)
6464
#define PSA_PLATFORM_SYSTEM_RESET_MSK (1UL << PSA_PLATFORM_SYSTEM_RESET_MSK_POS)
65+
#define PSA_PLATFORM_IOCTL_MSK_POS (7UL)
66+
#define PSA_PLATFORM_IOCTL_MSK (1UL << PSA_PLATFORM_IOCTL_MSK_POS)
6567

6668
/* -----------------------------------------------------------------------------
6769
* ITS Signals
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*
6+
*/
7+
8+
#ifndef __TFM_PLATFORM_API__
9+
#define __TFM_PLATFORM_API__
10+
11+
#include <limits.h>
12+
#include <stdint.h>
13+
#include "psa/client.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
/**
20+
* \brief TFM secure partition platform API version
21+
*/
22+
#define TFM_PLATFORM_API_VERSION_MAJOR (0)
23+
#define TFM_PLATFORM_API_VERSION_MINOR (3)
24+
25+
/*!
26+
* \enum tfm_platform_err_t
27+
*
28+
* \brief Platform service error types
29+
*
30+
*/
31+
enum tfm_platform_err_t {
32+
TFM_PLATFORM_ERR_SUCCESS = 0,
33+
TFM_PLATFORM_ERR_SYSTEM_ERROR,
34+
TFM_PLATFORM_ERR_INVALID_PARAM,
35+
TFM_PLATFORM_ERR_NOT_SUPPORTED,
36+
37+
/* Following entry is only to ensure the error code of int size */
38+
TFM_PLATFORM_ERR_FORCE_INT_SIZE = INT_MAX
39+
};
40+
41+
typedef int32_t tfm_platform_ioctl_req_t;
42+
43+
/*!
44+
* \brief Performs a platform-specific service
45+
*
46+
* \param[in] request Request identifier (valid values vary
47+
* based on the platform)
48+
* \param[in] input Input buffer to the requested service (or NULL)
49+
* \param[in,out] output Output buffer to the requested service (or NULL)
50+
*
51+
* \return Returns values as specified by the \ref tfm_platform_err_t
52+
*/
53+
enum tfm_platform_err_t tfm_platform_ioctl(tfm_platform_ioctl_req_t request,
54+
psa_invec *input,
55+
psa_outvec *output);
56+
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif
61+
62+
#endif /* __TFM_PLATFORM_API__ */

components/TARGET_PSA/services/inc/autogen_sid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define PSA_PLATFORM_LC_GET 0x00011000
5757
#define PSA_PLATFORM_LC_SET 0x00011001
5858
#define PSA_PLATFORM_SYSTEM_RESET 0x00011002
59+
#define PSA_PLATFORM_IOCTL 0x00011003
5960

6061
/* -----------------------------------------------------------------------------
6162
* ITS Service IDs

components/TARGET_PSA/services/inc/mbed_spm_partitions.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
* -------------------------------------------------------------------------- */
116116
#define PLATFORM_ID 8
117117

118-
#define PLATFORM_ROT_SRV_COUNT (3UL)
118+
#define PLATFORM_ROT_SRV_COUNT (4UL)
119119
#define PLATFORM_EXT_ROT_SRV_COUNT (1UL)
120120

121121

@@ -127,11 +127,14 @@
127127
#define PSA_PLATFORM_LC_SET_MSK (1UL << PSA_PLATFORM_LC_SET_MSK_POS)
128128
#define PSA_PLATFORM_SYSTEM_RESET_MSK_POS (6UL)
129129
#define PSA_PLATFORM_SYSTEM_RESET_MSK (1UL << PSA_PLATFORM_SYSTEM_RESET_MSK_POS)
130+
#define PSA_PLATFORM_IOCTL_MSK_POS (7UL)
131+
#define PSA_PLATFORM_IOCTL_MSK (1UL << PSA_PLATFORM_IOCTL_MSK_POS)
130132

131133
#define PLATFORM_WAIT_ANY_SID_MSK (\
132134
PSA_PLATFORM_LC_GET_MSK | \
133135
PSA_PLATFORM_LC_SET_MSK | \
134-
PSA_PLATFORM_SYSTEM_RESET_MSK)
136+
PSA_PLATFORM_SYSTEM_RESET_MSK | \
137+
PSA_PLATFORM_IOCTL_MSK)
135138

136139

137140
/* -----------------------------------------------------------------------------

components/TARGET_PSA/services/platform/COMPONENT_PSA_SRV_IMPL/platform_srv_impl.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019 ARM Limited
1+
/* Copyright (c) 2019-2020 Arm Limited
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -45,3 +45,12 @@ MBED_WEAK void mbed_psa_system_reset_impl(void)
4545
/* Reset the system */
4646
NVIC_SystemReset();
4747
}
48+
49+
MBED_WEAK enum tfm_platform_err_t tfm_platform_hal_ioctl(tfm_platform_ioctl_req_t request,
50+
psa_invec *in_vec,
51+
psa_outvec *out_vec)
52+
{
53+
(void)in_vec;
54+
(void)out_vec;
55+
return TFM_PLATFORM_ERR_NOT_SUPPORTED;
56+
}

components/TARGET_PSA/services/platform/COMPONENT_PSA_SRV_IMPL/platform_srv_impl.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019 ARM Limited
1+
/* Copyright (c) 2019-2020 Arm Limited
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -19,10 +19,25 @@
1919
#define __PLATFROM_SRV_IMPL_H__
2020

2121
#include "psa/client.h"
22+
#include "psa/lifecycle.h"
2223
#include "mbed_toolchain.h"
24+
#include "tfm_platform_api.h"
2325

2426
psa_status_t psa_platfrom_lifecycle_get_impl(uint32_t *lc_state);
2527
psa_status_t psa_platfrom_lifecycle_change_request_impl(uint32_t lc_state);
2628
MBED_NORETURN void mbed_psa_system_reset_impl(void);
2729

30+
/*!
31+
* \brief Performs a platform-specific service
32+
*
33+
* \param[in] request Request identifier (valid values vary
34+
* based on the platform)
35+
* \param[in] in_vec Input buffer to the requested service (or NULL)
36+
* \param[out] out_vec Output buffer to the requested service (or NULL)
37+
*
38+
* \return Returns values as specified by the \ref tfm_platform_err_t
39+
*/
40+
enum tfm_platform_err_t tfm_platform_hal_ioctl(tfm_platform_ioctl_req_t request,
41+
psa_invec *in_vec,
42+
psa_outvec *out_vec);
2843
#endif // __PLATFROM_SRV_IMPL_H__

components/TARGET_PSA/services/platform/COMPONENT_PSA_SRV_IPC/platform_ipc.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019 ARM Limited
1+
/* Copyright (c) 2019-2020 Arm Limited
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -20,6 +20,7 @@
2020
#include "psa/client.h"
2121
#include "mbed_toolchain.h"
2222
#include "mbed_error.h"
23+
#include "tfm_platform_api.h"
2324

2425
uint32_t psa_security_lifecycle_state(void)
2526
{
@@ -66,3 +67,46 @@ void mbed_psa_system_reset(void)
6667
}
6768
error("reset failed - cannot connect to service handle=%ld", conn);
6869
}
70+
71+
enum tfm_platform_err_t
72+
tfm_platform_ioctl(tfm_platform_ioctl_req_t request,
73+
psa_invec *input, psa_outvec *output)
74+
{
75+
tfm_platform_ioctl_req_t req = request;
76+
struct psa_invec in_vec[2] = { {0} };
77+
size_t inlen, outlen;
78+
psa_status_t status = PSA_ERROR_CONNECTION_REFUSED;
79+
psa_handle_t handle = PSA_NULL_HANDLE;
80+
81+
in_vec[0].base = &req;
82+
in_vec[0].len = sizeof(req);
83+
if (input != NULL) {
84+
in_vec[1].base = input->base;
85+
in_vec[1].len = input->len;
86+
inlen = 2;
87+
} else {
88+
inlen = 1;
89+
}
90+
91+
if (output != NULL) {
92+
outlen = 1;
93+
} else {
94+
outlen = 0;
95+
}
96+
97+
handle = psa_connect(PSA_PLATFORM_IOCTL, 1);
98+
if (handle <= 0) {
99+
return TFM_PLATFORM_ERR_SYSTEM_ERROR;
100+
}
101+
102+
status = psa_call(handle,
103+
in_vec, inlen,
104+
output, outlen);
105+
psa_close(handle);
106+
107+
if (status < PSA_SUCCESS) {
108+
return TFM_PLATFORM_ERR_SYSTEM_ERROR;
109+
} else {
110+
return (enum tfm_platform_err_t) status;
111+
}
112+
}

components/TARGET_PSA/services/platform/COMPONENT_SPE/platform_partition.c

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019 ARM Limited
1+
/* Copyright (c) 2019-2020 Arm Limited
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -17,9 +17,13 @@
1717

1818
#include "mbed_spm_partitions.h"
1919
#include "platform_srv_impl.h"
20+
#include "psa/lifecycle.h"
2021
#include "psa/internal_trusted_storage.h"
2122
#include "psa/service.h"
2223

24+
#define INPUT_BUFFER_SIZE 64
25+
#define OUTPUT_BUFFER_SIZE 64
26+
2327
typedef psa_status_t (*SignalHandler)(psa_msg_t *);
2428

2529
static psa_status_t lifecycle_get(psa_msg_t *msg)
@@ -58,6 +62,86 @@ static MBED_NORETURN psa_status_t system_reset_request(psa_msg_t *msg)
5862
mbed_psa_system_reset_impl();
5963
}
6064

65+
static enum tfm_platform_err_t
66+
platform_sp_ioctl_ipc(const psa_msg_t *msg)
67+
{
68+
void *input = NULL;
69+
void *output = NULL;
70+
psa_invec invec = {0};
71+
psa_outvec outvec = {0};
72+
uint8_t input_buffer[INPUT_BUFFER_SIZE] = {0};
73+
uint8_t output_buffer[OUTPUT_BUFFER_SIZE] = {0};
74+
tfm_platform_ioctl_req_t request = 0;
75+
enum tfm_platform_err_t ret = TFM_PLATFORM_ERR_SYSTEM_ERROR;
76+
size_t num = 0;
77+
uint32_t in_len = PSA_MAX_IOVEC;
78+
uint32_t out_len = PSA_MAX_IOVEC;
79+
80+
while ((in_len > 0) && (msg->in_size[in_len - 1] == 0)) {
81+
in_len--;
82+
}
83+
84+
while ((out_len > 0) && (msg->out_size[out_len - 1] == 0)) {
85+
out_len--;
86+
}
87+
88+
if ((in_len < 1) || (in_len > 2) ||
89+
(out_len > 1)) {
90+
return TFM_PLATFORM_ERR_SYSTEM_ERROR;
91+
}
92+
93+
num = psa_read(msg->handle, 0, &request, sizeof(request));
94+
if (num != sizeof(request)) {
95+
return PSA_ERROR_PROGRAMMER_ERROR;
96+
}
97+
98+
if (in_len > 1) {
99+
if (msg->in_size[1] > INPUT_BUFFER_SIZE) {
100+
return PSA_ERROR_PROGRAMMER_ERROR;
101+
}
102+
num = psa_read(msg->handle, 1, &input_buffer, msg->in_size[1]);
103+
if (num != msg->in_size[1]) {
104+
return PSA_ERROR_PROGRAMMER_ERROR;
105+
}
106+
invec.base = input_buffer;
107+
invec.len = msg->in_size[1];
108+
input = &invec;
109+
}
110+
111+
if (out_len > 0) {
112+
if (msg->out_size[0] > OUTPUT_BUFFER_SIZE) {
113+
return PSA_ERROR_PROGRAMMER_ERROR;
114+
}
115+
outvec.base = output_buffer;
116+
outvec.len = msg->out_size[0];
117+
output = &outvec;
118+
}
119+
120+
ret = tfm_platform_hal_ioctl(request, input, output);
121+
122+
if (output != NULL) {
123+
psa_write(msg->handle, 0, outvec.base, outvec.len);
124+
}
125+
126+
return ret;
127+
}
128+
129+
static psa_status_t platform_ioctl(psa_msg_t *msg)
130+
{
131+
/* platform_sp_ioctl_ipc returns either psa_status_t or one of the
132+
* following errorcodes:
133+
* enum tfm_platform_err_t {
134+
* TFM_PLATFORM_ERR_SUCCESS = 0,
135+
* TFM_PLATFORM_ERR_SYSTEM_ERROR,
136+
* TFM_PLATFORM_ERR_INVALID_PARAM,
137+
* TFM_PLATFORM_ERR_NOT_SUPPORTED,
138+
*
139+
* TFM_PLATFORM_ERR_FORCE_INT_SIZE = INT_MAX
140+
* };
141+
*/
142+
return platform_sp_ioctl_ipc(msg);
143+
}
144+
61145
static void message_handler(psa_msg_t *msg, SignalHandler handler)
62146
{
63147
psa_status_t status = PSA_SUCCESS;
@@ -102,5 +186,11 @@ void platform_partition_entry(void *ptr)
102186
}
103187
message_handler(&msg, system_reset_request);
104188
}
189+
if ((signals & PSA_PLATFORM_IOCTL_MSK) != 0) {
190+
if (PSA_SUCCESS != psa_get(PSA_PLATFORM_IOCTL_MSK, &msg)) {
191+
continue;
192+
}
193+
message_handler(&msg, platform_ioctl);
194+
}
105195
}
106196
}

components/TARGET_PSA/services/platform/platform_psa.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
"non_secure_clients": true,
3030
"minor_version": 1,
3131
"minor_policy": "RELAXED"
32+
},
33+
{
34+
"name": "PSA_PLATFORM_IOCTL",
35+
"identifier": "0x00011003",
36+
"signal": "PSA_PLATFORM_IOCTL_MSK",
37+
"non_secure_clients": true,
38+
"minor_version": 1,
39+
"minor_policy": "RELAXED"
3240
}
3341
],
3442
"extern_sids": [

0 commit comments

Comments
 (0)