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 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 25 | void ItemUpdater::createActivation(sdbusplus::message::message& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 26 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 27 | |
| 28 | using SVersion = server::Version; |
| 29 | using VersionPurpose = SVersion::VersionPurpose; |
| 30 | namespace mesg = sdbusplus::message; |
| 31 | namespace variant_ns = mesg::variant_ns; |
| 32 | |
| 33 | mesg::object_path objPath; |
| 34 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 35 | std::string version; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 36 | std::map<std::string, |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 37 | std::map<std::string, |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 38 | mesg::variant<std::string>>> interfaces; |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 39 | msg.read(objPath, interfaces); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 40 | std::string path(std::move(objPath)); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 41 | std::string filePath; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 42 | |
| 43 | for (const auto& intf : interfaces) |
| 44 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 45 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 46 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 47 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 48 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 49 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 50 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 51 | auto value = SVersion::convertVersionPurposeFromString( |
| 52 | variant_ns::get<std::string>(property.second)); |
| 53 | if (value == VersionPurpose::BMC || |
| 54 | value == VersionPurpose::System) |
| 55 | { |
| 56 | purpose = value; |
| 57 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 58 | } |
| 59 | else if (property.first == "Version") |
| 60 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 61 | version = variant_ns::get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 65 | else if (intf.first == FILEPATH_IFACE) |
| 66 | { |
| 67 | for (const auto& property : intf.second) |
| 68 | { |
| 69 | if (property.first == "Path") |
| 70 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 71 | filePath = variant_ns::get<std::string>(property.second); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 75 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 76 | if (version.empty() || |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 77 | filePath.empty() || |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 78 | purpose == VersionPurpose::Unknown) |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 79 | { |
| 80 | return; |
| 81 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 82 | |
| 83 | // Version id is the last item in the path |
| 84 | auto pos = path.rfind("/"); |
| 85 | if (pos == std::string::npos) |
| 86 | { |
| 87 | log<level::ERR>("No version id found in object path", |
| 88 | entry("OBJPATH=%s", path)); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 89 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | auto versionId = path.substr(pos + 1); |
| 93 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 94 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 95 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 96 | // Determine the Activation state by processing the given image dir. |
| 97 | auto activationState = server::Activation::Activations::Invalid; |
| 98 | ItemUpdater::ActivationStatus result = ItemUpdater:: |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 99 | validateSquashFSImage(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 100 | if (result == ItemUpdater::ActivationStatus::ready) |
| 101 | { |
| 102 | activationState = server::Activation::Activations::Ready; |
| 103 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 104 | activations.insert(std::make_pair( |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 105 | versionId, |
| 106 | std::make_unique<Activation>( |
| 107 | bus, |
| 108 | path, |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 109 | *this, |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 110 | versionId, |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 111 | activationState))); |
| 112 | versions.insert(std::make_pair( |
| 113 | versionId, |
| 114 | std::make_unique<phosphor::software:: |
| 115 | manager::Version>( |
| 116 | bus, |
| 117 | path, |
| 118 | version, |
| 119 | purpose, |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 120 | filePath, |
| 121 | std::bind(&ItemUpdater::erase, |
| 122 | this, |
| 123 | std::placeholders::_1)))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 124 | } |
Saqib Khan | 7b5010f | 2017-08-09 10:03:11 -0500 | [diff] [blame^] | 125 | else |
| 126 | { |
| 127 | log<level::INFO>("Software Object with the same version already exists", |
| 128 | entry("VERSION_ID=%s", versionId)); |
| 129 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 130 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 133 | void ItemUpdater::processBMCImage() |
| 134 | { |
| 135 | auto purpose = server::Version::VersionPurpose::BMC; |
| 136 | auto version = phosphor::software::manager::Version::getBMCVersion(); |
| 137 | auto id = phosphor::software::manager::Version::getId(version); |
| 138 | auto path = std::string{SOFTWARE_OBJPATH} + '/' + id; |
| 139 | activations.insert(std::make_pair( |
| 140 | id, |
| 141 | std::make_unique<Activation>( |
| 142 | bus, |
| 143 | path, |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 144 | *this, |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 145 | id, |
| 146 | server::Activation::Activations::Active))); |
| 147 | versions.insert(std::make_pair( |
| 148 | id, |
| 149 | std::make_unique<phosphor::software:: |
| 150 | manager::Version>( |
| 151 | bus, |
| 152 | path, |
| 153 | version, |
| 154 | purpose, |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 155 | "", |
| 156 | std::bind(&ItemUpdater::erase, |
| 157 | this, |
| 158 | std::placeholders::_1)))); |
| 159 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 163 | void ItemUpdater::erase(std::string entryId) |
| 164 | { |
| 165 | // Delete ReadWrite and ReadOnly partitions |
| 166 | removeReadWritePartition(entryId); |
| 167 | removeReadOnlyPartition(entryId); |
| 168 | |
| 169 | // Removing entry in versions map |
| 170 | auto it = versions.find(entryId); |
| 171 | if (it == versions.end()) |
| 172 | { |
| 173 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
| 174 | " in item updater versions map." \ |
| 175 | " Unable to remove.").c_str()); |
| 176 | return; |
| 177 | } |
| 178 | this->versions.erase(entryId); |
| 179 | |
| 180 | // Removing entry in activations map |
| 181 | auto ita = activations.find(entryId); |
| 182 | if (ita == activations.end()) |
| 183 | { |
| 184 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
| 185 | " in item updater activations map." \ |
| 186 | " Unable to remove.").c_str()); |
| 187 | return; |
| 188 | } |
| 189 | // TODO: openbmc/openbmc#1986 |
| 190 | // Test if this is the currently running image |
| 191 | // If not, don't continue. |
| 192 | |
| 193 | this->activations.erase(entryId); |
| 194 | } |
| 195 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 196 | ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage( |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 197 | const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 198 | { |
| 199 | |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 200 | fs::path file(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 201 | file /= bmcImage; |
| 202 | std::ifstream efile(file.c_str()); |
| 203 | |
| 204 | if (efile.good() == 1) |
| 205 | { |
| 206 | return ItemUpdater::ActivationStatus::ready; |
| 207 | } |
| 208 | else |
| 209 | { |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 210 | log<level::ERR>("Failed to find the BMC image."); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 211 | return ItemUpdater::ActivationStatus::invalid; |
| 212 | } |
| 213 | } |
| 214 | |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 215 | void ItemUpdater::freePriority(uint8_t value) |
| 216 | { |
| 217 | //TODO openbmc/openbmc#1896 Improve the performance of this function |
| 218 | for (const auto& intf : activations) |
| 219 | { |
| 220 | if(intf.second->redundancyPriority) |
| 221 | { |
| 222 | if (intf.second->redundancyPriority.get()->priority() == value) |
| 223 | { |
| 224 | intf.second->redundancyPriority.get()->priority(value+1); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 230 | void ItemUpdater::reset() |
| 231 | { |
| 232 | // Mark the read-write partition for recreation upon reboot. |
| 233 | auto method = bus.new_method_call( |
| 234 | SYSTEMD_BUSNAME, |
| 235 | SYSTEMD_PATH, |
| 236 | SYSTEMD_INTERFACE, |
| 237 | "StartUnit"); |
| 238 | method.append("obmc-flash-bmc-setenv@rwreset=true.service", "replace"); |
| 239 | bus.call_noreply(method); |
| 240 | |
| 241 | log<level::INFO>("BMC factory reset will take effect upon reboot."); |
| 242 | |
| 243 | return; |
| 244 | } |
| 245 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 246 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
| 247 | { |
| 248 | auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + |
| 249 | ".service"; |
| 250 | |
| 251 | // Remove the read-only partitions. |
| 252 | auto method = bus.new_method_call( |
| 253 | SYSTEMD_BUSNAME, |
| 254 | SYSTEMD_PATH, |
| 255 | SYSTEMD_INTERFACE, |
| 256 | "StartUnit"); |
| 257 | method.append(serviceFile, "replace"); |
| 258 | bus.call_noreply(method); |
| 259 | } |
| 260 | |
| 261 | void ItemUpdater::removeReadWritePartition(std::string versionId) |
| 262 | { |
| 263 | auto serviceFile = "obmc-flash-bmc-ubirw-remove.service"; |
| 264 | |
| 265 | // Remove the read-write partitions. |
| 266 | auto method = bus.new_method_call( |
| 267 | SYSTEMD_BUSNAME, |
| 268 | SYSTEMD_PATH, |
| 269 | SYSTEMD_INTERFACE, |
| 270 | "StartUnit"); |
| 271 | method.append(serviceFile, "replace"); |
| 272 | bus.call_noreply(method); |
| 273 | } |
| 274 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 275 | } // namespace updater |
| 276 | } // namespace software |
| 277 | } // namespace phosphor |