OEM IBM: Add file IO requester encode/decode APIs

Implement encode request and decode response and also
reduce the pointer and memcopy operations for ReadFileIntoMemory
and WriteFileIntoMemory requester and responser by
creating structures.

Signed-off-by: Priyanga <priyram1@in.ibm.com>
Change-Id: I8dc1d4684ae38b65b7ce8bb5b0f41ee31f36b2a5
diff --git a/libpldm/file_io.c b/libpldm/file_io.c
index f32892c..f5301f5 100644
--- a/libpldm/file_io.c
+++ b/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_IBM_OEM_TYPE;
@@ -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_IBM_OEM_TYPE;
+	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/libpldm/file_io.h b/libpldm/file_io.h
index 6354f81..e138275 100644
--- a/libpldm/file_io.h
+++ b/libpldm/file_io.h
@@ -43,8 +43,30 @@
 #define PLDM_GET_FILE_TABLE_REQ_BYTES 6
 #define PLDM_GET_FILE_TABLE_MIN_RESP_BYTES 6
 
-/** @brief Decode ReadFileIntoMemory and WriteFileFromMemory commands request
- *         data
+/** @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
  *
  *  @param[in] msg - Pointer to PLDM request message payload
  *  @param[in] payload_length - Length of request payload
@@ -65,9 +87,9 @@
  *  @param[in] instance_id - Message's instance id
  *  @param[in] command - PLDM command
  *  @param[in] completion_code - PLDM completion code
- *  @param[in] length - Number of bytes read. This could be less than what the
-			 requester asked for.
- *  @param[in,out] msg - Message will be written to this
+ *  @param[in] length - Number of bytes read. This could be less than
+ *                      what the  requester asked for.
+ *  @param[out] msg - Message will be written to this
  *  @return pldm_completion_codes
  *  @note  Caller is responsible for memory alloc and dealloc of param 'msg'
  */
@@ -75,6 +97,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