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