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