blob: 7806ae6a68f7b6cee3707b87a1ba3b942a6f4b64 [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
Zahed Hossain223a73d2019-07-04 12:46:18 -05005int decode_rw_file_memory_req(const struct pldm_msg *msg, size_t payload_length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05306 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
Priyanga8b976652019-06-27 11:30:33 -050018 struct pldm_read_write_file_memory_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -050019 (struct pldm_read_write_file_memory_req *)msg->payload;
Priyanga8b976652019-06-27 11:30:33 -050020
21 *file_handle = le32toh(request->file_handle);
22 *offset = le32toh(request->offset);
23 *length = le32toh(request->length);
24 *address = le64toh(request->address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053025
26 return PLDM_SUCCESS;
27}
28
29int 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)
32{
33 struct pldm_header_info header = {0};
34 int rc = PLDM_SUCCESS;
35
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053036 header.msg_type = PLDM_RESPONSE;
37 header.instance = instance_id;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038 header.pldm_type = PLDM_OEM;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053039 header.command = command;
40
41 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
42 return rc;
43 }
44
Priyanga8b976652019-06-27 11:30:33 -050045 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_OEM;
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
Zahed Hossain223a73d2019-07-04 12:46:18 -050084int decode_rw_file_memory_resp(const struct pldm_msg *msg,
85 size_t payload_length, uint8_t *completion_code,
86 uint32_t *length)
Priyanga8b976652019-06-27 11:30:33 -050087{
88 if (msg == NULL || length == NULL || completion_code == NULL) {
89 return PLDM_ERROR_INVALID_DATA;
90 }
91
92 if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) {
93 return PLDM_ERROR_INVALID_LENGTH;
94 }
95
96 struct pldm_read_write_file_memory_resp *response =
Zahed Hossain223a73d2019-07-04 12:46:18 -050097 (struct pldm_read_write_file_memory_resp *)msg->payload;
Priyanga8b976652019-06-27 11:30:33 -050098 *completion_code = response->completion_code;
99 if (*completion_code == PLDM_SUCCESS) {
100 *length = le32toh(response->length);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530101 }
102
103 return PLDM_SUCCESS;
104}
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530105
Zahed Hossain223a73d2019-07-04 12:46:18 -0500106int decode_get_file_table_req(const struct pldm_msg *msg, size_t payload_length,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530107 uint32_t *transfer_handle,
108 uint8_t *transfer_opflag, uint8_t *table_type)
109{
110 if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL ||
111 table_type == NULL) {
112 return PLDM_ERROR_INVALID_DATA;
113 }
114
115 if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) {
116 return PLDM_ERROR_INVALID_LENGTH;
117 }
118
119 struct pldm_get_file_table_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -0500120 (struct pldm_get_file_table_req *)msg->payload;
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530121
122 *transfer_handle = le32toh(request->transfer_handle);
123 *transfer_opflag = request->operation_flag;
124 *table_type = request->table_type;
125
126 return PLDM_SUCCESS;
127}
128
129int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
130 uint32_t next_transfer_handle,
131 uint8_t transfer_flag, const uint8_t *table_data,
132 size_t table_size, struct pldm_msg *msg)
133{
134 struct pldm_header_info header = {0};
135 int rc = PLDM_SUCCESS;
136
137 header.msg_type = PLDM_RESPONSE;
138 header.instance = instance_id;
139 header.pldm_type = PLDM_OEM;
140 header.command = PLDM_GET_FILE_TABLE;
141
142 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
143 return rc;
144 }
145
146 struct pldm_get_file_table_resp *response =
147 (struct pldm_get_file_table_resp *)msg->payload;
148 response->completion_code = completion_code;
149
150 if (completion_code == PLDM_SUCCESS) {
151 response->next_transfer_handle = htole32(next_transfer_handle);
152 response->transfer_flag = transfer_flag;
153 memcpy(response->table_data, table_data, table_size);
154 }
155
156 return PLDM_SUCCESS;
157}