blob: f5301f53fccfb0c177297a8479ef23f8ca34db59 [file] [log] [blame]
Tom Joseph7dae7772019-04-10 14:44:44 +05301#include "file_io.h"
2#include <endian.h>
3#include <string.h>
4
Eddie James3b02e272019-04-22 20:13:55 +00005int 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)
Tom Joseph7dae7772019-04-10 14:44:44 +05308{
9 if (msg == NULL || file_handle == NULL || offset == NULL ||
10 length == NULL || address == NULL) {
11 return PLDM_ERROR_INVALID_DATA;
12 }
13
Eddie James3b02e272019-04-22 20:13:55 +000014 if (payload_length != PLDM_RW_FILE_MEM_REQ_BYTES) {
Tom Joseph7dae7772019-04-10 14:44:44 +053015 return PLDM_ERROR_INVALID_LENGTH;
16 }
17
Priyangaf4736d42019-05-02 14:48:04 +053018 struct pldm_read_write_file_memory_req *request =
19 (struct pldm_read_write_file_memory_req *)msg;
20
21 *file_handle = le32toh(request->file_handle);
22 *offset = le32toh(request->offset);
23 *length = le32toh(request->length);
24 *address = le64toh(request->address);
Tom Joseph7dae7772019-04-10 14:44:44 +053025
26 return PLDM_SUCCESS;
27}
28
Eddie James3b02e272019-04-22 20:13:55 +000029int encode_rw_file_memory_resp(uint8_t instance_id, uint8_t command,
30 uint8_t completion_code, uint32_t length,
31 struct pldm_msg *msg)
Tom Joseph7dae7772019-04-10 14:44:44 +053032{
33 struct pldm_header_info header = {0};
34 int rc = PLDM_SUCCESS;
35
Tom Joseph7dae7772019-04-10 14:44:44 +053036 header.msg_type = PLDM_RESPONSE;
37 header.instance = instance_id;
38 header.pldm_type = PLDM_IBM_OEM_TYPE;
Eddie James3b02e272019-04-22 20:13:55 +000039 header.command = command;
Tom Joseph7dae7772019-04-10 14:44:44 +053040
41 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
42 return rc;
43 }
44
Priyangaf4736d42019-05-02 14:48:04 +053045 struct pldm_read_write_file_memory_resp *response =
46 (struct pldm_read_write_file_memory_resp *)msg->payload;
47 response->completion_code = completion_code;
48 if (response->completion_code == PLDM_SUCCESS) {
49 response->length = htole32(length);
50 }
51
52 return PLDM_SUCCESS;
53}
54
55int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
56 uint32_t file_handle, uint32_t offset,
57 uint32_t length, uint64_t address,
58 struct pldm_msg *msg)
59{
60 struct pldm_header_info header = {0};
61 int rc = PLDM_SUCCESS;
62 if (msg == NULL) {
63 return PLDM_ERROR_INVALID_DATA;
64 }
65
66 header.msg_type = PLDM_REQUEST;
67 header.instance = instance_id;
68 header.pldm_type = PLDM_IBM_OEM_TYPE;
69 header.command = command;
70
71 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
72 return rc;
73 }
74
75 struct pldm_read_write_file_memory_req *req =
76 (struct pldm_read_write_file_memory_req *)msg->payload;
77 req->file_handle = htole32(file_handle);
78 req->offset = htole32(offset);
79 req->length = htole32(length);
80 req->address = htole64(address);
81 return PLDM_SUCCESS;
82}
83
84int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length,
85 uint8_t *completion_code, uint32_t *length)
86{
87 if (msg == NULL || length == NULL || completion_code == NULL) {
88 return PLDM_ERROR_INVALID_DATA;
89 }
90
91 if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) {
92 return PLDM_ERROR_INVALID_LENGTH;
93 }
94
95 struct pldm_read_write_file_memory_resp *response =
96 (struct pldm_read_write_file_memory_resp *)msg;
97 *completion_code = response->completion_code;
98 if (*completion_code == PLDM_SUCCESS) {
99 *length = le32toh(response->length);
Tom Joseph7dae7772019-04-10 14:44:44 +0530100 }
101
102 return PLDM_SUCCESS;
103}
Tom Joseph61325fa2019-06-04 15:22:22 +0530104
105int decode_get_file_table_req(const uint8_t *msg, size_t payload_length,
106 uint32_t *transfer_handle,
107 uint8_t *transfer_opflag, uint8_t *table_type)
108{
109 if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL ||
110 table_type == NULL) {
111 return PLDM_ERROR_INVALID_DATA;
112 }
113
114 if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) {
115 return PLDM_ERROR_INVALID_LENGTH;
116 }
117
118 struct pldm_get_file_table_req *request =
119 (struct pldm_get_file_table_req *)msg;
120
121 *transfer_handle = le32toh(request->transfer_handle);
122 *transfer_opflag = request->operation_flag;
123 *table_type = request->table_type;
124
125 return PLDM_SUCCESS;
126}
127
128int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
129 uint32_t next_transfer_handle,
130 uint8_t transfer_flag, const uint8_t *table_data,
131 size_t table_size, struct pldm_msg *msg)
132{
133 struct pldm_header_info header = {0};
134 int rc = PLDM_SUCCESS;
135
136 header.msg_type = PLDM_RESPONSE;
137 header.instance = instance_id;
138 header.pldm_type = PLDM_IBM_OEM_TYPE;
139 header.command = PLDM_GET_FILE_TABLE;
140
141 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
142 return rc;
143 }
144
145 struct pldm_get_file_table_resp *response =
146 (struct pldm_get_file_table_resp *)msg->payload;
147 response->completion_code = completion_code;
148
149 if (completion_code == PLDM_SUCCESS) {
150 response->next_transfer_handle = htole32(next_transfer_handle);
151 response->transfer_flag = transfer_flag;
152 memcpy(response->table_data, table_data, table_size);
153 }
154
155 return PLDM_SUCCESS;
156}