Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 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> |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 4 | #include <stdlib.h> |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdio.h> |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 7 | |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 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) |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 23 | { |
| 24 | struct pldm_msgbuf _buf; |
| 25 | struct pldm_msgbuf *buf = &_buf; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 26 | int rc; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 27 | |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 28 | if (msg == NULL || req == NULL) { |
| 29 | return -EINVAL; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 30 | } |
| 31 | |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 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); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 39 | if (rc) { |
| 40 | return rc; |
| 41 | } |
| 42 | |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 43 | pldm_msgbuf_extract(buf, req->handle); |
| 44 | rc = pldm_msgbuf_extract(buf, req->length); |
| 45 | if (rc) { |
| 46 | return rc; |
| 47 | } |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 48 | |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 49 | rc = pldm_msgbuf_extract_array(buf, req->length, req->data, |
| 50 | req_length - sizeof(*req)); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 51 | if (rc) { |
| 52 | return rc; |
| 53 | } |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 54 | |
| 55 | return pldm_msgbuf_destroy_consumed(buf); |
| 56 | } |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame^] | 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 | } |