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