1 #include "config.h"
2 
3 #include "item_updater_mmc.hpp"
4 
5 #include "activation_mmc.hpp"
6 #include "utils.hpp"
7 #include "version.hpp"
8 
9 #include <filesystem>
10 #include <iostream>
11 #include <thread>
12 
13 namespace openpower
14 {
15 namespace software
16 {
17 namespace updater
18 {
19 
20 // These functions are just a stub (empty) because the current eMMC
21 // implementation uses the BMC updater (repo phosphor-bmc-code-mgmt) to write
22 // the new host FW to flash since it's delivered as a "System" image in the
23 // same BMC tarball as the BMC image.
24 
25 std::unique_ptr<Activation> ItemUpdaterMMC::createActivationObject(
26     const std::string& path, const std::string& versionId,
27     const std::string& extVersion,
28     sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
29         activationStatus,
30     AssociationList& assocs)
31 {
32     return std::make_unique<ActivationMMC>(
33         bus, path, *this, versionId, extVersion, activationStatus, assocs);
34 }
35 
36 std::unique_ptr<Version> ItemUpdaterMMC::createVersionObject(
37     const std::string& objPath, const std::string& versionId,
38     const std::string& versionString,
39     sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
40         versionPurpose,
41     const std::string& filePath)
42 {
43     auto version = std::make_unique<Version>(
44         bus, objPath, *this, versionId, versionString, versionPurpose, filePath,
45         std::bind(&ItemUpdaterMMC::erase, this, std::placeholders::_1));
46     version->deleteObject = std::make_unique<Delete>(bus, objPath, *version);
47     return version;
48 }
49 
50 bool ItemUpdaterMMC::validateImage(const std::string&)
51 {
52     return true;
53 }
54 
55 void ItemUpdaterMMC::processPNORImage()
56 {}
57 
58 void ItemUpdaterMMC::reset()
59 {
60     // Do not reset read-only files needed for reset or ext4 default files
61     const std::vector<std::string> exclusionList = {"alternate", "hostfw-a",
62                                                     "hostfw-b",  "lost+found",
63                                                     "nvram",     "running-ro"};
64     std::filesystem::path dirPath(std::string(MEDIA_DIR "hostfw/"));
65     // Delete all files in /media/hostfw/ except for those on exclusionList
66     for (const auto& p : std::filesystem::directory_iterator(dirPath))
67     {
68         if (std::find(exclusionList.begin(), exclusionList.end(),
69                       p.path().stem().string()) == exclusionList.end())
70         {
71             std::filesystem::remove_all(p);
72         }
73     }
74 
75     // Delete all BMC error logs to avoid discrepancies with the host error logs
76     utils::deleteAllErrorLogs(bus);
77 
78     // Set attribute to clear hypervisor NVRAM
79     utils::setClearNvram(bus);
80 
81     // Remove files related to the Hardware Management Console / BMC web app
82     utils::clearHMCManaged(bus);
83     std::filesystem::path consolePath("/var/lib/bmcweb/ibm-management-console");
84     if (std::filesystem::exists(consolePath))
85     {
86         std::filesystem::remove_all(consolePath);
87     }
88     std::filesystem::path bmcdataPath("/home/root/bmcweb_persistent_data.json");
89     if (std::filesystem::exists(bmcdataPath))
90     {
91         std::filesystem::remove(bmcdataPath);
92     }
93 
94     // Recreate default files.
95     // std::tuple<method, service_name>
96     const std::tuple<std::string, std::string> services[] = {
97         {"StartUnit", "obmc-flash-bios-init.service"},
98         {"StartUnit", "obmc-flash-bios-patch.service"},
99         {"StartUnit", "openpower-process-host-firmware.service"},
100         {"StartUnit", "openpower-update-bios-attr-table.service"},
101         {"RestartUnit", "org.open_power.HardwareIsolation.service"}};
102 
103     for (const auto& service : services)
104     {
105         auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
106                                           SYSTEMD_INTERFACE,
107                                           std::get<0>(service).c_str());
108         method.append(std::get<1>(service), "replace");
109         // Ignore errors if the service is not found - not all systems
110         // may have these services
111         try
112         {
113             bus.call_noreply(method);
114         }
115         catch (const std::exception& e)
116         {}
117     }
118 
119     // Wait a few seconds for the service files and reset operations to finish,
120     // otherwise the BMC may be rebooted and cause corruption.
121     constexpr auto resetWait = std::chrono::seconds(5);
122     std::this_thread::sleep_for(resetWait);
123 }
124 
125 bool ItemUpdaterMMC::isVersionFunctional(const std::string& versionId)
126 {
127     return versionId == functionalVersionId;
128 }
129 
130 void ItemUpdaterMMC::freePriority(uint8_t, const std::string&)
131 {}
132 
133 void ItemUpdaterMMC::deleteAll()
134 {}
135 
136 bool ItemUpdaterMMC::freeSpace()
137 {
138     return true;
139 }
140 
141 void ItemUpdaterMMC::updateFunctionalAssociation(const std::string&)
142 {}
143 
144 void GardResetMMC::reset()
145 {}
146 
147 } // namespace updater
148 } // namespace software
149 } // namespace openpower
150