blob: 4cfa5820f9849cad6d82b2527ed6ea77377c106f [file] [log] [blame]
John Wangd9659342020-02-27 16:46:05 +08001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "bios_table.h"
4
John Wangd9659342020-02-27 16:46:05 +08005#include "bios_attribute.hpp"
6#include "bios_table.hpp"
Tom Joseph7f839f92020-09-21 10:20:44 +05307#include "pldmd/dbus_impl_requester.hpp"
John Wangd9659342020-02-27 16:46:05 +08008
George Liu6492f522020-06-16 10:34:05 +08009#include <nlohmann/json.hpp>
10
John Wangd9659342020-02-27 16:46:05 +080011#include <functional>
12#include <iostream>
13#include <memory>
John Wangd9659342020-02-27 16:46:05 +080014#include <optional>
15#include <set>
16#include <string>
17#include <vector>
18
John Wangd9659342020-02-27 16:46:05 +080019namespace pldm
20{
21namespace responder
22{
23namespace bios
24{
25
George Liu1b180d82020-07-23 14:01:58 +080026enum class BoundType
27{
28 LowerBound,
29 UpperBound,
30 ScalarIncrement,
31 MinStringLength,
32 MaxStringLength,
33 OneOf
34};
35
36using AttributeName = std::string;
37using AttributeType = std::string;
38using ReadonlyStatus = bool;
39using DisplayName = std::string;
40using Description = std::string;
41using MenuPath = std::string;
42using CurrentValue = std::variant<int64_t, std::string>;
43using DefaultValue = std::variant<int64_t, std::string>;
44using OptionString = std::string;
45using OptionValue = std::variant<int64_t, std::string>;
46using Option = std::vector<std::tuple<OptionString, OptionValue>>;
47using BIOSTableObj =
48 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
49 MenuPath, CurrentValue, DefaultValue, Option>;
50using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
51
George Liu1244acf2020-08-14 09:11:11 +080052using PendingObj = std::tuple<AttributeType, CurrentValue>;
53using PendingAttributes = std::map<AttributeName, PendingObj>;
54
John Wangd9659342020-02-27 16:46:05 +080055/** @class BIOSConfig
56 * @brief Manager BIOS Attributes
57 */
58class BIOSConfig
59{
60 public:
61 BIOSConfig() = delete;
62 BIOSConfig(const BIOSConfig&) = delete;
63 BIOSConfig(BIOSConfig&&) = delete;
64 BIOSConfig& operator=(const BIOSConfig&) = delete;
65 BIOSConfig& operator=(BIOSConfig&&) = delete;
66 ~BIOSConfig() = default;
67
68 /** @brief Construct BIOSConfig
69 * @param[in] jsonDir - The directory where json file exists
70 * @param[in] tableDir - The directory where the persistent table is placed
71 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053072 * @param[in] fd - socket descriptor to communicate to host
73 * @param[in] eid - MCTP EID of host firmware
74 * @param[in] requester - pointer to Requester object
John Wangd9659342020-02-27 16:46:05 +080075 */
76 explicit BIOSConfig(const char* jsonDir, const char* tableDir,
Tom Joseph7f839f92020-09-21 10:20:44 +053077 DBusHandler* const dbusHandler, int fd, uint8_t eid,
78 dbus_api::Requester* requester);
John Wangd9659342020-02-27 16:46:05 +080079
80 /** @brief Set attribute value on dbus and attribute value table
81 * @param[in] entry - attribute value entry
82 * @param[in] size - size of the attribute value entry
Tom Joseph7f839f92020-09-21 10:20:44 +053083 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
84 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080085 * @return pldm_completion_codes
86 */
Tom Joseph7f839f92020-09-21 10:20:44 +053087 int setAttrValue(const void* entry, size_t size,
88 bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +080089
90 /** @brief Remove the persistent tables */
91 void removeTables();
92
93 /** @brief Build bios tables(string,attribute,attribute value table)*/
94 void buildTables();
95
96 /** @brief Get BIOS table of specified type
97 * @param[in] tableType - The table type
98 * @return The bios table, std::nullopt if the table is unaviliable
99 */
100 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
101
George Liu1b180d82020-07-23 14:01:58 +0800102 /** @brief set BIOS table
103 * @param[in] tableType - Indicates what table is being transferred
104 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
105 * BIOSAttributeValueTable=0x2}
106 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530107 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
108 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800109 * @return pldm_completion_codes
110 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530111 int setBIOSTable(uint8_t tableType, const Table& table,
112 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800113
John Wangd9659342020-02-27 16:46:05 +0800114 private:
115 const fs::path jsonDir;
116 const fs::path tableDir;
117 DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800118 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800119
Tom Joseph7f839f92020-09-21 10:20:44 +0530120 /** @brief socket descriptor to communicate to host */
121 int fd;
122
123 /** @brief MCTP EID of host firmware */
124 uint8_t eid;
125
126 /** @brief pointer to Requester object, primarily used to access API to
127 * obtain PLDM instance id.
128 */
129 dbus_api::Requester* requester;
130
John Wangd9659342020-02-27 16:46:05 +0800131 // vector persists all attributes
132 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
133 BIOSAttributes biosAttributes;
134
Sampa Misra46ece062020-03-18 07:17:44 -0500135 using propName = std::string;
136 using DbusChObjProperties = std::map<propName, PropertyValue>;
137
138 // vector to catch the D-Bus property change signals for BIOS attributes
139 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
140
141 /** @brief Method to update a BIOS attribute when the corresponding Dbus
142 * property is changed
143 * @param[in] chProperties - list of properties which have changed
144 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
145 * biosAttributes
146 * @return - none
147 */
148 void processBiosAttrChangeNotification(
149 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
150
John Wangd9659342020-02-27 16:46:05 +0800151 /** @brief Construct an attribute and persist it
152 * @tparam T - attribute type
153 * @param[in] entry - json entry
154 */
155 template <typename T>
156 void constructAttribute(const Json& entry)
157 {
158 try
159 {
160 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500161 auto biosAttrIndex = biosAttributes.size() - 1;
162 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
163
164 if (dBusMap.has_value())
165 {
166 using namespace sdbusplus::bus::match::rules;
167 biosAttrMatch.push_back(
168 std::make_unique<sdbusplus::bus::match::match>(
169 pldm::utils::DBusHandler::getBus(),
170 propertiesChanged(dBusMap->objectPath,
171 dBusMap->interface),
172 [this,
173 biosAttrIndex](sdbusplus::message::message& msg) {
174 DbusChObjProperties props;
175 std::string iface;
176 msg.read(iface, props);
177 processBiosAttrChangeNotification(props,
178 biosAttrIndex);
179 }));
180 }
John Wangd9659342020-02-27 16:46:05 +0800181 }
182 catch (const std::exception& e)
183 {
184 std::cerr << "Constructs Attribute Error, " << e.what()
185 << std::endl;
186 }
187 }
188
189 /** Construct attributes and persist them */
190 void constructAttributes();
191
192 using ParseHandler = std::function<void(const Json& entry)>;
193
194 /** @brief Helper function to parse json
195 * @param[in] filePath - Path of json file
196 * @param[in] handler - Handler to process each entry in the json
197 */
198 void load(const fs::path& filePath, ParseHandler handler);
199
200 /** @brief Build String Table and persist it
201 * @return The built string table, std::nullopt if it fails.
202 */
203 std::optional<Table> buildAndStoreStringTable();
204
205 /** @brief Build attr table and attr value table and persist them
206 * @param[in] stringTable - The string Table
207 */
208 void buildAndStoreAttrTables(const Table& stringTable);
209
210 /** @brief Persist the table
211 * @param[in] path - Path to persist the table
212 * @param[in] table - The table
213 */
214 void storeTable(const fs::path& path, const Table& table);
215
216 /** @brief Load bios table to ram
217 * @param[in] path - Path of the table
218 * @return The table, std::nullopt if loading fails
219 */
220 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800221
222 /** @brief Check the attribute value to update
223 * @param[in] attrValueEntry - The attribute value entry to update
224 * @param[in] attrEntry - The attribute table entry
225 * @param[in] stringTable - The string table
226 * @return pldm_completion_codes
227 */
228 int checkAttrValueToUpdate(
229 const pldm_bios_attr_val_table_entry* attrValueEntry,
230 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800231
232 /** @brief Check the attribute table
233 * @param[in] table - The table
234 * @return pldm_completion_codes
235 */
236 int checkAttributeTable(const Table& table);
237
238 /** @brief Check the attribute value table
239 * @param[in] table - The table
240 * @return pldm_completion_codes
241 */
242 int checkAttributeValueTable(const Table& table);
243
244 /** @brief Update the BaseBIOSTable property of the D-Bus interface
245 */
246 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800247
248 /** @brief Listen the PendingAttributes property of the D-Bus interface and
249 * update BaseBIOSTable
250 */
251 void listenPendingAttributes();
252
253 /** @brief Find attribute handle from bios attribute table
254 * @param[in] attrName - attribute name
255 * @return attribute handle
256 */
257 uint16_t findAttrHandle(const std::string& attrName);
258
259 /** @brief Listen the PendingAttributes property of the D-Bus interface
260 * and update BaseBIOSTable
261 * @param[in] msg - Data associated with subscribed signal
262 */
263 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800264};
265
266} // namespace bios
267} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500268} // namespace pldm