xref: /openbmc/libpldm/src/oem/meta/file_io.c (revision 84213eba)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 #include <libpldm/oem/meta/file_io.h>
3 #include <endian.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdio.h>
7 
8 #include "api.h"
9 #include "msgbuf.h"
10 #include "dsp/base.h"
11 
12 LIBPLDM_ABI_TESTING
13 void *pldm_oem_meta_file_io_write_req_data(
14 	struct pldm_oem_meta_file_io_write_req *req)
15 {
16 	return req->data;
17 }
18 
19 LIBPLDM_ABI_TESTING
20 int decode_oem_meta_file_io_write_req(
21 	const struct pldm_msg *msg, size_t payload_length,
22 	struct pldm_oem_meta_file_io_write_req *req, size_t req_length)
23 {
24 	struct pldm_msgbuf _buf;
25 	struct pldm_msgbuf *buf = &_buf;
26 	int rc;
27 
28 	if (msg == NULL || req == NULL) {
29 		return -EINVAL;
30 	}
31 
32 	if (req_length < sizeof(*req)) {
33 		return -EINVAL;
34 	}
35 
36 	rc = pldm_msgbuf_init_errno(buf,
37 				    PLDM_OEM_META_FILE_IO_WRITE_REQ_MIN_LENGTH,
38 				    msg->payload, payload_length);
39 	if (rc) {
40 		return rc;
41 	}
42 
43 	pldm_msgbuf_extract(buf, req->handle);
44 	rc = pldm_msgbuf_extract(buf, req->length);
45 	if (rc) {
46 		return rc;
47 	}
48 
49 	rc = pldm_msgbuf_extract_array(buf, req->length, req->data,
50 				       req_length - sizeof(*req));
51 	if (rc) {
52 		return rc;
53 	}
54 
55 	return pldm_msgbuf_destroy_consumed(buf);
56 }
57 
58 LIBPLDM_ABI_DEPRECATED
59 int decode_oem_meta_file_io_req(const struct pldm_msg *msg,
60 				size_t payload_length, uint8_t *file_handle,
61 				uint32_t *length, uint8_t *data)
62 {
63 	struct pldm_oem_meta_file_io_write_req *request_msg;
64 	size_t request_msg_len;
65 	int rc;
66 
67 	if (msg == NULL || file_handle == NULL || length == NULL ||
68 	    data == NULL) {
69 		return pldm_xlate_errno(-EINVAL);
70 	}
71 
72 	request_msg_len = sizeof(*request_msg) + payload_length;
73 	request_msg = malloc(request_msg_len);
74 
75 	rc = decode_oem_meta_file_io_write_req(msg, payload_length, request_msg,
76 					       request_msg_len);
77 	if (rc < 0) {
78 		free(request_msg);
79 		return pldm_xlate_errno(rc);
80 	}
81 
82 	*file_handle = request_msg->handle;
83 	*length = request_msg->length;
84 
85 	/* NOTE: Unsafe, memory safety is not possible due to API constraints. */
86 	memcpy(data, request_msg->data, request_msg->length);
87 
88 	free(request_msg);
89 
90 	return 0;
91 }
92