Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 1 | #include "file_io.h" |
| 2 | #include <endian.h> |
| 3 | #include <string.h> |
| 4 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 5 | int decode_rw_file_memory_req(const struct pldm_msg *msg, size_t payload_length, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 6 | 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 | |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 18 | struct pldm_read_write_file_memory_req *request = |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 19 | (struct pldm_read_write_file_memory_req *)msg->payload; |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 20 | |
| 21 | *file_handle = le32toh(request->file_handle); |
| 22 | *offset = le32toh(request->offset); |
| 23 | *length = le32toh(request->length); |
| 24 | *address = le64toh(request->address); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 25 | |
| 26 | return PLDM_SUCCESS; |
| 27 | } |
| 28 | |
| 29 | int 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 Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 36 | header.msg_type = PLDM_RESPONSE; |
| 37 | header.instance = instance_id; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 38 | header.pldm_type = PLDM_OEM; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 39 | header.command = command; |
| 40 | |
| 41 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 42 | return rc; |
| 43 | } |
| 44 | |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 45 | 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 | |
| 55 | int 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 Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 84 | int decode_rw_file_memory_resp(const struct pldm_msg *msg, |
| 85 | size_t payload_length, uint8_t *completion_code, |
| 86 | uint32_t *length) |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 87 | { |
| 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 Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 97 | (struct pldm_read_write_file_memory_resp *)msg->payload; |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 98 | *completion_code = response->completion_code; |
| 99 | if (*completion_code == PLDM_SUCCESS) { |
| 100 | *length = le32toh(response->length); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return PLDM_SUCCESS; |
| 104 | } |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 105 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 106 | int decode_get_file_table_req(const struct pldm_msg *msg, size_t payload_length, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 107 | 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 Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 120 | (struct pldm_get_file_table_req *)msg->payload; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 121 | |
| 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 | |
| 129 | int 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 | } |