xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision 1b180d8a6d3fa1be6a67e83bafb0cbdfa97ce4d8)
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"
7d965934fSJohn Wang 
86492f524SGeorge Liu #include <nlohmann/json.hpp>
96492f524SGeorge Liu 
10d965934fSJohn Wang #include <functional>
11d965934fSJohn Wang #include <iostream>
12d965934fSJohn Wang #include <memory>
13d965934fSJohn Wang #include <optional>
14d965934fSJohn Wang #include <set>
15d965934fSJohn Wang #include <string>
16d965934fSJohn Wang #include <vector>
17d965934fSJohn Wang 
18d965934fSJohn Wang namespace pldm
19d965934fSJohn Wang {
20d965934fSJohn Wang namespace responder
21d965934fSJohn Wang {
22d965934fSJohn Wang namespace bios
23d965934fSJohn Wang {
24d965934fSJohn Wang 
25*1b180d8aSGeorge Liu enum class BoundType
26*1b180d8aSGeorge Liu {
27*1b180d8aSGeorge Liu     LowerBound,
28*1b180d8aSGeorge Liu     UpperBound,
29*1b180d8aSGeorge Liu     ScalarIncrement,
30*1b180d8aSGeorge Liu     MinStringLength,
31*1b180d8aSGeorge Liu     MaxStringLength,
32*1b180d8aSGeorge Liu     OneOf
33*1b180d8aSGeorge Liu };
34*1b180d8aSGeorge Liu 
35*1b180d8aSGeorge Liu using AttributeName = std::string;
36*1b180d8aSGeorge Liu using AttributeType = std::string;
37*1b180d8aSGeorge Liu using ReadonlyStatus = bool;
38*1b180d8aSGeorge Liu using DisplayName = std::string;
39*1b180d8aSGeorge Liu using Description = std::string;
40*1b180d8aSGeorge Liu using MenuPath = std::string;
41*1b180d8aSGeorge Liu using CurrentValue = std::variant<int64_t, std::string>;
42*1b180d8aSGeorge Liu using DefaultValue = std::variant<int64_t, std::string>;
43*1b180d8aSGeorge Liu using OptionString = std::string;
44*1b180d8aSGeorge Liu using OptionValue = std::variant<int64_t, std::string>;
45*1b180d8aSGeorge Liu using Option = std::vector<std::tuple<OptionString, OptionValue>>;
46*1b180d8aSGeorge Liu using BIOSTableObj =
47*1b180d8aSGeorge Liu     std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
48*1b180d8aSGeorge Liu                MenuPath, CurrentValue, DefaultValue, Option>;
49*1b180d8aSGeorge Liu using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
50*1b180d8aSGeorge Liu 
51d965934fSJohn Wang /** @class BIOSConfig
52d965934fSJohn Wang  *  @brief Manager BIOS Attributes
53d965934fSJohn Wang  */
54d965934fSJohn Wang class BIOSConfig
55d965934fSJohn Wang {
56d965934fSJohn Wang   public:
57d965934fSJohn Wang     BIOSConfig() = delete;
58d965934fSJohn Wang     BIOSConfig(const BIOSConfig&) = delete;
59d965934fSJohn Wang     BIOSConfig(BIOSConfig&&) = delete;
60d965934fSJohn Wang     BIOSConfig& operator=(const BIOSConfig&) = delete;
61d965934fSJohn Wang     BIOSConfig& operator=(BIOSConfig&&) = delete;
62d965934fSJohn Wang     ~BIOSConfig() = default;
63d965934fSJohn Wang 
64d965934fSJohn Wang     /** @brief Construct BIOSConfig
65d965934fSJohn Wang      *  @param[in] jsonDir - The directory where json file exists
66d965934fSJohn Wang      *  @param[in] tableDir - The directory where the persistent table is placed
67d965934fSJohn Wang      *  @param[in] dbusHandler - Dbus Handler
68d965934fSJohn Wang      */
69d965934fSJohn Wang     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
70d965934fSJohn Wang                         DBusHandler* const dbusHandler);
71d965934fSJohn Wang 
72d965934fSJohn Wang     /** @brief Set attribute value on dbus and attribute value table
73d965934fSJohn Wang      *  @param[in] entry - attribute value entry
74d965934fSJohn Wang      *  @param[in] size - size of the attribute value entry
75d965934fSJohn Wang      *  @return pldm_completion_codes
76d965934fSJohn Wang      */
77d965934fSJohn Wang     int setAttrValue(const void* entry, size_t size);
78d965934fSJohn Wang 
79d965934fSJohn Wang     /** @brief Remove the persistent tables */
80d965934fSJohn Wang     void removeTables();
81d965934fSJohn Wang 
82d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
83d965934fSJohn Wang     void buildTables();
84d965934fSJohn Wang 
85d965934fSJohn Wang     /** @brief Get BIOS table of specified type
86d965934fSJohn Wang      *  @param[in] tableType - The table type
87d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
88d965934fSJohn Wang      */
89d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
90d965934fSJohn Wang 
91*1b180d8aSGeorge Liu     /** @brief set BIOS table
92*1b180d8aSGeorge Liu      *  @param[in] tableType - Indicates what table is being transferred
93*1b180d8aSGeorge Liu      *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
94*1b180d8aSGeorge Liu      *              BIOSAttributeValueTable=0x2}
95*1b180d8aSGeorge Liu      *  @param[in] table - table data
96*1b180d8aSGeorge Liu      *  @return pldm_completion_codes
97*1b180d8aSGeorge Liu      */
98*1b180d8aSGeorge Liu     int setBIOSTable(uint8_t tableType, const Table& table);
99*1b180d8aSGeorge Liu 
100d965934fSJohn Wang   private:
101d965934fSJohn Wang     const fs::path jsonDir;
102d965934fSJohn Wang     const fs::path tableDir;
103d965934fSJohn Wang     DBusHandler* const dbusHandler;
104*1b180d8aSGeorge Liu     bool isUpdateProperty;
105*1b180d8aSGeorge Liu     BaseBIOSTable baseBIOSTableMaps;
106d965934fSJohn Wang 
107d965934fSJohn Wang     // vector persists all attributes
108d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
109d965934fSJohn Wang     BIOSAttributes biosAttributes;
110d965934fSJohn Wang 
11146ece063SSampa Misra     using propName = std::string;
11246ece063SSampa Misra     using DbusChObjProperties = std::map<propName, PropertyValue>;
11346ece063SSampa Misra 
11446ece063SSampa Misra     // vector to catch the D-Bus property change signals for BIOS attributes
11546ece063SSampa Misra     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
11646ece063SSampa Misra 
11746ece063SSampa Misra     /** @brief Method to update a BIOS attribute when the corresponding Dbus
11846ece063SSampa Misra      *  property is changed
11946ece063SSampa Misra      *  @param[in] chProperties - list of properties which have changed
12046ece063SSampa Misra      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
12146ece063SSampa Misra      * biosAttributes
12246ece063SSampa Misra      *  @return - none
12346ece063SSampa Misra      */
12446ece063SSampa Misra     void processBiosAttrChangeNotification(
12546ece063SSampa Misra         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
12646ece063SSampa Misra 
127d965934fSJohn Wang     /** @brief Construct an attribute and persist it
128d965934fSJohn Wang      *  @tparam T - attribute type
129d965934fSJohn Wang      *  @param[in] entry - json entry
130d965934fSJohn Wang      */
131d965934fSJohn Wang     template <typename T>
132d965934fSJohn Wang     void constructAttribute(const Json& entry)
133d965934fSJohn Wang     {
134d965934fSJohn Wang         try
135d965934fSJohn Wang         {
136d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
13746ece063SSampa Misra             auto biosAttrIndex = biosAttributes.size() - 1;
13846ece063SSampa Misra             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
13946ece063SSampa Misra 
14046ece063SSampa Misra             if (dBusMap.has_value())
14146ece063SSampa Misra             {
14246ece063SSampa Misra                 using namespace sdbusplus::bus::match::rules;
14346ece063SSampa Misra                 biosAttrMatch.push_back(
14446ece063SSampa Misra                     std::make_unique<sdbusplus::bus::match::match>(
14546ece063SSampa Misra                         pldm::utils::DBusHandler::getBus(),
14646ece063SSampa Misra                         propertiesChanged(dBusMap->objectPath,
14746ece063SSampa Misra                                           dBusMap->interface),
14846ece063SSampa Misra                         [this,
14946ece063SSampa Misra                          biosAttrIndex](sdbusplus::message::message& msg) {
15046ece063SSampa Misra                             DbusChObjProperties props;
15146ece063SSampa Misra                             std::string iface;
15246ece063SSampa Misra                             msg.read(iface, props);
15346ece063SSampa Misra                             processBiosAttrChangeNotification(props,
15446ece063SSampa Misra                                                               biosAttrIndex);
15546ece063SSampa Misra                         }));
15646ece063SSampa Misra             }
157d965934fSJohn Wang         }
158d965934fSJohn Wang         catch (const std::exception& e)
159d965934fSJohn Wang         {
160d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
161d965934fSJohn Wang                       << std::endl;
162d965934fSJohn Wang         }
163d965934fSJohn Wang     }
164d965934fSJohn Wang 
165d965934fSJohn Wang     /** Construct attributes and persist them */
166d965934fSJohn Wang     void constructAttributes();
167d965934fSJohn Wang 
168d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
169d965934fSJohn Wang 
170d965934fSJohn Wang     /** @brief Helper function to parse json
171d965934fSJohn Wang      *  @param[in] filePath - Path of json file
172d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
173d965934fSJohn Wang      */
174d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
175d965934fSJohn Wang 
176d965934fSJohn Wang     /** @brief Build String Table and persist it
177d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
178d965934fSJohn Wang      */
179d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
180d965934fSJohn Wang 
181d965934fSJohn Wang     /** @brief Build attr table and attr value table and persist them
182d965934fSJohn Wang      *  @param[in] stringTable - The string Table
183d965934fSJohn Wang      */
184d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
185d965934fSJohn Wang 
186d965934fSJohn Wang     /** @brief Persist the table
187d965934fSJohn Wang      *  @param[in] path - Path to persist the table
188d965934fSJohn Wang      *  @param[in] table - The table
189d965934fSJohn Wang      */
190d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
191d965934fSJohn Wang 
192d965934fSJohn Wang     /** @brief Load bios table to ram
193d965934fSJohn Wang      *  @param[in] path - Path of the table
194d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
195d965934fSJohn Wang      */
196d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
1978241b340SJohn Wang 
1988241b340SJohn Wang     /** @brief Check the attribute value to update
1998241b340SJohn Wang      *  @param[in] attrValueEntry - The attribute value entry to update
2008241b340SJohn Wang      *  @param[in] attrEntry - The attribute table entry
2018241b340SJohn Wang      *  @param[in] stringTable - The string  table
2028241b340SJohn Wang      *  @return pldm_completion_codes
2038241b340SJohn Wang      */
2048241b340SJohn Wang     int checkAttrValueToUpdate(
2058241b340SJohn Wang         const pldm_bios_attr_val_table_entry* attrValueEntry,
2068241b340SJohn Wang         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
207*1b180d8aSGeorge Liu 
208*1b180d8aSGeorge Liu     /** @brief Check the attribute table
209*1b180d8aSGeorge Liu      *  @param[in] table - The table
210*1b180d8aSGeorge Liu      *  @return pldm_completion_codes
211*1b180d8aSGeorge Liu      */
212*1b180d8aSGeorge Liu     int checkAttributeTable(const Table& table);
213*1b180d8aSGeorge Liu 
214*1b180d8aSGeorge Liu     /** @brief Check the attribute value table
215*1b180d8aSGeorge Liu      *  @param[in] table - The table
216*1b180d8aSGeorge Liu      *  @return pldm_completion_codes
217*1b180d8aSGeorge Liu      */
218*1b180d8aSGeorge Liu     int checkAttributeValueTable(const Table& table);
219*1b180d8aSGeorge Liu 
220*1b180d8aSGeorge Liu     /** @brief Update the BaseBIOSTable property of the D-Bus interface
221*1b180d8aSGeorge Liu      */
222*1b180d8aSGeorge Liu     void updateBaseBIOSTableProperty();
223d965934fSJohn Wang };
224d965934fSJohn Wang 
225d965934fSJohn Wang } // namespace bios
226d965934fSJohn Wang } // namespace responder
227d965934fSJohn Wang } // namespace pldm
228