xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision 6492f524b7b2dfc9f04ee0ad68c4c2e35bbbcd25)
1d965934fSJohn Wang #pragma once
2d965934fSJohn Wang 
3*6492f524SGeorge Liu #include "bios_table.h"
4*6492f524SGeorge Liu 
5d965934fSJohn Wang #include "bios_attribute.hpp"
6d965934fSJohn Wang #include "bios_table.hpp"
7d965934fSJohn Wang 
8*6492f524SGeorge Liu #include <nlohmann/json.hpp>
9*6492f524SGeorge 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 
25d965934fSJohn Wang /** @class BIOSConfig
26d965934fSJohn Wang  *  @brief Manager BIOS Attributes
27d965934fSJohn Wang  */
28d965934fSJohn Wang class BIOSConfig
29d965934fSJohn Wang {
30d965934fSJohn Wang   public:
31d965934fSJohn Wang     BIOSConfig() = delete;
32d965934fSJohn Wang     BIOSConfig(const BIOSConfig&) = delete;
33d965934fSJohn Wang     BIOSConfig(BIOSConfig&&) = delete;
34d965934fSJohn Wang     BIOSConfig& operator=(const BIOSConfig&) = delete;
35d965934fSJohn Wang     BIOSConfig& operator=(BIOSConfig&&) = delete;
36d965934fSJohn Wang     ~BIOSConfig() = default;
37d965934fSJohn Wang 
38d965934fSJohn Wang     /** @brief Construct BIOSConfig
39d965934fSJohn Wang      *  @param[in] jsonDir - The directory where json file exists
40d965934fSJohn Wang      *  @param[in] tableDir - The directory where the persistent table is placed
41d965934fSJohn Wang      *  @param[in] dbusHandler - Dbus Handler
42d965934fSJohn Wang      */
43d965934fSJohn Wang     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
44d965934fSJohn Wang                         DBusHandler* const dbusHandler);
45d965934fSJohn Wang 
46d965934fSJohn Wang     /** @brief Set attribute value on dbus and attribute value table
47d965934fSJohn Wang      *  @param[in] entry - attribute value entry
48d965934fSJohn Wang      *  @param[in] size - size of the attribute value entry
49d965934fSJohn Wang      *  @return pldm_completion_codes
50d965934fSJohn Wang      */
51d965934fSJohn Wang     int setAttrValue(const void* entry, size_t size);
52d965934fSJohn Wang 
53d965934fSJohn Wang     /** @brief Remove the persistent tables */
54d965934fSJohn Wang     void removeTables();
55d965934fSJohn Wang 
56d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
57d965934fSJohn Wang     void buildTables();
58d965934fSJohn Wang 
59d965934fSJohn Wang     /** @brief Get BIOS table of specified type
60d965934fSJohn Wang      *  @param[in] tableType - The table type
61d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
62d965934fSJohn Wang      */
63d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
64d965934fSJohn Wang 
65d965934fSJohn Wang   private:
66d965934fSJohn Wang     const fs::path jsonDir;
67d965934fSJohn Wang     const fs::path tableDir;
68d965934fSJohn Wang     DBusHandler* const dbusHandler;
69d965934fSJohn Wang 
70d965934fSJohn Wang     // vector persists all attributes
71d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
72d965934fSJohn Wang     BIOSAttributes biosAttributes;
73d965934fSJohn Wang 
7446ece063SSampa Misra     using propName = std::string;
7546ece063SSampa Misra     using DbusChObjProperties = std::map<propName, PropertyValue>;
7646ece063SSampa Misra 
7746ece063SSampa Misra     // vector to catch the D-Bus property change signals for BIOS attributes
7846ece063SSampa Misra     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
7946ece063SSampa Misra 
8046ece063SSampa Misra     /** @brief Method to update a BIOS attribute when the corresponding Dbus
8146ece063SSampa Misra      *  property is changed
8246ece063SSampa Misra      *  @param[in] chProperties - list of properties which have changed
8346ece063SSampa Misra      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
8446ece063SSampa Misra      * biosAttributes
8546ece063SSampa Misra      *  @return - none
8646ece063SSampa Misra      */
8746ece063SSampa Misra     void processBiosAttrChangeNotification(
8846ece063SSampa Misra         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
8946ece063SSampa Misra 
90d965934fSJohn Wang     /** @brief Construct an attribute and persist it
91d965934fSJohn Wang      *  @tparam T - attribute type
92d965934fSJohn Wang      *  @param[in] entry - json entry
93d965934fSJohn Wang      */
94d965934fSJohn Wang     template <typename T>
95d965934fSJohn Wang     void constructAttribute(const Json& entry)
96d965934fSJohn Wang     {
97d965934fSJohn Wang         try
98d965934fSJohn Wang         {
99d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
10046ece063SSampa Misra             auto biosAttrIndex = biosAttributes.size() - 1;
10146ece063SSampa Misra             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
10246ece063SSampa Misra 
10346ece063SSampa Misra             if (dBusMap.has_value())
10446ece063SSampa Misra             {
10546ece063SSampa Misra                 using namespace sdbusplus::bus::match::rules;
10646ece063SSampa Misra                 biosAttrMatch.push_back(
10746ece063SSampa Misra                     std::make_unique<sdbusplus::bus::match::match>(
10846ece063SSampa Misra                         pldm::utils::DBusHandler::getBus(),
10946ece063SSampa Misra                         propertiesChanged(dBusMap->objectPath,
11046ece063SSampa Misra                                           dBusMap->interface),
11146ece063SSampa Misra                         [this,
11246ece063SSampa Misra                          biosAttrIndex](sdbusplus::message::message& msg) {
11346ece063SSampa Misra                             DbusChObjProperties props;
11446ece063SSampa Misra                             std::string iface;
11546ece063SSampa Misra                             msg.read(iface, props);
11646ece063SSampa Misra                             processBiosAttrChangeNotification(props,
11746ece063SSampa Misra                                                               biosAttrIndex);
11846ece063SSampa Misra                         }));
11946ece063SSampa Misra             }
120d965934fSJohn Wang         }
121d965934fSJohn Wang         catch (const std::exception& e)
122d965934fSJohn Wang         {
123d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
124d965934fSJohn Wang                       << std::endl;
125d965934fSJohn Wang         }
126d965934fSJohn Wang     }
127d965934fSJohn Wang 
128d965934fSJohn Wang     /** Construct attributes and persist them */
129d965934fSJohn Wang     void constructAttributes();
130d965934fSJohn Wang 
131d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
132d965934fSJohn Wang 
133d965934fSJohn Wang     /** @brief Helper function to parse json
134d965934fSJohn Wang      *  @param[in] filePath - Path of json file
135d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
136d965934fSJohn Wang      */
137d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
138d965934fSJohn Wang 
139d965934fSJohn Wang     /** @brief Build String Table and persist it
140d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
141d965934fSJohn Wang      */
142d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
143d965934fSJohn Wang 
144d965934fSJohn Wang     /** @brief Build attr table and attr value table and persist them
145d965934fSJohn Wang      *  @param[in] stringTable - The string Table
146d965934fSJohn Wang      */
147d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
148d965934fSJohn Wang 
149d965934fSJohn Wang     /** @brief Persist the table
150d965934fSJohn Wang      *  @param[in] path - Path to persist the table
151d965934fSJohn Wang      *  @param[in] table - The table
152d965934fSJohn Wang      */
153d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
154d965934fSJohn Wang 
155d965934fSJohn Wang     /** @brief Load bios table to ram
156d965934fSJohn Wang      *  @param[in] path - Path of the table
157d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
158d965934fSJohn Wang      */
159d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
1608241b340SJohn Wang 
1618241b340SJohn Wang     /** @brief Check the attribute value to update
1628241b340SJohn Wang      *  @param[in] attrValueEntry - The attribute value entry to update
1638241b340SJohn Wang      *  @param[in] attrEntry - The attribute table entry
1648241b340SJohn Wang      *  @param[in] stringTable - The string  table
1658241b340SJohn Wang      *  @return pldm_completion_codes
1668241b340SJohn Wang      */
1678241b340SJohn Wang     int checkAttrValueToUpdate(
1688241b340SJohn Wang         const pldm_bios_attr_val_table_entry* attrValueEntry,
1698241b340SJohn Wang         const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
170d965934fSJohn Wang };
171d965934fSJohn Wang 
172d965934fSJohn Wang } // namespace bios
173d965934fSJohn Wang } // namespace responder
174d965934fSJohn Wang } // namespace pldm
175