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 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 9 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 10 | #include <exception> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 11 | #include <filesystem> |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 12 | #include <format> |
| 13 | #include <stdexcept> |
| 14 | #include <vector> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 15 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace software |
| 19 | { |
| 20 | namespace updater |
| 21 | { |
| 22 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 23 | constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1"; |
| 24 | constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1"; |
| 25 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 26 | |
| 27 | namespace fs = std::filesystem; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 28 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Shawn McCarney | 17c2c94 | 2024-12-10 17:32:30 -0600 | [diff] [blame] | 30 | using SoftwareActivation = |
| 31 | sdbusplus::server::xyz::openbmc_project::software::Activation; |
| 32 | using ExtendedVersion = |
| 33 | sdbusplus::server::xyz::openbmc_project::software::ExtendedVersion; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 34 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 35 | auto Activation::activation(Activations value) -> Activations |
| 36 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 37 | if (value == Status::Activating) |
| 38 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 39 | value = startActivation(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 40 | } |
| 41 | else |
| 42 | { |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 43 | activationBlocksTransition.reset(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 44 | activationProgress.reset(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | return SoftwareActivation::activation(value); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | auto Activation::requestedActivation(RequestedActivations value) |
| 51 | -> RequestedActivations |
| 52 | { |
Shawn McCarney | 7cb5d05 | 2025-01-03 18:27:45 -0600 | [diff] [blame^] | 53 | if (value == SoftwareActivation::RequestedActivations::Active) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 54 | { |
Shawn McCarney | 7cb5d05 | 2025-01-03 18:27:45 -0600 | [diff] [blame^] | 55 | if (SoftwareActivation::requestedActivation() != |
| 56 | SoftwareActivation::RequestedActivations::Active) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 57 | { |
Shawn McCarney | 7cb5d05 | 2025-01-03 18:27:45 -0600 | [diff] [blame^] | 58 | // PSU image could be activated even when it's in active, |
| 59 | // e.g. in case a PSU is replaced and has a older image, it will be |
| 60 | // updated with the running PSU image that is stored in BMC. |
| 61 | if ((activation() == Status::Ready) || |
| 62 | (activation() == Status::Failed) || |
| 63 | (activation() == Status::Active)) |
| 64 | { |
| 65 | activation(Status::Activating); |
| 66 | } |
| 67 | } |
| 68 | else if (activation() == Status::Activating) |
| 69 | { |
| 70 | // Activation was requested when one was already in progress. |
| 71 | // Activate again once the current activation is done. New PSU |
| 72 | // information may have been found on D-Bus, or a new PSU may have |
| 73 | // been plugged in. |
| 74 | shouldActivateAgain = true; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | return SoftwareActivation::requestedActivation(value); |
| 78 | } |
| 79 | |
Shawn McCarney | 17c2c94 | 2024-12-10 17:32:30 -0600 | [diff] [blame] | 80 | auto Activation::extendedVersion(std::string value) -> std::string |
| 81 | { |
| 82 | auto info = Version::getExtVersionInfo(value); |
| 83 | manufacturer = info["manufacturer"]; |
| 84 | model = info["model"]; |
| 85 | |
| 86 | return ExtendedVersion::extendedVersion(value); |
| 87 | } |
| 88 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 89 | void Activation::unitStateChange(sdbusplus::message_t& msg) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 90 | { |
| 91 | uint32_t newStateID{}; |
| 92 | sdbusplus::message::object_path newStateObjPath; |
| 93 | std::string newStateUnit{}; |
| 94 | std::string newStateResult{}; |
| 95 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 96 | try |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 97 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 98 | // Read the msg and populate each variable |
| 99 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 100 | |
| 101 | if (newStateUnit == psuUpdateUnit) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 102 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 103 | if (newStateResult == "done") |
| 104 | { |
| 105 | onUpdateDone(); |
| 106 | } |
| 107 | if (newStateResult == "failed" || newStateResult == "dependency") |
| 108 | { |
| 109 | onUpdateFailed(); |
| 110 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 111 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 112 | } |
| 113 | catch (const std::exception& e) |
| 114 | { |
| 115 | lg2::error("Unable to handle unit state change event: {ERROR}", "ERROR", |
| 116 | e); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 120 | bool Activation::doUpdate(const std::string& psuInventoryPath) |
| 121 | { |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 122 | currentUpdatingPsu = psuInventoryPath; |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 123 | try |
| 124 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 125 | psuUpdateUnit = getUpdateService(currentUpdatingPsu); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 126 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 127 | SYSTEMD_INTERFACE, "StartUnit"); |
| 128 | method.append(psuUpdateUnit, "replace"); |
| 129 | bus.call_noreply(method); |
| 130 | return true; |
| 131 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 132 | catch (const std::exception& e) |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 133 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 134 | lg2::error("Error starting update service for PSU {PSU}: {ERROR}", |
| 135 | "PSU", psuInventoryPath, "ERROR", e); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 136 | onUpdateFailed(); |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | bool Activation::doUpdate() |
| 142 | { |
| 143 | // When the queue is empty, all updates are done |
| 144 | if (psuQueue.empty()) |
| 145 | { |
| 146 | finishActivation(); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | // Do the update on a PSU |
| 151 | const auto& psu = psuQueue.front(); |
| 152 | return doUpdate(psu); |
| 153 | } |
| 154 | |
| 155 | void Activation::onUpdateDone() |
| 156 | { |
| 157 | auto progress = activationProgress->progress() + progressStep; |
| 158 | activationProgress->progress(progress); |
| 159 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 160 | // Update the activation association |
| 161 | auto assocs = associations(); |
| 162 | assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 163 | currentUpdatingPsu); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 164 | associations(assocs); |
| 165 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 166 | activationListener->onUpdateDone(versionId, currentUpdatingPsu); |
| 167 | currentUpdatingPsu.clear(); |
| 168 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 169 | psuQueue.pop(); |
| 170 | doUpdate(); // Update the next psu |
| 171 | } |
| 172 | |
| 173 | void Activation::onUpdateFailed() |
| 174 | { |
| 175 | // TODO: report an event |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 176 | lg2::error("Failed to update PSU {PSU}", "PSU", psuQueue.front()); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 177 | std::queue<std::string>().swap(psuQueue); // Clear the queue |
| 178 | activation(Status::Failed); |
Shawn McCarney | 7cb5d05 | 2025-01-03 18:27:45 -0600 | [diff] [blame^] | 179 | shouldActivateAgain = false; |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | Activation::Status Activation::startActivation() |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 183 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 184 | // Check if the activation has file path |
| 185 | if (path().empty()) |
| 186 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 187 | lg2::warning( |
| 188 | "No image for the activation, skipped version {VERSION_ID}", |
| 189 | "VERSION_ID", versionId); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 190 | return activation(); // Return the previous activation status |
| 191 | } |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 192 | |
Shawn McCarney | d57bd2f | 2024-12-02 18:40:28 -0600 | [diff] [blame] | 193 | auto psuPaths = utils::getPSUInventoryPaths(bus); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 194 | if (psuPaths.empty()) |
| 195 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 196 | lg2::warning("No PSU inventory found"); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 197 | return Status::Failed; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 200 | for (const auto& p : psuPaths) |
| 201 | { |
Shawn McCarney | 46ea388 | 2024-12-10 11:25:38 -0600 | [diff] [blame] | 202 | if (!isPresent(p)) |
| 203 | { |
| 204 | continue; |
| 205 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 206 | if (isCompatible(p)) |
| 207 | { |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 208 | if (utils::isAssociated(p, associations())) |
| 209 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 210 | lg2::notice("PSU {PSU} is already running the image, skipping", |
| 211 | "PSU", p); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 212 | continue; |
| 213 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 214 | psuQueue.push(p); |
| 215 | } |
| 216 | else |
| 217 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 218 | lg2::notice("PSU {PSU} is not compatible", "PSU", p); |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | if (psuQueue.empty()) |
| 223 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 224 | lg2::warning("No PSU compatible with the software"); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 225 | return activation(); // Return the previous activation status |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 226 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 227 | |
Lei YU | 8afeee5 | 2019-10-21 15:25:35 +0800 | [diff] [blame] | 228 | if (!activationProgress) |
| 229 | { |
| 230 | activationProgress = std::make_unique<ActivationProgress>(bus, objPath); |
| 231 | } |
| 232 | if (!activationBlocksTransition) |
| 233 | { |
| 234 | activationBlocksTransition = |
| 235 | std::make_unique<ActivationBlocksTransition>(bus, objPath); |
| 236 | } |
| 237 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 238 | // The progress to be increased for each successful update of PSU |
| 239 | // E.g. in case we have 4 PSUs: |
Manojkiran Eda | 33cf9f0 | 2024-06-17 14:40:44 +0530 | [diff] [blame] | 240 | // 1. Initial progress is 10 |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 241 | // 2. Add 20 after each update is done, so we will see progress to be 30, |
| 242 | // 50, 70, 90 |
| 243 | // 3. When all PSUs are updated, it will be 100 and the interface is |
| 244 | // removed. |
| 245 | progressStep = 80 / psuQueue.size(); |
| 246 | if (doUpdate()) |
| 247 | { |
| 248 | activationProgress->progress(10); |
| 249 | return Status::Activating; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | return Status::Failed; |
| 254 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void Activation::finishActivation() |
| 258 | { |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 259 | storeImage(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 260 | activationProgress->progress(100); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 261 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 262 | deleteImageManagerObject(); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 263 | |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 264 | associationInterface->createActiveAssociation(objPath); |
| 265 | associationInterface->addFunctionalAssociation(objPath); |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 266 | associationInterface->addUpdateableAssociation(objPath); |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 267 | |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 268 | // Reset RequestedActivations to none so that it could be activated in |
| 269 | // future |
| 270 | requestedActivation(SoftwareActivation::RequestedActivations::None); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 271 | activation(Status::Active); |
Shawn McCarney | 7cb5d05 | 2025-01-03 18:27:45 -0600 | [diff] [blame^] | 272 | |
| 273 | // Automatically restart activation if a request occurred while code update |
| 274 | // was already in progress. New PSU information may have been found on |
| 275 | // D-Bus, or a new PSU may have been plugged in. |
| 276 | if (shouldActivateAgain) |
| 277 | { |
| 278 | shouldActivateAgain = false; |
| 279 | requestedActivation(SoftwareActivation::RequestedActivations::Active); |
| 280 | } |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 283 | void Activation::deleteImageManagerObject() |
| 284 | { |
| 285 | // Get the Delete object for <versionID> inside image_manager |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 286 | std::vector<std::string> services; |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 287 | constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete"; |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 288 | try |
| 289 | { |
| 290 | services = utils::getServices(bus, objPath.c_str(), deleteInterface); |
| 291 | } |
| 292 | catch (const std::exception& e) |
| 293 | { |
| 294 | lg2::error( |
| 295 | "Unable to find services to Delete object path {PATH}: {ERROR}", |
| 296 | "PATH", objPath, "ERROR", e); |
| 297 | } |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 298 | |
| 299 | // We need to find the phosphor-version-software-manager's version service |
| 300 | // to invoke the delete interface |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 301 | constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version"; |
| 302 | std::string versionService; |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 303 | for (const auto& service : services) |
| 304 | { |
| 305 | if (service.find(versionServiceStr) != std::string::npos) |
| 306 | { |
| 307 | versionService = service; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | if (versionService.empty()) |
| 312 | { |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 313 | // When updating a stored image, there is no version object created by |
| 314 | // "xyz.openbmc_project.Software.Version" service, so skip it. |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
| 318 | // Call the Delete object for <versionID> inside image_manager |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 319 | try |
| 320 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 321 | auto method = bus.new_method_call( |
| 322 | versionService.c_str(), objPath.c_str(), deleteInterface, "Delete"); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 323 | bus.call(method); |
| 324 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 325 | catch (const std::exception& e) |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 326 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 327 | lg2::error("Unable to Delete object path {PATH}: {ERROR}", "PATH", |
| 328 | objPath, "ERROR", e); |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Shawn McCarney | 46ea388 | 2024-12-10 11:25:38 -0600 | [diff] [blame] | 332 | bool Activation::isPresent(const std::string& psuInventoryPath) |
| 333 | { |
| 334 | bool isPres{false}; |
| 335 | try |
| 336 | { |
| 337 | auto service = |
| 338 | utils::getService(bus, psuInventoryPath.c_str(), ITEM_IFACE); |
| 339 | isPres = utils::getProperty<bool>(bus, service.c_str(), |
| 340 | psuInventoryPath.c_str(), ITEM_IFACE, |
| 341 | PRESENT); |
| 342 | } |
| 343 | catch (const std::exception& e) |
| 344 | { |
| 345 | // Treat as a warning condition and assume the PSU is missing. The |
| 346 | // D-Bus information might not be available if the PSU is missing. |
| 347 | lg2::warning("Unable to determine if PSU {PSU} is present: {ERROR}", |
| 348 | "PSU", psuInventoryPath, "ERROR", e); |
| 349 | } |
| 350 | return isPres; |
| 351 | } |
| 352 | |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 353 | bool Activation::isCompatible(const std::string& psuInventoryPath) |
| 354 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 355 | bool isCompat{false}; |
| 356 | try |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 357 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 358 | auto service = |
| 359 | utils::getService(bus, psuInventoryPath.c_str(), ASSET_IFACE); |
| 360 | auto psuManufacturer = utils::getProperty<std::string>( |
| 361 | bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE, |
| 362 | MANUFACTURER); |
| 363 | auto psuModel = utils::getModel(psuInventoryPath); |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 364 | // The model shall match |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 365 | if (psuModel == model) |
| 366 | { |
| 367 | // If PSU inventory has manufacturer property, it shall match |
| 368 | if (psuManufacturer.empty() || (psuManufacturer == manufacturer)) |
| 369 | { |
| 370 | isCompat = true; |
| 371 | } |
| 372 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 373 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 374 | catch (const std::exception& e) |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 375 | { |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 376 | lg2::error( |
| 377 | "Unable to determine if PSU {PSU} is compatible with firmware " |
| 378 | "versionId {VERSION_ID}: {ERROR}", |
| 379 | "PSU", psuInventoryPath, "VERSION_ID", versionId, "ERROR", e); |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 380 | } |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 381 | return isCompat; |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 382 | } |
| 383 | |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 384 | void Activation::storeImage() |
| 385 | { |
Shawn McCarney | ddf525f | 2024-09-27 09:27:06 -0500 | [diff] [blame] | 386 | // If image is not in IMG_DIR (temporary storage) then exit. We don't want |
| 387 | // to copy from IMG_DIR_PERSIST or IMG_DIR_BUILTIN. |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 388 | auto src = path(); |
Shawn McCarney | ddf525f | 2024-09-27 09:27:06 -0500 | [diff] [blame] | 389 | if (!src.starts_with(IMG_DIR)) |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 390 | { |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 391 | return; |
| 392 | } |
Shawn McCarney | ddf525f | 2024-09-27 09:27:06 -0500 | [diff] [blame] | 393 | |
| 394 | // Store image in persistent dir separated by model |
| 395 | // and only store the latest one by removing old ones |
| 396 | auto dst = fs::path(IMG_DIR_PERSIST) / model; |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 397 | try |
| 398 | { |
| 399 | fs::remove_all(dst); |
| 400 | fs::create_directories(dst); |
| 401 | fs::copy(src, dst); |
| 402 | path(dst.string()); // Update the FilePath interface |
| 403 | } |
| 404 | catch (const fs::filesystem_error& e) |
| 405 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 406 | lg2::error("Error storing PSU image: src={SRC}, dst={DST}: {ERROR}", |
| 407 | "SRC", src, "DST", dst, "ERROR", e); |
Lei YU | 2e0e2de | 2019-09-26 16:42:23 +0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 411 | std::string Activation::getUpdateService(const std::string& psuInventoryPath) |
| 412 | { |
| 413 | fs::path imagePath(path()); |
| 414 | |
| 415 | // The systemd unit shall be escaped |
| 416 | std::string args = psuInventoryPath; |
| 417 | args += "\\x20"; |
| 418 | args += imagePath; |
| 419 | std::replace(args.begin(), args.end(), '/', '-'); |
| 420 | |
| 421 | std::string service = PSU_UPDATE_SERVICE; |
| 422 | auto p = service.find('@'); |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame] | 423 | if (p == std::string::npos) |
| 424 | { |
| 425 | throw std::runtime_error{std::format( |
| 426 | "Invalid PSU update service name: {}", PSU_UPDATE_SERVICE)}; |
| 427 | } |
Lei YU | e8945ea | 2019-09-29 17:25:31 +0800 | [diff] [blame] | 428 | service.insert(p + 1, args); |
| 429 | return service; |
| 430 | } |
| 431 | |
Lei YU | 8afeee5 | 2019-10-21 15:25:35 +0800 | [diff] [blame] | 432 | void ActivationBlocksTransition::enableRebootGuard() |
| 433 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 434 | lg2::info("PSU image activating - BMC reboots are disabled."); |
Lei YU | 8afeee5 | 2019-10-21 15:25:35 +0800 | [diff] [blame] | 435 | |
| 436 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 437 | SYSTEMD_INTERFACE, "StartUnit"); |
| 438 | method.append("reboot-guard-enable.service", "replace"); |
| 439 | bus.call_noreply_noerror(method); |
| 440 | } |
| 441 | |
| 442 | void ActivationBlocksTransition::disableRebootGuard() |
| 443 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame] | 444 | lg2::info("PSU activation has ended - BMC reboots are re-enabled."); |
Lei YU | 8afeee5 | 2019-10-21 15:25:35 +0800 | [diff] [blame] | 445 | |
| 446 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 447 | SYSTEMD_INTERFACE, "StartUnit"); |
| 448 | method.append("reboot-guard-disable.service", "replace"); |
| 449 | bus.call_noreply_noerror(method); |
| 450 | } |
| 451 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 452 | } // namespace updater |
| 453 | } // namespace software |
| 454 | } // namespace phosphor |