blob: 4e6908990f41ea7dc03f988749e344219d681530 [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) {
Sampa Misra46ece062020-03-18 07:17:44 -0500202 DbusChObjProperties props;
203 std::string iface;
204 msg.read(iface, props);
205 processBiosAttrChangeNotification(props,
206 biosAttrIndex);
207 }));
Matt Spinlerd987c942023-03-24 10:18:19 -0500208
209 biosAttrMatch.push_back(
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500210 std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerd987c942023-03-24 10:18:19 -0500211 pldm::utils::DBusHandler::getBus(),
212 interfacesAdded() + argNpath(0, dBusMap->objectPath),
213 [this, biosAttrIndex, interface = dBusMap->interface](
Patrick Williamsb90fb7f2023-04-12 02:31:38 -0500214 sdbusplus::message_t& msg) {
Matt Spinlerd987c942023-03-24 10:18:19 -0500215 sdbusplus::message::object_path path;
216 DbusIfacesAdded interfaces;
217
218 msg.read(path, interfaces);
219 auto ifaceIt = interfaces.find(interface);
220 if (ifaceIt != interfaces.end())
221 {
222 processBiosAttrChangeNotification(
223 ifaceIt->second, biosAttrIndex);
224 }
225 }));
Sampa Misra46ece062020-03-18 07:17:44 -0500226 }
John Wangd9659342020-02-27 16:46:05 +0800227 }
228 catch (const std::exception& e)
229 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600230 error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP",
231 e.what());
John Wangd9659342020-02-27 16:46:05 +0800232 }
233 }
234
235 /** Construct attributes and persist them */
236 void constructAttributes();
237
238 using ParseHandler = std::function<void(const Json& entry)>;
239
240 /** @brief Helper function to parse json
241 * @param[in] filePath - Path of json file
242 * @param[in] handler - Handler to process each entry in the json
243 */
244 void load(const fs::path& filePath, ParseHandler handler);
245
246 /** @brief Build String Table and persist it
247 * @return The built string table, std::nullopt if it fails.
248 */
249 std::optional<Table> buildAndStoreStringTable();
250
Tom Josephca7b2522020-11-18 12:27:11 +0530251 /** @brief Build attribute table and attribute value table and persist them
252 * Read the BaseBIOSTable from the bios-settings-manager and update
253 * attribute table and attribute value table.
254 *
John Wangd9659342020-02-27 16:46:05 +0800255 * @param[in] stringTable - The string Table
256 */
257 void buildAndStoreAttrTables(const Table& stringTable);
258
259 /** @brief Persist the table
260 * @param[in] path - Path to persist the table
261 * @param[in] table - The table
262 */
263 void storeTable(const fs::path& path, const Table& table);
264
265 /** @brief Load bios table to ram
266 * @param[in] path - Path of the table
267 * @return The table, std::nullopt if loading fails
268 */
269 std::optional<Table> loadTable(const fs::path& path);
John Wang8241b342020-06-05 10:49:17 +0800270
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600271 /** @brief Method to decode the attribute name from the string handle
272 *
273 * @param[in] stringEntry - string entry from string table
274 * @return the decoded string
275 */
276 std::string decodeStringFromStringEntry(
277 const pldm_bios_string_table_entry* stringEntry);
278
279 /** @brief Method to print the string Handle by passing the attribute Handle
280 * of the bios attribute that got updated
281 *
282 * @param[in] handle - the Attribute handle of the bios attribute
283 * @param[in] index - index to the possible value handles
284 * @param[in] attrTable - the attribute table
285 * @param[in] stringTable - the string table
286 * @return string handle from the string table and decoded string to the
287 * name handle
288 */
289 std::string displayStringHandle(uint16_t handle, uint8_t index,
290 const std::optional<Table>& attrTable,
291 const std::optional<Table>& stringTable);
292
293 /** @brief Method to trace the bios attribute which got changed
294 *
295 * @param[in] attrValueEntry - The attribute value entry to update
296 * @param[in] attrEntry - The attribute table entry
297 * @param[in] isBMC - indicates if the attribute is set by BMC
298 */
299 void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry,
300 const pldm_bios_attr_table_entry* attrEntry,
301 bool isBMC);
302
John Wang8241b342020-06-05 10:49:17 +0800303 /** @brief Check the attribute value to update
304 * @param[in] attrValueEntry - The attribute value entry to update
305 * @param[in] attrEntry - The attribute table entry
306 * @param[in] stringTable - The string table
307 * @return pldm_completion_codes
308 */
309 int checkAttrValueToUpdate(
310 const pldm_bios_attr_val_table_entry* attrValueEntry,
311 const pldm_bios_attr_table_entry* attrEntry, Table& stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800312
313 /** @brief Check the attribute table
314 * @param[in] table - The table
315 * @return pldm_completion_codes
316 */
317 int checkAttributeTable(const Table& table);
318
319 /** @brief Check the attribute value table
320 * @param[in] table - The table
321 * @return pldm_completion_codes
322 */
323 int checkAttributeValueTable(const Table& table);
324
325 /** @brief Update the BaseBIOSTable property of the D-Bus interface
326 */
327 void updateBaseBIOSTableProperty();
George Liu1244acf2020-08-14 09:11:11 +0800328
329 /** @brief Listen the PendingAttributes property of the D-Bus interface and
330 * update BaseBIOSTable
331 */
332 void listenPendingAttributes();
333
334 /** @brief Find attribute handle from bios attribute table
335 * @param[in] attrName - attribute name
336 * @return attribute handle
337 */
338 uint16_t findAttrHandle(const std::string& attrName);
339
340 /** @brief Listen the PendingAttributes property of the D-Bus interface
341 * and update BaseBIOSTable
342 * @param[in] msg - Data associated with subscribed signal
343 */
344 void constructPendingAttribute(const PendingAttributes& pendingAttributes);
John Wangd9659342020-02-27 16:46:05 +0800345};
346
347} // namespace bios
348} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500349} // namespace pldm