blob: c52e1b6b4b51d056e42c7dbdeac8c578ec47bbb3 [file] [log] [blame]
Lei YU12c9f4c2019-09-11 15:08:15 +08001#include "config.h"
2
Lei YU01539e72019-07-31 10:57:38 +08003#include "activation.hpp"
4
Lei YU12c9f4c2019-09-11 15:08:15 +08005#include "utils.hpp"
6
Lei YUd0bbfa92019-09-11 16:10:54 +08007#include <phosphor-logging/elog-errors.hpp>
Shawn McCarneycdf86de2024-11-26 10:02:14 -06008#include <phosphor-logging/lg2.hpp>
Lei YU12c9f4c2019-09-11 15:08:15 +08009
Patrick Williams5670b182023-05-10 07:50:50 -050010#include <cassert>
11#include <filesystem>
12
Lei YU01539e72019-07-31 10:57:38 +080013namespace phosphor
14{
15namespace software
16{
17namespace updater
18{
19
Lei YU12c9f4c2019-09-11 15:08:15 +080020constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1";
21constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1";
22constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
23
24namespace fs = std::filesystem;
Lei YU01539e72019-07-31 10:57:38 +080025namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
26
Lei YUd0bbfa92019-09-11 16:10:54 +080027using namespace phosphor::logging;
Lei YU12c9f4c2019-09-11 15:08:15 +080028using SoftwareActivation = softwareServer::Activation;
29
Lei YU01539e72019-07-31 10:57:38 +080030auto Activation::activation(Activations value) -> Activations
31{
Lei YU12c9f4c2019-09-11 15:08:15 +080032 if (value == Status::Activating)
33 {
Lei YUff83c2a2019-09-12 13:55:18 +080034 value = startActivation();
Lei YU12c9f4c2019-09-11 15:08:15 +080035 }
36 else
37 {
Lei YU81c67722019-09-11 16:47:29 +080038 activationBlocksTransition.reset();
Lei YU90c8a8b2019-09-11 17:20:03 +080039 activationProgress.reset();
Lei YU12c9f4c2019-09-11 15:08:15 +080040 }
41
42 return SoftwareActivation::activation(value);
Lei YU01539e72019-07-31 10:57:38 +080043}
44
45auto Activation::requestedActivation(RequestedActivations value)
46 -> RequestedActivations
47{
Lei YU12c9f4c2019-09-11 15:08:15 +080048 if ((value == SoftwareActivation::RequestedActivations::Active) &&
49 (SoftwareActivation::requestedActivation() !=
50 SoftwareActivation::RequestedActivations::Active))
51 {
Lei YU63f9e712019-10-12 15:16:55 +080052 // 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 YU12c9f4c2019-09-11 15:08:15 +080057 {
58 activation(Status::Activating);
59 }
60 }
61 return SoftwareActivation::requestedActivation(value);
62}
63
Patrick Williams374fae52022-07-22 19:26:55 -050064void Activation::unitStateChange(sdbusplus::message_t& msg)
Lei YU12c9f4c2019-09-11 15:08:15 +080065{
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 YUff83c2a2019-09-12 13:55:18 +080078 onUpdateDone();
Lei YU12c9f4c2019-09-11 15:08:15 +080079 }
80 if (newStateResult == "failed" || newStateResult == "dependency")
81 {
Lei YUff83c2a2019-09-12 13:55:18 +080082 onUpdateFailed();
Lei YU12c9f4c2019-09-11 15:08:15 +080083 }
84 }
85}
86
Lei YUff83c2a2019-09-12 13:55:18 +080087bool Activation::doUpdate(const std::string& psuInventoryPath)
88{
Lei YU7f2a2152019-09-16 16:50:18 +080089 currentUpdatingPsu = psuInventoryPath;
Lei YUe8945ea2019-09-29 17:25:31 +080090 psuUpdateUnit = getUpdateService(currentUpdatingPsu);
Lei YUff83c2a2019-09-12 13:55:18 +080091 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 }
Patrick Williams374fae52022-07-22 19:26:55 -050099 catch (const sdbusplus::exception_t& e)
Lei YUff83c2a2019-09-12 13:55:18 +0800100 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600101 lg2::error("Error starting service: {ERROR}", "ERROR", e);
Lei YUff83c2a2019-09-12 13:55:18 +0800102 onUpdateFailed();
103 return false;
104 }
105}
106
107bool 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
121void Activation::onUpdateDone()
122{
123 auto progress = activationProgress->progress() + progressStep;
124 activationProgress->progress(progress);
125
Lei YU7f2a2152019-09-16 16:50:18 +0800126 // Update the activation association
127 auto assocs = associations();
128 assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
129 currentUpdatingPsu);
Lei YU7f2a2152019-09-16 16:50:18 +0800130 associations(assocs);
131
Lei YUffb36532019-10-15 13:55:24 +0800132 activationListener->onUpdateDone(versionId, currentUpdatingPsu);
133 currentUpdatingPsu.clear();
134
Lei YUff83c2a2019-09-12 13:55:18 +0800135 psuQueue.pop();
136 doUpdate(); // Update the next psu
137}
138
139void Activation::onUpdateFailed()
140{
141 // TODO: report an event
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600142 lg2::error("Failed to update PSU {PSU}", "PSU", psuQueue.front());
Lei YUff83c2a2019-09-12 13:55:18 +0800143 std::queue<std::string>().swap(psuQueue); // Clear the queue
144 activation(Status::Failed);
145}
146
147Activation::Status Activation::startActivation()
Lei YU12c9f4c2019-09-11 15:08:15 +0800148{
Lei YU63f9e712019-10-12 15:16:55 +0800149 // Check if the activation has file path
150 if (path().empty())
151 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600152 lg2::warning(
153 "No image for the activation, skipped version {VERSION_ID}",
154 "VERSION_ID", versionId);
Lei YU63f9e712019-10-12 15:16:55 +0800155 return activation(); // Return the previous activation status
156 }
Lei YU81c67722019-09-11 16:47:29 +0800157
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600158 auto psuPaths = utils::getPSUInventoryPaths(bus);
Lei YU12c9f4c2019-09-11 15:08:15 +0800159 if (psuPaths.empty())
160 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600161 lg2::warning("No PSU inventory found");
Lei YUff83c2a2019-09-12 13:55:18 +0800162 return Status::Failed;
Lei YU12c9f4c2019-09-11 15:08:15 +0800163 }
164
Lei YUff83c2a2019-09-12 13:55:18 +0800165 for (const auto& p : psuPaths)
166 {
Lei YU9edb7332019-09-19 14:46:19 +0800167 if (isCompatible(p))
168 {
Lei YU63f9e712019-10-12 15:16:55 +0800169 if (utils::isAssociated(p, associations()))
170 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600171 lg2::notice("PSU {PSU} is already running the image, skipping",
172 "PSU", p);
Lei YU63f9e712019-10-12 15:16:55 +0800173 continue;
174 }
Lei YU9edb7332019-09-19 14:46:19 +0800175 psuQueue.push(p);
176 }
177 else
178 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600179 lg2::notice("PSU {PSU} is not compatible", "PSU", p);
Lei YU9edb7332019-09-19 14:46:19 +0800180 }
181 }
182
183 if (psuQueue.empty())
184 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600185 lg2::warning("No PSU compatible with the software");
Lei YU63f9e712019-10-12 15:16:55 +0800186 return activation(); // Return the previous activation status
Lei YUff83c2a2019-09-12 13:55:18 +0800187 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800188
Lei YU8afeee52019-10-21 15:25:35 +0800189 if (!activationProgress)
190 {
191 activationProgress = std::make_unique<ActivationProgress>(bus, objPath);
192 }
193 if (!activationBlocksTransition)
194 {
195 activationBlocksTransition =
196 std::make_unique<ActivationBlocksTransition>(bus, objPath);
197 }
198
Lei YUff83c2a2019-09-12 13:55:18 +0800199 // The progress to be increased for each successful update of PSU
200 // E.g. in case we have 4 PSUs:
Manojkiran Eda33cf9f02024-06-17 14:40:44 +0530201 // 1. Initial progress is 10
Lei YUff83c2a2019-09-12 13:55:18 +0800202 // 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 YU12c9f4c2019-09-11 15:08:15 +0800216}
217
218void Activation::finishActivation()
219{
Lei YU2e0e2de2019-09-26 16:42:23 +0800220 storeImage();
Lei YU90c8a8b2019-09-11 17:20:03 +0800221 activationProgress->progress(100);
Lei YU81c67722019-09-11 16:47:29 +0800222
Lei YUd0bbfa92019-09-11 16:10:54 +0800223 deleteImageManagerObject();
Lei YU7f2a2152019-09-16 16:50:18 +0800224
Lei YU99301372019-09-29 16:27:12 +0800225 associationInterface->createActiveAssociation(objPath);
226 associationInterface->addFunctionalAssociation(objPath);
Lei YUa8b966f2020-03-18 10:32:24 +0800227 associationInterface->addUpdateableAssociation(objPath);
Lei YU7f2a2152019-09-16 16:50:18 +0800228
Lei YU1517f5f2019-10-14 16:44:42 +0800229 // Reset RequestedActivations to none so that it could be activated in
230 // future
231 requestedActivation(SoftwareActivation::RequestedActivations::None);
Lei YU12c9f4c2019-09-11 15:08:15 +0800232 activation(Status::Active);
Lei YU01539e72019-07-31 10:57:38 +0800233}
234
Lei YUd0bbfa92019-09-11 16:10:54 +0800235void Activation::deleteImageManagerObject()
236{
237 // Get the Delete object for <versionID> inside image_manager
238 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
239 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
240 std::string versionService;
Lei YU99301372019-09-29 16:27:12 +0800241 auto services = utils::getServices(bus, objPath.c_str(), deleteInterface);
Lei YUd0bbfa92019-09-11 16:10:54 +0800242
243 // We need to find the phosphor-version-software-manager's version service
244 // to invoke the delete interface
245 for (const auto& service : services)
246 {
247 if (service.find(versionServiceStr) != std::string::npos)
248 {
249 versionService = service;
250 break;
251 }
252 }
253 if (versionService.empty())
254 {
Lei YUe8945ea2019-09-29 17:25:31 +0800255 // When updating a stored image, there is no version object created by
256 // "xyz.openbmc_project.Software.Version" service, so skip it.
Lei YUd0bbfa92019-09-11 16:10:54 +0800257 return;
258 }
259
260 // Call the Delete object for <versionID> inside image_manager
Lei YU99301372019-09-29 16:27:12 +0800261 auto method = bus.new_method_call(versionService.c_str(), objPath.c_str(),
Lei YUd0bbfa92019-09-11 16:10:54 +0800262 deleteInterface, "Delete");
263 try
264 {
265 bus.call(method);
266 }
Patrick Williams374fae52022-07-22 19:26:55 -0500267 catch (const sdbusplus::exception_t& e)
Lei YUd0bbfa92019-09-11 16:10:54 +0800268 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600269 lg2::error(
270 "Error performing call to Delete object path {PATH}: {ERROR}",
271 "PATH", objPath, "ERROR", e);
Lei YUd0bbfa92019-09-11 16:10:54 +0800272 }
273}
274
Lei YU9edb7332019-09-19 14:46:19 +0800275bool Activation::isCompatible(const std::string& psuInventoryPath)
276{
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400277 auto service =
278 utils::getService(bus, psuInventoryPath.c_str(), ASSET_IFACE);
Lei YU9edb7332019-09-19 14:46:19 +0800279 auto psuManufacturer = utils::getProperty<std::string>(
280 bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE,
281 MANUFACTURER);
Shawn McCarney783406e2024-11-17 21:49:37 -0600282 auto psuModel = utils::getModel(psuInventoryPath);
Lei YU9edb7332019-09-19 14:46:19 +0800283 if (psuModel != model)
284 {
285 // The model shall match
286 return false;
287 }
288 if (!psuManufacturer.empty())
289 {
290 // If PSU inventory has manufacturer property, it shall match
291 return psuManufacturer == manufacturer;
292 }
293 return true;
294}
295
Lei YU2e0e2de2019-09-26 16:42:23 +0800296void Activation::storeImage()
297{
Shawn McCarneyddf525f2024-09-27 09:27:06 -0500298 // If image is not in IMG_DIR (temporary storage) then exit. We don't want
299 // to copy from IMG_DIR_PERSIST or IMG_DIR_BUILTIN.
Lei YUe8945ea2019-09-29 17:25:31 +0800300 auto src = path();
Shawn McCarneyddf525f2024-09-27 09:27:06 -0500301 if (!src.starts_with(IMG_DIR))
Lei YUe8945ea2019-09-29 17:25:31 +0800302 {
Lei YUe8945ea2019-09-29 17:25:31 +0800303 return;
304 }
Shawn McCarneyddf525f2024-09-27 09:27:06 -0500305
306 // Store image in persistent dir separated by model
307 // and only store the latest one by removing old ones
308 auto dst = fs::path(IMG_DIR_PERSIST) / model;
Lei YU2e0e2de2019-09-26 16:42:23 +0800309 try
310 {
311 fs::remove_all(dst);
312 fs::create_directories(dst);
313 fs::copy(src, dst);
314 path(dst.string()); // Update the FilePath interface
315 }
316 catch (const fs::filesystem_error& e)
317 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600318 lg2::error("Error storing PSU image: src={SRC}, dst={DST}: {ERROR}",
319 "SRC", src, "DST", dst, "ERROR", e);
Lei YU2e0e2de2019-09-26 16:42:23 +0800320 }
321}
322
Lei YUe8945ea2019-09-29 17:25:31 +0800323std::string Activation::getUpdateService(const std::string& psuInventoryPath)
324{
325 fs::path imagePath(path());
326
327 // The systemd unit shall be escaped
328 std::string args = psuInventoryPath;
329 args += "\\x20";
330 args += imagePath;
331 std::replace(args.begin(), args.end(), '/', '-');
332
333 std::string service = PSU_UPDATE_SERVICE;
334 auto p = service.find('@');
335 assert(p != std::string::npos);
336 service.insert(p + 1, args);
337 return service;
338}
339
Lei YU8afeee52019-10-21 15:25:35 +0800340void ActivationBlocksTransition::enableRebootGuard()
341{
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600342 lg2::info("PSU image activating - BMC reboots are disabled.");
Lei YU8afeee52019-10-21 15:25:35 +0800343
344 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
345 SYSTEMD_INTERFACE, "StartUnit");
346 method.append("reboot-guard-enable.service", "replace");
347 bus.call_noreply_noerror(method);
348}
349
350void ActivationBlocksTransition::disableRebootGuard()
351{
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600352 lg2::info("PSU activation has ended - BMC reboots are re-enabled.");
Lei YU8afeee52019-10-21 15:25:35 +0800353
354 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
355 SYSTEMD_INTERFACE, "StartUnit");
356 method.append("reboot-guard-disable.service", "replace");
357 bus.call_noreply_noerror(method);
358}
359
Lei YU01539e72019-07-31 10:57:38 +0800360} // namespace updater
361} // namespace software
362} // namespace phosphor