rse_comms_protocol_embed.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2022-2024, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include "rse_comms_protocol_common.h"
  11. #include "rse_comms_protocol_embed.h"
  12. psa_status_t rse_protocol_embed_serialize_msg(psa_handle_t handle,
  13. int16_t type,
  14. const psa_invec *in_vec,
  15. uint8_t in_len,
  16. const psa_outvec *out_vec,
  17. uint8_t out_len,
  18. struct rse_embed_msg_t *msg,
  19. size_t *msg_len)
  20. {
  21. uint32_t payload_size = 0;
  22. uint32_t i;
  23. assert(msg != NULL);
  24. assert(msg_len != NULL);
  25. assert(in_vec != NULL);
  26. msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
  27. msg->handle = handle;
  28. /* Fill msg iovec lengths */
  29. for (i = 0U; i < in_len; ++i) {
  30. msg->io_size[i] = in_vec[i].len;
  31. }
  32. for (i = 0U; i < out_len; ++i) {
  33. msg->io_size[in_len + i] = out_vec[i].len;
  34. }
  35. for (i = 0U; i < in_len; ++i) {
  36. if (in_vec[i].len > sizeof(msg->trailer) - payload_size) {
  37. return PSA_ERROR_INVALID_ARGUMENT;
  38. }
  39. memcpy(msg->trailer + payload_size,
  40. in_vec[i].base,
  41. in_vec[i].len);
  42. payload_size += in_vec[i].len;
  43. }
  44. /* Output the actual size of the message, to optimize sending */
  45. *msg_len = sizeof(*msg) - sizeof(msg->trailer) + payload_size;
  46. return PSA_SUCCESS;
  47. }
  48. psa_status_t rse_protocol_embed_deserialize_reply(psa_outvec *out_vec,
  49. uint8_t out_len,
  50. psa_status_t *return_val,
  51. const struct rse_embed_reply_t *reply,
  52. size_t reply_size)
  53. {
  54. uint32_t payload_offset = 0;
  55. uint32_t i;
  56. assert(reply != NULL);
  57. assert(return_val != NULL);
  58. for (i = 0U; i < out_len; ++i) {
  59. if ((sizeof(*reply) - sizeof(reply->trailer) + payload_offset)
  60. > reply_size) {
  61. return PSA_ERROR_INVALID_ARGUMENT;
  62. }
  63. memcpy(out_vec[i].base,
  64. reply->trailer + payload_offset,
  65. reply->out_size[i]);
  66. out_vec[i].len = reply->out_size[i];
  67. payload_offset += reply->out_size[i];
  68. }
  69. *return_val = reply->return_val;
  70. return PSA_SUCCESS;
  71. }