blob: 44612b31bc3b4cc573f63aa5d1a065cb3227b1b4 [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>
Shawn McCarney783406e2024-11-17 21:49:37 -060013#include <format>
14#include <set>
Patrick Williams5670b182023-05-10 07:50:50 -050015
Lei YUfda15a32019-09-19 14:43:02 +080016namespace
17{
Lei YU58c26e32019-09-27 17:52:06 +080018constexpr auto MANIFEST_VERSION = "version";
19constexpr auto MANIFEST_EXTENDED_VERSION = "extended_version";
20} // namespace
Lei YUfda15a32019-09-19 14:43:02 +080021
Lei YU01539e72019-07-31 10:57:38 +080022namespace phosphor
23{
24namespace software
25{
26namespace updater
27{
28namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Lei YU01539e72019-07-31 10:57:38 +080029
30using namespace sdbusplus::xyz::openbmc_project::Common::Error;
31using namespace phosphor::logging;
Lei YUad90ad52019-08-06 11:19:28 +080032using SVersion = server::Version;
33using VersionPurpose = SVersion::VersionPurpose;
Lei YU01539e72019-07-31 10:57:38 +080034
Patrick Williams374fae52022-07-22 19:26:55 -050035void ItemUpdater::createActivation(sdbusplus::message_t& m)
Lei YU01539e72019-07-31 10:57:38 +080036{
Lei YU01539e72019-07-31 10:57:38 +080037 sdbusplus::message::object_path objPath;
George Liude270292020-06-12 17:14:17 +080038 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Lei YU01539e72019-07-31 10:57:38 +080039 interfaces;
40 m.read(objPath, interfaces);
41
42 std::string path(std::move(objPath));
43 std::string filePath;
44 auto purpose = VersionPurpose::Unknown;
45 std::string version;
46
47 for (const auto& [interfaceName, propertyMap] : interfaces)
48 {
49 if (interfaceName == VERSION_IFACE)
50 {
51 for (const auto& [propertyName, propertyValue] : propertyMap)
52 {
53 if (propertyName == "Purpose")
54 {
55 // Only process the PSU images
56 auto value = SVersion::convertVersionPurposeFromString(
Lei YU1517f5f2019-10-14 16:44:42 +080057 std::get<std::string>(propertyValue));
Lei YU01539e72019-07-31 10:57:38 +080058
59 if (value == VersionPurpose::PSU)
60 {
61 purpose = value;
62 }
63 }
Lei YUf77189f2019-08-07 14:26:30 +080064 else if (propertyName == VERSION)
Lei YU01539e72019-07-31 10:57:38 +080065 {
Lei YU1517f5f2019-10-14 16:44:42 +080066 version = std::get<std::string>(propertyValue);
Lei YU01539e72019-07-31 10:57:38 +080067 }
68 }
69 }
70 else if (interfaceName == FILEPATH_IFACE)
71 {
72 const auto& it = propertyMap.find("Path");
73 if (it != propertyMap.end())
74 {
Lei YU1517f5f2019-10-14 16:44:42 +080075 filePath = std::get<std::string>(it->second);
Lei YU01539e72019-07-31 10:57:38 +080076 }
77 }
78 }
79 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
80 {
81 return;
82 }
83
Shawn McCarney799f5142024-09-26 14:50:25 -050084 // If we are only installing PSU images from the built-in directory, ignore
85 // PSU images from other directories
86 if (ALWAYS_USE_BUILTIN_IMG_DIR && !filePath.starts_with(IMG_DIR_BUILTIN))
87 {
88 return;
89 }
90
Lei YU01539e72019-07-31 10:57:38 +080091 // Version id is the last item in the path
George Liua0f2cf72024-08-23 14:50:12 +080092 auto pos = path.rfind('/');
Lei YU01539e72019-07-31 10:57:38 +080093 if (pos == std::string::npos)
94 {
95 log<level::ERR>("No version id found in object path",
96 entry("OBJPATH=%s", path.c_str()));
97 return;
98 }
99
100 auto versionId = path.substr(pos + 1);
101
102 if (activations.find(versionId) == activations.end())
103 {
104 // Determine the Activation state by processing the given image dir.
Lei YU91029442019-08-01 15:57:31 +0800105 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800106 auto activationState = Activation::Status::Ready;
Lei YU01539e72019-07-31 10:57:38 +0800107
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400108 associations.emplace_back(std::make_tuple(
109 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
110 PSU_INVENTORY_PATH_BASE));
Lei YU91029442019-08-01 15:57:31 +0800111
Lei YU01539e72019-07-31 10:57:38 +0800112 fs::path manifestPath(filePath);
113 manifestPath /= MANIFEST_FILE;
Lei YU58c26e32019-09-27 17:52:06 +0800114 std::string extendedVersion =
115 Version::getValue(manifestPath, {MANIFEST_EXTENDED_VERSION});
Lei YU01539e72019-07-31 10:57:38 +0800116
Lei YU99301372019-09-29 16:27:12 +0800117 auto activation =
118 createActivationObject(path, versionId, extendedVersion,
119 activationState, associations, filePath);
Lei YU01539e72019-07-31 10:57:38 +0800120 activations.emplace(versionId, std::move(activation));
121
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400122 auto versionPtr =
123 createVersionObject(path, versionId, version, purpose);
Lei YU01539e72019-07-31 10:57:38 +0800124 versions.emplace(versionId, std::move(versionPtr));
125 }
126 return;
127}
128
Lei YUa5c47bb2019-09-29 11:28:53 +0800129void ItemUpdater::erase(const std::string& versionId)
Lei YU01539e72019-07-31 10:57:38 +0800130{
131 auto it = versions.find(versionId);
132 if (it == versions.end())
133 {
134 log<level::ERR>(("Error: Failed to find version " + versionId +
135 " in item updater versions map."
136 " Unable to remove.")
137 .c_str());
138 }
139 else
140 {
Lei YU1517f5f2019-10-14 16:44:42 +0800141 versionStrings.erase(it->second->getVersionString());
142 versions.erase(it);
Lei YU01539e72019-07-31 10:57:38 +0800143 }
144
145 // Removing entry in activations map
146 auto ita = activations.find(versionId);
147 if (ita == activations.end())
148 {
149 log<level::ERR>(("Error: Failed to find version " + versionId +
150 " in item updater activations map."
151 " Unable to remove.")
152 .c_str());
153 }
154 else
155 {
156 activations.erase(versionId);
157 }
158}
159
Lei YU91029442019-08-01 15:57:31 +0800160void ItemUpdater::createActiveAssociation(const std::string& path)
161{
162 assocs.emplace_back(
163 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
164 associations(assocs);
165}
166
Lei YUad90ad52019-08-06 11:19:28 +0800167void ItemUpdater::addFunctionalAssociation(const std::string& path)
Lei YU91029442019-08-01 15:57:31 +0800168{
Lei YU91029442019-08-01 15:57:31 +0800169 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
170 FUNCTIONAL_REV_ASSOCIATION, path));
171 associations(assocs);
172}
173
Lei YUa8b966f2020-03-18 10:32:24 +0800174void ItemUpdater::addUpdateableAssociation(const std::string& path)
175{
176 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
177 UPDATEABLE_REV_ASSOCIATION, path));
178 associations(assocs);
179}
180
Lei YU91029442019-08-01 15:57:31 +0800181void ItemUpdater::removeAssociation(const std::string& path)
182{
183 for (auto iter = assocs.begin(); iter != assocs.end();)
184 {
George Liua5205e42024-08-23 15:27:54 +0800185 if ((std::get<2>(*iter)) == path)
Lei YU91029442019-08-01 15:57:31 +0800186 {
187 iter = assocs.erase(iter);
188 associations(assocs);
189 }
190 else
191 {
192 ++iter;
193 }
194 }
195}
196
Lei YUffb36532019-10-15 13:55:24 +0800197void ItemUpdater::onUpdateDone(const std::string& versionId,
198 const std::string& psuInventoryPath)
199{
200 // After update is done, remove old activation objects
201 for (auto it = activations.begin(); it != activations.end(); ++it)
202 {
203 if (it->second->getVersionId() != versionId &&
204 utils::isAssociated(psuInventoryPath, it->second->associations()))
205 {
206 removePsuObject(psuInventoryPath);
207 break;
208 }
209 }
Lei YU1517f5f2019-10-14 16:44:42 +0800210
211 auto it = activations.find(versionId);
212 assert(it != activations.end());
213 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUffb36532019-10-15 13:55:24 +0800214}
215
Lei YU01539e72019-07-31 10:57:38 +0800216std::unique_ptr<Activation> ItemUpdater::createActivationObject(
217 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800218 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800219 const AssociationList& assocs, const std::string& filePath)
Lei YU01539e72019-07-31 10:57:38 +0800220{
221 return std::make_unique<Activation>(bus, path, versionId, extVersion,
Lei YUffb36532019-10-15 13:55:24 +0800222 activationStatus, assocs, filePath,
223 this, this);
Lei YU01539e72019-07-31 10:57:38 +0800224}
225
Lei YUad90ad52019-08-06 11:19:28 +0800226void ItemUpdater::createPsuObject(const std::string& psuInventoryPath,
227 const std::string& psuVersion)
228{
229 auto versionId = utils::getVersionId(psuVersion);
230 auto path = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
231
232 auto it = activations.find(versionId);
233 if (it != activations.end())
234 {
235 // The versionId is already created, associate the path
236 auto associations = it->second->associations();
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400237 associations.emplace_back(
238 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
239 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800240 it->second->associations(associations);
Lei YUbd3b0072019-08-08 13:09:50 +0800241 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUad90ad52019-08-06 11:19:28 +0800242 }
243 else
244 {
245 // Create a new object for running PSU inventory
246 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800247 auto activationState = Activation::Status::Active;
Lei YUad90ad52019-08-06 11:19:28 +0800248
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400249 associations.emplace_back(
250 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
251 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800252
Lei YU99301372019-09-29 16:27:12 +0800253 auto activation = createActivationObject(
254 path, versionId, "", activationState, associations, "");
Lei YUad90ad52019-08-06 11:19:28 +0800255 activations.emplace(versionId, std::move(activation));
Lei YUbd3b0072019-08-08 13:09:50 +0800256 psuPathActivationMap.emplace(psuInventoryPath, activations[versionId]);
Lei YUad90ad52019-08-06 11:19:28 +0800257
258 auto versionPtr = createVersionObject(path, versionId, psuVersion,
Lei YU99301372019-09-29 16:27:12 +0800259 VersionPurpose::PSU);
Lei YUad90ad52019-08-06 11:19:28 +0800260 versions.emplace(versionId, std::move(versionPtr));
261
262 createActiveAssociation(path);
263 addFunctionalAssociation(path);
Lei YUa8b966f2020-03-18 10:32:24 +0800264 addUpdateableAssociation(path);
Lei YUad90ad52019-08-06 11:19:28 +0800265 }
266}
267
Lei YUbd3b0072019-08-08 13:09:50 +0800268void ItemUpdater::removePsuObject(const std::string& psuInventoryPath)
269{
Lei YUbd3b0072019-08-08 13:09:50 +0800270 auto it = psuPathActivationMap.find(psuInventoryPath);
271 if (it == psuPathActivationMap.end())
272 {
273 log<level::ERR>("No Activation found for PSU",
274 entry("PSUPATH=%s", psuInventoryPath.c_str()));
275 return;
276 }
277 const auto& activationPtr = it->second;
278 psuPathActivationMap.erase(psuInventoryPath);
279
280 auto associations = activationPtr->associations();
281 for (auto iter = associations.begin(); iter != associations.end();)
282 {
George Liua5205e42024-08-23 15:27:54 +0800283 if ((std::get<2>(*iter)) == psuInventoryPath)
Lei YUbd3b0072019-08-08 13:09:50 +0800284 {
285 iter = associations.erase(iter);
286 }
287 else
288 {
289 ++iter;
290 }
291 }
292 if (associations.empty())
293 {
294 // Remove the activation
Lei YUa5c47bb2019-09-29 11:28:53 +0800295 erase(activationPtr->getVersionId());
Lei YUbd3b0072019-08-08 13:09:50 +0800296 }
297 else
298 {
299 // Update association
300 activationPtr->associations(associations);
301 }
302}
303
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500304void ItemUpdater::addPsuToStatusMap(const std::string& psuPath)
305{
306 if (!psuStatusMap.contains(psuPath))
307 {
308 psuStatusMap[psuPath] = {false, ""};
309
Shawn McCarney783406e2024-11-17 21:49:37 -0600310 // Add PropertiesChanged listener for Item interface so we are notified
311 // when Present property changes
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500312 psuMatches.emplace_back(
313 bus, MatchRules::propertiesChanged(psuPath, ITEM_IFACE),
314 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
Shawn McCarney783406e2024-11-17 21:49:37 -0600315 std::placeholders::_1));
316 }
317}
318
319void ItemUpdater::handlePSUPresenceChanged(const std::string& psuPath)
320{
321 if (psuStatusMap.contains(psuPath))
322 {
323 if (psuStatusMap[psuPath].present)
324 {
325 // PSU is now present
326 psuStatusMap[psuPath].model = utils::getModel(psuPath);
327 auto version = utils::getVersion(psuPath);
328 if (!version.empty() && !psuPathActivationMap.contains(psuPath))
329 {
330 createPsuObject(psuPath, version);
331 }
332 }
333 else
334 {
335 // PSU is now missing
336 psuStatusMap[psuPath].model.clear();
337 if (psuPathActivationMap.contains(psuPath))
338 {
339 removePsuObject(psuPath);
340 }
341 }
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500342 }
343}
344
Lei YU01539e72019-07-31 10:57:38 +0800345std::unique_ptr<Version> ItemUpdater::createVersionObject(
346 const std::string& objPath, const std::string& versionId,
347 const std::string& versionString,
348 sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
Lei YU99301372019-09-29 16:27:12 +0800349 versionPurpose)
Lei YU01539e72019-07-31 10:57:38 +0800350{
Lei YU65207482019-10-11 16:39:36 +0800351 versionStrings.insert(versionString);
Lei YU01539e72019-07-31 10:57:38 +0800352 auto version = std::make_unique<Version>(
Lei YU99301372019-09-29 16:27:12 +0800353 bus, objPath, versionId, versionString, versionPurpose,
Lei YU01539e72019-07-31 10:57:38 +0800354 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
355 return version;
356}
357
Patrick Williams374fae52022-07-22 19:26:55 -0500358void ItemUpdater::onPsuInventoryChangedMsg(sdbusplus::message_t& msg)
Lei YUad90ad52019-08-06 11:19:28 +0800359{
Lei YUbd3b0072019-08-08 13:09:50 +0800360 using Interface = std::string;
Lei YUbd3b0072019-08-08 13:09:50 +0800361 Interface interface;
362 Properties properties;
Lei YUbd3b0072019-08-08 13:09:50 +0800363 std::string psuPath = msg.get_path();
364
365 msg.read(interface, properties);
Lei YUa2c2cd72019-08-09 15:54:10 +0800366 onPsuInventoryChanged(psuPath, properties);
367}
368
369void ItemUpdater::onPsuInventoryChanged(const std::string& psuPath,
370 const Properties& properties)
371{
Shawn McCarney783406e2024-11-17 21:49:37 -0600372 try
Lei YU1517f5f2019-10-14 16:44:42 +0800373 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600374 if (psuStatusMap.contains(psuPath) && properties.contains(PRESENT))
Lei YU1517f5f2019-10-14 16:44:42 +0800375 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600376 psuStatusMap[psuPath].present =
377 std::get<bool>(properties.at(PRESENT));
378 handlePSUPresenceChanged(psuPath);
379 if (psuStatusMap[psuPath].present)
Faisal Awada760053d2024-05-16 13:31:32 -0500380 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600381 // Check if there are new PSU images to update
382 processStoredImage();
383 syncToLatestImage();
Faisal Awada760053d2024-05-16 13:31:32 -0500384 }
Lei YU1517f5f2019-10-14 16:44:42 +0800385 }
Lei YUbd3b0072019-08-08 13:09:50 +0800386 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600387 catch (const std::exception& e)
Lei YUbd3b0072019-08-08 13:09:50 +0800388 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600389 log<level::ERR>(
390 std::format(
391 "Unable to handle inventory PropertiesChanged event: {}",
392 e.what())
393 .c_str());
Lei YUbd3b0072019-08-08 13:09:50 +0800394 }
Lei YUad90ad52019-08-06 11:19:28 +0800395}
396
397void ItemUpdater::processPSUImage()
398{
Shawn McCarney783406e2024-11-17 21:49:37 -0600399 try
Lei YUad90ad52019-08-06 11:19:28 +0800400 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600401 auto paths = utils::getPSUInventoryPath(bus);
402 for (const auto& p : paths)
Lei YUad90ad52019-08-06 11:19:28 +0800403 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600404 try
405 {
406 addPsuToStatusMap(p);
407 auto service = utils::getService(bus, p.c_str(), ITEM_IFACE);
408 psuStatusMap[p].present = utils::getProperty<bool>(
409 bus, service.c_str(), p.c_str(), ITEM_IFACE, PRESENT);
410 handlePSUPresenceChanged(p);
411 }
412 catch (const std::exception& e)
413 {
414 // Ignore errors; the information might not be available yet
415 }
Lei YUad90ad52019-08-06 11:19:28 +0800416 }
Lei YUad90ad52019-08-06 11:19:28 +0800417 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600418 catch (const std::exception& e)
419 {
420 // Ignore errors; the information might not be available yet
421 }
Lei YUad90ad52019-08-06 11:19:28 +0800422}
423
Lei YU58c26e32019-09-27 17:52:06 +0800424void ItemUpdater::processStoredImage()
425{
426 scanDirectory(IMG_DIR_BUILTIN);
Faisal Awada760053d2024-05-16 13:31:32 -0500427
Faisal Awadafb86e792024-09-11 10:51:17 -0500428 if (!ALWAYS_USE_BUILTIN_IMG_DIR)
429 {
430 scanDirectory(IMG_DIR_PERSIST);
431 }
Lei YU58c26e32019-09-27 17:52:06 +0800432}
433
434void ItemUpdater::scanDirectory(const fs::path& dir)
435{
Faisal Awada760053d2024-05-16 13:31:32 -0500436 auto manifest = dir;
437 auto path = dir;
Lei YU58c26e32019-09-27 17:52:06 +0800438 // The directory shall put PSU images in directories named with model
439 if (!fs::exists(dir))
440 {
441 // Skip
442 return;
443 }
444 if (!fs::is_directory(dir))
445 {
446 log<level::ERR>("The path is not a directory",
447 entry("PATH=%s", dir.c_str()));
448 return;
449 }
Faisal Awada760053d2024-05-16 13:31:32 -0500450
451 for (const auto& [key, item] : psuStatusMap)
Lei YU58c26e32019-09-27 17:52:06 +0800452 {
Faisal Awada760053d2024-05-16 13:31:32 -0500453 if (!item.model.empty())
Lei YU58c26e32019-09-27 17:52:06 +0800454 {
Faisal Awada760053d2024-05-16 13:31:32 -0500455 path = path / item.model;
456 manifest = dir / item.model / MANIFEST_FILE;
457 break;
458 }
459 }
460 if (path == dir)
461 {
462 log<level::ERR>("Model directory not found");
463 return;
464 }
465
466 if (!fs::is_directory(path))
467 {
468 log<level::ERR>("The path is not a directory",
469 entry("PATH=%s", path.c_str()));
470 return;
471 }
472
473 if (!fs::exists(manifest))
474 {
475 log<level::ERR>("No MANIFEST found",
476 entry("PATH=%s", manifest.c_str()));
477 return;
478 }
479 // If the model in manifest does not match the dir name
480 // Log a warning
481 if (fs::is_regular_file(manifest))
482 {
483 auto ret = Version::getValues(
484 manifest.string(), {MANIFEST_VERSION, MANIFEST_EXTENDED_VERSION});
485 auto version = ret[MANIFEST_VERSION];
486 auto extVersion = ret[MANIFEST_EXTENDED_VERSION];
487 auto info = Version::getExtVersionInfo(extVersion);
488 auto model = info["model"];
489 if (path.stem() != model)
490 {
491 log<level::ERR>("Unmatched model", entry("PATH=%s", path.c_str()),
492 entry("MODEL=%s", model.c_str()));
493 }
494 else
495 {
Lei YU58c26e32019-09-27 17:52:06 +0800496 auto versionId = utils::getVersionId(version);
497 auto it = activations.find(versionId);
498 if (it == activations.end())
499 {
500 // This is a version that is different than the running PSUs
501 auto activationState = Activation::Status::Ready;
502 auto purpose = VersionPurpose::PSU;
503 auto objPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
504
505 auto activation = createActivationObject(
506 objPath, versionId, extVersion, activationState, {}, path);
507 activations.emplace(versionId, std::move(activation));
508
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400509 auto versionPtr =
510 createVersionObject(objPath, versionId, version, purpose);
Lei YU58c26e32019-09-27 17:52:06 +0800511 versions.emplace(versionId, std::move(versionPtr));
512 }
513 else
514 {
515 // This is a version that a running PSU is using, set the path
516 // on the version object
517 it->second->path(path);
518 }
519 }
Faisal Awada760053d2024-05-16 13:31:32 -0500520 }
521 else
522 {
523 log<level::ERR>("MANIFEST is not a file",
524 entry("PATH=%s", manifest.c_str()));
Lei YU58c26e32019-09-27 17:52:06 +0800525 }
526}
527
Lei YU65207482019-10-11 16:39:36 +0800528std::optional<std::string> ItemUpdater::getLatestVersionId()
529{
Faisal Awadafb86e792024-09-11 10:51:17 -0500530 std::string latestVersion;
531 if (ALWAYS_USE_BUILTIN_IMG_DIR)
532 {
533 latestVersion = getFWVersionFromBuiltinDir();
534 }
535 else
536 {
537 latestVersion = utils::getLatestVersion(versionStrings);
538 }
Lei YU65207482019-10-11 16:39:36 +0800539 if (latestVersion.empty())
540 {
541 return {};
542 }
543
544 std::optional<std::string> versionId;
545 for (const auto& v : versions)
546 {
547 if (v.second->version() == latestVersion)
548 {
549 versionId = v.first;
550 break;
551 }
552 }
553 assert(versionId.has_value());
554 return versionId;
555}
556
Lei YU63f9e712019-10-12 15:16:55 +0800557void ItemUpdater::syncToLatestImage()
558{
559 auto latestVersionId = getLatestVersionId();
560 if (!latestVersionId)
561 {
562 return;
563 }
564 const auto& it = activations.find(*latestVersionId);
565 assert(it != activations.end());
566 const auto& activation = it->second;
567 const auto& assocs = activation->associations();
568
569 auto paths = utils::getPSUInventoryPath(bus);
570 for (const auto& p : paths)
571 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600572 // If there is a present PSU that is not associated with the latest
Faisal Awada760053d2024-05-16 13:31:32 -0500573 // image, run the activation so that all PSUs are running the same
574 // latest image.
Shawn McCarney783406e2024-11-17 21:49:37 -0600575 if (psuStatusMap.contains(p) && psuStatusMap[p].present)
Lei YU63f9e712019-10-12 15:16:55 +0800576 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600577 if (!utils::isAssociated(p, assocs))
578 {
579 log<level::INFO>(
580 "Automatically update PSU",
581 entry("VERSION_ID=%s", latestVersionId->c_str()));
582 invokeActivation(activation);
583 break;
584 }
Lei YU63f9e712019-10-12 15:16:55 +0800585 }
586 }
587}
588
589void ItemUpdater::invokeActivation(
590 const std::unique_ptr<Activation>& activation)
591{
592 activation->requestedActivation(Activation::RequestedActivations::Active);
593}
594
Faisal Awada760053d2024-05-16 13:31:32 -0500595void ItemUpdater::onPSUInterfaceAdded(sdbusplus::message_t& msg)
596{
Shawn McCarney783406e2024-11-17 21:49:37 -0600597 // Maintain static set of valid PSU paths. This is needed if PSU interface
598 // comes in a separate InterfacesAdded message from Item interface.
599 static std::set<std::string> psuPaths{};
Faisal Awada760053d2024-05-16 13:31:32 -0500600
Shawn McCarney783406e2024-11-17 21:49:37 -0600601 try
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500602 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600603 sdbusplus::message::object_path objPath;
604 std::map<std::string,
605 std::map<std::string, std::variant<bool, std::string>>>
606 interfaces;
607 msg.read(objPath, interfaces);
608 std::string path = objPath.str;
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500609
Shawn McCarney783406e2024-11-17 21:49:37 -0600610 if (interfaces.contains(PSU_INVENTORY_IFACE))
Faisal Awada760053d2024-05-16 13:31:32 -0500611 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600612 psuPaths.insert(path);
Faisal Awada760053d2024-05-16 13:31:32 -0500613 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600614
615 if (interfaces.contains(ITEM_IFACE) && psuPaths.contains(path) &&
616 !psuStatusMap.contains(path))
Faisal Awada760053d2024-05-16 13:31:32 -0500617 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600618 auto interface = interfaces[ITEM_IFACE];
619 if (interface.contains(PRESENT))
620 {
621 addPsuToStatusMap(path);
622 psuStatusMap[path].present = std::get<bool>(interface[PRESENT]);
623 handlePSUPresenceChanged(path);
624 if (psuStatusMap[path].present)
625 {
626 // Check if there are new PSU images to update
627 processStoredImage();
628 syncToLatestImage();
629 }
630 }
Faisal Awada760053d2024-05-16 13:31:32 -0500631 }
632 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600633 catch (const std::exception& e)
Faisal Awada760053d2024-05-16 13:31:32 -0500634 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600635 log<level::ERR>(
636 std::format("Unable to handle inventory InterfacesAdded event: {}",
637 e.what())
638 .c_str());
Faisal Awada760053d2024-05-16 13:31:32 -0500639 }
640}
641
642void ItemUpdater::processPSUImageAndSyncToLatest()
643{
644 processPSUImage();
645 processStoredImage();
646 syncToLatestImage();
647}
648
Faisal Awadafb86e792024-09-11 10:51:17 -0500649std::string ItemUpdater::getFWVersionFromBuiltinDir()
650{
651 std::string version;
652 for (const auto& activation : activations)
653 {
654 if (activation.second->path().starts_with(IMG_DIR_BUILTIN))
655 {
656 std::string versionId = activation.second->getVersionId();
657 auto it = versions.find(versionId);
658 if (it != versions.end())
659 {
660 const auto& versionPtr = it->second;
661 version = versionPtr->version();
662 break;
663 }
664 }
665 }
666 return version;
667}
668
Lei YU01539e72019-07-31 10:57:38 +0800669} // namespace updater
670} // namespace software
671} // namespace phosphor