ibm-oem: add handler to read PELs

Extend ReadFileByType* commands to let the host firmware read PELs off
of the BMC.

Signed-off-by: vkaverap <vkaverap@in.ibm.com>
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
Change-Id: I3721e1ffe61a66d0f011535504a6915e0c16764b
diff --git a/oem/ibm/libpldmresponder/file_io.hpp b/oem/ibm/libpldmresponder/file_io.hpp
index 231222c..5c357d5 100644
--- a/oem/ibm/libpldmresponder/file_io.hpp
+++ b/oem/ibm/libpldmresponder/file_io.hpp
@@ -1,11 +1,16 @@
 #pragma once
 
 #include "handler.hpp"
+#include "libpldmresponder/utils.hpp"
 
+#include <fcntl.h>
 #include <stdint.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include <filesystem>
+#include <iostream>
 #include <vector>
 
 #include "libpldm/base.h"
@@ -49,7 +54,7 @@
      *
      * @return returns 0 on success, negative errno on failure
      */
-    int transferDataHost(const fs::path& path, uint32_t offset, uint32_t length,
+    int transferDataHost(int fd, uint32_t offset, uint32_t length,
                          uint64_t address, bool upstream);
 };
 
@@ -81,9 +86,32 @@
     Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
 
+    int flags{};
+    if (upstream)
+    {
+        flags = O_RDONLY;
+    }
+    else if (fs::exists(path))
+    {
+        flags = O_RDWR;
+    }
+    else
+    {
+        flags = O_WRONLY;
+    }
+    int file = open(path.string().c_str(), flags);
+    if (file == -1)
+    {
+        std::cerr << "File does not exist, path = " << path.string() << "\n";
+        encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
+                                   responsePtr);
+        return response;
+    }
+    utils::CustomFD fd(file);
+
     while (length > dma::maxSize)
     {
-        auto rc = intf->transferDataHost(path, offset, dma::maxSize, address,
+        auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
                                          upstream);
         if (rc < 0)
         {
@@ -97,7 +125,7 @@
         address += dma::maxSize;
     }
 
-    auto rc = intf->transferDataHost(path, offset, length, address, upstream);
+    auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
     if (rc < 0)
     {
         encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,