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>
8d05d4725SJohn 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 
128facccfaSBrad Bishop #include <string>
138facccfaSBrad Bishop 
142d8fa225SAdriana Kobylak namespace openpower
152d8fa225SAdriana Kobylak {
162d8fa225SAdriana Kobylak namespace software
172d8fa225SAdriana Kobylak {
18befe5ce4SAdriana Kobylak namespace updater
192d8fa225SAdriana Kobylak {
202d8fa225SAdriana Kobylak 
21dd961b6cSMichael Tritz using ItemUpdaterInherit = sdbusplus::server::object::object<
229741cd19SGunnar Mills     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
23d05d4725SJohn Wang     sdbusplus::xyz::openbmc_project::Association::server::Definitions,
24234a07e8SMichael Tritz     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
25b541f1b6SMichael Tritz using GardResetInherit = sdbusplus::server::object::object<
26b541f1b6SMichael Tritz     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
277cb480edSAdriana Kobylak using ObjectEnable = sdbusplus::server::object::object<
287cb480edSAdriana Kobylak     sdbusplus::xyz::openbmc_project::Object::server::Enable>;
293accb322SPatrick Williams namespace MatchRules = sdbusplus::bus::match::rules;
30dd961b6cSMichael Tritz 
319741cd19SGunnar Mills using AssociationList =
329741cd19SGunnar Mills     std::vector<std::tuple<std::string, std::string, std::string>>;
339741cd19SGunnar Mills 
344ecea0f3SMichael Tritz constexpr auto GARD_PATH = "/org/open_power/control/gard";
357cb480edSAdriana Kobylak constexpr static auto volatilePath = "/org/open_power/control/volatile";
36b541f1b6SMichael Tritz 
37b541f1b6SMichael Tritz /** @class GardReset
38b541f1b6SMichael Tritz  *  @brief OpenBMC GARD factory reset implementation.
39b541f1b6SMichael Tritz  *  @details An implementation of xyz.openbmc_project.Common.FactoryReset under
40b541f1b6SMichael Tritz  *  /org/openpower/control/gard.
41b541f1b6SMichael Tritz  */
42b541f1b6SMichael Tritz class GardReset : public GardResetInherit
43b541f1b6SMichael Tritz {
44b541f1b6SMichael Tritz   public:
45b541f1b6SMichael Tritz     /** @brief Constructs GardReset.
46b541f1b6SMichael Tritz      *
47b541f1b6SMichael Tritz      * @param[in] bus    - The Dbus bus object
48b541f1b6SMichael Tritz      * @param[in] path   - The Dbus object path
49b541f1b6SMichael Tritz      */
5070dcb63aSAdriana Kobylak     GardReset(sdbusplus::bus::bus& bus, const std::string& path) :
51*9c887d14SPatrick Williams         GardResetInherit(bus, path.c_str(),
52*9c887d14SPatrick Williams                          GardResetInherit::action::emit_interface_added),
53*9c887d14SPatrick Williams         bus(bus), path(path)
54*9c887d14SPatrick Williams     {}
55b541f1b6SMichael Tritz 
56716de5b8SLei YU     virtual ~GardReset()
57*9c887d14SPatrick Williams     {}
58b541f1b6SMichael Tritz 
59716de5b8SLei YU   protected:
60b541f1b6SMichael Tritz     // TODO Remove once openbmc/openbmc#1975 is resolved
6170dcb63aSAdriana Kobylak     static constexpr auto interface = "xyz.openbmc_project.Common.FactoryReset";
62b541f1b6SMichael Tritz     sdbusplus::bus::bus& bus;
63b541f1b6SMichael Tritz     std::string path;
64b541f1b6SMichael Tritz 
65b541f1b6SMichael Tritz     /**
66b541f1b6SMichael Tritz      * @brief GARD factory reset - clears the PNOR GARD partition.
67b541f1b6SMichael Tritz      */
68716de5b8SLei YU     virtual void reset() = 0;
69b541f1b6SMichael Tritz };
70b541f1b6SMichael Tritz 
712d8fa225SAdriana Kobylak /** @class ItemUpdater
72139cf1a1SGunnar Mills  *  @brief Manages the activation of the host version items.
732d8fa225SAdriana Kobylak  */
74dd961b6cSMichael Tritz class ItemUpdater : public ItemUpdaterInherit
752d8fa225SAdriana Kobylak {
762d8fa225SAdriana Kobylak   public:
772d8fa225SAdriana Kobylak     /** @brief Constructs ItemUpdater
782d8fa225SAdriana Kobylak      *
79139cf1a1SGunnar Mills      * @param[in] bus    - The D-Bus bus object
80139cf1a1SGunnar Mills      * @param[in] path   - The D-Bus path
812d8fa225SAdriana Kobylak      */
82dd961b6cSMichael Tritz     ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
8370dcb63aSAdriana Kobylak         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
8470dcb63aSAdriana Kobylak         versionMatch(bus,
853accb322SPatrick Williams                      MatchRules::interfacesAdded() +
863accb322SPatrick Williams                          MatchRules::path("/xyz/openbmc_project/software"),
8770dcb63aSAdriana Kobylak                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
8870dcb63aSAdriana Kobylak                                this, std::placeholders::_1))
898facccfaSBrad Bishop     {}
902d8fa225SAdriana Kobylak 
91f3ce4337SLei YU     virtual ~ItemUpdater() = default;
92f3ce4337SLei YU 
9381bac88cSSaqib Khan     /** @brief Sets the given priority free by incrementing
94abe862aaSAdriana Kobylak      *  any existing priority with the same value by 1. It will then continue
95abe862aaSAdriana Kobylak      *  to resolve duplicate priorities caused by this increase, by increasing
96abe862aaSAdriana Kobylak      *  the priority by 1 until there are no more duplicate values.
9781bac88cSSaqib Khan      *
9881bac88cSSaqib Khan      *  @param[in] value - The priority that needs to be set free.
99b8e7f313SSaqib Khan      *  @param[in] versionId - The Id of the version for which we
100b8e7f313SSaqib Khan      *                         are trying to free up the priority.
10181bac88cSSaqib Khan      *  @return None
10281bac88cSSaqib Khan      */
103f3ce4337SLei YU     virtual void freePriority(uint8_t value, const std::string& versionId) = 0;
10481bac88cSSaqib Khan 
105167601b5SSaqib Khan     /**
106167601b5SSaqib Khan      * @brief Create and populate the active PNOR Version.
107167601b5SSaqib Khan      */
108f3ce4337SLei YU     virtual void processPNORImage() = 0;
109167601b5SSaqib Khan 
1109c8adfa3SLeonel Gonzalez     /** @brief Deletes version
1119c8adfa3SLeonel Gonzalez      *
1129c8adfa3SLeonel Gonzalez      *  @param[in] entryId - Id of the version to delete
1139c8adfa3SLeonel Gonzalez      *
114f3ce4337SLei YU      *  @return - Returns true if the version is deleted.
1159c8adfa3SLeonel Gonzalez      */
116f3ce4337SLei YU     virtual bool erase(std::string entryId);
1179c8adfa3SLeonel Gonzalez 
118234a07e8SMichael Tritz     /**
119234a07e8SMichael Tritz      * @brief Erases any non-active pnor versions.
120234a07e8SMichael Tritz      */
121f3ce4337SLei YU     virtual void deleteAll() = 0;
122234a07e8SMichael Tritz 
123fedbf3d3SGunnar Mills     /** @brief Brings the total number of active PNOR versions to
124fedbf3d3SGunnar Mills      *         ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
125fedbf3d3SGunnar Mills      *         run before activating a new PNOR version. If this function
126fedbf3d3SGunnar Mills      *         needs to delete any PNOR version(s) it will delete the
127fedbf3d3SGunnar Mills      *         version(s) with the highest priority, skipping the
128fedbf3d3SGunnar Mills      *         functional PNOR version.
1296da3dae3SLei YU      *
1306da3dae3SLei YU      *  @return - Return if space is freed or not
1312cbfa035SSaqib Khan      */
1326da3dae3SLei YU     virtual bool freeSpace() = 0;
1332cbfa035SSaqib Khan 
1349741cd19SGunnar Mills     /** @brief Creates an active association to the
1359741cd19SGunnar Mills      *  newly active software image
1369741cd19SGunnar Mills      *
1379741cd19SGunnar Mills      * @param[in]  path - The path to create the association to.
1389741cd19SGunnar Mills      */
139f3ce4337SLei YU     virtual void createActiveAssociation(const std::string& path);
1409741cd19SGunnar Mills 
1413c81037eSAdriana Kobylak     /** @brief Creates a updateable association to the
1423c81037eSAdriana Kobylak      *  "running" BMC software image
1433c81037eSAdriana Kobylak      *
1443c81037eSAdriana Kobylak      * @param[in]  path - The path to create the association.
1453c81037eSAdriana Kobylak      */
1463c81037eSAdriana Kobylak     virtual void createUpdateableAssociation(const std::string& path);
1473c81037eSAdriana Kobylak 
148833e4f37SGunnar Mills     /** @brief Updates the functional association to the
149833e4f37SGunnar Mills      *  new "running" PNOR image
150833e4f37SGunnar Mills      *
151f3ce4337SLei YU      * @param[in]  versionId - The id of the image to update the association to.
152833e4f37SGunnar Mills      */
153f3ce4337SLei YU     virtual void updateFunctionalAssociation(const std::string& versionId);
154833e4f37SGunnar Mills 
155b5237174SAdriana Kobylak     /** @brief Removes the associations from the provided software image path
1569741cd19SGunnar Mills      *
1579741cd19SGunnar Mills      * @param[in]  path - The path to remove the association from.
1589741cd19SGunnar Mills      */
159f3ce4337SLei YU     virtual void removeAssociation(const std::string& path);
1609741cd19SGunnar Mills 
161b541f1b6SMichael Tritz     /** @brief Persistent GardReset dbus object */
162b541f1b6SMichael Tritz     std::unique_ptr<GardReset> gardReset;
163b541f1b6SMichael Tritz 
1645b75651bSMichael Tritz     /** @brief Check whether the provided image id is the functional one
1655b75651bSMichael Tritz      *
1665b75651bSMichael Tritz      * @param[in] - versionId - The id of the image to check.
1675b75651bSMichael Tritz      *
1685b75651bSMichael Tritz      * @return - Returns true if this version is currently functional.
1695b75651bSMichael Tritz      */
170f3ce4337SLei YU     virtual bool isVersionFunctional(const std::string& versionId) = 0;
1715b75651bSMichael Tritz 
1727cb480edSAdriana Kobylak     /** @brief Persistent ObjectEnable D-Bus object */
1737cb480edSAdriana Kobylak     std::unique_ptr<ObjectEnable> volatileEnable;
1747cb480edSAdriana Kobylak 
175f3ce4337SLei YU   protected:
1762d8fa225SAdriana Kobylak     /** @brief Callback function for Software.Version match.
177139cf1a1SGunnar Mills      *  @details Creates an Activation D-Bus object.
1782d8fa225SAdriana Kobylak      *
1792d8fa225SAdriana Kobylak      * @param[in]  msg       - Data associated with subscribed signal
1802d8fa225SAdriana Kobylak      */
181a9ac9279SLei YU     virtual void createActivation(sdbusplus::message::message& msg);
182a9ac9279SLei YU 
183a9ac9279SLei YU     /** @brief Create Activation object */
184a9ac9279SLei YU     virtual std::unique_ptr<Activation> createActivationObject(
185a9ac9279SLei YU         const std::string& path, const std::string& versionId,
186a9ac9279SLei YU         const std::string& extVersion,
187a9ac9279SLei YU         sdbusplus::xyz::openbmc_project::Software::server::Activation::
188a9ac9279SLei YU             Activations activationStatus,
189a9ac9279SLei YU         AssociationList& assocs) = 0;
190a9ac9279SLei YU 
191a9ac9279SLei YU     /** @brief Create Version object */
192a9ac9279SLei YU     virtual std::unique_ptr<Version>
193a9ac9279SLei YU         createVersionObject(const std::string& objPath,
194a9ac9279SLei YU                             const std::string& versionId,
195a9ac9279SLei YU                             const std::string& versionString,
196a9ac9279SLei YU                             sdbusplus::xyz::openbmc_project::Software::server::
197a9ac9279SLei YU                                 Version::VersionPurpose versionPurpose,
198a9ac9279SLei YU                             const std::string& filePath) = 0;
199a9ac9279SLei YU 
200a9ac9279SLei YU     /** @brief Validate if image is valid or not */
201a9ac9279SLei YU     virtual bool validateImage(const std::string& path) = 0;
2027254f0eaSSaqib Khan 
203139cf1a1SGunnar Mills     /** @brief Persistent sdbusplus D-Bus bus connection. */
204d6a549eaSAdriana Kobylak     sdbusplus::bus::bus& bus;
2052d8fa225SAdriana Kobylak 
206139cf1a1SGunnar Mills     /** @brief Persistent map of Activation D-Bus objects and their
2072d8fa225SAdriana Kobylak      * version id */
208268616bfSAdriana Kobylak     std::map<std::string, std::unique_ptr<Activation>> activations;
2092d8fa225SAdriana Kobylak 
210139cf1a1SGunnar Mills     /** @brief Persistent map of Version D-Bus objects and their
211ce148700SSaqib Khan      * version id */
212ce148700SSaqib Khan     std::map<std::string, std::unique_ptr<Version>> versions;
213ce148700SSaqib Khan 
2142d8fa225SAdriana Kobylak     /** @brief sdbusplus signal match for Software.Version */
2153accb322SPatrick Williams     sdbusplus::bus::match_t versionMatch;
216dd961b6cSMichael Tritz 
2179741cd19SGunnar Mills     /** @brief This entry's associations */
2189741cd19SGunnar Mills     AssociationList assocs = {};
2199741cd19SGunnar Mills 
220dd961b6cSMichael Tritz     /** @brief Host factory reset - clears PNOR partitions for each
221139cf1a1SGunnar Mills      * Activation D-Bus object */
222f3ce4337SLei YU     void reset() override = 0;
22313fc66adSEddie James 
22413fc66adSEddie James     /** @brief Check whether the host is running
22513fc66adSEddie James      *
22613fc66adSEddie James      * @return - Returns true if the Chassis is powered on.
22713fc66adSEddie James      */
22813fc66adSEddie James     bool isChassisOn();
2292d8fa225SAdriana Kobylak };
2302d8fa225SAdriana Kobylak 
231befe5ce4SAdriana Kobylak } // namespace updater
2322d8fa225SAdriana Kobylak } // namespace software
2332d8fa225SAdriana Kobylak } // namespace openpower
234