1 /*
2  * Copyright (c) 2022-2024, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <assert.h>
9 #include <string.h>
10 
11 #include "rse_comms_protocol_common.h"
12 #include "rse_comms_protocol_embed.h"
13 
rse_protocol_embed_serialize_msg(psa_handle_t handle,int16_t type,const struct psa_invec * in_vec,uint8_t in_len,const struct psa_outvec * out_vec,uint8_t out_len,struct rse_embed_msg_t * msg,size_t * msg_len)14 psa_status_t rse_protocol_embed_serialize_msg(psa_handle_t handle,
15 					      int16_t type,
16 					      const struct psa_invec *in_vec,
17 					      uint8_t in_len,
18 					      const struct psa_outvec *out_vec,
19 					      uint8_t out_len,
20 					      struct rse_embed_msg_t *msg,
21 					      size_t *msg_len)
22 {
23 	uint32_t payload_size = 0;
24 	uint32_t i;
25 
26 	assert(msg != NULL);
27 	assert(msg_len != NULL);
28 	assert(in_vec != NULL);
29 
30 	msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
31 	msg->handle = handle;
32 
33 	/* Fill msg iovec lengths */
34 	for (i = 0U; i < in_len; ++i) {
35 		msg->io_size[i] = in_vec[i].len;
36 	}
37 	for (i = 0U; i < out_len; ++i) {
38 		msg->io_size[in_len + i] = out_vec[i].len;
39 	}
40 
41 	for (i = 0U; i < in_len; ++i) {
42 		if (in_vec[i].len > sizeof(msg->trailer) - payload_size) {
43 			return PSA_ERROR_INVALID_ARGUMENT;
44 		}
45 		memcpy(msg->trailer + payload_size,
46 		       psa_u32_to_ptr(in_vec[i].base),
47 		       in_vec[i].len);
48 		payload_size += in_vec[i].len;
49 	}
50 
51 	/* Output the actual size of the message, to optimize sending */
52 	*msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
53 
54 	return PSA_SUCCESS;
55 }
56 
rse_protocol_embed_deserialize_reply(struct psa_outvec * out_vec,uint8_t out_len,psa_status_t * return_val,const struct rse_embed_reply_t * reply,size_t reply_size)57 psa_status_t rse_protocol_embed_deserialize_reply(struct psa_outvec *out_vec,
58 						  uint8_t out_len,
59 						  psa_status_t *return_val,
60 						  const struct rse_embed_reply_t *reply,
61 						  size_t reply_size)
62 {
63 	uint32_t payload_offset = 0;
64 	uint32_t i;
65 
66 	assert(reply != NULL);
67 	assert(return_val != NULL);
68 
69 	for (i = 0U; i < out_len; ++i) {
70 		if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
71 		    > reply_size) {
72 			return PSA_ERROR_INVALID_ARGUMENT;
73 		}
74 
75 		memcpy(psa_u32_to_ptr(out_vec[i].base),
76 		       reply->trailer + payload_offset,
77 		       reply->out_size[i]);
78 		out_vec[i].len = reply->out_size[i];
79 		payload_offset += reply->out_size[i];
80 	}
81 
82 	*return_val = reply->return_val;
83 
84 	return PSA_SUCCESS;
85 }
86 
rse_protocol_embed_calculate_msg_len(psa_handle_t handle,const struct psa_invec * in_vec,uint8_t in_len,size_t * msg_len)87 psa_status_t rse_protocol_embed_calculate_msg_len(psa_handle_t handle,
88 						  const struct psa_invec *in_vec,
89 						  uint8_t in_len,
90 						  size_t *msg_len)
91 {
92 	uint32_t payload_size = 0;
93 	uint32_t i = 0;
94 
95 	assert(in_vec != NULL);
96 	assert(msg_len != NULL);
97 
98 	for (i = 0U; i < in_len; ++i)
99 		payload_size += in_vec[i].len;
100 
101 	/* Output the actual size of the message, to optimize sending */
102 	*msg_len = offsetof(struct rse_embed_msg_t, trailer) + payload_size;
103 
104 	return PSA_SUCCESS;
105 }
106