oem-ibm: libpldm: add ReadFileByTypeIntoMemory and WriteFileByTypeFromMemory

Implement Encode/Decode APIs for read/write file by type PLDM messages. Example
for a file type is a Platform Event Log file.

Change-Id: I822034c5f59943a78129136089822e552b2461bb
Signed-off-by: vkaverap <vkaverap@in.ibm.com>
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index dc8ad6b..9610967 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -381,3 +381,114 @@
 
 	return PLDM_SUCCESS;
 }
+
+int decode_rw_file_by_type_memory_req(const struct pldm_msg *msg,
+				      size_t payload_length,
+				      uint16_t *file_type,
+				      uint32_t *file_handle, uint32_t *offset,
+				      uint32_t *length, uint64_t *address)
+{
+	if (msg == NULL || file_type == NULL || file_handle == NULL ||
+	    offset == NULL || length == NULL || address == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_read_write_file_by_type_memory_req *request =
+	    (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
+	*file_type = le16toh(request->file_type);
+	*file_handle = le32toh(request->file_handle);
+	*offset = le32toh(request->offset);
+	*length = le32toh(request->length);
+	*address = le64toh(request->address);
+
+	return PLDM_SUCCESS;
+}
+
+int encode_rw_file_by_type_memory_resp(uint8_t instance_id, uint8_t command,
+				       uint8_t completion_code, uint32_t length,
+				       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_RESPONSE;
+	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_by_type_memory_resp *response =
+	    (struct pldm_read_write_file_by_type_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_by_type_memory_req(uint8_t instance_id, uint8_t command,
+				      uint16_t file_type, 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_by_type_memory_req *req =
+	    (struct pldm_read_write_file_by_type_memory_req *)msg->payload;
+	req->file_type = htole16(file_type);
+	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_by_type_memory_resp(const struct pldm_msg *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_BY_TYPE_MEM_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_read_write_file_by_type_memory_resp *response =
+	    (struct pldm_read_write_file_by_type_memory_resp *)msg->payload;
+	*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 437897c..15b568b 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -18,6 +18,8 @@
 	PLDM_WRITE_FILE = 0x5,
 	PLDM_READ_FILE_INTO_MEMORY = 0x6,
 	PLDM_WRITE_FILE_FROM_MEMORY = 0x7,
+	PLDM_READ_FILE_BY_TYPE_INTO_MEMORY = 0x8,
+	PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY = 0x9,
 };
 
 /** @brief PLDM Command specific codes
@@ -46,6 +48,8 @@
 #define PLDM_READ_FILE_RESP_BYTES 5
 #define PLDM_WRITE_FILE_REQ_BYTES 12
 #define PLDM_WRITE_FILE_RESP_BYTES 5
+#define PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES 22
+#define PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES 5
 
 /** @struct pldm_read_write_file_memory_req
  *
@@ -341,6 +345,96 @@
 int encode_write_file_resp(uint8_t instance_id, uint8_t completion_code,
 			   uint32_t length, struct pldm_msg *msg);
 
+/** @struct pldm_read_write_file_by_type_memory_req
+ *
+ *  Structure representing ReadFileByTypeIntoMemory and
+ * WriteFileByTypeFromMemory request
+ */
+struct pldm_read_write_file_by_type_memory_req {
+	uint16_t file_type;   //!< Type of file
+	uint32_t file_handle; //!< Handle to file
+	uint32_t offset;      //!< Offset to file where read starts
+	uint32_t length;      //!< Bytes to be read
+	uint64_t address;     //!< Memory address of the file
+} __attribute__((packed));
+
+/** @struct pldm_read_write_file_by_type_memory_resp
+ *
+ *  Structure representing ReadFileByTypeIntoMemory and
+ * WriteFileByTypeFromMemory response
+ */
+struct pldm_read_write_file_by_type_memory_resp {
+	uint8_t completion_code; //!< Completion code
+	uint32_t length;	 //!< Number of bytes read
+} __attribute__((packed));
+
+/** @brief Decode ReadFileByTypeIntoMemory and WriteFileByTypeFromMemory
+ * commands request data
+ *
+ *  @param[in] msg - Pointer to PLDM request message
+ *  @param[in] payload_length - Length of request payload
+ *  @param[in] file_type - Type of the file
+ *  @param[out] file_handle - A handle to the file
+ *  @param[out] offset - Offset to the file at which the read should begin
+ *  @param[out] length - Number of bytes to be read
+ *  @param[out] address - Memory address of the file content
+ *  @return pldm_completion_codes
+ */
+int decode_rw_file_by_type_memory_req(const struct pldm_msg *msg,
+				      size_t payload_length,
+				      uint16_t *file_type,
+				      uint32_t *file_handle, uint32_t *offset,
+				      uint32_t *length, uint64_t *address);
+
+/** @brief Create a PLDM response for ReadFileByTypeIntoMemory and
+ * WriteFileByTypeFromMemory
+ *
+ *  @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
+ *  @return pldm_completion_codes
+ *  @note  Caller is responsible for memory alloc and dealloc of param 'msg'
+ */
+int encode_rw_file_by_type_memory_resp(uint8_t instance_id, uint8_t command,
+				       uint8_t completion_code, uint32_t length,
+				       struct pldm_msg *msg);
+
+/** @brief Encode ReadFileByTypeIntoMemory and WriteFileByTypeFromMemory
+ *         commands request data
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] command - PLDM command
+ *  @param[in] file_type - Type of the file
+ *  @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_by_type_memory_req(uint8_t instance_id, uint8_t command,
+				      uint16_t file_type, uint32_t file_handle,
+				      uint32_t offset, uint32_t length,
+				      uint64_t address, struct pldm_msg *msg);
+
+/** @brief Decode ReadFileTypeIntoMemory and WriteFileTypeFromMemory
+ *         commands response data
+ *
+ *  @param[in] msg - pointer to PLDM response message
+ *  @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_by_type_memory_resp(const struct pldm_msg *msg,
+				       size_t payload_length,
+				       uint8_t *completion_code,
+				       uint32_t *length);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
index 665ceb1..979f0b7 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -703,3 +703,243 @@
     ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE);
     ASSERT_EQ(responseWr->payload[0], PLDM_ERROR);
 }
+
+TEST(ReadWriteFileByTypeMemory, testGoodDecodeRequest)
+{
+    std::array<uint8_t,
+               PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
+        requestMsg{};
+
+    auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
+    auto request = reinterpret_cast<pldm_read_write_file_by_type_memory_req*>(
+        requestPtr->payload);
+
+    // Random value for fileHandle, offset and length
+    uint16_t fileType = 0;
+    uint32_t fileHandle = 0x12345678;
+    uint32_t offset = 0x87654321;
+    uint32_t length = 0x13245768;
+    uint64_t address = 0x124356879ACBD456;
+
+    request->file_type = fileType;
+    request->file_handle = fileHandle;
+    request->offset = offset;
+    request->length = length;
+    request->address = address;
+
+    uint16_t retFileType = 0x1;
+    uint32_t retFileHandle = 0;
+    uint32_t retOffset = 0;
+    uint32_t retLength = 0;
+    uint64_t retAddress = 0;
+
+    // Invoke decode the read file request
+    auto rc = decode_rw_file_by_type_memory_req(
+        requestPtr, payload_length, &retFileType, &retFileHandle, &retOffset,
+        &retLength, &retAddress);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(fileType, retFileType);
+    ASSERT_EQ(fileHandle, retFileHandle);
+    ASSERT_EQ(offset, retOffset);
+    ASSERT_EQ(length, retLength);
+    ASSERT_EQ(address, retAddress);
+}
+
+TEST(ReadWriteFileByTypeMemory, testGoodDecodeResponse)
+{
+    std::array<uint8_t,
+               PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
+        responseMsg{};
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
+    auto response = reinterpret_cast<pldm_read_write_file_by_type_memory_resp*>(
+        responsePtr->payload);
+
+    // Random value for completion code and length
+    uint8_t completionCode = 0x0;
+    uint32_t length = 0x13245768;
+
+    response->completion_code = completionCode;
+    response->length = length;
+
+    uint8_t retCompletionCode = 0x1;
+    uint32_t retLength = 0;
+
+    // Invoke decode the read/write file response
+    auto rc = decode_rw_file_by_type_memory_resp(
+        responsePtr, payload_length, &retCompletionCode, &retLength);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+    ASSERT_EQ(length, retLength);
+}
+
+TEST(ReadWriteFileByTypeMemory, testBadDecodeRequest)
+{
+    uint16_t fileType = 0;
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 0;
+    uint64_t address = 0;
+
+    // Request payload message is missing
+    auto rc = decode_rw_file_by_type_memory_req(NULL, 0, &fileType, &fileHandle,
+                                                &offset, &length, &address);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t,
+               PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
+        requestMsg{};
+
+    auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    // Address is NULL
+    rc = decode_rw_file_by_type_memory_req(
+        requestPtr, requestMsg.size() - hdrSize, &fileType, &fileHandle,
+        &offset, &length, NULL);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    // Payload length is invalid
+    rc = decode_rw_file_by_type_memory_req(
+        requestPtr, 0, &fileType, &fileHandle, &offset, &length, &address);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileByTypeMemory, testBadDecodeResponse)
+{
+    uint32_t length = 0;
+    uint8_t completionCode = 0;
+
+    // Request payload message is missing
+    auto rc =
+        decode_rw_file_by_type_memory_resp(NULL, 0, &completionCode, &length);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t,
+               PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
+        responseMsg{};
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // Length is NULL
+    rc = decode_rw_file_by_type_memory_resp(
+        responsePtr, responseMsg.size() - hdrSize, &completionCode, NULL);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    // Payload length is invalid
+    rc = decode_rw_file_by_type_memory_resp(responsePtr, 0, &completionCode,
+                                            &length);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileByTypeMemory, testGoodEncodeRequest)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES>
+        requestMsg{};
+
+    uint16_t fileType = 0;
+    uint32_t fileHandle = 0x12345678;
+    uint32_t offset = 0x87654321;
+    uint32_t length = 0x13245768;
+    uint64_t address = 0x124356879ACBDE0F;
+
+    pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_rw_file_by_type_memory_req(
+        0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
+        length, address, request);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
+    ASSERT_EQ(request->hdr.instance_id, 0);
+    ASSERT_EQ(request->hdr.type, PLDM_OEM);
+    ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
+
+    ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
+                        sizeof(fileHandle)));
+
+    ASSERT_EQ(0,
+              memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
+                     &offset, sizeof(offset)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType) +
+                            sizeof(fileHandle) + sizeof(offset),
+                        &length, sizeof(length)));
+    ASSERT_EQ(0,
+              memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle) +
+                         sizeof(offset) + sizeof(length),
+                     &address, sizeof(address)));
+}
+
+TEST(ReadWriteFileByTypeMemory, testGoodEncodeResponse)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
+        responseMsg{};
+
+    uint32_t length = 0x13245768;
+    uint8_t completionCode = 0x0;
+
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = encode_rw_file_by_type_memory_resp(
+        0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, completionCode, length,
+        response);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+    ASSERT_EQ(response->hdr.instance_id, 0);
+    ASSERT_EQ(response->hdr.type, PLDM_OEM);
+    ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
+
+    ASSERT_EQ(
+        0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &length,
+                        sizeof(length)));
+}
+
+TEST(ReadWriteFileByTypeMemory, testBadEncodeResponse)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
+        responseMsg{};
+    uint32_t length = 0;
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // completion code is PLDM_ERROR
+    auto rc = encode_rw_file_by_type_memory_resp(
+        0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_ERROR, length, response);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+    ASSERT_EQ(response->hdr.instance_id, 0);
+    ASSERT_EQ(response->hdr.type, PLDM_OEM);
+    ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
+    ASSERT_EQ(response->payload[0], PLDM_ERROR);
+
+    // response is NULL pointer
+    rc = encode_rw_file_by_type_memory_resp(
+        0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_SUCCESS, length, NULL);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(ReadWriteFileByTypeMemory, testBadEncodeRequest)
+{
+    uint8_t fileType = 0;
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 0;
+    uint64_t address = 0;
+
+    // request is NULL pointer
+    auto rc = encode_rw_file_by_type_memory_req(
+        0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
+        length, address, NULL);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}