blob: ce7d9e3191265259a1cf5a5ea1f4c6b1acd03dbe [file] [log] [blame]
Delphine CC Chiu22fad392023-10-27 11:05:01 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2#include <libpldm/oem/meta/file_io.h>
3#include <endian.h>
Lora Lin0f5be282024-07-22 09:46:18 +08004#include <stdlib.h>
Delphine CC Chiu22fad392023-10-27 11:05:01 +08005#include <string.h>
6#include <stdio.h>
Delphine CC Chiu22fad392023-10-27 11:05:01 +08007
Lora Lin0f5be282024-07-22 09:46:18 +08008#include "api.h"
9#include "msgbuf.h"
10#include "dsp/base.h"
11
12LIBPLDM_ABI_TESTING
13void *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
19LIBPLDM_ABI_TESTING
20int 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)
Delphine CC Chiu22fad392023-10-27 11:05:01 +080023{
24 struct pldm_msgbuf _buf;
25 struct pldm_msgbuf *buf = &_buf;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000026 int rc;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080027
Lora Lin0f5be282024-07-22 09:46:18 +080028 if (msg == NULL || req == NULL) {
29 return -EINVAL;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080030 }
31
Lora Lin0f5be282024-07-22 09:46:18 +080032 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);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080039 if (rc) {
40 return rc;
41 }
42
Lora Lin0f5be282024-07-22 09:46:18 +080043 pldm_msgbuf_extract(buf, req->handle);
44 rc = pldm_msgbuf_extract(buf, req->length);
45 if (rc) {
46 return rc;
47 }
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000048
Lora Lin0f5be282024-07-22 09:46:18 +080049 rc = pldm_msgbuf_extract_array(buf, req->length, req->data,
50 req_length - sizeof(*req));
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000051 if (rc) {
52 return rc;
53 }
Delphine CC Chiu22fad392023-10-27 11:05:01 +080054
55 return pldm_msgbuf_destroy_consumed(buf);
56}
Lora Lin0f5be282024-07-22 09:46:18 +080057
58LIBPLDM_ABI_DEPRECATED
59int 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}