1 #pragma once
2 
3 #include "activation.hpp"
4 #include "version.hpp"
5 #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
6 
7 #include <sdbusplus/server.hpp>
8 #include <xyz/openbmc_project/Association/Definitions/server.hpp>
9 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
10 #include <xyz/openbmc_project/Object/Enable/server.hpp>
11 
12 #include <string>
13 
14 namespace openpower
15 {
16 namespace software
17 {
18 namespace updater
19 {
20 
21 using ItemUpdaterInherit = sdbusplus::server::object_t<
22     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
23     sdbusplus::xyz::openbmc_project::Association::server::Definitions,
24     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
25 using GardResetInherit = sdbusplus::server::object_t<
26     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
27 using ObjectEnable = sdbusplus::server::object_t<
28     sdbusplus::xyz::openbmc_project::Object::server::Enable>;
29 namespace MatchRules = sdbusplus::bus::match::rules;
30 
31 using AssociationList =
32     std::vector<std::tuple<std::string, std::string, std::string>>;
33 
34 constexpr auto GARD_PATH = "/org/open_power/control/gard";
35 constexpr static auto volatilePath = "/org/open_power/control/volatile";
36 
37 /** @class GardReset
38  *  @brief OpenBMC GARD factory reset implementation.
39  *  @details An implementation of xyz.openbmc_project.Common.FactoryReset under
40  *  /org/openpower/control/gard.
41  */
42 class GardReset : public GardResetInherit
43 {
44   public:
45     /** @brief Constructs GardReset.
46      *
47      * @param[in] bus    - The Dbus bus object
48      * @param[in] path   - The Dbus object path
49      */
50     GardReset(sdbusplus::bus_t& bus, const std::string& path) :
51         GardResetInherit(bus, path.c_str(),
52                          GardResetInherit::action::emit_interface_added),
53         bus(bus), path(path)
54     {}
55 
56     virtual ~GardReset()
57     {}
58 
59     /**
60      * @brief GARD factory reset - clears the PNOR GARD partition.
61      */
62     virtual void reset() = 0;
63 
64   protected:
65     // TODO Remove once openbmc/openbmc#1975 is resolved
66     static constexpr auto interface = "xyz.openbmc_project.Common.FactoryReset";
67     sdbusplus::bus_t& bus;
68     std::string path;
69 };
70 
71 /** @class ItemUpdater
72  *  @brief Manages the activation of the host version items.
73  */
74 class ItemUpdater : public ItemUpdaterInherit
75 {
76   public:
77     /** @brief Constructs ItemUpdater
78      *
79      * @param[in] bus    - The D-Bus bus object
80      * @param[in] path   - The D-Bus path
81      */
82     ItemUpdater(sdbusplus::bus_t& bus, const std::string& path) :
83         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
84         versionMatch(bus,
85                      MatchRules::interfacesAdded() +
86                          MatchRules::path("/xyz/openbmc_project/software"),
87                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
88                                this, std::placeholders::_1))
89     {}
90 
91     virtual ~ItemUpdater() = default;
92 
93     /** @brief Sets the given priority free by incrementing
94      *  any existing priority with the same value by 1. It will then continue
95      *  to resolve duplicate priorities caused by this increase, by increasing
96      *  the priority by 1 until there are no more duplicate values.
97      *
98      *  @param[in] value - The priority that needs to be set free.
99      *  @param[in] versionId - The Id of the version for which we
100      *                         are trying to free up the priority.
101      *  @return None
102      */
103     virtual void freePriority(uint8_t value, const std::string& versionId) = 0;
104 
105     /**
106      * @brief Create and populate the active PNOR Version.
107      */
108     virtual void processPNORImage() = 0;
109 
110     /** @brief Deletes version
111      *
112      *  @param[in] entryId - Id of the version to delete
113      *
114      *  @return - Returns true if the version is deleted.
115      */
116     virtual bool erase(std::string entryId);
117 
118     /**
119      * @brief Erases any non-active pnor versions.
120      */
121     virtual void deleteAll() = 0;
122 
123     /** @brief Brings the total number of active PNOR versions to
124      *         ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
125      *         run before activating a new PNOR version. If this function
126      *         needs to delete any PNOR version(s) it will delete the
127      *         version(s) with the highest priority, skipping the
128      *         functional PNOR version.
129      *
130      *  @return - Return if space is freed or not
131      */
132     virtual bool freeSpace() = 0;
133 
134     /** @brief Creates an active association to the
135      *  newly active software image
136      *
137      * @param[in]  path - The path to create the association to.
138      */
139     virtual void createActiveAssociation(const std::string& path);
140 
141     /** @brief Creates a updateable association to the
142      *  "running" BMC software image
143      *
144      * @param[in]  path - The path to create the association.
145      */
146     virtual void createUpdateableAssociation(const std::string& path);
147 
148     /** @brief Updates the functional association to the
149      *  new "running" PNOR image
150      *
151      * @param[in]  versionId - The id of the image to update the association to.
152      */
153     virtual void updateFunctionalAssociation(const std::string& versionId);
154 
155     /** @brief Removes the associations from the provided software image path
156      *
157      * @param[in]  path - The path to remove the association from.
158      */
159     virtual void removeAssociation(const std::string& path);
160 
161     /** @brief Persistent GardReset dbus object */
162     std::unique_ptr<GardReset> gardReset;
163 
164     /** @brief Check whether the provided image id is the functional one
165      *
166      * @param[in] - versionId - The id of the image to check.
167      *
168      * @return - Returns true if this version is currently functional.
169      */
170     virtual bool isVersionFunctional(const std::string& versionId) = 0;
171 
172     /** @brief Persistent ObjectEnable D-Bus object */
173     std::unique_ptr<ObjectEnable> volatileEnable;
174 
175   protected:
176     /** @brief Callback function for Software.Version match.
177      *  @details Creates an Activation D-Bus object.
178      *
179      * @param[in]  msg       - Data associated with subscribed signal
180      */
181     virtual void createActivation(sdbusplus::message_t& msg);
182 
183     /** @brief Create Activation object */
184     virtual std::unique_ptr<Activation> createActivationObject(
185         const std::string& path, const std::string& versionId,
186         const std::string& extVersion,
187         sdbusplus::xyz::openbmc_project::Software::server::Activation::
188             Activations activationStatus,
189         AssociationList& assocs) = 0;
190 
191     /** @brief Create Version object */
192     virtual std::unique_ptr<Version>
193         createVersionObject(const std::string& objPath,
194                             const std::string& versionId,
195                             const std::string& versionString,
196                             sdbusplus::xyz::openbmc_project::Software::server::
197                                 Version::VersionPurpose versionPurpose,
198                             const std::string& filePath) = 0;
199 
200     /** @brief Validate if image is valid or not */
201     virtual bool validateImage(const std::string& path) = 0;
202 
203     /** @brief Persistent sdbusplus D-Bus bus connection. */
204     sdbusplus::bus_t& bus;
205 
206     /** @brief Persistent map of Activation D-Bus objects and their
207      * version id */
208     std::map<std::string, std::unique_ptr<Activation>> activations;
209 
210     /** @brief Persistent map of Version D-Bus objects and their
211      * version id */
212     std::map<std::string, std::unique_ptr<Version>> versions;
213 
214     /** @brief sdbusplus signal match for Software.Version */
215     sdbusplus::bus::match_t versionMatch;
216 
217     /** @brief This entry's associations */
218     AssociationList assocs = {};
219 
220     /** @brief Host factory reset - clears PNOR partitions for each
221      * Activation D-Bus object */
222     void reset() override = 0;
223 
224     /** @brief Check whether the host is running
225      *
226      * @return - Returns true if the Chassis is powered on.
227      */
228     bool isChassisOn();
229 };
230 
231 } // namespace updater
232 } // namespace software
233 } // namespace openpower
234