Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include "activation.hpp" |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 6 | #include "association_interface.hpp" |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 7 | #include "types.hpp" |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 8 | #include "utils.hpp" |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 9 | #include "version.hpp" |
| 10 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 12 | #include <sdbusplus/server.hpp> |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp> |
| 15 | |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 16 | #include <filesystem> |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 17 | #include <map> |
| 18 | #include <string> |
| 19 | #include <variant> |
| 20 | #include <vector> |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 21 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 22 | class TestItemUpdater; |
| 23 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 24 | namespace phosphor |
| 25 | { |
| 26 | namespace software |
| 27 | { |
| 28 | namespace updater |
| 29 | { |
| 30 | |
| 31 | class Version; |
| 32 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 33 | using ItemUpdaterInherit = sdbusplus::server::object_t< |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 34 | sdbusplus::xyz::openbmc_project::Association::server::Definitions>; |
| 35 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 36 | namespace MatchRules = sdbusplus::bus::match::rules; |
| 37 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 38 | namespace fs = std::filesystem; |
| 39 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 40 | /** @class ItemUpdater |
| 41 | * @brief Manages the activation of the PSU version items. |
| 42 | */ |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 43 | class ItemUpdater : |
| 44 | public ItemUpdaterInherit, |
| 45 | public AssociationInterface, |
| 46 | public ActivationListener |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 47 | { |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 48 | friend class ::TestItemUpdater; |
| 49 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 50 | public: |
| 51 | /** @brief Constructs ItemUpdater |
| 52 | * |
| 53 | * @param[in] bus - The D-Bus bus object |
| 54 | * @param[in] path - The D-Bus path |
| 55 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 56 | ItemUpdater(sdbusplus::bus_t& bus, const std::string& path) : |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 57 | ItemUpdaterInherit(bus, path.c_str()), bus(bus), |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 58 | versionMatch( |
| 59 | bus, |
| 60 | MatchRules::interfacesAdded() + MatchRules::path(SOFTWARE_OBJPATH), |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 61 | std::bind(std::mem_fn(&ItemUpdater::onVersionInterfacesAddedMsg), |
| 62 | this, std::placeholders::_1)), |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 63 | psuInterfaceMatch( |
| 64 | bus, |
| 65 | MatchRules::interfacesAdded() + |
| 66 | MatchRules::path("/xyz/openbmc_project/inventory") + |
| 67 | MatchRules::sender("xyz.openbmc_project.Inventory.Manager"), |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 68 | std::bind(std::mem_fn(&ItemUpdater::onPSUInterfacesAdded), this, |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 69 | std::placeholders::_1)) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 70 | { |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 71 | processPSUImageAndSyncToLatest(); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** @brief Deletes version |
| 75 | * |
| 76 | * @param[in] versionId - Id of the version to delete |
| 77 | */ |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 78 | void erase(const std::string& versionId); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 79 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 80 | /** @brief Creates an active association to the |
| 81 | * newly active software image |
| 82 | * |
| 83 | * @param[in] path - The path to create the association to. |
| 84 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 85 | void createActiveAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 86 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 87 | /** @brief Add the functional association to the |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 88 | * new "running" PSU images |
| 89 | * |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 90 | * @param[in] path - The path to add the association to. |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 91 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 92 | void addFunctionalAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 93 | |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 94 | /** @brief Add the updateable association to the |
| 95 | * "running" PSU software image |
| 96 | * |
| 97 | * @param[in] path - The path to create the association. |
| 98 | */ |
| 99 | void addUpdateableAssociation(const std::string& path) override; |
| 100 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 101 | /** @brief Removes the associations from the provided software image path |
| 102 | * |
| 103 | * @param[in] path - The path to remove the association from. |
| 104 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 105 | void removeAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 106 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 107 | /** @brief Notify a PSU is updated |
| 108 | * |
| 109 | * @param[in] versionId - The versionId of the activation |
| 110 | * @param[in] psuInventoryPath - The PSU inventory path that is updated |
| 111 | */ |
| 112 | void onUpdateDone(const std::string& versionId, |
| 113 | const std::string& psuInventoryPath) override; |
| 114 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 115 | private: |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 116 | using Properties = |
| 117 | std::map<std::string, utils::UtilsInterface::PropertyType>; |
| 118 | using InterfacesAddedMap = |
| 119 | std::map<std::string, |
| 120 | std::map<std::string, std::variant<bool, std::string>>>; |
| 121 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 122 | /** @brief Callback function for Software.Version match. |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 123 | * |
| 124 | * @param[in] msg - Data associated with subscribed signal |
| 125 | */ |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 126 | void onVersionInterfacesAddedMsg(sdbusplus::message_t& msg); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 127 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 128 | /** @brief Called when new Software.Version interfaces are found |
| 129 | * @details Creates an Activation D-Bus object if appropriate |
| 130 | * Throws an exception if an error occurs. |
| 131 | * |
| 132 | * @param[in] path - D-Bus object path |
| 133 | * @param[in] interfaces - D-Bus interfaces that were added |
| 134 | */ |
| 135 | void onVersionInterfacesAdded(const std::string& path, |
| 136 | const InterfacesAddedMap& interfaces); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 137 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 138 | /** @brief Callback function for PSU inventory match. |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 139 | * |
| 140 | * @param[in] msg - Data associated with subscribed signal |
| 141 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 142 | void onPsuInventoryChangedMsg(sdbusplus::message_t& msg); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 143 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 144 | /** @brief Called when a PSU inventory object has changed |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 145 | * @details Update an Activation D-Bus object for PSU inventory. |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 146 | * Throws an exception if an error occurs. |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 147 | * |
| 148 | * @param[in] psuPath - The PSU inventory path |
| 149 | * @param[in] properties - The updated properties |
| 150 | */ |
| 151 | void onPsuInventoryChanged(const std::string& psuPath, |
| 152 | const Properties& properties); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 153 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 154 | /** @brief Create Activation object */ |
| 155 | std::unique_ptr<Activation> createActivationObject( |
| 156 | const std::string& path, const std::string& versionId, |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 157 | const std::string& extVersion, Activation::Status activationStatus, |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 158 | const AssociationList& assocs, const std::string& filePath); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 159 | |
| 160 | /** @brief Create Version object */ |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 161 | std::unique_ptr<Version> createVersionObject( |
| 162 | const std::string& objPath, const std::string& versionId, |
| 163 | const std::string& versionString, |
| 164 | sdbusplus::xyz::openbmc_project::Software::server::Version:: |
| 165 | VersionPurpose versionPurpose); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 166 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 167 | /** @brief Create Activation and Version object for PSU inventory |
| 168 | * @details If the same version exists for multiple PSUs, just add |
| 169 | * related association, instead of creating new objects. |
| 170 | * */ |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 171 | void createPsuObject(const std::string& psuInventoryPath, |
| 172 | const std::string& psuVersion); |
| 173 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 174 | /** @brief Remove Activation and Version object for PSU inventory |
Manojkiran Eda | 33cf9f0 | 2024-06-17 14:40:44 +0530 | [diff] [blame] | 175 | * @details If the same version exists for multiple PSUs, just remove |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 176 | * related association. |
| 177 | * If the version has no association, the Activation and |
| 178 | * Version object will be removed |
| 179 | */ |
| 180 | void removePsuObject(const std::string& psuInventoryPath); |
| 181 | |
Shawn McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 182 | /** @brief Add PSU inventory path to the PSU status map |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 183 | * @details Also adds a PropertiesChanged listener for the inventory path |
| 184 | * so we are notified when the Present property changes. |
Shawn McCarney | 73a6f0d | 2024-10-30 16:11:29 -0500 | [diff] [blame] | 185 | * Does nothing if the inventory path already exists in the map. |
| 186 | * |
| 187 | * @param[in] psuPath - The PSU inventory path |
| 188 | */ |
| 189 | void addPsuToStatusMap(const std::string& psuPath); |
| 190 | |
Shawn McCarney | 783406e | 2024-11-17 21:49:37 -0600 | [diff] [blame] | 191 | /** @brief Handle a change in presence for a PSU. |
| 192 | * |
| 193 | * @param[in] psuPath - The PSU inventory path |
| 194 | */ |
| 195 | void handlePSUPresenceChanged(const std::string& psuPath); |
| 196 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 197 | /** |
| 198 | * @brief Create and populate the active PSU Version. |
| 199 | */ |
| 200 | void processPSUImage(); |
| 201 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 202 | /** @brief Create PSU Version from stored images */ |
| 203 | void processStoredImage(); |
| 204 | |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 205 | /** @brief Scan a directory and create PSU Version from stored images |
| 206 | * @details Throws an exception if an error occurs |
| 207 | * |
| 208 | * @param[in] dir Directory path to scan |
| 209 | */ |
| 210 | void scanDirectory(const fs::path& dir); |
| 211 | |
| 212 | /** @brief Find the PSU model subdirectory within the specified directory |
| 213 | * @details Throws an exception if an error occurs |
| 214 | * |
| 215 | * @param[in] dir Directory path to search |
| 216 | * |
| 217 | * @return Subdirectory path, or an empty path if none found |
| 218 | */ |
| 219 | fs::path findModelDirectory(const fs::path& dir); |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 220 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 221 | /** @brief Get the versionId of the latest PSU version */ |
| 222 | std::optional<std::string> getLatestVersionId(); |
| 223 | |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 224 | /** @brief Update PSUs to the latest version */ |
| 225 | void syncToLatestImage(); |
| 226 | |
| 227 | /** @brief Invoke the activation via DBus */ |
George Liu | 80c2daa | 2024-08-23 15:46:12 +0800 | [diff] [blame] | 228 | static void invokeActivation(const std::unique_ptr<Activation>& activation); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 229 | |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 230 | /** @brief Callback function for interfaces added signal. |
| 231 | * |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 232 | * This method is called when new interfaces are added. It updates the |
| 233 | * internal status map and processes the new PSU if it's present. |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 234 | * |
| 235 | * @param[in] msg - Data associated with subscribed signal |
| 236 | */ |
Shawn McCarney | 487e2e1 | 2024-11-25 17:19:46 -0600 | [diff] [blame^] | 237 | void onPSUInterfacesAdded(sdbusplus::message_t& msg); |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * @brief Handles the processing of PSU images. |
| 241 | * |
| 242 | * This function responsible for invoking the sequence of processing PSU |
| 243 | * images, processing stored images, and syncing to the latest firmware |
| 244 | * image. |
| 245 | */ |
| 246 | void processPSUImageAndSyncToLatest(); |
| 247 | |
Faisal Awada | fb86e79 | 2024-09-11 10:51:17 -0500 | [diff] [blame] | 248 | /** @brief Retrieve FW version from IMG_DIR_BUILTIN |
| 249 | * |
| 250 | * This function retrieves the firmware version from the PSU model directory |
| 251 | * that is in the IMG_DIR_BUILTIN. It loops through the activations map to |
| 252 | * find matching path starts with IMG_DIR_BUILTIN, then gets the |
| 253 | * corresponding version ID, and then looks it up in the versions map to |
| 254 | * retrieve the associated version string. |
| 255 | */ |
| 256 | std::string getFWVersionFromBuiltinDir(); |
| 257 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 258 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 259 | sdbusplus::bus_t& bus; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 260 | |
| 261 | /** @brief Persistent map of Activation D-Bus objects and their |
| 262 | * version id */ |
| 263 | std::map<std::string, std::unique_ptr<Activation>> activations; |
| 264 | |
| 265 | /** @brief Persistent map of Version D-Bus objects and their |
| 266 | * version id */ |
| 267 | std::map<std::string, std::unique_ptr<Version>> versions; |
| 268 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 269 | /** @brief The reference map of PSU Inventory objects and the |
| 270 | * Activation*/ |
| 271 | std::map<std::string, const std::unique_ptr<Activation>&> |
| 272 | psuPathActivationMap; |
| 273 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 274 | /** @brief sdbusplus signal match for PSU Software*/ |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 275 | sdbusplus::bus::match_t versionMatch; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 276 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 277 | /** @brief sdbusplus signal matches for PSU Inventory */ |
| 278 | std::vector<sdbusplus::bus::match_t> psuMatches; |
| 279 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 280 | /** @brief This entry's associations */ |
| 281 | AssociationList assocs; |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 282 | |
| 283 | /** @brief A collection of the version strings */ |
| 284 | std::set<std::string> versionStrings; |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 285 | |
| 286 | /** @brief A struct to hold the PSU present status and model */ |
| 287 | struct psuStatus |
| 288 | { |
| 289 | bool present; |
| 290 | std::string model; |
| 291 | }; |
| 292 | |
| 293 | /** @brief The map of PSU inventory path and the psuStatus |
| 294 | * |
| 295 | * It is used to handle psu inventory changed event, that only create psu |
| 296 | * software object when a PSU is present and the model is retrieved */ |
| 297 | std::map<std::string, psuStatus> psuStatusMap; |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 298 | |
| 299 | /** @brief Signal match for PSU interfaces added. |
| 300 | * |
| 301 | * This match listens for D-Bus signals indicating new interface has been |
| 302 | * added. When such a signal received, it triggers the |
| 303 | * `onInterfacesAdded` method to handle the new PSU. |
| 304 | */ |
| 305 | sdbusplus::bus::match_t psuInterfaceMatch; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | } // namespace updater |
| 309 | } // namespace software |
| 310 | } // namespace phosphor |