blob: 893e8b9c54330b247e30af0b8d76ffbb077d29ed [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>
Saqib Khance148702017-06-11 12:01:58 -05006#include "version.hpp"
Gunnar Mills9741cd12017-08-28 15:09:00 -05007#include "org/openbmc/Associations/server.hpp"
Michael Tritz234a07e2017-09-21 00:53:06 -05008#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05009
10namespace openpower
11{
12namespace software
13{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050014namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050015{
16
Michael Tritzdd961b62017-05-17 14:07:03 -050017using ItemUpdaterInherit = sdbusplus::server::object::object<
Gunnar Mills139cf1a2017-09-21 16:25:37 -050018 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
19 sdbusplus::org::openbmc::server::Associations,
20 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritzb541f1b2017-10-15 15:10:21 -050021using GardResetInherit = sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Patrick Williams3accb322017-05-30 16:29:52 -050023namespace MatchRules = sdbusplus::bus::match::rules;
Michael Tritzdd961b62017-05-17 14:07:03 -050024
Gunnar Mills9741cd12017-08-28 15:09:00 -050025using AssociationList =
26 std::vector<std::tuple<std::string, std::string, std::string>>;
27
Michael Tritzb541f1b2017-10-15 15:10:21 -050028constexpr 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 */
35class 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 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{
77 public:
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050078 /** @brief Constructs ItemUpdater
79 *
Gunnar Mills139cf1a2017-09-21 16:25:37 -050080 * @param[in] bus - The D-Bus bus object
81 * @param[in] path - The D-Bus path
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050082 */
Michael Tritzdd961b62017-05-17 14:07:03 -050083 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
84 ItemUpdaterInherit(bus, path.c_str()),
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050085 bus(bus),
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050086 versionMatch(
87 bus,
Patrick Williams3accb322017-05-30 16:29:52 -050088 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 Kobylak2d8fa222017-03-15 12:34:32 -050094 {
Saqib Khan167601b2017-06-18 23:33:46 -050095 processPNORImage();
Michael Tritzb541f1b2017-10-15 15:10:21 -050096 gardReset = std::make_unique<GardReset>(bus, GARD_PATH);
97
98 // Emit deferred signal.
99 emit_object_added();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500100 }
101
Saqib Khan81bac882017-06-08 12:17:01 -0500102 /** @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 Khanb8e7f312017-08-12 10:24:10 -0500106 * @param[in] versionId - The Id of the version for which we
107 * are trying to free up the priority.
Saqib Khan81bac882017-06-08 12:17:01 -0500108 * @return None
109 */
Saqib Khanb8e7f312017-08-12 10:24:10 -0500110 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan81bac882017-06-08 12:17:01 -0500111
Saqib Khan2af5c492017-07-17 16:15:13 -0500112 /** @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 Khan167601b2017-06-18 23:33:46 -0500121 /**
122 * @brief Create and populate the active PNOR Version.
123 */
124 void processPNORImage();
125
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500126 /** @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 Tritz234a07e2017-09-21 00:53:06 -0500134 /**
135 * @brief Erases any non-active pnor versions.
136 */
137 void deleteAll();
138
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500139 /** @brief Deletes the active PNOR version with highest priority
140 if the total number of volumes exceeds the threshold.
Saqib Khan2cbfa032017-08-17 14:52:37 -0500141 */
142 void freeSpace();
143
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500144 /** @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 Mills9741cd12017-08-28 15:09:00 -0500152 /** @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 Mills61010b22017-09-20 15:25:26 -0500157 void createActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500158
Gunnar Mills833e4f32017-09-14 12:30:27 -0500159 /** @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 Mills9741cd12017-08-28 15:09:00 -0500166 /** @brief Removes an active association to the software image
167 *
168 * @param[in] path - The path to remove the association from.
169 */
Gunnar Mills61010b22017-09-20 15:25:26 -0500170 void removeActiveAssociation(const std::string& path);
Gunnar Mills9741cd12017-08-28 15:09:00 -0500171
Michael Tritzb541f1b2017-10-15 15:10:21 -0500172 /** @brief Persistent GardReset dbus object */
173 std::unique_ptr<GardReset> gardReset;
174
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500175 private:
176 /** @brief Callback function for Software.Version match.
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500177 * @details Creates an Activation D-Bus object.
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500178 *
179 * @param[in] msg - Data associated with subscribed signal
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500180 */
Patrick Williams3accb322017-05-30 16:29:52 -0500181 void createActivation(sdbusplus::message::message& msg);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500182
Saqib Khan7254f0e2017-04-10 21:45:37 -0500183 /**
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500184 * @brief Validates the presence of SquashFS image in the image dir.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500185 *
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500186 * @param[in] filePath - The path to the SquashFS image.
Saqib Khana8ade7e2017-04-12 10:27:56 -0500187 * @param[out] result - 0 --> if validation was successful
188 * - -1--> Otherwise
189 */
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500190 static int validateSquashFSImage(const std::string& filePath);
Saqib Khan7254f0e2017-04-10 21:45:37 -0500191
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500192 /** @brief Persistent sdbusplus D-Bus bus connection. */
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500193 sdbusplus::bus::bus& bus;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500194
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500195 /** @brief Persistent map of Activation D-Bus objects and their
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500196 * version id */
Adriana Kobylak268616b2017-04-05 15:23:30 -0500197 std::map<std::string, std::unique_ptr<Activation>> activations;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500198
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500199 /** @brief Persistent map of Version D-Bus objects and their
Saqib Khance148702017-06-11 12:01:58 -0500200 * version id */
201 std::map<std::string, std::unique_ptr<Version>> versions;
202
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500203 /** @brief sdbusplus signal match for Software.Version */
Patrick Williams3accb322017-05-30 16:29:52 -0500204 sdbusplus::bus::match_t versionMatch;
Michael Tritzdd961b62017-05-17 14:07:03 -0500205
Gunnar Mills9741cd12017-08-28 15:09:00 -0500206 /** @brief This entry's associations */
207 AssociationList assocs = {};
208
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500209 /** @brief Clears read only PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500210 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500211 *
212 * @param[in] versionId - The id of the ro partition to remove.
213 */
214 void removeReadOnlyPartition(std::string versionId);
215
216 /** @brief Clears read write PNOR partition for
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500217 * given Activation D-Bus object
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500218 *
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500219 * @param[in] versionId - The id of the rw partition to remove.
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500220 */
221 void removeReadWritePartition(std::string versionId);
222
223 /** @brief Clears preserved PNOR partition */
224 void removePreservedPartition();
225
Michael Tritzdd961b62017-05-17 14:07:03 -0500226 /** @brief Host factory reset - clears PNOR partitions for each
Gunnar Mills139cf1a2017-09-21 16:25:37 -0500227 * Activation D-Bus object */
Michael Tritzdd961b62017-05-17 14:07:03 -0500228 void reset() override;
Eddie James13fc66a2017-08-31 15:36:44 -0500229
230 /** @brief Check whether the provided image id is the functional one
231 *
232 * @param[in] - versionId - The id of the image to check.
233 *
234 * @return - Returns true if this version is currently functional.
235 */
236 static bool isVersionFunctional(std::string versionId);
237
238 /** @brief Check whether the host is running
239 *
240 * @return - Returns true if the Chassis is powered on.
241 */
242 bool isChassisOn();
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500243};
244
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500245} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500246} // namespace software
247} // namespace openpower