blob: 8e664b892654835349c6bbf80da1defe973f1b64 [file] [log] [blame]
Lei YU01539e72019-07-31 10:57:38 +08001#include "config.h"
2
3#include "item_updater.hpp"
4
Lei YUad90ad52019-08-06 11:19:28 +08005#include "utils.hpp"
6
Lei YU01539e72019-07-31 10:57:38 +08007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
Patrick Williams5670b182023-05-10 07:50:50 -050011#include <cassert>
12#include <filesystem>
13
Lei YUfda15a32019-09-19 14:43:02 +080014namespace
15{
Lei YU58c26e32019-09-27 17:52:06 +080016constexpr auto MANIFEST_VERSION = "version";
17constexpr auto MANIFEST_EXTENDED_VERSION = "extended_version";
Faisal Awada760053d2024-05-16 13:31:32 -050018constexpr auto TIMEOUT = 10;
Lei YU58c26e32019-09-27 17:52:06 +080019} // namespace
Lei YUfda15a32019-09-19 14:43:02 +080020
Lei YU01539e72019-07-31 10:57:38 +080021namespace phosphor
22{
23namespace software
24{
25namespace updater
26{
27namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Lei YU01539e72019-07-31 10:57:38 +080028
29using namespace sdbusplus::xyz::openbmc_project::Common::Error;
30using namespace phosphor::logging;
Lei YUad90ad52019-08-06 11:19:28 +080031using SVersion = server::Version;
32using VersionPurpose = SVersion::VersionPurpose;
Lei YU01539e72019-07-31 10:57:38 +080033
Patrick Williams374fae52022-07-22 19:26:55 -050034void ItemUpdater::createActivation(sdbusplus::message_t& m)
Lei YU01539e72019-07-31 10:57:38 +080035{
Lei YU01539e72019-07-31 10:57:38 +080036 sdbusplus::message::object_path objPath;
George Liude270292020-06-12 17:14:17 +080037 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Lei YU01539e72019-07-31 10:57:38 +080038 interfaces;
39 m.read(objPath, interfaces);
40
41 std::string path(std::move(objPath));
42 std::string filePath;
43 auto purpose = VersionPurpose::Unknown;
44 std::string version;
45
46 for (const auto& [interfaceName, propertyMap] : interfaces)
47 {
48 if (interfaceName == VERSION_IFACE)
49 {
50 for (const auto& [propertyName, propertyValue] : propertyMap)
51 {
52 if (propertyName == "Purpose")
53 {
54 // Only process the PSU images
55 auto value = SVersion::convertVersionPurposeFromString(
Lei YU1517f5f2019-10-14 16:44:42 +080056 std::get<std::string>(propertyValue));
Lei YU01539e72019-07-31 10:57:38 +080057
58 if (value == VersionPurpose::PSU)
59 {
60 purpose = value;
61 }
62 }
Lei YUf77189f2019-08-07 14:26:30 +080063 else if (propertyName == VERSION)
Lei YU01539e72019-07-31 10:57:38 +080064 {
Lei YU1517f5f2019-10-14 16:44:42 +080065 version = std::get<std::string>(propertyValue);
Lei YU01539e72019-07-31 10:57:38 +080066 }
67 }
68 }
69 else if (interfaceName == FILEPATH_IFACE)
70 {
71 const auto& it = propertyMap.find("Path");
72 if (it != propertyMap.end())
73 {
Lei YU1517f5f2019-10-14 16:44:42 +080074 filePath = std::get<std::string>(it->second);
Lei YU01539e72019-07-31 10:57:38 +080075 }
76 }
77 }
78 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
79 {
80 return;
81 }
82
83 // Version id is the last item in the path
George Liua0f2cf72024-08-23 14:50:12 +080084 auto pos = path.rfind('/');
Lei YU01539e72019-07-31 10:57:38 +080085 if (pos == std::string::npos)
86 {
87 log<level::ERR>("No version id found in object path",
88 entry("OBJPATH=%s", path.c_str()));
89 return;
90 }
91
92 auto versionId = path.substr(pos + 1);
93
94 if (activations.find(versionId) == activations.end())
95 {
96 // Determine the Activation state by processing the given image dir.
Lei YU91029442019-08-01 15:57:31 +080097 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +080098 auto activationState = Activation::Status::Ready;
Lei YU01539e72019-07-31 10:57:38 +080099
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400100 associations.emplace_back(std::make_tuple(
101 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
102 PSU_INVENTORY_PATH_BASE));
Lei YU91029442019-08-01 15:57:31 +0800103
Lei YU01539e72019-07-31 10:57:38 +0800104 fs::path manifestPath(filePath);
105 manifestPath /= MANIFEST_FILE;
Lei YU58c26e32019-09-27 17:52:06 +0800106 std::string extendedVersion =
107 Version::getValue(manifestPath, {MANIFEST_EXTENDED_VERSION});
Lei YU01539e72019-07-31 10:57:38 +0800108
Lei YU99301372019-09-29 16:27:12 +0800109 auto activation =
110 createActivationObject(path, versionId, extendedVersion,
111 activationState, associations, filePath);
Lei YU01539e72019-07-31 10:57:38 +0800112 activations.emplace(versionId, std::move(activation));
113
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400114 auto versionPtr =
115 createVersionObject(path, versionId, version, purpose);
Lei YU01539e72019-07-31 10:57:38 +0800116 versions.emplace(versionId, std::move(versionPtr));
117 }
118 return;
119}
120
Lei YUa5c47bb2019-09-29 11:28:53 +0800121void ItemUpdater::erase(const std::string& versionId)
Lei YU01539e72019-07-31 10:57:38 +0800122{
123 auto it = versions.find(versionId);
124 if (it == versions.end())
125 {
126 log<level::ERR>(("Error: Failed to find version " + versionId +
127 " in item updater versions map."
128 " Unable to remove.")
129 .c_str());
130 }
131 else
132 {
Lei YU1517f5f2019-10-14 16:44:42 +0800133 versionStrings.erase(it->second->getVersionString());
134 versions.erase(it);
Lei YU01539e72019-07-31 10:57:38 +0800135 }
136
137 // Removing entry in activations map
138 auto ita = activations.find(versionId);
139 if (ita == activations.end())
140 {
141 log<level::ERR>(("Error: Failed to find version " + versionId +
142 " in item updater activations map."
143 " Unable to remove.")
144 .c_str());
145 }
146 else
147 {
148 activations.erase(versionId);
149 }
150}
151
Lei YU91029442019-08-01 15:57:31 +0800152void ItemUpdater::createActiveAssociation(const std::string& path)
153{
154 assocs.emplace_back(
155 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
156 associations(assocs);
157}
158
Lei YUad90ad52019-08-06 11:19:28 +0800159void ItemUpdater::addFunctionalAssociation(const std::string& path)
Lei YU91029442019-08-01 15:57:31 +0800160{
Lei YU91029442019-08-01 15:57:31 +0800161 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
162 FUNCTIONAL_REV_ASSOCIATION, path));
163 associations(assocs);
164}
165
Lei YUa8b966f2020-03-18 10:32:24 +0800166void ItemUpdater::addUpdateableAssociation(const std::string& path)
167{
168 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
169 UPDATEABLE_REV_ASSOCIATION, path));
170 associations(assocs);
171}
172
Lei YU91029442019-08-01 15:57:31 +0800173void ItemUpdater::removeAssociation(const std::string& path)
174{
175 for (auto iter = assocs.begin(); iter != assocs.end();)
176 {
George Liua5205e42024-08-23 15:27:54 +0800177 if ((std::get<2>(*iter)) == path)
Lei YU91029442019-08-01 15:57:31 +0800178 {
179 iter = assocs.erase(iter);
180 associations(assocs);
181 }
182 else
183 {
184 ++iter;
185 }
186 }
187}
188
Lei YUffb36532019-10-15 13:55:24 +0800189void ItemUpdater::onUpdateDone(const std::string& versionId,
190 const std::string& psuInventoryPath)
191{
192 // After update is done, remove old activation objects
193 for (auto it = activations.begin(); it != activations.end(); ++it)
194 {
195 if (it->second->getVersionId() != versionId &&
196 utils::isAssociated(psuInventoryPath, it->second->associations()))
197 {
198 removePsuObject(psuInventoryPath);
199 break;
200 }
201 }
Lei YU1517f5f2019-10-14 16:44:42 +0800202
203 auto it = activations.find(versionId);
204 assert(it != activations.end());
205 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUffb36532019-10-15 13:55:24 +0800206}
207
Lei YU01539e72019-07-31 10:57:38 +0800208std::unique_ptr<Activation> ItemUpdater::createActivationObject(
209 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800210 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800211 const AssociationList& assocs, const std::string& filePath)
Lei YU01539e72019-07-31 10:57:38 +0800212{
213 return std::make_unique<Activation>(bus, path, versionId, extVersion,
Lei YUffb36532019-10-15 13:55:24 +0800214 activationStatus, assocs, filePath,
215 this, this);
Lei YU01539e72019-07-31 10:57:38 +0800216}
217
Lei YUad90ad52019-08-06 11:19:28 +0800218void ItemUpdater::createPsuObject(const std::string& psuInventoryPath,
219 const std::string& psuVersion)
220{
221 auto versionId = utils::getVersionId(psuVersion);
222 auto path = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
223
224 auto it = activations.find(versionId);
225 if (it != activations.end())
226 {
227 // The versionId is already created, associate the path
228 auto associations = it->second->associations();
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400229 associations.emplace_back(
230 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
231 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800232 it->second->associations(associations);
Lei YUbd3b0072019-08-08 13:09:50 +0800233 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUad90ad52019-08-06 11:19:28 +0800234 }
235 else
236 {
237 // Create a new object for running PSU inventory
238 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800239 auto activationState = Activation::Status::Active;
Lei YUad90ad52019-08-06 11:19:28 +0800240
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400241 associations.emplace_back(
242 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
243 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800244
Lei YU99301372019-09-29 16:27:12 +0800245 auto activation = createActivationObject(
246 path, versionId, "", activationState, associations, "");
Lei YUad90ad52019-08-06 11:19:28 +0800247 activations.emplace(versionId, std::move(activation));
Lei YUbd3b0072019-08-08 13:09:50 +0800248 psuPathActivationMap.emplace(psuInventoryPath, activations[versionId]);
Lei YUad90ad52019-08-06 11:19:28 +0800249
250 auto versionPtr = createVersionObject(path, versionId, psuVersion,
Lei YU99301372019-09-29 16:27:12 +0800251 VersionPurpose::PSU);
Lei YUad90ad52019-08-06 11:19:28 +0800252 versions.emplace(versionId, std::move(versionPtr));
253
254 createActiveAssociation(path);
255 addFunctionalAssociation(path);
Lei YUa8b966f2020-03-18 10:32:24 +0800256 addUpdateableAssociation(path);
Lei YUad90ad52019-08-06 11:19:28 +0800257 }
258}
259
Lei YUbd3b0072019-08-08 13:09:50 +0800260void ItemUpdater::removePsuObject(const std::string& psuInventoryPath)
261{
Lei YU1517f5f2019-10-14 16:44:42 +0800262 psuStatusMap[psuInventoryPath] = {false, ""};
263
Lei YUbd3b0072019-08-08 13:09:50 +0800264 auto it = psuPathActivationMap.find(psuInventoryPath);
265 if (it == psuPathActivationMap.end())
266 {
267 log<level::ERR>("No Activation found for PSU",
268 entry("PSUPATH=%s", psuInventoryPath.c_str()));
269 return;
270 }
271 const auto& activationPtr = it->second;
272 psuPathActivationMap.erase(psuInventoryPath);
273
274 auto associations = activationPtr->associations();
275 for (auto iter = associations.begin(); iter != associations.end();)
276 {
George Liua5205e42024-08-23 15:27:54 +0800277 if ((std::get<2>(*iter)) == psuInventoryPath)
Lei YUbd3b0072019-08-08 13:09:50 +0800278 {
279 iter = associations.erase(iter);
280 }
281 else
282 {
283 ++iter;
284 }
285 }
286 if (associations.empty())
287 {
288 // Remove the activation
Lei YUa5c47bb2019-09-29 11:28:53 +0800289 erase(activationPtr->getVersionId());
Lei YUbd3b0072019-08-08 13:09:50 +0800290 }
291 else
292 {
293 // Update association
294 activationPtr->associations(associations);
295 }
296}
297
Lei YU01539e72019-07-31 10:57:38 +0800298std::unique_ptr<Version> ItemUpdater::createVersionObject(
299 const std::string& objPath, const std::string& versionId,
300 const std::string& versionString,
301 sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
Lei YU99301372019-09-29 16:27:12 +0800302 versionPurpose)
Lei YU01539e72019-07-31 10:57:38 +0800303{
Lei YU65207482019-10-11 16:39:36 +0800304 versionStrings.insert(versionString);
Lei YU01539e72019-07-31 10:57:38 +0800305 auto version = std::make_unique<Version>(
Lei YU99301372019-09-29 16:27:12 +0800306 bus, objPath, versionId, versionString, versionPurpose,
Lei YU01539e72019-07-31 10:57:38 +0800307 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
308 return version;
309}
310
Patrick Williams374fae52022-07-22 19:26:55 -0500311void ItemUpdater::onPsuInventoryChangedMsg(sdbusplus::message_t& msg)
Lei YUad90ad52019-08-06 11:19:28 +0800312{
Lei YUbd3b0072019-08-08 13:09:50 +0800313 using Interface = std::string;
Lei YUbd3b0072019-08-08 13:09:50 +0800314 Interface interface;
315 Properties properties;
Lei YUbd3b0072019-08-08 13:09:50 +0800316 std::string psuPath = msg.get_path();
317
318 msg.read(interface, properties);
Lei YUa2c2cd72019-08-09 15:54:10 +0800319 onPsuInventoryChanged(psuPath, properties);
320}
321
322void ItemUpdater::onPsuInventoryChanged(const std::string& psuPath,
323 const Properties& properties)
324{
Lei YU1517f5f2019-10-14 16:44:42 +0800325 std::optional<bool> present;
326 std::optional<std::string> model;
Lei YUbd3b0072019-08-08 13:09:50 +0800327
Lei YU1517f5f2019-10-14 16:44:42 +0800328 // The code was expecting to get callback on multiple properties changed.
329 // But in practice, the callback is received one-by-one for each property.
330 // So it has to handle Present and Version property separately.
Lei YUf77189f2019-08-07 14:26:30 +0800331 auto p = properties.find(PRESENT);
Lei YU1517f5f2019-10-14 16:44:42 +0800332 if (p != properties.end())
333 {
334 present = std::get<bool>(p->second);
335 psuStatusMap[psuPath].present = *present;
336 }
337 p = properties.find(MODEL);
338 if (p != properties.end())
339 {
340 model = std::get<std::string>(p->second);
341 psuStatusMap[psuPath].model = *model;
342 }
343
344 // If present or model is not changed, ignore
345 if (!present.has_value() && !model.has_value())
Lei YUbd3b0072019-08-08 13:09:50 +0800346 {
347 return;
348 }
349
Lei YU1517f5f2019-10-14 16:44:42 +0800350 if (psuStatusMap[psuPath].present)
Lei YUbd3b0072019-08-08 13:09:50 +0800351 {
Lei YU1517f5f2019-10-14 16:44:42 +0800352 // If model is not updated, let's wait for it
353 if (psuStatusMap[psuPath].model.empty())
354 {
355 log<level::DEBUG>("Waiting for model to be updated");
356 return;
357 }
358
359 auto version = utils::getVersion(psuPath);
Lei YUdcaf8932019-09-09 16:09:35 +0800360 if (!version.empty())
Lei YUbd3b0072019-08-08 13:09:50 +0800361 {
Faisal Awada760053d2024-05-16 13:31:32 -0500362 if (psuPathActivationMap.find(psuPath) ==
363 psuPathActivationMap.end())
364 {
365 createPsuObject(psuPath, version);
366 }
Lei YU1517f5f2019-10-14 16:44:42 +0800367 // Check if there is new PSU images to update
368 syncToLatestImage();
369 }
370 else
371 {
372 // TODO: log an event
373 log<level::ERR>("Failed to get PSU version",
374 entry("PSU=%s", psuPath.c_str()));
Lei YUbd3b0072019-08-08 13:09:50 +0800375 }
Lei YUbd3b0072019-08-08 13:09:50 +0800376 }
377 else
378 {
Lei YU1517f5f2019-10-14 16:44:42 +0800379 if (!present.has_value())
380 {
381 // If a PSU is plugged out, model property is update to empty as
382 // well, and we get callback here, but ignore that because it is
383 // handled by "Present" callback.
384 return;
385 }
Lei YUbd3b0072019-08-08 13:09:50 +0800386 // Remove object or association
387 removePsuObject(psuPath);
388 }
Lei YUad90ad52019-08-06 11:19:28 +0800389}
390
391void ItemUpdater::processPSUImage()
392{
393 auto paths = utils::getPSUInventoryPath(bus);
394 for (const auto& p : paths)
395 {
Lei YU5f3584d2019-08-27 16:28:53 +0800396 auto service = utils::getService(bus, p.c_str(), ITEM_IFACE);
Lei YUad90ad52019-08-06 11:19:28 +0800397 auto present = utils::getProperty<bool>(bus, service.c_str(), p.c_str(),
Lei YUf77189f2019-08-07 14:26:30 +0800398 ITEM_IFACE, PRESENT);
Faisal Awada760053d2024-05-16 13:31:32 -0500399 psuStatusMap[p].model = utils::getProperty<std::string>(
400 bus, service.c_str(), p.c_str(), ASSET_IFACE, MODEL);
Lei YU5f3584d2019-08-27 16:28:53 +0800401 auto version = utils::getVersion(p);
Faisal Awada760053d2024-05-16 13:31:32 -0500402 if ((psuPathActivationMap.find(p) == psuPathActivationMap.end()) &&
403 present && !version.empty())
Lei YUad90ad52019-08-06 11:19:28 +0800404 {
405 createPsuObject(p, version);
Faisal Awada760053d2024-05-16 13:31:32 -0500406 // Add matches for PSU Inventory's property changes
407 psuMatches.emplace_back(
408 bus, MatchRules::propertiesChanged(p, ITEM_IFACE),
409 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
410 std::placeholders::_1)); // For present
411 psuMatches.emplace_back(
412 bus, MatchRules::propertiesChanged(p, ASSET_IFACE),
413 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
414 std::placeholders::_1)); // For model
Lei YUad90ad52019-08-06 11:19:28 +0800415 }
Lei YUad90ad52019-08-06 11:19:28 +0800416 }
417}
418
Lei YU58c26e32019-09-27 17:52:06 +0800419void ItemUpdater::processStoredImage()
420{
421 scanDirectory(IMG_DIR_BUILTIN);
Faisal Awada760053d2024-05-16 13:31:32 -0500422
Faisal Awadafb86e792024-09-11 10:51:17 -0500423 if (!ALWAYS_USE_BUILTIN_IMG_DIR)
424 {
425 scanDirectory(IMG_DIR_PERSIST);
426 }
Lei YU58c26e32019-09-27 17:52:06 +0800427}
428
429void ItemUpdater::scanDirectory(const fs::path& dir)
430{
Faisal Awada760053d2024-05-16 13:31:32 -0500431 auto manifest = dir;
432 auto path = dir;
Lei YU58c26e32019-09-27 17:52:06 +0800433 // The directory shall put PSU images in directories named with model
434 if (!fs::exists(dir))
435 {
436 // Skip
437 return;
438 }
439 if (!fs::is_directory(dir))
440 {
441 log<level::ERR>("The path is not a directory",
442 entry("PATH=%s", dir.c_str()));
443 return;
444 }
Faisal Awada760053d2024-05-16 13:31:32 -0500445
446 for (const auto& [key, item] : psuStatusMap)
Lei YU58c26e32019-09-27 17:52:06 +0800447 {
Faisal Awada760053d2024-05-16 13:31:32 -0500448 if (!item.model.empty())
Lei YU58c26e32019-09-27 17:52:06 +0800449 {
Faisal Awada760053d2024-05-16 13:31:32 -0500450 path = path / item.model;
451 manifest = dir / item.model / MANIFEST_FILE;
452 break;
453 }
454 }
455 if (path == dir)
456 {
457 log<level::ERR>("Model directory not found");
458 return;
459 }
460
461 if (!fs::is_directory(path))
462 {
463 log<level::ERR>("The path is not a directory",
464 entry("PATH=%s", path.c_str()));
465 return;
466 }
467
468 if (!fs::exists(manifest))
469 {
470 log<level::ERR>("No MANIFEST found",
471 entry("PATH=%s", manifest.c_str()));
472 return;
473 }
474 // If the model in manifest does not match the dir name
475 // Log a warning
476 if (fs::is_regular_file(manifest))
477 {
478 auto ret = Version::getValues(
479 manifest.string(), {MANIFEST_VERSION, MANIFEST_EXTENDED_VERSION});
480 auto version = ret[MANIFEST_VERSION];
481 auto extVersion = ret[MANIFEST_EXTENDED_VERSION];
482 auto info = Version::getExtVersionInfo(extVersion);
483 auto model = info["model"];
484 if (path.stem() != model)
485 {
486 log<level::ERR>("Unmatched model", entry("PATH=%s", path.c_str()),
487 entry("MODEL=%s", model.c_str()));
488 }
489 else
490 {
Lei YU58c26e32019-09-27 17:52:06 +0800491 auto versionId = utils::getVersionId(version);
492 auto it = activations.find(versionId);
493 if (it == activations.end())
494 {
495 // This is a version that is different than the running PSUs
496 auto activationState = Activation::Status::Ready;
497 auto purpose = VersionPurpose::PSU;
498 auto objPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
499
500 auto activation = createActivationObject(
501 objPath, versionId, extVersion, activationState, {}, path);
502 activations.emplace(versionId, std::move(activation));
503
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400504 auto versionPtr =
505 createVersionObject(objPath, versionId, version, purpose);
Lei YU58c26e32019-09-27 17:52:06 +0800506 versions.emplace(versionId, std::move(versionPtr));
507 }
508 else
509 {
510 // This is a version that a running PSU is using, set the path
511 // on the version object
512 it->second->path(path);
513 }
514 }
Faisal Awada760053d2024-05-16 13:31:32 -0500515 }
516 else
517 {
518 log<level::ERR>("MANIFEST is not a file",
519 entry("PATH=%s", manifest.c_str()));
Lei YU58c26e32019-09-27 17:52:06 +0800520 }
521}
522
Lei YU65207482019-10-11 16:39:36 +0800523std::optional<std::string> ItemUpdater::getLatestVersionId()
524{
Faisal Awadafb86e792024-09-11 10:51:17 -0500525 std::string latestVersion;
526 if (ALWAYS_USE_BUILTIN_IMG_DIR)
527 {
528 latestVersion = getFWVersionFromBuiltinDir();
529 }
530 else
531 {
532 latestVersion = utils::getLatestVersion(versionStrings);
533 }
Lei YU65207482019-10-11 16:39:36 +0800534 if (latestVersion.empty())
535 {
536 return {};
537 }
538
539 std::optional<std::string> versionId;
540 for (const auto& v : versions)
541 {
542 if (v.second->version() == latestVersion)
543 {
544 versionId = v.first;
545 break;
546 }
547 }
548 assert(versionId.has_value());
549 return versionId;
550}
551
Lei YU63f9e712019-10-12 15:16:55 +0800552void ItemUpdater::syncToLatestImage()
553{
554 auto latestVersionId = getLatestVersionId();
555 if (!latestVersionId)
556 {
557 return;
558 }
559 const auto& it = activations.find(*latestVersionId);
560 assert(it != activations.end());
561 const auto& activation = it->second;
562 const auto& assocs = activation->associations();
563
564 auto paths = utils::getPSUInventoryPath(bus);
565 for (const auto& p : paths)
566 {
Faisal Awada760053d2024-05-16 13:31:32 -0500567 // As long as there is a PSU is not associated with the latest
568 // image, run the activation so that all PSUs are running the same
569 // latest image.
Lei YU63f9e712019-10-12 15:16:55 +0800570 if (!utils::isAssociated(p, assocs))
571 {
572 log<level::INFO>("Automatically update PSU",
573 entry("VERSION_ID=%s", latestVersionId->c_str()));
574 invokeActivation(activation);
575 break;
576 }
577 }
578}
579
580void ItemUpdater::invokeActivation(
581 const std::unique_ptr<Activation>& activation)
582{
583 activation->requestedActivation(Activation::RequestedActivations::Active);
584}
585
Faisal Awada760053d2024-05-16 13:31:32 -0500586void ItemUpdater::onPSUInterfaceAdded(sdbusplus::message_t& msg)
587{
588 sdbusplus::message::object_path objPath;
589 std::map<std::string,
590 std::map<std::string, std::variant<bool, std::string>>>
591 interfaces;
592 msg.read(objPath, interfaces);
593 std::string path = objPath.str;
594
595 if (interfaces.find(PSU_INVENTORY_IFACE) == interfaces.end() ||
596 (psuStatusMap[path].present && !psuStatusMap[path].model.empty()))
597 {
598 return;
599 }
600
601 auto timeout = std::chrono::steady_clock::now() +
602 std::chrono::seconds(TIMEOUT);
603
604 // Poll the inventory item until it gets the present property
605 // or the timeout is reached
606 while (std::chrono::steady_clock::now() < timeout)
607 {
608 try
609 {
610 psuStatusMap[path].present = utils::getProperty<bool>(
611 bus, msg.get_sender(), path.c_str(), ITEM_IFACE, PRESENT);
612 break;
613 }
614 catch (const std::exception& e)
615 {
616 auto err = errno;
617 log<level::INFO>(
618 std::format("Failed to get Inventory Item Present. errno={}",
619 err)
620 .c_str());
621 sleep(1);
622 }
623 }
624
625 // Poll the inventory item until it retrieves model or the timeout is
626 // reached. The model is the path trail of the firmware's and manifest
627 // subdirectory. If the model not found the firmware and manifest
628 // cannot be located.
629 timeout = std::chrono::steady_clock::now() + std::chrono::seconds(TIMEOUT);
630 while (std::chrono::steady_clock::now() < timeout &&
631 psuStatusMap[path].present)
632 {
633 try
634 {
635 psuStatusMap[path].model = utils::getProperty<std::string>(
636 bus, msg.get_sender(), path.c_str(), ASSET_IFACE, MODEL);
637 processPSUImageAndSyncToLatest();
638 break;
639 }
640 catch (const std::exception& e)
641 {
642 auto err = errno;
643 log<level::INFO>(
644 std::format(
645 "Failed to get Inventory Decorator Asset model. errno={}",
646 err)
647 .c_str());
648 sleep(1);
649 }
650 }
651}
652
653void ItemUpdater::processPSUImageAndSyncToLatest()
654{
655 processPSUImage();
656 processStoredImage();
657 syncToLatestImage();
658}
659
Faisal Awadafb86e792024-09-11 10:51:17 -0500660std::string ItemUpdater::getFWVersionFromBuiltinDir()
661{
662 std::string version;
663 for (const auto& activation : activations)
664 {
665 if (activation.second->path().starts_with(IMG_DIR_BUILTIN))
666 {
667 std::string versionId = activation.second->getVersionId();
668 auto it = versions.find(versionId);
669 if (it != versions.end())
670 {
671 const auto& versionPtr = it->second;
672 version = versionPtr->version();
673 break;
674 }
675 }
676 }
677 return version;
678}
679
Lei YU01539e72019-07-31 10:57:38 +0800680} // namespace updater
681} // namespace software
682} // namespace phosphor