blob: 7b37e4d4ac5fe38e5ad864e0f1f0a60a4d8756d6 [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 Millsfedbf3d2018-01-17 11:17:31 -0600144 /** @brief Brings the total number of active PNOR versions to
145 * ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
146 * run before activating a new PNOR version. If this function
147 * needs to delete any PNOR version(s) it will delete the
148 * version(s) with the highest priority, skipping the
149 * functional PNOR version.
Saqib Khan2cbfa032017-08-17 14:52:37 -0500150 */
151 void freeSpace();
152
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500153 /** @brief Determine the software version id
154 * from the symlink target (e.g. /media/ro-2a1022fe).
155 *
156 * @param[in] symlinkPath - The path of the symlink.
157 * @param[out] id - The version id as a string.
158 */
159 static std::string determineId(const std::string& symlinkPath);
160
Gunnar Mills9741cd12017-08-28 15:09:00 -0500161 /** @brief Creates an active association to the
162 * newly active software image
163 *
164 * @param[in] path - The path to create the association to.
165 */
Gunnar Mills61010b22017-09-20 15:25:26 -0500166 void createActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500167
Gunnar Mills833e4f32017-09-14 12:30:27 -0500168 /** @brief Updates the functional association to the
169 * new "running" PNOR image
170 *
171 * @param[in] path - The path to update the association to.
172 */
173 void updateFunctionalAssociation(const std::string& path);
174
Gunnar Mills9741cd12017-08-28 15:09:00 -0500175 /** @brief Removes an active association to the software image
176 *
177 * @param[in] path - The path to remove the association from.
178 */
Gunnar Mills61010b22017-09-20 15:25:26 -0500179 void removeActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500180
Michael Tritzb541f1b2017-10-15 15:10:21 -0500181 /** @brief Persistent GardReset dbus object */
182 std::unique_ptr<GardReset> gardReset;
183
Michael Tritz5b756512017-10-06 16:52:01 -0500184 /** @brief Check whether the provided image id is the functional one
185 *
186 * @param[in] - versionId - The id of the image to check.
187 *
188 * @return - Returns true if this version is currently functional.
189 */
190 static bool isVersionFunctional(const std::string& versionId);
191
Adriana Kobylak7cb480e2017-11-07 13:22:59 -0600192 /** @brief Persistent ObjectEnable D-Bus object */
193 std::unique_ptr<ObjectEnable> volatileEnable;
194
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500195 private:
196 /** @brief Callback function for Software.Version match.
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500197 * @details Creates an Activation D-Bus object.
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500198 *
199 * @param[in] msg - Data associated with subscribed signal
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500200 */
Patrick Williams3accb322017-05-30 16:29:52 -0500201 void createActivation(sdbusplus::message::message& msg);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500202
Saqib Khan7254f0e2017-04-10 21:45:37 -0500203 /**
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500204 * @brief Validates the presence of SquashFS image in the image dir.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500205 *
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500206 * @param[in] filePath - The path to the SquashFS image.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500207 * @param[out] result - 0 --> if validation was successful
208 * - -1--> Otherwise
209 */
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500210 static int validateSquashFSImage(const std::string& filePath);
Saqib Khan7254f0e2017-04-10 21:45:37 -0500211
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500212 /** @brief Persistent sdbusplus D-Bus bus connection. */
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500213 sdbusplus::bus::bus& bus;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500214
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500215 /** @brief Persistent map of Activation D-Bus objects and their
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500216 * version id */
Adriana Kobylak268616b2017-04-05 15:23:30 -0500217 std::map<std::string, std::unique_ptr<Activation>> activations;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500218
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500219 /** @brief Persistent map of Version D-Bus objects and their
Saqib Khance148702017-06-11 12:01:58 -0500220 * version id */
221 std::map<std::string, std::unique_ptr<Version>> versions;
222
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500223 /** @brief sdbusplus signal match for Software.Version */
Patrick Williams3accb322017-05-30 16:29:52 -0500224 sdbusplus::bus::match_t versionMatch;
Michael Tritzdd961b62017-05-17 14:07:03 -0500225
Gunnar Mills9741cd12017-08-28 15:09:00 -0500226 /** @brief This entry's associations */
227 AssociationList assocs = {};
228
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500229 /** @brief Clears read only PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500230 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500231 *
232 * @param[in] versionId - The id of the ro partition to remove.
233 */
234 void removeReadOnlyPartition(std::string versionId);
235
236 /** @brief Clears read write PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500237 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500238 *
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500239 * @param[in] versionId - The id of the rw partition to remove.
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500240 */
241 void removeReadWritePartition(std::string versionId);
242
243 /** @brief Clears preserved PNOR partition */
244 void removePreservedPartition();
245
Michael Tritzdd961b62017-05-17 14:07:03 -0500246 /** @brief Host factory reset - clears PNOR partitions for each
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500247 * Activation D-Bus object */
Michael Tritzdd961b62017-05-17 14:07:03 -0500248 void reset() override;
Eddie James13fc66a2017-08-31 15:36:44 -0500249
Eddie James13fc66a2017-08-31 15:36:44 -0500250 /** @brief Check whether the host is running
251 *
252 * @return - Returns true if the Chassis is powered on.
253 */
254 bool isChassisOn();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500255};
256
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500257} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500258} // namespace software
259} // namespace openpower