oem-ibm: Maintain bootside Mapping and Set bios attribute

This commit adds code to maintain a mapping between running,
non-running and Temp, Perm side which would help the bmc
and remote PLDM terminus determine at any point in time if the
code update was successful and that remote PLDM terminus knows
exactly which side it is running on.

In the event the code update failed, then for that case
the new added bios attribute "fw_boot_side", which will
be set in the event. If BMC tries to boot from the new
"Temp" side (other side), and if it fails, then BMC must
update fw_boot_side to "Perm".

Change-Id: I9f1edad1e36850742aba88d93f8cf0fc8b9d8c8d
Signed-off-by: Archana Kakani <archana.kakani@ibm.com>
diff --git a/oem/ibm/libpldmresponder/inband_code_update.cpp b/oem/ibm/libpldmresponder/inband_code_update.cpp
index 8acfe56..855258d 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.cpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.cpp
@@ -7,6 +7,7 @@
 #include <arpa/inet.h>
 #include <libpldm/entity.h>
 
+#include <nlohmann/json.hpp>
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/server.hpp>
 #include <xyz/openbmc_project/Dump/NewDump/server.hpp>
@@ -39,6 +40,9 @@
 /** @brief The file name of the hostfw image */
 constexpr auto hostfwImageName = "image-hostfw";
 
+/** @brief The filename of the file where bootside data will be saved */
+constexpr auto bootSideFileName = "bootSide";
+
 /** @brief The path to the code update tarball file */
 auto tarImagePath = fs::path(imageDirPath) / tarImageName;
 
@@ -49,6 +53,15 @@
  *         manager */
 auto updateImagePath = fs::path("/tmp/images") / tarImageName;
 
+/** @brief Current boot side */
+constexpr auto bootSideAttrName = "fw_boot_side_current";
+
+/** @brief Next boot side */
+constexpr auto bootNextSideAttrName = "fw_boot_side";
+
+/** @brief The filepath of file where bootside data will be saved */
+auto bootSideDirPath = fs::path("/var/lib/pldm/") / bootSideFileName;
+
 std::string CodeUpdate::fetchCurrentBootSide()
 {
     return currBootSide;
@@ -67,14 +80,23 @@
 
 int CodeUpdate::setNextBootSide(const std::string& nextSide)
 {
+    info("setNextBootSide, nextSide={NXT_SIDE}", "NXT_SIDE", nextSide);
+    pldm_boot_side_data pldmBootSideData = readBootSideFile();
+    currBootSide =
+        (pldmBootSideData.current_boot_side == "Perm" ? Pside : Tside);
     nextBootSide = nextSide;
+    pldmBootSideData.next_boot_side = (nextSide == Pside ? "Perm" : "Temp");
     std::string objPath{};
     if (nextBootSide == currBootSide)
     {
+        info(
+            "Current bootside is same as next boot side, setting priority of running version 0");
         objPath = runningVersion;
     }
     else
     {
+        info(
+            "Current bootside is not same as next boot side, setting priority of non running version 0");
         objPath = nonRunningVersion;
     }
     if (objPath.empty())
@@ -83,6 +105,26 @@
         return PLDM_PLATFORM_INVALID_STATE_VALUE;
     }
 
+    try
+    {
+        auto priorityPropValue = dBusIntf->getDbusPropertyVariant(
+            objPath.c_str(), "Priority", redundancyIntf);
+        const auto& priorityValue = std::get<uint8_t>(priorityPropValue);
+        if (priorityValue == 0)
+        {
+            // Requested next boot side is already set
+            return PLDM_SUCCESS;
+        }
+    }
+    catch (const std::exception& e)
+    {
+        // Alternate side may not be present due to a failed code update
+        error("Alternate side may not be present due to a failed code update. "
+              "ERROR: {ERR}",
+              "ERR", e);
+        return PLDM_PLATFORM_INVALID_STATE_VALUE;
+    }
+
     pldm::utils::DBusMapping dbusMapping{objPath, redundancyIntf, "Priority",
                                          "uint8_t"};
     uint8_t val = 0;
@@ -97,6 +139,7 @@
               "PATH", objPath, "ERROR", e);
         return PLDM_ERROR;
     }
