xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision 6d6d1e8d1dfc1d9ebd5aea72566993ce96da2794)
1d965934fSJohn Wang #pragma once
2d965934fSJohn Wang 
36492f524SGeorge Liu #include "bios_table.h"
46492f524SGeorge Liu 
5d965934fSJohn Wang #include "bios_attribute.hpp"
6d965934fSJohn Wang #include "bios_table.hpp"
77f839f9dSTom Joseph #include "pldmd/dbus_impl_requester.hpp"
8d965934fSJohn Wang 
96492f524SGeorge Liu #include <nlohmann/json.hpp>
106492f524SGeorge Liu 
11d965934fSJohn Wang #include <functional>
12d965934fSJohn Wang #include <iostream>
13d965934fSJohn Wang #include <memory>
14d965934fSJohn Wang #include <optional>
15d965934fSJohn Wang #include <set>
16d965934fSJohn Wang #include <string>
17d965934fSJohn Wang #include <vector>
18d965934fSJohn Wang 
19d965934fSJohn Wang namespace pldm
20d965934fSJohn Wang {
21d965934fSJohn Wang namespace responder
22d965934fSJohn Wang {
23d965934fSJohn Wang namespace bios
24d965934fSJohn Wang {
25d965934fSJohn Wang 
261b180d8aSGeorge Liu enum class BoundType
271b180d8aSGeorge Liu {
281b180d8aSGeorge Liu     LowerBound,
291b180d8aSGeorge Liu     UpperBound,
301b180d8aSGeorge Liu     ScalarIncrement,
311b180d8aSGeorge Liu     MinStringLength,
321b180d8aSGeorge Liu     MaxStringLength,
331b180d8aSGeorge Liu     OneOf
341b180d8aSGeorge Liu };
351b180d8aSGeorge Liu 
361b180d8aSGeorge Liu using AttributeName = std::string;
371b180d8aSGeorge Liu using AttributeType = std::string;
381b180d8aSGeorge Liu using ReadonlyStatus = bool;
391b180d8aSGeorge Liu using DisplayName = std::string;
401b180d8aSGeorge Liu using Description = std::string;
411b180d8aSGeorge Liu using MenuPath = std::string;
421b180d8aSGeorge Liu using CurrentValue = std::variant<int64_t, std::string>;
431b180d8aSGeorge Liu using DefaultValue = std::variant<int64_t, std::string>;
441b180d8aSGeorge Liu using OptionString = std::string;
451b180d8aSGeorge Liu using OptionValue = std::variant<int64_t, std::string>;
461b180d8aSGeorge Liu using Option = std::vector<std::tuple<OptionString, OptionValue>>;
471b180d8aSGeorge Liu using BIOSTableObj =
481b180d8aSGeorge Liu     std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
491b180d8aSGeorge Liu                MenuPath, CurrentValue, DefaultValue, Option>;
501b180d8aSGeorge Liu using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
511b180d8aSGeorge Liu 
521244acfdSGeorge Liu using PendingObj = std::tuple<AttributeType, CurrentValue>;
531244acfdSGeorge Liu using PendingAttributes = std::map<AttributeName, PendingObj>;
541244acfdSGeorge Liu 
55d965934fSJohn Wang /** @class BIOSConfig
56d965934fSJohn Wang  *  @brief Manager BIOS Attributes
57d965934fSJohn Wang  */
58d965934fSJohn Wang class BIOSConfig
59d965934fSJohn Wang {
60d965934fSJohn Wang   public:
61d965934fSJohn Wang     BIOSConfig() = delete;
62d965934fSJohn Wang     BIOSConfig(const BIOSConfig&) = delete;
63d965934fSJohn Wang     BIOSConfig(BIOSConfig&&) = delete;
64d965934fSJohn Wang     BIOSConfig& operator=(const BIOSConfig&) = delete;
65d965934fSJohn Wang     BIOSConfig& operator=(BIOSConfig&&) = delete;
66d965934fSJohn Wang     ~BIOSConfig() = default;
67d965934fSJohn Wang 
68d965934fSJohn Wang     /** @brief Construct BIOSConfig
69d965934fSJohn Wang      *  @param[in] jsonDir - The directory where json file exists
70d965934fSJohn Wang      *  @param[in] tableDir - The directory where the persistent table is placed
71d965934fSJohn Wang      *  @param[in] dbusHandler - Dbus Handler
727f839f9dSTom Joseph      *  @param[in] fd - socket descriptor to communicate to host
737f839f9dSTom Joseph      *  @param[in] eid - MCTP EID of host firmware
747f839f9dSTom Joseph      *  @param[in] requester - pointer to Requester object
75d965934fSJohn Wang      */
76d965934fSJohn Wang     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
777f839f9dSTom Joseph                         DBusHandler* const dbusHandler, int fd, uint8_t eid,
787f839f9dSTom Joseph                         dbus_api::Requester* requester);
79d965934fSJohn Wang 
80d965934fSJohn Wang     /** @brief Set attribute value on dbus and attribute value table
81d965934fSJohn Wang      *  @param[in] entry - attribute value entry
82d965934fSJohn Wang      *  @param[in] size - size of the attribute value entry
83*6d6d1e8dSGeorge Liu      *  @param[in] updateDBus          - update Attr value D-Bus property
84*6d6d1e8dSGeorge Liu      *                                   if this is set to true
857f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
867f839f9dSTom Joseph      *                                   if this is set to true
87d965934fSJohn Wang      *  @return pldm_completion_codes
88d965934fSJohn Wang      */
89*6d6d1e8dSGeorge Liu     int setAttrValue(const void* entry, size_t size, bool updateDBus = true,
907f839f9dSTom Joseph                      bool updateBaseBIOSTable = true);
91d965934fSJohn Wang 
92d965934fSJohn Wang     /** @brief Remove the persistent tables */
93d965934fSJohn Wang     void removeTables();
94d965934fSJohn Wang 
95d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
96d965934fSJohn Wang     void buildTables();
97d965934fSJohn Wang 
98d965934fSJohn Wang     /** @brief Get BIOS table of specified type
99d965934fSJohn Wang      *  @param[in] tableType - The table type
100d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
101d965934fSJohn Wang      */
102d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
103d965934fSJohn Wang 
1041b180d8aSGeorge Liu     /** @brief set BIOS table
1051b180d8aSGeorge Liu      *  @param[in] tableType - Indicates what table is being transferred
1061b180d8aSGeorge Liu      *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
1071b180d8aSGeorge Liu      *              BIOSAttributeValueTable=0x2}
1081b180d8aSGeorge Liu      *  @param[in] table - table data
1097f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
1107f839f9dSTom Joseph      *                                   if this is set to true
1111b180d8aSGeorge Liu      *  @return pldm_completion_codes
1121b180d8aSGeorge Liu      */
1137f839f9dSTom Joseph     int setBIOSTable(uint8_t tableType, const Table& table,
1147f839f9dSTom Joseph                      bool updateBaseBIOSTable = true);
1151b180d8aSGeorge Liu 
116d965934fSJohn Wang   private:
117ca7b2524STom Joseph     /** @enum Index into the fields in the BaseBIOSTable
118ca7b2524STom Joseph      */
119ca7b2524STom Joseph     enum class Index : uint8_t
120ca7b2524STom Joseph     {
121ca7b2524STom Joseph         attributeType = 0,
122ca7b2524STom Joseph         readOnly,
123ca7b2524STom Joseph         displayName,
124ca7b2524STom Joseph         description,
125ca7b2524STom Joseph         menuPath,
126ca7b2524STom Joseph         currentValue,
127ca7b2524STom Joseph         defaultValue,
128ca7b2524STom Joseph         options,
129ca7b2524STom Joseph     };
130ca7b2524STom Joseph 
131d965934fSJohn Wang     const fs::path jsonDir;
132d965934fSJohn Wang     const fs::path tableDir;
133d965934fSJohn Wang     DBusHandler* const dbusHandler;
1341b180d8aSGeorge Liu     BaseBIOSTable baseBIOSTableMaps;
135d965934fSJohn Wang 
1367f839f9dSTom Joseph     /** @brief socket descriptor to communicate to host */
1377f839f9dSTom Joseph     int fd;
1387f839f9dSTom Joseph 
1397f839f9dSTom Joseph     /** @brief MCTP EID of host firmware */
1407f839f9dSTom Joseph     uint8_t eid;
1417f839f9dSTom Joseph 
1427f839f9dSTom Joseph     /** @brief pointer to Requester object, primarily used to access API to
1437f839f9dSTom Joseph      *  obtain PLDM instance id.
1447f839f9dSTom Joseph      */
1457f839f9dSTom Joseph     dbus_api::Requester* requester;
1467f839f9dSTom Joseph 
147d965934fSJohn Wang     // vector persists all attributes
148d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
149d965934fSJohn Wang     BIOSAttributes biosAttributes;
150d965934fSJohn Wang 
15146ece063SSampa Misra     using propName = std::string;
15246ece063SSampa Misra     using DbusChObjProperties = std::map<propName, PropertyValue>;
15346ece063SSampa Misra 
15446ece063SSampa Misra     // vector to catch the D-Bus property change signals for BIOS attributes
15546ece063SSampa Misra     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
15646ece063SSampa Misra 
15746ece063SSampa Misra     /** @brief Method to update a BIOS attribute when the corresponding Dbus
15846ece063SSampa Misra      *  property is changed
15946ece063SSampa Misra      *  @param[in] chProperties - list of properties which have changed
16046ece063SSampa Misra      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
16146ece063SSampa Misra      * biosAttributes
16246ece063SSampa Misra      *  @return - none
16346ece063SSampa Misra      */
16446ece063SSampa Misra     void processBiosAttrChangeNotification(
16546ece063SSampa Misra         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
16646ece063SSampa Misra 
167d965934fSJohn Wang     /** @brief Construct an attribute and persist it
168d965934fSJohn Wang      *  @tparam T - attribute type
169d965934fSJohn Wang      *  @param[in] entry - json entry
170d965934fSJohn Wang      */
171d965934fSJohn Wang     template <typename T>
172d965934fSJohn Wang     void constructAttribute(const Json& entry)
173d965934fSJohn Wang     {
174d965934fSJohn Wang         try
175d965934fSJohn Wang         {
176d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
17746ece063SSampa Misra             auto biosAttrIndex = biosAttributes.size() - 1;
17846ece063SSampa Misra             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
17946ece063SSampa Misra 
18046ece063SSampa Misra             if (dBusMap.has_value())
18146ece063SSampa Misra             {
18246ece063SSampa Misra                 using namespace sdbusplus::bus::match::rules;
18346ece063SSampa Misra                 biosAttrMatch.push_back(
18446ece063SSampa Misra                     std::make_unique<sdbusplus::bus::match::match>(
18546ece063SSampa Misra                         pldm::utils::DBusHandler::getBus(),
18646ece063SSampa Misra                         propertiesChanged(dBusMap->objectPath,
18746ece063SSampa Misra                                           dBusMap->interface),
18846ece063SSampa Misra                         [this,
18946ece063SSampa Misra                          biosAttrIndex](sdbusplus::message::message& msg) {
19046ece063SSampa Misra                             DbusChObjProperties props;
19146ece063SSampa Misra                             std::string iface;
19246ece063SSampa Misra                             msg.read(iface, props);
19346ece063SSampa Misra                             processBiosAttrChangeNotification(props,
19446ece063SSampa Misra                                                               biosAttrIndex);
19546ece063SSampa Misra                         }));
19646ece063SSampa Misra             }
197d965934fSJohn Wang         }
198d965934fSJohn Wang         catch (const std::exception& e)
199d965934fSJohn Wang         {
200d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
201d965934fSJohn Wang                       << std::endl;
202d965934fSJohn Wang         }
203d965934fSJohn Wang     }
204d965934fSJohn Wang 
205d965934fSJohn Wang     /** Construct attributes and persist them */
206d965934fSJohn Wang     void constructAttributes();
207d965934fSJohn Wang 
208d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
209d965934fSJohn Wang 
210d965934fSJohn Wang     /** @brief Helper function to parse json
211d965934fSJohn Wang      *  @param[in] filePath - Path of json file
212d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
213d965934fSJohn Wang      */
214d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
215d965934fSJohn Wang 
216d965934fSJohn Wang     /** @brief Build String Table and persist it
217d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
218d965934fSJohn Wang      */
219d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
220d965934fSJohn Wang 
221ca7b2524STom Joseph     /** @brief Build attribute table and attribute value table and persist them
222ca7b2524STom Joseph      *         Read the BaseBIOSTable from the bios-settings-manager and update
223ca7b2524STom Joseph      *         attribute table and attribute value table.
224ca7b2524STom Joseph      *
225d965934fSJohn Wang      *  @param[in] stringTable - The string Table
226d965934fSJohn Wang      */
227d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
228d965934fSJohn Wang 
229d965934fSJohn Wang     /** @brief Persist the table
230d965934fSJohn Wang      *  @param[in] path - Path to persist the table
231d965934fSJohn Wang      *  @param[in] table - The table
232d965934fSJohn Wang      */
233d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
234d965934fSJohn Wang 
235d965934fSJohn Wang     /** @brief Load bios table to ram
236d965934fSJohn Wang      *  @param[in] path - Path of the table
237d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
238d965934fSJohn Wang      */
239d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
2408241b340SJohn Wang 
2418241b340SJohn Wang     /** @brief Check the attribute value to update
2428241b340SJohn Wang      *  @param[in] attrValueEntry - The attribute value entry to update
2438241b340SJohn Wang      *  @param[in] attrEntry - The attribute table entry
2448241b340SJohn Wang      *  @param[in] stringTable - The string  table
2458241b340SJohn Wang      *  @return pldm_completion_codes
2468241b340SJohn Wang      */
2478241b340SJohn Wang     int checkAttrValueToUpdate(
2488241b340SJohn Wang         const pldm_bios_attr_val_table_entry* attrValueEntry,
2498241b340SJohn Wang         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
2501b180d8aSGeorge Liu 
2511b180d8aSGeorge Liu     /** @brief Check the attribute table
2521b180d8aSGeorge Liu      *  @param[in] table - The table
2531b180d8aSGeorge Liu      *  @return pldm_completion_codes
2541b180d8aSGeorge Liu      */
2551b180d8aSGeorge Liu     int checkAttributeTable(const Table& table);
2561b180d8aSGeorge Liu 
2571b180d8aSGeorge Liu     /** @brief Check the attribute value table
2581b180d8aSGeorge Liu      *  @param[in] table - The table
2591b180d8aSGeorge Liu      *  @return pldm_completion_codes
2601b180d8aSGeorge Liu      */
2611b180d8aSGeorge Liu     int checkAttributeValueTable(const Table& table);
2621b180d8aSGeorge Liu 
2631b180d8aSGeorge Liu     /** @brief Update the BaseBIOSTable property of the D-Bus interface
2641b180d8aSGeorge Liu      */
2651b180d8aSGeorge Liu     void updateBaseBIOSTableProperty();
2661244acfdSGeorge Liu 
2671244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface and
2681244acfdSGeorge Liu      *         update BaseBIOSTable
2691244acfdSGeorge Liu      */
2701244acfdSGeorge Liu     void listenPendingAttributes();
2711244acfdSGeorge Liu 
2721244acfdSGeorge Liu     /** @brief Find attribute handle from bios attribute table
2731244acfdSGeorge Liu      *  @param[in] attrName - attribute name
2741244acfdSGeorge Liu      *  @return attribute handle
2751244acfdSGeorge Liu      */
2761244acfdSGeorge Liu     uint16_t findAttrHandle(const std::string& attrName);
2771244acfdSGeorge Liu 
2781244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface
2791244acfdSGeorge Liu      * and update BaseBIOSTable
2801244acfdSGeorge Liu      *  @param[in] msg - Data associated with subscribed signal
2811244acfdSGeorge Liu      */
2821244acfdSGeorge Liu     void constructPendingAttribute(const PendingAttributes& pendingAttributes);
283d965934fSJohn Wang };
284d965934fSJohn Wang 
285d965934fSJohn Wang } // namespace bios
286d965934fSJohn Wang } // namespace responder
287d965934fSJohn Wang } // namespace pldm
288