blob: 36a7739dddc3cd75c25b0e3a08310f8ed3f7a21d [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>;
61
John Wangd9659342020-02-27 16:46:05 +080062/** @class BIOSConfig
63 * @brief Manager BIOS Attributes
64 */
65class BIOSConfig
66{
67 public:
68 BIOSConfig() = delete;
69 BIOSConfig(const BIOSConfig&) = delete;
70 BIOSConfig(BIOSConfig&&) = delete;
71 BIOSConfig& operator=(const BIOSConfig&) = delete;
72 BIOSConfig& operator=(BIOSConfig&&) = delete;
73 ~BIOSConfig() = default;
74
75 /** @brief Construct BIOSConfig
76 * @param[in] jsonDir - The directory where json file exists
77 * @param[in] tableDir - The directory where the persistent table is placed
78 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053079 * @param[in] fd - socket descriptor to communicate to host
80 * @param[in] eid - MCTP EID of host firmware
Andrew Jefferya330b2f2023-05-04 14:55:37 +093081 * @param[in] instanceIdDb - pointer to an InstanceIdDb object
Sampa Misrac0c79482021-06-02 08:01:54 -050082 * @param[in] handler - PLDM request handler
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060083 * @param[in] platformConfigHandler - pointer to platform config Handler
John Wangd9659342020-02-27 16:46:05 +080084 */
Sampa Misrac0c79482021-06-02 08:01:54 -050085 explicit BIOSConfig(
86 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040087 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093088 pldm::InstanceIdDb* instanceIdDb,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050089 pldm::requester::Handler<pldm::requester::Request>* handler,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060090 pldm::responder::platform_config::Handler* platformConfigHandler);
John Wangd9659342020-02-27 16:46:05 +080091
92 /** @brief Set attribute value on dbus and attribute value table
93 * @param[in] entry - attribute value entry
94 * @param[in] size - size of the attribute value entry
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060095 * @param[in] isBMC - indicates if the attribute is set by BMC
George Liu6d6d1e82021-02-16 11:08:55 +080096 * @param[in] updateDBus - update Attr value D-Bus property
97 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053098 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
99 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +0800100 * @return pldm_completion_codes
101 */
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600102 int setAttrValue(const void* entry, size_t size, bool isBMC,
103 bool updateDBus = true, bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +0800104
105 /** @brief Remove the persistent tables */
106 void removeTables();
107
108 /** @brief Build bios tables(string,attribute,attribute value table)*/
109 void buildTables();
110
111 /** @brief Get BIOS table of specified type
112 * @param[in] tableType - The table type
113 * @return The bios table, std::nullopt if the table is unaviliable
114 */
115 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
116
George Liu1b180d82020-07-23 14:01:58 +0800117 /** @brief set BIOS table
118 * @param[in] tableType - Indicates what table is being transferred
119 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
120 * BIOSAttributeValueTable=0x2}
121 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530122 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
123 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800124 * @return pldm_completion_codes
125 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530126 int setBIOSTable(uint8_t tableType, const Table& table,
127 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800128
John Wangd9659342020-02-27 16:46:05 +0800129 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530130 /** @enum Index into the fields in the BaseBIOSTable
131 */
132 enum class Index : uint8_t
133 {
134 attributeType = 0,
135 readOnly,
136 displayName,
137 description,
138 menuPath,
139 currentValue,
140 defaultValue,
141 options,
142 };
143
John Wangd9659342020-02-27 16:46:05 +0800144 const fs::path jsonDir;
145 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400146 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800147 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800148
Tom Joseph7f839f92020-09-21 10:20:44 +0530149 /** @brief socket descriptor to communicate to host */
150 int fd;
151
152 /** @brief MCTP EID of host firmware */
153 uint8_t eid;
154
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930155 /** @brief pointer to an Instance ID database object, used to obtain PLDM
156 * instance IDs.
Tom Joseph7f839f92020-09-21 10:20:44 +0530157 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930158 pldm::InstanceIdDb* instanceIdDb;
Tom Joseph7f839f92020-09-21 10:20:44 +0530159
Sampa Misrac0c79482021-06-02 08:01:54 -0500160 /** @brief PLDM request handler */
161 pldm::requester::Handler<pldm::requester::Request>* handler;
162
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600163 /** @brief platform config Handler*/
164 pldm::responder::platform_config::Handler* platformConfigHandler;
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500165
John Wangd9659342020-02-27 16:46:05 +0800166 // vector persists all attributes
167 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
168 BIOSAttributes biosAttributes;
169
Sampa Misra46ece062020-03-18 07:17:44 -0500170 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400171 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500172
Matt Spinlerd987c942023-03-24 10:18:19 -0500173 using ifaceName = std::string;
174 using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>;
175
Sampa Misra46ece062020-03-18 07:17:44 -0500176 // vector to catch the D-Bus property change signals for BIOS attributes
Patrick Williams84b790c2022-07-22 19:26:56 -0500177 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
Sampa Misra46ece062020-03-18 07:17:44 -0500178
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500179 /** @brief system type/model */
180 std::string sysType;
181
Sampa Misra46ece062020-03-18 07:17:44 -0500182 /** @brief Method to update a BIOS attribute when the corresponding Dbus
183 * property is changed
184 * @param[in] chProperties - list of properties which have changed
185 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
186 * biosAttributes
187 * @return - none
188 */
189 void processBiosAttrChangeNotification(
190 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
191
John Wangd9659342020-02-27 16:46:05 +0800192 /** @brief Construct an attribute and persist it
193 * @tparam T - attribute type
194 * @param[in] entry - json entry
195 */
196 template <typename T>
197 void constructAttribute(const Json& entry)
198 {
199 try
200 {
201 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500202 auto biosAttrIndex = biosAttributes.size() - 1;
203 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
204
205 if (dBusMap.has_value())
206 {
207 using namespace sdbusplus::bus::match::rules;
208 biosAttrMatch.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500209 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misra46ece062020-03-18 07:17:44 -0500210 pldm::utils::DBusHandler::getBus(),
211 propertiesChanged(dBusMap->objectPath,
212 dBusMap->interface),
Patrick Williams84b790c2022-07-22 19:26:56 -0500213 [this, biosAttrIndex](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500214 DbusChObjProperties props;
215 std::string iface;
216 msg.read(iface, props);
217 processBiosAttrChangeNotification(props, biosAttrIndex);
Patrick Williamsa6756622023-10-20 11:19:15 -0500218 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500219
220 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500221 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500222 pldm::utils::DBusHandler::getBus(),
223 interfacesAdded() + argNpath(0, dBusMap->objectPath),
224 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500225 sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500226 sdbusplus::message::object_path path;
227 DbusIfacesAdded interfaces;
Matt Spinlerd987c942023-03-24 10:18:19 -0500228
Patrick Williams6da4f912023-05-10 07:50:53 -0500229 msg.read(path, interfaces);
230 auto ifaceIt = interfaces.find(interface);
231 if (ifaceIt != interfaces.end())
232 {
233 processBiosAttrChangeNotification(ifaceIt->second,
234 biosAttrIndex);
235 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500236 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500237 }
John Wangd9659342020-02-27 16:46:05 +0800238 }
239 catch (const std::exception& e)
240 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600241 error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
242 e.what());
John Wangd9659342020-02-27 16:46:05 +0800243 }
244 }
245
246 /** Construct attributes and persist them */
247 void constructAttributes();
248
249 using ParseHandler = std::function<void(const Json& entry)>;
250
251 /** @brief Helper function to parse json
252 * @param[in] filePath - Path of json file
253 * @param[in] handler - Handler to process each entry in the json
254 */
255 void load(const fs::path& filePath, ParseHandler handler);
256
257 /** @brief Build String Table and persist it
258 * @return The built string table, std::nullopt if it fails.
259 */
260 std::optional<Table> buildAndStoreStringTable();
261
Tom Josephca7b2522020-11-18 12:27:11 +0530262 /** @brief Build attribute table and attribute value table and persist them
263 * Read the BaseBIOSTable from the bios-settings-manager and update
264 * attribute table and attribute value table.
265 *
John Wangd9659342020-02-27 16:46:05 +0800266 * @param[in] stringTable - The string Table
267 */
268 void buildAndStoreAttrTables(const Table& stringTable);
269
270 /** @brief Persist the table
271 * @param[in] path - Path to persist the table
272 * @param[in] table - The table
273 */
274 void storeTable(const fs::path& path, const Table& table);
275
276 /** @brief Load bios table to ram
277 * @param[in] path - Path of the table
278 * @return The table, std::nullopt if loading fails
279 */
280 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800281
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600282 /** @brief Method to decode the attribute name from the string handle
283 *
284 * @param[in] stringEntry - string entry from string table
285 * @return the decoded string
286 */
287 std::string decodeStringFromStringEntry(
288 const pldm_bios_string_table_entry* stringEntry);
289
290 /** @brief Method to print the string Handle by passing the attribute Handle
291 * of the bios attribute that got updated
292 *
293 * @param[in] handle - the Attribute handle of the bios attribute
294 * @param[in] index - index to the possible value handles
295 * @param[in] attrTable - the attribute table
296 * @param[in] stringTable - the string table
297 * @return string handle from the string table and decoded string to the
298 * name handle
299 */
300 std::string displayStringHandle(uint16_t handle, uint8_t index,
301 const std::optional<Table>& attrTable,
302 const std::optional<Table>& stringTable);
303
304 /** @brief Method to trace the bios attribute which got changed
305 *
306 * @param[in] attrValueEntry - The attribute value entry to update
307 * @param[in] attrEntry - The attribute table entry
308 * @param[in] isBMC - indicates if the attribute is set by BMC
309 */
310 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
311 const pldm_bios_attr_table_entry* attrEntry,
312 bool isBMC);
313
John Wang8241b342020-06-05 10:49:17 +0800314 /** @brief Check the attribute value to update
315 * @param[in] attrValueEntry - The attribute value entry to update
316 * @param[in] attrEntry - The attribute table entry
317 * @param[in] stringTable - The string table
318 * @return pldm_completion_codes
319 */
320 int checkAttrValueToUpdate(
321 const pldm_bios_attr_val_table_entry* attrValueEntry,
322 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800323
324 /** @brief Check the attribute table
325 * @param[in] table - The table
326 * @return pldm_completion_codes
327 */
328 int checkAttributeTable(const Table& table);
329
330 /** @brief Check the attribute value table
331 * @param[in] table - The table
332 * @return pldm_completion_codes
333 */
334 int checkAttributeValueTable(const Table& table);
335
336 /** @brief Update the BaseBIOSTable property of the D-Bus interface
337 */
338 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800339
340 /** @brief Listen the PendingAttributes property of the D-Bus interface and
341 * update BaseBIOSTable
342 */
343 void listenPendingAttributes();
344
345 /** @brief Find attribute handle from bios attribute table
346 * @param[in] attrName - attribute name
347 * @return attribute handle
348 */
349 uint16_t findAttrHandle(const std::string& attrName);
350
351 /** @brief Listen the PendingAttributes property of the D-Bus interface
352 * and update BaseBIOSTable
353 * @param[in] msg - Data associated with subscribed signal
354 */
355 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800356};
357
358} // namespace bios
359} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500360} // namespace pldm