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_vec != NULL); 30 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 } 46 memcpy(msg->trailer + payload_size, 47 in_vec[i].base, 48 in_vec[i].len); 49 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 58 psa_status_t rse_protocol_embed_deserialize_reply(psa_outvec *out_vec, 59 uint8_t out_len, 60 psa_status_t *return_val, 61 const struct rse_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); 68 assert(return_val != NULL); 69 70 for (i = 0U; i < out_len; ++i) { 71 if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset) 72 > reply_size) { 73 return PSA_ERROR_INVALID_ARGUMENT; 74 } 75 76 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]; 81 } 82 83 *return_val = reply->return_val; 84 85 return PSA_SUCCESS; 86 } 87