Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 3 | #include "activation.hpp" |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 4 | #include "item_updater_helper.hpp" |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 5 | #include "version.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp" |
| 7 | |
| 8 | #include <sdbusplus/server.hpp> |
John Wang | 85c356f | 2019-09-11 16:20:13 +0800 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/FactoryReset/server.hpp> |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Control/FieldMode/server.hpp> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 12 | |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 13 | #include <string> |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 14 | #include <vector> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 15 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace software |
| 19 | { |
| 20 | namespace updater |
| 21 | { |
| 22 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 23 | using ItemUpdaterInherit = sdbusplus::server::object::object< |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 24 | sdbusplus::xyz::openbmc_project::Common::server::FactoryReset, |
| 25 | sdbusplus::xyz::openbmc_project::Control::server::FieldMode, |
John Wang | 85c356f | 2019-09-11 16:20:13 +0800 | [diff] [blame] | 26 | sdbusplus::xyz::openbmc_project::Association::server::Definitions, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 27 | sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>; |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 28 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 29 | namespace MatchRules = sdbusplus::bus::match::rules; |
Gunnar Mills | 4844291 | 2017-10-06 13:34:07 -0500 | [diff] [blame] | 30 | using VersionClass = phosphor::software::manager::Version; |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 31 | using AssociationList = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 32 | std::vector<std::tuple<std::string, std::string, std::string>>; |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 33 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 34 | /** @class ItemUpdater |
| 35 | * @brief Manages the activation of the BMC version items. |
| 36 | */ |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 37 | class ItemUpdater : public ItemUpdaterInherit |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 38 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 39 | public: |
| 40 | /* |
| 41 | * @brief Types of Activation status for image validation. |
| 42 | */ |
| 43 | enum class ActivationStatus |
| 44 | { |
| 45 | ready, |
| 46 | invalid, |
| 47 | active |
| 48 | }; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 49 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 50 | /** @brief Constructs ItemUpdater |
| 51 | * |
| 52 | * @param[in] bus - The D-Bus bus object |
| 53 | */ |
| 54 | ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) : |
Patrick Williams | 35aa9a8 | 2022-04-05 15:55:30 -0500 | [diff] [blame] | 55 | ItemUpdaterInherit(bus, path.c_str(), |
| 56 | ItemUpdaterInherit::action::defer_emit), |
| 57 | bus(bus), helper(bus), |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 58 | versionMatch(bus, |
| 59 | MatchRules::interfacesAdded() + |
| 60 | MatchRules::path("/xyz/openbmc_project/software"), |
| 61 | std::bind(std::mem_fn(&ItemUpdater::createActivation), |
| 62 | this, std::placeholders::_1)) |
| 63 | { |
Lei YU | 531fbc2 | 2021-12-10 20:03:18 +0800 | [diff] [blame] | 64 | getRunningSlot(); |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 65 | setBMCInventoryPath(); |
| 66 | processBMCImage(); |
| 67 | restoreFieldModeStatus(); |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 68 | #ifdef HOST_BIOS_UPGRADE |
| 69 | createBIOSObject(); |
| 70 | #endif |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 71 | emit_object_added(); |
| 72 | }; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 73 | |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 74 | /** @brief Save priority value to persistent storage (flash and optionally |
| 75 | * a U-Boot environment variable) |
| 76 | * |
| 77 | * @param[in] versionId - The Id of the version |
| 78 | * @param[in] value - The priority value |
| 79 | * @return None |
| 80 | */ |
| 81 | void savePriority(const std::string& versionId, uint8_t value); |
| 82 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 83 | /** @brief Sets the given priority free by incrementing |
| 84 | * any existing priority with the same value by 1 |
| 85 | * |
| 86 | * @param[in] value - The priority that needs to be set free. |
| 87 | * @param[in] versionId - The Id of the version for which we |
| 88 | * are trying to free up the priority. |
| 89 | * @return None |
| 90 | */ |
| 91 | void freePriority(uint8_t value, const std::string& versionId); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 92 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 93 | /** |
| 94 | * @brief Create and populate the active BMC Version. |
| 95 | */ |
| 96 | void processBMCImage(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 97 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 98 | /** |
| 99 | * @brief Erase specified entry D-Bus object |
| 100 | * if Action property is not set to Active |
| 101 | * |
| 102 | * @param[in] entryId - unique identifier of the entry |
| 103 | */ |
| 104 | void erase(std::string entryId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 105 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 106 | /** |
| 107 | * @brief Deletes all versions except for the current one |
| 108 | */ |
| 109 | void deleteAll(); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 110 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 111 | /** @brief Creates an active association to the |
| 112 | * newly active software image |
| 113 | * |
| 114 | * @param[in] path - The path to create the association to. |
| 115 | */ |
| 116 | void createActiveAssociation(const std::string& path); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 117 | |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 118 | /** @brief Removes the associations from the provided software image path |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 119 | * |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 120 | * @param[in] path - The path to remove the associations from. |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 121 | */ |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 122 | void removeAssociations(const std::string& path); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 123 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 124 | /** @brief Determine if the given priority is the lowest |
| 125 | * |
| 126 | * @param[in] value - The priority that needs to be checked. |
| 127 | * |
| 128 | * @return boolean corresponding to whether the given |
| 129 | * priority is lowest. |
| 130 | */ |
| 131 | bool isLowestPriority(uint8_t value); |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 132 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 133 | /** |
| 134 | * @brief Updates the U-Boot variables to point to the requested |
| 135 | * versionId, so that the systems boots from this version on |
| 136 | * the next reboot. |
| 137 | * |
| 138 | * @param[in] versionId - The version to point the system to boot from. |
| 139 | */ |
| 140 | void updateUbootEnvVars(const std::string& versionId); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 141 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 142 | /** |
| 143 | * @brief Updates the uboot variables to point to BMC version with lowest |
| 144 | * priority, so that the system boots from this version on the |
| 145 | * next boot. |
| 146 | */ |
| 147 | void resetUbootEnvVars(); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 148 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 149 | /** @brief Brings the total number of active BMC versions to |
| 150 | * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be |
| 151 | * run before activating a new BMC version. If this function |
| 152 | * needs to delete any BMC version(s) it will delete the |
| 153 | * version(s) with the highest priority, skipping the |
| 154 | * functional BMC version. |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 155 | * |
| 156 | * @param[in] caller - The Activation object that called this function. |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 157 | */ |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 158 | void freeSpace(const Activation& caller); |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 159 | |
AppaRao Puli | 1bb6dcb | 2020-03-27 20:51:57 +0530 | [diff] [blame] | 160 | /** @brief Creates a updateable association to the |
| 161 | * "running" BMC software image |
| 162 | * |
| 163 | * @param[in] path - The path to create the association. |
| 164 | */ |
| 165 | void createUpdateableAssociation(const std::string& path); |
| 166 | |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 167 | /** @brief Persistent map of Version D-Bus objects and their |
| 168 | * version id */ |
| 169 | std::map<std::string, std::unique_ptr<VersionClass>> versions; |
| 170 | |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 171 | /** @brief Vector of needed BMC images in the tarball*/ |
| 172 | std::vector<std::string> imageUpdateList; |
| 173 | |
Lei YU | 531fbc2 | 2021-12-10 20:03:18 +0800 | [diff] [blame] | 174 | /** @breif The slot of running BMC image */ |
| 175 | uint32_t runningImageSlot = 0; |
| 176 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 177 | private: |
| 178 | /** @brief Callback function for Software.Version match. |
| 179 | * @details Creates an Activation D-Bus object. |
| 180 | * |
| 181 | * @param[in] msg - Data associated with subscribed signal |
| 182 | */ |
| 183 | void createActivation(sdbusplus::message::message& msg); |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 184 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 185 | /** |
| 186 | * @brief Validates the presence of SquashFS image in the image dir. |
| 187 | * |
| 188 | * @param[in] filePath - The path to the image dir. |
| 189 | * @param[out] result - ActivationStatus Enum. |
| 190 | * ready if validation was successful. |
| 191 | * invalid if validation fail. |
| 192 | * active if image is the current version. |
| 193 | * |
| 194 | */ |
| 195 | ActivationStatus validateSquashFSImage(const std::string& filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 196 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 197 | /** @brief BMC factory reset - marks the read-write partition for |
| 198 | * recreation upon reboot. */ |
| 199 | void reset() override; |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 200 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 201 | /** |
| 202 | * @brief Enables field mode, if value=true. |
| 203 | * |
| 204 | * @param[in] value - If true, enables field mode. |
| 205 | * @param[out] result - Returns the current state of field mode. |
| 206 | * |
| 207 | */ |
| 208 | bool fieldModeEnabled(bool value) override; |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 209 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 210 | /** @brief Sets the BMC inventory item path under |
| 211 | * /xyz/openbmc_project/inventory/system/chassis/. */ |
| 212 | void setBMCInventoryPath(); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 213 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 214 | /** @brief The path to the BMC inventory item. */ |
| 215 | std::string bmcInventoryPath; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 216 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 217 | /** @brief Restores field mode status on reboot. */ |
| 218 | void restoreFieldModeStatus(); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 219 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 220 | /** @brief Creates a functional association to the |
| 221 | * "running" BMC software image |
| 222 | * |
| 223 | * @param[in] path - The path to create the association to. |
| 224 | */ |
| 225 | void createFunctionalAssociation(const std::string& path); |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 226 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 227 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
| 228 | sdbusplus::bus::bus& bus; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 229 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 230 | /** @brief The helper of image updater. */ |
| 231 | Helper helper; |
| 232 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 233 | /** @brief Persistent map of Activation D-Bus objects and their |
| 234 | * version id */ |
| 235 | std::map<std::string, std::unique_ptr<Activation>> activations; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 236 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 237 | /** @brief sdbusplus signal match for Software.Version */ |
| 238 | sdbusplus::bus::match_t versionMatch; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 239 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 240 | /** @brief This entry's associations */ |
| 241 | AssociationList assocs = {}; |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 242 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 243 | /** @brief Clears read only partition for |
| 244 | * given Activation D-Bus object. |
| 245 | * |
| 246 | * @param[in] versionId - The version id. |
| 247 | */ |
| 248 | void removeReadOnlyPartition(std::string versionId); |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 249 | |
| 250 | /** @brief Copies U-Boot from the currently booted BMC chip to the |
| 251 | * alternate chip. |
| 252 | */ |
| 253 | void mirrorUbootToAlt(); |
Bright Cheng | 8e9ccfe | 2019-11-18 16:18:44 +0800 | [diff] [blame] | 254 | |
| 255 | /** @brief Check the required image files |
| 256 | * |
| 257 | * @param[in] filePath - BMC tarball file path |
| 258 | * @param[in] imageList - Image filenames included in the BMC tarball |
| 259 | * @param[out] result - Boolean |
| 260 | * true if all image files are found in BMC tarball |
| 261 | * false if one of image files is missing |
| 262 | */ |
| 263 | bool checkImage(const std::string& filePath, |
| 264 | const std::vector<std::string>& imageList); |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 265 | |
| 266 | #ifdef HOST_BIOS_UPGRADE |
| 267 | /** @brief Create the BIOS object without knowing the version. |
| 268 | * |
| 269 | * The object is created only to provide the DBus access so that an |
| 270 | * external service could set the correct BIOS version. |
| 271 | * On BIOS code update, the version is updated accordingly. |
| 272 | */ |
| 273 | void createBIOSObject(); |
| 274 | |
| 275 | /** @brief Persistent Activation D-Bus object for BIOS */ |
| 276 | std::unique_ptr<Activation> biosActivation; |
| 277 | |
Lei YU | 16aa28a | 2021-05-07 10:17:30 +0800 | [diff] [blame] | 278 | public: |
Lei YU | 6e9fb1d | 2021-02-19 18:01:40 +0800 | [diff] [blame] | 279 | /** @brief Persistent Version D-Bus object for BIOS */ |
| 280 | std::unique_ptr<VersionClass> biosVersion; |
| 281 | #endif |
Lei YU | 531fbc2 | 2021-12-10 20:03:18 +0800 | [diff] [blame] | 282 | |
| 283 | /** @brief Get the slot number of running image */ |
| 284 | void getRunningSlot(); |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 285 | }; |
| 286 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 287 | } // namespace updater |
| 288 | } // namespace software |
| 289 | } // namespace phosphor |