blob: 18e13b00c2bbe3ca9d3b9320819ef119bde193ac [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"
Sampa Misrac0c79482021-06-02 08:01:54 -05008#include "requester/handler.hpp"
John Wangd9659342020-02-27 16:46:05 +08009
George Liu6492f522020-06-16 10:34:05 +080010#include <nlohmann/json.hpp>
11
John Wangd9659342020-02-27 16:46:05 +080012#include <functional>
13#include <iostream>
14#include <memory>
John Wangd9659342020-02-27 16:46:05 +080015#include <optional>
16#include <set>
17#include <string>
18#include <vector>
19
John Wangd9659342020-02-27 16:46:05 +080020namespace pldm
21{
22namespace responder
23{
24namespace bios
25{
26
George Liu1b180d82020-07-23 14:01:58 +080027enum class BoundType
28{
29 LowerBound,
30 UpperBound,
31 ScalarIncrement,
32 MinStringLength,
33 MaxStringLength,
34 OneOf
35};
36
37using AttributeName = std::string;
38using AttributeType = std::string;
39using ReadonlyStatus = bool;
40using DisplayName = std::string;
41using Description = std::string;
42using MenuPath = std::string;
43using CurrentValue = std::variant<int64_t, std::string>;
44using DefaultValue = std::variant<int64_t, std::string>;
45using OptionString = std::string;
46using OptionValue = std::variant<int64_t, std::string>;
47using Option = std::vector<std::tuple<OptionString, OptionValue>>;
48using BIOSTableObj =
49 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
50 MenuPath, CurrentValue, DefaultValue, Option>;
51using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
52
George Liu1244acf2020-08-14 09:11:11 +080053using PendingObj = std::tuple<AttributeType, CurrentValue>;
54using PendingAttributes = std::map<AttributeName, PendingObj>;
55
John Wangd9659342020-02-27 16:46:05 +080056/** @class BIOSConfig
57 * @brief Manager BIOS Attributes
58 */
59class BIOSConfig
60{
61 public:
62 BIOSConfig() = delete;
63 BIOSConfig(const BIOSConfig&) = delete;
64 BIOSConfig(BIOSConfig&&) = delete;
65 BIOSConfig& operator=(const BIOSConfig&) = delete;
66 BIOSConfig& operator=(BIOSConfig&&) = delete;
67 ~BIOSConfig() = default;
68
69 /** @brief Construct BIOSConfig
70 * @param[in] jsonDir - The directory where json file exists
71 * @param[in] tableDir - The directory where the persistent table is placed
72 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053073 * @param[in] fd - socket descriptor to communicate to host
74 * @param[in] eid - MCTP EID of host firmware
75 * @param[in] requester - pointer to Requester object
Sampa Misrac0c79482021-06-02 08:01:54 -050076 * @param[in] handler - PLDM request handler
John Wangd9659342020-02-27 16:46:05 +080077 */
Sampa Misrac0c79482021-06-02 08:01:54 -050078 explicit BIOSConfig(
79 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040080 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Sampa Misrac0c79482021-06-02 08:01:54 -050081 dbus_api::Requester* requester,
82 pldm::requester::Handler<pldm::requester::Request>* handler);
John Wangd9659342020-02-27 16:46:05 +080083
84 /** @brief Set attribute value on dbus and attribute value table
85 * @param[in] entry - attribute value entry
86 * @param[in] size - size of the attribute value entry
George Liu6d6d1e82021-02-16 11:08:55 +080087 * @param[in] updateDBus - update Attr value D-Bus property
88 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053089 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
90 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080091 * @return pldm_completion_codes
92 */
George Liu6d6d1e82021-02-16 11:08:55 +080093 int setAttrValue(const void* entry, size_t size, bool updateDBus = true,
Tom Joseph7f839f92020-09-21 10:20:44 +053094 bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +080095
96 /** @brief Remove the persistent tables */
97 void removeTables();
98
99 /** @brief Build bios tables(string,attribute,attribute value table)*/
100 void buildTables();
101
102 /** @brief Get BIOS table of specified type
103 * @param[in] tableType - The table type
104 * @return The bios table, std::nullopt if the table is unaviliable
105 */
106 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
107
George Liu1b180d82020-07-23 14:01:58 +0800108 /** @brief set BIOS table
109 * @param[in] tableType - Indicates what table is being transferred
110 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
111 * BIOSAttributeValueTable=0x2}
112 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530113 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
114 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800115 * @return pldm_completion_codes
116 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530117 int setBIOSTable(uint8_t tableType, const Table& table,
118 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800119
John Wangd9659342020-02-27 16:46:05 +0800120 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530121 /** @enum Index into the fields in the BaseBIOSTable
122 */
123 enum class Index : uint8_t
124 {
125 attributeType = 0,
126 readOnly,
127 displayName,
128 description,
129 menuPath,
130 currentValue,
131 defaultValue,
132 options,
133 };
134
John Wangd9659342020-02-27 16:46:05 +0800135 const fs::path jsonDir;
136 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400137 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800138 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800139
Tom Joseph7f839f92020-09-21 10:20:44 +0530140 /** @brief socket descriptor to communicate to host */
141 int fd;
142
143 /** @brief MCTP EID of host firmware */
144 uint8_t eid;
145
146 /** @brief pointer to Requester object, primarily used to access API to
147 * obtain PLDM instance id.
148 */
149 dbus_api::Requester* requester;
150
Sampa Misrac0c79482021-06-02 08:01:54 -0500151 /** @brief PLDM request handler */
152 pldm::requester::Handler<pldm::requester::Request>* handler;
153
John Wangd9659342020-02-27 16:46:05 +0800154 // vector persists all attributes
155 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
156 BIOSAttributes biosAttributes;
157
Sampa Misra46ece062020-03-18 07:17:44 -0500158 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400159 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500160
161 // vector to catch the D-Bus property change signals for BIOS attributes
162 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
163
164 /** @brief Method to update a BIOS attribute when the corresponding Dbus
165 * property is changed
166 * @param[in] chProperties - list of properties which have changed
167 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
168 * biosAttributes
169 * @return - none
170 */
171 void processBiosAttrChangeNotification(
172 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
173
John Wangd9659342020-02-27 16:46:05 +0800174 /** @brief Construct an attribute and persist it
175 * @tparam T - attribute type
176 * @param[in] entry - json entry
177 */
178 template <typename T>
179 void constructAttribute(const Json& entry)
180 {
181 try
182 {
183 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500184 auto biosAttrIndex = biosAttributes.size() - 1;
185 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
186
187 if (dBusMap.has_value())
188 {
189 using namespace sdbusplus::bus::match::rules;
190 biosAttrMatch.push_back(
191 std::make_unique<sdbusplus::bus::match::match>(
192 pldm::utils::DBusHandler::getBus(),
193 propertiesChanged(dBusMap->objectPath,
194 dBusMap->interface),
195 [this,
196 biosAttrIndex](sdbusplus::message::message& msg) {
197 DbusChObjProperties props;
198 std::string iface;
199 msg.read(iface, props);
200 processBiosAttrChangeNotification(props,
201 biosAttrIndex);
202 }));
203 }
John Wangd9659342020-02-27 16:46:05 +0800204 }
205 catch (const std::exception& e)
206 {
207 std::cerr << "Constructs Attribute Error, " << e.what()
208 << std::endl;
209 }
210 }
211
212 /** Construct attributes and persist them */
213 void constructAttributes();
214
215 using ParseHandler = std::function<void(const Json& entry)>;
216
217 /** @brief Helper function to parse json
218 * @param[in] filePath - Path of json file
219 * @param[in] handler - Handler to process each entry in the json
220 */
221 void load(const fs::path& filePath, ParseHandler handler);
222
223 /** @brief Build String Table and persist it
224 * @return The built string table, std::nullopt if it fails.
225 */
226 std::optional<Table> buildAndStoreStringTable();
227
Tom Josephca7b2522020-11-18 12:27:11 +0530228 /** @brief Build attribute table and attribute value table and persist them
229 * Read the BaseBIOSTable from the bios-settings-manager and update
230 * attribute table and attribute value table.
231 *
John Wangd9659342020-02-27 16:46:05 +0800232 * @param[in] stringTable - The string Table
233 */
234 void buildAndStoreAttrTables(const Table& stringTable);
235
236 /** @brief Persist the table
237 * @param[in] path - Path to persist the table
238 * @param[in] table - The table
239 */
240 void storeTable(const fs::path& path, const Table& table);
241
242 /** @brief Load bios table to ram
243 * @param[in] path - Path of the table
244 * @return The table, std::nullopt if loading fails
245 */
246 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800247
248 /** @brief Check the attribute value to update
249 * @param[in] attrValueEntry - The attribute value entry to update
250 * @param[in] attrEntry - The attribute table entry
251 * @param[in] stringTable - The string table
252 * @return pldm_completion_codes
253 */
254 int checkAttrValueToUpdate(
255 const pldm_bios_attr_val_table_entry* attrValueEntry,
256 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800257
258 /** @brief Check the attribute table
259 * @param[in] table - The table
260 * @return pldm_completion_codes
261 */
262 int checkAttributeTable(const Table& table);
263
264 /** @brief Check the attribute value table
265 * @param[in] table - The table
266 * @return pldm_completion_codes
267 */
268 int checkAttributeValueTable(const Table& table);
269
270 /** @brief Update the BaseBIOSTable property of the D-Bus interface
271 */
272 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800273
274 /** @brief Listen the PendingAttributes property of the D-Bus interface and
275 * update BaseBIOSTable
276 */
277 void listenPendingAttributes();
278
279 /** @brief Find attribute handle from bios attribute table
280 * @param[in] attrName - attribute name
281 * @return attribute handle
282 */
283 uint16_t findAttrHandle(const std::string& attrName);
284
285 /** @brief Listen the PendingAttributes property of the D-Bus interface
286 * and update BaseBIOSTable
287 * @param[in] msg - Data associated with subscribed signal
288 */
289 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800290};
291
292} // namespace bios
293} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500294} // namespace pldm