blob: aea02ee6d9c7e2c3c8021f40a5643231f9e3b421 [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>
Shawn McCarneycdf86de2024-11-26 10:02:14 -06008#include <phosphor-logging/lg2.hpp>
Lei YU01539e72019-07-31 10:57:38 +08009#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 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -060095 lg2::error("No version id found in object path {OBJPATH}", "OBJPATH",
96 path);
Lei YU01539e72019-07-31 10:57:38 +080097 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 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600134 lg2::error("Error: Failed to find version {VERSION_ID} in "
135 "item updater versions map. Unable to remove.",
136 "VERSION_ID", versionId);
Lei YU01539e72019-07-31 10:57:38 +0800137 }
138 else
139 {
Lei YU1517f5f2019-10-14 16:44:42 +0800140 versionStrings.erase(it->second->getVersionString());
141 versions.erase(it);
Lei YU01539e72019-07-31 10:57:38 +0800142 }
143
144 // Removing entry in activations map
145 auto ita = activations.find(versionId);
146 if (ita == activations.end())
147 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600148 lg2::error("Error: Failed to find version {VERSION_ID} in "
149 "item updater activations map. Unable to remove.",
150 "VERSION_ID", versionId);
Lei YU01539e72019-07-31 10:57:38 +0800151 }
152 else
153 {
154 activations.erase(versionId);
155 }
156}
157
Lei YU91029442019-08-01 15:57:31 +0800158void ItemUpdater::createActiveAssociation(const std::string& path)
159{
160 assocs.emplace_back(
161 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
162 associations(assocs);
163}
164
Lei YUad90ad52019-08-06 11:19:28 +0800165void ItemUpdater::addFunctionalAssociation(const std::string& path)
Lei YU91029442019-08-01 15:57:31 +0800166{
Lei YU91029442019-08-01 15:57:31 +0800167 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
168 FUNCTIONAL_REV_ASSOCIATION, path));
169 associations(assocs);
170}
171
Lei YUa8b966f2020-03-18 10:32:24 +0800172void ItemUpdater::addUpdateableAssociation(const std::string& path)
173{
174 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
175 UPDATEABLE_REV_ASSOCIATION, path));
176 associations(assocs);
177}
178
Lei YU91029442019-08-01 15:57:31 +0800179void ItemUpdater::removeAssociation(const std::string& path)
180{
181 for (auto iter = assocs.begin(); iter != assocs.end();)
182 {
George Liua5205e42024-08-23 15:27:54 +0800183 if ((std::get<2>(*iter)) == path)
Lei YU91029442019-08-01 15:57:31 +0800184 {
185 iter = assocs.erase(iter);
186 associations(assocs);
187 }
188 else
189 {
190 ++iter;
191 }
192 }
193}
194
Lei YUffb36532019-10-15 13:55:24 +0800195void ItemUpdater::onUpdateDone(const std::string& versionId,
196 const std::string& psuInventoryPath)
197{
198 // After update is done, remove old activation objects
199 for (auto it = activations.begin(); it != activations.end(); ++it)
200 {
201 if (it->second->getVersionId() != versionId &&
202 utils::isAssociated(psuInventoryPath, it->second->associations()))
203 {
204 removePsuObject(psuInventoryPath);
205 break;
206 }
207 }
Lei YU1517f5f2019-10-14 16:44:42 +0800208
209 auto it = activations.find(versionId);
210 assert(it != activations.end());
211 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUffb36532019-10-15 13:55:24 +0800212}
213
Lei YU01539e72019-07-31 10:57:38 +0800214std::unique_ptr<Activation> ItemUpdater::createActivationObject(
215 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800216 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800217 const AssociationList& assocs, const std::string& filePath)
Lei YU01539e72019-07-31 10:57:38 +0800218{
219 return std::make_unique<Activation>(bus, path, versionId, extVersion,
Lei YUffb36532019-10-15 13:55:24 +0800220 activationStatus, assocs, filePath,
221 this, this);
Lei YU01539e72019-07-31 10:57:38 +0800222}
223
Lei YUad90ad52019-08-06 11:19:28 +0800224void ItemUpdater::createPsuObject(const std::string& psuInventoryPath,
225 const std::string& psuVersion)
226{
227 auto versionId = utils::getVersionId(psuVersion);
228 auto path = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
229
230 auto it = activations.find(versionId);
231 if (it != activations.end())
232 {
233 // The versionId is already created, associate the path
234 auto associations = it->second->associations();
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400235 associations.emplace_back(
236 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
237 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800238 it->second->associations(associations);
Lei YUbd3b0072019-08-08 13:09:50 +0800239 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUad90ad52019-08-06 11:19:28 +0800240 }
241 else
242 {
243 // Create a new object for running PSU inventory
244 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800245 auto activationState = Activation::Status::Active;
Lei YUad90ad52019-08-06 11:19:28 +0800246
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400247 associations.emplace_back(
248 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
249 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800250
Lei YU99301372019-09-29 16:27:12 +0800251 auto activation = createActivationObject(
252 path, versionId, "", activationState, associations, "");
Lei YUad90ad52019-08-06 11:19:28 +0800253 activations.emplace(versionId, std::move(activation));
Lei YUbd3b0072019-08-08 13:09:50 +0800254 psuPathActivationMap.emplace(psuInventoryPath, activations[versionId]);
Lei YUad90ad52019-08-06 11:19:28 +0800255
256 auto versionPtr = createVersionObject(path, versionId, psuVersion,
Lei YU99301372019-09-29 16:27:12 +0800257 VersionPurpose::PSU);
Lei YUad90ad52019-08-06 11:19:28 +0800258 versions.emplace(versionId, std::move(versionPtr));
259
260 createActiveAssociation(path);
261 addFunctionalAssociation(path);
Lei YUa8b966f2020-03-18 10:32:24 +0800262 addUpdateableAssociation(path);
Lei YUad90ad52019-08-06 11:19:28 +0800263 }
264}
265
Lei YUbd3b0072019-08-08 13:09:50 +0800266void ItemUpdater::removePsuObject(const std::string& psuInventoryPath)
267{
Lei YUbd3b0072019-08-08 13:09:50 +0800268 auto it = psuPathActivationMap.find(psuInventoryPath);
269 if (it == psuPathActivationMap.end())
270 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600271 lg2::error("No Activation found for PSU {PSUPATH}", "PSUPATH",
272 psuInventoryPath);
Lei YUbd3b0072019-08-08 13:09:50 +0800273 return;
274 }
275 const auto& activationPtr = it->second;
276 psuPathActivationMap.erase(psuInventoryPath);
277
278 auto associations = activationPtr->associations();
279 for (auto iter = associations.begin(); iter != associations.end();)
280 {
George Liua5205e42024-08-23 15:27:54 +0800281 if ((std::get<2>(*iter)) == psuInventoryPath)
Lei YUbd3b0072019-08-08 13:09:50 +0800282 {
283 iter = associations.erase(iter);
284 }
285 else
286 {
287 ++iter;
288 }
289 }
290 if (associations.empty())
291 {
292 // Remove the activation
Lei YUa5c47bb2019-09-29 11:28:53 +0800293 erase(activationPtr->getVersionId());
Lei YUbd3b0072019-08-08 13:09:50 +0800294 }
295 else
296 {
297 // Update association
298 activationPtr->associations(associations);
299 }
300}
301
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500302void ItemUpdater::addPsuToStatusMap(const std::string& psuPath)
303{
304 if (!psuStatusMap.contains(psuPath))
305 {
306 psuStatusMap[psuPath] = {false, ""};
307
Shawn McCarney783406e2024-11-17 21:49:37 -0600308 // Add PropertiesChanged listener for Item interface so we are notified
309 // when Present property changes
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500310 psuMatches.emplace_back(
311 bus, MatchRules::propertiesChanged(psuPath, ITEM_IFACE),
312 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
Shawn McCarney783406e2024-11-17 21:49:37 -0600313 std::placeholders::_1));
314 }
315}
316
317void ItemUpdater::handlePSUPresenceChanged(const std::string& psuPath)
318{
319 if (psuStatusMap.contains(psuPath))
320 {
321 if (psuStatusMap[psuPath].present)
322 {
323 // PSU is now present
324 psuStatusMap[psuPath].model = utils::getModel(psuPath);
325 auto version = utils::getVersion(psuPath);
326 if (!version.empty() && !psuPathActivationMap.contains(psuPath))
327 {
328 createPsuObject(psuPath, version);
329 }
330 }
331 else
332 {
333 // PSU is now missing
334 psuStatusMap[psuPath].model.clear();
335 if (psuPathActivationMap.contains(psuPath))
336 {
337 removePsuObject(psuPath);
338 }
339 }
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500340 }
341}
342
Lei YU01539e72019-07-31 10:57:38 +0800343std::unique_ptr<Version> ItemUpdater::createVersionObject(
344 const std::string& objPath, const std::string& versionId,
345 const std::string& versionString,
346 sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
Lei YU99301372019-09-29 16:27:12 +0800347 versionPurpose)
Lei YU01539e72019-07-31 10:57:38 +0800348{
Lei YU65207482019-10-11 16:39:36 +0800349 versionStrings.insert(versionString);
Lei YU01539e72019-07-31 10:57:38 +0800350 auto version = std::make_unique<Version>(
Lei YU99301372019-09-29 16:27:12 +0800351 bus, objPath, versionId, versionString, versionPurpose,
Lei YU01539e72019-07-31 10:57:38 +0800352 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
353 return version;
354}
355
Patrick Williams374fae52022-07-22 19:26:55 -0500356void ItemUpdater::onPsuInventoryChangedMsg(sdbusplus::message_t& msg)
Lei YUad90ad52019-08-06 11:19:28 +0800357{
Lei YUbd3b0072019-08-08 13:09:50 +0800358 using Interface = std::string;
Lei YUbd3b0072019-08-08 13:09:50 +0800359 Interface interface;
360 Properties properties;
Lei YUbd3b0072019-08-08 13:09:50 +0800361 std::string psuPath = msg.get_path();
362
363 msg.read(interface, properties);
Lei YUa2c2cd72019-08-09 15:54:10 +0800364 onPsuInventoryChanged(psuPath, properties);
365}
366
367void ItemUpdater::onPsuInventoryChanged(const std::string& psuPath,
368 const Properties& properties)
369{
Shawn McCarney783406e2024-11-17 21:49:37 -0600370 try
Lei YU1517f5f2019-10-14 16:44:42 +0800371 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600372 if (psuStatusMap.contains(psuPath) && properties.contains(PRESENT))
Lei YU1517f5f2019-10-14 16:44:42 +0800373 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600374 psuStatusMap[psuPath].present =
375 std::get<bool>(properties.at(PRESENT));
376 handlePSUPresenceChanged(psuPath);
377 if (psuStatusMap[psuPath].present)
Faisal Awada760053d2024-05-16 13:31:32 -0500378 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600379 // Check if there are new PSU images to update
380 processStoredImage();
381 syncToLatestImage();
Faisal Awada760053d2024-05-16 13:31:32 -0500382 }
Lei YU1517f5f2019-10-14 16:44:42 +0800383 }
Lei YUbd3b0072019-08-08 13:09:50 +0800384 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600385 catch (const std::exception& e)
Lei YUbd3b0072019-08-08 13:09:50 +0800386 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600387 lg2::error(
388 "Unable to handle inventory PropertiesChanged event: {ERROR}",
389 "ERROR", e);
Lei YUbd3b0072019-08-08 13:09:50 +0800390 }
Lei YUad90ad52019-08-06 11:19:28 +0800391}
392
393void ItemUpdater::processPSUImage()
394{
Shawn McCarney783406e2024-11-17 21:49:37 -0600395 try
Lei YUad90ad52019-08-06 11:19:28 +0800396 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600397 auto paths = utils::getPSUInventoryPath(bus);
398 for (const auto& p : paths)
Lei YUad90ad52019-08-06 11:19:28 +0800399 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600400 try
401 {
402 addPsuToStatusMap(p);
403 auto service = utils::getService(bus, p.c_str(), ITEM_IFACE);
404 psuStatusMap[p].present = utils::getProperty<bool>(
405 bus, service.c_str(), p.c_str(), ITEM_IFACE, PRESENT);
406 handlePSUPresenceChanged(p);
407 }
408 catch (const std::exception& e)
409 {
410 // Ignore errors; the information might not be available yet
411 }
Lei YUad90ad52019-08-06 11:19:28 +0800412 }
Lei YUad90ad52019-08-06 11:19:28 +0800413 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600414 catch (const std::exception& e)
415 {
416 // Ignore errors; the information might not be available yet
417 }
Lei YUad90ad52019-08-06 11:19:28 +0800418}
419
Lei YU58c26e32019-09-27 17:52:06 +0800420void ItemUpdater::processStoredImage()
421{
422 scanDirectory(IMG_DIR_BUILTIN);
Faisal Awada760053d2024-05-16 13:31:32 -0500423
Faisal Awadafb86e792024-09-11 10:51:17 -0500424 if (!ALWAYS_USE_BUILTIN_IMG_DIR)
425 {
426 scanDirectory(IMG_DIR_PERSIST);
427 }
Lei YU58c26e32019-09-27 17:52:06 +0800428}
429
430void ItemUpdater::scanDirectory(const fs::path& dir)
431{
Faisal Awada760053d2024-05-16 13:31:32 -0500432 auto manifest = dir;
433 auto path = dir;
Lei YU58c26e32019-09-27 17:52:06 +0800434 // The directory shall put PSU images in directories named with model
435 if (!fs::exists(dir))
436 {
437 // Skip
438 return;
439 }
440 if (!fs::is_directory(dir))
441 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600442 lg2::error("The path is not a directory: {PATH}", "PATH", dir);
Lei YU58c26e32019-09-27 17:52:06 +0800443 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 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600457 lg2::error("Model directory not found");
Faisal Awada760053d2024-05-16 13:31:32 -0500458 return;
459 }
460
461 if (!fs::is_directory(path))
462 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600463 lg2::error("The path is not a directory: {PATH}", "PATH", path);
Faisal Awada760053d2024-05-16 13:31:32 -0500464 return;
465 }
466
467 if (!fs::exists(manifest))
468 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600469 lg2::error("No MANIFEST found at {PATH}", "PATH", manifest);
Faisal Awada760053d2024-05-16 13:31:32 -0500470 return;
471 }
472 // If the model in manifest does not match the dir name
473 // Log a warning
474 if (fs::is_regular_file(manifest))
475 {
476 auto ret = Version::getValues(
477 manifest.string(), {MANIFEST_VERSION, MANIFEST_EXTENDED_VERSION});
478 auto version = ret[MANIFEST_VERSION];
479 auto extVersion = ret[MANIFEST_EXTENDED_VERSION];
480 auto info = Version::getExtVersionInfo(extVersion);
481 auto model = info["model"];
482 if (path.stem() != model)
483 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600484 lg2::error("Unmatched model: path={PATH}, model={MODEL}", "PATH",
485 path, "MODEL", model);
Faisal Awada760053d2024-05-16 13:31:32 -0500486 }
487 else
488 {
Lei YU58c26e32019-09-27 17:52:06 +0800489 auto versionId = utils::getVersionId(version);
490 auto it = activations.find(versionId);
491 if (it == activations.end())
492 {
493 // This is a version that is different than the running PSUs
494 auto activationState = Activation::Status::Ready;
495 auto purpose = VersionPurpose::PSU;
496 auto objPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
497
498 auto activation = createActivationObject(
499 objPath, versionId, extVersion, activationState, {}, path);
500 activations.emplace(versionId, std::move(activation));
501
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400502 auto versionPtr =
503 createVersionObject(objPath, versionId, version, purpose);
Lei YU58c26e32019-09-27 17:52:06 +0800504 versions.emplace(versionId, std::move(versionPtr));
505 }
506 else
507 {
508 // This is a version that a running PSU is using, set the path
509 // on the version object
510 it->second->path(path);
511 }
512 }
Faisal Awada760053d2024-05-16 13:31:32 -0500513 }
514 else
515 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600516 lg2::error("MANIFEST is not a file: {PATH}", "PATH", manifest);
Lei YU58c26e32019-09-27 17:52:06 +0800517 }
518}
519
Lei YU65207482019-10-11 16:39:36 +0800520std::optional<std::string> ItemUpdater::getLatestVersionId()
521{
Faisal Awadafb86e792024-09-11 10:51:17 -0500522 std::string latestVersion;
523 if (ALWAYS_USE_BUILTIN_IMG_DIR)
524 {
525 latestVersion = getFWVersionFromBuiltinDir();
526 }
527 else
528 {
529 latestVersion = utils::getLatestVersion(versionStrings);
530 }
Lei YU65207482019-10-11 16:39:36 +0800531 if (latestVersion.empty())
532 {
533 return {};
534 }
535
536 std::optional<std::string> versionId;
537 for (const auto& v : versions)
538 {
539 if (v.second->version() == latestVersion)
540 {
541 versionId = v.first;
542 break;
543 }
544 }
545 assert(versionId.has_value());
546 return versionId;
547}
548
Lei YU63f9e712019-10-12 15:16:55 +0800549void ItemUpdater::syncToLatestImage()
550{
551 auto latestVersionId = getLatestVersionId();
552 if (!latestVersionId)
553 {
554 return;
555 }
556 const auto& it = activations.find(*latestVersionId);
557 assert(it != activations.end());
558 const auto& activation = it->second;
559 const auto& assocs = activation->associations();
560
561 auto paths = utils::getPSUInventoryPath(bus);
562 for (const auto& p : paths)
563 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600564 // If there is a present PSU that is not associated with the latest
Faisal Awada760053d2024-05-16 13:31:32 -0500565 // image, run the activation so that all PSUs are running the same
566 // latest image.
Shawn McCarney783406e2024-11-17 21:49:37 -0600567 if (psuStatusMap.contains(p) && psuStatusMap[p].present)
Lei YU63f9e712019-10-12 15:16:55 +0800568 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600569 if (!utils::isAssociated(p, assocs))
570 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600571 lg2::info("Automatically update PSUs to version {VERSION_ID}",
572 "VERSION_ID", *latestVersionId);
Shawn McCarney783406e2024-11-17 21:49:37 -0600573 invokeActivation(activation);
574 break;
575 }
Lei YU63f9e712019-10-12 15:16:55 +0800576 }
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{
Shawn McCarney783406e2024-11-17 21:49:37 -0600588 // Maintain static set of valid PSU paths. This is needed if PSU interface
589 // comes in a separate InterfacesAdded message from Item interface.
590 static std::set<std::string> psuPaths{};
Faisal Awada760053d2024-05-16 13:31:32 -0500591
Shawn McCarney783406e2024-11-17 21:49:37 -0600592 try
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500593 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600594 sdbusplus::message::object_path objPath;
595 std::map<std::string,
596 std::map<std::string, std::variant<bool, std::string>>>
597 interfaces;
598 msg.read(objPath, interfaces);
599 std::string path = objPath.str;
Shawn McCarney73a6f0d2024-10-30 16:11:29 -0500600
Shawn McCarney783406e2024-11-17 21:49:37 -0600601 if (interfaces.contains(PSU_INVENTORY_IFACE))
Faisal Awada760053d2024-05-16 13:31:32 -0500602 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600603 psuPaths.insert(path);
Faisal Awada760053d2024-05-16 13:31:32 -0500604 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600605
606 if (interfaces.contains(ITEM_IFACE) && psuPaths.contains(path) &&
607 !psuStatusMap.contains(path))
Faisal Awada760053d2024-05-16 13:31:32 -0500608 {
Shawn McCarney783406e2024-11-17 21:49:37 -0600609 auto interface = interfaces[ITEM_IFACE];
610 if (interface.contains(PRESENT))
611 {
612 addPsuToStatusMap(path);
613 psuStatusMap[path].present = std::get<bool>(interface[PRESENT]);
614 handlePSUPresenceChanged(path);
615 if (psuStatusMap[path].present)
616 {
617 // Check if there are new PSU images to update
618 processStoredImage();
619 syncToLatestImage();
620 }
621 }
Faisal Awada760053d2024-05-16 13:31:32 -0500622 }
623 }
Shawn McCarney783406e2024-11-17 21:49:37 -0600624 catch (const std::exception& e)
Faisal Awada760053d2024-05-16 13:31:32 -0500625 {
Shawn McCarneycdf86de2024-11-26 10:02:14 -0600626 lg2::error("Unable to handle inventory InterfacesAdded event: {ERROR}",
627 "ERROR", e);
Faisal Awada760053d2024-05-16 13:31:32 -0500628 }
629}
630
631void ItemUpdater::processPSUImageAndSyncToLatest()
632{
633 processPSUImage();
634 processStoredImage();
635 syncToLatestImage();
636}
637
Faisal Awadafb86e792024-09-11 10:51:17 -0500638std::string ItemUpdater::getFWVersionFromBuiltinDir()
639{
640 std::string version;
641 for (const auto& activation : activations)
642 {
643 if (activation.second->path().starts_with(IMG_DIR_BUILTIN))
644 {
645 std::string versionId = activation.second->getVersionId();
646 auto it = versions.find(versionId);
647 if (it != versions.end())
648 {
649 const auto& versionPtr = it->second;
650 version = versionPtr->version();
651 break;
652 }
653 }
654 }
655 return version;
656}
657
Lei YU01539e72019-07-31 10:57:38 +0800658} // namespace updater
659} // namespace software
660} // namespace phosphor