Sync OEM:libpldm encode/decode APIs into PLDM

Change-Id: I756506e8f1678d6ee82528fdb26ea817d4a2b3cf
Signed-off-by: Priyanga <priyram1@in.ibm.com>
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index a415414..fd96f1c 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -15,12 +15,13 @@
 		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))));
+	struct pldm_read_write_file_memory_req *request =
+	    (struct pldm_read_write_file_memory_req *)msg;
+
+	*file_handle = le32toh(request->file_handle);
+	*offset = le32toh(request->offset);
+	*length = le32toh(request->length);
+	*address = le64toh(request->address);
 
 	return PLDM_SUCCESS;
 }
@@ -32,9 +33,6 @@
 	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_OEM;
@@ -44,10 +42,61 @@
 		return rc;
 	}
 
-	if (msg->payload[0] == PLDM_SUCCESS) {
-		uint8_t *dst = msg->payload + sizeof(completion_code);
-		length = htole32(length);
-		memcpy(dst, &length, sizeof(length));
+	struct pldm_read_write_file_memory_resp *response =
+	    (struct pldm_read_write_file_memory_resp *)msg->payload;
+	response->completion_code = completion_code;
+	if (response->completion_code == PLDM_SUCCESS) {
+		response->length = htole32(length);
+	}
+
+	return PLDM_SUCCESS;
+}
+
+int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
+			      uint32_t file_handle, uint32_t offset,
+			      uint32_t length, uint64_t address,
+			      struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_OEM;
+	header.command = command;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_read_write_file_memory_req *req =
+	    (struct pldm_read_write_file_memory_req *)msg->payload;
+	req->file_handle = htole32(file_handle);
+	req->offset = htole32(offset);
+	req->length = htole32(length);
+	req->address = htole64(address);
+	return PLDM_SUCCESS;
+}
+
+int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length,
+			       uint8_t *completion_code, uint32_t *length)
+{
+	if (msg == NULL || length == NULL || completion_code == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_read_write_file_memory_resp *response =
+	    (struct pldm_read_write_file_memory_resp *)msg;
+	*completion_code = response->completion_code;
+	if (*completion_code == PLDM_SUCCESS) {
+		*length = le32toh(response->length);
 	}
 
 	return PLDM_SUCCESS;
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
index eb23332..64f203c 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -41,6 +41,28 @@
 #define PLDM_GET_FILE_TABLE_REQ_BYTES 6
 #define PLDM_GET_FILE_TABLE_MIN_RESP_BYTES 6
 
+/** @struct pldm_read_write_file_memory_req
+ *
+ *  Structure representing ReadFileIntoMemory request and WriteFileFromMemory
+ *  request
+ */
+struct pldm_read_write_file_memory_req {
+	uint32_t file_handle; //!< A Handle to the file
+	uint32_t offset;      //!< Offset to the file
+	uint32_t length;      //!< Number of bytes to be read/write
+	uint64_t address;     //!< Memory address of the file
+} __attribute__((packed));
+
+/** @struct pldm_read_write_file_memory_resp
+ *
+ *  Structure representing ReadFileIntoMemory response and WriteFileFromMemory
+ *  response
+ */
+struct pldm_read_write_file_memory_resp {
+	uint8_t completion_code; //!< completion code
+	uint32_t length;	 //!< Number of bytes read/written
+} __attribute__((packed));
+
 /** @brief Decode ReadFileIntoMemory and WriteFileFromMemory commands request
  *         data
  *
@@ -73,6 +95,36 @@
 			       uint8_t completion_code, uint32_t length,
 			       struct pldm_msg *msg);
 
+/** @brief Encode ReadFileIntoMemory and WriteFileFromMemory
+ *         commands request data
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] command - PLDM command
+ *  @param[in] file_handle - A handle to the file
+ *  @param[in] offset -  Offset to the file at which the read should begin
+ *  @param[in] length -  Number of bytes to be read/written
+ *  @param[in] address - Memory address where the file content has to be
+ *                       written to
+ *  @param[out] msg - Message will be written to this
+ *  @return pldm_completion_codes
+ */
+int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
+			      uint32_t file_handle, uint32_t offset,
+			      uint32_t length, uint64_t address,
+			      struct pldm_msg *msg);
+
+/** @brief Decode ReadFileIntoMemory and WriteFileFromMemory
+ *         commands response data
+ *
+ *  @param[in] msg - pointer to PLDM response message payload
+ *  @param[in] payload_length - Length of response payload
+ *  @param[out] completion_code - PLDM completion code
+ *  @param[out] length - Number of bytes to be read/written
+ *  @return pldm_completion_codes
+ */
+int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length,
+			       uint8_t *completion_code, uint32_t *length);
+
 /** @struct pldm_get_file_table_req
  *
  *  Structure representing GetFileTable request