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