blob: b3464cb0ff835cedf77c3901d3a602d3a3d07395 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include "libpldm/file_io.h"
2#include <endian.h>
3#include <string.h>
4
5int decode_rw_file_memory_req(const struct pldm_msg *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 struct pldm_read_write_file_memory_req *request =
19 (struct pldm_read_write_file_memory_req *)msg->payload;
20
21 *file_handle = le32toh(request->file_handle);
22 *offset = le32toh(request->offset);
23 *length = le32toh(request->length);
24 *address = le64toh(request->address);
25
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 if (msg == NULL) {
34 return PLDM_ERROR_INVALID_LENGTH;
35 }
36
37 struct pldm_header_info header = {0};
38 header.msg_type = PLDM_RESPONSE;
39 header.instance = instance_id;
40 header.pldm_type = PLDM_OEM;
41 header.command = command;
42 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
43 if (rc != PLDM_SUCCESS) {
44 return rc;
45 }
46
47 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{
62 if (msg == NULL) {
63 return PLDM_ERROR_INVALID_DATA;
64 }
65
66 struct pldm_header_info header = {0};
67 header.msg_type = PLDM_REQUEST;
68 header.instance = instance_id;
69 header.pldm_type = PLDM_OEM;
70 header.command = command;
71 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
72 if (rc != PLDM_SUCCESS) {
73 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
85int decode_rw_file_memory_resp(const struct pldm_msg *msg,
86 size_t payload_length, uint8_t *completion_code,
87 uint32_t *length)
88{
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 =
98 (struct pldm_read_write_file_memory_resp *)msg->payload;
99 *completion_code = response->completion_code;
100 if (*completion_code == PLDM_SUCCESS) {
101 *length = le32toh(response->length);
102 }
103
104 return PLDM_SUCCESS;
105}
106
107int decode_get_file_table_req(const struct pldm_msg *msg, size_t payload_length,
108 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 =
121 (struct pldm_get_file_table_req *)msg->payload;
122
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{
135 if (msg == NULL) {
136 return PLDM_ERROR_INVALID_LENGTH;
137 }
138
139 struct pldm_header_info header = {0};
140 header.msg_type = PLDM_RESPONSE;
141 header.instance = instance_id;
142 header.pldm_type = PLDM_OEM;
143 header.command = PLDM_GET_FILE_TABLE;
144 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
145 if (rc != PLDM_SUCCESS) {
146 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}
161
162int 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{
166 if (msg == NULL) {
167 return PLDM_ERROR_INVALID_DATA;
168 }
169
170 struct pldm_header_info header = {0};
171 header.msg_type = PLDM_REQUEST;
172 header.instance = instance_id;
173 header.pldm_type = PLDM_OEM;
174 header.command = PLDM_GET_FILE_TABLE;
175 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
176 if (rc != PLDM_SUCCESS) {
177 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
226int 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{
252 if (msg == NULL) {
253 return PLDM_ERROR_INVALID_DATA;
254 }
255
256 if (length == 0) {
257 return PLDM_ERROR_INVALID_LENGTH;
258 }
259
260 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) {
267 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{
310 if (msg == NULL) {
311 return PLDM_ERROR_INVALID_DATA;
312 }
313
314 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) {
321 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{
366 if (msg == NULL) {
367 return PLDM_ERROR_INVALID_DATA;
368 }
369
370 if (length == 0) {
371 return PLDM_ERROR_INVALID_LENGTH;
372 }
373
374 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
384 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{
419 if (msg == NULL) {
420 return PLDM_ERROR_INVALID_DATA;
421 }
422
423 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) {
430 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}
443
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{
474 if (msg == NULL) {
475 return PLDM_ERROR_INVALID_DATA;
476 }
477
478 struct pldm_header_info header = {0};
479 header.msg_type = PLDM_RESPONSE;
480 header.instance = instance_id;
481 header.pldm_type = PLDM_OEM;
482 header.command = command;
483 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
484 if (rc != PLDM_SUCCESS) {
485 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{
503 if (msg == NULL) {
504 return PLDM_ERROR_INVALID_DATA;
505 }
506
507 struct pldm_header_info header = {0};
508 header.msg_type = PLDM_REQUEST;
509 header.instance = instance_id;
510 header.pldm_type = PLDM_OEM;
511 header.command = command;
512 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
513 if (rc != PLDM_SUCCESS) {
514 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}
550
551int decode_new_file_req(const struct pldm_msg *msg, size_t payload_length,
552 uint16_t *file_type, uint32_t *file_handle,
553 uint64_t *length)
554{
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);
568 *length = le64toh(request->length);
569
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{
576 if (msg == NULL) {
577 return PLDM_ERROR_INVALID_DATA;
578 }
579
580 struct pldm_header_info header = {0};
581 header.msg_type = PLDM_RESPONSE;
582 header.instance = instance_id;
583 header.pldm_type = PLDM_OEM;
584 header.command = PLDM_NEW_FILE_AVAILABLE;
585 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
586 if (rc != PLDM_SUCCESS) {
587 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,
598 uint32_t file_handle, uint64_t length,
599 struct pldm_msg *msg)
600{
601 if (msg == NULL) {
602 return PLDM_ERROR_INVALID_DATA;
603 }
604
605 struct pldm_header_info header = {0};
606 header.msg_type = PLDM_REQUEST;
607 header.instance = instance_id;
608 header.pldm_type = PLDM_OEM;
609 header.command = PLDM_NEW_FILE_AVAILABLE;
610 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
611 if (rc != PLDM_SUCCESS) {
612 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);
619 req->length = htole64(length);
620
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}
641
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
652 if (payload_length < PLDM_RW_FILE_BY_TYPE_REQ_BYTES) {
653 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{
670 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
678 struct pldm_header_info header = {0};
679 header.msg_type = PLDM_RESPONSE;
680 header.instance = instance_id;
681 header.pldm_type = PLDM_OEM;
682 header.command = command;
683 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
684 if (rc != PLDM_SUCCESS) {
685 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{
703 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
711 struct pldm_header_info header = {0};
712 header.msg_type = PLDM_REQUEST;
713 header.instance = instance_id;
714 header.pldm_type = PLDM_OEM;
715 header.command = command;
716 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
717 if (rc != PLDM_SUCCESS) {
718 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}
752
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{
777 if (msg == NULL) {
778 return PLDM_ERROR_INVALID_DATA;
779 }
780
781 struct pldm_header_info header = {0};
782 header.msg_type = PLDM_RESPONSE;
783 header.instance = instance_id;
784 header.pldm_type = PLDM_OEM;
785 header.command = PLDM_FILE_ACK;
786 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
787 if (rc != PLDM_SUCCESS) {
788 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{
802 if (msg == NULL) {
803 return PLDM_ERROR_INVALID_DATA;
804 }
805
806 struct pldm_header_info header = {0};
807 header.msg_type = PLDM_REQUEST;
808 header.instance = instance_id;
809 header.pldm_type = PLDM_OEM;
810 header.command = PLDM_FILE_ACK;
811 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
812 if (rc != PLDM_SUCCESS) {
813 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}
842
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}
945
946int encode_new_file_with_metadata_req(
947 uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
948 uint64_t length, uint32_t file_meta_data_1, uint32_t file_meta_data_2,
949 uint32_t file_meta_data_3, uint32_t file_meta_data_4, struct pldm_msg *msg)
950{
951 if (msg == NULL) {
952 return PLDM_ERROR_INVALID_DATA;
953 }
954
955 struct pldm_header_info header = {0};
956 header.msg_type = PLDM_REQUEST;
957 header.instance = instance_id;
958 header.pldm_type = PLDM_OEM;
959 header.command = PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA;
960 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
961 if (rc != PLDM_SUCCESS) {
962 return rc;
963 }
964
965 struct pldm_new_file_with_metadata_req *req =
966 (struct pldm_new_file_with_metadata_req *)msg->payload;
967 req->file_type = htole16(file_type);
968 req->file_handle = htole32(file_handle);
969 req->length = htole64(length);
970 req->file_meta_data_1 = htole32(file_meta_data_1);
971 req->file_meta_data_2 = htole32(file_meta_data_2);
972 req->file_meta_data_3 = htole32(file_meta_data_3);
973 req->file_meta_data_4 = htole32(file_meta_data_4);
974
975 return PLDM_SUCCESS;
976}
977
978int decode_new_file_with_metadata_resp(const struct pldm_msg *msg,
979 size_t payload_length,
980 uint8_t *completion_code)
981{
982 if (msg == NULL || completion_code == NULL) {
983 return PLDM_ERROR_INVALID_DATA;
984 }
985
986 if (payload_length !=
987 PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES) {
988 return PLDM_ERROR_INVALID_LENGTH;
989 }
990
991 struct pldm_new_file_with_metadata_resp *response =
992 (struct pldm_new_file_with_metadata_resp *)msg->payload;
993
994 *completion_code = msg->payload[0];
995 if (*completion_code == PLDM_SUCCESS) {
996 *completion_code = response->completion_code;
997 }
998 return PLDM_SUCCESS;
999}
1000
1001int decode_new_file_with_metadata_req(
1002 const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
1003 uint32_t *file_handle, uint64_t *length, uint32_t *file_meta_data_1,
1004 uint32_t *file_meta_data_2, uint32_t *file_meta_data_3,
1005 uint32_t *file_meta_data_4)
1006{
1007 if (msg == NULL || file_type == NULL || file_handle == NULL ||
1008 length == NULL) {
1009 return PLDM_ERROR_INVALID_DATA;
1010 }
1011
1012 if (payload_length !=
1013 PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES) {
1014 return PLDM_ERROR_INVALID_LENGTH;
1015 }
1016
1017 struct pldm_new_file_with_metadata_req *request =
1018 (struct pldm_new_file_with_metadata_req *)msg->payload;
1019 *file_type = le16toh(request->file_type);
1020 *file_handle = le32toh(request->file_handle);
1021 *length = le64toh(request->length);
1022 *file_meta_data_1 = le32toh(request->file_meta_data_1);
1023 *file_meta_data_2 = le32toh(request->file_meta_data_2);
1024 *file_meta_data_3 = le32toh(request->file_meta_data_3);
1025 *file_meta_data_4 = le32toh(request->file_meta_data_4);
1026
1027 return PLDM_SUCCESS;
1028}
1029
1030int encode_new_file_with_metadata_resp(uint8_t instance_id,
1031 uint8_t completion_code,
1032 struct pldm_msg *msg)
1033{
1034 if (msg == NULL) {
1035 return PLDM_ERROR_INVALID_DATA;
1036 }
1037
1038 struct pldm_header_info header = {0};
1039 header.msg_type = PLDM_RESPONSE;
1040 header.instance = instance_id;
1041 header.pldm_type = PLDM_OEM;
1042 header.command = PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA;
1043 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1044 if (rc != PLDM_SUCCESS) {
1045 return rc;
1046 }
1047
1048 struct pldm_new_file_with_metadata_resp *response =
1049 (struct pldm_new_file_with_metadata_resp *)msg->payload;
1050
1051 if (response->completion_code == PLDM_SUCCESS) {
1052 response->completion_code = completion_code;
1053 }
1054
1055 return PLDM_SUCCESS;
1056}