Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 1 | #include "file_io.h" |
| 2 | #include <endian.h> |
| 3 | #include <string.h> |
| 4 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 5 | int decode_rw_file_memory_req(const uint8_t *msg, size_t payload_length, |
| 6 | uint32_t *file_handle, uint32_t *offset, |
| 7 | uint32_t *length, uint64_t *address) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 8 | { |
| 9 | if (msg == NULL || file_handle == NULL || offset == NULL || |
| 10 | length == NULL || address == NULL) { |
| 11 | return PLDM_ERROR_INVALID_DATA; |
| 12 | } |
| 13 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 14 | if (payload_length != PLDM_RW_FILE_MEM_REQ_BYTES) { |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 15 | return PLDM_ERROR_INVALID_LENGTH; |
| 16 | } |
| 17 | |
| 18 | *file_handle = le32toh(*((uint32_t *)msg)); |
| 19 | *offset = le32toh(*((uint32_t *)(msg + sizeof(*file_handle)))); |
| 20 | *length = le32toh( |
| 21 | *((uint32_t *)(msg + sizeof(*file_handle) + sizeof(*offset)))); |
| 22 | *address = le64toh(*((uint64_t *)(msg + sizeof(*file_handle) + |
| 23 | sizeof(*offset) + sizeof(*length)))); |
| 24 | |
| 25 | return PLDM_SUCCESS; |
| 26 | } |
| 27 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 28 | int encode_rw_file_memory_resp(uint8_t instance_id, uint8_t command, |
| 29 | uint8_t completion_code, uint32_t length, |
| 30 | struct pldm_msg *msg) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 31 | { |
| 32 | struct pldm_header_info header = {0}; |
| 33 | int rc = PLDM_SUCCESS; |
| 34 | |
| 35 | uint8_t *payload = msg->payload; |
| 36 | *payload = completion_code; |
| 37 | |
| 38 | header.msg_type = PLDM_RESPONSE; |
| 39 | header.instance = instance_id; |
| 40 | header.pldm_type = PLDM_IBM_OEM_TYPE; |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 41 | header.command = command; |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 42 | |
| 43 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 44 | return rc; |
| 45 | } |
| 46 | |
| 47 | if (msg->payload[0] == PLDM_SUCCESS) { |
| 48 | uint8_t *dst = msg->payload + sizeof(completion_code); |
| 49 | length = htole32(length); |
| 50 | memcpy(dst, &length, sizeof(length)); |
| 51 | } |
| 52 | |
| 53 | return PLDM_SUCCESS; |
| 54 | } |
Tom Joseph | 61325fa | 2019-06-04 15:22:22 +0530 | [diff] [blame] | 55 | |
| 56 | int decode_get_file_table_req(const uint8_t *msg, size_t payload_length, |
| 57 | uint32_t *transfer_handle, |
| 58 | uint8_t *transfer_opflag, uint8_t *table_type) |
| 59 | { |
| 60 | if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL || |
| 61 | table_type == NULL) { |
| 62 | return PLDM_ERROR_INVALID_DATA; |
| 63 | } |
| 64 | |
| 65 | if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) { |
| 66 | return PLDM_ERROR_INVALID_LENGTH; |
| 67 | } |
| 68 | |
| 69 | struct pldm_get_file_table_req *request = |
| 70 | (struct pldm_get_file_table_req *)msg; |
| 71 | |
| 72 | *transfer_handle = le32toh(request->transfer_handle); |
| 73 | *transfer_opflag = request->operation_flag; |
| 74 | *table_type = request->table_type; |
| 75 | |
| 76 | return PLDM_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code, |
| 80 | uint32_t next_transfer_handle, |
| 81 | uint8_t transfer_flag, const uint8_t *table_data, |
| 82 | size_t table_size, struct pldm_msg *msg) |
| 83 | { |
| 84 | struct pldm_header_info header = {0}; |
| 85 | int rc = PLDM_SUCCESS; |
| 86 | |
| 87 | header.msg_type = PLDM_RESPONSE; |
| 88 | header.instance = instance_id; |
| 89 | header.pldm_type = PLDM_IBM_OEM_TYPE; |
| 90 | header.command = PLDM_GET_FILE_TABLE; |
| 91 | |
| 92 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | struct pldm_get_file_table_resp *response = |
| 97 | (struct pldm_get_file_table_resp *)msg->payload; |
| 98 | response->completion_code = completion_code; |
| 99 | |
| 100 | if (completion_code == PLDM_SUCCESS) { |
| 101 | response->next_transfer_handle = htole32(next_transfer_handle); |
| 102 | response->transfer_flag = transfer_flag; |
| 103 | memcpy(response->table_data, table_data, table_size); |
| 104 | } |
| 105 | |
| 106 | return PLDM_SUCCESS; |
| 107 | } |