blob: f09891c724ef765ba6a16492136f477d35136e04 [file] [log] [blame]
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05001#pragma once
2
3#include <sdbusplus/server.hpp>
4#include "activation.hpp"
Michael Tritzdd961b62017-05-17 14:07:03 -05005#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Adriana Kobylak7cb480e2017-11-07 13:22:59 -06006#include <xyz/openbmc_project/Object/Enable/server.hpp>
Saqib Khance148702017-06-11 12:01:58 -05007#include "version.hpp"
Gunnar Mills9741cd12017-08-28 15:09:00 -05008#include "org/openbmc/Associations/server.hpp"
Michael Tritz234a07e2017-09-21 00:53:06 -05009#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050010
11namespace openpower
12{
13namespace software
14{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050015namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050016{
17
Michael Tritzdd961b62017-05-17 14:07:03 -050018using ItemUpdaterInherit = sdbusplus::server::object::object<
Gunnar Mills139cf1a2017-09-21 16:25:37 -050019 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
20 sdbusplus::org::openbmc::server::Associations,
21 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritzb541f1b2017-10-15 15:10:21 -050022using GardResetInherit = sdbusplus::server::object::object<
23 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Adriana Kobylak7cb480e2017-11-07 13:22:59 -060024using ObjectEnable = sdbusplus::server::object::object<
25 sdbusplus::xyz::openbmc_project::Object::server::Enable>;
Patrick Williams3accb322017-05-30 16:29:52 -050026namespace MatchRules = sdbusplus::bus::match::rules;
Michael Tritzdd961b62017-05-17 14:07:03 -050027
Gunnar Mills9741cd12017-08-28 15:09:00 -050028using AssociationList =
29 std::vector<std::tuple<std::string, std::string, std::string>>;
30
Michael Tritz4ecea0f2017-12-05 17:51:31 -060031constexpr auto GARD_PATH = "/org/open_power/control/gard";
Adriana Kobylak7cb480e2017-11-07 13:22:59 -060032constexpr static auto volatilePath = "/org/open_power/control/volatile";
Michael Tritzb541f1b2017-10-15 15:10:21 -050033
34/** @class GardReset
35 * @brief OpenBMC GARD factory reset implementation.
36 * @details An implementation of xyz.openbmc_project.Common.FactoryReset under
37 * /org/openpower/control/gard.
38 */
39class GardReset : public GardResetInherit
40{
41 public:
42 /** @brief Constructs GardReset.
43 *
44 * @param[in] bus - The Dbus bus object
45 * @param[in] path - The Dbus object path
46 */
47 GardReset(sdbusplus::bus::bus& bus,
48 const std::string& path) :
49 GardResetInherit(bus, path.c_str(), true),
50 bus(bus),
51 path(path)
52 {
53 std::vector<std::string> interfaces({interface});
54 bus.emit_interfaces_added(path.c_str(), interfaces);
55 }
56
57 ~GardReset()
58 {
59 std::vector<std::string> interfaces({interface});
60 bus.emit_interfaces_removed(path.c_str(), interfaces);
61 }
62
63 private:
64 // TODO Remove once openbmc/openbmc#1975 is resolved
65 static constexpr auto interface =
66 "xyz.openbmc_project.Common.FactoryReset";
67 sdbusplus::bus::bus& bus;
68 std::string path;
69
70 /**
71 * @brief GARD factory reset - clears the PNOR GARD partition.
72 */
73 void reset() override;
74};
75
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050076/** @class ItemUpdater
Gunnar Mills139cf1a2017-09-21 16:25:37 -050077 * @brief Manages the activation of the host version items.
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050078 */
Michael Tritzdd961b62017-05-17 14:07:03 -050079class ItemUpdater : public ItemUpdaterInherit
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050080{
81 public:
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050082 /** @brief Constructs ItemUpdater
83 *
Gunnar Mills139cf1a2017-09-21 16:25:37 -050084 * @param[in] bus - The D-Bus bus object
85 * @param[in] path - The D-Bus path
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050086 */
Michael Tritzdd961b62017-05-17 14:07:03 -050087 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
88 ItemUpdaterInherit(bus, path.c_str()),
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050089 bus(bus),
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050090 versionMatch(
91 bus,
Patrick Williams3accb322017-05-30 16:29:52 -050092 MatchRules::interfacesAdded() +
93 MatchRules::path("/xyz/openbmc_project/software"),
94 std::bind(
95 std::mem_fn(&ItemUpdater::createActivation),
96 this,
97 std::placeholders::_1))
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050098 {
Saqib Khan167601b2017-06-18 23:33:46 -050099 processPNORImage();
Michael Tritzb541f1b2017-10-15 15:10:21 -0500100 gardReset = std::make_unique<GardReset>(bus, GARD_PATH);
Adriana Kobylak7cb480e2017-11-07 13:22:59 -0600101 volatileEnable = std::make_unique<ObjectEnable>(bus, volatilePath);
Michael Tritzb541f1b2017-10-15 15:10:21 -0500102
103 // Emit deferred signal.
104 emit_object_added();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500105 }
106
Saqib Khan81bac882017-06-08 12:17:01 -0500107 /** @brief Sets the given priority free by incrementing
108 * any existing priority with the same value by 1
109 *
110 * @param[in] value - The priority that needs to be set free.
Saqib Khanb8e7f312017-08-12 10:24:10 -0500111 * @param[in] versionId - The Id of the version for which we
112 * are trying to free up the priority.
Saqib Khan81bac882017-06-08 12:17:01 -0500113 * @return None
114 */
Saqib Khanb8e7f312017-08-12 10:24:10 -0500115 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan81bac882017-06-08 12:17:01 -0500116
Saqib Khan2af5c492017-07-17 16:15:13 -0500117 /** @brief Determine is the given priority is the lowest
118 *
119 * @param[in] value - The priority that needs to be checked.
120 *
121 * @return boolean corresponding to whether the given
122 * priority is lowest.
123 */
124 bool isLowestPriority(uint8_t value);
125
Saqib Khan167601b2017-06-18 23:33:46 -0500126 /**
127 * @brief Create and populate the active PNOR Version.
128 */
129 void processPNORImage();
130
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500131 /** @brief Deletes version
132 *
133 * @param[in] entryId - Id of the version to delete
134 *
135 * @return None
136 */
137 void erase(std::string entryId);
138
Michael Tritz234a07e2017-09-21 00:53:06 -0500139 /**
140 * @brief Erases any non-active pnor versions.
141 */
142 void deleteAll();
143
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500144 /** @brief Deletes the active PNOR version with highest priority
145 if the total number of volumes exceeds the threshold.
Saqib Khan2cbfa032017-08-17 14:52:37 -0500146 */
147 void freeSpace();
148
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500149 /** @brief Determine the software version id
150 * from the symlink target (e.g. /media/ro-2a1022fe).
151 *
152 * @param[in] symlinkPath - The path of the symlink.
153 * @param[out] id - The version id as a string.
154 */
155 static std::string determineId(const std::string& symlinkPath);
156
Gunnar Mills9741cd12017-08-28 15:09:00 -0500157 /** @brief Creates an active association to the
158 * newly active software image
159 *
160 * @param[in] path - The path to create the association to.
161 */
Gunnar Mills61010b22017-09-20 15:25:26 -0500162 void createActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500163
Gunnar Mills833e4f32017-09-14 12:30:27 -0500164 /** @brief Updates the functional association to the
165 * new "running" PNOR image
166 *
167 * @param[in] path - The path to update the association to.
168 */
169 void updateFunctionalAssociation(const std::string& path);
170
Gunnar Mills9741cd12017-08-28 15:09:00 -0500171 /** @brief Removes an active association to the software image
172 *
173 * @param[in] path - The path to remove the association from.
174 */
Gunnar Mills61010b22017-09-20 15:25:26 -0500175 void removeActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500176
Michael Tritzb541f1b2017-10-15 15:10:21 -0500177 /** @brief Persistent GardReset dbus object */
178 std::unique_ptr<GardReset> gardReset;
179
Michael Tritz5b756512017-10-06 16:52:01 -0500180 /** @brief Check whether the provided image id is the functional one
181 *
182 * @param[in] - versionId - The id of the image to check.
183 *
184 * @return - Returns true if this version is currently functional.
185 */
186 static bool isVersionFunctional(const std::string& versionId);
187
Adriana Kobylak7cb480e2017-11-07 13:22:59 -0600188 /** @brief Persistent ObjectEnable D-Bus object */
189 std::unique_ptr<ObjectEnable> volatileEnable;
190
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500191 private:
192 /** @brief Callback function for Software.Version match.
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500193 * @details Creates an Activation D-Bus object.
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500194 *
195 * @param[in] msg - Data associated with subscribed signal
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500196 */
Patrick Williams3accb322017-05-30 16:29:52 -0500197 void createActivation(sdbusplus::message::message& msg);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500198
Saqib Khan7254f0e2017-04-10 21:45:37 -0500199 /**
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500200 * @brief Validates the presence of SquashFS image in the image dir.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500201 *
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500202 * @param[in] filePath - The path to the SquashFS image.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500203 * @param[out] result - 0 --> if validation was successful
204 * - -1--> Otherwise
205 */
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500206 static int validateSquashFSImage(const std::string& filePath);
Saqib Khan7254f0e2017-04-10 21:45:37 -0500207
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500208 /** @brief Persistent sdbusplus D-Bus bus connection. */
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500209 sdbusplus::bus::bus& bus;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500210
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500211 /** @brief Persistent map of Activation D-Bus objects and their
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500212 * version id */
Adriana Kobylak268616b2017-04-05 15:23:30 -0500213 std::map<std::string, std::unique_ptr<Activation>> activations;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500214
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500215 /** @brief Persistent map of Version D-Bus objects and their
Saqib Khance148702017-06-11 12:01:58 -0500216 * version id */
217 std::map<std::string, std::unique_ptr<Version>> versions;
218
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500219 /** @brief sdbusplus signal match for Software.Version */
Patrick Williams3accb322017-05-30 16:29:52 -0500220 sdbusplus::bus::match_t versionMatch;
Michael Tritzdd961b62017-05-17 14:07:03 -0500221
Gunnar Mills9741cd12017-08-28 15:09:00 -0500222 /** @brief This entry's associations */
223 AssociationList assocs = {};
224
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500225 /** @brief Clears read only PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500226 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500227 *
228 * @param[in] versionId - The id of the ro partition to remove.
229 */
230 void removeReadOnlyPartition(std::string versionId);
231
232 /** @brief Clears read write PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500233 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500234 *
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500235 * @param[in] versionId - The id of the rw partition to remove.
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500236 */
237 void removeReadWritePartition(std::string versionId);
238
239 /** @brief Clears preserved PNOR partition */
240 void removePreservedPartition();
241
Michael Tritzdd961b62017-05-17 14:07:03 -0500242 /** @brief Host factory reset - clears PNOR partitions for each
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500243 * Activation D-Bus object */
Michael Tritzdd961b62017-05-17 14:07:03 -0500244 void reset() override;
Eddie James13fc66a2017-08-31 15:36:44 -0500245
Eddie James13fc66a2017-08-31 15:36:44 -0500246 /** @brief Check whether the host is running
247 *
248 * @return - Returns true if the Chassis is powered on.
249 */
250 bool isChassisOn();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500251};
252
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500253} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500254} // namespace software
255} // namespace openpower