Implement response encode/request decode for ReadFileIntoMemory

The ReadFileIntoMemory command is an OEM command to support large
size transfer from BMC to Host via DMA mechanism.

Change-Id: I416a1af8a4d1ea91a7dffbbb98ae58eeccf6d0ea
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/libpldm/file_io.c b/libpldm/file_io.c
new file mode 100644
index 0000000..426033a
--- /dev/null
+++ b/libpldm/file_io.c
@@ -0,0 +1,53 @@
+#include "file_io.h"
+#include <endian.h>
+#include <string.h>
+
+int decode_read_file_memory_req(const uint8_t *msg, size_t payload_length,
+				uint32_t *file_handle, uint32_t *offset,
+				uint32_t *length, uint64_t *address)
+{
+	if (msg == NULL || file_handle == NULL || offset == NULL ||
+	    length == NULL || address == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_READ_FILE_MEM_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	*file_handle = le32toh(*((uint32_t *)msg));
+	*offset = le32toh(*((uint32_t *)(msg + sizeof(*file_handle))));
+	*length = le32toh(
+	    *((uint32_t *)(msg + sizeof(*file_handle) + sizeof(*offset))));
+	*address = le64toh(*((uint64_t *)(msg + sizeof(*file_handle) +
+					  sizeof(*offset) + sizeof(*length))));
+
+	return PLDM_SUCCESS;
+}
+
+int encode_read_file_memory_resp(uint8_t instance_id, uint8_t completion_code,
+				 uint32_t length, struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	uint8_t *payload = msg->payload;
+	*payload = completion_code;
+
+	header.msg_type = PLDM_RESPONSE;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_IBM_OEM_TYPE;
+	header.command = PLDM_READ_FILE_INTO_MEMORY;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	if (msg->payload[0] == PLDM_SUCCESS) {
+		uint8_t *dst = msg->payload + sizeof(completion_code);
+		length = htole32(length);
+		memcpy(dst, &length, sizeof(length));
+	}
+
+	return PLDM_SUCCESS;
+}