xref: /openbmc/openpower-pnor-code-mgmt/mmc/item_updater_mmc.cpp (revision f8e024296d02170b360c894349d900acfd940a4d)
18bc2ab4eSAdriana Kobylak #include "config.h"
28bc2ab4eSAdriana Kobylak 
38bc2ab4eSAdriana Kobylak #include "item_updater_mmc.hpp"
48bc2ab4eSAdriana Kobylak 
58bc2ab4eSAdriana Kobylak #include "activation_mmc.hpp"
656a46771SAdriana Kobylak #include "utils.hpp"
78bc2ab4eSAdriana Kobylak #include "version.hpp"
88bc2ab4eSAdriana Kobylak 
92b31497eSMarri Devender Rao #include <phosphor-logging/log.hpp>
102b31497eSMarri Devender Rao 
110ddd4fadSIsaac Kurth #include <filesystem>
124cf521e3SPatrick Williams #include <format>
13bde5d7d4SIsaac Kurth #include <iostream>
14295fce09SAdriana Kobylak #include <thread>
150ddd4fadSIsaac Kurth 
168bc2ab4eSAdriana Kobylak namespace openpower
178bc2ab4eSAdriana Kobylak {
188bc2ab4eSAdriana Kobylak namespace software
198bc2ab4eSAdriana Kobylak {
208bc2ab4eSAdriana Kobylak namespace updater
218bc2ab4eSAdriana Kobylak {
228bc2ab4eSAdriana Kobylak 
232b31497eSMarri Devender Rao using ::phosphor::logging::level;
242b31497eSMarri Devender Rao using ::phosphor::logging::log;
258bc2ab4eSAdriana Kobylak // These functions are just a stub (empty) because the current eMMC
268bc2ab4eSAdriana Kobylak // implementation uses the BMC updater (repo phosphor-bmc-code-mgmt) to write
278bc2ab4eSAdriana Kobylak // the new host FW to flash since it's delivered as a "System" image in the
288bc2ab4eSAdriana Kobylak // same BMC tarball as the BMC image.
298bc2ab4eSAdriana Kobylak 
createActivationObject(const std::string & path,const std::string & versionId,const std::string & extVersion,sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations activationStatus,AssociationList & assocs)308bc2ab4eSAdriana Kobylak std::unique_ptr<Activation> ItemUpdaterMMC::createActivationObject(
318bc2ab4eSAdriana Kobylak     const std::string& path, const std::string& versionId,
328bc2ab4eSAdriana Kobylak     const std::string& extVersion,
338bc2ab4eSAdriana Kobylak     sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
348bc2ab4eSAdriana Kobylak         activationStatus,
358bc2ab4eSAdriana Kobylak     AssociationList& assocs)
368bc2ab4eSAdriana Kobylak {
378bc2ab4eSAdriana Kobylak     return std::make_unique<ActivationMMC>(
388bc2ab4eSAdriana Kobylak         bus, path, *this, versionId, extVersion, activationStatus, assocs);
398bc2ab4eSAdriana Kobylak }
408bc2ab4eSAdriana Kobylak 
createVersionObject(const std::string & objPath,const std::string & versionId,const std::string & versionString,sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose versionPurpose,const std::string & filePath)418bc2ab4eSAdriana Kobylak std::unique_ptr<Version> ItemUpdaterMMC::createVersionObject(
428bc2ab4eSAdriana Kobylak     const std::string& objPath, const std::string& versionId,
438bc2ab4eSAdriana Kobylak     const std::string& versionString,
448bc2ab4eSAdriana Kobylak     sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
458bc2ab4eSAdriana Kobylak         versionPurpose,
468bc2ab4eSAdriana Kobylak     const std::string& filePath)
478bc2ab4eSAdriana Kobylak {
488bc2ab4eSAdriana Kobylak     auto version = std::make_unique<Version>(
498bc2ab4eSAdriana Kobylak         bus, objPath, *this, versionId, versionString, versionPurpose, filePath,
508bc2ab4eSAdriana Kobylak         std::bind(&ItemUpdaterMMC::erase, this, std::placeholders::_1));
518bc2ab4eSAdriana Kobylak     version->deleteObject = std::make_unique<Delete>(bus, objPath, *version);
528bc2ab4eSAdriana Kobylak     return version;
538bc2ab4eSAdriana Kobylak }
548bc2ab4eSAdriana Kobylak 
validateImage(const std::string &)55c8f22502SBrad Bishop bool ItemUpdaterMMC::validateImage(const std::string&)
568bc2ab4eSAdriana Kobylak {
578bc2ab4eSAdriana Kobylak     return true;
588bc2ab4eSAdriana Kobylak }
598bc2ab4eSAdriana Kobylak 
processPNORImage()607fb6c346SPatrick Williams void ItemUpdaterMMC::processPNORImage() {}
618bc2ab4eSAdriana Kobylak 
reset()628bc2ab4eSAdriana Kobylak void ItemUpdaterMMC::reset()
630ddd4fadSIsaac Kurth {
640ddd4fadSIsaac Kurth     // Do not reset read-only files needed for reset or ext4 default files
65*f8e02429SPatrick Williams     const std::vector<std::string> exclusionList = {
66*f8e02429SPatrick Williams         "alternate",  "hostfw-a", "hostfw-b",
67*f8e02429SPatrick Williams         "lost+found", "nvram",    "running-ro"};
680ddd4fadSIsaac Kurth     std::filesystem::path dirPath(std::string(MEDIA_DIR "hostfw/"));
690ddd4fadSIsaac Kurth     // Delete all files in /media/hostfw/ except for those on exclusionList
700ddd4fadSIsaac Kurth     for (const auto& p : std::filesystem::directory_iterator(dirPath))
710ddd4fadSIsaac Kurth     {
720ddd4fadSIsaac Kurth         if (std::find(exclusionList.begin(), exclusionList.end(),
730ddd4fadSIsaac Kurth                       p.path().stem().string()) == exclusionList.end())
740ddd4fadSIsaac Kurth         {
750ddd4fadSIsaac Kurth             std::filesystem::remove_all(p);
760ddd4fadSIsaac Kurth         }
770ddd4fadSIsaac Kurth     }
780ddd4fadSIsaac Kurth 
79267c413fSAdriana Kobylak     // Delete all BMC error logs to avoid discrepancies with the host error logs
80267c413fSAdriana Kobylak     utils::deleteAllErrorLogs(bus);
81267c413fSAdriana Kobylak 
82f9a72a7aSAdriana Kobylak     // Set attribute to clear hypervisor NVRAM
83f9a72a7aSAdriana Kobylak     utils::setClearNvram(bus);
84f9a72a7aSAdriana Kobylak 
852b31497eSMarri Devender Rao     // reset the enabled property of dimms/cpu after factory reset
862b31497eSMarri Devender Rao     gardReset->reset();
872b31497eSMarri Devender Rao 
88bde5d7d4SIsaac Kurth     // Remove files related to the Hardware Management Console / BMC web app
8956a46771SAdriana Kobylak     utils::clearHMCManaged(bus);
90bde5d7d4SIsaac Kurth     std::filesystem::path consolePath("/var/lib/bmcweb/ibm-management-console");
91bde5d7d4SIsaac Kurth     if (std::filesystem::exists(consolePath))
92bde5d7d4SIsaac Kurth     {
93bde5d7d4SIsaac Kurth         std::filesystem::remove_all(consolePath);
94bde5d7d4SIsaac Kurth     }
95bde5d7d4SIsaac Kurth     std::filesystem::path bmcdataPath("/home/root/bmcweb_persistent_data.json");
96bde5d7d4SIsaac Kurth     if (std::filesystem::exists(bmcdataPath))
97bde5d7d4SIsaac Kurth     {
98bde5d7d4SIsaac Kurth         std::filesystem::remove(bmcdataPath);
99bde5d7d4SIsaac Kurth     }
100bde5d7d4SIsaac Kurth 
1010ddd4fadSIsaac Kurth     // Recreate default files.
1026b56bd4bSRamesh Iyyar     // std::tuple<method, service_name>
1036b56bd4bSRamesh Iyyar     const std::tuple<std::string, std::string> services[] = {
1046b56bd4bSRamesh Iyyar         {"StartUnit", "obmc-flash-bios-init.service"},
1056b56bd4bSRamesh Iyyar         {"StartUnit", "obmc-flash-bios-patch.service"},
1066b56bd4bSRamesh Iyyar         {"StartUnit", "openpower-process-host-firmware.service"},
1076b56bd4bSRamesh Iyyar         {"StartUnit", "openpower-update-bios-attr-table.service"},
1086b56bd4bSRamesh Iyyar         {"RestartUnit", "org.open_power.HardwareIsolation.service"}};
109bde5d7d4SIsaac Kurth 
110bde5d7d4SIsaac Kurth     for (const auto& service : services)
111bde5d7d4SIsaac Kurth     {
1120ddd4fadSIsaac Kurth         auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
1136b56bd4bSRamesh Iyyar                                           SYSTEMD_INTERFACE,
1146b56bd4bSRamesh Iyyar                                           std::get<0>(service).c_str());
1156b56bd4bSRamesh Iyyar         method.append(std::get<1>(service), "replace");
116bde5d7d4SIsaac Kurth         // Ignore errors if the service is not found - not all systems
117bde5d7d4SIsaac Kurth         // may have these services
118bde5d7d4SIsaac Kurth         try
119bde5d7d4SIsaac Kurth         {
1200ddd4fadSIsaac Kurth             bus.call_noreply(method);
121bde5d7d4SIsaac Kurth         }
122bde5d7d4SIsaac Kurth         catch (const std::exception& e)
123bde5d7d4SIsaac Kurth         {}
124bde5d7d4SIsaac Kurth     }
125295fce09SAdriana Kobylak 
126295fce09SAdriana Kobylak     // Wait a few seconds for the service files and reset operations to finish,
127295fce09SAdriana Kobylak     // otherwise the BMC may be rebooted and cause corruption.
128295fce09SAdriana Kobylak     constexpr auto resetWait = std::chrono::seconds(5);
129295fce09SAdriana Kobylak     std::this_thread::sleep_for(resetWait);
1300ddd4fadSIsaac Kurth }
1318bc2ab4eSAdriana Kobylak 
isVersionFunctional(const std::string & versionId)1328bc2ab4eSAdriana Kobylak bool ItemUpdaterMMC::isVersionFunctional(const std::string& versionId)
1338bc2ab4eSAdriana Kobylak {
1348bc2ab4eSAdriana Kobylak     return versionId == functionalVersionId;
1358bc2ab4eSAdriana Kobylak }
1368bc2ab4eSAdriana Kobylak 
freePriority(uint8_t,const std::string &)1377fb6c346SPatrick Williams void ItemUpdaterMMC::freePriority(uint8_t, const std::string&) {}
1388bc2ab4eSAdriana Kobylak 
deleteAll()1397fb6c346SPatrick Williams void ItemUpdaterMMC::deleteAll() {}
1408bc2ab4eSAdriana Kobylak 
freeSpace()1418bc2ab4eSAdriana Kobylak bool ItemUpdaterMMC::freeSpace()
1428bc2ab4eSAdriana Kobylak {
1438bc2ab4eSAdriana Kobylak     return true;
1448bc2ab4eSAdriana Kobylak }
1458bc2ab4eSAdriana Kobylak 
updateFunctionalAssociation(const std::string &)1467fb6c346SPatrick Williams void ItemUpdaterMMC::updateFunctionalAssociation(const std::string&) {}
enableInventoryItems()1472b31497eSMarri Devender Rao void GardResetMMC::enableInventoryItems()
1482b31497eSMarri Devender Rao {
1492b31497eSMarri Devender Rao     (void)enableInventoryItemsHelper(
1502b31497eSMarri Devender Rao         "xyz.openbmc_project.PLDM",
1512b31497eSMarri Devender Rao         "xyz.openbmc_project.Inventory.Item.CpuCore",
1522b31497eSMarri Devender Rao         "/xyz/openbmc_project/inventory/system/chassis/motherboard");
1532b31497eSMarri Devender Rao 
1542b31497eSMarri Devender Rao     (void)enableInventoryItemsHelper("xyz.openbmc_project.Inventory.Manager",
1552b31497eSMarri Devender Rao                                      "xyz.openbmc_project.Inventory.Item.Dimm",
1562b31497eSMarri Devender Rao                                      "/xyz/openbmc_project/inventory");
1572b31497eSMarri Devender Rao }
1582b31497eSMarri Devender Rao 
enableInventoryItemsHelper(const std::string & service,const std::string & intf,const std::string & objPath)1592b31497eSMarri Devender Rao void GardResetMMC::enableInventoryItemsHelper(const std::string& service,
1602b31497eSMarri Devender Rao                                               const std::string& intf,
1612b31497eSMarri Devender Rao                                               const std::string& objPath)
1622b31497eSMarri Devender Rao {
1632b31497eSMarri Devender Rao     const std::vector<std::string> intflist{intf};
1642b31497eSMarri Devender Rao 
1652b31497eSMarri Devender Rao     std::vector<std::string> objs;
1662b31497eSMarri Devender Rao     try
1672b31497eSMarri Devender Rao     {
1682b31497eSMarri Devender Rao         auto mapperCall = bus.new_method_call(
1692b31497eSMarri Devender Rao             "xyz.openbmc_project.ObjectMapper",
1702b31497eSMarri Devender Rao             "/xyz/openbmc_project/object_mapper",
1712b31497eSMarri Devender Rao             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths");
1722b31497eSMarri Devender Rao         mapperCall.append(objPath);
1732b31497eSMarri Devender Rao         mapperCall.append(0);
1742b31497eSMarri Devender Rao         mapperCall.append(intflist);
1752b31497eSMarri Devender Rao 
1762b31497eSMarri Devender Rao         auto response = bus.call(mapperCall);
1772b31497eSMarri Devender Rao         response.read(objs);
1782b31497eSMarri Devender Rao         for (auto& obj : objs)
1792b31497eSMarri Devender Rao         {
180*f8e02429SPatrick Williams             auto method =
181*f8e02429SPatrick Williams                 bus.new_method_call(service.c_str(), obj.c_str(),
182*f8e02429SPatrick Williams                                     "org.freedesktop.DBus.Properties", "Set");
1832b31497eSMarri Devender Rao             std::variant<bool> propertyVal{true};
1842b31497eSMarri Devender Rao             method.append("xyz.openbmc_project.Object.Enable", "Enabled",
1852b31497eSMarri Devender Rao                           propertyVal);
1862b31497eSMarri Devender Rao             bus.call_noreply(method);
1872b31497eSMarri Devender Rao         }
1882b31497eSMarri Devender Rao     }
1892b31497eSMarri Devender Rao     catch (const sdbusplus::exception_t& e)
1902b31497eSMarri Devender Rao     {
1912b31497eSMarri Devender Rao         log<level::ERR>(
1924cf521e3SPatrick Williams             std::format("Failed to enable specified inventory items ex({}) "
1932b31497eSMarri Devender Rao                         "intf({}) objpath({})",
1942b31497eSMarri Devender Rao                         e.what(), intf, objPath)
1952b31497eSMarri Devender Rao                 .c_str());
1962b31497eSMarri Devender Rao     }
1972b31497eSMarri Devender Rao }
1988bc2ab4eSAdriana Kobylak 
reset()1998bc2ab4eSAdriana Kobylak void GardResetMMC::reset()
2002b31497eSMarri Devender Rao {
2012b31497eSMarri Devender Rao     log<level::INFO>("GardResetMMC::reset");
2022b31497eSMarri Devender Rao     (void)enableInventoryItems();
2032b31497eSMarri Devender Rao }
2048bc2ab4eSAdriana Kobylak 
2058bc2ab4eSAdriana Kobylak } // namespace updater
2068bc2ab4eSAdriana Kobylak } // namespace software
2078bc2ab4eSAdriana Kobylak } // namespace openpower
208