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