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