Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 1 | /* |
David Vincze | c140f5f | 2023-09-07 15:48:25 +0200 | [diff] [blame^] | 2 | * Copyright (c) 2022-2024, Arm Limited. All rights reserved. |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Tamas Ban | d1bbcbe | 2022-10-03 15:34:02 +0200 | [diff] [blame] | 8 | #include <assert.h> |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
| 11 | #include <common/debug.h> |
David Vincze | c140f5f | 2023-09-07 15:48:25 +0200 | [diff] [blame^] | 12 | #include "rss_comms_protocol_common.h" |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 13 | #include "rss_comms_protocol_embed.h" |
| 14 | |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 15 | psa_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 Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 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 | } |
David Vincze | 92265e8 | 2023-03-06 15:02:08 +0100 | [diff] [blame] | 46 | memcpy(msg->trailer + payload_size, |
| 47 | in_vec[i].base, |
| 48 | in_vec[i].len); |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 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 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 Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 68 | assert(return_val != NULL); |
| 69 | |
| 70 | for (i = 0U; i < out_len; ++i) { |
David Vincze | 92265e8 | 2023-03-06 15:02:08 +0100 | [diff] [blame] | 71 | if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset) |
| 72 | > reply_size) { |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 73 | return PSA_ERROR_INVALID_ARGUMENT; |
| 74 | } |
| 75 | |
David Vincze | 92265e8 | 2023-03-06 15:02:08 +0100 | [diff] [blame] | 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]; |
Raef Coles | 734aaac | 2022-06-15 14:37:22 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | *return_val = reply->return_val; |
| 84 | |
| 85 | return PSA_SUCCESS; |
| 86 | } |