blob: 2d07593fe9497bb7d5452eed9ace7a3784f2566c [file] [log] [blame]
John Wangd9659342020-02-27 16:46:05 +08001#pragma once
2
3#include "bios_attribute.hpp"
4#include "bios_table.hpp"
Tom Joseph7f839f92020-09-21 10:20:44 +05305#include "pldmd/dbus_impl_requester.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05006#include "requester/handler.hpp"
John Wangd9659342020-02-27 16:46:05 +08007
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/bios_table.h>
9
George Liu6492f522020-06-16 10:34:05 +080010#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060011#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080012
John Wangd9659342020-02-27 16:46:05 +080013#include <functional>
14#include <iostream>
15#include <memory>
John Wangd9659342020-02-27 16:46:05 +080016#include <optional>
17#include <set>
18#include <string>
19#include <vector>
20
Riya Dixit49cfb132023-03-02 04:26:53 -060021PHOSPHOR_LOG2_USING;
22
John Wangd9659342020-02-27 16:46:05 +080023namespace pldm
24{
25namespace responder
26{
27namespace bios
28{
George Liu1b180d82020-07-23 14:01:58 +080029enum class BoundType
30{
31 LowerBound,
32 UpperBound,
33 ScalarIncrement,
34 MinStringLength,
35 MaxStringLength,
36 OneOf
37};
38
39using AttributeName = std::string;
40using AttributeType = std::string;
41using ReadonlyStatus = bool;
42using DisplayName = std::string;
43using Description = std::string;
44using MenuPath = std::string;
45using CurrentValue = std::variant<int64_t, std::string>;
46using DefaultValue = std::variant<int64_t, std::string>;
47using OptionString = std::string;
48using OptionValue = std::variant<int64_t, std::string>;
49using Option = std::vector<std::tuple<OptionString, OptionValue>>;
50using BIOSTableObj =
51 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
52 MenuPath, CurrentValue, DefaultValue, Option>;
53using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
54
George Liu1244acf2020-08-14 09:11:11 +080055using PendingObj = std::tuple<AttributeType, CurrentValue>;
56using PendingAttributes = std::map<AttributeName, PendingObj>;
57
John Wangd9659342020-02-27 16:46:05 +080058/** @class BIOSConfig
59 * @brief Manager BIOS Attributes
60 */
61class BIOSConfig
62{
63 public:
64 BIOSConfig() = delete;
65 BIOSConfig(const BIOSConfig&) = delete;
66 BIOSConfig(BIOSConfig&&) = delete;
67 BIOSConfig& operator=(const BIOSConfig&) = delete;
68 BIOSConfig& operator=(BIOSConfig&&) = delete;
69 ~BIOSConfig() = default;
70
71 /** @brief Construct BIOSConfig
72 * @param[in] jsonDir - The directory where json file exists
73 * @param[in] tableDir - The directory where the persistent table is placed
74 * @param[in] dbusHandler - Dbus Handler
Tom Joseph7f839f92020-09-21 10:20:44 +053075 * @param[in] fd - socket descriptor to communicate to host
76 * @param[in] eid - MCTP EID of host firmware
77 * @param[in] requester - pointer to Requester object
Sampa Misrac0c79482021-06-02 08:01:54 -050078 * @param[in] handler - PLDM request handler
John Wangd9659342020-02-27 16:46:05 +080079 */
Sampa Misrac0c79482021-06-02 08:01:54 -050080 explicit BIOSConfig(
81 const char* jsonDir, const char* tableDir,
Brad Bishop5079ac42021-08-19 18:35:06 -040082 pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid,
Sampa Misrac0c79482021-06-02 08:01:54 -050083 dbus_api::Requester* requester,
84 pldm::requester::Handler<pldm::requester::Request>* handler);
John Wangd9659342020-02-27 16:46:05 +080085
86 /** @brief Set attribute value on dbus and attribute value table
87 * @param[in] entry - attribute value entry
88 * @param[in] size - size of the attribute value entry
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060089 * @param[in] isBMC - indicates if the attribute is set by BMC
George Liu6d6d1e82021-02-16 11:08:55 +080090 * @param[in] updateDBus - update Attr value D-Bus property
91 * if this is set to true
Tom Joseph7f839f92020-09-21 10:20:44 +053092 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
93 * if this is set to true
John Wangd9659342020-02-27 16:46:05 +080094 * @return pldm_completion_codes
95 */
Sagar Srinivascac0ebb2021-11-23 07:50:28 -060096 int setAttrValue(const void* entry, size_t size, bool isBMC,
97 bool updateDBus = true, bool updateBaseBIOSTable = true);
John Wangd9659342020-02-27 16:46:05 +080098
99 /** @brief Remove the persistent tables */
100 void removeTables();
101
102 /** @brief Build bios tables(string,attribute,attribute value table)*/
103 void buildTables();
104
105 /** @brief Get BIOS table of specified type
106 * @param[in] tableType - The table type
107 * @return The bios table, std::nullopt if the table is unaviliable
108 */
109 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
110
George Liu1b180d82020-07-23 14:01:58 +0800111 /** @brief set BIOS table
112 * @param[in] tableType - Indicates what table is being transferred
113 * {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
114 * BIOSAttributeValueTable=0x2}
115 * @param[in] table - table data
Tom Joseph7f839f92020-09-21 10:20:44 +0530116 * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property
117 * if this is set to true
George Liu1b180d82020-07-23 14:01:58 +0800118 * @return pldm_completion_codes
119 */
Tom Joseph7f839f92020-09-21 10:20:44 +0530120 int setBIOSTable(uint8_t tableType, const Table& table,
121 bool updateBaseBIOSTable = true);
George Liu1b180d82020-07-23 14:01:58 +0800122
John Wangd9659342020-02-27 16:46:05 +0800123 private:
Tom Josephca7b2522020-11-18 12:27:11 +0530124 /** @enum Index into the fields in the BaseBIOSTable
125 */
126 enum class Index : uint8_t
127 {
128 attributeType = 0,
129 readOnly,
130 displayName,
131 description,
132 menuPath,
133 currentValue,
134 defaultValue,
135 options,
136 };
137
John Wangd9659342020-02-27 16:46:05 +0800138 const fs::path jsonDir;
139 const fs::path tableDir;
Brad Bishop5079ac42021-08-19 18:35:06 -0400140 pldm::utils::DBusHandler* const dbusHandler;
George Liu1b180d82020-07-23 14:01:58 +0800141 BaseBIOSTable baseBIOSTableMaps;
John Wangd9659342020-02-27 16:46:05 +0800142
Tom Joseph7f839f92020-09-21 10:20:44 +0530143 /** @brief socket descriptor to communicate to host */
144 int fd;
145
146 /** @brief MCTP EID of host firmware */
147 uint8_t eid;
148
149 /** @brief pointer to Requester object, primarily used to access API to
150 * obtain PLDM instance id.
151 */
152 dbus_api::Requester* requester;
153
Sampa Misrac0c79482021-06-02 08:01:54 -0500154 /** @brief PLDM request handler */
155 pldm::requester::Handler<pldm::requester::Request>* handler;
156
John Wangd9659342020-02-27 16:46:05 +0800157 // vector persists all attributes
158 using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
159 BIOSAttributes biosAttributes;
160
Sampa Misra46ece062020-03-18 07:17:44 -0500161 using propName = std::string;
Brad Bishop5079ac42021-08-19 18:35:06 -0400162 using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>;
Sampa Misra46ece062020-03-18 07:17:44 -0500163
Matt Spinlerd987c942023-03-24 10:18:19 -0500164 using ifaceName = std::string;
165 using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>;
166
Sampa Misra46ece062020-03-18 07:17:44 -0500167 // vector to catch the D-Bus property change signals for BIOS attributes
Patrick Williams84b790c2022-07-22 19:26:56 -0500168 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch;
Sampa Misra46ece062020-03-18 07:17:44 -0500169
170 /** @brief Method to update a BIOS attribute when the corresponding Dbus
171 * property is changed
172 * @param[in] chProperties - list of properties which have changed
173 * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
174 * biosAttributes
175 * @return - none
176 */
177 void processBiosAttrChangeNotification(
178 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
179
John Wangd9659342020-02-27 16:46:05 +0800180 /** @brief Construct an attribute and persist it
181 * @tparam T - attribute type
182 * @param[in] entry - json entry
183 */
184 template <typename T>
185 void constructAttribute(const Json& entry)
186 {
187 try
188 {
189 biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
Sampa Misra46ece062020-03-18 07:17:44 -0500190 auto biosAttrIndex = biosAttributes.size() - 1;
191 auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
192
193 if (dBusMap.has_value())
194 {
195 using namespace sdbusplus::bus::match::rules;
196 biosAttrMatch.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500197 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misra46ece062020-03-18 07:17:44 -0500198 pldm::utils::DBusHandler::getBus(),
199 propertiesChanged(dBusMap->objectPath,
200 dBusMap->interface),
Patrick Williams84b790c2022-07-22 19:26:56 -0500201 [this, biosAttrIndex](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500202 DbusChObjProperties props;
203 std::string iface;
204 msg.read(iface, props);
205 processBiosAttrChangeNotification(props, biosAttrIndex);
Sampa Misra46ece062020-03-18 07:17:44 -0500206 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500207
208 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500209 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500210 pldm::utils::DBusHandler::getBus(),
211 interfacesAdded() + argNpath(0, dBusMap->objectPath),
212 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500213 sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500214 sdbusplus::message::object_path path;
215 DbusIfacesAdded interfaces;
Matt Spinlerd987c942023-03-24 10:18:19 -0500216
Patrick Williams6da4f912023-05-10 07:50:53 -0500217 msg.read(path, interfaces);
218 auto ifaceIt = interfaces.find(interface);
219 if (ifaceIt != interfaces.end())
220 {
221 processBiosAttrChangeNotification(ifaceIt->second,
222 biosAttrIndex);
223 }
Matt Spinlerd987c942023-03-24 10:18:19 -0500224 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500225 }
John Wangd9659342020-02-27 16:46:05 +0800226 }
227 catch (const std::exception& e)
228 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600229 error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
230 e.what());
John Wangd9659342020-02-27 16:46:05 +0800231 }
232 }
233
234 /** Construct attributes and persist them */
235 void constructAttributes();
236
237 using ParseHandler = std::function<void(const Json& entry)>;
238
239 /** @brief Helper function to parse json
240 * @param[in] filePath - Path of json file
241 * @param[in] handler - Handler to process each entry in the json
242 */
243 void load(const fs::path& filePath, ParseHandler handler);
244
245 /** @brief Build String Table and persist it
246 * @return The built string table, std::nullopt if it fails.
247 */
248 std::optional<Table> buildAndStoreStringTable();
249
Tom Josephca7b2522020-11-18 12:27:11 +0530250 /** @brief Build attribute table and attribute value table and persist them
251 * Read the BaseBIOSTable from the bios-settings-manager and update
252 * attribute table and attribute value table.
253 *
John Wangd9659342020-02-27 16:46:05 +0800254 * @param[in] stringTable - The string Table
255 */
256 void buildAndStoreAttrTables(const Table& stringTable);
257
258 /** @brief Persist the table
259 * @param[in] path - Path to persist the table
260 * @param[in] table - The table
261 */
262 void storeTable(const fs::path& path, const Table& table);
263
264 /** @brief Load bios table to ram
265 * @param[in] path - Path of the table
266 * @return The table, std::nullopt if loading fails
267 */
268 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800269
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600270 /** @brief Method to decode the attribute name from the string handle
271 *
272 * @param[in] stringEntry - string entry from string table
273 * @return the decoded string
274 */
275 std::string decodeStringFromStringEntry(
276 const pldm_bios_string_table_entry* stringEntry);
277
278 /** @brief Method to print the string Handle by passing the attribute Handle
279 * of the bios attribute that got updated
280 *
281 * @param[in] handle - the Attribute handle of the bios attribute
282 * @param[in] index - index to the possible value handles
283 * @param[in] attrTable - the attribute table
284 * @param[in] stringTable - the string table
285 * @return string handle from the string table and decoded string to the
286 * name handle
287 */
288 std::string displayStringHandle(uint16_t handle, uint8_t index,
289 const std::optional<Table>& attrTable,
290 const std::optional<Table>& stringTable);
291
292 /** @brief Method to trace the bios attribute which got changed
293 *
294 * @param[in] attrValueEntry - The attribute value entry to update
295 * @param[in] attrEntry - The attribute table entry
296 * @param[in] isBMC - indicates if the attribute is set by BMC
297 */
298 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
299 const pldm_bios_attr_table_entry* attrEntry,
300 bool isBMC);
301
John Wang8241b342020-06-05 10:49:17 +0800302 /** @brief Check the attribute value to update
303 * @param[in] attrValueEntry - The attribute value entry to update
304 * @param[in] attrEntry - The attribute table entry
305 * @param[in] stringTable - The string table
306 * @return pldm_completion_codes
307 */
308 int checkAttrValueToUpdate(
309 const pldm_bios_attr_val_table_entry* attrValueEntry,
310 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800311
312 /** @brief Check the attribute table
313 * @param[in] table - The table
314 * @return pldm_completion_codes
315 */
316 int checkAttributeTable(const Table& table);
317
318 /** @brief Check the attribute value table
319 * @param[in] table - The table
320 * @return pldm_completion_codes
321 */
322 int checkAttributeValueTable(const Table& table);
323
324 /** @brief Update the BaseBIOSTable property of the D-Bus interface
325 */
326 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800327
328 /** @brief Listen the PendingAttributes property of the D-Bus interface and
329 * update BaseBIOSTable
330 */
331 void listenPendingAttributes();
332
333 /** @brief Find attribute handle from bios attribute table
334 * @param[in] attrName - attribute name
335 * @return attribute handle
336 */
337 uint16_t findAttrHandle(const std::string& attrName);
338
339 /** @brief Listen the PendingAttributes property of the D-Bus interface
340 * and update BaseBIOSTable
341 * @param[in] msg - Data associated with subscribed signal
342 */
343 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800344};
345
346} // namespace bios
347} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500348} // namespace pldm