blob: 05628ccdd307d73b57a9a5f6155053ce87b8cd25 [file] [log] [blame]
Raef Coles734aaac2022-06-15 14:37:22 +01001/*
David Vinczec140f5f2023-09-07 15:48:25 +02002 * Copyright (c) 2022-2024, 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>
David Vinczec140f5f2023-09-07 15:48:25 +020012#include "rss_comms_protocol_common.h"
Raef Coles734aaac2022-06-15 14:37:22 +010013#include "rss_comms_protocol_embed.h"
14
Raef Coles734aaac2022-06-15 14:37:22 +010015psa_status_t rss_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 rss_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_vec != NULL);
Raef Coles734aaac2022-06-15 14:37:22 +010030
31 msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
32 msg->handle = handle;
33
34 /* Fill msg iovec lengths */
35 for (i = 0U; i < in_len; ++i) {
36 msg->io_size[i] = in_vec[i].len;
37 }
38 for (i = 0U; i < out_len; ++i) {
39 msg->io_size[in_len + i] = out_vec[i].len;
40 }
41
42 for (i = 0U; i < in_len; ++i) {
43 if (in_vec[i].len > sizeof(msg->trailer) - payload_size) {
44 return PSA_ERROR_INVALID_ARGUMENT;
45 }
David Vincze92265e82023-03-06 15:02:08 +010046 memcpy(msg->trailer + payload_size,
47 in_vec[i].base,
48 in_vec[i].len);
Raef Coles734aaac2022-06-15 14:37:22 +010049 payload_size += in_vec[i].len;
50 }
51
52 /* Output the actual size of the message, to optimize sending */
53 *msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
54
55 return PSA_SUCCESS;
56}
57
58psa_status_t rss_protocol_embed_deserialize_reply(psa_outvec *out_vec,
59 uint8_t out_len,
60 psa_status_t *return_val,
61 const struct rss_embed_reply_t *reply,
62 size_t reply_size)
63{
64 uint32_t payload_offset = 0;
65 uint32_t i;
66
67 assert(reply != NULL);
Raef Coles734aaac2022-06-15 14:37:22 +010068 assert(return_val != NULL);
69
70 for (i = 0U; i < out_len; ++i) {
David Vincze92265e82023-03-06 15:02:08 +010071 if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
72 > reply_size) {
Raef Coles734aaac2022-06-15 14:37:22 +010073 return PSA_ERROR_INVALID_ARGUMENT;
74 }
75
David Vincze92265e82023-03-06 15:02:08 +010076 memcpy(out_vec[i].base,
77 reply->trailer + payload_offset,
78 reply->out_size[i]);
79 out_vec[i].len = reply->out_size[i];
80 payload_offset += reply->out_size[i];
Raef Coles734aaac2022-06-15 14:37:22 +010081 }
82
83 *return_val = reply->return_val;
84
85 return PSA_SUCCESS;
86}