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,
206                                                               biosAttrIndex);
207                         }));
208 
209                 biosAttrMatch.push_back(
210                     std::make_unique<sdbusplus::bus::match_t>(
211                         pldm::utils::DBusHandler::getBus(),
212                         interfacesAdded() + argNpath(0, dBusMap->objectPath),
213                         [this, biosAttrIndex, interface = dBusMap->interface](
214                             sdbusplus::message_t& msg) {
215                             sdbusplus::message::object_path path;
216                             DbusIfacesAdded interfaces;
217 
218                             msg.read(path, interfaces);
219                             auto ifaceIt = interfaces.find(interface);
220                             if (ifaceIt != interfaces.end())
221                             {
222                                 processBiosAttrChangeNotification(
223                                     ifaceIt->second, biosAttrIndex);
224                             }
225                         }));
226             }
227         }
228         catch (const std::exception& e)
229         {
230             error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
231                   e.what());
232         }
233     }
234 
235     /** Construct attributes and persist them */
236     void constructAttributes();
237 
238     using ParseHandler = std::function<void(const Json& entry)>;
239 
240     /** @brief Helper function to parse json
241      *  @param[in] filePath - Path of json file
242      *  @param[in] handler - Handler to process each entry in the json
243      */
244     void load(const fs::path& filePath, ParseHandler handler);
245 
246     /** @brief Build String Table and persist it
247      *  @return The built string table, std::nullopt if it fails.
248      */
249     std::optional<Table> buildAndStoreStringTable();
250 
251     /** @brief Build attribute table and attribute value table and persist them
252      *         Read the BaseBIOSTable from the bios-settings-manager and update
253      *         attribute table and attribute value table.
254      *
255      *  @param[in] stringTable - The string Table
256      */
257     void buildAndStoreAttrTables(const Table& stringTable);
258 
259     /** @brief Persist the table
260      *  @param[in] path - Path to persist the table
261      *  @param[in] table - The table
262      */
263     void storeTable(const fs::path& path, const Table& table);
264 
265     /** @brief Load bios table to ram
266      *  @param[in] path - Path of the table
267      *  @return The table, std::nullopt if loading fails
268      */
269     std::optional<Table> loadTable(const fs::path& path);
270 
271     /** @brief Method to decode the attribute name from the string handle
272      *
273      *  @param[in] stringEntry - string entry from string table
274      *  @return the decoded string
275      */
276     std::string decodeStringFromStringEntry(
277         const pldm_bios_string_table_entry* stringEntry);
278 
279     /** @brief Method to print the string Handle by passing the attribute Handle
280      *         of the bios attribute that got updated
281      *
282      *  @param[in] handle - the Attribute handle of the bios attribute
283      *  @param[in] index - index to the possible value handles
284      *  @param[in] attrTable - the attribute table
285      *  @param[in] stringTable - the string table
286      *  @return string handle from the string table and decoded string to the
287      * name handle
288      */
289     std::string displayStringHandle(uint16_t handle, uint8_t index,
290                                     const std::optional<Table>& attrTable,
291                                     const std::optional<Table>& stringTable);
292 
293     /** @brief Method to trace the bios attribute which got changed
294      *
295      *  @param[in] attrValueEntry - The attribute value entry to update
296      *  @param[in] attrEntry - The attribute table entry
297      *  @param[in] isBMC - indicates if the attribute is set by BMC
298      */
299     void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
300                          const pldm_bios_attr_table_entry* attrEntry,
301                          bool isBMC);
302 
303     /** @brief Check the attribute value to update
304      *  @param[in] attrValueEntry - The attribute value entry to update
305      *  @param[in] attrEntry - The attribute table entry
306      *  @param[in] stringTable - The string  table
307      *  @return pldm_completion_codes
308      */
309     int checkAttrValueToUpdate(
310         const pldm_bios_attr_val_table_entry* attrValueEntry,
311         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
312 
313     /** @brief Check the attribute table
314      *  @param[in] table - The table
315      *  @return pldm_completion_codes
316      */
317     int checkAttributeTable(const Table& table);
318 
319     /** @brief Check the attribute value table
320      *  @param[in] table - The table
321      *  @return pldm_completion_codes
322      */
323     int checkAttributeValueTable(const Table& table);
324 
325     /** @brief Update the BaseBIOSTable property of the D-Bus interface
326      */
327     void updateBaseBIOSTableProperty();
328 
329     /** @brief Listen the PendingAttributes property of the D-Bus interface and
330      *         update BaseBIOSTable
331      */
332     void listenPendingAttributes();
333 
334     /** @brief Find attribute handle from bios attribute table
335      *  @param[in] attrName - attribute name
336      *  @return attribute handle
337      */
338     uint16_t findAttrHandle(const std::string& attrName);
339 
340     /** @brief Listen the PendingAttributes property of the D-Bus interface
341      * and update BaseBIOSTable
342      *  @param[in] msg - Data associated with subscribed signal
343      */
344     void constructPendingAttribute(const PendingAttributes& pendingAttributes);
345 };
346 
347 } // namespace bios
348 } // namespace responder
349 } // namespace pldm
350