eeprom: update pt5161l getVersion()

Add a step to read fw_load_status before fetching the firmware
version. This ensures the service only proceeds when the device
reports a normal status.

Test on Harma:
```
<7> Host state xyz.openbmc_project.State.Host.HostState.Running matches to retrieve the version
<3> Status from file: /sys/kernel/debug/pt5161l/21-0024/fw_load_status is invalid: abnormal
<7> Host state xyz.openbmc_project.State.Host.HostState.Running matches to retrieve the version
<7> Device version is ready
<7> Harma_MB_Retimer_0_2251: set version 2.8.19
<7> Device version is ready
<7> Harma_MB_Retimer_1_8040: set version 2.8.19
```

Change-Id: I7b665652d8bee20956da3e3d15ff1d222560a656
Signed-off-by: Daniel Hsu <Daniel-Hsu@quantatw.com>
diff --git a/eeprom-device/eeprom_device.cpp b/eeprom-device/eeprom_device.cpp
index 41cfea2..f89292b 100644
--- a/eeprom-device/eeprom_device.cpp
+++ b/eeprom-device/eeprom_device.cpp
@@ -370,6 +370,7 @@
 
 sdbusplus::async::task<> EEPROMDevice::processHostStateChange()
 {
+    constexpr int maxRetries = 15;
     auto requiredHostState = deviceVersion->getHostStateToQueryVersion();
 
     if (!requiredHostState)
@@ -383,7 +384,7 @@
         auto nextResult = co_await hostPower.stateChangedMatch.next<
             std::string, std::map<std::string, std::variant<std::string>>>();
 
-        auto [interfaceName, changedProperties] = nextResult;
+        const auto& [interfaceName, changedProperties] = nextResult;
 
         auto it = changedProperties.find("CurrentHostState");
         if (it != changedProperties.end())
@@ -393,10 +394,22 @@
             if (currentHostState ==
                 State::convertForMessage(*requiredHostState))
             {
+                auto isDeviceReady = false;
                 debug("Host state {STATE} matches to retrieve the version",
                       "STATE", currentHostState);
+                for (int i = 0; i < maxRetries; ++i)
+                {
+                    isDeviceReady = deviceVersion->isDeviceReady();
+                    if (isDeviceReady)
+                    {
+                        debug("Device version is ready");
+                        break;
+                    }
+                    co_await sdbusplus::async::sleep_for(
+                        ctx, std::chrono::seconds(2));
+                }
                 std::string version = deviceVersion->getVersion();
-                if (!version.empty())
+                if (isDeviceReady && !version.empty())
                 {
                     softwareCurrent->setVersion(
                         version,
diff --git a/eeprom-device/eeprom_device_version.hpp b/eeprom-device/eeprom_device_version.hpp
index b05b2c1..814443c 100644
--- a/eeprom-device/eeprom_device_version.hpp
+++ b/eeprom-device/eeprom_device_version.hpp
@@ -17,6 +17,10 @@
         chipModel(chipModel), bus(bus), address(address)
     {}
 
+    virtual bool isDeviceReady()
+    {
+        return true;
+    }
     virtual std::string getVersion() = 0;
     virtual std::optional<HostPowerInf::HostState>
         getHostStateToQueryVersion() = 0;
diff --git a/eeprom-device/pt5161l/pt5161l.cpp b/eeprom-device/pt5161l/pt5161l.cpp
index 2406862..1cc51df 100644
--- a/eeprom-device/pt5161l/pt5161l.cpp
+++ b/eeprom-device/pt5161l/pt5161l.cpp
@@ -39,6 +39,32 @@
     return version;
 }
 
+bool PT5161LDeviceVersion::isDeviceReady()
+{
+    std::string status;
+    std::ostringstream busOss;
+    std::ostringstream addrOss;
+
+    busOss << std::setw(2) << std::setfill('0') << static_cast<int>(bus);
+    addrOss << std::setw(4) << std::setfill('0') << std::hex << std::nouppercase
+            << static_cast<int>(address);
+
+    std::string fw_load_status = "/sys/kernel/debug/pt5161l/" + busOss.str() +
+                                 "-" + addrOss.str() + "/fw_load_status";
+
+    std::ifstream file(fw_load_status);
+
+    if (file && std::getline(file, status) && status == "normal")
+    {
+        return true;
+    }
+
+    error("Status from file: {PATH} is invalid: {STATUS}", "PATH",
+          fw_load_status, "STATUS", status);
+
+    return false;
+}
+
 std::optional<HostPowerInf::HostState>
     PT5161LDeviceVersion::getHostStateToQueryVersion()
 {
diff --git a/eeprom-device/pt5161l/pt5161l.hpp b/eeprom-device/pt5161l/pt5161l.hpp
index 23cf017..8c86bbe 100644
--- a/eeprom-device/pt5161l/pt5161l.hpp
+++ b/eeprom-device/pt5161l/pt5161l.hpp
@@ -6,6 +6,7 @@
 {
   public:
     using DeviceVersion::DeviceVersion;
+    bool isDeviceReady() final;
     std::string getVersion() final;
     std::optional<HostPowerInf::HostState> getHostStateToQueryVersion() final;
 };