blob: 6c045a959092c419b7eb4ee1d7fd02680221ded8 [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{
George Liub7095ff2021-06-14 16:01:57 +080033 if (msg == NULL) {
34 return PLDM_ERROR_INVALID_LENGTH;
35 }
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053036
George Liub7095ff2021-06-14 16:01:57 +080037 struct pldm_header_info header = {0};
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053038 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;
George Liub7095ff2021-06-14 16:01:57 +080042 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
43 if (rc != PLDM_SUCCESS) {
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053044 return rc;
45 }
46
Priyanga8b976652019-06-27 11:30:33 -050047 struct pldm_read_write_file_memory_resp *response =
48 (struct pldm_read_write_file_memory_resp *)msg->payload;
49 response->completion_code = completion_code;
50 if (response->completion_code == PLDM_SUCCESS) {
51 response->length = htole32(length);
52 }
53
54 return PLDM_SUCCESS;
55}
56
57int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
58 uint32_t file_handle, uint32_t offset,
59 uint32_t length, uint64_t address,
60 struct pldm_msg *msg)
61{
Priyanga8b976652019-06-27 11:30:33 -050062 if (msg == NULL) {
63 return PLDM_ERROR_INVALID_DATA;
64 }
65
George Liub7095ff2021-06-14 16:01:57 +080066 struct pldm_header_info header = {0};
Priyanga8b976652019-06-27 11:30:33 -050067 header.msg_type = PLDM_REQUEST;
68 header.instance = instance_id;
69 header.pldm_type = PLDM_OEM;
70 header.command = command;
George Liub7095ff2021-06-14 16:01:57 +080071 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
72 if (rc != PLDM_SUCCESS) {
Priyanga8b976652019-06-27 11:30:33 -050073 return rc;
74 }
75
76 struct pldm_read_write_file_memory_req *req =
77 (struct pldm_read_write_file_memory_req *)msg->payload;
78 req->file_handle = htole32(file_handle);
79 req->offset = htole32(offset);
80 req->length = htole32(length);
81 req->address = htole64(address);
82 return PLDM_SUCCESS;
83}
84
Zahed Hossain223a73d2019-07-04 12:46:18 -050085int decode_rw_file_memory_resp(const struct pldm_msg *msg,
86 size_t payload_length, uint8_t *completion_code,
87 uint32_t *length)
Priyanga8b976652019-06-27 11:30:33 -050088{
89 if (msg == NULL || length == NULL || completion_code == NULL) {
90 return PLDM_ERROR_INVALID_DATA;
91 }
92
93 if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) {
94 return PLDM_ERROR_INVALID_LENGTH;
95 }
96
97 struct pldm_read_write_file_memory_resp *response =
Zahed Hossain223a73d2019-07-04 12:46:18 -050098 (struct pldm_read_write_file_memory_resp *)msg->payload;
Priyanga8b976652019-06-27 11:30:33 -050099 *completion_code = response->completion_code;
100 if (*completion_code == PLDM_SUCCESS) {
101 *length = le32toh(response->length);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530102 }
103
104 return PLDM_SUCCESS;
105}
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530106
Zahed Hossain223a73d2019-07-04 12:46:18 -0500107int decode_get_file_table_req(const struct pldm_msg *msg, size_t payload_length,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530108 uint32_t *transfer_handle,
109 uint8_t *transfer_opflag, uint8_t *table_type)
110{
111 if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL ||
112 table_type == NULL) {
113 return PLDM_ERROR_INVALID_DATA;
114 }
115
116 if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) {
117 return PLDM_ERROR_INVALID_LENGTH;
118 }
119
120 struct pldm_get_file_table_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -0500121 (struct pldm_get_file_table_req *)msg->payload;
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530122
123 *transfer_handle = le32toh(request->transfer_handle);
124 *transfer_opflag = request->operation_flag;
125 *table_type = request->table_type;
126
127 return PLDM_SUCCESS;
128}
129
130int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
131 uint32_t next_transfer_handle,
132 uint8_t transfer_flag, const uint8_t *table_data,
133 size_t table_size, struct pldm_msg *msg)
134{
George Liub7095ff2021-06-14 16:01:57 +0800135 if (msg == NULL) {
136 return PLDM_ERROR_INVALID_LENGTH;
137 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530138
George Liub7095ff2021-06-14 16:01:57 +0800139 struct pldm_header_info header = {0};
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530140 header.msg_type = PLDM_RESPONSE;
141 header.instance = instance_id;
142 header.pldm_type = PLDM_OEM;
143 header.command = PLDM_GET_FILE_TABLE;
George Liub7095ff2021-06-14 16:01:57 +0800144 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
145 if (rc != PLDM_SUCCESS) {
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530146 return rc;
147 }
148
149 struct pldm_get_file_table_resp *response =
150 (struct pldm_get_file_table_resp *)msg->payload;
151 response->completion_code = completion_code;
152
153 if (completion_code == PLDM_SUCCESS) {
154 response->next_transfer_handle = htole32(next_transfer_handle);
155 response->transfer_flag = transfer_flag;
156 memcpy(response->table_data, table_data, table_size);
157 }
158
159 return PLDM_SUCCESS;
160}
vkaverap2ffe3292019-06-24 00:08:13 -0500161
Pavithra Barithayad15c8092020-05-12 02:26:26 -0500162int encode_get_file_table_req(uint8_t instance_id, uint32_t transfer_handle,
163 uint8_t transfer_opflag, uint8_t table_type,
164 struct pldm_msg *msg)
165{
Pavithra Barithayad15c8092020-05-12 02:26:26 -0500166 if (msg == NULL) {
167 return PLDM_ERROR_INVALID_DATA;
168 }
169
George Liub7095ff2021-06-14 16:01:57 +0800170 struct pldm_header_info header = {0};
Pavithra Barithayad15c8092020-05-12 02:26:26 -0500171 header.msg_type = PLDM_REQUEST;
172 header.instance = instance_id;
173 header.pldm_type = PLDM_OEM;
174 header.command = PLDM_GET_FILE_TABLE;
George Liub7095ff2021-06-14 16:01:57 +0800175 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
176 if (rc != PLDM_SUCCESS) {
Pavithra Barithayad15c8092020-05-12 02:26:26 -0500177 return rc;
178 }
179
180 struct pldm_get_file_table_req *request =
181 (struct pldm_get_file_table_req *)msg->payload;
182
183 request->transfer_handle = htole32(transfer_handle);
184 request->operation_flag = transfer_opflag;
185 request->table_type = table_type;
186 return PLDM_SUCCESS;
187}
188
189int decode_get_file_table_resp(const struct pldm_msg *msg,
190 size_t payload_length, uint8_t *completion_code,
191 uint32_t *next_transfer_handle,
192 uint8_t *transfer_flag,
193 uint8_t *file_table_data_start_offset,
194 size_t *file_table_length)
195{
196 if (msg == NULL || transfer_flag == NULL ||
197 next_transfer_handle == NULL || completion_code == NULL ||
198 file_table_data_start_offset == NULL || file_table_length == NULL) {
199 return PLDM_ERROR_INVALID_DATA;
200 }
201
202 if (payload_length <= PLDM_GET_FILE_TABLE_MIN_RESP_BYTES) {
203 return PLDM_ERROR_INVALID_LENGTH;
204 }
205
206 *completion_code = msg->payload[0];
207
208 if (PLDM_SUCCESS != *completion_code) {
209 return PLDM_SUCCESS;
210 }
211
212 struct pldm_get_file_table_resp *response =
213 (struct pldm_get_file_table_resp *)msg->payload;
214
215 *next_transfer_handle = le32toh(response->next_transfer_handle);
216 *transfer_flag = response->transfer_flag;
217 *file_table_data_start_offset = sizeof(*completion_code) +
218 sizeof(*next_transfer_handle) +
219 sizeof(*transfer_flag);
220 *file_table_length =
221 payload_length - PLDM_GET_FILE_TABLE_MIN_RESP_BYTES;
222
223 return PLDM_SUCCESS;
224}
225
vkaverap2ffe3292019-06-24 00:08:13 -0500226int decode_read_file_req(const struct pldm_msg *msg, size_t payload_length,
227 uint32_t *file_handle, uint32_t *offset,
228 uint32_t *length)
229{
230 if (msg == NULL || file_handle == NULL || offset == NULL ||
231 length == NULL) {
232 return PLDM_ERROR_INVALID_DATA;
233 }
234
235 if (payload_length != PLDM_READ_FILE_REQ_BYTES) {
236 return PLDM_ERROR_INVALID_LENGTH;
237 }
238
239 struct pldm_read_file_req *request =
240 (struct pldm_read_file_req *)msg->payload;
241
242 *file_handle = le32toh(request->file_handle);
243 *offset = le32toh(request->offset);
244 *length = le32toh(request->length);
245
246 return PLDM_SUCCESS;
247}
248
249int encode_read_file_req(uint8_t instance_id, uint32_t file_handle,
250 uint32_t offset, uint32_t length, struct pldm_msg *msg)
251{
vkaverap2ffe3292019-06-24 00:08:13 -0500252 if (msg == NULL) {
253 return PLDM_ERROR_INVALID_DATA;
254 }
255
256 if (length == 0) {
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500257 return PLDM_ERROR_INVALID_LENGTH;
vkaverap2ffe3292019-06-24 00:08:13 -0500258 }
259
George Liub7095ff2021-06-14 16:01:57 +0800260 struct pldm_header_info header = {0};
261 header.msg_type = PLDM_REQUEST;
262 header.instance = instance_id;
263 header.pldm_type = PLDM_OEM;
264 header.command = PLDM_READ_FILE;
265 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
266 if (rc != PLDM_SUCCESS) {
vkaverap2ffe3292019-06-24 00:08:13 -0500267 return rc;
268 }
269
270 struct pldm_read_file_req *request =
271 (struct pldm_read_file_req *)msg->payload;
272
273 request->file_handle = htole32(file_handle);
274 request->offset = htole32(offset);
275 request->length = htole32(length);
276
277 return PLDM_SUCCESS;
278}
279
280int decode_read_file_resp(const struct pldm_msg *msg, size_t payload_length,
281 uint8_t *completion_code, uint32_t *length,
282 size_t *file_data_offset)
283{
284 if (msg == NULL || completion_code == NULL || length == NULL) {
285 return PLDM_ERROR_INVALID_DATA;
286 }
287
288 if (payload_length < PLDM_READ_FILE_RESP_BYTES) {
289 return PLDM_ERROR_INVALID_LENGTH;
290 }
291
292 struct pldm_read_file_resp *response =
293 (struct pldm_read_file_resp *)msg->payload;
294
295 *completion_code = response->completion_code;
296 if (*completion_code == PLDM_SUCCESS) {
297 *length = le32toh(response->length);
298 if (payload_length != PLDM_READ_FILE_RESP_BYTES + *length) {
299 return PLDM_ERROR_INVALID_LENGTH;
300 }
301 *file_data_offset = sizeof(*completion_code) + sizeof(*length);
302 }
303
304 return PLDM_SUCCESS;
305}
306
307int encode_read_file_resp(uint8_t instance_id, uint8_t completion_code,
308 uint32_t length, struct pldm_msg *msg)
309{
vkaverap2ffe3292019-06-24 00:08:13 -0500310 if (msg == NULL) {
311 return PLDM_ERROR_INVALID_DATA;
312 }
313
George Liub7095ff2021-06-14 16:01:57 +0800314 struct pldm_header_info header = {0};
315 header.msg_type = PLDM_RESPONSE;
316 header.instance = instance_id;
317 header.pldm_type = PLDM_OEM;
318 header.command = PLDM_READ_FILE;
319 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
320 if (rc != PLDM_SUCCESS) {
vkaverap2ffe3292019-06-24 00:08:13 -0500321 return rc;
322 }
323
324 struct pldm_read_file_resp *response =
325 (struct pldm_read_file_resp *)msg->payload;
326 response->completion_code = completion_code;
327
328 if (response->completion_code == PLDM_SUCCESS) {
329 response->length = htole32(length);
330 }
331
332 return PLDM_SUCCESS;
333}
334
335int decode_write_file_req(const struct pldm_msg *msg, size_t payload_length,
336 uint32_t *file_handle, uint32_t *offset,
337 uint32_t *length, size_t *file_data_offset)
338{
339 if (msg == NULL || file_handle == NULL || length == NULL) {
340 return PLDM_ERROR_INVALID_DATA;
341 }
342
343 if (payload_length < PLDM_WRITE_FILE_REQ_BYTES) {
344 return PLDM_ERROR_INVALID_LENGTH;
345 }
346
347 struct pldm_write_file_req *request =
348 (struct pldm_write_file_req *)msg->payload;
349
350 *file_handle = le32toh(request->file_handle);
351 *offset = le32toh(request->offset);
352 *length = le32toh(request->length);
353 if (payload_length != PLDM_WRITE_FILE_REQ_BYTES + *length) {
354 return PLDM_ERROR_INVALID_LENGTH;
355 }
356 *file_data_offset =
357 sizeof(*file_handle) + sizeof(*offset) + sizeof(*length);
358
359 return PLDM_SUCCESS;
360}
361
362int encode_write_file_req(uint8_t instance_id, uint32_t file_handle,
363 uint32_t offset, uint32_t length,
364 struct pldm_msg *msg)
365{
vkaverap2ffe3292019-06-24 00:08:13 -0500366 if (msg == NULL) {
367 return PLDM_ERROR_INVALID_DATA;
368 }
369
vkaverap2ffe3292019-06-24 00:08:13 -0500370 if (length == 0) {
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500371 return PLDM_ERROR_INVALID_LENGTH;
vkaverap2ffe3292019-06-24 00:08:13 -0500372 }
373
George Liub7095ff2021-06-14 16:01:57 +0800374 struct pldm_header_info header = {0};
375 header.msg_type = PLDM_REQUEST;
376 header.instance = instance_id;
377 header.pldm_type = PLDM_OEM;
378 header.command = PLDM_WRITE_FILE;
379 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
380 if (rc != PLDM_SUCCESS) {
381 return rc;
382 }
383
vkaverap2ffe3292019-06-24 00:08:13 -0500384 struct pldm_write_file_req *request =
385 (struct pldm_write_file_req *)msg->payload;
386
387 request->file_handle = htole32(file_handle);
388 request->offset = htole32(offset);
389 request->length = htole32(length);
390
391 return PLDM_SUCCESS;
392}
393
394int decode_write_file_resp(const struct pldm_msg *msg, size_t payload_length,
395 uint8_t *completion_code, uint32_t *length)
396{
397 if (msg == NULL || completion_code == NULL || length == NULL) {
398 return PLDM_ERROR_INVALID_DATA;
399 }
400
401 if (payload_length != PLDM_WRITE_FILE_RESP_BYTES) {
402 return PLDM_ERROR_INVALID_LENGTH;
403 }
404
405 struct pldm_write_file_resp *response =
406 (struct pldm_write_file_resp *)msg->payload;
407
408 *completion_code = le32toh(response->completion_code);
409 if (response->completion_code == PLDM_SUCCESS) {
410 *length = le32toh(response->length);
411 }
412
413 return PLDM_SUCCESS;
414}
415
416int encode_write_file_resp(uint8_t instance_id, uint8_t completion_code,
417 uint32_t length, struct pldm_msg *msg)
418{
vkaverap2ffe3292019-06-24 00:08:13 -0500419 if (msg == NULL) {
420 return PLDM_ERROR_INVALID_DATA;
421 }
422
George Liub7095ff2021-06-14 16:01:57 +0800423 struct pldm_header_info header = {0};
424 header.msg_type = PLDM_RESPONSE;
425 header.instance = instance_id;
426 header.pldm_type = PLDM_OEM;
427 header.command = PLDM_WRITE_FILE;
428 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
429 if (rc != PLDM_SUCCESS) {
vkaverap2ffe3292019-06-24 00:08:13 -0500430 return rc;
431 }
432
433 struct pldm_write_file_resp *response =
434 (struct pldm_write_file_resp *)msg->payload;
435 response->completion_code = completion_code;
436
437 if (response->completion_code == PLDM_SUCCESS) {
438 response->length = htole32(length);
439 }
440
441 return PLDM_SUCCESS;
442}
vkaverap07404562019-08-05 22:57:11 -0500443
444int decode_rw_file_by_type_memory_req(const struct pldm_msg *msg,
445 size_t payload_length,
446 uint16_t *file_type,
447 uint32_t *file_handle, uint32_t *offset,
448 uint32_t *length, uint64_t *address)
449{
450 if (msg == NULL || file_type == NULL || file_handle == NULL ||
451 offset == NULL || length == NULL || address == NULL) {
452 return PLDM_ERROR_INVALID_DATA;
453 }
454
455 if (payload_length != PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES) {
456 return PLDM_ERROR_INVALID_LENGTH;
457 }
458
459 struct pldm_read_write_file_by_type_memory_req *request =
460 (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
461 *file_type = le16toh(request->file_type);
462 *file_handle = le32toh(request->file_handle);
463 *offset = le32toh(request->offset);
464 *length = le32toh(request->length);
465 *address = le64toh(request->address);
466
467 return PLDM_SUCCESS;
468}
469
470int encode_rw_file_by_type_memory_resp(uint8_t instance_id, uint8_t command,
471 uint8_t completion_code, uint32_t length,
472 struct pldm_msg *msg)
473{
vkaverap07404562019-08-05 22:57:11 -0500474 if (msg == NULL) {
475 return PLDM_ERROR_INVALID_DATA;
476 }
477
George Liub7095ff2021-06-14 16:01:57 +0800478 struct pldm_header_info header = {0};
vkaverap07404562019-08-05 22:57:11 -0500479 header.msg_type = PLDM_RESPONSE;
480 header.instance = instance_id;
481 header.pldm_type = PLDM_OEM;
482 header.command = command;
George Liub7095ff2021-06-14 16:01:57 +0800483 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
484 if (rc != PLDM_SUCCESS) {
vkaverap07404562019-08-05 22:57:11 -0500485 return rc;
486 }
487
488 struct pldm_read_write_file_by_type_memory_resp *response =
489 (struct pldm_read_write_file_by_type_memory_resp *)msg->payload;
490 response->completion_code = completion_code;
491 if (response->completion_code == PLDM_SUCCESS) {
492 response->length = htole32(length);
493 }
494
495 return PLDM_SUCCESS;
496}
497
498int encode_rw_file_by_type_memory_req(uint8_t instance_id, uint8_t command,
499 uint16_t file_type, uint32_t file_handle,
500 uint32_t offset, uint32_t length,
501 uint64_t address, struct pldm_msg *msg)
502{
vkaverap07404562019-08-05 22:57:11 -0500503 if (msg == NULL) {
504 return PLDM_ERROR_INVALID_DATA;
505 }
506
George Liub7095ff2021-06-14 16:01:57 +0800507 struct pldm_header_info header = {0};
vkaverap07404562019-08-05 22:57:11 -0500508 header.msg_type = PLDM_REQUEST;
509 header.instance = instance_id;
510 header.pldm_type = PLDM_OEM;
511 header.command = command;
George Liub7095ff2021-06-14 16:01:57 +0800512 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
513 if (rc != PLDM_SUCCESS) {
vkaverap07404562019-08-05 22:57:11 -0500514 return rc;
515 }
516
517 struct pldm_read_write_file_by_type_memory_req *req =
518 (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
519 req->file_type = htole16(file_type);
520 req->file_handle = htole32(file_handle);
521 req->offset = htole32(offset);
522 req->length = htole32(length);
523 req->address = htole64(address);
524
525 return PLDM_SUCCESS;
526}
527
528int decode_rw_file_by_type_memory_resp(const struct pldm_msg *msg,
529 size_t payload_length,
530 uint8_t *completion_code,
531 uint32_t *length)
532{
533 if (msg == NULL || length == NULL || completion_code == NULL) {
534 return PLDM_ERROR_INVALID_DATA;
535 }
536
537 if (payload_length != PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES) {
538 return PLDM_ERROR_INVALID_LENGTH;
539 }
540
541 struct pldm_read_write_file_by_type_memory_resp *response =
542 (struct pldm_read_write_file_by_type_memory_resp *)msg->payload;
543 *completion_code = response->completion_code;
544 if (*completion_code == PLDM_SUCCESS) {
545 *length = le32toh(response->length);
546 }
547
548 return PLDM_SUCCESS;
549}
vkaverapa9aac722019-08-22 02:10:15 -0500550
551int decode_new_file_req(const struct pldm_msg *msg, size_t payload_length,
552 uint16_t *file_type, uint32_t *file_handle,
Deepak Kodihalli83388762020-01-28 04:09:58 -0600553 uint64_t *length)
vkaverapa9aac722019-08-22 02:10:15 -0500554{
555 if (msg == NULL || file_type == NULL || file_handle == NULL ||
556 length == NULL) {
557 return PLDM_ERROR_INVALID_DATA;
558 }
559
560 if (payload_length != PLDM_NEW_FILE_REQ_BYTES) {
561 return PLDM_ERROR_INVALID_LENGTH;
562 }
563
564 struct pldm_new_file_req *request =
565 (struct pldm_new_file_req *)msg->payload;
566 *file_type = le16toh(request->file_type);
567 *file_handle = le32toh(request->file_handle);
Deepak Kodihalli83388762020-01-28 04:09:58 -0600568 *length = le64toh(request->length);
vkaverapa9aac722019-08-22 02:10:15 -0500569
570 return PLDM_SUCCESS;
571}
572
573int encode_new_file_resp(uint8_t instance_id, uint8_t completion_code,
574 struct pldm_msg *msg)
575{
vkaverapa9aac722019-08-22 02:10:15 -0500576 if (msg == NULL) {
577 return PLDM_ERROR_INVALID_DATA;
578 }
579
George Liub7095ff2021-06-14 16:01:57 +0800580 struct pldm_header_info header = {0};
vkaverapa9aac722019-08-22 02:10:15 -0500581 header.msg_type = PLDM_RESPONSE;
582 header.instance = instance_id;
583 header.pldm_type = PLDM_OEM;
584 header.command = PLDM_NEW_FILE_AVAILABLE;
George Liub7095ff2021-06-14 16:01:57 +0800585 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
586 if (rc != PLDM_SUCCESS) {
vkaverapa9aac722019-08-22 02:10:15 -0500587 return rc;
588 }
589
590 struct pldm_new_file_resp *response =
591 (struct pldm_new_file_resp *)msg->payload;
592 response->completion_code = completion_code;
593
594 return PLDM_SUCCESS;
595}
596
597int encode_new_file_req(uint8_t instance_id, uint16_t file_type,
Deepak Kodihalli83388762020-01-28 04:09:58 -0600598 uint32_t file_handle, uint64_t length,
vkaverapa9aac722019-08-22 02:10:15 -0500599 struct pldm_msg *msg)
600{
vkaverapa9aac722019-08-22 02:10:15 -0500601 if (msg == NULL) {
602 return PLDM_ERROR_INVALID_DATA;
603 }
604
George Liub7095ff2021-06-14 16:01:57 +0800605 struct pldm_header_info header = {0};
vkaverapa9aac722019-08-22 02:10:15 -0500606 header.msg_type = PLDM_REQUEST;
607 header.instance = instance_id;
608 header.pldm_type = PLDM_OEM;
609 header.command = PLDM_NEW_FILE_AVAILABLE;
George Liub7095ff2021-06-14 16:01:57 +0800610 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
611 if (rc != PLDM_SUCCESS) {
vkaverapa9aac722019-08-22 02:10:15 -0500612 return rc;
613 }
614
615 struct pldm_new_file_req *req =
616 (struct pldm_new_file_req *)msg->payload;
617 req->file_type = htole16(file_type);
618 req->file_handle = htole32(file_handle);
Deepak Kodihalli83388762020-01-28 04:09:58 -0600619 req->length = htole64(length);
vkaverapa9aac722019-08-22 02:10:15 -0500620
621 return PLDM_SUCCESS;
622}
623
624int decode_new_file_resp(const struct pldm_msg *msg, size_t payload_length,
625 uint8_t *completion_code)
626{
627 if (msg == NULL || completion_code == NULL) {
628 return PLDM_ERROR_INVALID_DATA;
629 }
630
631 if (payload_length != PLDM_NEW_FILE_RESP_BYTES) {
632 return PLDM_ERROR_INVALID_LENGTH;
633 }
634
635 struct pldm_new_file_resp *response =
636 (struct pldm_new_file_resp *)msg->payload;
637 *completion_code = response->completion_code;
638
639 return PLDM_SUCCESS;
640}
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600641
642int decode_rw_file_by_type_req(const struct pldm_msg *msg,
643 size_t payload_length, uint16_t *file_type,
644 uint32_t *file_handle, uint32_t *offset,
645 uint32_t *length)
646{
647 if (msg == NULL || file_type == NULL || file_handle == NULL ||
648 offset == NULL || length == NULL) {
649 return PLDM_ERROR_INVALID_DATA;
650 }
651
Sampa Misrad823cc02020-03-24 04:53:20 -0500652 if (payload_length < PLDM_RW_FILE_BY_TYPE_REQ_BYTES) {
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600653 return PLDM_ERROR_INVALID_LENGTH;
654 }
655
656 struct pldm_read_write_file_by_type_req *request =
657 (struct pldm_read_write_file_by_type_req *)msg->payload;
658 *file_type = le16toh(request->file_type);
659 *file_handle = le32toh(request->file_handle);
660 *offset = le32toh(request->offset);
661 *length = le32toh(request->length);
662
663 return PLDM_SUCCESS;
664}
665
666int encode_rw_file_by_type_resp(uint8_t instance_id, uint8_t command,
667 uint8_t completion_code, uint32_t length,
668 struct pldm_msg *msg)
669{
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600670 if (msg == NULL) {
671 return PLDM_ERROR_INVALID_DATA;
672 }
673 if (command != PLDM_READ_FILE_BY_TYPE &&
674 command != PLDM_WRITE_FILE_BY_TYPE) {
675 return PLDM_ERROR_INVALID_DATA;
676 }
677
George Liub7095ff2021-06-14 16:01:57 +0800678 struct pldm_header_info header = {0};
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600679 header.msg_type = PLDM_RESPONSE;
680 header.instance = instance_id;
681 header.pldm_type = PLDM_OEM;
682 header.command = command;
George Liub7095ff2021-06-14 16:01:57 +0800683 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
684 if (rc != PLDM_SUCCESS) {
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600685 return rc;
686 }
687
688 struct pldm_read_write_file_by_type_resp *response =
689 (struct pldm_read_write_file_by_type_resp *)msg->payload;
690 response->completion_code = completion_code;
691 if (response->completion_code == PLDM_SUCCESS) {
692 response->length = htole32(length);
693 }
694
695 return PLDM_SUCCESS;
696}
697
698int encode_rw_file_by_type_req(uint8_t instance_id, uint8_t command,
699 uint16_t file_type, uint32_t file_handle,
700 uint32_t offset, uint32_t length,
701 struct pldm_msg *msg)
702{
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600703 if (msg == NULL) {
704 return PLDM_ERROR_INVALID_DATA;
705 }
706 if (command != PLDM_READ_FILE_BY_TYPE &&
707 command != PLDM_WRITE_FILE_BY_TYPE) {
708 return PLDM_ERROR_INVALID_DATA;
709 }
710
George Liub7095ff2021-06-14 16:01:57 +0800711 struct pldm_header_info header = {0};
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600712 header.msg_type = PLDM_REQUEST;
713 header.instance = instance_id;
714 header.pldm_type = PLDM_OEM;
715 header.command = command;
George Liub7095ff2021-06-14 16:01:57 +0800716 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
717 if (rc != PLDM_SUCCESS) {
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600718 return rc;
719 }
720
721 struct pldm_read_write_file_by_type_req *req =
722 (struct pldm_read_write_file_by_type_req *)msg->payload;
723 req->file_type = htole16(file_type);
724 req->file_handle = htole32(file_handle);
725 req->offset = htole32(offset);
726 req->length = htole32(length);
727
728 return PLDM_SUCCESS;
729}
730
731int decode_rw_file_by_type_resp(const struct pldm_msg *msg,
732 size_t payload_length, uint8_t *completion_code,
733 uint32_t *length)
734{
735 if (msg == NULL || length == NULL || completion_code == NULL) {
736 return PLDM_ERROR_INVALID_DATA;
737 }
738
739 if (payload_length != PLDM_RW_FILE_BY_TYPE_RESP_BYTES) {
740 return PLDM_ERROR_INVALID_LENGTH;
741 }
742
743 struct pldm_read_write_file_by_type_resp *response =
744 (struct pldm_read_write_file_by_type_resp *)msg->payload;
745 *completion_code = response->completion_code;
746 if (*completion_code == PLDM_SUCCESS) {
747 *length = le32toh(response->length);
748 }
749
750 return PLDM_SUCCESS;
751}
vkaverapf4e0a492019-11-19 01:47:35 -0600752
753int decode_file_ack_req(const struct pldm_msg *msg, size_t payload_length,
754 uint16_t *file_type, uint32_t *file_handle,
755 uint8_t *file_status)
756{
757 if (msg == NULL || file_type == NULL || file_handle == NULL) {
758 return PLDM_ERROR_INVALID_DATA;
759 }
760
761 if (payload_length != PLDM_FILE_ACK_REQ_BYTES) {
762 return PLDM_ERROR_INVALID_LENGTH;
763 }
764
765 struct pldm_file_ack_req *request =
766 (struct pldm_file_ack_req *)msg->payload;
767 *file_type = le16toh(request->file_type);
768 *file_handle = le32toh(request->file_handle);
769 *file_status = request->file_status;
770
771 return PLDM_SUCCESS;
772}
773
774int encode_file_ack_resp(uint8_t instance_id, uint8_t completion_code,
775 struct pldm_msg *msg)
776{
vkaverapf4e0a492019-11-19 01:47:35 -0600777 if (msg == NULL) {
778 return PLDM_ERROR_INVALID_DATA;
779 }
780
George Liub7095ff2021-06-14 16:01:57 +0800781 struct pldm_header_info header = {0};
vkaverapf4e0a492019-11-19 01:47:35 -0600782 header.msg_type = PLDM_RESPONSE;
783 header.instance = instance_id;
784 header.pldm_type = PLDM_OEM;
785 header.command = PLDM_FILE_ACK;
George Liub7095ff2021-06-14 16:01:57 +0800786 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
787 if (rc != PLDM_SUCCESS) {
vkaverapf4e0a492019-11-19 01:47:35 -0600788 return rc;
789 }
790
791 struct pldm_file_ack_resp *response =
792 (struct pldm_file_ack_resp *)msg->payload;
793 response->completion_code = completion_code;
794
795 return PLDM_SUCCESS;
796}
797
798int encode_file_ack_req(uint8_t instance_id, uint16_t file_type,
799 uint32_t file_handle, uint8_t file_status,
800 struct pldm_msg *msg)
801{
vkaverapf4e0a492019-11-19 01:47:35 -0600802 if (msg == NULL) {
803 return PLDM_ERROR_INVALID_DATA;
804 }
805
George Liub7095ff2021-06-14 16:01:57 +0800806 struct pldm_header_info header = {0};
vkaverapf4e0a492019-11-19 01:47:35 -0600807 header.msg_type = PLDM_REQUEST;
808 header.instance = instance_id;
809 header.pldm_type = PLDM_OEM;
810 header.command = PLDM_FILE_ACK;
George Liub7095ff2021-06-14 16:01:57 +0800811 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
812 if (rc != PLDM_SUCCESS) {
vkaverapf4e0a492019-11-19 01:47:35 -0600813 return rc;
814 }
815
816 struct pldm_file_ack_req *req =
817 (struct pldm_file_ack_req *)msg->payload;
818 req->file_type = htole16(file_type);
819 req->file_handle = htole32(file_handle);
820 req->file_status = file_status;
821
822 return PLDM_SUCCESS;
823}
824
825int decode_file_ack_resp(const struct pldm_msg *msg, size_t payload_length,
826 uint8_t *completion_code)
827{
828 if (msg == NULL || completion_code == NULL) {
829 return PLDM_ERROR_INVALID_DATA;
830 }
831
832 if (payload_length != PLDM_FILE_ACK_RESP_BYTES) {
833 return PLDM_ERROR_INVALID_LENGTH;
834 }
835
836 struct pldm_file_ack_resp *response =
837 (struct pldm_file_ack_resp *)msg->payload;
838 *completion_code = response->completion_code;
839
840 return PLDM_SUCCESS;
841}
Sridevi Ramesh8b51fa22022-06-13 06:28:13 -0500842
843int encode_file_ack_with_meta_data_req(
844 uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
845 uint8_t file_status, uint32_t file_meta_data_1, uint32_t file_meta_data_2,
846 uint32_t file_meta_data_3, uint32_t file_meta_data_4, struct pldm_msg *msg)
847{
848 if (msg == NULL) {
849 return PLDM_ERROR_INVALID_DATA;
850 }
851
852 struct pldm_header_info header = {0};
853 header.msg_type = PLDM_REQUEST;
854 header.instance = instance_id;
855 header.pldm_type = PLDM_OEM;
856 header.command = PLDM_FILE_ACK_WITH_META_DATA;
857 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
858 if (rc != PLDM_SUCCESS) {
859 return rc;
860 }
861
862 struct pldm_file_ack_with_meta_data_req *req =
863 (struct pldm_file_ack_with_meta_data_req *)msg->payload;
864 req->file_type = htole16(file_type);
865 req->file_handle = htole32(file_handle);
866 req->file_status = file_status;
867 req->file_meta_data_1 = htole32(file_meta_data_1);
868 req->file_meta_data_2 = htole32(file_meta_data_2);
869 req->file_meta_data_3 = htole32(file_meta_data_3);
870 req->file_meta_data_4 = htole32(file_meta_data_4);
871
872 return PLDM_SUCCESS;
873}
874
875int decode_file_ack_with_meta_data_resp(const struct pldm_msg *msg,
876 size_t payload_length,
877 uint8_t *completion_code)
878{
879 if (msg == NULL || completion_code == NULL) {
880 return PLDM_ERROR_INVALID_DATA;
881 }
882
883 if (payload_length != PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES) {
884 return PLDM_ERROR_INVALID_LENGTH;
885 }
886
887 struct pldm_file_ack_with_meta_data_resp *response =
888 (struct pldm_file_ack_with_meta_data_resp *)msg->payload;
889 *completion_code = response->completion_code;
890
891 return PLDM_SUCCESS;
892}
893
894int decode_file_ack_with_meta_data_req(
895 const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
896 uint32_t *file_handle, uint8_t *file_status, uint32_t *file_meta_data_1,
897 uint32_t *file_meta_data_2, uint32_t *file_meta_data_3,
898 uint32_t *file_meta_data_4)
899{
900 if (msg == NULL || file_type == NULL || file_handle == NULL) {
901 return PLDM_ERROR_INVALID_DATA;
902 }
903
904 if (payload_length != PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES) {
905 return PLDM_ERROR_INVALID_LENGTH;
906 }
907
908 struct pldm_file_ack_with_meta_data_req *request =
909 (struct pldm_file_ack_with_meta_data_req *)msg->payload;
910 *file_type = le16toh(request->file_type);
911 *file_handle = le32toh(request->file_handle);
912 *file_status = request->file_status;
913 *file_meta_data_1 = le32toh(request->file_meta_data_1);
914 *file_meta_data_2 = le32toh(request->file_meta_data_2);
915 *file_meta_data_3 = le32toh(request->file_meta_data_3);
916 *file_meta_data_4 = le32toh(request->file_meta_data_4);
917
918 return PLDM_SUCCESS;
919}
920
921int encode_file_ack_with_meta_data_resp(uint8_t instance_id,
922 uint8_t completion_code,
923 struct pldm_msg *msg)
924{
925 if (msg == NULL) {
926 return PLDM_ERROR_INVALID_DATA;
927 }
928
929 struct pldm_header_info header = {0};
930 header.msg_type = PLDM_RESPONSE;
931 header.instance = instance_id;
932 header.pldm_type = PLDM_OEM;
933 header.command = PLDM_FILE_ACK_WITH_META_DATA;
934 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
935 if (rc != PLDM_SUCCESS) {
936 return rc;
937 }
938
939 struct pldm_file_ack_with_meta_data_resp *response =
940 (struct pldm_file_ack_with_meta_data_resp *)msg->payload;
941 response->completion_code = completion_code;
942
943 return PLDM_SUCCESS;
944}