| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
|  | 3 | #include <sdbusplus/server.hpp> | 
|  | 4 | #include "activation.hpp" | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Common/FactoryReset/server.hpp> | 
| Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 6 | #include "version.hpp" | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 7 | #include "org/openbmc/Associations/server.hpp" | 
| Michael Tritz | 234a07e | 2017-09-21 00:53:06 -0500 | [diff] [blame] | 8 | #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp" | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 9 |  | 
|  | 10 | namespace openpower | 
|  | 11 | { | 
|  | 12 | namespace software | 
|  | 13 | { | 
| Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 14 | namespace updater | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 15 | { | 
|  | 16 |  | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 17 | using ItemUpdaterInherit = sdbusplus::server::object::object< | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 18 | sdbusplus::xyz::openbmc_project::Common::server::FactoryReset, | 
|  | 19 | sdbusplus::org::openbmc::server::Associations, | 
|  | 20 | sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>; | 
| Michael Tritz | b541f1b | 2017-10-15 15:10:21 -0500 | [diff] [blame] | 21 | using GardResetInherit = sdbusplus::server::object::object< | 
|  | 22 | sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>; | 
| Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 23 | namespace MatchRules = sdbusplus::bus::match::rules; | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 24 |  | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 25 | using AssociationList = | 
|  | 26 | std::vector<std::tuple<std::string, std::string, std::string>>; | 
|  | 27 |  | 
| Michael Tritz | b541f1b | 2017-10-15 15:10:21 -0500 | [diff] [blame] | 28 | constexpr auto GARD_PATH = "/org/openpower/control/gard"; | 
|  | 29 |  | 
|  | 30 | /** @class GardReset | 
|  | 31 | *  @brief OpenBMC GARD factory reset implementation. | 
|  | 32 | *  @details An implementation of xyz.openbmc_project.Common.FactoryReset under | 
|  | 33 | *  /org/openpower/control/gard. | 
|  | 34 | */ | 
|  | 35 | class GardReset : public GardResetInherit | 
|  | 36 | { | 
|  | 37 | public: | 
|  | 38 | /** @brief Constructs GardReset. | 
|  | 39 | * | 
|  | 40 | * @param[in] bus    - The Dbus bus object | 
|  | 41 | * @param[in] path   - The Dbus object path | 
|  | 42 | */ | 
|  | 43 | GardReset(sdbusplus::bus::bus& bus, | 
|  | 44 | const std::string& path) : | 
|  | 45 | GardResetInherit(bus, path.c_str(), true), | 
|  | 46 | bus(bus), | 
|  | 47 | path(path) | 
|  | 48 | { | 
|  | 49 | std::vector<std::string> interfaces({interface}); | 
|  | 50 | bus.emit_interfaces_added(path.c_str(), interfaces); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | ~GardReset() | 
|  | 54 | { | 
|  | 55 | std::vector<std::string> interfaces({interface}); | 
|  | 56 | bus.emit_interfaces_removed(path.c_str(), interfaces); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | private: | 
|  | 60 | // TODO Remove once openbmc/openbmc#1975 is resolved | 
|  | 61 | static constexpr auto interface = | 
|  | 62 | "xyz.openbmc_project.Common.FactoryReset"; | 
|  | 63 | sdbusplus::bus::bus& bus; | 
|  | 64 | std::string path; | 
|  | 65 |  | 
|  | 66 | /** | 
|  | 67 | * @brief GARD factory reset - clears the PNOR GARD partition. | 
|  | 68 | */ | 
|  | 69 | void reset() override; | 
|  | 70 | }; | 
|  | 71 |  | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 72 | /** @class ItemUpdater | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 73 | *  @brief Manages the activation of the host version items. | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 74 | */ | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 75 | class ItemUpdater : public ItemUpdaterInherit | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 76 | { | 
|  | 77 | public: | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 78 | /** @brief Constructs ItemUpdater | 
|  | 79 | * | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 80 | * @param[in] bus    - The D-Bus bus object | 
|  | 81 | * @param[in] path   - The D-Bus path | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 82 | */ | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 83 | ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) : | 
|  | 84 | ItemUpdaterInherit(bus, path.c_str()), | 
| Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 85 | bus(bus), | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 86 | versionMatch( | 
|  | 87 | bus, | 
| Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 88 | MatchRules::interfacesAdded() + | 
|  | 89 | MatchRules::path("/xyz/openbmc_project/software"), | 
|  | 90 | std::bind( | 
|  | 91 | std::mem_fn(&ItemUpdater::createActivation), | 
|  | 92 | this, | 
|  | 93 | std::placeholders::_1)) | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 94 | { | 
| Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 95 | processPNORImage(); | 
| Michael Tritz | b541f1b | 2017-10-15 15:10:21 -0500 | [diff] [blame] | 96 | gardReset = std::make_unique<GardReset>(bus, GARD_PATH); | 
|  | 97 |  | 
|  | 98 | // Emit deferred signal. | 
|  | 99 | emit_object_added(); | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 102 | /** @brief Sets the given priority free by incrementing | 
|  | 103 | *  any existing priority with the same value by 1 | 
|  | 104 | * | 
|  | 105 | *  @param[in] value - The priority that needs to be set free. | 
| Saqib Khan | b8e7f31 | 2017-08-12 10:24:10 -0500 | [diff] [blame] | 106 | *  @param[in] versionId - The Id of the version for which we | 
|  | 107 | *                         are trying to free up the priority. | 
| Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 108 | *  @return None | 
|  | 109 | */ | 
| Saqib Khan | b8e7f31 | 2017-08-12 10:24:10 -0500 | [diff] [blame] | 110 | void freePriority(uint8_t value, const std::string& versionId); | 
| Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 111 |  | 
| Saqib Khan | 2af5c49 | 2017-07-17 16:15:13 -0500 | [diff] [blame] | 112 | /** @brief Determine is the given priority is the lowest | 
|  | 113 | * | 
|  | 114 | *  @param[in] value - The priority that needs to be checked. | 
|  | 115 | * | 
|  | 116 | *  @return boolean corresponding to whether the given | 
|  | 117 | *           priority is lowest. | 
|  | 118 | */ | 
|  | 119 | bool isLowestPriority(uint8_t value); | 
|  | 120 |  | 
| Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 121 | /** | 
|  | 122 | * @brief Create and populate the active PNOR Version. | 
|  | 123 | */ | 
|  | 124 | void processPNORImage(); | 
|  | 125 |  | 
| Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 126 | /** @brief Deletes version | 
|  | 127 | * | 
|  | 128 | *  @param[in] entryId - Id of the version to delete | 
|  | 129 | * | 
|  | 130 | *  @return None | 
|  | 131 | */ | 
|  | 132 | void erase(std::string entryId); | 
|  | 133 |  | 
| Michael Tritz | 234a07e | 2017-09-21 00:53:06 -0500 | [diff] [blame] | 134 | /** | 
|  | 135 | * @brief Erases any non-active pnor versions. | 
|  | 136 | */ | 
|  | 137 | void deleteAll(); | 
|  | 138 |  | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 139 | /** @brief Deletes the active PNOR version with highest priority | 
|  | 140 | if the total number of volumes exceeds the threshold. | 
| Saqib Khan | 2cbfa03 | 2017-08-17 14:52:37 -0500 | [diff] [blame] | 141 | */ | 
|  | 142 | void freeSpace(); | 
|  | 143 |  | 
| Gunnar Mills | 2badd7a | 2017-09-20 12:51:28 -0500 | [diff] [blame] | 144 | /** @brief Determine the software version id | 
|  | 145 | *         from the symlink target (e.g. /media/ro-2a1022fe). | 
|  | 146 | * | 
|  | 147 | * @param[in] symlinkPath - The path of the symlink. | 
|  | 148 | * @param[out] id - The version id as a string. | 
|  | 149 | */ | 
|  | 150 | static std::string determineId(const std::string& symlinkPath); | 
|  | 151 |  | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 152 | /** @brief Creates an active association to the | 
|  | 153 | *  newly active software image | 
|  | 154 | * | 
|  | 155 | * @param[in]  path - The path to create the association to. | 
|  | 156 | */ | 
| Gunnar Mills | 61010b2 | 2017-09-20 15:25:26 -0500 | [diff] [blame] | 157 | void createActiveAssociation(const std::string& path); | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 158 |  | 
| Gunnar Mills | 833e4f3 | 2017-09-14 12:30:27 -0500 | [diff] [blame] | 159 | /** @brief Updates the functional association to the | 
|  | 160 | *  new "running" PNOR image | 
|  | 161 | * | 
|  | 162 | * @param[in]  path - The path to update the association to. | 
|  | 163 | */ | 
|  | 164 | void updateFunctionalAssociation(const std::string& path); | 
|  | 165 |  | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 166 | /** @brief Removes an active association to the software image | 
|  | 167 | * | 
|  | 168 | * @param[in]  path - The path to remove the association from. | 
|  | 169 | */ | 
| Gunnar Mills | 61010b2 | 2017-09-20 15:25:26 -0500 | [diff] [blame] | 170 | void removeActiveAssociation(const std::string& path); | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 171 |  | 
| Michael Tritz | b541f1b | 2017-10-15 15:10:21 -0500 | [diff] [blame] | 172 | /** @brief Persistent GardReset dbus object */ | 
|  | 173 | std::unique_ptr<GardReset> gardReset; | 
|  | 174 |  | 
| Michael Tritz | 5b75651 | 2017-10-06 16:52:01 -0500 | [diff] [blame^] | 175 | /** @brief Check whether the provided image id is the functional one | 
|  | 176 | * | 
|  | 177 | * @param[in] - versionId - The id of the image to check. | 
|  | 178 | * | 
|  | 179 | * @return - Returns true if this version is currently functional. | 
|  | 180 | */ | 
|  | 181 | static bool isVersionFunctional(const std::string& versionId); | 
|  | 182 |  | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 183 | private: | 
|  | 184 | /** @brief Callback function for Software.Version match. | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 185 | *  @details Creates an Activation D-Bus object. | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 186 | * | 
|  | 187 | * @param[in]  msg       - Data associated with subscribed signal | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 188 | */ | 
| Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 189 | void createActivation(sdbusplus::message::message& msg); | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 190 |  | 
| Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 191 | /** | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 192 | * @brief Validates the presence of SquashFS image in the image dir. | 
| Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 193 | * | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 194 | * @param[in]  filePath - The path to the SquashFS image. | 
| Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 195 | * @param[out] result    - 0 --> if validation was successful | 
|  | 196 | *                       - -1--> Otherwise | 
|  | 197 | */ | 
| Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 198 | static int validateSquashFSImage(const std::string& filePath); | 
| Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 199 |  | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 200 | /** @brief Persistent sdbusplus D-Bus bus connection. */ | 
| Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 201 | sdbusplus::bus::bus& bus; | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 202 |  | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 203 | /** @brief Persistent map of Activation D-Bus objects and their | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 204 | * version id */ | 
| Adriana Kobylak | 268616b | 2017-04-05 15:23:30 -0500 | [diff] [blame] | 205 | std::map<std::string, std::unique_ptr<Activation>> activations; | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 206 |  | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 207 | /** @brief Persistent map of Version D-Bus objects and their | 
| Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 208 | * version id */ | 
|  | 209 | std::map<std::string, std::unique_ptr<Version>> versions; | 
|  | 210 |  | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 211 | /** @brief sdbusplus signal match for Software.Version */ | 
| Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 212 | sdbusplus::bus::match_t versionMatch; | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 213 |  | 
| Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 214 | /** @brief This entry's associations */ | 
|  | 215 | AssociationList assocs = {}; | 
|  | 216 |  | 
| Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 217 | /** @brief Clears read only PNOR partition for | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 218 | *  given Activation D-Bus object | 
| Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 219 | * | 
|  | 220 | * @param[in]  versionId - The id of the ro partition to remove. | 
|  | 221 | */ | 
|  | 222 | void removeReadOnlyPartition(std::string versionId); | 
|  | 223 |  | 
|  | 224 | /** @brief Clears read write PNOR partition for | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 225 | *  given Activation D-Bus object | 
| Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 226 | * | 
| Saqib Khan | 1e0aa5c | 2017-08-31 11:04:17 -0500 | [diff] [blame] | 227 | *  @param[in]  versionId - The id of the rw partition to remove. | 
| Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 228 | */ | 
|  | 229 | void removeReadWritePartition(std::string versionId); | 
|  | 230 |  | 
|  | 231 | /** @brief Clears preserved PNOR partition */ | 
|  | 232 | void removePreservedPartition(); | 
|  | 233 |  | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 234 | /** @brief Host factory reset - clears PNOR partitions for each | 
| Gunnar Mills | 139cf1a | 2017-09-21 16:25:37 -0500 | [diff] [blame] | 235 | * Activation D-Bus object */ | 
| Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 236 | void reset() override; | 
| Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 237 |  | 
| Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 238 | /** @brief Check whether the host is running | 
|  | 239 | * | 
|  | 240 | * @return - Returns true if the Chassis is powered on. | 
|  | 241 | */ | 
|  | 242 | bool isChassisOn(); | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 243 | }; | 
|  | 244 |  | 
| Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 245 | } // namespace updater | 
| Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 246 | } // namespace software | 
|  | 247 | } // namespace openpower |