1 #pragma once
2 
3 #include "bios_table.h"
4 
5 #include "bios_attribute.hpp"
6 #include "bios_table.hpp"
7 #include "pldmd/dbus_impl_requester.hpp"
8 
9 #include <nlohmann/json.hpp>
10 
11 #include <functional>
12 #include <iostream>
13 #include <memory>
14 #include <optional>
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 namespace pldm
20 {
21 namespace responder
22 {
23 namespace bios
24 {
25 
26 enum class BoundType
27 {
28     LowerBound,
29     UpperBound,
30     ScalarIncrement,
31     MinStringLength,
32     MaxStringLength,
33     OneOf
34 };
35 
36 using AttributeName = std::string;
37 using AttributeType = std::string;
38 using ReadonlyStatus = bool;
39 using DisplayName = std::string;
40 using Description = std::string;
41 using MenuPath = std::string;
42 using CurrentValue = std::variant<int64_t, std::string>;
43 using DefaultValue = std::variant<int64_t, std::string>;
44 using OptionString = std::string;
45 using OptionValue = std::variant<int64_t, std::string>;
46 using Option = std::vector<std::tuple<OptionString, OptionValue>>;
47 using BIOSTableObj =
48     std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
49                MenuPath, CurrentValue, DefaultValue, Option>;
50 using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
51 
52 using PendingObj = std::tuple<AttributeType, CurrentValue>;
53 using PendingAttributes = std::map<AttributeName, PendingObj>;
54 
55 /** @class BIOSConfig
56  *  @brief Manager BIOS Attributes
57  */
58 class BIOSConfig
59 {
60   public:
61     BIOSConfig() = delete;
62     BIOSConfig(const BIOSConfig&) = delete;
63     BIOSConfig(BIOSConfig&&) = delete;
64     BIOSConfig& operator=(const BIOSConfig&) = delete;
65     BIOSConfig& operator=(BIOSConfig&&) = delete;
66     ~BIOSConfig() = default;
67 
68     /** @brief Construct BIOSConfig
69      *  @param[in] jsonDir - The directory where json file exists
70      *  @param[in] tableDir - The directory where the persistent table is placed
71      *  @param[in] dbusHandler - Dbus Handler
72      *  @param[in] fd - socket descriptor to communicate to host
73      *  @param[in] eid - MCTP EID of host firmware
74      *  @param[in] requester - pointer to Requester object
75      */
76     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
77                         DBusHandler* const dbusHandler, int fd, uint8_t eid,
78                         dbus_api::Requester* requester);
79 
80     /** @brief Set attribute value on dbus and attribute value table
81      *  @param[in] entry - attribute value entry
82      *  @param[in] size - size of the attribute value entry
83      *  @param[in] updateDBus          - update Attr value D-Bus property
84      *                                   if this is set to true
85      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
86      *                                   if this is set to true
87      *  @return pldm_completion_codes
88      */
89     int setAttrValue(const void* entry, size_t size, bool updateDBus = true,
90                      bool updateBaseBIOSTable = true);
91 
92     /** @brief Remove the persistent tables */
93     void removeTables();
94 
95     /** @brief Build bios tables(string,attribute,attribute value table)*/
96     void buildTables();
97 
98     /** @brief Get BIOS table of specified type
99      *  @param[in] tableType - The table type
100      *  @return The bios table, std::nullopt if the table is unaviliable
101      */
102     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
103 
104     /** @brief set BIOS table
105      *  @param[in] tableType - Indicates what table is being transferred
106      *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
107      *              BIOSAttributeValueTable=0x2}
108      *  @param[in] table - table data
109      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
110      *                                   if this is set to true
111      *  @return pldm_completion_codes
112      */
113     int setBIOSTable(uint8_t tableType, const Table& table,
114                      bool updateBaseBIOSTable = true);
115 
116   private:
117     /** @enum Index into the fields in the BaseBIOSTable
118      */
119     enum class Index : uint8_t
120     {
121         attributeType = 0,
122         readOnly,
123         displayName,
124         description,
125         menuPath,
126         currentValue,
127         defaultValue,
128         options,
129     };
130 
131     const fs::path jsonDir;
132     const fs::path tableDir;
133     DBusHandler* const dbusHandler;
134     BaseBIOSTable baseBIOSTableMaps;
135 
136     /** @brief socket descriptor to communicate to host */
137     int fd;
138 
139     /** @brief MCTP EID of host firmware */
140     uint8_t eid;
141 
142     /** @brief pointer to Requester object, primarily used to access API to
143      *  obtain PLDM instance id.
144      */
145     dbus_api::Requester* requester;
146 
147     // vector persists all attributes
148     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
149     BIOSAttributes biosAttributes;
150 
151     using propName = std::string;
152     using DbusChObjProperties = std::map<propName, PropertyValue>;
153 
154     // vector to catch the D-Bus property change signals for BIOS attributes
155     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
156 
157     /** @brief Method to update a BIOS attribute when the corresponding Dbus
158      *  property is changed
159      *  @param[in] chProperties - list of properties which have changed
160      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
161      * biosAttributes
162      *  @return - none
163      */
164     void processBiosAttrChangeNotification(
165         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
166 
167     /** @brief Construct an attribute and persist it
168      *  @tparam T - attribute type
169      *  @param[in] entry - json entry
170      */
171     template <typename T>
172     void constructAttribute(const Json& entry)
173     {
174         try
175         {
176             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
177             auto biosAttrIndex = biosAttributes.size() - 1;
178             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
179 
180             if (dBusMap.has_value())
181             {
182                 using namespace sdbusplus::bus::match::rules;
183                 biosAttrMatch.push_back(
184                     std::make_unique<sdbusplus::bus::match::match>(
185                         pldm::utils::DBusHandler::getBus(),
186                         propertiesChanged(dBusMap->objectPath,
187                                           dBusMap->interface),
188                         [this,
189                          biosAttrIndex](sdbusplus::message::message& msg) {
190                             DbusChObjProperties props;
191                             std::string iface;
192                             msg.read(iface, props);
193                             processBiosAttrChangeNotification(props,
194                                                               biosAttrIndex);
195                         }));
196             }
197         }
198         catch (const std::exception& e)
199         {
200             std::cerr << "Constructs Attribute Error, " << e.what()
201                       << std::endl;
202         }
203     }
204 
205     /** Construct attributes and persist them */
206     void constructAttributes();
207 
208     using ParseHandler = std::function<void(const Json& entry)>;
209 
210     /** @brief Helper function to parse json
211      *  @param[in] filePath - Path of json file
212      *  @param[in] handler - Handler to process each entry in the json
213      */
214     void load(const fs::path& filePath, ParseHandler handler);
215 
216     /** @brief Build String Table and persist it
217      *  @return The built string table, std::nullopt if it fails.
218      */
219     std::optional<Table> buildAndStoreStringTable();
220 
221     /** @brief Build attribute table and attribute value table and persist them
222      *         Read the BaseBIOSTable from the bios-settings-manager and update
223      *         attribute table and attribute value table.
224      *
225      *  @param[in] stringTable - The string Table
226      */
227     void buildAndStoreAttrTables(const Table& stringTable);
228 
229     /** @brief Persist the table
230      *  @param[in] path - Path to persist the table
231      *  @param[in] table - The table
232      */
233     void storeTable(const fs::path& path, const Table& table);
234 
235     /** @brief Load bios table to ram
236      *  @param[in] path - Path of the table
237      *  @return The table, std::nullopt if loading fails
238      */
239     std::optional<Table> loadTable(const fs::path& path);
240 
241     /** @brief Check the attribute value to update
242      *  @param[in] attrValueEntry - The attribute value entry to update
243      *  @param[in] attrEntry - The attribute table entry
244      *  @param[in] stringTable - The string  table
245      *  @return pldm_completion_codes
246      */
247     int checkAttrValueToUpdate(
248         const pldm_bios_attr_val_table_entry* attrValueEntry,
249         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
250 
251     /** @brief Check the attribute table
252      *  @param[in] table - The table
253      *  @return pldm_completion_codes
254      */
255     int checkAttributeTable(const Table& table);
256 
257     /** @brief Check the attribute value table
258      *  @param[in] table - The table
259      *  @return pldm_completion_codes
260      */
261     int checkAttributeValueTable(const Table& table);
262 
263     /** @brief Update the BaseBIOSTable property of the D-Bus interface
264      */
265     void updateBaseBIOSTableProperty();
266 
267     /** @brief Listen the PendingAttributes property of the D-Bus interface and
268      *         update BaseBIOSTable
269      */
270     void listenPendingAttributes();
271 
272     /** @brief Find attribute handle from bios attribute table
273      *  @param[in] attrName - attribute name
274      *  @return attribute handle
275      */
276     uint16_t findAttrHandle(const std::string& attrName);
277 
278     /** @brief Listen the PendingAttributes property of the D-Bus interface
279      * and update BaseBIOSTable
280      *  @param[in] msg - Data associated with subscribed signal
281      */
282     void constructPendingAttribute(const PendingAttributes& pendingAttributes);
283 };
284 
285 } // namespace bios
286 } // namespace responder
287 } // namespace pldm
288