blob: a6ae3e7e25bad56cfcfe8db67ff44bf89cdbd806 [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
George Liu6d6d1e82021-02-16 11:08:55 +080083 * @param[in] updateDBus - update Attr value D-Bus property
84 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053085 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
86 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080087 * @return pldm_completion_codes
88 */
George Liu6d6d1e82021-02-16 11:08:55 +080089 int setAttrValue(const void* entry, size_t size, bool updateDBus = true,
Tom Joseph7f839f92020-09-21 10:20:44 +053090 bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +080091
92 /** @brief Remove the persistent tables */
93 void removeTables();
94
95 /** @brief Build bios tables(string,attribute,attribute value table)*/
96 void buildTables();
97
98 /** @brief Get BIOS table of specified type
99 * @param[in] tableType - The table type
100 * @return The bios table, std::nullopt if the table is unaviliable
101 */
102 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
103
George Liu1b180d82020-07-23 14:01:58 +0800104 /** @brief set BIOS table
105 * @param[in] tableType - Indicates what table is being transferred
106 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
107 * BIOSAttributeValueTable=0x2}
108 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530109 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
110 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800111 * @return pldm_completion_codes
112 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530113 int setBIOSTable(uint8_t tableType, const Table& table,
114 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800115
John Wangd9659342020-02-27 16:46:05 +0800116 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530117 /** @enum Index into the fields in the BaseBIOSTable
118 */
119 enum class Index : uint8_t
120 {
121 attributeType = 0,
122 readOnly,
123 displayName,
124 description,
125 menuPath,
126 currentValue,
127 defaultValue,
128 options,
129 };
130
John Wangd9659342020-02-27 16:46:05 +0800131 const fs::path jsonDir;
132 const fs::path tableDir;
133 DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800134 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800135
Tom Joseph7f839f92020-09-21 10:20:44 +0530136 /** @brief socket descriptor to communicate to host */
137 int fd;
138
139 /** @brief MCTP EID of host firmware */
140 uint8_t eid;
141
142 /** @brief pointer to Requester object, primarily used to access API to
143 * obtain PLDM instance id.
144 */
145 dbus_api::Requester* requester;
146
John Wangd9659342020-02-27 16:46:05 +0800147 // vector persists all attributes
148 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
149 BIOSAttributes biosAttributes;
150
Sampa Misra46ece062020-03-18 07:17:44 -0500151 using propName = std::string;
152 using DbusChObjProperties = std::map<propName, PropertyValue>;
153
154 // vector to catch the D-Bus property change signals for BIOS attributes
155 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
156
157 /** @brief Method to update a BIOS attribute when the corresponding Dbus
158 * property is changed
159 * @param[in] chProperties - list of properties which have changed
160 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
161 * biosAttributes
162 * @return - none
163 */
164 void processBiosAttrChangeNotification(
165 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
166
John Wangd9659342020-02-27 16:46:05 +0800167 /** @brief Construct an attribute and persist it
168 * @tparam T - attribute type
169 * @param[in] entry - json entry
170 */
171 template <typename T>
172 void constructAttribute(const Json& entry)
173 {
174 try
175 {
176 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500177 auto biosAttrIndex = biosAttributes.size() - 1;
178 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
179
180 if (dBusMap.has_value())
181 {
182 using namespace sdbusplus::bus::match::rules;
183 biosAttrMatch.push_back(
184 std::make_unique<sdbusplus::bus::match::match>(
185 pldm::utils::DBusHandler::getBus(),
186 propertiesChanged(dBusMap->objectPath,
187 dBusMap->interface),
188 [this,
189 biosAttrIndex](sdbusplus::message::message& msg) {
190 DbusChObjProperties props;
191 std::string iface;
192 msg.read(iface, props);
193 processBiosAttrChangeNotification(props,
194 biosAttrIndex);
195 }));
196 }
John Wangd9659342020-02-27 16:46:05 +0800197 }
198 catch (const std::exception& e)
199 {
200 std::cerr << "Constructs Attribute Error, " << e.what()
201 << std::endl;
202 }
203 }
204
205 /** Construct attributes and persist them */
206 void constructAttributes();
207
208 using ParseHandler = std::function<void(const Json& entry)>;
209
210 /** @brief Helper function to parse json
211 * @param[in] filePath - Path of json file
212 * @param[in] handler - Handler to process each entry in the json
213 */
214 void load(const fs::path& filePath, ParseHandler handler);
215
216 /** @brief Build String Table and persist it
217 * @return The built string table, std::nullopt if it fails.
218 */
219 std::optional<Table> buildAndStoreStringTable();
220
Tom Josephca7b2522020-11-18 12:27:11 +0530221 /** @brief Build attribute table and attribute value table and persist them
222 * Read the BaseBIOSTable from the bios-settings-manager and update
223 * attribute table and attribute value table.
224 *
John Wangd9659342020-02-27 16:46:05 +0800225 * @param[in] stringTable - The string Table
226 */
227 void buildAndStoreAttrTables(const Table& stringTable);
228
229 /** @brief Persist the table
230 * @param[in] path - Path to persist the table
231 * @param[in] table - The table
232 */
233 void storeTable(const fs::path& path, const Table& table);
234
235 /** @brief Load bios table to ram
236 * @param[in] path - Path of the table
237 * @return The table, std::nullopt if loading fails
238 */
239 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800240
241 /** @brief Check the attribute value to update
242 * @param[in] attrValueEntry - The attribute value entry to update
243 * @param[in] attrEntry - The attribute table entry
244 * @param[in] stringTable - The string table
245 * @return pldm_completion_codes
246 */
247 int checkAttrValueToUpdate(
248 const pldm_bios_attr_val_table_entry* attrValueEntry,
249 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800250
251 /** @brief Check the attribute table
252 * @param[in] table - The table
253 * @return pldm_completion_codes
254 */
255 int checkAttributeTable(const Table& table);
256
257 /** @brief Check the attribute value table
258 * @param[in] table - The table
259 * @return pldm_completion_codes
260 */
261 int checkAttributeValueTable(const Table& table);
262
263 /** @brief Update the BaseBIOSTable property of the D-Bus interface
264 */
265 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800266
267 /** @brief Listen the PendingAttributes property of the D-Bus interface and
268 * update BaseBIOSTable
269 */
270 void listenPendingAttributes();
271
272 /** @brief Find attribute handle from bios attribute table
273 * @param[in] attrName - attribute name
274 * @return attribute handle
275 */
276 uint16_t findAttrHandle(const std::string& attrName);
277
278 /** @brief Listen the PendingAttributes property of the D-Bus interface
279 * and update BaseBIOSTable
280 * @param[in] msg - Data associated with subscribed signal
281 */
282 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800283};
284
285} // namespace bios
286} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500287} // namespace pldm