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 | } |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 158 | |
| 159 | int decode_read_file_req(const struct pldm_msg *msg, size_t payload_length, |
| 160 | uint32_t *file_handle, uint32_t *offset, |
| 161 | uint32_t *length) |
| 162 | { |
| 163 | if (msg == NULL || file_handle == NULL || offset == NULL || |
| 164 | length == NULL) { |
| 165 | return PLDM_ERROR_INVALID_DATA; |
| 166 | } |
| 167 | |
| 168 | if (payload_length != PLDM_READ_FILE_REQ_BYTES) { |
| 169 | return PLDM_ERROR_INVALID_LENGTH; |
| 170 | } |
| 171 | |
| 172 | struct pldm_read_file_req *request = |
| 173 | (struct pldm_read_file_req *)msg->payload; |
| 174 | |
| 175 | *file_handle = le32toh(request->file_handle); |
| 176 | *offset = le32toh(request->offset); |
| 177 | *length = le32toh(request->length); |
| 178 | |
| 179 | return PLDM_SUCCESS; |
| 180 | } |
| 181 | |
| 182 | int encode_read_file_req(uint8_t instance_id, uint32_t file_handle, |
| 183 | uint32_t offset, uint32_t length, struct pldm_msg *msg) |
| 184 | { |
| 185 | struct pldm_header_info header = {0}; |
| 186 | int rc = PLDM_SUCCESS; |
| 187 | |
| 188 | header.msg_type = PLDM_REQUEST; |
| 189 | header.instance = instance_id; |
| 190 | header.pldm_type = PLDM_OEM; |
| 191 | header.command = PLDM_READ_FILE; |
| 192 | |
| 193 | if (msg == NULL) { |
| 194 | return PLDM_ERROR_INVALID_DATA; |
| 195 | } |
| 196 | |
| 197 | if (length == 0) { |
| 198 | return PLDM_INVALID_READ_LENGTH; |
| 199 | } |
| 200 | |
| 201 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 202 | return rc; |
| 203 | } |
| 204 | |
| 205 | struct pldm_read_file_req *request = |
| 206 | (struct pldm_read_file_req *)msg->payload; |
| 207 | |
| 208 | request->file_handle = htole32(file_handle); |
| 209 | request->offset = htole32(offset); |
| 210 | request->length = htole32(length); |
| 211 | |
| 212 | return PLDM_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | int decode_read_file_resp(const struct pldm_msg *msg, size_t payload_length, |
| 216 | uint8_t *completion_code, uint32_t *length, |
| 217 | size_t *file_data_offset) |
| 218 | { |
| 219 | if (msg == NULL || completion_code == NULL || length == NULL) { |
| 220 | return PLDM_ERROR_INVALID_DATA; |
| 221 | } |
| 222 | |
| 223 | if (payload_length < PLDM_READ_FILE_RESP_BYTES) { |
| 224 | return PLDM_ERROR_INVALID_LENGTH; |
| 225 | } |
| 226 | |
| 227 | struct pldm_read_file_resp *response = |
| 228 | (struct pldm_read_file_resp *)msg->payload; |
| 229 | |
| 230 | *completion_code = response->completion_code; |
| 231 | if (*completion_code == PLDM_SUCCESS) { |
| 232 | *length = le32toh(response->length); |
| 233 | if (payload_length != PLDM_READ_FILE_RESP_BYTES + *length) { |
| 234 | return PLDM_ERROR_INVALID_LENGTH; |
| 235 | } |
| 236 | *file_data_offset = sizeof(*completion_code) + sizeof(*length); |
| 237 | } |
| 238 | |
| 239 | return PLDM_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | int encode_read_file_resp(uint8_t instance_id, uint8_t completion_code, |
| 243 | uint32_t length, struct pldm_msg *msg) |
| 244 | { |
| 245 | struct pldm_header_info header = {0}; |
| 246 | int rc = PLDM_SUCCESS; |
| 247 | |
| 248 | header.msg_type = PLDM_RESPONSE; |
| 249 | header.instance = instance_id; |
| 250 | header.pldm_type = PLDM_OEM; |
| 251 | header.command = PLDM_READ_FILE; |
| 252 | |
| 253 | if (msg == NULL) { |
| 254 | return PLDM_ERROR_INVALID_DATA; |
| 255 | } |
| 256 | |
| 257 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | struct pldm_read_file_resp *response = |
| 262 | (struct pldm_read_file_resp *)msg->payload; |
| 263 | response->completion_code = completion_code; |
| 264 | |
| 265 | if (response->completion_code == PLDM_SUCCESS) { |
| 266 | response->length = htole32(length); |
| 267 | } |
| 268 | |
| 269 | return PLDM_SUCCESS; |
| 270 | } |
| 271 | |
| 272 | int decode_write_file_req(const struct pldm_msg *msg, size_t payload_length, |
| 273 | uint32_t *file_handle, uint32_t *offset, |
| 274 | uint32_t *length, size_t *file_data_offset) |
| 275 | { |
| 276 | if (msg == NULL || file_handle == NULL || length == NULL) { |
| 277 | return PLDM_ERROR_INVALID_DATA; |
| 278 | } |
| 279 | |
| 280 | if (payload_length < PLDM_WRITE_FILE_REQ_BYTES) { |
| 281 | return PLDM_ERROR_INVALID_LENGTH; |
| 282 | } |
| 283 | |
| 284 | struct pldm_write_file_req *request = |
| 285 | (struct pldm_write_file_req *)msg->payload; |
| 286 | |
| 287 | *file_handle = le32toh(request->file_handle); |
| 288 | *offset = le32toh(request->offset); |
| 289 | *length = le32toh(request->length); |
| 290 | if (payload_length != PLDM_WRITE_FILE_REQ_BYTES + *length) { |
| 291 | return PLDM_ERROR_INVALID_LENGTH; |
| 292 | } |
| 293 | *file_data_offset = |
| 294 | sizeof(*file_handle) + sizeof(*offset) + sizeof(*length); |
| 295 | |
| 296 | return PLDM_SUCCESS; |
| 297 | } |
| 298 | |
| 299 | int encode_write_file_req(uint8_t instance_id, uint32_t file_handle, |
| 300 | uint32_t offset, uint32_t length, |
| 301 | struct pldm_msg *msg) |
| 302 | { |
| 303 | struct pldm_header_info header = {0}; |
| 304 | int rc = PLDM_SUCCESS; |
| 305 | |
| 306 | header.msg_type = PLDM_REQUEST; |
| 307 | header.instance = instance_id; |
| 308 | header.pldm_type = PLDM_OEM; |
| 309 | header.command = PLDM_WRITE_FILE; |
| 310 | |
| 311 | if (msg == NULL) { |
| 312 | return PLDM_ERROR_INVALID_DATA; |
| 313 | } |
| 314 | |
| 315 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 316 | return rc; |
| 317 | } |
| 318 | |
| 319 | if (length == 0) { |
| 320 | return PLDM_INVALID_WRITE_LENGTH; |
| 321 | } |
| 322 | |
| 323 | struct pldm_write_file_req *request = |
| 324 | (struct pldm_write_file_req *)msg->payload; |
| 325 | |
| 326 | request->file_handle = htole32(file_handle); |
| 327 | request->offset = htole32(offset); |
| 328 | request->length = htole32(length); |
| 329 | |
| 330 | return PLDM_SUCCESS; |
| 331 | } |
| 332 | |
| 333 | int decode_write_file_resp(const struct pldm_msg *msg, size_t payload_length, |
| 334 | uint8_t *completion_code, uint32_t *length) |
| 335 | { |
| 336 | if (msg == NULL || completion_code == NULL || length == NULL) { |
| 337 | return PLDM_ERROR_INVALID_DATA; |
| 338 | } |
| 339 | |
| 340 | if (payload_length != PLDM_WRITE_FILE_RESP_BYTES) { |
| 341 | return PLDM_ERROR_INVALID_LENGTH; |
| 342 | } |
| 343 | |
| 344 | struct pldm_write_file_resp *response = |
| 345 | (struct pldm_write_file_resp *)msg->payload; |
| 346 | |
| 347 | *completion_code = le32toh(response->completion_code); |
| 348 | if (response->completion_code == PLDM_SUCCESS) { |
| 349 | *length = le32toh(response->length); |
| 350 | } |
| 351 | |
| 352 | return PLDM_SUCCESS; |
| 353 | } |
| 354 | |
| 355 | int encode_write_file_resp(uint8_t instance_id, uint8_t completion_code, |
| 356 | uint32_t length, struct pldm_msg *msg) |
| 357 | { |
| 358 | struct pldm_header_info header = {0}; |
| 359 | int rc = PLDM_SUCCESS; |
| 360 | |
| 361 | header.msg_type = PLDM_RESPONSE; |
| 362 | header.instance = instance_id; |
| 363 | header.pldm_type = PLDM_OEM; |
| 364 | header.command = PLDM_WRITE_FILE; |
| 365 | |
| 366 | if (msg == NULL) { |
| 367 | return PLDM_ERROR_INVALID_DATA; |
| 368 | } |
| 369 | |
| 370 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 371 | return rc; |
| 372 | } |
| 373 | |
| 374 | struct pldm_write_file_resp *response = |
| 375 | (struct pldm_write_file_resp *)msg->payload; |
| 376 | response->completion_code = completion_code; |
| 377 | |
| 378 | if (response->completion_code == PLDM_SUCCESS) { |
| 379 | response->length = htole32(length); |
| 380 | } |
| 381 | |
| 382 | return PLDM_SUCCESS; |
| 383 | } |