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