Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 3 | #include "activation.hpp" |
| 4 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
| 6 | |
| 7 | #include <cassert> |
| 8 | #include <filesystem> |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 11 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace updater |
| 17 | { |
| 18 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 19 | constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1"; |
| 20 | constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1"; |
| 21 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 22 | |
| 23 | namespace fs = std::filesystem; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 24 | namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server; |
| 25 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 26 | using namespace phosphor::logging; |
| 27 | using sdbusplus::exception::SdBusError; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 28 | using SoftwareActivation = softwareServer::Activation; |
| 29 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 30 | auto Activation::activation(Activations value) -> Activations |
| 31 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 32 | if (value == Status::Activating) |
| 33 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 34 | value = startActivation(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | { |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 38 | activationBlocksTransition.reset(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 39 | activationProgress.reset(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return SoftwareActivation::activation(value); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | auto Activation::requestedActivation(RequestedActivations value) |
| 46 | -> RequestedActivations |
| 47 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 48 | if ((value == SoftwareActivation::RequestedActivations::Active) && |
| 49 | (SoftwareActivation::requestedActivation() != |
| 50 | SoftwareActivation::RequestedActivations::Active)) |
| 51 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 52 | // PSU image could be activated even when it's in active, |
| 53 | // e.g. in case a PSU is replaced and has a older image, it will be |
| 54 | // updated with the running PSU image that is stored in BMC. |
| 55 | if ((activation() == Status::Ready) || |
| 56 | (activation() == Status::Failed) || activation() == Status::Active) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 57 | { |
| 58 | activation(Status::Activating); |
| 59 | } |
| 60 | } |
| 61 | return SoftwareActivation::requestedActivation(value); |
| 62 | } |
| 63 | |
| 64 | void Activation::unitStateChange(sdbusplus::message::message& msg) |
| 65 | { |
| 66 | uint32_t newStateID{}; |
| 67 | sdbusplus::message::object_path newStateObjPath; |
| 68 | std::string newStateUnit{}; |
| 69 | std::string newStateResult{}; |
| 70 | |
| 71 | // Read the msg and populate each variable |
| 72 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 73 | |
| 74 | if (newStateUnit == psuUpdateUnit) |
| 75 | { |
| 76 | if (newStateResult == "done") |
| 77 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 78 | onUpdateDone(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 79 | } |
| 80 | if (newStateResult == "failed" || newStateResult == "dependency") |
| 81 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 82 | onUpdateFailed(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 87 | bool Activation::doUpdate(const std::string& psuInventoryPath) |
| 88 | { |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 89 | currentUpdatingPsu = psuInventoryPath; |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 90 | psuUpdateUnit = getUpdateService(currentUpdatingPsu); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 91 | try |
| 92 | { |
| 93 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 94 | SYSTEMD_INTERFACE, "StartUnit"); |
| 95 | method.append(psuUpdateUnit, "replace"); |
| 96 | bus.call_noreply(method); |
| 97 | return true; |
| 98 | } |
| 99 | catch (const SdBusError& e) |
| 100 | { |
| 101 | log<level::ERR>("Error staring service", entry("ERROR=%s", e.what())); |
| 102 | onUpdateFailed(); |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool Activation::doUpdate() |
| 108 | { |
| 109 | // When the queue is empty, all updates are done |
| 110 | if (psuQueue.empty()) |
| 111 | { |
| 112 | finishActivation(); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // Do the update on a PSU |
| 117 | const auto& psu = psuQueue.front(); |
| 118 | return doUpdate(psu); |
| 119 | } |
| 120 | |
| 121 | void Activation::onUpdateDone() |
| 122 | { |
| 123 | auto progress = activationProgress->progress() + progressStep; |
| 124 | activationProgress->progress(progress); |
| 125 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 126 | // Update the activation association |
| 127 | auto assocs = associations(); |
| 128 | assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 129 | currentUpdatingPsu); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 130 | associations(assocs); |
| 131 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame^] | 132 | activationListener->onUpdateDone(versionId, currentUpdatingPsu); |
| 133 | currentUpdatingPsu.clear(); |
| 134 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 135 | psuQueue.pop(); |
| 136 | doUpdate(); // Update the next psu |
| 137 | } |
| 138 | |
| 139 | void Activation::onUpdateFailed() |
| 140 | { |
| 141 | // TODO: report an event |
| 142 | log<level::ERR>("Failed to udpate PSU", |
| 143 | entry("PSU=%s", psuQueue.front().c_str())); |
| 144 | std::queue<std::string>().swap(psuQueue); // Clear the queue |
| 145 | activation(Status::Failed); |
| 146 | } |
| 147 | |
| 148 | Activation::Status Activation::startActivation() |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 149 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 150 | // Check if the activation has file path |
| 151 | if (path().empty()) |
| 152 | { |
| 153 | log<level::WARNING>("No image for the activation, skipped", |
| 154 | entry("VERSION_ID=%s", versionId.c_str())); |
| 155 | return activation(); // Return the previous activation status |
| 156 | } |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 157 | if (!activationProgress) |
| 158 | { |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 159 | activationProgress = std::make_unique<ActivationProgress>(bus, objPath); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 160 | } |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 161 | if (!activationBlocksTransition) |
| 162 | { |
| 163 | activationBlocksTransition = |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 164 | std::make_unique<ActivationBlocksTransition>(bus, objPath); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 167 | auto psuPaths = utils::getPSUInventoryPath(bus); |
| 168 | if (psuPaths.empty()) |
| 169 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 170 | log<level::WARNING>("No PSU inventory found"); |
| 171 | return Status::Failed; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 174 | for (const auto& p : psuPaths) |
| 175 | { |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 176 | if (isCompatible(p)) |
| 177 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 178 | if (utils::isAssociated(p, associations())) |
| 179 | { |
| 180 | log<level::NOTICE>("PSU already running the image, skipping", |
| 181 | entry("PSU=%s", p.c_str())); |
| 182 | continue; |
| 183 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 184 | psuQueue.push(p); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | log<level::NOTICE>("PSU not compatible", |
| 189 | entry("PSU=%s", p.c_str())); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (psuQueue.empty()) |
| 194 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 195 | log<level::WARNING>("No PSU compatible with the software"); |
| 196 | return activation(); // Return the previous activation status |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 197 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 198 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 199 | // The progress to be increased for each successful update of PSU |
| 200 | // E.g. in case we have 4 PSUs: |
| 201 | // 1. Initial progrss is 10 |
| 202 | // 2. Add 20 after each update is done, so we will see progress to be 30, |
| 203 | // 50, 70, 90 |
| 204 | // 3. When all PSUs are updated, it will be 100 and the interface is |
| 205 | // removed. |
| 206 | progressStep = 80 / psuQueue.size(); |
| 207 | if (doUpdate()) |
| 208 | { |
| 209 | activationProgress->progress(10); |
| 210 | return Status::Activating; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | return Status::Failed; |
| 215 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void Activation::finishActivation() |
| 219 | { |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 220 | storeImage(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 221 | activationProgress->progress(100); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 222 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 223 | deleteImageManagerObject(); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 224 | |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 225 | associationInterface->createActiveAssociation(objPath); |
| 226 | associationInterface->addFunctionalAssociation(objPath); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 227 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 228 | activation(Status::Active); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 231 | void Activation::deleteImageManagerObject() |
| 232 | { |
| 233 | // Get the Delete object for <versionID> inside image_manager |
| 234 | constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version"; |
| 235 | constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete"; |
| 236 | std::string versionService; |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 237 | auto services = utils::getServices(bus, objPath.c_str(), deleteInterface); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 238 | |
| 239 | // We need to find the phosphor-version-software-manager's version service |
| 240 | // to invoke the delete interface |
| 241 | for (const auto& service : services) |
| 242 | { |
| 243 | if (service.find(versionServiceStr) != std::string::npos) |
| 244 | { |
| 245 | versionService = service; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | if (versionService.empty()) |
| 250 | { |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 251 | // When updating a stored image, there is no version object created by |
| 252 | // "xyz.openbmc_project.Software.Version" service, so skip it. |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Call the Delete object for <versionID> inside image_manager |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 257 | auto method = bus.new_method_call(versionService.c_str(), objPath.c_str(), |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 258 | deleteInterface, "Delete"); |
| 259 | try |
| 260 | { |
| 261 | bus.call(method); |
| 262 | } |
| 263 | catch (const SdBusError& e) |
| 264 | { |
| 265 | log<level::ERR>("Error performing call to Delete object path", |
| 266 | entry("ERROR=%s", e.what()), |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 267 | entry("PATH=%s", objPath.c_str())); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 271 | bool Activation::isCompatible(const std::string& psuInventoryPath) |
| 272 | { |
| 273 | auto service = |
| 274 | utils::getService(bus, psuInventoryPath.c_str(), ASSET_IFACE); |
| 275 | auto psuManufacturer = utils::getProperty<std::string>( |
| 276 | bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE, |
| 277 | MANUFACTURER); |
| 278 | auto psuModel = utils::getProperty<std::string>( |
| 279 | bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE, MODEL); |
| 280 | if (psuModel != model) |
| 281 | { |
| 282 | // The model shall match |
| 283 | return false; |
| 284 | } |
| 285 | if (!psuManufacturer.empty()) |
| 286 | { |
| 287 | // If PSU inventory has manufacturer property, it shall match |
| 288 | return psuManufacturer == manufacturer; |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 293 | void Activation::storeImage() |
| 294 | { |
| 295 | // Store image in persistent dir separated by model |
| 296 | // and only store the latest one by removing old ones |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 297 | auto src = path(); |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 298 | auto dst = fs::path(IMG_DIR_PERSIST) / model; |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 299 | if (src == dst) |
| 300 | { |
| 301 | // This happens when updating an stored image, no need to store it again |
| 302 | return; |
| 303 | } |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 304 | try |
| 305 | { |
| 306 | fs::remove_all(dst); |
| 307 | fs::create_directories(dst); |
| 308 | fs::copy(src, dst); |
| 309 | path(dst.string()); // Update the FilePath interface |
| 310 | } |
| 311 | catch (const fs::filesystem_error& e) |
| 312 | { |
| 313 | log<level::ERR>("Error storing PSU image", entry("ERROR=%s", e.what()), |
| 314 | entry("SRC=%s", src.c_str()), |
| 315 | entry("DST=%s", dst.c_str())); |
| 316 | } |
| 317 | } |
| 318 | |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 319 | std::string Activation::getUpdateService(const std::string& psuInventoryPath) |
| 320 | { |
| 321 | fs::path imagePath(path()); |
| 322 | |
| 323 | // The systemd unit shall be escaped |
| 324 | std::string args = psuInventoryPath; |
| 325 | args += "\\x20"; |
| 326 | args += imagePath; |
| 327 | std::replace(args.begin(), args.end(), '/', '-'); |
| 328 | |
| 329 | std::string service = PSU_UPDATE_SERVICE; |
| 330 | auto p = service.find('@'); |
| 331 | assert(p != std::string::npos); |
| 332 | service.insert(p + 1, args); |
| 333 | return service; |
| 334 | } |
| 335 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 336 | } // namespace updater |
| 337 | } // namespace software |
| 338 | } // namespace phosphor |