John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "bios_attribute.hpp" |
| 4 | #include "bios_table.hpp" |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 5 | #include "pldmd/dbus_impl_requester.hpp" |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 6 | #include "requester/handler.hpp" |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 7 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/bios_table.h> |
| 9 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 10 | #include <nlohmann/json.hpp> |
| 11 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 12 | #include <functional> |
| 13 | #include <iostream> |
| 14 | #include <memory> |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 15 | #include <optional> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 20 | namespace pldm |
| 21 | { |
| 22 | namespace responder |
| 23 | { |
| 24 | namespace bios |
| 25 | { |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 26 | enum class BoundType |
| 27 | { |
| 28 | LowerBound, |
| 29 | UpperBound, |
| 30 | ScalarIncrement, |
| 31 | MinStringLength, |
| 32 | MaxStringLength, |
| 33 | OneOf |
| 34 | }; |
| 35 | |
| 36 | using AttributeName = std::string; |
| 37 | using AttributeType = std::string; |
| 38 | using ReadonlyStatus = bool; |
| 39 | using DisplayName = std::string; |
| 40 | using Description = std::string; |
| 41 | using MenuPath = std::string; |
| 42 | using CurrentValue = std::variant<int64_t, std::string>; |
| 43 | using DefaultValue = std::variant<int64_t, std::string>; |
| 44 | using OptionString = std::string; |
| 45 | using OptionValue = std::variant<int64_t, std::string>; |
| 46 | using Option = std::vector<std::tuple<OptionString, OptionValue>>; |
| 47 | using BIOSTableObj = |
| 48 | std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description, |
| 49 | MenuPath, CurrentValue, DefaultValue, Option>; |
| 50 | using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>; |
| 51 | |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 52 | using PendingObj = std::tuple<AttributeType, CurrentValue>; |
| 53 | using PendingAttributes = std::map<AttributeName, PendingObj>; |
| 54 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 55 | /** @class BIOSConfig |
| 56 | * @brief Manager BIOS Attributes |
| 57 | */ |
| 58 | class BIOSConfig |
| 59 | { |
| 60 | public: |
| 61 | BIOSConfig() = delete; |
| 62 | BIOSConfig(const BIOSConfig&) = delete; |
| 63 | BIOSConfig(BIOSConfig&&) = delete; |
| 64 | BIOSConfig& operator=(const BIOSConfig&) = delete; |
| 65 | BIOSConfig& operator=(BIOSConfig&&) = delete; |
| 66 | ~BIOSConfig() = default; |
| 67 | |
| 68 | /** @brief Construct BIOSConfig |
| 69 | * @param[in] jsonDir - The directory where json file exists |
| 70 | * @param[in] tableDir - The directory where the persistent table is placed |
| 71 | * @param[in] dbusHandler - Dbus Handler |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 72 | * @param[in] fd - socket descriptor to communicate to host |
| 73 | * @param[in] eid - MCTP EID of host firmware |
| 74 | * @param[in] requester - pointer to Requester object |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 75 | * @param[in] handler - PLDM request handler |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 76 | */ |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 77 | explicit BIOSConfig( |
| 78 | const char* jsonDir, const char* tableDir, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 79 | pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 80 | dbus_api::Requester* requester, |
| 81 | pldm::requester::Handler<pldm::requester::Request>* handler); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 82 | |
| 83 | /** @brief Set attribute value on dbus and attribute value table |
| 84 | * @param[in] entry - attribute value entry |
| 85 | * @param[in] size - size of the attribute value entry |
Sagar Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 86 | * @param[in] isBMC - indicates if the attribute is set by BMC |
George Liu | 6d6d1e8 | 2021-02-16 11:08:55 +0800 | [diff] [blame] | 87 | * @param[in] updateDBus - update Attr value D-Bus property |
| 88 | * if this is set to true |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 89 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 90 | * if this is set to true |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 91 | * @return pldm_completion_codes |
| 92 | */ |
Sagar Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 93 | int setAttrValue(const void* entry, size_t size, bool isBMC, |
| 94 | bool updateDBus = true, bool updateBaseBIOSTable = true); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 95 | |
| 96 | /** @brief Remove the persistent tables */ |
| 97 | void removeTables(); |
| 98 | |
| 99 | /** @brief Build bios tables(string,attribute,attribute value table)*/ |
| 100 | void buildTables(); |
| 101 | |
| 102 | /** @brief Get BIOS table of specified type |
| 103 | * @param[in] tableType - The table type |
| 104 | * @return The bios table, std::nullopt if the table is unaviliable |
| 105 | */ |
| 106 | std::optional<Table> getBIOSTable(pldm_bios_table_types tableType); |
| 107 | |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 108 | /** @brief set BIOS table |
| 109 | * @param[in] tableType - Indicates what table is being transferred |
| 110 | * {BIOSStringTable=0x0, BIOSAttributeTable=0x1, |
| 111 | * BIOSAttributeValueTable=0x2} |
| 112 | * @param[in] table - table data |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 113 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 114 | * if this is set to true |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 115 | * @return pldm_completion_codes |
| 116 | */ |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 117 | int setBIOSTable(uint8_t tableType, const Table& table, |
| 118 | bool updateBaseBIOSTable = true); |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 119 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 120 | private: |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 121 | /** @enum Index into the fields in the BaseBIOSTable |
| 122 | */ |
| 123 | enum class Index : uint8_t |
| 124 | { |
| 125 | attributeType = 0, |
| 126 | readOnly, |
| 127 | displayName, |
| 128 | description, |
| 129 | menuPath, |
| 130 | currentValue, |
| 131 | defaultValue, |
| 132 | options, |
| 133 | }; |
| 134 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 135 | const fs::path jsonDir; |
| 136 | const fs::path tableDir; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 137 | pldm::utils::DBusHandler* const dbusHandler; |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 138 | BaseBIOSTable baseBIOSTableMaps; |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 139 | |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 140 | /** @brief socket descriptor to communicate to host */ |
| 141 | int fd; |
| 142 | |
| 143 | /** @brief MCTP EID of host firmware */ |
| 144 | uint8_t eid; |
| 145 | |
| 146 | /** @brief pointer to Requester object, primarily used to access API to |
| 147 | * obtain PLDM instance id. |
| 148 | */ |
| 149 | dbus_api::Requester* requester; |
| 150 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 151 | /** @brief PLDM request handler */ |
| 152 | pldm::requester::Handler<pldm::requester::Request>* handler; |
| 153 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 154 | // vector persists all attributes |
| 155 | using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>; |
| 156 | BIOSAttributes biosAttributes; |
| 157 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 158 | using propName = std::string; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 159 | using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>; |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 160 | |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 161 | using ifaceName = std::string; |
| 162 | using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>; |
| 163 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 164 | // vector to catch the D-Bus property change signals for BIOS attributes |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 165 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch; |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 166 | |
| 167 | /** @brief Method to update a BIOS attribute when the corresponding Dbus |
| 168 | * property is changed |
| 169 | * @param[in] chProperties - list of properties which have changed |
| 170 | * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in |
| 171 | * biosAttributes |
| 172 | * @return - none |
| 173 | */ |
| 174 | void processBiosAttrChangeNotification( |
| 175 | const DbusChObjProperties& chProperties, uint32_t biosAttrIndex); |
| 176 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 177 | /** @brief Construct an attribute and persist it |
| 178 | * @tparam T - attribute type |
| 179 | * @param[in] entry - json entry |
| 180 | */ |
| 181 | template <typename T> |
| 182 | void constructAttribute(const Json& entry) |
| 183 | { |
| 184 | try |
| 185 | { |
| 186 | biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler)); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 187 | auto biosAttrIndex = biosAttributes.size() - 1; |
| 188 | auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap(); |
| 189 | |
| 190 | if (dBusMap.has_value()) |
| 191 | { |
| 192 | using namespace sdbusplus::bus::match::rules; |
| 193 | biosAttrMatch.push_back( |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 194 | std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 195 | pldm::utils::DBusHandler::getBus(), |
| 196 | propertiesChanged(dBusMap->objectPath, |
| 197 | dBusMap->interface), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 198 | [this, biosAttrIndex](sdbusplus::message_t& msg) { |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 199 | DbusChObjProperties props; |
| 200 | std::string iface; |
| 201 | msg.read(iface, props); |
| 202 | processBiosAttrChangeNotification(props, |
| 203 | biosAttrIndex); |
| 204 | })); |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 205 | |
| 206 | biosAttrMatch.push_back( |
Patrick Williams | b90fb7f | 2023-04-12 02:31:38 -0500 | [diff] [blame^] | 207 | std::make_unique<sdbusplus::bus::match_t>( |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 208 | pldm::utils::DBusHandler::getBus(), |
| 209 | interfacesAdded() + argNpath(0, dBusMap->objectPath), |
| 210 | [this, biosAttrIndex, interface = dBusMap->interface]( |
Patrick Williams | b90fb7f | 2023-04-12 02:31:38 -0500 | [diff] [blame^] | 211 | sdbusplus::message_t& msg) { |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 212 | sdbusplus::message::object_path path; |
| 213 | DbusIfacesAdded interfaces; |
| 214 | |
| 215 | msg.read(path, interfaces); |
| 216 | auto ifaceIt = interfaces.find(interface); |
| 217 | if (ifaceIt != interfaces.end()) |
| 218 | { |
| 219 | processBiosAttrChangeNotification( |
| 220 | ifaceIt->second, biosAttrIndex); |
| 221 | } |
| 222 | })); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 223 | } |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 224 | } |
| 225 | catch (const std::exception& e) |
| 226 | { |
| 227 | std::cerr << "Constructs Attribute Error, " << e.what() |
| 228 | << std::endl; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** Construct attributes and persist them */ |
| 233 | void constructAttributes(); |
| 234 | |
| 235 | using ParseHandler = std::function<void(const Json& entry)>; |
| 236 | |
| 237 | /** @brief Helper function to parse json |
| 238 | * @param[in] filePath - Path of json file |
| 239 | * @param[in] handler - Handler to process each entry in the json |
| 240 | */ |
| 241 | void load(const fs::path& filePath, ParseHandler handler); |
| 242 | |
| 243 | /** @brief Build String Table and persist it |
| 244 | * @return The built string table, std::nullopt if it fails. |
| 245 | */ |
| 246 | std::optional<Table> buildAndStoreStringTable(); |
| 247 | |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 248 | /** @brief Build attribute table and attribute value table and persist them |
| 249 | * Read the BaseBIOSTable from the bios-settings-manager and update |
| 250 | * attribute table and attribute value table. |
| 251 | * |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 252 | * @param[in] stringTable - The string Table |
| 253 | */ |
| 254 | void buildAndStoreAttrTables(const Table& stringTable); |
| 255 | |
| 256 | /** @brief Persist the table |
| 257 | * @param[in] path - Path to persist the table |
| 258 | * @param[in] table - The table |
| 259 | */ |
| 260 | void storeTable(const fs::path& path, const Table& table); |
| 261 | |
| 262 | /** @brief Load bios table to ram |
| 263 | * @param[in] path - Path of the table |
| 264 | * @return The table, std::nullopt if loading fails |
| 265 | */ |
| 266 | std::optional<Table> loadTable(const fs::path& path); |
John Wang | 8241b34 | 2020-06-05 10:49:17 +0800 | [diff] [blame] | 267 | |
Sagar Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 268 | /** @brief Method to decode the attribute name from the string handle |
| 269 | * |
| 270 | * @param[in] stringEntry - string entry from string table |
| 271 | * @return the decoded string |
| 272 | */ |
| 273 | std::string decodeStringFromStringEntry( |
| 274 | const pldm_bios_string_table_entry* stringEntry); |
| 275 | |
| 276 | /** @brief Method to print the string Handle by passing the attribute Handle |
| 277 | * of the bios attribute that got updated |
| 278 | * |
| 279 | * @param[in] handle - the Attribute handle of the bios attribute |
| 280 | * @param[in] index - index to the possible value handles |
| 281 | * @param[in] attrTable - the attribute table |
| 282 | * @param[in] stringTable - the string table |
| 283 | * @return string handle from the string table and decoded string to the |
| 284 | * name handle |
| 285 | */ |
| 286 | std::string displayStringHandle(uint16_t handle, uint8_t index, |
| 287 | const std::optional<Table>& attrTable, |
| 288 | const std::optional<Table>& stringTable); |
| 289 | |
| 290 | /** @brief Method to trace the bios attribute which got changed |
| 291 | * |
| 292 | * @param[in] attrValueEntry - The attribute value entry to update |
| 293 | * @param[in] attrEntry - The attribute table entry |
| 294 | * @param[in] isBMC - indicates if the attribute is set by BMC |
| 295 | */ |
| 296 | void traceBIOSUpdate(const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 297 | const pldm_bios_attr_table_entry* attrEntry, |
| 298 | bool isBMC); |
| 299 | |
John Wang | 8241b34 | 2020-06-05 10:49:17 +0800 | [diff] [blame] | 300 | /** @brief Check the attribute value to update |
| 301 | * @param[in] attrValueEntry - The attribute value entry to update |
| 302 | * @param[in] attrEntry - The attribute table entry |
| 303 | * @param[in] stringTable - The string table |
| 304 | * @return pldm_completion_codes |
| 305 | */ |
| 306 | int checkAttrValueToUpdate( |
| 307 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 308 | const pldm_bios_attr_table_entry* attrEntry, Table& stringTable); |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 309 | |
| 310 | /** @brief Check the attribute table |
| 311 | * @param[in] table - The table |
| 312 | * @return pldm_completion_codes |
| 313 | */ |
| 314 | int checkAttributeTable(const Table& table); |
| 315 | |
| 316 | /** @brief Check the attribute value table |
| 317 | * @param[in] table - The table |
| 318 | * @return pldm_completion_codes |
| 319 | */ |
| 320 | int checkAttributeValueTable(const Table& table); |
| 321 | |
| 322 | /** @brief Update the BaseBIOSTable property of the D-Bus interface |
| 323 | */ |
| 324 | void updateBaseBIOSTableProperty(); |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 325 | |
| 326 | /** @brief Listen the PendingAttributes property of the D-Bus interface and |
| 327 | * update BaseBIOSTable |
| 328 | */ |
| 329 | void listenPendingAttributes(); |
| 330 | |
| 331 | /** @brief Find attribute handle from bios attribute table |
| 332 | * @param[in] attrName - attribute name |
| 333 | * @return attribute handle |
| 334 | */ |
| 335 | uint16_t findAttrHandle(const std::string& attrName); |
| 336 | |
| 337 | /** @brief Listen the PendingAttributes property of the D-Bus interface |
| 338 | * and update BaseBIOSTable |
| 339 | * @param[in] msg - Data associated with subscribed signal |
| 340 | */ |
| 341 | void constructPendingAttribute(const PendingAttributes& pendingAttributes); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | } // namespace bios |
| 345 | } // namespace responder |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 346 | } // namespace pldm |