1 /*
2 * Copyright (c) 2022-2023, 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,
58 in_vec[i].base,
59 in_vec[i].len);
60 payload_size += in_vec[i].len;
61 }
62
63 /* Output the actual size of the message, to optimize sending */
64 *msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
65
66 return PSA_SUCCESS;
67 }
68
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)69 psa_status_t rss_protocol_embed_deserialize_reply(psa_outvec *out_vec,
70 uint8_t out_len,
71 psa_status_t *return_val,
72 const struct rss_embed_reply_t *reply,
73 size_t reply_size)
74 {
75 uint32_t payload_offset = 0;
76 uint32_t i;
77
78 assert(reply != NULL);
79 assert(return_val != NULL);
80
81 for (i = 0U; i < out_len; ++i) {
82 if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
83 > reply_size) {
84 return PSA_ERROR_INVALID_ARGUMENT;
85 }
86
87 memcpy(out_vec[i].base,
88 reply->trailer + payload_offset,
89 reply->out_size[i]);
90 out_vec[i].len = reply->out_size[i];
91 payload_offset += reply->out_size[i];
92 }
93
94 *return_val = reply->return_val;
95
96 return PSA_SUCCESS;
97 }
98