oem-ibm: inband: Add processCodeUpdateLid()

The new processCodeUpdateLid() function will process the LID
files sent by the hypervisor so that they can be used by the
code update application to be writtent to flash.

This first commit validates that the LID files contain a valid
magic number in their header.

Change-Id: I9113d46fa6af0d0ef8a0c7b898c1229d49ff05e6
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/oem/ibm/libpldmresponder/inband_code_update.cpp b/oem/ibm/libpldmresponder/inband_code_update.cpp
index cdcf143..8de9993 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.cpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.cpp
@@ -6,10 +6,13 @@
 #include "oem_ibm_handler.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
 
+#include <arpa/inet.h>
+
 #include <sdbusplus/server.hpp>
 #include <xyz/openbmc_project/Dump/NewDump/server.hpp>
 
 #include <exception>
+#include <fstream>
 
 namespace pldm
 {
@@ -213,5 +216,33 @@
     return rc;
 }
 
+int processCodeUpdateLid(const std::string& filePath)
+{
+    struct LidHeader
+    {
+        uint16_t magicNumber;
+    };
+    LidHeader header;
+
+    std::ifstream ifs(filePath, std::ios::in | std::ios::binary);
+    if (!ifs)
+    {
+        std::cerr << "ifstream open error: " << filePath << "\n";
+        return PLDM_ERROR;
+    }
+    ifs.seekg(0);
+    ifs.read(reinterpret_cast<char*>(&header), sizeof(header));
+    ifs.close();
+
+    constexpr auto magicNumber = 0x0222;
+    if (htons(header.magicNumber) != magicNumber)
+    {
+        std::cerr << "Invalid magic number: " << filePath << "\n";
+        return PLDM_ERROR;
+    }
+
+    return PLDM_SUCCESS;
+}
+
 } // namespace responder
 } // namespace pldm
diff --git a/oem/ibm/libpldmresponder/inband_code_update.hpp b/oem/ibm/libpldmresponder/inband_code_update.hpp
index 2ef638d..aad58e1 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.hpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.hpp
@@ -143,5 +143,11 @@
                 const std::vector<set_effecter_state_field>& stateField,
                 CodeUpdate* codeUpdate);
 
+/* @brief Method to process LIDs during inband update, such as verifying and
+ *        removing the header to get them ready to be written to flash
+ * @param[in] filePath - Path to the LID file
+ * @return - PLDM_SUCCESS codes
+ */
+int processCodeUpdateLid(const std::string& filePath);
 } // namespace responder
 } // namespace pldm