blob: 77a0764dfb55655b471f27419cae406b6d6494bd [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>;
50using Option = std::vector<std::tuple<OptionString, OptionValue>>;
51using BIOSTableObj =
52 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
53 MenuPath, CurrentValue, DefaultValue, Option>;
54using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
55
George Liu1244acf2020-08-14 09:11:11 +080056using PendingObj = std::tuple<AttributeType, CurrentValue>;
57using PendingAttributes = std::map<AttributeName, PendingObj>;
58
John Wangd9659342020-02-27 16:46:05 +080059/** @class BIOSConfig
60 * @brief Manager BIOS Attributes
61 */
62class BIOSConfig
63{
64 public:
65 BIOSConfig() = delete;
66 BIOSConfig(const BIOSConfig&) = delete;
67 BIOSConfig(BIOSConfig&&) = delete;
68 BIOSConfig& operator=(const BIOSConfig&) = delete;
69 BIOSConfig& operator=(BIOSConfig&&) = delete;
70 ~BIOSConfig() = default;
71
72 /** @brief Construct BIOSConfig
73 * @param[in] jsonDir - The directory where json file exists
74 * @param[in] tableDir - The directory where the persistent table is placed
75 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053076 * @param[in] fd - socket descriptor to communicate to host
77 * @param[in] eid - MCTP EID of host firmware
Andrew Jefferya330b2f2023-05-04 14:55:37 +093078 * @param[in] instanceIdDb - pointer to an InstanceIdDb object
Sampa Misrac0c79482021-06-02 08:01:54 -050079 * @param[in] handler - PLDM request handler
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050080 * @param[in] oemBiosHandler - pointer to oem Bios Handler
John Wangd9659342020-02-27 16:46:05 +080081 */
Sampa Misrac0c79482021-06-02 08:01:54 -050082 explicit BIOSConfig(
83 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040084 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093085 pldm::InstanceIdDb* instanceIdDb,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050086 pldm::requester::Handler<pldm::requester::Request>* handler,
87 pldm::responder::oem_bios::Handler* oemBiosHandler);
John Wangd9659342020-02-27 16:46:05 +080088
89 /** @brief Set attribute value on dbus and attribute value table
90 * @param[in] entry - attribute value entry
91 * @param[in] size - size of the attribute value entry
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060092 * @param[in] isBMC - indicates if the attribute is set by BMC
George Liu6d6d1e82021-02-16 11:08:55 +080093 * @param[in] updateDBus - update Attr value D-Bus property
94 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053095 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
96 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080097 * @return pldm_completion_codes
98 */
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060099 int setAttrValue(const void* entry, size_t size, bool isBMC,
100 bool updateDBus = true, bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +0800101
102 /** @brief Remove the persistent tables */
103 void removeTables();
104
105 /** @brief Build bios tables(string,attribute,attribute value table)*/
106 void buildTables();
107
108 /** @brief Get BIOS table of specified type
109 * @param[in] tableType - The table type
110 * @return The bios table, std::nullopt if the table is unaviliable
111 */
112 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
113
George Liu1b180d82020-07-23 14:01:58 +0800114 /** @brief set BIOS table
115 * @param[in] tableType - Indicates what table is being transferred
116 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
117 * BIOSAttributeValueTable=0x2}
118 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530119 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
120 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800121 * @return pldm_completion_codes
122 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530123 int setBIOSTable(uint8_t tableType, const Table& table,
124 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800125
John Wangd9659342020-02-27 16:46:05 +0800126 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530127 /** @enum Index into the fields in the BaseBIOSTable
128 */
129 enum class Index : uint8_t
130 {
131 attributeType = 0,
132 readOnly,
133 displayName,
134 description,
135 menuPath,
136 currentValue,
137 defaultValue,
138 options,
139 };
140
John Wangd9659342020-02-27 16:46:05 +0800141 const fs::path jsonDir;
142 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400143 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800144 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800145
Tom Joseph7f839f92020-09-21 10:20:44 +0530146 /** @brief socket descriptor to communicate to host */
147 int fd;
148
149 /** @brief MCTP EID of host firmware */
150 uint8_t eid;
151
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930152 /** @brief pointer to an Instance ID database object, used to obtain PLDM
153 * instance IDs.
Tom Joseph7f839f92020-09-21 10:20:44 +0530154 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930155 pldm::InstanceIdDb* instanceIdDb;
Tom Joseph7f839f92020-09-21 10:20:44 +0530156
Sampa Misrac0c79482021-06-02 08:01:54 -0500157 /** @brief PLDM request handler */
158 pldm::requester::Handler<pldm::requester::Request>* handler;
159
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500160 /** @brief oem Bios Handler*/
161 pldm::responder::oem_bios::Handler* oemBiosHandler;
162
John Wangd9659342020-02-27 16:46:05 +0800163 // vector persists all attributes
164 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
165 BIOSAttributes biosAttributes;
166
Sampa Misra46ece062020-03-18 07:17:44 -0500167 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400168 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500169
Matt Spinlerd987c942023-03-24 10:18:19 -0500170 using ifaceName = std::string;
171 using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>;
172
Sampa Misra46ece062020-03-18 07:17:44 -0500173 // vector to catch the D-Bus property change signals for BIOS attributes
Patrick Williams84b790c2022-07-22 19:26:56 -0500174 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
Sampa Misra46ece062020-03-18 07:17:44 -0500175
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500176 /** @brief system type/model */
177 std::string sysType;
178
Sampa Misra46ece062020-03-18 07:17:44 -0500179 /** @brief Method to update a BIOS attribute when the corresponding Dbus
180 * property is changed
181 * @param[in] chProperties - list of properties which have changed
182 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
183 * biosAttributes
184 * @return - none
185 */
186 void processBiosAttrChangeNotification(
187 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
188
John Wangd9659342020-02-27 16:46:05 +0800189 /** @brief Construct an attribute and persist it
190 * @tparam T - attribute type
191 * @param[in] entry - json entry
192 */
193 template <typename T>
194 void constructAttribute(const Json& entry)
195 {
196 try
197 {
198 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500199 auto biosAttrIndex = biosAttributes.size() - 1;
200 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
201
202 if (dBusMap.has_value())
203 {
204 using namespace sdbusplus::bus::match::rules;
205 biosAttrMatch.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500206 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misra46ece062020-03-18 07:17:44 -0500207 pldm::utils::DBusHandler::getBus(),
208 propertiesChanged(dBusMap->objectPath,
209 dBusMap->interface),
Patrick Williams84b790c2022-07-22 19:26:56 -0500210 [this, biosAttrIndex](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500211 DbusChObjProperties props;
212 std::string iface;
213 msg.read(iface, props);
214 processBiosAttrChangeNotification(props, biosAttrIndex);
Patrick Williamsa6756622023-10-20 11:19:15 -0500215 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500216
217 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500218 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500219 pldm::utils::DBusHandler::getBus(),
220 interfacesAdded() + argNpath(0, dBusMap->objectPath),
221 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500222 sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500223 sdbusplus::message::object_path path;
224 DbusIfacesAdded interfaces;
Matt Spinlerd987c942023-03-24 10:18:19 -0500225
Patrick Williams6da4f912023-05-10 07:50:53 -0500226 msg.read(path, interfaces);
227 auto ifaceIt = interfaces.find(interface);
228 if (ifaceIt != interfaces.end())
229 {
230 processBiosAttrChangeNotification(ifaceIt->second,
231 biosAttrIndex);
232 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500233 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500234 }
John Wangd9659342020-02-27 16:46:05 +0800235 }
236 catch (const std::exception& e)
237 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600238 error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
239 e.what());
John Wangd9659342020-02-27 16:46:05 +0800240 }
241 }
242
243 /** Construct attributes and persist them */
244 void constructAttributes();
245
246 using ParseHandler = std::function<void(const Json& entry)>;
247
248 /** @brief Helper function to parse json
249 * @param[in] filePath - Path of json file
250 * @param[in] handler - Handler to process each entry in the json
251 */
252 void load(const fs::path& filePath, ParseHandler handler);
253
254 /** @brief Build String Table and persist it
255 * @return The built string table, std::nullopt if it fails.
256 */
257 std::optional<Table> buildAndStoreStringTable();
258
Tom Josephca7b2522020-11-18 12:27:11 +0530259 /** @brief Build attribute table and attribute value table and persist them
260 * Read the BaseBIOSTable from the bios-settings-manager and update
261 * attribute table and attribute value table.
262 *
John Wangd9659342020-02-27 16:46:05 +0800263 * @param[in] stringTable - The string Table
264 */
265 void buildAndStoreAttrTables(const Table& stringTable);
266
267 /** @brief Persist the table
268 * @param[in] path - Path to persist the table
269 * @param[in] table - The table
270 */
271 void storeTable(const fs::path& path, const Table& table);
272
273 /** @brief Load bios table to ram
274 * @param[in] path - Path of the table
275 * @return The table, std::nullopt if loading fails
276 */
277 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800278
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600279 /** @brief Method to decode the attribute name from the string handle
280 *
281 * @param[in] stringEntry - string entry from string table
282 * @return the decoded string
283 */
284 std::string decodeStringFromStringEntry(
285 const pldm_bios_string_table_entry* stringEntry);
286
287 /** @brief Method to print the string Handle by passing the attribute Handle
288 * of the bios attribute that got updated
289 *
290 * @param[in] handle - the Attribute handle of the bios attribute
291 * @param[in] index - index to the possible value handles
292 * @param[in] attrTable - the attribute table
293 * @param[in] stringTable - the string table
294 * @return string handle from the string table and decoded string to the
295 * name handle
296 */
297 std::string displayStringHandle(uint16_t handle, uint8_t index,
298 const std::optional<Table>& attrTable,
299 const std::optional<Table>& stringTable);
300
301 /** @brief Method to trace the bios attribute which got changed
302 *
303 * @param[in] attrValueEntry - The attribute value entry to update
304 * @param[in] attrEntry - The attribute table entry
305 * @param[in] isBMC - indicates if the attribute is set by BMC
306 */
307 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
308 const pldm_bios_attr_table_entry* attrEntry,
309 bool isBMC);
310
John Wang8241b342020-06-05 10:49:17 +0800311 /** @brief Check the attribute value to update
312 * @param[in] attrValueEntry - The attribute value entry to update
313 * @param[in] attrEntry - The attribute table entry
314 * @param[in] stringTable - The string table
315 * @return pldm_completion_codes
316 */
317 int checkAttrValueToUpdate(
318 const pldm_bios_attr_val_table_entry* attrValueEntry,
319 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800320
321 /** @brief Check the attribute table
322 * @param[in] table - The table
323 * @return pldm_completion_codes
324 */
325 int checkAttributeTable(const Table& table);
326
327 /** @brief Check the attribute value table
328 * @param[in] table - The table
329 * @return pldm_completion_codes
330 */
331 int checkAttributeValueTable(const Table& table);
332
333 /** @brief Update the BaseBIOSTable property of the D-Bus interface
334 */
335 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800336
337 /** @brief Listen the PendingAttributes property of the D-Bus interface and
338 * update BaseBIOSTable
339 */
340 void listenPendingAttributes();
341
342 /** @brief Find attribute handle from bios attribute table
343 * @param[in] attrName - attribute name
344 * @return attribute handle
345 */
346 uint16_t findAttrHandle(const std::string& attrName);
347
348 /** @brief Listen the PendingAttributes property of the D-Bus interface
349 * and update BaseBIOSTable
350 * @param[in] msg - Data associated with subscribed signal
351 */
352 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800353};
354
355} // namespace bios
356} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500357} // namespace pldm