blob: 13b42cc160c4ac929d3f3c9f2452125d308d40e3 [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
7#include <cassert>
8#include <filesystem>
Lei YUd0bbfa92019-09-11 16:10:54 +08009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
Lei YU12c9f4c2019-09-11 15:08:15 +080011
Lei YU01539e72019-07-31 10:57:38 +080012namespace phosphor
13{
14namespace software
15{
16namespace updater
17{
18
Lei YU12c9f4c2019-09-11 15:08:15 +080019constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1";
20constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1";
21constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
22
23namespace fs = std::filesystem;
Lei YU01539e72019-07-31 10:57:38 +080024namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
25
Lei YUd0bbfa92019-09-11 16:10:54 +080026using namespace phosphor::logging;
27using sdbusplus::exception::SdBusError;
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
64void Activation::unitStateChange(sdbusplus::message::message& msg)
65{
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 }
99 catch (const SdBusError& e)
100 {
101 log<level::ERR>("Error staring service", entry("ERROR=%s", e.what()));
102 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
142 log<level::ERR>("Failed to udpate PSU",
143 entry("PSU=%s", psuQueue.front().c_str()));
144 std::queue<std::string>().swap(psuQueue); // Clear the queue
145 activation(Status::Failed);
146}
147
148Activation::Status Activation::startActivation()
Lei YU12c9f4c2019-09-11 15:08:15 +0800149{
Lei YU63f9e712019-10-12 15:16:55 +0800150 // Check if the activation has file path
151 if (path().empty())
152 {
153 log<level::WARNING>("No image for the activation, skipped",
154 entry("VERSION_ID=%s", versionId.c_str()));
155 return activation(); // Return the previous activation status
156 }
Lei YU81c67722019-09-11 16:47:29 +0800157
Lei YU12c9f4c2019-09-11 15:08:15 +0800158 auto psuPaths = utils::getPSUInventoryPath(bus);
159 if (psuPaths.empty())
160 {
Lei YUff83c2a2019-09-12 13:55:18 +0800161 log<level::WARNING>("No PSU inventory found");
162 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 {
171 log<level::NOTICE>("PSU already running the image, skipping",
172 entry("PSU=%s", p.c_str()));
173 continue;
174 }
Lei YU9edb7332019-09-19 14:46:19 +0800175 psuQueue.push(p);
176 }
177 else
178 {
179 log<level::NOTICE>("PSU not compatible",
180 entry("PSU=%s", p.c_str()));
181 }
182 }
183
184 if (psuQueue.empty())
185 {
Lei YU63f9e712019-10-12 15:16:55 +0800186 log<level::WARNING>("No PSU compatible with the software");
187 return activation(); // Return the previous activation status
Lei YUff83c2a2019-09-12 13:55:18 +0800188 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800189
Lei YU8afeee52019-10-21 15:25:35 +0800190 if (!activationProgress)
191 {
192 activationProgress = std::make_unique<ActivationProgress>(bus, objPath);
193 }
194 if (!activationBlocksTransition)
195 {
196 activationBlocksTransition =
197 std::make_unique<ActivationBlocksTransition>(bus, objPath);
198 }
199
Lei YUff83c2a2019-09-12 13:55:18 +0800200 // The progress to be increased for each successful update of PSU
201 // E.g. in case we have 4 PSUs:
202 // 1. Initial progrss is 10
203 // 2. Add 20 after each update is done, so we will see progress to be 30,
204 // 50, 70, 90
205 // 3. When all PSUs are updated, it will be 100 and the interface is
206 // removed.
207 progressStep = 80 / psuQueue.size();
208 if (doUpdate())
209 {
210 activationProgress->progress(10);
211 return Status::Activating;
212 }
213 else
214 {
215 return Status::Failed;
216 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800217}
218
219void Activation::finishActivation()
220{
Lei YU2e0e2de2019-09-26 16:42:23 +0800221 storeImage();
Lei YU90c8a8b2019-09-11 17:20:03 +0800222 activationProgress->progress(100);
Lei YU81c67722019-09-11 16:47:29 +0800223
Lei YUd0bbfa92019-09-11 16:10:54 +0800224 deleteImageManagerObject();
Lei YU7f2a2152019-09-16 16:50:18 +0800225
Lei YU99301372019-09-29 16:27:12 +0800226 associationInterface->createActiveAssociation(objPath);
227 associationInterface->addFunctionalAssociation(objPath);
Lei YUa8b966f2020-03-18 10:32:24 +0800228 associationInterface->addUpdateableAssociation(objPath);
Lei YU7f2a2152019-09-16 16:50:18 +0800229
Lei YU1517f5f2019-10-14 16:44:42 +0800230 // Reset RequestedActivations to none so that it could be activated in
231 // future
232 requestedActivation(SoftwareActivation::RequestedActivations::None);
Lei YU12c9f4c2019-09-11 15:08:15 +0800233 activation(Status::Active);
Lei YU01539e72019-07-31 10:57:38 +0800234}
235
Lei YUd0bbfa92019-09-11 16:10:54 +0800236void Activation::deleteImageManagerObject()
237{
238 // Get the Delete object for <versionID> inside image_manager
239 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
240 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
241 std::string versionService;
Lei YU99301372019-09-29 16:27:12 +0800242 auto services = utils::getServices(bus, objPath.c_str(), deleteInterface);
Lei YUd0bbfa92019-09-11 16:10:54 +0800243
244 // We need to find the phosphor-version-software-manager's version service
245 // to invoke the delete interface
246 for (const auto& service : services)
247 {
248 if (service.find(versionServiceStr) != std::string::npos)
249 {
250 versionService = service;
251 break;
252 }
253 }
254 if (versionService.empty())
255 {
Lei YUe8945ea2019-09-29 17:25:31 +0800256 // When updating a stored image, there is no version object created by
257 // "xyz.openbmc_project.Software.Version" service, so skip it.
Lei YUd0bbfa92019-09-11 16:10:54 +0800258 return;
259 }
260
261 // Call the Delete object for <versionID> inside image_manager
Lei YU99301372019-09-29 16:27:12 +0800262 auto method = bus.new_method_call(versionService.c_str(), objPath.c_str(),
Lei YUd0bbfa92019-09-11 16:10:54 +0800263 deleteInterface, "Delete");
264 try
265 {
266 bus.call(method);
267 }
268 catch (const SdBusError& e)
269 {
270 log<level::ERR>("Error performing call to Delete object path",
271 entry("ERROR=%s", e.what()),
Lei YU99301372019-09-29 16:27:12 +0800272 entry("PATH=%s", objPath.c_str()));
Lei YUd0bbfa92019-09-11 16:10:54 +0800273 }
274}
275
Lei YU9edb7332019-09-19 14:46:19 +0800276bool Activation::isCompatible(const std::string& psuInventoryPath)
277{
278 auto service =
279 utils::getService(bus, psuInventoryPath.c_str(), ASSET_IFACE);
280 auto psuManufacturer = utils::getProperty<std::string>(
281 bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE,
282 MANUFACTURER);
283 auto psuModel = utils::getProperty<std::string>(
284 bus, service.c_str(), psuInventoryPath.c_str(), ASSET_IFACE, MODEL);
285 if (psuModel != model)
286 {
287 // The model shall match
288 return false;
289 }
290 if (!psuManufacturer.empty())
291 {
292 // If PSU inventory has manufacturer property, it shall match
293 return psuManufacturer == manufacturer;
294 }
295 return true;
296}
297
Lei YU2e0e2de2019-09-26 16:42:23 +0800298void Activation::storeImage()
299{
300 // Store image in persistent dir separated by model
301 // and only store the latest one by removing old ones
Lei YUe8945ea2019-09-29 17:25:31 +0800302 auto src = path();
Lei YU2e0e2de2019-09-26 16:42:23 +0800303 auto dst = fs::path(IMG_DIR_PERSIST) / model;
Lei YUe8945ea2019-09-29 17:25:31 +0800304 if (src == dst)
305 {
306 // This happens when updating an stored image, no need to store it again
307 return;
308 }
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 {
318 log<level::ERR>("Error storing PSU image", entry("ERROR=%s", e.what()),
319 entry("SRC=%s", src.c_str()),
320 entry("DST=%s", dst.c_str()));
321 }
322}
323
Lei YUe8945ea2019-09-29 17:25:31 +0800324std::string Activation::getUpdateService(const std::string& psuInventoryPath)
325{
326 fs::path imagePath(path());
327
328 // The systemd unit shall be escaped
329 std::string args = psuInventoryPath;
330 args += "\\x20";
331 args += imagePath;
332 std::replace(args.begin(), args.end(), '/', '-');
333
334 std::string service = PSU_UPDATE_SERVICE;
335 auto p = service.find('@');
336 assert(p != std::string::npos);
337 service.insert(p + 1, args);
338 return service;
339}
340
Lei YU8afeee52019-10-21 15:25:35 +0800341void ActivationBlocksTransition::enableRebootGuard()
342{
343 log<level::INFO>("PSU image activating - BMC reboots are disabled.");
344
345 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
346 SYSTEMD_INTERFACE, "StartUnit");
347 method.append("reboot-guard-enable.service", "replace");
348 bus.call_noreply_noerror(method);
349}
350
351void ActivationBlocksTransition::disableRebootGuard()
352{
353 log<level::INFO>("PSU activation has ended - BMC reboots are re-enabled.");
354
355 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
356 SYSTEMD_INTERFACE, "StartUnit");
357 method.append("reboot-guard-disable.service", "replace");
358 bus.call_noreply_noerror(method);
359}
360
Lei YU01539e72019-07-31 10:57:38 +0800361} // namespace updater
362} // namespace software
363} // namespace phosphor