Enable IBM PLDM OEM commands support

Create folder structure (oem/ibm) for ibm pldm oem commands. Move the
files from the ibm-pldm-oem repo [https://github.com/openbmc/ibm-pldm-oem]
to the folder oem/ibm/ under the pldm repo and enable conditional
compilation for it. The test files are also conditionally compiled.

You would need to provide --enable-oem-ibm to configure.

This is done to simplify the build time and runtime dependencies between the
standard and oem implementations.

Signed-off-by: Jinu Joy Thomas <jinu.joy.thomas@in.ibm.com>
Change-Id: Iaa93c73faff87290e3d3d5d155d9ecae6e7ee6f9
diff --git a/oem/ibm/libpldm/.clang-format b/oem/ibm/libpldm/.clang-format
new file mode 100644
index 0000000..a3ef233
--- /dev/null
+++ b/oem/ibm/libpldm/.clang-format
@@ -0,0 +1,6 @@
+BasedOnStyle: LLVM
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Linux
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
new file mode 100644
index 0000000..ca33cef
--- /dev/null
+++ b/oem/ibm/libpldm/file_io.c
@@ -0,0 +1,54 @@
+#include "file_io.h"
+#include <endian.h>
+#include <string.h>
+
+int decode_rw_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_RW_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_rw_file_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;
+
+	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 = command;
+
+	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;
+}
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
new file mode 100644
index 0000000..345b363
--- /dev/null
+++ b/oem/ibm/libpldm/file_io.h
@@ -0,0 +1,70 @@
+#ifndef FILEIO_H
+#define FILEIO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base.h"
+
+#define PLDM_IBM_OEM_TYPE 0x3F
+
+/** @brief PLDM Commands in IBM OEM type
+ */
+enum pldm_fileio_commands {
+	PLDM_READ_FILE_INTO_MEMORY = 0x6,
+	PLDM_WRITE_FILE_FROM_MEMORY = 0x7,
+};
+
+/** @brief PLDM Command specific codes
+ */
+enum pldm_fileio_completion_codes {
+	PLDM_INVALID_FILE_HANDLE = 0x80,
+	PLDM_DATA_OUT_OF_RANGE = 0x81,
+	PLDM_INVALID_READ_LENGTH = 0x82,
+	PLDM_INVALID_WRITE_LENGTH = 0x83,
+};
+
+#define PLDM_RW_FILE_MEM_REQ_BYTES 20
+#define PLDM_RW_FILE_MEM_RESP_BYTES 5
+
+/** @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
+ *  @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 where the file content has to be
+ *                        written to
+ *  @return pldm_completion_codes
+ */
+int decode_rw_file_memory_req(const uint8_t *msg, size_t payload_length,
+			      uint32_t *file_handle, uint32_t *offset,
+			      uint32_t *length, uint64_t *address);
+
+/** @brief Create a PLDM response for ReadFileIntoMemory and
+ *         WriteFileFromMemory
+ *
+ *  @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_memory_resp(uint8_t instance_id, uint8_t command,
+			       uint8_t completion_code, uint32_t length,
+			       struct pldm_msg *msg);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FILEIO_H */
diff --git a/oem/ibm/libpldmresponder/file_io.cpp b/oem/ibm/libpldmresponder/file_io.cpp
new file mode 100644
index 0000000..9cf093c
--- /dev/null
+++ b/oem/ibm/libpldmresponder/file_io.cpp
@@ -0,0 +1,243 @@
+#include "file_io.hpp"
+
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <cstring>
+#include <fstream>
+#include <phosphor-logging/log.hpp>
+
+#include "libpldm/base.h"
+
+namespace pldm
+{
+
+namespace responder
+{
+
+namespace fs = std::filesystem;
+using namespace phosphor::logging;
+
+namespace dma
+{
+
+/** @struct AspeedXdmaOp
+ *
+ * Structure representing XDMA operation
+ */
+struct AspeedXdmaOp
+{
+    uint64_t hostAddr; //!< the DMA address on the host side, configured by
+                       //!< PCI subsystem.
+    uint32_t len;      //!< the size of the transfer in bytes, it should be a
+                       //!< multiple of 16 bytes
+    uint32_t upstream; //!< boolean indicating the direction of the DMA
+                       //!< operation, true means a transfer from BMC to host.
+};
+
+constexpr auto xdmaDev = "/dev/xdma";
+
+int DMA::transferDataHost(const fs::path& path, uint32_t offset,
+                          uint32_t length, uint64_t address, bool upstream)
+{
+    static const size_t pageSize = getpagesize();
+    uint32_t numPages = length / pageSize;
+    uint32_t pageAlignedLength = numPages * pageSize;
+
+    if (length > pageAlignedLength)
+    {
+        pageAlignedLength += pageSize;
+    }
+
+    auto mmapCleanup = [pageAlignedLength](void* vgaMem) {
+        munmap(vgaMem, pageAlignedLength);
+    };
+
+    int fd = -1;
+    int rc = 0;
+    fd = open(xdmaDev, O_RDWR);
+    if (fd < 0)
+    {
+        rc = -errno;
+        log<level::ERR>("Failed to open the XDMA device", entry("RC=%d", rc));
+        return rc;
+    }
+
+    utils::CustomFD xdmaFd(fd);
+
+    void* vgaMem;
+    vgaMem = mmap(nullptr, pageAlignedLength, upstream ? PROT_WRITE : PROT_READ,
+                  MAP_SHARED, xdmaFd(), 0);
+    if (MAP_FAILED == vgaMem)
+    {
+        rc = -errno;
+        log<level::ERR>("Failed to mmap the XDMA device", entry("RC=%d", rc));
+        return rc;
+    }
+
+    std::unique_ptr<void, decltype(mmapCleanup)> vgaMemPtr(vgaMem, mmapCleanup);
+
+    if (upstream)
+    {
+        std::ifstream stream(path.string());
+
+        stream.seekg(offset);
+        stream.read(static_cast<char*>(vgaMemPtr.get()), length);
+
+        if (static_cast<uint32_t>(stream.gcount()) != length)
+        {
+            log<level::ERR>("mismatch between number of characters to read and "
+                            "the length read",
+                            entry("LENGTH=%d", length),
+                            entry("COUNT=%d", stream.gcount()));
+            return -1;
+        }
+    }
+
+    AspeedXdmaOp xdmaOp;
+    xdmaOp.upstream = upstream ? 1 : 0;
+    xdmaOp.hostAddr = address;
+    xdmaOp.len = length;
+
+    rc = write(xdmaFd(), &xdmaOp, sizeof(xdmaOp));
+    if (rc < 0)
+    {
+        rc = -errno;
+        log<level::ERR>("Failed to execute the DMA operation",
+                        entry("RC=%d", rc), entry("UPSTREAM=%d", upstream),
+                        entry("ADDRESS=%lld", address),
+                        entry("LENGTH=%d", length));
+        return rc;
+    }
+
+    if (!upstream)
+    {
+        std::ofstream stream(path.string());
+
+        stream.seekp(offset);
+        stream.write(static_cast<const char*>(vgaMemPtr.get()), length);
+    }
+
+    return 0;
+}
+
+} // namespace dma
+
+Response readFileIntoMemory(const uint8_t* request, size_t payloadLength)
+{
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 0;
+    uint64_t address = 0;
+    fs::path path("");
+
+    Response response((sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES), 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+
+    if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES)
+    {
+        encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
+                                   PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
+        return response;
+    }
+
+    decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset,
+                              &length, &address);
+
+    if (!fs::exists(path))
+    {
+        log<level::ERR>("File does not exist", entry("HANDLE=%d", fileHandle));
+        encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
+                                   PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
+        return response;
+    }
+
+    auto fileSize = fs::file_size(path);
+    if (offset >= fileSize)
+    {
+        log<level::ERR>("Offset exceeds file size", entry("OFFSET=%d", offset),
+                        entry("FILE_SIZE=%d", fileSize));
+        encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
+                                   PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
+        return response;
+    }
+
+    if (offset + length > fileSize)
+    {
+        length = fileSize - offset;
+    }
+
+    if (length % dma::minSize)
+    {
+        log<level::ERR>("Read length is not a multiple of DMA minSize",
+                        entry("LENGTH=%d", length));
+        encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
+                                   PLDM_INVALID_READ_LENGTH, 0, responsePtr);
+        return response;
+    }
+
+    using namespace dma;
+    DMA intf;
+    return transferAll<DMA>(&intf, PLDM_READ_FILE_INTO_MEMORY, path, offset,
+                            length, address, true);
+}
+
+Response writeFileFromMemory(const uint8_t* request, size_t payloadLength)
+{
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 0;
+    uint64_t address = 0;
+    fs::path path("");
+
+    Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+
+    if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES)
+    {
+        encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
+                                   PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
+        return response;
+    }
+
+    decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset,
+                              &length, &address);
+
+    if (length % dma::minSize)
+    {
+        log<level::ERR>("Write length is not a multiple of DMA minSize",
+                        entry("LENGTH=%d", length));
+        encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
+                                   PLDM_INVALID_WRITE_LENGTH, 0, responsePtr);
+        return response;
+    }
+
+    if (!fs::exists(path))
+    {
+        log<level::ERR>("File does not exist", entry("HANDLE=%d", fileHandle));
+        encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
+                                   PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
+        return response;
+    }
+
+    auto fileSize = fs::file_size(path);
+    if (offset >= fileSize)
+    {
+        log<level::ERR>("Offset exceeds file size", entry("OFFSET=%d", offset),
+                        entry("FILE_SIZE=%d", fileSize));
+        encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
+                                   PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
+        return response;
+    }
+
+    using namespace dma;
+    DMA intf;
+    return transferAll<DMA>(&intf, PLDM_WRITE_FILE_FROM_MEMORY, path, offset,
+                            length, address, false);
+}
+
+} // namespace responder
+} // namespace pldm
diff --git a/oem/ibm/libpldmresponder/file_io.hpp b/oem/ibm/libpldmresponder/file_io.hpp
new file mode 100644
index 0000000..17a6fe2
--- /dev/null
+++ b/oem/ibm/libpldmresponder/file_io.hpp
@@ -0,0 +1,168 @@
+#pragma once
+
+#include <stdint.h>
+#include <unistd.h>
+
+#include <filesystem>
+
+#include "libpldm/base.h"
+#include "libpldm/file_io.h"
+
+namespace pldm
+{
+
+namespace responder
+{
+
+using Response = std::vector<uint8_t>;
+
+namespace utils
+{
+
+/** @struct CustomFD
+ *
+ *  RAII wrapper for file descriptor.
+ */
+struct CustomFD
+{
+    CustomFD(const CustomFD&) = delete;
+    CustomFD& operator=(const CustomFD&) = delete;
+    CustomFD(CustomFD&&) = delete;
+    CustomFD& operator=(CustomFD&&) = delete;
+
+    CustomFD(int fd) : fd(fd)
+    {
+    }
+
+    ~CustomFD()
+    {
+        if (fd >= 0)
+        {
+            close(fd);
+        }
+    }
+
+    int operator()() const
+    {
+        return fd;
+    }
+
+  private:
+    int fd = -1;
+};
+
+} // namespace utils
+
+namespace dma
+{
+
+// The minimum data size of dma transfer in bytes
+constexpr uint32_t minSize = 16;
+
+// 16MB - 4096B (16773120 bytes) is the maximum data size of DMA transfer
+constexpr size_t maxSize = (16 * 1024 * 1024) - 4096;
+
+namespace fs = std::filesystem;
+
+/**
+ * @class DMA
+ *
+ * Expose API to initiate transfer of data by DMA
+ *
+ * This class only exposes the public API transferDataHost to transfer data
+ * between BMC and host using DMA. This allows for mocking the transferDataHost
+ * for unit testing purposes.
+ */
+class DMA
+{
+  public:
+    /** @brief API to transfer data between BMC and host using DMA
+     *
+     * @param[in] path     - pathname of the file to transfer data from or to
+     * @param[in] offset   - offset in the file
+     * @param[in] length   - length of the data to transfer
+     * @param[in] address  - DMA address on the host
+     * @param[in] upstream - indicates direction of the transfer; true indicates
+     *                       transfer to the host
+     *
+     * @return returns 0 on success, negative errno on failure
+     */
+    int transferDataHost(const fs::path& path, uint32_t offset, uint32_t length,
+                         uint64_t address, bool upstream);
+};
+
+/** @brief Transfer the data between BMC and host using DMA.
+ *
+ *  There is a max size for each DMA operation, transferAll API abstracts this
+ *  and the requested length is broken down into multiple DMA operations if the
+ *  length exceed max size.
+ *
+ * @tparam[in] T - DMA interface type
+ * @param[in] intf - interface passed to invoke DMA transfer
+ * @param[in] command  - PLDM command
+ * @param[in] path     - pathname of the file to transfer data from or to
+ * @param[in] offset   - offset in the file
+ * @param[in] length   - length of the data to transfer
+ * @param[in] address  - DMA address on the host
+ * @param[in] upstream - indicates direction of the transfer; true indicates
+ *                       transfer to the host
+ * @return PLDM response message
+ */
+
+template <class DMAInterface>
+Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
+                     uint32_t offset, uint32_t length, uint64_t address,
+                     bool upstream)
+{
+    uint32_t origLength = length;
+    Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+
+    while (length > dma::maxSize)
+    {
+        auto rc = intf->transferDataHost(path, offset, dma::maxSize, address,
+                                         upstream);
+        if (rc < 0)
+        {
+            encode_rw_file_memory_resp(0, command, PLDM_ERROR, 0, responsePtr);
+            return response;
+        }
+
+        offset += dma::maxSize;
+        length -= dma::maxSize;
+        address += dma::maxSize;
+    }
+
+    auto rc = intf->transferDataHost(path, offset, length, address, upstream);
+    if (rc < 0)
+    {
+        encode_rw_file_memory_resp(0, command, PLDM_ERROR, 0, responsePtr);
+        return response;
+    }
+
+    encode_rw_file_memory_resp(0, command, PLDM_SUCCESS, origLength,
+                               responsePtr);
+    return response;
+}
+
+} // namespace dma
+
+/** @brief Handler for readFileIntoMemory command
+ *
+ *  @param[in] request - pointer to PLDM request payload
+ *  @param[in] payloadLength - length of the message payload
+ *
+ *  @return PLDM response message
+ */
+Response readFileIntoMemory(const uint8_t* request, size_t payloadLength);
+
+/** @brief Handler for writeFileIntoMemory command
+ *
+ *  @param[in] request - pointer to PLDM request payload
+ *  @param[in] payloadLength - length of the message payload
+ *
+ *  @return PLDM response message
+ */
+Response writeFileFromMemory(const uint8_t* request, size_t payloadLength);
+} // namespace responder
+} // namespace pldm
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
new file mode 100644
index 0000000..2a5dce1
--- /dev/null
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -0,0 +1,132 @@
+#include <string.h>
+
+#include <array>
+
+#include "libpldm/base.h"
+#include "libpldm/file_io.h"
+
+#include <gtest/gtest.h>
+
+TEST(ReadWriteFileMemory, testGoodDecodeRequest)
+{
+    std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
+
+    // Random value for fileHandle, offset, length, address
+    uint32_t fileHandle = 0x12345678;
+    uint32_t offset = 0x87654321;
+    uint32_t length = 0x13245768;
+    uint64_t address = 0x124356879ACBDE0F;
+
+    memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle));
+    memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length,
+           sizeof(length));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
+               sizeof(length),
+           &address, sizeof(address));
+
+    uint32_t retFileHandle = 0;
+    uint32_t retOffset = 0;
+    uint32_t retLength = 0;
+    uint64_t retAddress = 0;
+
+    // Invoke decode the read file memory request
+    auto rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(),
+                                        &retFileHandle, &retOffset, &retLength,
+                                        &retAddress);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(fileHandle, retFileHandle);
+    ASSERT_EQ(offset, retOffset);
+    ASSERT_EQ(length, retLength);
+    ASSERT_EQ(address, retAddress);
+}
+
+TEST(ReadWriteFileMemory, testBadDecodeRequest)
+{
+    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_memory_req(NULL, 0, &fileHandle, &offset, &length,
+                                        &address);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
+
+    // Address is NULL
+    rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(),
+                                   &fileHandle, &offset, &length, NULL);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    // Payload length is invalid
+    rc = decode_rw_file_memory_req(requestMsg.data(), 0, &fileHandle, &offset,
+                                   &length, &address);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileMemory, testGoodEncodeResponse)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
+        responseMsg{};
+    uint32_t length = 0xFF00EE11;
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // ReadFileIntoMemory
+    auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
+                                         PLDM_SUCCESS, 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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
+    ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
+                        &length, sizeof(length)));
+
+    // WriteFileFromMemory
+    rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
+                                    PLDM_SUCCESS, 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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
+    ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
+                        &length, sizeof(length)));
+}
+
+TEST(ReadWriteFileMemory, testBadEncodeResponse)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
+        responseMsg{};
+    uint32_t length = 0;
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // ReadFileIntoMemory
+    auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
+    ASSERT_EQ(response->payload[0], PLDM_ERROR);
+
+    // WriteFileFromMemory
+    rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
+    ASSERT_EQ(response->payload[0], PLDM_ERROR);
+}
diff --git a/oem/ibm/test/libpldmresponder_fileio_test.cpp b/oem/ibm/test/libpldmresponder_fileio_test.cpp
new file mode 100644
index 0000000..060b0ed
--- /dev/null
+++ b/oem/ibm/test/libpldmresponder_fileio_test.cpp
@@ -0,0 +1,189 @@
+#include "libpldmresponder/file_io.hpp"
+
+#include "libpldm/base.h"
+#include "libpldm/file_io.h"
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#define SD_JOURNAL_SUPPRESS_LOCATION
+
+#include <systemd/sd-journal.h>
+
+std::vector<std::string> logs;
+
+extern "C" {
+
+int sd_journal_send(const char* format, ...)
+{
+    logs.push_back(format);
+    return 0;
+}
+
+int sd_journal_send_with_location(const char* file, const char* line,
+                                  const char* func, const char* format, ...)
+{
+    logs.push_back(format);
+    return 0;
+}
+}
+
+namespace pldm
+{
+
+namespace responder
+{
+
+namespace dma
+{
+
+class MockDMA
+{
+  public:
+    MOCK_METHOD5(transferDataHost,
+                 int(const fs::path& file, uint32_t offset, uint32_t length,
+                     uint64_t address, bool upstream));
+};
+
+} // namespace dma
+} // namespace responder
+} // namespace pldm
+using namespace pldm::responder;
+using ::testing::_;
+using ::testing::Return;
+
+TEST(TransferDataHost, GoodPath)
+{
+    using namespace pldm::responder::dma;
+
+    MockDMA dmaObj;
+    fs::path path("");
+
+    // Minimum length of 16 and expect transferDataHost to be called once
+    // returns the default value of 0 (the return type of transferDataHost is
+    // int, the default value for int is 0)
+    uint32_t length = minSize;
+    EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, true)).Times(1);
+    auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY,
+                                         path, 0, length, 0, true);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
+                        &length, sizeof(length)));
+
+    // maxsize of DMA
+    length = maxSize;
+    EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, true)).Times(1);
+    response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path,
+                                    0, length, 0, true);
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
+                        &length, sizeof(length)));
+
+    // length greater than maxsize of DMA
+    length = maxSize + minSize;
+    EXPECT_CALL(dmaObj, transferDataHost(path, 0, maxSize, 0, true)).Times(1);
+    EXPECT_CALL(dmaObj, transferDataHost(path, maxSize, minSize, maxSize, true))
+        .Times(1);
+    response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path,
+                                    0, length, 0, true);
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
+                        &length, sizeof(length)));
+
+    // length greater than 2*maxsize of DMA
+    length = 3 * maxSize;
+    EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, true)).Times(3);
+    response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path,
+                                    0, length, 0, true);
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
+                        &length, sizeof(length)));
+
+    // check for downstream(copy data from host to BMC) parameter
+    length = minSize;
+    EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, false)).Times(1);
+    response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path,
+                                    0, length, 0, false);
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
+                        &length, sizeof(length)));
+}
+
+TEST(TransferDataHost, BadPath)
+{
+    using namespace pldm::responder::dma;
+
+    MockDMA dmaObj;
+    fs::path path("");
+
+    // Minimum length of 16 and transferDataHost returning a negative errno
+    uint32_t length = minSize;
+    EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1));
+    auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY,
+                                         path, 0, length, 0, true);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
+
+    // length greater than maxsize of DMA and transferDataHost returning a
+    // negative errno
+    length = maxSize + minSize;
+    EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1));
+    response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path,
+                                    0, length, 0, true);
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
+}
+
+TEST(ReadFileIntoMemory, BadPath)
+{
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 10;
+    uint64_t address = 0;
+
+    std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
+    memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle));
+    memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length,
+           sizeof(length));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
+               sizeof(length),
+           &address, sizeof(address));
+
+    // Pass invalid payload length
+    auto response = readFileIntoMemory(requestMsg.data(), 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(WriteFileFromMemory, BadPath)
+{
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 10;
+    uint64_t address = 0;
+
+    std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
+    memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle));
+    memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length,
+           sizeof(length));
+    memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
+               sizeof(length),
+           &address, sizeof(address));
+
+    // Pass invalid payload length
+    auto response = writeFileFromMemory(requestMsg.data(), 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
+
+    // The length field is not a multiple of DMA minsize
+    response = writeFileFromMemory(requestMsg.data(), requestMsg.size());
+    responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_WRITE_LENGTH);
+}