Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 1 | #include <fstream> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 2 | #include <string> |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 4 | #include "config.h" |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 5 | #include "item_updater.hpp" |
| 6 | #include "xyz/openbmc_project/Software/Version/server.hpp" |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 7 | #include <experimental/filesystem> |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 8 | #include "version.hpp" |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace software |
| 13 | { |
| 14 | namespace updater |
| 15 | { |
| 16 | |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 17 | // When you see server:: you know we're referencing our base class |
| 18 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
| 19 | |
| 20 | using namespace phosphor::logging; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | constexpr auto bmcImage = "image-rofs"; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 24 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 25 | constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1"; |
| 26 | constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1"; |
| 27 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 28 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 29 | void ItemUpdater::createActivation(sdbusplus::message::message& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 30 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 31 | |
| 32 | using SVersion = server::Version; |
| 33 | using VersionPurpose = SVersion::VersionPurpose; |
| 34 | namespace mesg = sdbusplus::message; |
| 35 | namespace variant_ns = mesg::variant_ns; |
| 36 | |
| 37 | mesg::object_path objPath; |
| 38 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 39 | std::string version; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 40 | std::map<std::string, |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 41 | std::map<std::string, |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 42 | mesg::variant<std::string>>> interfaces; |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 43 | msg.read(objPath, interfaces); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 44 | std::string path(std::move(objPath)); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 45 | std::string filePath; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 46 | |
| 47 | for (const auto& intf : interfaces) |
| 48 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 49 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 50 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 51 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 52 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 53 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 54 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 55 | auto value = SVersion::convertVersionPurposeFromString( |
| 56 | variant_ns::get<std::string>(property.second)); |
| 57 | if (value == VersionPurpose::BMC || |
| 58 | value == VersionPurpose::System) |
| 59 | { |
| 60 | purpose = value; |
| 61 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 62 | } |
| 63 | else if (property.first == "Version") |
| 64 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 65 | version = variant_ns::get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 69 | else if (intf.first == FILEPATH_IFACE) |
| 70 | { |
| 71 | for (const auto& property : intf.second) |
| 72 | { |
| 73 | if (property.first == "Path") |
| 74 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 75 | filePath = variant_ns::get<std::string>(property.second); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 79 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 80 | if (version.empty() || |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 81 | filePath.empty() || |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 82 | purpose == VersionPurpose::Unknown) |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 83 | { |
| 84 | return; |
| 85 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 86 | |
| 87 | // Version id is the last item in the path |
| 88 | auto pos = path.rfind("/"); |
| 89 | if (pos == std::string::npos) |
| 90 | { |
| 91 | log<level::ERR>("No version id found in object path", |
| 92 | entry("OBJPATH=%s", path)); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 93 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | auto versionId = path.substr(pos + 1); |
| 97 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 98 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 99 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 100 | // Determine the Activation state by processing the given image dir. |
| 101 | auto activationState = server::Activation::Activations::Invalid; |
| 102 | ItemUpdater::ActivationStatus result = ItemUpdater:: |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 103 | validateSquashFSImage(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 104 | if (result == ItemUpdater::ActivationStatus::ready) |
| 105 | { |
| 106 | activationState = server::Activation::Activations::Ready; |
| 107 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 108 | activations.insert(std::make_pair( |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 109 | versionId, |
| 110 | std::make_unique<Activation>( |
| 111 | bus, |
| 112 | path, |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 113 | *this, |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 114 | versionId, |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 115 | activationState))); |
| 116 | versions.insert(std::make_pair( |
| 117 | versionId, |
| 118 | std::make_unique<phosphor::software:: |
| 119 | manager::Version>( |
| 120 | bus, |
| 121 | path, |
| 122 | version, |
| 123 | purpose, |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 124 | filePath))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 125 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 126 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 129 | void ItemUpdater::processBMCImage() |
| 130 | { |
| 131 | auto purpose = server::Version::VersionPurpose::BMC; |
| 132 | auto version = phosphor::software::manager::Version::getBMCVersion(); |
| 133 | auto id = phosphor::software::manager::Version::getId(version); |
| 134 | auto path = std::string{SOFTWARE_OBJPATH} + '/' + id; |
| 135 | activations.insert(std::make_pair( |
| 136 | id, |
| 137 | std::make_unique<Activation>( |
| 138 | bus, |
| 139 | path, |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 140 | *this, |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 141 | id, |
| 142 | server::Activation::Activations::Active))); |
| 143 | versions.insert(std::make_pair( |
| 144 | id, |
| 145 | std::make_unique<phosphor::software:: |
| 146 | manager::Version>( |
| 147 | bus, |
| 148 | path, |
| 149 | version, |
| 150 | purpose, |
| 151 | ""))); |
| 152 | return; |
| 153 | } |
| 154 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 155 | ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage( |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 156 | const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 157 | { |
| 158 | |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 159 | fs::path file(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 160 | file /= bmcImage; |
| 161 | std::ifstream efile(file.c_str()); |
| 162 | |
| 163 | if (efile.good() == 1) |
| 164 | { |
| 165 | return ItemUpdater::ActivationStatus::ready; |
| 166 | } |
| 167 | else |
| 168 | { |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 169 | log<level::ERR>("Failed to find the BMC image."); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 170 | return ItemUpdater::ActivationStatus::invalid; |
| 171 | } |
| 172 | } |
| 173 | |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 174 | void ItemUpdater::freePriority(uint8_t value) |
| 175 | { |
| 176 | //TODO openbmc/openbmc#1896 Improve the performance of this function |
| 177 | for (const auto& intf : activations) |
| 178 | { |
| 179 | if(intf.second->redundancyPriority) |
| 180 | { |
| 181 | if (intf.second->redundancyPriority.get()->priority() == value) |
| 182 | { |
| 183 | intf.second->redundancyPriority.get()->priority(value+1); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 189 | void ItemUpdater::reset() |
| 190 | { |
| 191 | // Mark the read-write partition for recreation upon reboot. |
| 192 | auto method = bus.new_method_call( |
| 193 | SYSTEMD_BUSNAME, |
| 194 | SYSTEMD_PATH, |
| 195 | SYSTEMD_INTERFACE, |
| 196 | "StartUnit"); |
| 197 | method.append("obmc-flash-bmc-setenv@rwreset=true.service", "replace"); |
| 198 | bus.call_noreply(method); |
| 199 | |
| 200 | log<level::INFO>("BMC factory reset will take effect upon reboot."); |
| 201 | |
| 202 | return; |
| 203 | } |
| 204 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 205 | } // namespace updater |
| 206 | } // namespace software |
| 207 | } // namespace phosphor |