blob: a415414a79c7a5b7bdd635096a7aef185f24a823 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#include "file_io.h"
2#include <endian.h>
3#include <string.h>
4
5int decode_rw_file_memory_req(const uint8_t *msg, size_t payload_length,
6 uint32_t *file_handle, uint32_t *offset,
7 uint32_t *length, uint64_t *address)
8{
9 if (msg == NULL || file_handle == NULL || offset == NULL ||
10 length == NULL || address == NULL) {
11 return PLDM_ERROR_INVALID_DATA;
12 }
13
14 if (payload_length != PLDM_RW_FILE_MEM_REQ_BYTES) {
15 return PLDM_ERROR_INVALID_LENGTH;
16 }
17
18 *file_handle = le32toh(*((uint32_t *)msg));
19 *offset = le32toh(*((uint32_t *)(msg + sizeof(*file_handle))));
20 *length = le32toh(
21 *((uint32_t *)(msg + sizeof(*file_handle) + sizeof(*offset))));
22 *address = le64toh(*((uint64_t *)(msg + sizeof(*file_handle) +
23 sizeof(*offset) + sizeof(*length))));
24
25 return PLDM_SUCCESS;
26}
27
28int encode_rw_file_memory_resp(uint8_t instance_id, uint8_t command,
29 uint8_t completion_code, uint32_t length,
30 struct pldm_msg *msg)
31{
32 struct pldm_header_info header = {0};
33 int rc = PLDM_SUCCESS;
34
35 uint8_t *payload = msg->payload;
36 *payload = completion_code;
37
38 header.msg_type = PLDM_RESPONSE;
39 header.instance = instance_id;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050040 header.pldm_type = PLDM_OEM;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053041 header.command = command;
42
43 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
44 return rc;
45 }
46
47 if (msg->payload[0] == PLDM_SUCCESS) {
48 uint8_t *dst = msg->payload + sizeof(completion_code);
49 length = htole32(length);
50 memcpy(dst, &length, sizeof(length));
51 }
52
53 return PLDM_SUCCESS;
54}
Tom Joseph0c6d22c2019-06-26 09:58:41 +053055
56int decode_get_file_table_req(const uint8_t *msg, size_t payload_length,
57 uint32_t *transfer_handle,
58 uint8_t *transfer_opflag, uint8_t *table_type)
59{
60 if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL ||
61 table_type == NULL) {
62 return PLDM_ERROR_INVALID_DATA;
63 }
64
65 if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) {
66 return PLDM_ERROR_INVALID_LENGTH;
67 }
68
69 struct pldm_get_file_table_req *request =
70 (struct pldm_get_file_table_req *)msg;
71
72 *transfer_handle = le32toh(request->transfer_handle);
73 *transfer_opflag = request->operation_flag;
74 *table_type = request->table_type;
75
76 return PLDM_SUCCESS;
77}
78
79int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
80 uint32_t next_transfer_handle,
81 uint8_t transfer_flag, const uint8_t *table_data,
82 size_t table_size, struct pldm_msg *msg)
83{
84 struct pldm_header_info header = {0};
85 int rc = PLDM_SUCCESS;
86
87 header.msg_type = PLDM_RESPONSE;
88 header.instance = instance_id;
89 header.pldm_type = PLDM_OEM;
90 header.command = PLDM_GET_FILE_TABLE;
91
92 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
93 return rc;
94 }
95
96 struct pldm_get_file_table_resp *response =
97 (struct pldm_get_file_table_resp *)msg->payload;
98 response->completion_code = completion_code;
99
100 if (completion_code == PLDM_SUCCESS) {
101 response->next_transfer_handle = htole32(next_transfer_handle);
102 response->transfer_flag = transfer_flag;
103 memcpy(response->table_data, table_data, table_size);
104 }
105
106 return PLDM_SUCCESS;
107}