Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "item_updater.hpp" |
| 4 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
| 6 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 8 | #include <phosphor-logging/lg2.hpp> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 11 | #include <cassert> |
| 12 | #include <filesystem> |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 13 | #include <format> |
| 14 | #include <set> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 15 | |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 16 | namespace |
| 17 | { |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 18 | constexpr auto MANIFEST_VERSION = "version"; |
| 19 | constexpr auto MANIFEST_EXTENDED_VERSION = "extended_version"; |
| 20 | } // namespace |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 21 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace software |
| 25 | { |
| 26 | namespace updater |
| 27 | { |
| 28 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 29 | |
| 30 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 31 | using namespace phosphor::logging; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 32 | using SVersion = server::Version; |
| 33 | using VersionPurpose = SVersion::VersionPurpose; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 34 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 35 | void ItemUpdater::createActivation(sdbusplus::message_t& m) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 36 | { |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 37 | sdbusplus::message::object_path objPath; |
George Liu | de27029 | 2020-06-12 17:14:17 +0800 | [diff] [blame] | 38 | std::map<std::string, std::map<std::string, std::variant<std::string>>> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 39 | 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 YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 57 | std::get<std::string>(propertyValue)); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 58 | |
| 59 | if (value == VersionPurpose::PSU) |
| 60 | { |
| 61 | purpose = value; |
| 62 | } |
| 63 | } |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 64 | else if (propertyName == VERSION) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 65 | { |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 66 | version = std::get<std::string>(propertyValue); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | else if (interfaceName == FILEPATH_IFACE) |
| 71 | { |
| 72 | const auto& it = propertyMap.find("Path"); |
| 73 | if (it != propertyMap.end()) |
| 74 | { |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 75 | filePath = std::get<std::string>(it->second); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | if ((filePath.empty()) || (purpose == VersionPurpose::Unknown)) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | |
Shawn McCarney | 799f514 | 2024-09-26 14:50:25 -0500 | [diff] [blame] | 84 | // 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 YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 91 | // Version id is the last item in the path |
George Liu | a0f2cf7 | 2024-08-23 14:50:12 +0800 | [diff] [blame] | 92 | auto pos = path.rfind('/'); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 93 | if (pos == std::string::npos) |
| 94 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 95 | lg2::error("No version id found in object path {OBJPATH}", "OBJPATH", |
| 96 | path); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 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 YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 105 | AssociationList associations; |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 106 | auto activationState = Activation::Status::Ready; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 107 | |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 108 | associations.emplace_back(std::make_tuple( |
| 109 | ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 110 | PSU_INVENTORY_PATH_BASE)); |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 111 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 112 | fs::path manifestPath(filePath); |
| 113 | manifestPath /= MANIFEST_FILE; |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 114 | std::string extendedVersion = |
| 115 | Version::getValue(manifestPath, {MANIFEST_EXTENDED_VERSION}); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 116 | |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 117 | auto activation = |
| 118 | createActivationObject(path, versionId, extendedVersion, |
| 119 | activationState, associations, filePath); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 120 | activations.emplace(versionId, std::move(activation)); |
| 121 | |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 122 | auto versionPtr = |
| 123 | createVersionObject(path, versionId, version, purpose); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 124 | versions.emplace(versionId, std::move(versionPtr)); |
| 125 | } |
| 126 | return; |
| 127 | } |
| 128 | |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 129 | void ItemUpdater::erase(const std::string& versionId) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 130 | { |
| 131 | auto it = versions.find(versionId); |
| 132 | if (it == versions.end()) |
| 133 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 134 | lg2::error("Error: Failed to find version {VERSION_ID} in " |
| 135 | "item updater versions map. Unable to remove.", |
| 136 | "VERSION_ID", versionId); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 137 | } |
| 138 | else |
| 139 | { |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 140 | versionStrings.erase(it->second->getVersionString()); |
| 141 | versions.erase(it); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Removing entry in activations map |
| 145 | auto ita = activations.find(versionId); |
| 146 | if (ita == activations.end()) |
| 147 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 148 | lg2::error("Error: Failed to find version {VERSION_ID} in " |
| 149 | "item updater activations map. Unable to remove.", |
| 150 | "VERSION_ID", versionId); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | { |
| 154 | activations.erase(versionId); |
| 155 | } |
| 156 | } |
| 157 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 158 | void 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 YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 165 | void ItemUpdater::addFunctionalAssociation(const std::string& path) |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 166 | { |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 167 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
| 168 | FUNCTIONAL_REV_ASSOCIATION, path)); |
| 169 | associations(assocs); |
| 170 | } |
| 171 | |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 172 | void 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 YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 179 | void ItemUpdater::removeAssociation(const std::string& path) |
| 180 | { |
| 181 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 182 | { |
George Liu | a5205e4 | 2024-08-23 15:27:54 +0800 | [diff] [blame] | 183 | if ((std::get<2>(*iter)) == path) |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 184 | { |
| 185 | iter = assocs.erase(iter); |
| 186 | associations(assocs); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | ++iter; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 195 | void 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 YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 208 | |
| 209 | auto it = activations.find(versionId); |
| 210 | assert(it != activations.end()); |
| 211 | psuPathActivationMap.emplace(psuInventoryPath, it->second); |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 214 | std::unique_ptr<Activation> ItemUpdater::createActivationObject( |
| 215 | const std::string& path, const std::string& versionId, |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 216 | const std::string& extVersion, Activation::Status activationStatus, |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 217 | const AssociationList& assocs, const std::string& filePath) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 218 | { |
| 219 | return std::make_unique<Activation>(bus, path, versionId, extVersion, |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 220 | activationStatus, assocs, filePath, |
| 221 | this, this); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 224 | void 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 Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 235 | associations.emplace_back( |
| 236 | std::make_tuple(ACTIVATION_FWD_ASSOCIATION, |
| 237 | ACTIVATION_REV_ASSOCIATION, psuInventoryPath)); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 238 | it->second->associations(associations); |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 239 | psuPathActivationMap.emplace(psuInventoryPath, it->second); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | { |
| 243 | // Create a new object for running PSU inventory |
| 244 | AssociationList associations; |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 245 | auto activationState = Activation::Status::Active; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 246 | |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 247 | associations.emplace_back( |
| 248 | std::make_tuple(ACTIVATION_FWD_ASSOCIATION, |
| 249 | ACTIVATION_REV_ASSOCIATION, psuInventoryPath)); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 250 | |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 251 | auto activation = createActivationObject( |
| 252 | path, versionId, "", activationState, associations, ""); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 253 | activations.emplace(versionId, std::move(activation)); |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 254 | psuPathActivationMap.emplace(psuInventoryPath, activations[versionId]); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 255 | |
| 256 | auto versionPtr = createVersionObject(path, versionId, psuVersion, |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 257 | VersionPurpose::PSU); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 258 | versions.emplace(versionId, std::move(versionPtr)); |
| 259 | |
| 260 | createActiveAssociation(path); |
| 261 | addFunctionalAssociation(path); |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 262 | addUpdateableAssociation(path); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 266 | void ItemUpdater::removePsuObject(const std::string& psuInventoryPath) |
| 267 | { |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 268 | auto it = psuPathActivationMap.find(psuInventoryPath); |
| 269 | if (it == psuPathActivationMap.end()) |
| 270 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 271 | lg2::error("No Activation found for PSU {PSUPATH}", "PSUPATH", |
| 272 | psuInventoryPath); |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 273 | 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 Liu | a5205e4 | 2024-08-23 15:27:54 +0800 | [diff] [blame] | 281 | if ((std::get<2>(*iter)) == psuInventoryPath) |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 282 | { |
| 283 | iter = associations.erase(iter); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | ++iter; |
| 288 | } |
| 289 | } |
| 290 | if (associations.empty()) |
| 291 | { |
| 292 | // Remove the activation |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 293 | erase(activationPtr->getVersionId()); |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 294 | } |
| 295 | else |
| 296 | { |
| 297 | // Update association |
| 298 | activationPtr->associations(associations); |
| 299 | } |
| 300 | } |
| 301 | |
Shawn McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 302 | void ItemUpdater::addPsuToStatusMap(const std::string& psuPath) |
| 303 | { |
| 304 | if (!psuStatusMap.contains(psuPath)) |
| 305 | { |
| 306 | psuStatusMap[psuPath] = {false, ""}; |
| 307 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 308 | // Add PropertiesChanged listener for Item interface so we are notified |
| 309 | // when Present property changes |
Shawn McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 310 | psuMatches.emplace_back( |
| 311 | bus, MatchRules::propertiesChanged(psuPath, ITEM_IFACE), |
| 312 | std::bind(&ItemUpdater::onPsuInventoryChangedMsg, this, |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 313 | std::placeholders::_1)); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void 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 McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 343 | std::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 YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 347 | versionPurpose) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 348 | { |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 349 | versionStrings.insert(versionString); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 350 | auto version = std::make_unique<Version>( |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 351 | bus, objPath, versionId, versionString, versionPurpose, |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 352 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1)); |
| 353 | return version; |
| 354 | } |
| 355 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 356 | void ItemUpdater::onPsuInventoryChangedMsg(sdbusplus::message_t& msg) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 357 | { |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 358 | using Interface = std::string; |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 359 | Interface interface; |
| 360 | Properties properties; |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 361 | std::string psuPath = msg.get_path(); |
| 362 | |
| 363 | msg.read(interface, properties); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 364 | onPsuInventoryChanged(psuPath, properties); |
| 365 | } |
| 366 | |
| 367 | void ItemUpdater::onPsuInventoryChanged(const std::string& psuPath, |
| 368 | const Properties& properties) |
| 369 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 370 | try |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 371 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 372 | if (psuStatusMap.contains(psuPath) && properties.contains(PRESENT)) |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 373 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 374 | psuStatusMap[psuPath].present = |
| 375 | std::get<bool>(properties.at(PRESENT)); |
| 376 | handlePSUPresenceChanged(psuPath); |
| 377 | if (psuStatusMap[psuPath].present) |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 378 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 379 | // Check if there are new PSU images to update |
| 380 | processStoredImage(); |
| 381 | syncToLatestImage(); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 382 | } |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 383 | } |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 384 | } |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 385 | catch (const std::exception& e) |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 386 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 387 | lg2::error( |
| 388 | "Unable to handle inventory PropertiesChanged event: {ERROR}", |
| 389 | "ERROR", e); |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 390 | } |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void ItemUpdater::processPSUImage() |
| 394 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 395 | try |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 396 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 397 | auto paths = utils::getPSUInventoryPath(bus); |
| 398 | for (const auto& p : paths) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 399 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 400 | 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 YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 412 | } |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 413 | } |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 414 | catch (const std::exception& e) |
| 415 | { |
| 416 | // Ignore errors; the information might not be available yet |
| 417 | } |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 420 | void ItemUpdater::processStoredImage() |
| 421 | { |
| 422 | scanDirectory(IMG_DIR_BUILTIN); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 423 | |
Faisal Awada | fb86e79 | 2024-09-11 10:51:17 -0500 | [diff] [blame] | 424 | if (!ALWAYS_USE_BUILTIN_IMG_DIR) |
| 425 | { |
| 426 | scanDirectory(IMG_DIR_PERSIST); |
| 427 | } |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void ItemUpdater::scanDirectory(const fs::path& dir) |
| 431 | { |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 432 | auto manifest = dir; |
| 433 | auto path = dir; |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 434 | // 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 McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 442 | lg2::error("The path is not a directory: {PATH}", "PATH", dir); |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 443 | return; |
| 444 | } |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 445 | |
| 446 | for (const auto& [key, item] : psuStatusMap) |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 447 | { |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 448 | if (!item.model.empty()) |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 449 | { |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 450 | path = path / item.model; |
| 451 | manifest = dir / item.model / MANIFEST_FILE; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | if (path == dir) |
| 456 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 457 | lg2::error("Model directory not found"); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
| 461 | if (!fs::is_directory(path)) |
| 462 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 463 | lg2::error("The path is not a directory: {PATH}", "PATH", path); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (!fs::exists(manifest)) |
| 468 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 469 | lg2::error("No MANIFEST found at {PATH}", "PATH", manifest); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 470 | 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 McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 484 | lg2::error("Unmatched model: path={PATH}, model={MODEL}", "PATH", |
| 485 | path, "MODEL", model); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 486 | } |
| 487 | else |
| 488 | { |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 489 | 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 Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 502 | auto versionPtr = |
| 503 | createVersionObject(objPath, versionId, version, purpose); |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 504 | 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 Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 513 | } |
| 514 | else |
| 515 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 516 | lg2::error("MANIFEST is not a file: {PATH}", "PATH", manifest); |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 520 | std::optional<std::string> ItemUpdater::getLatestVersionId() |
| 521 | { |
Faisal Awada | fb86e79 | 2024-09-11 10:51:17 -0500 | [diff] [blame] | 522 | 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 YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 531 | 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 YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 549 | void 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 McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 564 | // If there is a present PSU that is not associated with the latest |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 565 | // image, run the activation so that all PSUs are running the same |
| 566 | // latest image. |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 567 | if (psuStatusMap.contains(p) && psuStatusMap[p].present) |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 568 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 569 | if (!utils::isAssociated(p, assocs)) |
| 570 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 571 | lg2::info("Automatically update PSUs to version {VERSION_ID}", |
| 572 | "VERSION_ID", *latestVersionId); |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 573 | invokeActivation(activation); |
| 574 | break; |
| 575 | } |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | void ItemUpdater::invokeActivation( |
| 581 | const std::unique_ptr<Activation>& activation) |
| 582 | { |
| 583 | activation->requestedActivation(Activation::RequestedActivations::Active); |
| 584 | } |
| 585 | |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 586 | void ItemUpdater::onPSUInterfaceAdded(sdbusplus::message_t& msg) |
| 587 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 588 | // 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 Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 591 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 592 | try |
Shawn McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 593 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 594 | 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 McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 600 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 601 | if (interfaces.contains(PSU_INVENTORY_IFACE)) |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 602 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 603 | psuPaths.insert(path); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 604 | } |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 605 | |
| 606 | if (interfaces.contains(ITEM_IFACE) && psuPaths.contains(path) && |
| 607 | !psuStatusMap.contains(path)) |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 608 | { |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 609 | 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 Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 622 | } |
| 623 | } |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 624 | catch (const std::exception& e) |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 625 | { |
Shawn McCarney | cdf86de | 2024-11-26 10:02:14 -0600 | [diff] [blame^] | 626 | lg2::error("Unable to handle inventory InterfacesAdded event: {ERROR}", |
| 627 | "ERROR", e); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | void ItemUpdater::processPSUImageAndSyncToLatest() |
| 632 | { |
| 633 | processPSUImage(); |
| 634 | processStoredImage(); |
| 635 | syncToLatestImage(); |
| 636 | } |
| 637 | |
Faisal Awada | fb86e79 | 2024-09-11 10:51:17 -0500 | [diff] [blame] | 638 | std::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 YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 658 | } // namespace updater |
| 659 | } // namespace software |
| 660 | } // namespace phosphor |