blob: 426033ac3e4479ae6cd0c982068d6fb296db65f4 [file] [log] [blame]
Tom Joseph7dae7772019-04-10 14:44:44 +05301#include "file_io.h"
2#include <endian.h>
3#include <string.h>
4
5int decode_read_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)
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_READ_FILE_MEM_REQ_BYTES) {
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
28int encode_read_file_memory_resp(uint8_t instance_id, uint8_t completion_code,
29 uint32_t length, struct pldm_msg *msg)
30{
31 struct pldm_header_info header = {0};
32 int rc = PLDM_SUCCESS;
33
34 uint8_t *payload = msg->payload;
35 *payload = completion_code;
36
37 header.msg_type = PLDM_RESPONSE;
38 header.instance = instance_id;
39 header.pldm_type = PLDM_IBM_OEM_TYPE;
40 header.command = PLDM_READ_FILE_INTO_MEMORY;
41
42 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
43 return rc;
44 }
45
46 if (msg->payload[0] == PLDM_SUCCESS) {
47 uint8_t *dst = msg->payload + sizeof(completion_code);
48 length = htole32(length);
49 memcpy(dst, &length, sizeof(length));
50 }
51
52 return PLDM_SUCCESS;
53}