1 /*
2  * Copyright (c) 2022, 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 <common/debug.h>
12 #include "rss_comms_protocol_embed.h"
13 
14 #define TYPE_OFFSET	(16U)
15 #define TYPE_MASK	(0xFFFFUL << TYPE_OFFSET)
16 #define IN_LEN_OFFSET	(8U)
17 #define IN_LEN_MASK	(0xFFUL << IN_LEN_OFFSET)
18 #define OUT_LEN_OFFSET	(0U)
19 #define OUT_LEN_MASK	(0xFFUL << OUT_LEN_OFFSET)
20 
21 #define PARAM_PACK(type, in_len, out_len)			  \
22 	(((((uint32_t)type) << TYPE_OFFSET) & TYPE_MASK)	| \
23 	 ((((uint32_t)in_len) << IN_LEN_OFFSET) & IN_LEN_MASK)	| \
24 	 ((((uint32_t)out_len) << OUT_LEN_OFFSET) & OUT_LEN_MASK))
25 
rss_protocol_embed_serialize_msg(psa_handle_t handle,int16_t type,const psa_invec * in_vec,uint8_t in_len,const psa_outvec * out_vec,uint8_t out_len,struct rss_embed_msg_t * msg,size_t * msg_len)26 psa_status_t rss_protocol_embed_serialize_msg(psa_handle_t handle,
27 					      int16_t type,
28 					      const psa_invec *in_vec,
29 					      uint8_t in_len,
30 					      const psa_outvec *out_vec,
31 					      uint8_t out_len,
32 					      struct rss_embed_msg_t *msg,
33 					      size_t *msg_len)
34 {
35 	uint32_t payload_size = 0;
36 	uint32_t i;
37 
38 	assert(msg != NULL);
39 	assert(msg_len != NULL);
40 	assert(in_vec != NULL);
41 
42 	msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
43 	msg->handle = handle;
44 
45 	/* Fill msg iovec lengths */
46 	for (i = 0U; i < in_len; ++i) {
47 		msg->io_size[i] = in_vec[i].len;
48 	}
49 	for (i = 0U; i < out_len; ++i) {
50 		msg->io_size[in_len + i] = out_vec[i].len;
51 	}
52 
53 	for (i = 0U; i < in_len; ++i) {
54 		if (in_vec[i].len > sizeof(msg->trailer) - payload_size) {
55 			return PSA_ERROR_INVALID_ARGUMENT;
56 		}
57 		memcpy(msg->trailer + payload_size, in_vec[i].base, in_vec[i].len);
58 		payload_size += in_vec[i].len;
59 	}
60 
61 	/* Output the actual size of the message, to optimize sending */
62 	*msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
63 
64 	return PSA_SUCCESS;
65 }
66 
rss_protocol_embed_deserialize_reply(psa_outvec * out_vec,uint8_t out_len,psa_status_t * return_val,const struct rss_embed_reply_t * reply,size_t reply_size)67 psa_status_t rss_protocol_embed_deserialize_reply(psa_outvec *out_vec,
68 						  uint8_t out_len,
69 						  psa_status_t *return_val,
70 						  const struct rss_embed_reply_t *reply,
71 						  size_t reply_size)
72 {
73 	uint32_t payload_offset = 0;
74 	uint32_t i;
75 
76 	assert(reply != NULL);
77 	assert(return_val != NULL);
78 
79 	for (i = 0U; i < out_len; ++i) {
80 		if (sizeof(reply) - sizeof(reply->trailer) + payload_offset > reply_size) {
81 			return PSA_ERROR_INVALID_ARGUMENT;
82 		}
83 
84 		memcpy(out_vec[i].base, reply->trailer + payload_offset, out_vec[i].len);
85 		payload_offset += out_vec[i].len;
86 	}
87 
88 	*return_val = reply->return_val;
89 
90 	return PSA_SUCCESS;
91 }
92