xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision ca7b252410783386e3bab2d4c584bf1e701fe378)
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
837f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
847f839f9dSTom Joseph      *                                   if this is set to true
85d965934fSJohn Wang      *  @return pldm_completion_codes
86d965934fSJohn Wang      */
877f839f9dSTom Joseph     int setAttrValue(const void* entry, size_t size,
887f839f9dSTom Joseph                      bool updateBaseBIOSTable = true);
89d965934fSJohn Wang 
90d965934fSJohn Wang     /** @brief Remove the persistent tables */
91d965934fSJohn Wang     void removeTables();
92d965934fSJohn Wang 
93d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
94d965934fSJohn Wang     void buildTables();
95d965934fSJohn Wang 
96d965934fSJohn Wang     /** @brief Get BIOS table of specified type
97d965934fSJohn Wang      *  @param[in] tableType - The table type
98d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
99d965934fSJohn Wang      */
100d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
101d965934fSJohn Wang 
1021b180d8aSGeorge Liu     /** @brief set BIOS table
1031b180d8aSGeorge Liu      *  @param[in] tableType - Indicates what table is being transferred
1041b180d8aSGeorge Liu      *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
1051b180d8aSGeorge Liu      *              BIOSAttributeValueTable=0x2}
1061b180d8aSGeorge Liu      *  @param[in] table - table data
1077f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
1087f839f9dSTom Joseph      *                                   if this is set to true
1091b180d8aSGeorge Liu      *  @return pldm_completion_codes
1101b180d8aSGeorge Liu      */
1117f839f9dSTom Joseph     int setBIOSTable(uint8_t tableType, const Table& table,
1127f839f9dSTom Joseph                      bool updateBaseBIOSTable = true);
1131b180d8aSGeorge Liu 
114d965934fSJohn Wang   private:
115*ca7b2524STom Joseph     /** @enum Index into the fields in the BaseBIOSTable
116*ca7b2524STom Joseph      */
117*ca7b2524STom Joseph     enum class Index : uint8_t
118*ca7b2524STom Joseph     {
119*ca7b2524STom Joseph         attributeType = 0,
120*ca7b2524STom Joseph         readOnly,
121*ca7b2524STom Joseph         displayName,
122*ca7b2524STom Joseph         description,
123*ca7b2524STom Joseph         menuPath,
124*ca7b2524STom Joseph         currentValue,
125*ca7b2524STom Joseph         defaultValue,
126*ca7b2524STom Joseph         options,
127*ca7b2524STom Joseph     };
128*ca7b2524STom Joseph 
129d965934fSJohn Wang     const fs::path jsonDir;
130d965934fSJohn Wang     const fs::path tableDir;
131d965934fSJohn Wang     DBusHandler* const dbusHandler;
1321b180d8aSGeorge Liu     BaseBIOSTable baseBIOSTableMaps;
133d965934fSJohn Wang 
1347f839f9dSTom Joseph     /** @brief socket descriptor to communicate to host */
1357f839f9dSTom Joseph     int fd;
1367f839f9dSTom Joseph 
1377f839f9dSTom Joseph     /** @brief MCTP EID of host firmware */
1387f839f9dSTom Joseph     uint8_t eid;
1397f839f9dSTom Joseph 
1407f839f9dSTom Joseph     /** @brief pointer to Requester object, primarily used to access API to
1417f839f9dSTom Joseph      *  obtain PLDM instance id.
1427f839f9dSTom Joseph      */
1437f839f9dSTom Joseph     dbus_api::Requester* requester;
1447f839f9dSTom Joseph 
145d965934fSJohn Wang     // vector persists all attributes
146d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
147d965934fSJohn Wang     BIOSAttributes biosAttributes;
148d965934fSJohn Wang 
14946ece063SSampa Misra     using propName = std::string;
15046ece063SSampa Misra     using DbusChObjProperties = std::map<propName, PropertyValue>;
15146ece063SSampa Misra 
15246ece063SSampa Misra     // vector to catch the D-Bus property change signals for BIOS attributes
15346ece063SSampa Misra     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
15446ece063SSampa Misra 
15546ece063SSampa Misra     /** @brief Method to update a BIOS attribute when the corresponding Dbus
15646ece063SSampa Misra      *  property is changed
15746ece063SSampa Misra      *  @param[in] chProperties - list of properties which have changed
15846ece063SSampa Misra      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
15946ece063SSampa Misra      * biosAttributes
16046ece063SSampa Misra      *  @return - none
16146ece063SSampa Misra      */
16246ece063SSampa Misra     void processBiosAttrChangeNotification(
16346ece063SSampa Misra         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
16446ece063SSampa Misra 
165d965934fSJohn Wang     /** @brief Construct an attribute and persist it
166d965934fSJohn Wang      *  @tparam T - attribute type
167d965934fSJohn Wang      *  @param[in] entry - json entry
168d965934fSJohn Wang      */
169d965934fSJohn Wang     template <typename T>
170d965934fSJohn Wang     void constructAttribute(const Json& entry)
171d965934fSJohn Wang     {
172d965934fSJohn Wang         try
173d965934fSJohn Wang         {
174d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
17546ece063SSampa Misra             auto biosAttrIndex = biosAttributes.size() - 1;
17646ece063SSampa Misra             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
17746ece063SSampa Misra 
17846ece063SSampa Misra             if (dBusMap.has_value())
17946ece063SSampa Misra             {
18046ece063SSampa Misra                 using namespace sdbusplus::bus::match::rules;
18146ece063SSampa Misra                 biosAttrMatch.push_back(
18246ece063SSampa Misra                     std::make_unique<sdbusplus::bus::match::match>(
18346ece063SSampa Misra                         pldm::utils::DBusHandler::getBus(),
18446ece063SSampa Misra                         propertiesChanged(dBusMap->objectPath,
18546ece063SSampa Misra                                           dBusMap->interface),
18646ece063SSampa Misra                         [this,
18746ece063SSampa Misra                          biosAttrIndex](sdbusplus::message::message& msg) {
18846ece063SSampa Misra                             DbusChObjProperties props;
18946ece063SSampa Misra                             std::string iface;
19046ece063SSampa Misra                             msg.read(iface, props);
19146ece063SSampa Misra                             processBiosAttrChangeNotification(props,
19246ece063SSampa Misra                                                               biosAttrIndex);
19346ece063SSampa Misra                         }));
19446ece063SSampa Misra             }
195d965934fSJohn Wang         }
196d965934fSJohn Wang         catch (const std::exception& e)
197d965934fSJohn Wang         {
198d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
199d965934fSJohn Wang                       << std::endl;
200d965934fSJohn Wang         }
201d965934fSJohn Wang     }
202d965934fSJohn Wang 
203d965934fSJohn Wang     /** Construct attributes and persist them */
204d965934fSJohn Wang     void constructAttributes();
205d965934fSJohn Wang 
206d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
207d965934fSJohn Wang 
208d965934fSJohn Wang     /** @brief Helper function to parse json
209d965934fSJohn Wang      *  @param[in] filePath - Path of json file
210d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
211d965934fSJohn Wang      */
212d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
213d965934fSJohn Wang 
214d965934fSJohn Wang     /** @brief Build String Table and persist it
215d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
216d965934fSJohn Wang      */
217d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
218d965934fSJohn Wang 
219*ca7b2524STom Joseph     /** @brief Build attribute table and attribute value table and persist them
220*ca7b2524STom Joseph      *         Read the BaseBIOSTable from the bios-settings-manager and update
221*ca7b2524STom Joseph      *         attribute table and attribute value table.
222*ca7b2524STom Joseph      *
223d965934fSJohn Wang      *  @param[in] stringTable - The string Table
224d965934fSJohn Wang      */
225d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
226d965934fSJohn Wang 
227d965934fSJohn Wang     /** @brief Persist the table
228d965934fSJohn Wang      *  @param[in] path - Path to persist the table
229d965934fSJohn Wang      *  @param[in] table - The table
230d965934fSJohn Wang      */
231d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
232d965934fSJohn Wang 
233d965934fSJohn Wang     /** @brief Load bios table to ram
234d965934fSJohn Wang      *  @param[in] path - Path of the table
235d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
236d965934fSJohn Wang      */
237d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
2388241b340SJohn Wang 
2398241b340SJohn Wang     /** @brief Check the attribute value to update
2408241b340SJohn Wang      *  @param[in] attrValueEntry - The attribute value entry to update
2418241b340SJohn Wang      *  @param[in] attrEntry - The attribute table entry
2428241b340SJohn Wang      *  @param[in] stringTable - The string  table
2438241b340SJohn Wang      *  @return pldm_completion_codes
2448241b340SJohn Wang      */
2458241b340SJohn Wang     int checkAttrValueToUpdate(
2468241b340SJohn Wang         const pldm_bios_attr_val_table_entry* attrValueEntry,
2478241b340SJohn Wang         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
2481b180d8aSGeorge Liu 
2491b180d8aSGeorge Liu     /** @brief Check the attribute table
2501b180d8aSGeorge Liu      *  @param[in] table - The table
2511b180d8aSGeorge Liu      *  @return pldm_completion_codes
2521b180d8aSGeorge Liu      */
2531b180d8aSGeorge Liu     int checkAttributeTable(const Table& table);
2541b180d8aSGeorge Liu 
2551b180d8aSGeorge Liu     /** @brief Check the attribute value table
2561b180d8aSGeorge Liu      *  @param[in] table - The table
2571b180d8aSGeorge Liu      *  @return pldm_completion_codes
2581b180d8aSGeorge Liu      */
2591b180d8aSGeorge Liu     int checkAttributeValueTable(const Table& table);
2601b180d8aSGeorge Liu 
2611b180d8aSGeorge Liu     /** @brief Update the BaseBIOSTable property of the D-Bus interface
2621b180d8aSGeorge Liu      */
2631b180d8aSGeorge Liu     void updateBaseBIOSTableProperty();
2641244acfdSGeorge Liu 
2651244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface and
2661244acfdSGeorge Liu      *         update BaseBIOSTable
2671244acfdSGeorge Liu      */
2681244acfdSGeorge Liu     void listenPendingAttributes();
2691244acfdSGeorge Liu 
2701244acfdSGeorge Liu     /** @brief Find attribute handle from bios attribute table
2711244acfdSGeorge Liu      *  @param[in] attrName - attribute name
2721244acfdSGeorge Liu      *  @return attribute handle
2731244acfdSGeorge Liu      */
2741244acfdSGeorge Liu     uint16_t findAttrHandle(const std::string& attrName);
2751244acfdSGeorge Liu 
2761244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface
2771244acfdSGeorge Liu      * and update BaseBIOSTable
2781244acfdSGeorge Liu      *  @param[in] msg - Data associated with subscribed signal
2791244acfdSGeorge Liu      */
2801244acfdSGeorge Liu     void constructPendingAttribute(const PendingAttributes& pendingAttributes);
281d965934fSJohn Wang };
282d965934fSJohn Wang 
283d965934fSJohn Wang } // namespace bios
284d965934fSJohn Wang } // namespace responder
285d965934fSJohn Wang } // namespace pldm
286