blob: 90441c4fe7e5bdaaa82192f0f230c052168fd4dd [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<
Adriana Kobylak70dcb632018-02-27 15:46:52 -060019 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<
Adriana Kobylak70dcb632018-02-27 15:46:52 -060023 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Adriana Kobylak7cb480e2017-11-07 13:22:59 -060024using ObjectEnable = sdbusplus::server::object::object<
Adriana Kobylak70dcb632018-02-27 15:46:52 -060025 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 =
Adriana Kobylak70dcb632018-02-27 15:46:52 -060029 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Mills9741cd12017-08-28 15:09:00 -050030
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{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060041 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, const std::string& path) :
48 GardResetInherit(bus, path.c_str(), true), bus(bus), path(path)
49 {
50 std::vector<std::string> interfaces({interface});
51 bus.emit_interfaces_added(path.c_str(), interfaces);
52 }
Michael Tritzb541f1b2017-10-15 15:10:21 -050053
Adriana Kobylak70dcb632018-02-27 15:46:52 -060054 ~GardReset()
55 {
56 std::vector<std::string> interfaces({interface});
57 bus.emit_interfaces_removed(path.c_str(), interfaces);
58 }
Michael Tritzb541f1b2017-10-15 15:10:21 -050059
Adriana Kobylak70dcb632018-02-27 15:46:52 -060060 private:
61 // TODO Remove once openbmc/openbmc#1975 is resolved
62 static constexpr auto interface = "xyz.openbmc_project.Common.FactoryReset";
63 sdbusplus::bus::bus& bus;
64 std::string path;
Michael Tritzb541f1b2017-10-15 15:10:21 -050065
Adriana Kobylak70dcb632018-02-27 15:46:52 -060066 /**
67 * @brief GARD factory reset - clears the PNOR GARD partition.
68 */
69 void reset() override;
Michael Tritzb541f1b2017-10-15 15:10:21 -050070};
71
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050072/** @class ItemUpdater
Gunnar Mills139cf1a2017-09-21 16:25:37 -050073 * @brief Manages the activation of the host version items.
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050074 */
Michael Tritzdd961b62017-05-17 14:07:03 -050075class ItemUpdater : public ItemUpdaterInherit
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050076{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060077 public:
78 /** @brief Constructs ItemUpdater
79 *
80 * @param[in] bus - The D-Bus bus object
81 * @param[in] path - The D-Bus path
82 */
83 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
84 ItemUpdaterInherit(bus, path.c_str()), bus(bus),
85 versionMatch(bus,
86 MatchRules::interfacesAdded() +
87 MatchRules::path("/xyz/openbmc_project/software"),
88 std::bind(std::mem_fn(&ItemUpdater::createActivation),
89 this, std::placeholders::_1))
90 {
91 processPNORImage();
92 gardReset = std::make_unique<GardReset>(bus, GARD_PATH);
93 volatileEnable = std::make_unique<ObjectEnable>(bus, volatilePath);
Michael Tritzb541f1b2017-10-15 15:10:21 -050094
Adriana Kobylak70dcb632018-02-27 15:46:52 -060095 // Emit deferred signal.
96 emit_object_added();
97 }
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050098
Adriana Kobylak70dcb632018-02-27 15:46:52 -060099 /** @brief Sets the given priority free by incrementing
100 * any existing priority with the same value by 1
101 *
102 * @param[in] value - The priority that needs to be set free.
103 * @param[in] versionId - The Id of the version for which we
104 * are trying to free up the priority.
105 * @return None
106 */
107 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan81bac882017-06-08 12:17:01 -0500108
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600109 /** @brief Determine is the given priority is the lowest
110 *
111 * @param[in] value - The priority that needs to be checked.
112 *
113 * @return boolean corresponding to whether the given
114 * priority is lowest.
115 */
116 bool isLowestPriority(uint8_t value);
Saqib Khan2af5c492017-07-17 16:15:13 -0500117
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600118 /**
119 * @brief Create and populate the active PNOR Version.
120 */
121 void processPNORImage();
Saqib Khan167601b2017-06-18 23:33:46 -0500122
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600123 /** @brief Deletes version
124 *
125 * @param[in] entryId - Id of the version to delete
126 *
127 * @return None
128 */
129 void erase(std::string entryId);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500130
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600131 /**
132 * @brief Erases any non-active pnor versions.
133 */
134 void deleteAll();
Michael Tritz234a07e2017-09-21 00:53:06 -0500135
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600136 /** @brief Brings the total number of active PNOR versions to
137 * ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
138 * run before activating a new PNOR version. If this function
139 * needs to delete any PNOR version(s) it will delete the
140 * version(s) with the highest priority, skipping the
141 * functional PNOR version.
142 */
143 void freeSpace();
Saqib Khan2cbfa032017-08-17 14:52:37 -0500144
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600145 /** @brief Determine the software version id
146 * from the symlink target (e.g. /media/ro-2a1022fe).
147 *
148 * @param[in] symlinkPath - The path of the symlink.
149 * @param[out] id - The version id as a string.
150 */
151 static std::string determineId(const std::string& symlinkPath);
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500152
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600153 /** @brief Creates an active association to the
154 * newly active software image
155 *
156 * @param[in] path - The path to create the association to.
157 */
158 void createActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500159
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600160 /** @brief Updates the functional association to the
161 * new "running" PNOR image
162 *
163 * @param[in] path - The path to update the association to.
164 */
165 void updateFunctionalAssociation(const std::string& path);
Gunnar Mills833e4f32017-09-14 12:30:27 -0500166
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600167 /** @brief Removes an active association to the software image
168 *
169 * @param[in] path - The path to remove the association from.
170 */
171 void removeActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500172
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600173 /** @brief Persistent GardReset dbus object */
174 std::unique_ptr<GardReset> gardReset;
Michael Tritzb541f1b2017-10-15 15:10:21 -0500175
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600176 /** @brief Check whether the provided image id is the functional one
177 *
178 * @param[in] - versionId - The id of the image to check.
179 *
180 * @return - Returns true if this version is currently functional.
181 */
182 static bool isVersionFunctional(const std::string& versionId);
Michael Tritz5b756512017-10-06 16:52:01 -0500183
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600184 /** @brief Persistent ObjectEnable D-Bus object */
185 std::unique_ptr<ObjectEnable> volatileEnable;
Adriana Kobylak7cb480e2017-11-07 13:22:59 -0600186
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600187 private:
188 /** @brief Callback function for Software.Version match.
189 * @details Creates an Activation D-Bus object.
190 *
191 * @param[in] msg - Data associated with subscribed signal
192 */
193 void createActivation(sdbusplus::message::message& msg);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500194
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600195 /**
196 * @brief Validates the presence of SquashFS image in the image dir.
197 *
198 * @param[in] filePath - The path to the SquashFS image.
199 * @param[out] result - 0 --> if validation was successful
200 * - -1--> Otherwise
201 */
202 static int validateSquashFSImage(const std::string& filePath);
Saqib Khan7254f0e2017-04-10 21:45:37 -0500203
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600204 /** @brief Persistent sdbusplus D-Bus bus connection. */
205 sdbusplus::bus::bus& bus;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500206
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600207 /** @brief Persistent map of Activation D-Bus objects and their
208 * version id */
209 std::map<std::string, std::unique_ptr<Activation>> activations;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500210
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600211 /** @brief Persistent map of Version D-Bus objects and their
212 * version id */
213 std::map<std::string, std::unique_ptr<Version>> versions;
Saqib Khance148702017-06-11 12:01:58 -0500214
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600215 /** @brief sdbusplus signal match for Software.Version */
216 sdbusplus::bus::match_t versionMatch;
Michael Tritzdd961b62017-05-17 14:07:03 -0500217
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600218 /** @brief This entry's associations */
219 AssociationList assocs = {};
Gunnar Mills9741cd12017-08-28 15:09:00 -0500220
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600221 /** @brief Clears read only PNOR partition for
222 * given Activation D-Bus object
223 *
224 * @param[in] versionId - The id of the ro partition to remove.
225 */
226 void removeReadOnlyPartition(std::string versionId);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500227
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600228 /** @brief Clears read write PNOR partition for
229 * given Activation D-Bus object
230 *
231 * @param[in] versionId - The id of the rw partition to remove.
232 */
233 void removeReadWritePartition(std::string versionId);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500234
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600235 /** @brief Clears preserved PNOR partition */
236 void removePreservedPartition();
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500237
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600238 /** @brief Host factory reset - clears PNOR partitions for each
239 * Activation D-Bus object */
240 void reset() override;
Eddie James13fc66a2017-08-31 15:36:44 -0500241
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600242 /** @brief Check whether the host is running
243 *
244 * @return - Returns true if the Chassis is powered on.
245 */
246 bool isChassisOn();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500247};
248
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500249} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500250} // namespace software
251} // namespace openpower