Fixed open file failures

Handled file open failure issue and made changes to reset the
EEPROM file pointer to a "safe" location if the VPD is a DDIMM SPD.

Change-Id: I5332378e958cbe5b771f3497532b8876411f6913
Signed-off-by: jinuthomas <jinu.joy.thomas@in.ibm.com>
diff --git a/ibm_vpd_utils.cpp b/ibm_vpd_utils.cpp
index 2378574..f0395a9 100644
--- a/ibm_vpd_utils.cpp
+++ b/ibm_vpd_utils.cpp
@@ -3,6 +3,7 @@
 #include "ibm_vpd_utils.hpp"
 
 #include "common_utility.hpp"
+#include "const.hpp"
 #include "defines.hpp"
 #include "vpd_exceptions.hpp"
 
@@ -1022,7 +1023,7 @@
     vpdFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
     try
     {
-        vpdFile.open(file, std::ios::binary);
+        vpdFile.open(file, std::ios::binary | std::ios::in);
         vpdFile.seekg(offset, std::ios_base::cur);
         vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
         vpdVector.resize(vpdFile.gcount());
@@ -1038,8 +1039,8 @@
         throw;
     }
 
-    // Make sure we reset the EEPROM pointer to a "safe" location if it was DIMM
-    // SPD that we just read.
+    // Make sure we reset the EEPROM pointer to a "safe" location if it was
+    // a DDIMM SPD that we just read.
     for (const auto& item : js["frus"][file])
     {
         if (item.find("extraInterfaces") != item.end())
@@ -1048,22 +1049,28 @@
                     "xyz.openbmc_project.Inventory.Item.Dimm") !=
                 item["extraInterfaces"].end())
             {
-                try
+                // check added here for DDIMM only workarround
+                vpdType dimmType = vpdTypeCheck(vpdVector);
+                if (dimmType == constants::DDR4_DDIMM_MEMORY_VPD ||
+                    dimmType == constants::DDR5_DDIMM_MEMORY_VPD)
                 {
-                    // moves the EEPROM pointer to 2048 'th byte.
-                    vpdFile.seekg(2047, std::ios::beg);
-                    // Read that byte and discard - to affirm the move
-                    // operation.
-                    char ch;
-                    vpdFile.read(&ch, sizeof(ch));
-                }
-                catch (const std::ifstream::failure& fail)
-                {
-                    std::cerr << "Exception in file handling [" << file
-                              << "] error : " << fail.what();
-                    std::cerr << "Stream file size = " << vpdFile.gcount()
-                              << std::endl;
-                    throw;
+                    try
+                    {
+                        // moves the EEPROM pointer to 2048 'th byte.
+                        vpdFile.seekg(2047, std::ios::beg);
+                        // Read that byte and discard - to affirm the move
+                        // operation.
+                        char ch;
+                        vpdFile.read(&ch, sizeof(ch));
+                    }
+                    catch (const std::ifstream::failure& fail)
+                    {
+                        std::cerr << "Exception in file handling [" << file
+                                  << "] error : " << fail.what();
+                        std::cerr << "Stream file size = " << vpdFile.gcount()
+                                  << std::endl;
+                        throw;
+                    }
                 }
                 break;
             }