12d8fa225SAdriana Kobylak #pragma once
22d8fa225SAdriana Kobylak 
32d8fa225SAdriana Kobylak #include "activation.hpp"
4f6ed5897SGunnar Mills #include "version.hpp"
5f6ed5897SGunnar Mills #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
6f6ed5897SGunnar Mills 
7f6ed5897SGunnar Mills #include <sdbusplus/server.hpp>
8*d05d4725SJohn Wang #include <xyz/openbmc_project/Association/Definitions/server.hpp>
9dd961b6cSMichael Tritz #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
107cb480edSAdriana Kobylak #include <xyz/openbmc_project/Object/Enable/server.hpp>
112d8fa225SAdriana Kobylak 
122d8fa225SAdriana Kobylak namespace openpower
132d8fa225SAdriana Kobylak {
142d8fa225SAdriana Kobylak namespace software
152d8fa225SAdriana Kobylak {
16befe5ce4SAdriana Kobylak namespace updater
172d8fa225SAdriana Kobylak {
182d8fa225SAdriana Kobylak 
19dd961b6cSMichael Tritz using ItemUpdaterInherit = sdbusplus::server::object::object<
209741cd19SGunnar Mills     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
21*d05d4725SJohn Wang     sdbusplus::xyz::openbmc_project::Association::server::Definitions,
22234a07e8SMichael Tritz     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
23b541f1b6SMichael Tritz using GardResetInherit = sdbusplus::server::object::object<
24b541f1b6SMichael Tritz     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
257cb480edSAdriana Kobylak using ObjectEnable = sdbusplus::server::object::object<
267cb480edSAdriana Kobylak     sdbusplus::xyz::openbmc_project::Object::server::Enable>;
273accb322SPatrick Williams namespace MatchRules = sdbusplus::bus::match::rules;
28dd961b6cSMichael Tritz 
299741cd19SGunnar Mills using AssociationList =
309741cd19SGunnar Mills     std::vector<std::tuple<std::string, std::string, std::string>>;
319741cd19SGunnar Mills 
324ecea0f3SMichael Tritz constexpr auto GARD_PATH = "/org/open_power/control/gard";
337cb480edSAdriana Kobylak constexpr static auto volatilePath = "/org/open_power/control/volatile";
34b541f1b6SMichael Tritz 
35b541f1b6SMichael Tritz /** @class GardReset
36b541f1b6SMichael Tritz  *  @brief OpenBMC GARD factory reset implementation.
37b541f1b6SMichael Tritz  *  @details An implementation of xyz.openbmc_project.Common.FactoryReset under
38b541f1b6SMichael Tritz  *  /org/openpower/control/gard.
39b541f1b6SMichael Tritz  */
40b541f1b6SMichael Tritz class GardReset : public GardResetInherit
41b541f1b6SMichael Tritz {
42b541f1b6SMichael Tritz   public:
43b541f1b6SMichael Tritz     /** @brief Constructs GardReset.
44b541f1b6SMichael Tritz      *
45b541f1b6SMichael Tritz      * @param[in] bus    - The Dbus bus object
46b541f1b6SMichael Tritz      * @param[in] path   - The Dbus object path
47b541f1b6SMichael Tritz      */
4870dcb63aSAdriana Kobylak     GardReset(sdbusplus::bus::bus& bus, const std::string& path) :
4970dcb63aSAdriana Kobylak         GardResetInherit(bus, path.c_str(), true), bus(bus), path(path)
50b541f1b6SMichael Tritz     {
51b541f1b6SMichael Tritz         std::vector<std::string> interfaces({interface});
52b541f1b6SMichael Tritz         bus.emit_interfaces_added(path.c_str(), interfaces);
53b541f1b6SMichael Tritz     }
54b541f1b6SMichael Tritz 
55716de5b8SLei YU     virtual ~GardReset()
56b541f1b6SMichael Tritz     {
57b541f1b6SMichael Tritz         std::vector<std::string> interfaces({interface});
58b541f1b6SMichael Tritz         bus.emit_interfaces_removed(path.c_str(), interfaces);
59b541f1b6SMichael Tritz     }
60b541f1b6SMichael Tritz 
61716de5b8SLei YU   protected:
62b541f1b6SMichael Tritz     // TODO Remove once openbmc/openbmc#1975 is resolved
6370dcb63aSAdriana Kobylak     static constexpr auto interface = "xyz.openbmc_project.Common.FactoryReset";
64b541f1b6SMichael Tritz     sdbusplus::bus::bus& bus;
65b541f1b6SMichael Tritz     std::string path;
66b541f1b6SMichael Tritz 
67b541f1b6SMichael Tritz     /**
68b541f1b6SMichael Tritz      * @brief GARD factory reset - clears the PNOR GARD partition.
69b541f1b6SMichael Tritz      */
70716de5b8SLei YU     virtual void reset() = 0;
71b541f1b6SMichael Tritz };
72b541f1b6SMichael Tritz 
732d8fa225SAdriana Kobylak /** @class ItemUpdater
74139cf1a1SGunnar Mills  *  @brief Manages the activation of the host version items.
752d8fa225SAdriana Kobylak  */
76dd961b6cSMichael Tritz class ItemUpdater : public ItemUpdaterInherit
772d8fa225SAdriana Kobylak {
782d8fa225SAdriana Kobylak   public:
792d8fa225SAdriana Kobylak     /** @brief Constructs ItemUpdater
802d8fa225SAdriana Kobylak      *
81139cf1a1SGunnar Mills      * @param[in] bus    - The D-Bus bus object
82139cf1a1SGunnar Mills      * @param[in] path   - The D-Bus path
832d8fa225SAdriana Kobylak      */
84dd961b6cSMichael Tritz     ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
8570dcb63aSAdriana Kobylak         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
8670dcb63aSAdriana Kobylak         versionMatch(bus,
873accb322SPatrick Williams                      MatchRules::interfacesAdded() +
883accb322SPatrick Williams                          MatchRules::path("/xyz/openbmc_project/software"),
8970dcb63aSAdriana Kobylak                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
9070dcb63aSAdriana Kobylak                                this, std::placeholders::_1))
912d8fa225SAdriana Kobylak     {
922d8fa225SAdriana Kobylak     }
932d8fa225SAdriana Kobylak 
94f3ce4337SLei YU     virtual ~ItemUpdater() = default;
95f3ce4337SLei YU 
9681bac88cSSaqib Khan     /** @brief Sets the given priority free by incrementing
97abe862aaSAdriana Kobylak      *  any existing priority with the same value by 1. It will then continue
98abe862aaSAdriana Kobylak      *  to resolve duplicate priorities caused by this increase, by increasing
99abe862aaSAdriana Kobylak      *  the priority by 1 until there are no more duplicate values.
10081bac88cSSaqib Khan      *
10181bac88cSSaqib Khan      *  @param[in] value - The priority that needs to be set free.
102b8e7f313SSaqib Khan      *  @param[in] versionId - The Id of the version for which we
103b8e7f313SSaqib Khan      *                         are trying to free up the priority.
10481bac88cSSaqib Khan      *  @return None
10581bac88cSSaqib Khan      */
106f3ce4337SLei YU     virtual void freePriority(uint8_t value, const std::string& versionId) = 0;
10781bac88cSSaqib Khan 
108167601b5SSaqib Khan     /**
109167601b5SSaqib Khan      * @brief Create and populate the active PNOR Version.
110167601b5SSaqib Khan      */
111f3ce4337SLei YU     virtual void processPNORImage() = 0;
112167601b5SSaqib Khan 
1139c8adfa3SLeonel Gonzalez     /** @brief Deletes version
1149c8adfa3SLeonel Gonzalez      *
1159c8adfa3SLeonel Gonzalez      *  @param[in] entryId - Id of the version to delete
1169c8adfa3SLeonel Gonzalez      *
117f3ce4337SLei YU      *  @return - Returns true if the version is deleted.
1189c8adfa3SLeonel Gonzalez      */
119f3ce4337SLei YU     virtual bool erase(std::string entryId);
1209c8adfa3SLeonel Gonzalez 
121234a07e8SMichael Tritz     /**
122234a07e8SMichael Tritz      * @brief Erases any non-active pnor versions.
123234a07e8SMichael Tritz      */
124f3ce4337SLei YU     virtual void deleteAll() = 0;
125234a07e8SMichael Tritz 
126fedbf3d3SGunnar Mills     /** @brief Brings the total number of active PNOR versions to
127fedbf3d3SGunnar Mills      *         ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
128fedbf3d3SGunnar Mills      *         run before activating a new PNOR version. If this function
129fedbf3d3SGunnar Mills      *         needs to delete any PNOR version(s) it will delete the
130fedbf3d3SGunnar Mills      *         version(s) with the highest priority, skipping the
131fedbf3d3SGunnar Mills      *         functional PNOR version.
1326da3dae3SLei YU      *
1336da3dae3SLei YU      *  @return - Return if space is freed or not
1342cbfa035SSaqib Khan      */
1356da3dae3SLei YU     virtual bool freeSpace() = 0;
1362cbfa035SSaqib Khan 
1379741cd19SGunnar Mills     /** @brief Creates an active association to the
1389741cd19SGunnar Mills      *  newly active software image
1399741cd19SGunnar Mills      *
1409741cd19SGunnar Mills      * @param[in]  path - The path to create the association to.
1419741cd19SGunnar Mills      */
142f3ce4337SLei YU     virtual void createActiveAssociation(const std::string& path);
1439741cd19SGunnar Mills 
144833e4f37SGunnar Mills     /** @brief Updates the functional association to the
145833e4f37SGunnar Mills      *  new "running" PNOR image
146833e4f37SGunnar Mills      *
147f3ce4337SLei YU      * @param[in]  versionId - The id of the image to update the association to.
148833e4f37SGunnar Mills      */
149f3ce4337SLei YU     virtual void updateFunctionalAssociation(const std::string& versionId);
150833e4f37SGunnar Mills 
151b5237174SAdriana Kobylak     /** @brief Removes the associations from the provided software image path
1529741cd19SGunnar Mills      *
1539741cd19SGunnar Mills      * @param[in]  path - The path to remove the association from.
1549741cd19SGunnar Mills      */
155f3ce4337SLei YU     virtual void removeAssociation(const std::string& path);
1569741cd19SGunnar Mills 
157b541f1b6SMichael Tritz     /** @brief Persistent GardReset dbus object */
158b541f1b6SMichael Tritz     std::unique_ptr<GardReset> gardReset;
159b541f1b6SMichael Tritz 
1605b75651bSMichael Tritz     /** @brief Check whether the provided image id is the functional one
1615b75651bSMichael Tritz      *
1625b75651bSMichael Tritz      * @param[in] - versionId - The id of the image to check.
1635b75651bSMichael Tritz      *
1645b75651bSMichael Tritz      * @return - Returns true if this version is currently functional.
1655b75651bSMichael Tritz      */
166f3ce4337SLei YU     virtual bool isVersionFunctional(const std::string& versionId) = 0;
1675b75651bSMichael Tritz 
1687cb480edSAdriana Kobylak     /** @brief Persistent ObjectEnable D-Bus object */
1697cb480edSAdriana Kobylak     std::unique_ptr<ObjectEnable> volatileEnable;
1707cb480edSAdriana Kobylak 
171f3ce4337SLei YU   protected:
1722d8fa225SAdriana Kobylak     /** @brief Callback function for Software.Version match.
173139cf1a1SGunnar Mills      *  @details Creates an Activation D-Bus object.
1742d8fa225SAdriana Kobylak      *
1752d8fa225SAdriana Kobylak      * @param[in]  msg       - Data associated with subscribed signal
1762d8fa225SAdriana Kobylak      */
177a9ac9279SLei YU     virtual void createActivation(sdbusplus::message::message& msg);
178a9ac9279SLei YU 
179a9ac9279SLei YU     /** @brief Create Activation object */
180a9ac9279SLei YU     virtual std::unique_ptr<Activation> createActivationObject(
181a9ac9279SLei YU         const std::string& path, const std::string& versionId,
182a9ac9279SLei YU         const std::string& extVersion,
183a9ac9279SLei YU         sdbusplus::xyz::openbmc_project::Software::server::Activation::
184a9ac9279SLei YU             Activations activationStatus,
185a9ac9279SLei YU         AssociationList& assocs) = 0;
186a9ac9279SLei YU 
187a9ac9279SLei YU     /** @brief Create Version object */
188a9ac9279SLei YU     virtual std::unique_ptr<Version>
189a9ac9279SLei YU         createVersionObject(const std::string& objPath,
190a9ac9279SLei YU                             const std::string& versionId,
191a9ac9279SLei YU                             const std::string& versionString,
192a9ac9279SLei YU                             sdbusplus::xyz::openbmc_project::Software::server::
193a9ac9279SLei YU                                 Version::VersionPurpose versionPurpose,
194a9ac9279SLei YU                             const std::string& filePath) = 0;
195a9ac9279SLei YU 
196a9ac9279SLei YU     /** @brief Validate if image is valid or not */
197a9ac9279SLei YU     virtual bool validateImage(const std::string& path) = 0;
1987254f0eaSSaqib Khan 
199139cf1a1SGunnar Mills     /** @brief Persistent sdbusplus D-Bus bus connection. */
200d6a549eaSAdriana Kobylak     sdbusplus::bus::bus& bus;
2012d8fa225SAdriana Kobylak 
202139cf1a1SGunnar Mills     /** @brief Persistent map of Activation D-Bus objects and their
2032d8fa225SAdriana Kobylak      * version id */
204268616bfSAdriana Kobylak     std::map<std::string, std::unique_ptr<Activation>> activations;
2052d8fa225SAdriana Kobylak 
206139cf1a1SGunnar Mills     /** @brief Persistent map of Version D-Bus objects and their
207ce148700SSaqib Khan      * version id */
208ce148700SSaqib Khan     std::map<std::string, std::unique_ptr<Version>> versions;
209ce148700SSaqib Khan 
2102d8fa225SAdriana Kobylak     /** @brief sdbusplus signal match for Software.Version */
2113accb322SPatrick Williams     sdbusplus::bus::match_t versionMatch;
212dd961b6cSMichael Tritz 
2139741cd19SGunnar Mills     /** @brief This entry's associations */
2149741cd19SGunnar Mills     AssociationList assocs = {};
2159741cd19SGunnar Mills 
216dd961b6cSMichael Tritz     /** @brief Host factory reset - clears PNOR partitions for each
217139cf1a1SGunnar Mills      * Activation D-Bus object */
218f3ce4337SLei YU     void reset() override = 0;
21913fc66adSEddie James 
22013fc66adSEddie James     /** @brief Check whether the host is running
22113fc66adSEddie James      *
22213fc66adSEddie James      * @return - Returns true if the Chassis is powered on.
22313fc66adSEddie James      */
22413fc66adSEddie James     bool isChassisOn();
2252d8fa225SAdriana Kobylak };
2262d8fa225SAdriana Kobylak 
227befe5ce4SAdriana Kobylak } // namespace updater
2282d8fa225SAdriana Kobylak } // namespace software
2292d8fa225SAdriana Kobylak } // namespace openpower
230