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 | |
Priyanga | f4736d4 | 2019-05-02 14:48:04 +0530 | [diff] [blame^] | 18 | struct pldm_read_write_file_memory_req *request = |
| 19 | (struct pldm_read_write_file_memory_req *)msg; |
| 20 | |
| 21 | *file_handle = le32toh(request->file_handle); |
| 22 | *offset = le32toh(request->offset); |
| 23 | *length = le32toh(request->length); |
| 24 | *address = le64toh(request->address); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 25 | |
| 26 | return PLDM_SUCCESS; |
| 27 | } |
| 28 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 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) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 32 | { |
| 33 | struct pldm_header_info header = {0}; |
| 34 | int rc = PLDM_SUCCESS; |
| 35 | |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 36 | header.msg_type = PLDM_RESPONSE; |
| 37 | header.instance = instance_id; |
| 38 | header.pldm_type = PLDM_IBM_OEM_TYPE; |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 39 | header.command = command; |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 40 | |
| 41 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 42 | return rc; |
| 43 | } |
| 44 | |
Priyanga | f4736d4 | 2019-05-02 14:48:04 +0530 | [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_IBM_OEM_TYPE; |
| 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 | |
| 84 | int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length, |
| 85 | uint8_t *completion_code, uint32_t *length) |
| 86 | { |
| 87 | if (msg == NULL || length == NULL || completion_code == NULL) { |
| 88 | return PLDM_ERROR_INVALID_DATA; |
| 89 | } |
| 90 | |
| 91 | if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) { |
| 92 | return PLDM_ERROR_INVALID_LENGTH; |
| 93 | } |
| 94 | |
| 95 | struct pldm_read_write_file_memory_resp *response = |
| 96 | (struct pldm_read_write_file_memory_resp *)msg; |
| 97 | *completion_code = response->completion_code; |
| 98 | if (*completion_code == PLDM_SUCCESS) { |
| 99 | *length = le32toh(response->length); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return PLDM_SUCCESS; |
| 103 | } |
Tom Joseph | 61325fa | 2019-06-04 15:22:22 +0530 | [diff] [blame] | 104 | |
| 105 | int decode_get_file_table_req(const uint8_t *msg, size_t payload_length, |
| 106 | uint32_t *transfer_handle, |
| 107 | uint8_t *transfer_opflag, uint8_t *table_type) |
| 108 | { |
| 109 | if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL || |
| 110 | table_type == NULL) { |
| 111 | return PLDM_ERROR_INVALID_DATA; |
| 112 | } |
| 113 | |
| 114 | if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) { |
| 115 | return PLDM_ERROR_INVALID_LENGTH; |
| 116 | } |
| 117 | |
| 118 | struct pldm_get_file_table_req *request = |
| 119 | (struct pldm_get_file_table_req *)msg; |
| 120 | |
| 121 | *transfer_handle = le32toh(request->transfer_handle); |
| 122 | *transfer_opflag = request->operation_flag; |
| 123 | *table_type = request->table_type; |
| 124 | |
| 125 | return PLDM_SUCCESS; |
| 126 | } |
| 127 | |
| 128 | int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code, |
| 129 | uint32_t next_transfer_handle, |
| 130 | uint8_t transfer_flag, const uint8_t *table_data, |
| 131 | size_t table_size, struct pldm_msg *msg) |
| 132 | { |
| 133 | struct pldm_header_info header = {0}; |
| 134 | int rc = PLDM_SUCCESS; |
| 135 | |
| 136 | header.msg_type = PLDM_RESPONSE; |
| 137 | header.instance = instance_id; |
| 138 | header.pldm_type = PLDM_IBM_OEM_TYPE; |
| 139 | header.command = PLDM_GET_FILE_TABLE; |
| 140 | |
| 141 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 142 | return rc; |
| 143 | } |
| 144 | |
| 145 | struct pldm_get_file_table_resp *response = |
| 146 | (struct pldm_get_file_table_resp *)msg->payload; |
| 147 | response->completion_code = completion_code; |
| 148 | |
| 149 | if (completion_code == PLDM_SUCCESS) { |
| 150 | response->next_transfer_handle = htole32(next_transfer_handle); |
| 151 | response->transfer_flag = transfer_flag; |
| 152 | memcpy(response->table_data, table_data, table_size); |
| 153 | } |
| 154 | |
| 155 | return PLDM_SUCCESS; |
| 156 | } |