xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision c453e1641fc46a5a2e6c082b5a561410ecdaf779)
1d965934fSJohn Wang #pragma once
2d965934fSJohn Wang 
3d965934fSJohn Wang #include "bios_attribute.hpp"
4d965934fSJohn Wang #include "bios_table.hpp"
57f839f9dSTom Joseph #include "pldmd/dbus_impl_requester.hpp"
6c0c79481SSampa Misra #include "requester/handler.hpp"
7d965934fSJohn Wang 
8*c453e164SGeorge Liu #include <libpldm/bios_table.h>
9*c453e164SGeorge Liu 
106492f524SGeorge Liu #include <nlohmann/json.hpp>
116492f524SGeorge Liu 
12d965934fSJohn Wang #include <functional>
13d965934fSJohn Wang #include <iostream>
14d965934fSJohn Wang #include <memory>
15d965934fSJohn Wang #include <optional>
16d965934fSJohn Wang #include <set>
17d965934fSJohn Wang #include <string>
18d965934fSJohn Wang #include <vector>
19d965934fSJohn Wang 
20d965934fSJohn Wang namespace pldm
21d965934fSJohn Wang {
22d965934fSJohn Wang namespace responder
23d965934fSJohn Wang {
24d965934fSJohn Wang namespace bios
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
75c0c79481SSampa Misra      *  @param[in] handler - PLDM request handler
76d965934fSJohn Wang      */
77c0c79481SSampa Misra     explicit BIOSConfig(
78c0c79481SSampa Misra         const char* jsonDir, const char* tableDir,
795079ac4aSBrad Bishop         pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
80c0c79481SSampa Misra         dbus_api::Requester* requester,
81c0c79481SSampa Misra         pldm::requester::Handler<pldm::requester::Request>* handler);
82d965934fSJohn Wang 
83d965934fSJohn Wang     /** @brief Set attribute value on dbus and attribute value table
84d965934fSJohn Wang      *  @param[in] entry - attribute value entry
85d965934fSJohn Wang      *  @param[in] size - size of the attribute value entry
86cac0ebb2SSagar Srinivas      *  @param[in] isBMC - indicates if the attribute is set by BMC
876d6d1e8dSGeorge Liu      *  @param[in] updateDBus          - update Attr value D-Bus property
886d6d1e8dSGeorge Liu      *                                   if this is set to true
897f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
907f839f9dSTom Joseph      *                                   if this is set to true
91d965934fSJohn Wang      *  @return pldm_completion_codes
92d965934fSJohn Wang      */
93cac0ebb2SSagar Srinivas     int setAttrValue(const void* entry, size_t size, bool isBMC,
94cac0ebb2SSagar Srinivas                      bool updateDBus = true, bool updateBaseBIOSTable = true);
95d965934fSJohn Wang 
96d965934fSJohn Wang     /** @brief Remove the persistent tables */
97d965934fSJohn Wang     void removeTables();
98d965934fSJohn Wang 
99d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
100d965934fSJohn Wang     void buildTables();
101d965934fSJohn Wang 
102d965934fSJohn Wang     /** @brief Get BIOS table of specified type
103d965934fSJohn Wang      *  @param[in] tableType - The table type
104d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
105d965934fSJohn Wang      */
106d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
107d965934fSJohn Wang 
1081b180d8aSGeorge Liu     /** @brief set BIOS table
1091b180d8aSGeorge Liu      *  @param[in] tableType - Indicates what table is being transferred
1101b180d8aSGeorge Liu      *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
1111b180d8aSGeorge Liu      *              BIOSAttributeValueTable=0x2}
1121b180d8aSGeorge Liu      *  @param[in] table - table data
1137f839f9dSTom Joseph      *  @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
1147f839f9dSTom Joseph      *                                   if this is set to true
1151b180d8aSGeorge Liu      *  @return pldm_completion_codes
1161b180d8aSGeorge Liu      */
1177f839f9dSTom Joseph     int setBIOSTable(uint8_t tableType, const Table& table,
1187f839f9dSTom Joseph                      bool updateBaseBIOSTable = true);
1191b180d8aSGeorge Liu 
120d965934fSJohn Wang   private:
121ca7b2524STom Joseph     /** @enum Index into the fields in the BaseBIOSTable
122ca7b2524STom Joseph      */
123ca7b2524STom Joseph     enum class Index : uint8_t
124ca7b2524STom Joseph     {
125ca7b2524STom Joseph         attributeType = 0,
126ca7b2524STom Joseph         readOnly,
127ca7b2524STom Joseph         displayName,
128ca7b2524STom Joseph         description,
129ca7b2524STom Joseph         menuPath,
130ca7b2524STom Joseph         currentValue,
131ca7b2524STom Joseph         defaultValue,
132ca7b2524STom Joseph         options,
133ca7b2524STom Joseph     };
134ca7b2524STom Joseph 
135d965934fSJohn Wang     const fs::path jsonDir;
136d965934fSJohn Wang     const fs::path tableDir;
1375079ac4aSBrad Bishop     pldm::utils::DBusHandler* const dbusHandler;
1381b180d8aSGeorge Liu     BaseBIOSTable baseBIOSTableMaps;
139d965934fSJohn Wang 
1407f839f9dSTom Joseph     /** @brief socket descriptor to communicate to host */
1417f839f9dSTom Joseph     int fd;
1427f839f9dSTom Joseph 
1437f839f9dSTom Joseph     /** @brief MCTP EID of host firmware */
1447f839f9dSTom Joseph     uint8_t eid;
1457f839f9dSTom Joseph 
1467f839f9dSTom Joseph     /** @brief pointer to Requester object, primarily used to access API to
1477f839f9dSTom Joseph      *  obtain PLDM instance id.
1487f839f9dSTom Joseph      */
1497f839f9dSTom Joseph     dbus_api::Requester* requester;
1507f839f9dSTom Joseph 
151c0c79481SSampa Misra     /** @brief PLDM request handler */
152c0c79481SSampa Misra     pldm::requester::Handler<pldm::requester::Request>* handler;
153c0c79481SSampa Misra 
154d965934fSJohn Wang     // vector persists all attributes
155d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
156d965934fSJohn Wang     BIOSAttributes biosAttributes;
157d965934fSJohn Wang 
15846ece063SSampa Misra     using propName = std::string;
1595079ac4aSBrad Bishop     using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
16046ece063SSampa Misra 
16146ece063SSampa Misra     // vector to catch the D-Bus property change signals for BIOS attributes
16284b790cbSPatrick Williams     std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
16346ece063SSampa Misra 
16446ece063SSampa Misra     /** @brief Method to update a BIOS attribute when the corresponding Dbus
16546ece063SSampa Misra      *  property is changed
16646ece063SSampa Misra      *  @param[in] chProperties - list of properties which have changed
16746ece063SSampa Misra      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
16846ece063SSampa Misra      * biosAttributes
16946ece063SSampa Misra      *  @return - none
17046ece063SSampa Misra      */
17146ece063SSampa Misra     void processBiosAttrChangeNotification(
17246ece063SSampa Misra         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
17346ece063SSampa Misra 
174d965934fSJohn Wang     /** @brief Construct an attribute and persist it
175d965934fSJohn Wang      *  @tparam T - attribute type
176d965934fSJohn Wang      *  @param[in] entry - json entry
177d965934fSJohn Wang      */
178d965934fSJohn Wang     template <typename T>
179d965934fSJohn Wang     void constructAttribute(const Json& entry)
180d965934fSJohn Wang     {
181d965934fSJohn Wang         try
182d965934fSJohn Wang         {
183d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
18446ece063SSampa Misra             auto biosAttrIndex = biosAttributes.size() - 1;
18546ece063SSampa Misra             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
18646ece063SSampa Misra 
18746ece063SSampa Misra             if (dBusMap.has_value())
18846ece063SSampa Misra             {
18946ece063SSampa Misra                 using namespace sdbusplus::bus::match::rules;
19046ece063SSampa Misra                 biosAttrMatch.push_back(
19184b790cbSPatrick Williams                     std::make_unique<sdbusplus::bus::match_t>(
19246ece063SSampa Misra                         pldm::utils::DBusHandler::getBus(),
19346ece063SSampa Misra                         propertiesChanged(dBusMap->objectPath,
19446ece063SSampa Misra                                           dBusMap->interface),
19584b790cbSPatrick Williams                         [this, biosAttrIndex](sdbusplus::message_t& msg) {
19646ece063SSampa Misra                             DbusChObjProperties props;
19746ece063SSampa Misra                             std::string iface;
19846ece063SSampa Misra                             msg.read(iface, props);
19946ece063SSampa Misra                             processBiosAttrChangeNotification(props,
20046ece063SSampa Misra                                                               biosAttrIndex);
20146ece063SSampa Misra                         }));
20246ece063SSampa Misra             }
203d965934fSJohn Wang         }
204d965934fSJohn Wang         catch (const std::exception& e)
205d965934fSJohn Wang         {
206d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
207d965934fSJohn Wang                       << std::endl;
208d965934fSJohn Wang         }
209d965934fSJohn Wang     }
210d965934fSJohn Wang 
211d965934fSJohn Wang     /** Construct attributes and persist them */
212d965934fSJohn Wang     void constructAttributes();
213d965934fSJohn Wang 
214d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
215d965934fSJohn Wang 
216d965934fSJohn Wang     /** @brief Helper function to parse json
217d965934fSJohn Wang      *  @param[in] filePath - Path of json file
218d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
219d965934fSJohn Wang      */
220d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
221d965934fSJohn Wang 
222d965934fSJohn Wang     /** @brief Build String Table and persist it
223d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
224d965934fSJohn Wang      */
225d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
226d965934fSJohn Wang 
227ca7b2524STom Joseph     /** @brief Build attribute table and attribute value table and persist them
228ca7b2524STom Joseph      *         Read the BaseBIOSTable from the bios-settings-manager and update
229ca7b2524STom Joseph      *         attribute table and attribute value table.
230ca7b2524STom Joseph      *
231d965934fSJohn Wang      *  @param[in] stringTable - The string Table
232d965934fSJohn Wang      */
233d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
234d965934fSJohn Wang 
235d965934fSJohn Wang     /** @brief Persist the table
236d965934fSJohn Wang      *  @param[in] path - Path to persist the table
237d965934fSJohn Wang      *  @param[in] table - The table
238d965934fSJohn Wang      */
239d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
240d965934fSJohn Wang 
241d965934fSJohn Wang     /** @brief Load bios table to ram
242d965934fSJohn Wang      *  @param[in] path - Path of the table
243d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
244d965934fSJohn Wang      */
245d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
2468241b340SJohn Wang 
247cac0ebb2SSagar Srinivas     /** @brief Method to decode the attribute name from the string handle
248cac0ebb2SSagar Srinivas      *
249cac0ebb2SSagar Srinivas      *  @param[in] stringEntry - string entry from string table
250cac0ebb2SSagar Srinivas      *  @return the decoded string
251cac0ebb2SSagar Srinivas      */
252cac0ebb2SSagar Srinivas     std::string decodeStringFromStringEntry(
253cac0ebb2SSagar Srinivas         const pldm_bios_string_table_entry* stringEntry);
254cac0ebb2SSagar Srinivas 
255cac0ebb2SSagar Srinivas     /** @brief Method to print the string Handle by passing the attribute Handle
256cac0ebb2SSagar Srinivas      *         of the bios attribute that got updated
257cac0ebb2SSagar Srinivas      *
258cac0ebb2SSagar Srinivas      *  @param[in] handle - the Attribute handle of the bios attribute
259cac0ebb2SSagar Srinivas      *  @param[in] index - index to the possible value handles
260cac0ebb2SSagar Srinivas      *  @param[in] attrTable - the attribute table
261cac0ebb2SSagar Srinivas      *  @param[in] stringTable - the string table
262cac0ebb2SSagar Srinivas      *  @return string handle from the string table and decoded string to the
263cac0ebb2SSagar Srinivas      * name handle
264cac0ebb2SSagar Srinivas      */
265cac0ebb2SSagar Srinivas     std::string displayStringHandle(uint16_t handle, uint8_t index,
266cac0ebb2SSagar Srinivas                                     const std::optional<Table>& attrTable,
267cac0ebb2SSagar Srinivas                                     const std::optional<Table>& stringTable);
268cac0ebb2SSagar Srinivas 
269cac0ebb2SSagar Srinivas     /** @brief Method to trace the bios attribute which got changed
270cac0ebb2SSagar Srinivas      *
271cac0ebb2SSagar Srinivas      *  @param[in] attrValueEntry - The attribute value entry to update
272cac0ebb2SSagar Srinivas      *  @param[in] attrEntry - The attribute table entry
273cac0ebb2SSagar Srinivas      *  @param[in] isBMC - indicates if the attribute is set by BMC
274cac0ebb2SSagar Srinivas      */
275cac0ebb2SSagar Srinivas     void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
276cac0ebb2SSagar Srinivas                          const pldm_bios_attr_table_entry* attrEntry,
277cac0ebb2SSagar Srinivas                          bool isBMC);
278cac0ebb2SSagar Srinivas 
2798241b340SJohn Wang     /** @brief Check the attribute value to update
2808241b340SJohn Wang      *  @param[in] attrValueEntry - The attribute value entry to update
2818241b340SJohn Wang      *  @param[in] attrEntry - The attribute table entry
2828241b340SJohn Wang      *  @param[in] stringTable - The string  table
2838241b340SJohn Wang      *  @return pldm_completion_codes
2848241b340SJohn Wang      */
2858241b340SJohn Wang     int checkAttrValueToUpdate(
2868241b340SJohn Wang         const pldm_bios_attr_val_table_entry* attrValueEntry,
2878241b340SJohn Wang         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
2881b180d8aSGeorge Liu 
2891b180d8aSGeorge Liu     /** @brief Check the attribute table
2901b180d8aSGeorge Liu      *  @param[in] table - The table
2911b180d8aSGeorge Liu      *  @return pldm_completion_codes
2921b180d8aSGeorge Liu      */
2931b180d8aSGeorge Liu     int checkAttributeTable(const Table& table);
2941b180d8aSGeorge Liu 
2951b180d8aSGeorge Liu     /** @brief Check the attribute value table
2961b180d8aSGeorge Liu      *  @param[in] table - The table
2971b180d8aSGeorge Liu      *  @return pldm_completion_codes
2981b180d8aSGeorge Liu      */
2991b180d8aSGeorge Liu     int checkAttributeValueTable(const Table& table);
3001b180d8aSGeorge Liu 
3011b180d8aSGeorge Liu     /** @brief Update the BaseBIOSTable property of the D-Bus interface
3021b180d8aSGeorge Liu      */
3031b180d8aSGeorge Liu     void updateBaseBIOSTableProperty();
3041244acfdSGeorge Liu 
3051244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface and
3061244acfdSGeorge Liu      *         update BaseBIOSTable
3071244acfdSGeorge Liu      */
3081244acfdSGeorge Liu     void listenPendingAttributes();
3091244acfdSGeorge Liu 
3101244acfdSGeorge Liu     /** @brief Find attribute handle from bios attribute table
3111244acfdSGeorge Liu      *  @param[in] attrName - attribute name
3121244acfdSGeorge Liu      *  @return attribute handle
3131244acfdSGeorge Liu      */
3141244acfdSGeorge Liu     uint16_t findAttrHandle(const std::string& attrName);
3151244acfdSGeorge Liu 
3161244acfdSGeorge Liu     /** @brief Listen the PendingAttributes property of the D-Bus interface
3171244acfdSGeorge Liu      * and update BaseBIOSTable
3181244acfdSGeorge Liu      *  @param[in] msg - Data associated with subscribed signal
3191244acfdSGeorge Liu      */
3201244acfdSGeorge Liu     void constructPendingAttribute(const PendingAttributes& pendingAttributes);
321d965934fSJohn Wang };
322d965934fSJohn Wang 
323d965934fSJohn Wang } // namespace bios
324d965934fSJohn Wang } // namespace responder
325d965934fSJohn Wang } // namespace pldm
326