blob: 7818ac4a81c6f9aaee72c5512d9ce72eb84ac859 [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"
Kamalkumar Patel3c50c822024-01-30 07:14:40 -06007#include "platform_config.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05008#include "requester/handler.hpp"
John Wangd9659342020-02-27 16:46:05 +08009
George Liuc453e162022-12-21 17:16:23 +080010#include <libpldm/bios_table.h>
11
George Liu6492f522020-06-16 10:34:05 +080012#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060013#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080014
John Wangd9659342020-02-27 16:46:05 +080015#include <functional>
16#include <iostream>
17#include <memory>
John Wangd9659342020-02-27 16:46:05 +080018#include <optional>
19#include <set>
20#include <string>
21#include <vector>
22
Riya Dixit49cfb132023-03-02 04:26:53 -060023PHOSPHOR_LOG2_USING;
24
John Wangd9659342020-02-27 16:46:05 +080025namespace pldm
26{
27namespace responder
28{
29namespace bios
30{
George Liu1b180d82020-07-23 14:01:58 +080031enum class BoundType
32{
33 LowerBound,
34 UpperBound,
35 ScalarIncrement,
36 MinStringLength,
37 MaxStringLength,
38 OneOf
39};
40
41using AttributeName = std::string;
42using AttributeType = std::string;
43using ReadonlyStatus = bool;
44using DisplayName = std::string;
45using Description = std::string;
46using MenuPath = std::string;
47using CurrentValue = std::variant<int64_t, std::string>;
48using DefaultValue = std::variant<int64_t, std::string>;
49using OptionString = std::string;
50using OptionValue = std::variant<int64_t, std::string>;
Sagar Srinivas7927f902023-10-09 07:53:00 -050051using ValueDisplayName = std::string;
52using Option =
53 std::vector<std::tuple<OptionString, OptionValue, ValueDisplayName>>;
George Liu1b180d82020-07-23 14:01:58 +080054using BIOSTableObj =
55 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
56 MenuPath, CurrentValue, DefaultValue, Option>;
57using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
58
George Liu1244acf2020-08-14 09:11:11 +080059using PendingObj = std::tuple<AttributeType, CurrentValue>;
60using PendingAttributes = std::map<AttributeName, PendingObj>;
Archana Kakani62dd8ff2024-02-12 10:00:40 -060061using Callback = std::function<void()>;
George Liu1244acf2020-08-14 09:11:11 +080062
John Wangd9659342020-02-27 16:46:05 +080063/** @class BIOSConfig
64 * @brief Manager BIOS Attributes
65 */
66class BIOSConfig
67{
68 public:
69 BIOSConfig() = delete;
70 BIOSConfig(const BIOSConfig&) = delete;
71 BIOSConfig(BIOSConfig&&) = delete;
72 BIOSConfig& operator=(const BIOSConfig&) = delete;
73 BIOSConfig& operator=(BIOSConfig&&) = delete;
74 ~BIOSConfig() = default;
75
76 /** @brief Construct BIOSConfig
77 * @param[in] jsonDir - The directory where json file exists
78 * @param[in] tableDir - The directory where the persistent table is placed
79 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053080 * @param[in] fd - socket descriptor to communicate to host
81 * @param[in] eid - MCTP EID of host firmware
Andrew Jefferya330b2f2023-05-04 14:55:37 +093082 * @param[in] instanceIdDb - pointer to an InstanceIdDb object
Sampa Misrac0c79482021-06-02 08:01:54 -050083 * @param[in] handler - PLDM request handler
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060084 * @param[in] platformConfigHandler - pointer to platform config Handler
Archana Kakani62dd8ff2024-02-12 10:00:40 -060085 * @param[in] requestPLDMServiceName - Callback for claiming the PLDM
86 * service name Called only after building BIOS tables.
John Wangd9659342020-02-27 16:46:05 +080087 */
Sampa Misrac0c79482021-06-02 08:01:54 -050088 explicit BIOSConfig(
89 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040090 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093091 pldm::InstanceIdDb* instanceIdDb,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050092 pldm::requester::Handler<pldm::requester::Request>* handler,
Archana Kakani62dd8ff2024-02-12 10:00:40 -060093 pldm::responder::platform_config::Handler* platformConfigHandler,
94 pldm::responder::bios::Callback requestPLDMServiceName);
John Wangd9659342020-02-27 16:46:05 +080095
96 /** @brief Set attribute value on dbus and attribute value table
97 * @param[in] entry - attribute value entry
98 * @param[in] size - size of the attribute value entry
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060099 * @param[in] isBMC - indicates if the attribute is set by BMC
George Liu6d6d1e82021-02-16 11:08:55 +0800100 * @param[in] updateDBus - update Attr value D-Bus property
101 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +0530102 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
103 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +0800104 * @return pldm_completion_codes
105 */
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600106 int setAttrValue(const void* entry, size_t size, bool isBMC,
107 bool updateDBus = true, bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +0800108
109 /** @brief Remove the persistent tables */
110 void removeTables();
111
112 /** @brief Build bios tables(string,attribute,attribute value table)*/
113 void buildTables();
114
115 /** @brief Get BIOS table of specified type
116 * @param[in] tableType - The table type
117 * @return The bios table, std::nullopt if the table is unaviliable
118 */
119 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
120
George Liu1b180d82020-07-23 14:01:58 +0800121 /** @brief set BIOS table
122 * @param[in] tableType - Indicates what table is being transferred
123 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
124 * BIOSAttributeValueTable=0x2}
125 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530126 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
127 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800128 * @return pldm_completion_codes
129 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530130 int setBIOSTable(uint8_t tableType, const Table& table,
131 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800132
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600133 /** @brief Construct the BIOS Attributes and build the tables
134 * after receiving system type from entity manager.
Archana Kakani46f352e2024-03-17 08:21:08 -0500135 * Also register the Service Name only if
136 * System specific Bios attributes are supported
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600137 * @param[in] String - System Type
Archana Kakani46f352e2024-03-17 08:21:08 -0500138 * @param[in] bool - flag to register service name
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600139 * @return void
140 */
Archana Kakani46f352e2024-03-17 08:21:08 -0500141 void initBIOSAttributes(const std::string& sysType, bool registerService);
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600142
John Wangd9659342020-02-27 16:46:05 +0800143 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530144 /** @enum Index into the fields in the BaseBIOSTable
145 */
146 enum class Index : uint8_t
147 {
148 attributeType = 0,
149 readOnly,
150 displayName,
151 description,
152 menuPath,
153 currentValue,
154 defaultValue,
155 options,
156 };
157
John Wangd9659342020-02-27 16:46:05 +0800158 const fs::path jsonDir;
159 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400160 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800161 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800162
Tom Joseph7f839f92020-09-21 10:20:44 +0530163 /** @brief socket descriptor to communicate to host */
164 int fd;
165
166 /** @brief MCTP EID of host firmware */
167 uint8_t eid;
168
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930169 /** @brief pointer to an Instance ID database object, used to obtain PLDM
170 * instance IDs.
Tom Joseph7f839f92020-09-21 10:20:44 +0530171 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930172 pldm::InstanceIdDb* instanceIdDb;
Tom Joseph7f839f92020-09-21 10:20:44 +0530173
Sampa Misrac0c79482021-06-02 08:01:54 -0500174 /** @brief PLDM request handler */
175 pldm::requester::Handler<pldm::requester::Request>* handler;
176
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600177 /** @brief platform config Handler*/
178 pldm::responder::platform_config::Handler* platformConfigHandler;
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500179
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600180 /** @brief Callback for registering the PLDM service name */
181 pldm::responder::bios::Callback requestPLDMServiceName;
182
John Wangd9659342020-02-27 16:46:05 +0800183 // vector persists all attributes
184 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
185 BIOSAttributes biosAttributes;
186
Sampa Misra46ece062020-03-18 07:17:44 -0500187 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400188 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500189
Matt Spinlerd987c942023-03-24 10:18:19 -0500190 using ifaceName = std::string;
191 using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>;
192
Sampa Misra46ece062020-03-18 07:17:44 -0500193 // vector to catch the D-Bus property change signals for BIOS attributes
Patrick Williams84b790c2022-07-22 19:26:56 -0500194 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
Sampa Misra46ece062020-03-18 07:17:44 -0500195
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500196 /** @brief system type/model */
197 std::string sysType;
198
Sampa Misra46ece062020-03-18 07:17:44 -0500199 /** @brief Method to update a BIOS attribute when the corresponding Dbus
200 * property is changed
201 * @param[in] chProperties - list of properties which have changed
202 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
203 * biosAttributes
204 * @return - none
205 */
206 void processBiosAttrChangeNotification(
207 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
208
Archana Kakani46f352e2024-03-17 08:21:08 -0500209 /** @brief Method is used to initiate bios attributes only if system type
210 * is already populated by entity manager.
211 * Register the callback if system type is yet to be populated by Entity
212 * manager
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600213 */
Archana Kakani46f352e2024-03-17 08:21:08 -0500214 void checkSystemTypeAvailability();
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600215
John Wangd9659342020-02-27 16:46:05 +0800216 /** @brief Construct an attribute and persist it
217 * @tparam T - attribute type
218 * @param[in] entry - json entry
219 */
220 template <typename T>
221 void constructAttribute(const Json& entry)
222 {
223 try
224 {
225 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500226 auto biosAttrIndex = biosAttributes.size() - 1;
227 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
228
229 if (dBusMap.has_value())
230 {
231 using namespace sdbusplus::bus::match::rules;
232 biosAttrMatch.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500233 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misra46ece062020-03-18 07:17:44 -0500234 pldm::utils::DBusHandler::getBus(),
235 propertiesChanged(dBusMap->objectPath,
236 dBusMap->interface),
Patrick Williams84b790c2022-07-22 19:26:56 -0500237 [this, biosAttrIndex](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500238 DbusChObjProperties props;
239 std::string iface;
240 msg.read(iface, props);
241 processBiosAttrChangeNotification(props, biosAttrIndex);
Patrick Williamsa6756622023-10-20 11:19:15 -0500242 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500243
244 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500245 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500246 pldm::utils::DBusHandler::getBus(),
247 interfacesAdded() + argNpath(0, dBusMap->objectPath),
248 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500249 sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500250 sdbusplus::message::object_path path;
251 DbusIfacesAdded interfaces;
Matt Spinlerd987c942023-03-24 10:18:19 -0500252
Patrick Williams6da4f912023-05-10 07:50:53 -0500253 msg.read(path, interfaces);
254 auto ifaceIt = interfaces.find(interface);
255 if (ifaceIt != interfaces.end())
256 {
257 processBiosAttrChangeNotification(ifaceIt->second,
258 biosAttrIndex);
259 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500260 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500261 }
John Wangd9659342020-02-27 16:46:05 +0800262 }
263 catch (const std::exception& e)
264 {
Riya Dixit89644442024-03-31 05:39:59 -0500265 error("Failed to construct an attribute, error - {ERROR}", "ERROR",
266 e);
John Wangd9659342020-02-27 16:46:05 +0800267 }
268 }
269
270 /** Construct attributes and persist them */
271 void constructAttributes();
272
273 using ParseHandler = std::function<void(const Json& entry)>;
274
275 /** @brief Helper function to parse json
276 * @param[in] filePath - Path of json file
277 * @param[in] handler - Handler to process each entry in the json
278 */
279 void load(const fs::path& filePath, ParseHandler handler);
280
281 /** @brief Build String Table and persist it
282 * @return The built string table, std::nullopt if it fails.
283 */
284 std::optional<Table> buildAndStoreStringTable();
285
Tom Josephca7b2522020-11-18 12:27:11 +0530286 /** @brief Build attribute table and attribute value table and persist them
287 * Read the BaseBIOSTable from the bios-settings-manager and update
288 * attribute table and attribute value table.
289 *
John Wangd9659342020-02-27 16:46:05 +0800290 * @param[in] stringTable - The string Table
291 */
292 void buildAndStoreAttrTables(const Table& stringTable);
293
294 /** @brief Persist the table
295 * @param[in] path - Path to persist the table
296 * @param[in] table - The table
297 */
298 void storeTable(const fs::path& path, const Table& table);
299
300 /** @brief Load bios table to ram
301 * @param[in] path - Path of the table
302 * @return The table, std::nullopt if loading fails
303 */
304 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800305
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600306 /** @brief Method to decode the attribute name from the string handle
307 *
308 * @param[in] stringEntry - string entry from string table
309 * @return the decoded string
310 */
311 std::string decodeStringFromStringEntry(
312 const pldm_bios_string_table_entry* stringEntry);
313
314 /** @brief Method to print the string Handle by passing the attribute Handle
315 * of the bios attribute that got updated
316 *
317 * @param[in] handle - the Attribute handle of the bios attribute
318 * @param[in] index - index to the possible value handles
319 * @param[in] attrTable - the attribute table
320 * @param[in] stringTable - the string table
321 * @return string handle from the string table and decoded string to the
322 * name handle
323 */
324 std::string displayStringHandle(uint16_t handle, uint8_t index,
325 const std::optional<Table>& attrTable,
326 const std::optional<Table>& stringTable);
327
328 /** @brief Method to trace the bios attribute which got changed
329 *
330 * @param[in] attrValueEntry - The attribute value entry to update
331 * @param[in] attrEntry - The attribute table entry
332 * @param[in] isBMC - indicates if the attribute is set by BMC
333 */
334 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
335 const pldm_bios_attr_table_entry* attrEntry,
336 bool isBMC);
337
John Wang8241b342020-06-05 10:49:17 +0800338 /** @brief Check the attribute value to update
339 * @param[in] attrValueEntry - The attribute value entry to update
340 * @param[in] attrEntry - The attribute table entry
341 * @param[in] stringTable - The string table
342 * @return pldm_completion_codes
343 */
344 int checkAttrValueToUpdate(
345 const pldm_bios_attr_val_table_entry* attrValueEntry,
346 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800347
348 /** @brief Check the attribute table
349 * @param[in] table - The table
350 * @return pldm_completion_codes
351 */
352 int checkAttributeTable(const Table& table);
353
354 /** @brief Check the attribute value table
355 * @param[in] table - The table
356 * @return pldm_completion_codes
357 */
358 int checkAttributeValueTable(const Table& table);
359
360 /** @brief Update the BaseBIOSTable property of the D-Bus interface
361 */
362 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800363
364 /** @brief Listen the PendingAttributes property of the D-Bus interface and
365 * update BaseBIOSTable
366 */
367 void listenPendingAttributes();
368
369 /** @brief Find attribute handle from bios attribute table
370 * @param[in] attrName - attribute name
371 * @return attribute handle
372 */
373 uint16_t findAttrHandle(const std::string& attrName);
374
375 /** @brief Listen the PendingAttributes property of the D-Bus interface
376 * and update BaseBIOSTable
377 * @param[in] msg - Data associated with subscribed signal
378 */
379 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800380};
381
382} // namespace bios
383} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500384} // namespace pldm