blob: 16b56e6b05ce05dc98f14d291864bacb8b32e02d [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}
vkaverap2ffe3292019-06-24 00:08:13 -0500158
159int decode_read_file_req(const struct pldm_msg *msg, size_t payload_length,
160 uint32_t *file_handle, uint32_t *offset,
161 uint32_t *length)
162{
163 if (msg == NULL || file_handle == NULL || offset == NULL ||
164 length == NULL) {
165 return PLDM_ERROR_INVALID_DATA;
166 }
167
168 if (payload_length != PLDM_READ_FILE_REQ_BYTES) {
169 return PLDM_ERROR_INVALID_LENGTH;
170 }
171
172 struct pldm_read_file_req *request =
173 (struct pldm_read_file_req *)msg->payload;
174
175 *file_handle = le32toh(request->file_handle);
176 *offset = le32toh(request->offset);
177 *length = le32toh(request->length);
178
179 return PLDM_SUCCESS;
180}
181
182int encode_read_file_req(uint8_t instance_id, uint32_t file_handle,
183 uint32_t offset, uint32_t length, struct pldm_msg *msg)
184{
185 struct pldm_header_info header = {0};
186 int rc = PLDM_SUCCESS;
187
188 header.msg_type = PLDM_REQUEST;
189 header.instance = instance_id;
190 header.pldm_type = PLDM_OEM;
191 header.command = PLDM_READ_FILE;
192
193 if (msg == NULL) {
194 return PLDM_ERROR_INVALID_DATA;
195 }
196
197 if (length == 0) {
198 return PLDM_INVALID_READ_LENGTH;
199 }
200
201 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
202 return rc;
203 }
204
205 struct pldm_read_file_req *request =
206 (struct pldm_read_file_req *)msg->payload;
207
208 request->file_handle = htole32(file_handle);
209 request->offset = htole32(offset);
210 request->length = htole32(length);
211
212 return PLDM_SUCCESS;
213}
214
215int decode_read_file_resp(const struct pldm_msg *msg, size_t payload_length,
216 uint8_t *completion_code, uint32_t *length,
217 size_t *file_data_offset)
218{
219 if (msg == NULL || completion_code == NULL || length == NULL) {
220 return PLDM_ERROR_INVALID_DATA;
221 }
222
223 if (payload_length < PLDM_READ_FILE_RESP_BYTES) {
224 return PLDM_ERROR_INVALID_LENGTH;
225 }
226
227 struct pldm_read_file_resp *response =
228 (struct pldm_read_file_resp *)msg->payload;
229
230 *completion_code = response->completion_code;
231 if (*completion_code == PLDM_SUCCESS) {
232 *length = le32toh(response->length);
233 if (payload_length != PLDM_READ_FILE_RESP_BYTES + *length) {
234 return PLDM_ERROR_INVALID_LENGTH;
235 }
236 *file_data_offset = sizeof(*completion_code) + sizeof(*length);
237 }
238
239 return PLDM_SUCCESS;
240}
241
242int encode_read_file_resp(uint8_t instance_id, uint8_t completion_code,
243 uint32_t length, struct pldm_msg *msg)
244{
245 struct pldm_header_info header = {0};
246 int rc = PLDM_SUCCESS;
247
248 header.msg_type = PLDM_RESPONSE;
249 header.instance = instance_id;
250 header.pldm_type = PLDM_OEM;
251 header.command = PLDM_READ_FILE;
252
253 if (msg == NULL) {
254 return PLDM_ERROR_INVALID_DATA;
255 }
256
257 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
258 return rc;
259 }
260
261 struct pldm_read_file_resp *response =
262 (struct pldm_read_file_resp *)msg->payload;
263 response->completion_code = completion_code;
264
265 if (response->completion_code == PLDM_SUCCESS) {
266 response->length = htole32(length);
267 }
268
269 return PLDM_SUCCESS;
270}
271
272int decode_write_file_req(const struct pldm_msg *msg, size_t payload_length,
273 uint32_t *file_handle, uint32_t *offset,
274 uint32_t *length, size_t *file_data_offset)
275{
276 if (msg == NULL || file_handle == NULL || length == NULL) {
277 return PLDM_ERROR_INVALID_DATA;
278 }
279
280 if (payload_length < PLDM_WRITE_FILE_REQ_BYTES) {
281 return PLDM_ERROR_INVALID_LENGTH;
282 }
283
284 struct pldm_write_file_req *request =
285 (struct pldm_write_file_req *)msg->payload;
286
287 *file_handle = le32toh(request->file_handle);
288 *offset = le32toh(request->offset);
289 *length = le32toh(request->length);
290 if (payload_length != PLDM_WRITE_FILE_REQ_BYTES + *length) {
291 return PLDM_ERROR_INVALID_LENGTH;
292 }
293 *file_data_offset =
294 sizeof(*file_handle) + sizeof(*offset) + sizeof(*length);
295
296 return PLDM_SUCCESS;
297}
298
299int encode_write_file_req(uint8_t instance_id, uint32_t file_handle,
300 uint32_t offset, uint32_t length,
301 struct pldm_msg *msg)
302{
303 struct pldm_header_info header = {0};
304 int rc = PLDM_SUCCESS;
305
306 header.msg_type = PLDM_REQUEST;
307 header.instance = instance_id;
308 header.pldm_type = PLDM_OEM;
309 header.command = PLDM_WRITE_FILE;
310
311 if (msg == NULL) {
312 return PLDM_ERROR_INVALID_DATA;
313 }
314
315 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
316 return rc;
317 }
318
319 if (length == 0) {
320 return PLDM_INVALID_WRITE_LENGTH;
321 }
322
323 struct pldm_write_file_req *request =
324 (struct pldm_write_file_req *)msg->payload;
325
326 request->file_handle = htole32(file_handle);
327 request->offset = htole32(offset);
328 request->length = htole32(length);
329
330 return PLDM_SUCCESS;
331}
332
333int decode_write_file_resp(const struct pldm_msg *msg, size_t payload_length,
334 uint8_t *completion_code, uint32_t *length)
335{
336 if (msg == NULL || completion_code == NULL || length == NULL) {
337 return PLDM_ERROR_INVALID_DATA;
338 }
339
340 if (payload_length != PLDM_WRITE_FILE_RESP_BYTES) {
341 return PLDM_ERROR_INVALID_LENGTH;
342 }
343
344 struct pldm_write_file_resp *response =
345 (struct pldm_write_file_resp *)msg->payload;
346
347 *completion_code = le32toh(response->completion_code);
348 if (response->completion_code == PLDM_SUCCESS) {
349 *length = le32toh(response->length);
350 }
351
352 return PLDM_SUCCESS;
353}
354
355int encode_write_file_resp(uint8_t instance_id, uint8_t completion_code,
356 uint32_t length, struct pldm_msg *msg)
357{
358 struct pldm_header_info header = {0};
359 int rc = PLDM_SUCCESS;
360
361 header.msg_type = PLDM_RESPONSE;
362 header.instance = instance_id;
363 header.pldm_type = PLDM_OEM;
364 header.command = PLDM_WRITE_FILE;
365
366 if (msg == NULL) {
367 return PLDM_ERROR_INVALID_DATA;
368 }
369
370 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
371 return rc;
372 }
373
374 struct pldm_write_file_resp *response =
375 (struct pldm_write_file_resp *)msg->payload;
376 response->completion_code = completion_code;
377
378 if (response->completion_code == PLDM_SUCCESS) {
379 response->length = htole32(length);
380 }
381
382 return PLDM_SUCCESS;
383}
vkaverap07404562019-08-05 22:57:11 -0500384
385int decode_rw_file_by_type_memory_req(const struct pldm_msg *msg,
386 size_t payload_length,
387 uint16_t *file_type,
388 uint32_t *file_handle, uint32_t *offset,
389 uint32_t *length, uint64_t *address)
390{
391 if (msg == NULL || file_type == NULL || file_handle == NULL ||
392 offset == NULL || length == NULL || address == NULL) {
393 return PLDM_ERROR_INVALID_DATA;
394 }
395
396 if (payload_length != PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES) {
397 return PLDM_ERROR_INVALID_LENGTH;
398 }
399
400 struct pldm_read_write_file_by_type_memory_req *request =
401 (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
402 *file_type = le16toh(request->file_type);
403 *file_handle = le32toh(request->file_handle);
404 *offset = le32toh(request->offset);
405 *length = le32toh(request->length);
406 *address = le64toh(request->address);
407
408 return PLDM_SUCCESS;
409}
410
411int encode_rw_file_by_type_memory_resp(uint8_t instance_id, uint8_t command,
412 uint8_t completion_code, uint32_t length,
413 struct pldm_msg *msg)
414{
415 struct pldm_header_info header = {0};
416 int rc = PLDM_SUCCESS;
417
418 if (msg == NULL) {
419 return PLDM_ERROR_INVALID_DATA;
420 }
421
422 header.msg_type = PLDM_RESPONSE;
423 header.instance = instance_id;
424 header.pldm_type = PLDM_OEM;
425 header.command = command;
426
427 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
428 return rc;
429 }
430
431 struct pldm_read_write_file_by_type_memory_resp *response =
432 (struct pldm_read_write_file_by_type_memory_resp *)msg->payload;
433 response->completion_code = completion_code;
434 if (response->completion_code == PLDM_SUCCESS) {
435 response->length = htole32(length);
436 }
437
438 return PLDM_SUCCESS;
439}
440
441int encode_rw_file_by_type_memory_req(uint8_t instance_id, uint8_t command,
442 uint16_t file_type, uint32_t file_handle,
443 uint32_t offset, uint32_t length,
444 uint64_t address, struct pldm_msg *msg)
445{
446 struct pldm_header_info header = {0};
447 int rc = PLDM_SUCCESS;
448
449 if (msg == NULL) {
450 return PLDM_ERROR_INVALID_DATA;
451 }
452
453 header.msg_type = PLDM_REQUEST;
454 header.instance = instance_id;
455 header.pldm_type = PLDM_OEM;
456 header.command = command;
457
458 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
459 return rc;
460 }
461
462 struct pldm_read_write_file_by_type_memory_req *req =
463 (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
464 req->file_type = htole16(file_type);
465 req->file_handle = htole32(file_handle);
466 req->offset = htole32(offset);
467 req->length = htole32(length);
468 req->address = htole64(address);
469
470 return PLDM_SUCCESS;
471}
472
473int decode_rw_file_by_type_memory_resp(const struct pldm_msg *msg,
474 size_t payload_length,
475 uint8_t *completion_code,
476 uint32_t *length)
477{
478 if (msg == NULL || length == NULL || completion_code == NULL) {
479 return PLDM_ERROR_INVALID_DATA;
480 }
481
482 if (payload_length != PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES) {
483 return PLDM_ERROR_INVALID_LENGTH;
484 }
485
486 struct pldm_read_write_file_by_type_memory_resp *response =
487 (struct pldm_read_write_file_by_type_memory_resp *)msg->payload;
488 *completion_code = response->completion_code;
489 if (*completion_code == PLDM_SUCCESS) {
490 *length = le32toh(response->length);
491 }
492
493 return PLDM_SUCCESS;
494}
vkaverapa9aac722019-08-22 02:10:15 -0500495
496int decode_new_file_req(const struct pldm_msg *msg, size_t payload_length,
497 uint16_t *file_type, uint32_t *file_handle,
Deepak Kodihalli83388762020-01-28 04:09:58 -0600498 uint64_t *length)
vkaverapa9aac722019-08-22 02:10:15 -0500499{
500 if (msg == NULL || file_type == NULL || file_handle == NULL ||
501 length == NULL) {
502 return PLDM_ERROR_INVALID_DATA;
503 }
504
505 if (payload_length != PLDM_NEW_FILE_REQ_BYTES) {
506 return PLDM_ERROR_INVALID_LENGTH;
507 }
508
509 struct pldm_new_file_req *request =
510 (struct pldm_new_file_req *)msg->payload;
511 *file_type = le16toh(request->file_type);
512 *file_handle = le32toh(request->file_handle);
Deepak Kodihalli83388762020-01-28 04:09:58 -0600513 *length = le64toh(request->length);
vkaverapa9aac722019-08-22 02:10:15 -0500514
515 return PLDM_SUCCESS;
516}
517
518int encode_new_file_resp(uint8_t instance_id, uint8_t completion_code,
519 struct pldm_msg *msg)
520{
521 struct pldm_header_info header = {0};
522 int rc = PLDM_SUCCESS;
523
524 if (msg == NULL) {
525 return PLDM_ERROR_INVALID_DATA;
526 }
527
528 header.msg_type = PLDM_RESPONSE;
529 header.instance = instance_id;
530 header.pldm_type = PLDM_OEM;
531 header.command = PLDM_NEW_FILE_AVAILABLE;
532
533 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
534 return rc;
535 }
536
537 struct pldm_new_file_resp *response =
538 (struct pldm_new_file_resp *)msg->payload;
539 response->completion_code = completion_code;
540
541 return PLDM_SUCCESS;
542}
543
544int encode_new_file_req(uint8_t instance_id, uint16_t file_type,
Deepak Kodihalli83388762020-01-28 04:09:58 -0600545 uint32_t file_handle, uint64_t length,
vkaverapa9aac722019-08-22 02:10:15 -0500546 struct pldm_msg *msg)
547{
548 struct pldm_header_info header = {0};
549 int rc = PLDM_SUCCESS;
550
551 if (msg == NULL) {
552 return PLDM_ERROR_INVALID_DATA;
553 }
554
555 header.msg_type = PLDM_REQUEST;
556 header.instance = instance_id;
557 header.pldm_type = PLDM_OEM;
558 header.command = PLDM_NEW_FILE_AVAILABLE;
559
560 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
561 return rc;
562 }
563
564 struct pldm_new_file_req *req =
565 (struct pldm_new_file_req *)msg->payload;
566 req->file_type = htole16(file_type);
567 req->file_handle = htole32(file_handle);
Deepak Kodihalli83388762020-01-28 04:09:58 -0600568 req->length = htole64(length);
vkaverapa9aac722019-08-22 02:10:15 -0500569
570 return PLDM_SUCCESS;
571}
572
573int decode_new_file_resp(const struct pldm_msg *msg, size_t payload_length,
574 uint8_t *completion_code)
575{
576 if (msg == NULL || completion_code == NULL) {
577 return PLDM_ERROR_INVALID_DATA;
578 }
579
580 if (payload_length != PLDM_NEW_FILE_RESP_BYTES) {
581 return PLDM_ERROR_INVALID_LENGTH;
582 }
583
584 struct pldm_new_file_resp *response =
585 (struct pldm_new_file_resp *)msg->payload;
586 *completion_code = response->completion_code;
587
588 return PLDM_SUCCESS;
589}
Deepak Kodihallidce1c992019-11-19 07:06:53 -0600590
591int decode_rw_file_by_type_req(const struct pldm_msg *msg,
592 size_t payload_length, uint16_t *file_type,
593 uint32_t *file_handle, uint32_t *offset,
594 uint32_t *length)
595{
596 if (msg == NULL || file_type == NULL || file_handle == NULL ||
597 offset == NULL || length == NULL) {
598 return PLDM_ERROR_INVALID_DATA;
599 }
600
601 if (payload_length != PLDM_RW_FILE_BY_TYPE_REQ_BYTES) {
602 return PLDM_ERROR_INVALID_LENGTH;
603 }
604
605 struct pldm_read_write_file_by_type_req *request =
606 (struct pldm_read_write_file_by_type_req *)msg->payload;
607 *file_type = le16toh(request->file_type);
608 *file_handle = le32toh(request->file_handle);
609 *offset = le32toh(request->offset);
610 *length = le32toh(request->length);
611
612 return PLDM_SUCCESS;
613}
614
615int encode_rw_file_by_type_resp(uint8_t instance_id, uint8_t command,
616 uint8_t completion_code, uint32_t length,
617 struct pldm_msg *msg)
618{
619 struct pldm_header_info header = {0};
620 int rc = PLDM_SUCCESS;
621
622 if (msg == NULL) {
623 return PLDM_ERROR_INVALID_DATA;
624 }
625 if (command != PLDM_READ_FILE_BY_TYPE &&
626 command != PLDM_WRITE_FILE_BY_TYPE) {
627 return PLDM_ERROR_INVALID_DATA;
628 }
629
630 header.msg_type = PLDM_RESPONSE;
631 header.instance = instance_id;
632 header.pldm_type = PLDM_OEM;
633 header.command = command;
634
635 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
636 return rc;
637 }
638
639 struct pldm_read_write_file_by_type_resp *response =
640 (struct pldm_read_write_file_by_type_resp *)msg->payload;
641 response->completion_code = completion_code;
642 if (response->completion_code == PLDM_SUCCESS) {
643 response->length = htole32(length);
644 }
645
646 return PLDM_SUCCESS;
647}
648
649int encode_rw_file_by_type_req(uint8_t instance_id, uint8_t command,
650 uint16_t file_type, uint32_t file_handle,
651 uint32_t offset, uint32_t length,
652 struct pldm_msg *msg)
653{
654 struct pldm_header_info header = {0};
655 int rc = PLDM_SUCCESS;
656
657 if (msg == NULL) {
658 return PLDM_ERROR_INVALID_DATA;
659 }
660 if (command != PLDM_READ_FILE_BY_TYPE &&
661 command != PLDM_WRITE_FILE_BY_TYPE) {
662 return PLDM_ERROR_INVALID_DATA;
663 }
664
665 header.msg_type = PLDM_REQUEST;
666 header.instance = instance_id;
667 header.pldm_type = PLDM_OEM;
668 header.command = command;
669
670 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
671 return rc;
672 }
673
674 struct pldm_read_write_file_by_type_req *req =
675 (struct pldm_read_write_file_by_type_req *)msg->payload;
676 req->file_type = htole16(file_type);
677 req->file_handle = htole32(file_handle);
678 req->offset = htole32(offset);
679 req->length = htole32(length);
680
681 return PLDM_SUCCESS;
682}
683
684int decode_rw_file_by_type_resp(const struct pldm_msg *msg,
685 size_t payload_length, uint8_t *completion_code,
686 uint32_t *length)
687{
688 if (msg == NULL || length == NULL || completion_code == NULL) {
689 return PLDM_ERROR_INVALID_DATA;
690 }
691
692 if (payload_length != PLDM_RW_FILE_BY_TYPE_RESP_BYTES) {
693 return PLDM_ERROR_INVALID_LENGTH;
694 }
695
696 struct pldm_read_write_file_by_type_resp *response =
697 (struct pldm_read_write_file_by_type_resp *)msg->payload;
698 *completion_code = response->completion_code;
699 if (*completion_code == PLDM_SUCCESS) {
700 *length = le32toh(response->length);
701 }
702
703 return PLDM_SUCCESS;
704}
vkaverapf4e0a492019-11-19 01:47:35 -0600705
706int decode_file_ack_req(const struct pldm_msg *msg, size_t payload_length,
707 uint16_t *file_type, uint32_t *file_handle,
708 uint8_t *file_status)
709{
710 if (msg == NULL || file_type == NULL || file_handle == NULL) {
711 return PLDM_ERROR_INVALID_DATA;
712 }
713
714 if (payload_length != PLDM_FILE_ACK_REQ_BYTES) {
715 return PLDM_ERROR_INVALID_LENGTH;
716 }
717
718 struct pldm_file_ack_req *request =
719 (struct pldm_file_ack_req *)msg->payload;
720 *file_type = le16toh(request->file_type);
721 *file_handle = le32toh(request->file_handle);
722 *file_status = request->file_status;
723
724 return PLDM_SUCCESS;
725}
726
727int encode_file_ack_resp(uint8_t instance_id, uint8_t completion_code,
728 struct pldm_msg *msg)
729{
730 struct pldm_header_info header = {0};
731 int rc = PLDM_SUCCESS;
732
733 if (msg == NULL) {
734 return PLDM_ERROR_INVALID_DATA;
735 }
736
737 header.msg_type = PLDM_RESPONSE;
738 header.instance = instance_id;
739 header.pldm_type = PLDM_OEM;
740 header.command = PLDM_FILE_ACK;
741
742 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
743 return rc;
744 }
745
746 struct pldm_file_ack_resp *response =
747 (struct pldm_file_ack_resp *)msg->payload;
748 response->completion_code = completion_code;
749
750 return PLDM_SUCCESS;
751}
752
753int encode_file_ack_req(uint8_t instance_id, uint16_t file_type,
754 uint32_t file_handle, uint8_t file_status,
755 struct pldm_msg *msg)
756{
757 struct pldm_header_info header = {0};
758 int rc = PLDM_SUCCESS;
759
760 if (msg == NULL) {
761 return PLDM_ERROR_INVALID_DATA;
762 }
763
764 header.msg_type = PLDM_REQUEST;
765 header.instance = instance_id;
766 header.pldm_type = PLDM_OEM;
767 header.command = PLDM_FILE_ACK;
768
769 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
770 return rc;
771 }
772
773 struct pldm_file_ack_req *req =
774 (struct pldm_file_ack_req *)msg->payload;
775 req->file_type = htole16(file_type);
776 req->file_handle = htole32(file_handle);
777 req->file_status = file_status;
778
779 return PLDM_SUCCESS;
780}
781
782int decode_file_ack_resp(const struct pldm_msg *msg, size_t payload_length,
783 uint8_t *completion_code)
784{
785 if (msg == NULL || completion_code == NULL) {
786 return PLDM_ERROR_INVALID_DATA;
787 }
788
789 if (payload_length != PLDM_FILE_ACK_RESP_BYTES) {
790 return PLDM_ERROR_INVALID_LENGTH;
791 }
792
793 struct pldm_file_ack_resp *response =
794 (struct pldm_file_ack_resp *)msg->payload;
795 *completion_code = response->completion_code;
796
797 return PLDM_SUCCESS;
798}