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