+    writeBootSideFile(pldmBootSideData);
     return PLDM_SUCCESS;
 }
 
@@ -152,6 +195,7 @@
 
 void CodeUpdate::setVersions()
 {
+    PendingAttributesList biosAttrList;
     static constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
     static constexpr auto functionalObjPath =
         "/xyz/openbmc_project/software/functional";
@@ -186,6 +230,94 @@
                 break;
             }
         }
+        if (!fs::exists(bootSideDirPath))
+        {
+            pldm_boot_side_data pldmBootSideData;
+            std::string nextBootSideBiosValue = "Temp";
+            auto attributeValue = getBiosAttrValue<std::string>("fw_boot_side");
+
+            // We enter this path during Genesis boot/boot after Factory reset.
+            // PLDM waits for Entity manager to populate System Type. After
+            // receiving system Type from EM it populates the bios attributes
+            // specific to that system We do not have bios attributes populated
+            // when we reach here so setting it to default value of the
+            // attribute as mentioned in the json files.
+            if (attributeValue.has_value())
+            {
+                nextBootSideBiosValue = attributeValue.value();
+            }
+            else
+            {
+                info(
+                    "Boot side is not initialized yet, so setting default value");
+                nextBootSideBiosValue = "Temp";
+            }
+            pldmBootSideData.current_boot_side = nextBootSideBiosValue;
+            pldmBootSideData.next_boot_side = nextBootSideBiosValue;
+            pldmBootSideData.running_version_object = runningVersion;
+
+            writeBootSideFile(pldmBootSideData);
+            biosAttrList.emplace_back(std::make_pair(
+                bootSideAttrName,
+                std::make_tuple(EnumAttribute,
+                                pldmBootSideData.current_boot_side)));
+            biosAttrList.push_back(std::make_pair(
+                bootNextSideAttrName,
+                std::make_tuple(EnumAttribute,
+                                pldmBootSideData.next_boot_side)));
+            setBiosAttr(biosAttrList);
+        }
+        else
+        {
+            pldm_boot_side_data pldmBootSideData = readBootSideFile();
+            if (pldmBootSideData.running_version_object != runningVersion)
+            {
+                info(
+                    "BMC have booted with the new image runningPath={RUNN_PATH}",
+                    "RUNN_PATH", runningVersion.c_str());
+                info("Previous Image was: {RUNN_VERS}", "RUNN_VERS",
+                     pldmBootSideData.running_version_object);
+                auto current_boot_side =
+                    (pldmBootSideData.current_boot_side == "Temp" ? "Perm"
+                                                                  : "Temp");
+                pldmBootSideData.current_boot_side = current_boot_side;
+                pldmBootSideData.next_boot_side = current_boot_side;
+                pldmBootSideData.running_version_object = runningVersion;
+                writeBootSideFile(pldmBootSideData);
+                biosAttrList.emplace_back(std::make_pair(
+                    bootSideAttrName,
+                    std::make_tuple(EnumAttribute,
+                                    pldmBootSideData.current_boot_side)));
+                biosAttrList.push_back(std::make_pair(
+                    bootNextSideAttrName,
+                    std::make_tuple(EnumAttribute,
+                                    pldmBootSideData.next_boot_side)));
+                setBiosAttr(biosAttrList);
+            }
+            else
+            {
+                info(
+                    "BMC have booted with the previous image runningPath={RUNN_PATH}",
+                    "RUNN_PATH", pldmBootSideData.running_version_object);
+                pldm_boot_side_data pldmBootSideData = readBootSideFile();
+                pldmBootSideData.next_boot_side =
+                    pldmBootSideData.current_boot_side;
+                writeBootSideFile(pldmBootSideData);
+                biosAttrList.emplace_back(std::make_pair(
+                    bootSideAttrName,
+                    std::make_tuple(EnumAttribute,
+                                    pldmBootSideData.current_boot_side)));
+                biosAttrList.push_back(std::make_pair(
+                    bootNextSideAttrName,
+                    std::make_tuple(EnumAttribute,
+                                    pldmBootSideData.next_boot_side)));
+                setBiosAttr(biosAttrList);
+            }
+            currBootSide =
+                (pldmBootSideData.current_boot_side == "Temp" ? Tside : Pside);
+            nextBootSide =
+                (pldmBootSideData.next_boot_side == "Temp" ? Tside : Pside);
+        }
     }
     catch (const std::exception& e)
     {
@@ -328,11 +460,92 @@
 
 void CodeUpdate::processRenameEvent()
 {
+    info("Processing Rename Event");
+
+    PendingAttributesList biosAttrList;
+    pldm_boot_side_data pldmBootSideData = readBootSideFile();
+    pldmBootSideData.current_boot_side = "Perm";
+    pldmBootSideData.next_boot_side = "Perm";
+
     currBootSide = Pside;
+    nextBootSide = Pside;
+
     auto sensorId = getBootSideRenameStateSensor();
+    info("Received sendor id for rename {ID}", "ID", sensorId);
     sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
                          PLDM_OEM_IBM_BOOT_SIDE_RENAME_STATE_RENAMED,
                          PLDM_OEM_IBM_BOOT_SIDE_RENAME_STATE_NOT_RENAMED);
+    writeBootSideFile(pldmBootSideData);
+    biosAttrList.emplace_back(std::make_pair(
+        bootSideAttrName,
+        std::make_tuple(EnumAttribute, pldmBootSideData.current_boot_side)));
+    biosAttrList.push_back(std::make_pair(
+        bootNextSideAttrName,
+        std::make_tuple(EnumAttribute, pldmBootSideData.next_boot_side)));
+    setBiosAttr(biosAttrList);
+}
+
+void CodeUpdate::writeBootSideFile(const pldm_boot_side_data& pldmBootSideData)
+{
+    try
+    {
+        fs::create_directories(bootSideDirPath.parent_path());
+        std::ofstream writeFile(bootSideDirPath, std::ios::out);
+        if (!writeFile.is_open())
+        {
+            error("Failed to open bootside file {FILE} for writing", "FILE",
+                  bootSideDirPath.string());
+            return;
+        }
+
+        nlohmann::json data;
+        data["CurrentBootSide"] = pldmBootSideData.current_boot_side;
+        data["NextBootSide"] = pldmBootSideData.next_boot_side;
+        data["RunningObject"] = pldmBootSideData.running_version_object;
+
+        try
+        {
+            writeFile << data.dump(4);
+        }
+        catch (const nlohmann::json::exception& e)
+        {
+            error("JSON serialization for BootSide failed: {ERROR}", "ERROR",
+                  e);
+            return;
+        }
+
+        writeFile.close();
+    }
+    catch (const std::exception& e)
+    {
+        error("Error {ERROR] while writing bootside file: {FILE}", "ERROR", e,
+              "FILE", bootSideDirPath);
+    }
+}
+
+pldm_boot_side_data CodeUpdate::readBootSideFile()
+{
+    pldm_boot_side_data pldmBootSideDataRead{};
+
+    std::ifstream readFile(bootSideDirPath.string(), std::ios::in);
+
+    if (!readFile)
+    {
+        error("Failed to read Bootside file");
+        return pldmBootSideDataRead;
+    }
+
+    nlohmann::json jsonBootSideData;
+    readFile >> jsonBootSideData;
+
+    pldm_boot_side_data data;
+    data.current_boot_side = jsonBootSideData.value("CurrentBootSide", "");
+    data.next_boot_side = jsonBootSideData.value("NextBootSide", "");
+    data.running_version_object = jsonBootSideData.value("RunningObject", "");
+
+    readFile.close();
+
+    return pldmBootSideDataRead;
 }
 
 void CodeUpdate::processPriorityChangeNotification(