xref: /rk3399_ARM-atf/drivers/arm/rse/rse_comms_protocol_embed.c (revision 67ad1ac8e77ca84ba03ea0e754f7b95c51cc796d)
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 <common/debug.h>
12 #include "rse_comms_protocol_common.h"
13 #include "rse_comms_protocol_embed.h"
14 
15 psa_status_t rse_protocol_embed_serialize_msg(psa_handle_t handle,
16 					      int16_t type,
17 					      const psa_invec *in_vec,
18 					      uint8_t in_len,
19 					      const psa_outvec *out_vec,
20 					      uint8_t out_len,
21 					      struct rse_embed_msg_t *msg,
22 					      size_t *msg_len)
23 {
24 	uint32_t payload_size = 0;
25 	uint32_t i;
26 
27 	assert(msg != NULL);
28 	assert(msg_len != NULL);
29 	assert((in_len == 0u) || (in_vec != NULL));
30 	assert((out_len == 0u) || (out_vec != NULL));
31 
32 	msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
33 	msg->handle = handle;
34 
35 	/* Fill msg iovec lengths */
36 	for (i = 0U; i < in_len; ++i) {
37 		msg->io_size[i] = in_vec[i].len;
38 	}
39 	for (i = 0U; i < out_len; ++i) {
40 		msg->io_size[in_len + i] = out_vec[i].len;
41 	}
42 
43 	for (i = 0U; i < in_len; ++i) {
44 		if (in_vec[i].len > sizeof(msg->trailer) - payload_size) {
45 			return PSA_ERROR_INVALID_ARGUMENT;
46 		}
47 		memcpy(msg->trailer + payload_size,
48 		       in_vec[i].base,
49 		       in_vec[i].len);
50 		payload_size += in_vec[i].len;
51 	}
52 
53 	/* Output the actual size of the message, to optimize sending */
54 	*msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
55 
56 	return PSA_SUCCESS;
57 }
58 
59 psa_status_t rse_protocol_embed_deserialize_reply(psa_outvec *out_vec,
60 						  uint8_t out_len,
61 						  psa_status_t *return_val,
62 						  const struct rse_embed_reply_t *reply,
63 						  size_t reply_size)
64 {
65 	uint32_t payload_offset = 0;
66 	uint32_t i;
67 
68 	assert(reply != NULL);
69 	assert(return_val != NULL);
70 
71 	for (i = 0U; i < out_len; ++i) {
72 		if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
73 		    > reply_size) {
74 			return PSA_ERROR_INVALID_ARGUMENT;
75 		}
76 
77 		memcpy(out_vec[i].base,
78 		       reply->trailer + payload_offset,
79 		       reply->out_size[i]);
80 		out_vec[i].len = reply->out_size[i];
81 		payload_offset += reply->out_size[i];
82 	}
83 
84 	*return_val = reply->return_val;
85 
86 	return PSA_SUCCESS;
87 }
88