1 #pragma once
2 
3 #include <sdbusplus/server.hpp>
4 #include "activation.hpp"
5 #include "version.hpp"
6 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
7 #include <xyz/openbmc_project/Control/FieldMode/server.hpp>
8 #include "org/openbmc/Associations/server.hpp"
9 
10 namespace phosphor
11 {
12 namespace software
13 {
14 namespace updater
15 {
16 
17 using ItemUpdaterInherit = sdbusplus::server::object::object<
18     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
19     sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
20     sdbusplus::org::openbmc::server::Associations>;
21 
22 namespace MatchRules = sdbusplus::bus::match::rules;
23 
24 using AssociationList =
25         std::vector<std::tuple<std::string, std::string, std::string>>;
26 
27 /** @class ItemUpdater
28  *  @brief Manages the activation of the BMC version items.
29  */
30 class ItemUpdater : public ItemUpdaterInherit
31 {
32     public:
33         /*
34          * @brief Types of Activation status for image validation.
35          */
36         enum class ActivationStatus
37         {
38             ready,
39             invalid,
40             active
41         };
42 
43         /** @brief Constructs ItemUpdater
44          *
45          * @param[in] bus    - The Dbus bus object
46          */
47         ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
48                     ItemUpdaterInherit(bus, path.c_str(), false),
49                     bus(bus),
50                     versionMatch(
51                             bus,
52                             MatchRules::interfacesAdded() +
53                             MatchRules::path("/xyz/openbmc_project/software"),
54                             std::bind(
55                                     std::mem_fn(&ItemUpdater::createActivation),
56                                     this,
57                                     std::placeholders::_1))
58         {
59             setBMCInventoryPath();
60             processBMCImage();
61             restoreFieldModeStatus();
62             emit_object_added();
63         };
64 
65     /** @brief Sets the given priority free by incrementing
66      *  any existing priority with the same value by 1
67      *
68      *  @param[in] value - The priority that needs to be set free.
69      *
70      *  @return None
71      */
72     void freePriority(uint8_t value);
73 
74     /**
75      * @brief Create and populate the active BMC Version.
76      */
77     void processBMCImage();
78 
79     /**
80      * @brief Erase specified entry d-bus object
81      *        if Action property is not set to Active
82      *
83      * @param[in] entryId - unique identifier of the entry
84      */
85     void erase(std::string entryId);
86 
87 
88     /** @brief Creates an active association to the
89      *  newly active software image
90      *
91      * @param[in]  path - The path to create the association to.
92      */
93     void createActiveAssociation(std::string path);
94 
95     /** @brief Removes an active association to the software image
96      *
97      * @param[in]  path - The path to remove the association from.
98      */
99     void removeActiveAssociation(std::string path);
100 
101     private:
102         /** @brief Callback function for Software.Version match.
103          *  @details Creates an Activation dbus object.
104          *
105          * @param[in]  msg       - Data associated with subscribed signal
106          */
107         void createActivation(sdbusplus::message::message& msg);
108 
109         /**
110          * @brief Validates the presence of SquashFS iamge in the image dir.
111          *
112          * @param[in]  filePath  - The path to the image dir.
113          * @param[out] result    - ActivationStatus Enum.
114          *                         ready if validation was successful.
115          *                         invalid if validation fail.
116          *                         active if image is the current version.
117          *
118          */
119         ActivationStatus validateSquashFSImage(const std::string& filePath);
120 
121         /** @brief BMC factory reset - marks the read-write partition for
122           * recreation upon reboot. */
123         void reset() override;
124 
125         /**
126          * @brief Enables field mode, if value=true.
127          *
128          * @param[in]  value  - If true, enables field mode.
129          * @param[out] result - Returns the current state of field mode.
130          *
131          */
132         bool fieldModeEnabled(bool value) override;
133 
134         /** @brief Sets the BMC inventory item path under
135          *  /xyz/openbmc_project/inventory/system/chassis/. */
136         void setBMCInventoryPath();
137 
138         /** @brief The path to the BMC inventory item. */
139         std::string bmcInventoryPath;
140 
141         /** @brief Restores field mode status on reboot. */
142         void restoreFieldModeStatus();
143 
144         /** @brief Persistent sdbusplus DBus bus connection. */
145         sdbusplus::bus::bus& bus;
146 
147         /** @brief Persistent map of Activation dbus objects and their
148           * version id */
149         std::map<std::string, std::unique_ptr<Activation>> activations;
150 
151         /** @brief Persistent map of Version dbus objects and their
152           * version id */
153         std::map<std::string, std::unique_ptr<phosphor::software::
154             manager::Version>> versions;
155 
156         /** @brief sdbusplus signal match for Software.Version */
157         sdbusplus::bus::match_t versionMatch;
158 
159         /** @brief This entry's associations */
160         AssociationList assocs = {};
161 
162         /** @brief Clears read only partition for
163           * given Activation dbus object.
164           *
165           * @param[in]  versionId - The version id.
166           */
167         void removeReadOnlyPartition(std::string versionId);
168 };
169 
170 
171 
172 } // namespace updater
173 } // namespace software
174 } // namespace phosphor
175