blob: c453258f23bbf009844fa24f7c2501ff3c09b995 [file] [log] [blame]
Raef Coles734aaac2022-06-15 14:37:22 +01001/*
David Vincze92265e82023-03-06 15:02:08 +01002 * Copyright (c) 2022-2023, Arm Limited. All rights reserved.
Raef Coles734aaac2022-06-15 14:37:22 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Tamas Band1bbcbe2022-10-03 15:34:02 +02008#include <assert.h>
Raef Coles734aaac2022-06-15 14:37:22 +01009#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
26psa_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);
Raef Coles734aaac2022-06-15 14:37:22 +010041
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 }
David Vincze92265e82023-03-06 15:02:08 +010057 memcpy(msg->trailer + payload_size,
58 in_vec[i].base,
59 in_vec[i].len);
Raef Coles734aaac2022-06-15 14:37:22 +010060 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
69psa_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);
Raef Coles734aaac2022-06-15 14:37:22 +010079 assert(return_val != NULL);
80
81 for (i = 0U; i < out_len; ++i) {
David Vincze92265e82023-03-06 15:02:08 +010082 if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
83 > reply_size) {
Raef Coles734aaac2022-06-15 14:37:22 +010084 return PSA_ERROR_INVALID_ARGUMENT;
85 }
86
David Vincze92265e82023-03-06 15:02:08 +010087 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];
Raef Coles734aaac2022-06-15 14:37:22 +010092 }
93
94 *return_val = reply->return_val;
95
96 return PSA_SUCCESS;
97}