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