blob: 8c92917ed90fbbd202d8d689125883a40ab8376f [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
Shawn McCarney799f5142024-09-26 14:50:25 -050083 // If we are only installing PSU images from the built-in directory, ignore
84 // PSU images from other directories
85 if (ALWAYS_USE_BUILTIN_IMG_DIR && !filePath.starts_with(IMG_DIR_BUILTIN))
86 {
87 return;
88 }
89
Lei YU01539e72019-07-31 10:57:38 +080090 // Version id is the last item in the path
George Liua0f2cf72024-08-23 14:50:12 +080091 auto pos = path.rfind('/');
Lei YU01539e72019-07-31 10:57:38 +080092 if (pos == std::string::npos)
93 {
94 log<level::ERR>("No version id found in object path",
95 entry("OBJPATH=%s", path.c_str()));
96 return;
97 }
98
99 auto versionId = path.substr(pos + 1);
100
101 if (activations.find(versionId) == activations.end())
102 {
103 // Determine the Activation state by processing the given image dir.
Lei YU91029442019-08-01 15:57:31 +0800104 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800105 auto activationState = Activation::Status::Ready;
Lei YU01539e72019-07-31 10:57:38 +0800106
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400107 associations.emplace_back(std::make_tuple(
108 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
109 PSU_INVENTORY_PATH_BASE));
Lei YU91029442019-08-01 15:57:31 +0800110
Lei YU01539e72019-07-31 10:57:38 +0800111 fs::path manifestPath(filePath);
112 manifestPath /= MANIFEST_FILE;
Lei YU58c26e32019-09-27 17:52:06 +0800113 std::string extendedVersion =
114 Version::getValue(manifestPath, {MANIFEST_EXTENDED_VERSION});
Lei YU01539e72019-07-31 10:57:38 +0800115
Lei YU99301372019-09-29 16:27:12 +0800116 auto activation =
117 createActivationObject(path, versionId, extendedVersion,
118 activationState, associations, filePath);
Lei YU01539e72019-07-31 10:57:38 +0800119 activations.emplace(versionId, std::move(activation));
120
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400121 auto versionPtr =
122 createVersionObject(path, versionId, version, purpose);
Lei YU01539e72019-07-31 10:57:38 +0800123 versions.emplace(versionId, std::move(versionPtr));
124 }
125 return;
126}
127
Lei YUa5c47bb2019-09-29 11:28:53 +0800128void ItemUpdater::erase(const std::string& versionId)
Lei YU01539e72019-07-31 10:57:38 +0800129{
130 auto it = versions.find(versionId);
131 if (it == versions.end())
132 {
133 log<level::ERR>(("Error: Failed to find version " + versionId +
134 " in item updater versions map."
135 " Unable to remove.")
136 .c_str());
137 }
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 {
148 log<level::ERR>(("Error: Failed to find version " + versionId +
149 " in item updater activations map."
150 " Unable to remove.")
151 .c_str());
152 }
153 else
154 {
155 activations.erase(versionId);
156 }
157}
158
Lei YU91029442019-08-01 15:57:31 +0800159void ItemUpdater::createActiveAssociation(const std::string& path)
160{
161 assocs.emplace_back(
162 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
163 associations(assocs);
164}
165
Lei YUad90ad52019-08-06 11:19:28 +0800166void ItemUpdater::addFunctionalAssociation(const std::string& path)
Lei YU91029442019-08-01 15:57:31 +0800167{
Lei YU91029442019-08-01 15:57:31 +0800168 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
169 FUNCTIONAL_REV_ASSOCIATION, path));
170 associations(assocs);
171}
172
Lei YUa8b966f2020-03-18 10:32:24 +0800173void ItemUpdater::addUpdateableAssociation(const std::string& path)
174{
175 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
176 UPDATEABLE_REV_ASSOCIATION, path));
177 associations(assocs);
178}
179
Lei YU91029442019-08-01 15:57:31 +0800180void ItemUpdater::removeAssociation(const std::string& path)
181{
182 for (auto iter = assocs.begin(); iter != assocs.end();)
183 {
George Liua5205e42024-08-23 15:27:54 +0800184 if ((std::get<2>(*iter)) == path)
Lei YU91029442019-08-01 15:57:31 +0800185 {
186 iter = assocs.erase(iter);
187 associations(assocs);
188 }
189 else
190 {
191 ++iter;
192 }
193 }
194}
195
Lei YUffb36532019-10-15 13:55:24 +0800196void ItemUpdater::onUpdateDone(const std::string& versionId,
197 const std::string& psuInventoryPath)
198{
199 // After update is done, remove old activation objects
200 for (auto it = activations.begin(); it != activations.end(); ++it)
201 {
202 if (it->second->getVersionId() != versionId &&
203 utils::isAssociated(psuInventoryPath, it->second->associations()))
204 {
205 removePsuObject(psuInventoryPath);
206 break;
207 }
208 }
Lei YU1517f5f2019-10-14 16:44:42 +0800209
210 auto it = activations.find(versionId);
211 assert(it != activations.end());
212 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUffb36532019-10-15 13:55:24 +0800213}
214
Lei YU01539e72019-07-31 10:57:38 +0800215std::unique_ptr<Activation> ItemUpdater::createActivationObject(
216 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800217 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800218 const AssociationList& assocs, const std::string& filePath)
Lei YU01539e72019-07-31 10:57:38 +0800219{
220 return std::make_unique<Activation>(bus, path, versionId, extVersion,
Lei YUffb36532019-10-15 13:55:24 +0800221 activationStatus, assocs, filePath,
222 this, this);
Lei YU01539e72019-07-31 10:57:38 +0800223}
224
Lei YUad90ad52019-08-06 11:19:28 +0800225void ItemUpdater::createPsuObject(const std::string& psuInventoryPath,
226 const std::string& psuVersion)
227{
228 auto versionId = utils::getVersionId(psuVersion);
229 auto path = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
230
231 auto it = activations.find(versionId);
232 if (it != activations.end())
233 {
234 // The versionId is already created, associate the path
235 auto associations = it->second->associations();
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400236 associations.emplace_back(
237 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
238 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800239 it->second->associations(associations);
Lei YUbd3b0072019-08-08 13:09:50 +0800240 psuPathActivationMap.emplace(psuInventoryPath, it->second);
Lei YUad90ad52019-08-06 11:19:28 +0800241 }
242 else
243 {
244 // Create a new object for running PSU inventory
245 AssociationList associations;
Lei YU58c26e32019-09-27 17:52:06 +0800246 auto activationState = Activation::Status::Active;
Lei YUad90ad52019-08-06 11:19:28 +0800247
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400248 associations.emplace_back(
249 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
250 ACTIVATION_REV_ASSOCIATION, psuInventoryPath));
Lei YUad90ad52019-08-06 11:19:28 +0800251
Lei YU99301372019-09-29 16:27:12 +0800252 auto activation = createActivationObject(
253 path, versionId, "", activationState, associations, "");
Lei YUad90ad52019-08-06 11:19:28 +0800254 activations.emplace(versionId, std::move(activation));
Lei YUbd3b0072019-08-08 13:09:50 +0800255 psuPathActivationMap.emplace(psuInventoryPath, activations[versionId]);
Lei YUad90ad52019-08-06 11:19:28 +0800256
257 auto versionPtr = createVersionObject(path, versionId, psuVersion,
Lei YU99301372019-09-29 16:27:12 +0800258 VersionPurpose::PSU);
Lei YUad90ad52019-08-06 11:19:28 +0800259 versions.emplace(versionId, std::move(versionPtr));
260
261 createActiveAssociation(path);
262 addFunctionalAssociation(path);
Lei YUa8b966f2020-03-18 10:32:24 +0800263 addUpdateableAssociation(path);
Lei YUad90ad52019-08-06 11:19:28 +0800264 }
265}
266
Lei YUbd3b0072019-08-08 13:09:50 +0800267void ItemUpdater::removePsuObject(const std::string& psuInventoryPath)
268{
Lei YU1517f5f2019-10-14 16:44:42 +0800269 psuStatusMap[psuInventoryPath] = {false, ""};
270
Lei YUbd3b0072019-08-08 13:09:50 +0800271 auto it = psuPathActivationMap.find(psuInventoryPath);
272 if (it == psuPathActivationMap.end())
273 {
274 log<level::ERR>("No Activation found for PSU",
275 entry("PSUPATH=%s", psuInventoryPath.c_str()));
276 return;
277 }
278 const auto& activationPtr = it->second;
279 psuPathActivationMap.erase(psuInventoryPath);
280
281 auto associations = activationPtr->associations();
282 for (auto iter = associations.begin(); iter != associations.end();)
283 {
George Liua5205e42024-08-23 15:27:54 +0800284 if ((std::get<2>(*iter)) == psuInventoryPath)
Lei YUbd3b0072019-08-08 13:09:50 +0800285 {
286 iter = associations.erase(iter);
287 }
288 else
289 {
290 ++iter;
291 }
292 }
293 if (associations.empty())
294 {
295 // Remove the activation
Lei YUa5c47bb2019-09-29 11:28:53 +0800296 erase(activationPtr->getVersionId());
Lei YUbd3b0072019-08-08 13:09:50 +0800297 }
298 else
299 {
300 // Update association
301 activationPtr->associations(associations);
302 }
303}
304
Lei YU01539e72019-07-31 10:57:38 +0800305std::unique_ptr<Version> ItemUpdater::createVersionObject(
306 const std::string& objPath, const std::string& versionId,
307 const std::string& versionString,
308 sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
Lei YU99301372019-09-29 16:27:12 +0800309 versionPurpose)
Lei YU01539e72019-07-31 10:57:38 +0800310{
Lei YU65207482019-10-11 16:39:36 +0800311 versionStrings.insert(versionString);
Lei YU01539e72019-07-31 10:57:38 +0800312 auto version = std::make_unique<Version>(
Lei YU99301372019-09-29 16:27:12 +0800313 bus, objPath, versionId, versionString, versionPurpose,
Lei YU01539e72019-07-31 10:57:38 +0800314 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
315 return version;
316}
317
Patrick Williams374fae52022-07-22 19:26:55 -0500318void ItemUpdater::onPsuInventoryChangedMsg(sdbusplus::message_t& msg)
Lei YUad90ad52019-08-06 11:19:28 +0800319{
Lei YUbd3b0072019-08-08 13:09:50 +0800320 using Interface = std::string;
Lei YUbd3b0072019-08-08 13:09:50 +0800321 Interface interface;
322 Properties properties;
Lei YUbd3b0072019-08-08 13:09:50 +0800323 std::string psuPath = msg.get_path();
324
325 msg.read(interface, properties);
Lei YUa2c2cd72019-08-09 15:54:10 +0800326 onPsuInventoryChanged(psuPath, properties);
327}
328
329void ItemUpdater::onPsuInventoryChanged(const std::string& psuPath,
330 const Properties& properties)
331{
Lei YU1517f5f2019-10-14 16:44:42 +0800332 std::optional<bool> present;
333 std::optional<std::string> model;
Lei YUbd3b0072019-08-08 13:09:50 +0800334
Lei YU1517f5f2019-10-14 16:44:42 +0800335 // The code was expecting to get callback on multiple properties changed.
336 // But in practice, the callback is received one-by-one for each property.
337 // So it has to handle Present and Version property separately.
Lei YUf77189f2019-08-07 14:26:30 +0800338 auto p = properties.find(PRESENT);
Lei YU1517f5f2019-10-14 16:44:42 +0800339 if (p != properties.end())
340 {
341 present = std::get<bool>(p->second);
342 psuStatusMap[psuPath].present = *present;
343 }
344 p = properties.find(MODEL);
345 if (p != properties.end())
346 {
347 model = std::get<std::string>(p->second);
348 psuStatusMap[psuPath].model = *model;
349 }
350
351 // If present or model is not changed, ignore
352 if (!present.has_value() && !model.has_value())
Lei YUbd3b0072019-08-08 13:09:50 +0800353 {
354 return;
355 }
356
Lei YU1517f5f2019-10-14 16:44:42 +0800357 if (psuStatusMap[psuPath].present)
Lei YUbd3b0072019-08-08 13:09:50 +0800358 {
Lei YU1517f5f2019-10-14 16:44:42 +0800359 // If model is not updated, let's wait for it
360 if (psuStatusMap[psuPath].model.empty())
361 {
362 log<level::DEBUG>("Waiting for model to be updated");
363 return;
364 }
365
366 auto version = utils::getVersion(psuPath);
Lei YUdcaf8932019-09-09 16:09:35 +0800367 if (!version.empty())
Lei YUbd3b0072019-08-08 13:09:50 +0800368 {
Faisal Awada760053d2024-05-16 13:31:32 -0500369 if (psuPathActivationMap.find(psuPath) ==
370 psuPathActivationMap.end())
371 {
372 createPsuObject(psuPath, version);
373 }
Lei YU1517f5f2019-10-14 16:44:42 +0800374 // Check if there is new PSU images to update
375 syncToLatestImage();
376 }
377 else
378 {
379 // TODO: log an event
380 log<level::ERR>("Failed to get PSU version",
381 entry("PSU=%s", psuPath.c_str()));
Lei YUbd3b0072019-08-08 13:09:50 +0800382 }
Lei YUbd3b0072019-08-08 13:09:50 +0800383 }
384 else
385 {
Lei YU1517f5f2019-10-14 16:44:42 +0800386 if (!present.has_value())
387 {
388 // If a PSU is plugged out, model property is update to empty as
389 // well, and we get callback here, but ignore that because it is
390 // handled by "Present" callback.
391 return;
392 }
Lei YUbd3b0072019-08-08 13:09:50 +0800393 // Remove object or association
394 removePsuObject(psuPath);
395 }
Lei YUad90ad52019-08-06 11:19:28 +0800396}
397
398void ItemUpdater::processPSUImage()
399{
400 auto paths = utils::getPSUInventoryPath(bus);
401 for (const auto& p : paths)
402 {
Lei YU5f3584d2019-08-27 16:28:53 +0800403 auto service = utils::getService(bus, p.c_str(), ITEM_IFACE);
Lei YUad90ad52019-08-06 11:19:28 +0800404 auto present = utils::getProperty<bool>(bus, service.c_str(), p.c_str(),
Lei YUf77189f2019-08-07 14:26:30 +0800405 ITEM_IFACE, PRESENT);
Faisal Awada760053d2024-05-16 13:31:32 -0500406 psuStatusMap[p].model = utils::getProperty<std::string>(
407 bus, service.c_str(), p.c_str(), ASSET_IFACE, MODEL);
Lei YU5f3584d2019-08-27 16:28:53 +0800408 auto version = utils::getVersion(p);
Faisal Awada760053d2024-05-16 13:31:32 -0500409 if ((psuPathActivationMap.find(p) == psuPathActivationMap.end()) &&
410 present && !version.empty())
Lei YUad90ad52019-08-06 11:19:28 +0800411 {
412 createPsuObject(p, version);
Faisal Awada760053d2024-05-16 13:31:32 -0500413 // Add matches for PSU Inventory's property changes
414 psuMatches.emplace_back(
415 bus, MatchRules::propertiesChanged(p, ITEM_IFACE),
416 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
417 std::placeholders::_1)); // For present
418 psuMatches.emplace_back(
419 bus, MatchRules::propertiesChanged(p, ASSET_IFACE),
420 std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this,
421 std::placeholders::_1)); // For model
Lei YUad90ad52019-08-06 11:19:28 +0800422 }
Lei YUad90ad52019-08-06 11:19:28 +0800423 }
424}
425
Lei YU58c26e32019-09-27 17:52:06 +0800426void ItemUpdater::processStoredImage()
427{
428 scanDirectory(IMG_DIR_BUILTIN);
Faisal Awada760053d2024-05-16 13:31:32 -0500429
Faisal Awadafb86e792024-09-11 10:51:17 -0500430 if (!ALWAYS_USE_BUILTIN_IMG_DIR)
431 {
432 scanDirectory(IMG_DIR_PERSIST);
433 }
Lei YU58c26e32019-09-27 17:52:06 +0800434}
435
436void ItemUpdater::scanDirectory(const fs::path& dir)
437{
Faisal Awada760053d2024-05-16 13:31:32 -0500438 auto manifest = dir;
439 auto path = dir;
Lei YU58c26e32019-09-27 17:52:06 +0800440 // The directory shall put PSU images in directories named with model
441 if (!fs::exists(dir))
442 {
443 // Skip
444 return;
445 }
446 if (!fs::is_directory(dir))
447 {
448 log<level::ERR>("The path is not a directory",
449 entry("PATH=%s", dir.c_str()));
450 return;
451 }
Faisal Awada760053d2024-05-16 13:31:32 -0500452
453 for (const auto& [key, item] : psuStatusMap)
Lei YU58c26e32019-09-27 17:52:06 +0800454 {
Faisal Awada760053d2024-05-16 13:31:32 -0500455 if (!item.model.empty())
Lei YU58c26e32019-09-27 17:52:06 +0800456 {
Faisal Awada760053d2024-05-16 13:31:32 -0500457 path = path / item.model;
458 manifest = dir / item.model / MANIFEST_FILE;
459 break;
460 }
461 }
462 if (path == dir)
463 {
464 log<level::ERR>("Model directory not found");
465 return;
466 }
467
468 if (!fs::is_directory(path))
469 {
470 log<level::ERR>("The path is not a directory",
471 entry("PATH=%s", path.c_str()));
472 return;
473 }
474
475 if (!fs::exists(manifest))
476 {
477 log<level::ERR>("No MANIFEST found",
478 entry("PATH=%s", manifest.c_str()));
479 return;
480 }
481 // If the model in manifest does not match the dir name
482 // Log a warning
483 if (fs::is_regular_file(manifest))
484 {
485 auto ret = Version::getValues(
486 manifest.string(), {MANIFEST_VERSION, MANIFEST_EXTENDED_VERSION});
487 auto version = ret[MANIFEST_VERSION];
488 auto extVersion = ret[MANIFEST_EXTENDED_VERSION];
489 auto info = Version::getExtVersionInfo(extVersion);
490 auto model = info["model"];
491 if (path.stem() != model)
492 {
493 log<level::ERR>("Unmatched model", entry("PATH=%s", path.c_str()),
494 entry("MODEL=%s", model.c_str()));
495 }
496 else
497 {
Lei YU58c26e32019-09-27 17:52:06 +0800498 auto versionId = utils::getVersionId(version);
499 auto it = activations.find(versionId);
500 if (it == activations.end())
501 {
502 // This is a version that is different than the running PSUs
503 auto activationState = Activation::Status::Ready;
504 auto purpose = VersionPurpose::PSU;
505 auto objPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
506
507 auto activation = createActivationObject(
508 objPath, versionId, extVersion, activationState, {}, path);
509 activations.emplace(versionId, std::move(activation));
510
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400511 auto versionPtr =
512 createVersionObject(objPath, versionId, version, purpose);
Lei YU58c26e32019-09-27 17:52:06 +0800513 versions.emplace(versionId, std::move(versionPtr));
514 }
515 else
516 {
517 // This is a version that a running PSU is using, set the path
518 // on the version object
519 it->second->path(path);
520 }
521 }
Faisal Awada760053d2024-05-16 13:31:32 -0500522 }
523 else
524 {
525 log<level::ERR>("MANIFEST is not a file",
526 entry("PATH=%s", manifest.c_str()));
Lei YU58c26e32019-09-27 17:52:06 +0800527 }
528}
529
Lei YU65207482019-10-11 16:39:36 +0800530std::optional<std::string> ItemUpdater::getLatestVersionId()
531{
Faisal Awadafb86e792024-09-11 10:51:17 -0500532 std::string latestVersion;
533 if (ALWAYS_USE_BUILTIN_IMG_DIR)
534 {
535 latestVersion = getFWVersionFromBuiltinDir();
536 }
537 else
538 {
539 latestVersion = utils::getLatestVersion(versionStrings);
540 }
Lei YU65207482019-10-11 16:39:36 +0800541 if (latestVersion.empty())
542 {
543 return {};
544 }
545
546 std::optional<std::string> versionId;
547 for (const auto& v : versions)
548 {
549 if (v.second->version() == latestVersion)
550 {
551 versionId = v.first;
552 break;
553 }
554 }
555 assert(versionId.has_value());
556 return versionId;
557}
558
Lei YU63f9e712019-10-12 15:16:55 +0800559void ItemUpdater::syncToLatestImage()
560{
561 auto latestVersionId = getLatestVersionId();
562 if (!latestVersionId)
563 {
564 return;
565 }
566 const auto& it = activations.find(*latestVersionId);
567 assert(it != activations.end());
568 const auto& activation = it->second;
569 const auto& assocs = activation->associations();
570
571 auto paths = utils::getPSUInventoryPath(bus);
572 for (const auto& p : paths)
573 {
Faisal Awada760053d2024-05-16 13:31:32 -0500574 // As long as there is a PSU is not associated with the latest
575 // image, run the activation so that all PSUs are running the same
576 // latest image.
Lei YU63f9e712019-10-12 15:16:55 +0800577 if (!utils::isAssociated(p, assocs))
578 {
579 log<level::INFO>("Automatically update PSU",
580 entry("VERSION_ID=%s", latestVersionId->c_str()));
581 invokeActivation(activation);
582 break;
583 }
584 }
585}
586
587void ItemUpdater::invokeActivation(
588 const std::unique_ptr<Activation>& activation)
589{
590 activation->requestedActivation(Activation::RequestedActivations::Active);
591}
592
Faisal Awada760053d2024-05-16 13:31:32 -0500593void ItemUpdater::onPSUInterfaceAdded(sdbusplus::message_t& msg)
594{
595 sdbusplus::message::object_path objPath;
596 std::map<std::string,
597 std::map<std::string, std::variant<bool, std::string>>>
598 interfaces;
599 msg.read(objPath, interfaces);
600 std::string path = objPath.str;
601
602 if (interfaces.find(PSU_INVENTORY_IFACE) == interfaces.end() ||
603 (psuStatusMap[path].present && !psuStatusMap[path].model.empty()))
604 {
605 return;
606 }
607
608 auto timeout = std::chrono::steady_clock::now() +
609 std::chrono::seconds(TIMEOUT);
610
611 // Poll the inventory item until it gets the present property
612 // or the timeout is reached
613 while (std::chrono::steady_clock::now() < timeout)
614 {
615 try
616 {
617 psuStatusMap[path].present = utils::getProperty<bool>(
618 bus, msg.get_sender(), path.c_str(), ITEM_IFACE, PRESENT);
619 break;
620 }
621 catch (const std::exception& e)
622 {
623 auto err = errno;
624 log<level::INFO>(
625 std::format("Failed to get Inventory Item Present. errno={}",
626 err)
627 .c_str());
628 sleep(1);
629 }
630 }
631
632 // Poll the inventory item until it retrieves model or the timeout is
633 // reached. The model is the path trail of the firmware's and manifest
634 // subdirectory. If the model not found the firmware and manifest
635 // cannot be located.
636 timeout = std::chrono::steady_clock::now() + std::chrono::seconds(TIMEOUT);
637 while (std::chrono::steady_clock::now() < timeout &&
638 psuStatusMap[path].present)
639 {
640 try
641 {
642 psuStatusMap[path].model = utils::getProperty<std::string>(
643 bus, msg.get_sender(), path.c_str(), ASSET_IFACE, MODEL);
644 processPSUImageAndSyncToLatest();
645 break;
646 }
647 catch (const std::exception& e)
648 {
649 auto err = errno;
650 log<level::INFO>(
651 std::format(
652 "Failed to get Inventory Decorator Asset model. errno={}",
653 err)
654 .c_str());
655 sleep(1);
656 }
657 }
658}
659
660void ItemUpdater::processPSUImageAndSyncToLatest()
661{
662 processPSUImage();
663 processStoredImage();
664 syncToLatestImage();
665}
666
Faisal Awadafb86e792024-09-11 10:51:17 -0500667std::string ItemUpdater::getFWVersionFromBuiltinDir()
668{
669 std::string version;
670 for (const auto& activation : activations)
671 {
672 if (activation.second->path().starts_with(IMG_DIR_BUILTIN))
673 {
674 std::string versionId = activation.second->getVersionId();
675 auto it = versions.find(versionId);
676 if (it != versions.end())
677 {
678 const auto& versionPtr = it->second;
679 version = versionPtr->version();
680 break;
681 }
682 }
683 }
684 return version;
685}
686
Lei YU01539e72019-07-31 10:57:38 +0800687} // namespace updater
688} // namespace software
689} // namespace phosphor