blob: 10cc0130f441039eb416bc989059305733abb005 [file] [log] [blame]
John Wangd9659342020-02-27 16:46:05 +08001#pragma once
2
3#include "bios_attribute.hpp"
4#include "bios_table.hpp"
Andrew Jeffery2abbce72023-10-18 10:17:35 +10305#include "common/instance_id.hpp"
Sagar Srinivas11ce8d22022-07-28 11:32:34 -05006#include "oem_handler.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05007#include "requester/handler.hpp"
John Wangd9659342020-02-27 16:46:05 +08008
George Liuc453e162022-12-21 17:16:23 +08009#include <libpldm/bios_table.h>
10
George Liu6492f522020-06-16 10:34:05 +080011#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060012#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080013
John Wangd9659342020-02-27 16:46:05 +080014#include <functional>
15#include <iostream>
16#include <memory>
John Wangd9659342020-02-27 16:46:05 +080017#include <optional>
18#include <set>
19#include <string>
20#include <vector>
21
Riya Dixit49cfb132023-03-02 04:26:53 -060022PHOSPHOR_LOG2_USING;
23
John Wangd9659342020-02-27 16:46:05 +080024namespace pldm
25{
26namespace responder
27{
28namespace bios
29{
George Liu1b180d82020-07-23 14:01:58 +080030enum class BoundType
31{
32 LowerBound,
33 UpperBound,
34 ScalarIncrement,
35 MinStringLength,
36 MaxStringLength,
37 OneOf
38};
39
40using AttributeName = std::string;
41using AttributeType = std::string;
42using ReadonlyStatus = bool;
43using DisplayName = std::string;
44using Description = std::string;
45using MenuPath = std::string;
46using CurrentValue = std::variant<int64_t, std::string>;
47using DefaultValue = std::variant<int64_t, std::string>;
48using OptionString = std::string;
49using OptionValue = std::variant<int64_t, std::string>;
Sagar Srinivas7927f902023-10-09 07:53:00 -050050using ValueDisplayName = std::string;
51using Option =
52 std::vector<std::tuple<OptionString, OptionValue, ValueDisplayName>>;
George Liu1b180d82020-07-23 14:01:58 +080053using BIOSTableObj =
54 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
55 MenuPath, CurrentValue, DefaultValue, Option>;
56using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
57
George Liu1244acf2020-08-14 09:11:11 +080058using PendingObj = std::tuple<AttributeType, CurrentValue>;
59using PendingAttributes = std::map<AttributeName, PendingObj>;
60
John Wangd9659342020-02-27 16:46:05 +080061/** @class BIOSConfig
62 * @brief Manager BIOS Attributes
63 */
64class BIOSConfig
65{
66 public:
67 BIOSConfig() = delete;
68 BIOSConfig(const BIOSConfig&) = delete;
69 BIOSConfig(BIOSConfig&&) = delete;
70 BIOSConfig& operator=(const BIOSConfig&) = delete;
71 BIOSConfig& operator=(BIOSConfig&&) = delete;
72 ~BIOSConfig() = default;
73
74 /** @brief Construct BIOSConfig
75 * @param[in] jsonDir - The directory where json file exists
76 * @param[in] tableDir - The directory where the persistent table is placed
77 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053078 * @param[in] fd - socket descriptor to communicate to host
79 * @param[in] eid - MCTP EID of host firmware
Andrew Jefferya330b2f2023-05-04 14:55:37 +093080 * @param[in] instanceIdDb - pointer to an InstanceIdDb object
Sampa Misrac0c79482021-06-02 08:01:54 -050081 * @param[in] handler - PLDM request handler
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050082 * @param[in] oemBiosHandler - pointer to oem Bios Handler
John Wangd9659342020-02-27 16:46:05 +080083 */
Sampa Misrac0c79482021-06-02 08:01:54 -050084 explicit BIOSConfig(
85 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040086 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093087 pldm::InstanceIdDb* instanceIdDb,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050088 pldm::requester::Handler<pldm::requester::Request>* handler,
89 pldm::responder::oem_bios::Handler* oemBiosHandler);
John Wangd9659342020-02-27 16:46:05 +080090
91 /** @brief Set attribute value on dbus and attribute value table
92 * @param[in] entry - attribute value entry
93 * @param[in] size - size of the attribute value entry
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060094 * @param[in] isBMC - indicates if the attribute is set by BMC
George Liu6d6d1e82021-02-16 11:08:55 +080095 * @param[in] updateDBus - update Attr value D-Bus property
96 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053097 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
98 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080099 * @return pldm_completion_codes
100 */
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600101 int setAttrValue(const void* entry, size_t size, bool isBMC,
102 bool updateDBus = true, bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +0800103
104 /** @brief Remove the persistent tables */
105 void removeTables();
106
107 /** @brief Build bios tables(string,attribute,attribute value table)*/
108 void buildTables();
109
110 /** @brief Get BIOS table of specified type
111 * @param[in] tableType - The table type
112 * @return The bios table, std::nullopt if the table is unaviliable
113 */
114 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
115
George Liu1b180d82020-07-23 14:01:58 +0800116 /** @brief set BIOS table
117 * @param[in] tableType - Indicates what table is being transferred
118 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
119 * BIOSAttributeValueTable=0x2}
120 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530121 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
122 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800123 * @return pldm_completion_codes
124 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530125 int setBIOSTable(uint8_t tableType, const Table& table,
126 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800127
John Wangd9659342020-02-27 16:46:05 +0800128 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530129 /** @enum Index into the fields in the BaseBIOSTable
130 */
131 enum class Index : uint8_t
132 {
133 attributeType = 0,
134 readOnly,
135 displayName,
136 description,
137 menuPath,
138 currentValue,
139 defaultValue,
140 options,
141 };
142
John Wangd9659342020-02-27 16:46:05 +0800143 const fs::path jsonDir;
144 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400145 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800146 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800147
Tom Joseph7f839f92020-09-21 10:20:44 +0530148 /** @brief socket descriptor to communicate to host */
149 int fd;
150
151 /** @brief MCTP EID of host firmware */
152 uint8_t eid;
153
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930154 /** @brief pointer to an Instance ID database object, used to obtain PLDM
155 * instance IDs.
Tom Joseph7f839f92020-09-21 10:20:44 +0530156 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930157 pldm::InstanceIdDb* instanceIdDb;
Tom Joseph7f839f92020-09-21 10:20:44 +0530158
Sampa Misrac0c79482021-06-02 08:01:54 -0500159 /** @brief PLDM request handler */
160 pldm::requester::Handler<pldm::requester::Request>* handler;
161
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500162 /** @brief oem Bios Handler*/
163 pldm::responder::oem_bios::Handler* oemBiosHandler;
164
John Wangd9659342020-02-27 16:46:05 +0800165 // vector persists all attributes
166 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
167 BIOSAttributes biosAttributes;
168
Sampa Misra46ece062020-03-18 07:17:44 -0500169 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400170 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500171
Matt Spinlerd987c942023-03-24 10:18:19 -0500172 using ifaceName = std::string;
173 using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>;
174
Sampa Misra46ece062020-03-18 07:17:44 -0500175 // vector to catch the D-Bus property change signals for BIOS attributes
Patrick Williams84b790c2022-07-22 19:26:56 -0500176 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
Sampa Misra46ece062020-03-18 07:17:44 -0500177
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500178 /** @brief system type/model */
179 std::string sysType;
180
Sampa Misra46ece062020-03-18 07:17:44 -0500181 /** @brief Method to update a BIOS attribute when the corresponding Dbus
182 * property is changed
183 * @param[in] chProperties - list of properties which have changed
184 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
185 * biosAttributes
186 * @return - none
187 */
188 void processBiosAttrChangeNotification(
189 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
190
John Wangd9659342020-02-27 16:46:05 +0800191 /** @brief Construct an attribute and persist it
192 * @tparam T - attribute type
193 * @param[in] entry - json entry
194 */
195 template <typename T>
196 void constructAttribute(const Json& entry)
197 {
198 try
199 {
200 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500201 auto biosAttrIndex = biosAttributes.size() - 1;
202 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
203
204 if (dBusMap.has_value())
205 {
206 using namespace sdbusplus::bus::match::rules;
207 biosAttrMatch.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500208 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misra46ece062020-03-18 07:17:44 -0500209 pldm::utils::DBusHandler::getBus(),
210 propertiesChanged(dBusMap->objectPath,
211 dBusMap->interface),
Patrick Williams84b790c2022-07-22 19:26:56 -0500212 [this, biosAttrIndex](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500213 DbusChObjProperties props;
214 std::string iface;
215 msg.read(iface, props);
216 processBiosAttrChangeNotification(props, biosAttrIndex);
Patrick Williamsa6756622023-10-20 11:19:15 -0500217 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500218
219 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500220 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500221 pldm::utils::DBusHandler::getBus(),
222 interfacesAdded() + argNpath(0, dBusMap->objectPath),
223 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500224 sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500225 sdbusplus::message::object_path path;
226 DbusIfacesAdded interfaces;
Matt Spinlerd987c942023-03-24 10:18:19 -0500227
Patrick Williams6da4f912023-05-10 07:50:53 -0500228 msg.read(path, interfaces);
229 auto ifaceIt = interfaces.find(interface);
230 if (ifaceIt != interfaces.end())
231 {
232 processBiosAttrChangeNotification(ifaceIt->second,
233 biosAttrIndex);
234 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500235 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500236 }
John Wangd9659342020-02-27 16:46:05 +0800237 }
238 catch (const std::exception& e)
239 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600240 error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
241 e.what());
John Wangd9659342020-02-27 16:46:05 +0800242 }
243 }
244
245 /** Construct attributes and persist them */
246 void constructAttributes();
247
248 using ParseHandler = std::function<void(const Json& entry)>;
249
250 /** @brief Helper function to parse json
251 * @param[in] filePath - Path of json file
252 * @param[in] handler - Handler to process each entry in the json
253 */
254 void load(const fs::path& filePath, ParseHandler handler);
255
256 /** @brief Build String Table and persist it
257 * @return The built string table, std::nullopt if it fails.
258 */
259 std::optional<Table> buildAndStoreStringTable();
260
Tom Josephca7b2522020-11-18 12:27:11 +0530261 /** @brief Build attribute table and attribute value table and persist them
262 * Read the BaseBIOSTable from the bios-settings-manager and update
263 * attribute table and attribute value table.
264 *
John Wangd9659342020-02-27 16:46:05 +0800265 * @param[in] stringTable - The string Table
266 */
267 void buildAndStoreAttrTables(const Table& stringTable);
268
269 /** @brief Persist the table
270 * @param[in] path - Path to persist the table
271 * @param[in] table - The table
272 */
273 void storeTable(const fs::path& path, const Table& table);
274
275 /** @brief Load bios table to ram
276 * @param[in] path - Path of the table
277 * @return The table, std::nullopt if loading fails
278 */
279 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800280
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600281 /** @brief Method to decode the attribute name from the string handle
282 *
283 * @param[in] stringEntry - string entry from string table
284 * @return the decoded string
285 */
286 std::string decodeStringFromStringEntry(
287 const pldm_bios_string_table_entry* stringEntry);
288
289 /** @brief Method to print the string Handle by passing the attribute Handle
290 * of the bios attribute that got updated
291 *
292 * @param[in] handle - the Attribute handle of the bios attribute
293 * @param[in] index - index to the possible value handles
294 * @param[in] attrTable - the attribute table
295 * @param[in] stringTable - the string table
296 * @return string handle from the string table and decoded string to the
297 * name handle
298 */
299 std::string displayStringHandle(uint16_t handle, uint8_t index,
300 const std::optional<Table>& attrTable,
301 const std::optional<Table>& stringTable);
302
303 /** @brief Method to trace the bios attribute which got changed
304 *
305 * @param[in] attrValueEntry - The attribute value entry to update
306 * @param[in] attrEntry - The attribute table entry
307 * @param[in] isBMC - indicates if the attribute is set by BMC
308 */
309 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
310 const pldm_bios_attr_table_entry* attrEntry,
311 bool isBMC);
312
John Wang8241b342020-06-05 10:49:17 +0800313 /** @brief Check the attribute value to update
314 * @param[in] attrValueEntry - The attribute value entry to update
315 * @param[in] attrEntry - The attribute table entry
316 * @param[in] stringTable - The string table
317 * @return pldm_completion_codes
318 */
319 int checkAttrValueToUpdate(
320 const pldm_bios_attr_val_table_entry* attrValueEntry,
321 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800322
323 /** @brief Check the attribute table
324 * @param[in] table - The table
325 * @return pldm_completion_codes
326 */
327 int checkAttributeTable(const Table& table);
328
329 /** @brief Check the attribute value table
330 * @param[in] table - The table
331 * @return pldm_completion_codes
332 */
333 int checkAttributeValueTable(const Table& table);
334
335 /** @brief Update the BaseBIOSTable property of the D-Bus interface
336 */
337 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800338
339 /** @brief Listen the PendingAttributes property of the D-Bus interface and
340 * update BaseBIOSTable
341 */
342 void listenPendingAttributes();
343
344 /** @brief Find attribute handle from bios attribute table
345 * @param[in] attrName - attribute name
346 * @return attribute handle
347 */
348 uint16_t findAttrHandle(const std::string& attrName);
349
350 /** @brief Listen the PendingAttributes property of the D-Bus interface
351 * and update BaseBIOSTable
352 * @param[in] msg - Data associated with subscribed signal
353 */
354 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800355};
356
357} // namespace bios
358} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500359} // namespace pldm