PEL: Add boot progress code to SRC hex data

Add the first 8 characters from the ASCII string field of the current
progress SRC, taken from the xyz.openbmc_project.State.Boot.Raw D-Bus
interface, to SRC hex word 4 when creating a PEL.

This is how the field is defined in the PEL spec, and is to help with
debug so that one can know which part of the boot was occurring when
the PEL was created.  Note that at this point most progress codes are
sent down from one of the host firmware subsystems and not created by
the BMC.

The field is only inserted into the SRC if those characters are present
and represent a valid 4 byte number, such as "C7004000".  This is then
represented as 0xC7004000 in the SRC word. Otherwise, the word is left
at a value of zero.

For example:
...
    "Valid Word Count":         "0x09",
    "Reference Code":           "BD8D1001",
    "Hex Word 2":               "00080455",
    "Hex Word 3":               "2E2D0010",
    "Hex Word 4":               "C7004000", <---Progress code
    "Hex Word 5":               "00000000",
    "Hex Word 6":               "00000005",
    "Hex Word 7":               "00000000",
    "Hex Word 8":               "00000000",
    "Hex Word 9":               "00000000"
...

Signed-off-by: Vijay Lobo <vijaylobo@gmail.com>
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Iba41e88626c0e081e5759b994e3630ef8b12daf4
diff --git a/extensions/openpower-pels/src.cpp b/extensions/openpower-pels/src.cpp
index 33e6082..75b291a 100644
--- a/extensions/openpower-pels/src.cpp
+++ b/extensions/openpower-pels/src.cpp
@@ -347,6 +347,7 @@
     //   P: Platform dump status
     //  FF: SRC format, set below
 
+    setProgressCode(dataIface);
     setDumpStatus(dataIface);
     setBMCFormat();
     setBMCPosition();
@@ -1507,5 +1508,56 @@
     return data;
 }
 
+void SRC::setProgressCode(const DataInterfaceBase& dataIface)
+{
+    std::vector<uint8_t> progressSRC;
+
+    try
+    {
+        progressSRC = dataIface.getRawProgressSRC();
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>(
+            fmt::format("Error getting progress code: {}", e.what()).c_str());
+        return;
+    }
+
+    _hexData[2] = getProgressCode(progressSRC);
+}
+
+uint32_t SRC::getProgressCode(std::vector<uint8_t>& rawProgressSRC)
+{
+    uint32_t progressCode = 0;
+
+    // A valid progress SRC is at least 72 bytes
+    if (rawProgressSRC.size() < 72)
+    {
+        return progressCode;
+    }
+
+    try
+    {
+        // The ASCII string field in progress SRCs starts at offset 40.
+        // Take the first 8 characters to put in the uint32:
+        //   "CC009189" -> 0xCC009189
+        Stream stream{rawProgressSRC, 40};
+        src::AsciiString aString{stream};
+        auto progressCodeString = aString.get().substr(0, 8);
+
+        if (std::all_of(progressCodeString.begin(), progressCodeString.end(),
+                        [](char c) {
+                            return std::isxdigit(static_cast<unsigned char>(c));
+                        }))
+        {
+            progressCode = std::stoul(progressCodeString, nullptr, 16);
+        }
+    }
+    catch (const std::exception& e)
+    {}
+
+    return progressCode;
+}
+
 } // namespace pels
 } // namespace openpower