12d8fa225SAdriana Kobylak #pragma once
22d8fa225SAdriana Kobylak 
32d8fa225SAdriana Kobylak #include "activation.hpp"
4f6ed5897SGunnar Mills #include "org/openbmc/Associations/server.hpp"
5f6ed5897SGunnar Mills #include "version.hpp"
6f6ed5897SGunnar Mills #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
7f6ed5897SGunnar Mills 
8f6ed5897SGunnar Mills #include <sdbusplus/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,
21234a07e8SMichael Tritz     sdbusplus::org::openbmc::server::Associations,
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 
55b541f1b6SMichael Tritz     ~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 
61b541f1b6SMichael Tritz   private:
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      */
70b541f1b6SMichael Tritz     void reset() override;
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     {
92167601b5SSaqib Khan         processPNORImage();
93b541f1b6SMichael Tritz         gardReset = std::make_unique<GardReset>(bus, GARD_PATH);
947cb480edSAdriana Kobylak         volatileEnable = std::make_unique<ObjectEnable>(bus, volatilePath);
95b541f1b6SMichael Tritz 
96b541f1b6SMichael Tritz         // Emit deferred signal.
97b541f1b6SMichael Tritz         emit_object_added();
982d8fa225SAdriana Kobylak     }
992d8fa225SAdriana Kobylak 
10081bac88cSSaqib Khan     /** @brief Sets the given priority free by incrementing
10181bac88cSSaqib Khan      *  any existing priority with the same value by 1
10281bac88cSSaqib Khan      *
10381bac88cSSaqib Khan      *  @param[in] value - The priority that needs to be set free.
104b8e7f313SSaqib Khan      *  @param[in] versionId - The Id of the version for which we
105b8e7f313SSaqib Khan      *                         are trying to free up the priority.
10681bac88cSSaqib Khan      *  @return None
10781bac88cSSaqib Khan      */
108b8e7f313SSaqib Khan     void freePriority(uint8_t value, const std::string& versionId);
10981bac88cSSaqib Khan 
1102af5c496SSaqib Khan     /** @brief Determine is the given priority is the lowest
1112af5c496SSaqib Khan      *
1122af5c496SSaqib Khan      *  @param[in] value - The priority that needs to be checked.
1132af5c496SSaqib Khan      *
1142af5c496SSaqib Khan      *  @return boolean corresponding to whether the given
1152af5c496SSaqib Khan      *           priority is lowest.
1162af5c496SSaqib Khan      */
1172af5c496SSaqib Khan     bool isLowestPriority(uint8_t value);
1182af5c496SSaqib Khan 
119167601b5SSaqib Khan     /**
120167601b5SSaqib Khan      * @brief Create and populate the active PNOR Version.
121167601b5SSaqib Khan      */
122167601b5SSaqib Khan     void processPNORImage();
123167601b5SSaqib Khan 
1249c8adfa3SLeonel Gonzalez     /** @brief Deletes version
1259c8adfa3SLeonel Gonzalez      *
1269c8adfa3SLeonel Gonzalez      *  @param[in] entryId - Id of the version to delete
1279c8adfa3SLeonel Gonzalez      *
1289c8adfa3SLeonel Gonzalez      *  @return None
1299c8adfa3SLeonel Gonzalez      */
1309c8adfa3SLeonel Gonzalez     void erase(std::string entryId);
1319c8adfa3SLeonel Gonzalez 
132234a07e8SMichael Tritz     /**
133234a07e8SMichael Tritz      * @brief Erases any non-active pnor versions.
134234a07e8SMichael Tritz      */
135234a07e8SMichael Tritz     void deleteAll();
136234a07e8SMichael Tritz 
137fedbf3d3SGunnar Mills     /** @brief Brings the total number of active PNOR versions to
138fedbf3d3SGunnar Mills      *         ACTIVE_PNOR_MAX_ALLOWED -1. This function is intended to be
139fedbf3d3SGunnar Mills      *         run before activating a new PNOR version. If this function
140fedbf3d3SGunnar Mills      *         needs to delete any PNOR version(s) it will delete the
141fedbf3d3SGunnar Mills      *         version(s) with the highest priority, skipping the
142fedbf3d3SGunnar Mills      *         functional PNOR version.
1432cbfa035SSaqib Khan      */
1442cbfa035SSaqib Khan     void freeSpace();
1452cbfa035SSaqib Khan 
1462badd7a6SGunnar Mills     /** @brief Determine the software version id
1472badd7a6SGunnar Mills      *         from the symlink target (e.g. /media/ro-2a1022fe).
1482badd7a6SGunnar Mills      *
1492badd7a6SGunnar Mills      * @param[in] symlinkPath - The path of the symlink.
1502badd7a6SGunnar Mills      * @param[out] id - The version id as a string.
1512badd7a6SGunnar Mills      */
1522badd7a6SGunnar Mills     static std::string determineId(const std::string& symlinkPath);
1532badd7a6SGunnar Mills 
1549741cd19SGunnar Mills     /** @brief Creates an active association to the
1559741cd19SGunnar Mills      *  newly active software image
1569741cd19SGunnar Mills      *
1579741cd19SGunnar Mills      * @param[in]  path - The path to create the association to.
1589741cd19SGunnar Mills      */
15961010b23SGunnar Mills     void createActiveAssociation(const std::string& path);
1609741cd19SGunnar Mills 
161833e4f37SGunnar Mills     /** @brief Updates the functional association to the
162833e4f37SGunnar Mills      *  new "running" PNOR image
163833e4f37SGunnar Mills      *
164833e4f37SGunnar Mills      * @param[in]  path - The path to update the association to.
165833e4f37SGunnar Mills      */
166833e4f37SGunnar Mills     void updateFunctionalAssociation(const std::string& path);
167833e4f37SGunnar Mills 
168*b5237174SAdriana Kobylak     /** @brief Removes the associations from the provided software image path
1699741cd19SGunnar Mills      *
1709741cd19SGunnar Mills      * @param[in]  path - The path to remove the association from.
1719741cd19SGunnar Mills      */
172*b5237174SAdriana Kobylak     void removeAssociation(const std::string& path);
1739741cd19SGunnar Mills 
174b541f1b6SMichael Tritz     /** @brief Persistent GardReset dbus object */
175b541f1b6SMichael Tritz     std::unique_ptr<GardReset> gardReset;
176b541f1b6SMichael Tritz 
1775b75651bSMichael Tritz     /** @brief Check whether the provided image id is the functional one
1785b75651bSMichael Tritz      *
1795b75651bSMichael Tritz      * @param[in] - versionId - The id of the image to check.
1805b75651bSMichael Tritz      *
1815b75651bSMichael Tritz      * @return - Returns true if this version is currently functional.
1825b75651bSMichael Tritz      */
1835b75651bSMichael Tritz     static bool isVersionFunctional(const std::string& versionId);
1845b75651bSMichael Tritz 
1857cb480edSAdriana Kobylak     /** @brief Persistent ObjectEnable D-Bus object */
1867cb480edSAdriana Kobylak     std::unique_ptr<ObjectEnable> volatileEnable;
1877cb480edSAdriana Kobylak 
1882d8fa225SAdriana Kobylak   private:
1892d8fa225SAdriana Kobylak     /** @brief Callback function for Software.Version match.
190139cf1a1SGunnar Mills      *  @details Creates an Activation D-Bus object.
1912d8fa225SAdriana Kobylak      *
1922d8fa225SAdriana Kobylak      * @param[in]  msg       - Data associated with subscribed signal
1932d8fa225SAdriana Kobylak      */
1943accb322SPatrick Williams     void createActivation(sdbusplus::message::message& msg);
1952d8fa225SAdriana Kobylak 
1967254f0eaSSaqib Khan     /**
197139cf1a1SGunnar Mills      * @brief Validates the presence of SquashFS image in the image dir.
198a8ade7e4SSaqib Khan      *
199139cf1a1SGunnar Mills      * @param[in]  filePath - The path to the SquashFS image.
200a8ade7e4SSaqib Khan      * @param[out] result    - 0 --> if validation was successful
201a8ade7e4SSaqib Khan      *                       - -1--> Otherwise
202a8ade7e4SSaqib Khan      */
2035ba6b10eSAdriana Kobylak     static int validateSquashFSImage(const std::string& filePath);
2047254f0eaSSaqib Khan 
205139cf1a1SGunnar Mills     /** @brief Persistent sdbusplus D-Bus bus connection. */
206d6a549eaSAdriana Kobylak     sdbusplus::bus::bus& bus;
2072d8fa225SAdriana Kobylak 
208139cf1a1SGunnar Mills     /** @brief Persistent map of Activation D-Bus objects and their
2092d8fa225SAdriana Kobylak      * version id */
210268616bfSAdriana Kobylak     std::map<std::string, std::unique_ptr<Activation>> activations;
2112d8fa225SAdriana Kobylak 
212139cf1a1SGunnar Mills     /** @brief Persistent map of Version D-Bus objects and their
213ce148700SSaqib Khan      * version id */
214ce148700SSaqib Khan     std::map<std::string, std::unique_ptr<Version>> versions;
215ce148700SSaqib Khan 
2162d8fa225SAdriana Kobylak     /** @brief sdbusplus signal match for Software.Version */
2173accb322SPatrick Williams     sdbusplus::bus::match_t versionMatch;
218dd961b6cSMichael Tritz 
2199741cd19SGunnar Mills     /** @brief This entry's associations */
2209741cd19SGunnar Mills     AssociationList assocs = {};
2219741cd19SGunnar Mills 
2229c8adfa3SLeonel Gonzalez     /** @brief Clears read only PNOR partition for
223139cf1a1SGunnar Mills      *  given Activation D-Bus object
2249c8adfa3SLeonel Gonzalez      *
2259c8adfa3SLeonel Gonzalez      * @param[in]  versionId - The id of the ro partition to remove.
2269c8adfa3SLeonel Gonzalez      */
2279c8adfa3SLeonel Gonzalez     void removeReadOnlyPartition(std::string versionId);
2289c8adfa3SLeonel Gonzalez 
2299c8adfa3SLeonel Gonzalez     /** @brief Clears read write PNOR partition for
230139cf1a1SGunnar Mills      *  given Activation D-Bus object
2319c8adfa3SLeonel Gonzalez      *
2329c8adfa3SLeonel Gonzalez      *  @param[in]  versionId - The id of the rw partition to remove.
2339c8adfa3SLeonel Gonzalez      */
2349c8adfa3SLeonel Gonzalez     void removeReadWritePartition(std::string versionId);
2359c8adfa3SLeonel Gonzalez 
2369c8adfa3SLeonel Gonzalez     /** @brief Clears preserved PNOR partition */
2379c8adfa3SLeonel Gonzalez     void removePreservedPartition();
2389c8adfa3SLeonel Gonzalez 
239dd961b6cSMichael Tritz     /** @brief Host factory reset - clears PNOR partitions for each
240139cf1a1SGunnar Mills      * Activation D-Bus object */
241dd961b6cSMichael Tritz     void reset() override;
24213fc66adSEddie James 
24313fc66adSEddie James     /** @brief Check whether the host is running
24413fc66adSEddie James      *
24513fc66adSEddie James      * @return - Returns true if the Chassis is powered on.
24613fc66adSEddie James      */
24713fc66adSEddie James     bool isChassisOn();
2482d8fa225SAdriana Kobylak };
2492d8fa225SAdriana Kobylak 
250befe5ce4SAdriana Kobylak } // namespace updater
2512d8fa225SAdriana Kobylak } // namespace software
2522d8fa225SAdriana Kobylak } // namespace openpower
253