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" |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 5 | #include "pldmd/instance_id.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> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 12 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 13 | #include <functional> |
| 14 | #include <iostream> |
| 15 | #include <memory> |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 16 | #include <optional> |
| 17 | #include <set> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 21 | PHOSPHOR_LOG2_USING; |
| 22 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 23 | namespace pldm |
| 24 | { |
| 25 | namespace responder |
| 26 | { |
| 27 | namespace bios |
| 28 | { |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 29 | enum class BoundType |
| 30 | { |
| 31 | LowerBound, |
| 32 | UpperBound, |
| 33 | ScalarIncrement, |
| 34 | MinStringLength, |
| 35 | MaxStringLength, |
| 36 | OneOf |
| 37 | }; |
| 38 | |
| 39 | using AttributeName = std::string; |
| 40 | using AttributeType = std::string; |
| 41 | using ReadonlyStatus = bool; |
| 42 | using DisplayName = std::string; |
| 43 | using Description = std::string; |
| 44 | using MenuPath = std::string; |
| 45 | using CurrentValue = std::variant<int64_t, std::string>; |
| 46 | using DefaultValue = std::variant<int64_t, std::string>; |
| 47 | using OptionString = std::string; |
| 48 | using OptionValue = std::variant<int64_t, std::string>; |
| 49 | using Option = std::vector<std::tuple<OptionString, OptionValue>>; |
| 50 | using BIOSTableObj = |
| 51 | std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description, |
| 52 | MenuPath, CurrentValue, DefaultValue, Option>; |
| 53 | using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>; |
| 54 | |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 55 | using PendingObj = std::tuple<AttributeType, CurrentValue>; |
| 56 | using PendingAttributes = std::map<AttributeName, PendingObj>; |
| 57 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 58 | /** @class BIOSConfig |
| 59 | * @brief Manager BIOS Attributes |
| 60 | */ |
| 61 | class 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 Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 75 | * @param[in] fd - socket descriptor to communicate to host |
| 76 | * @param[in] eid - MCTP EID of host firmware |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 77 | * @param[in] instanceIdDb - pointer to an InstanceIdDb object |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 78 | * @param[in] handler - PLDM request handler |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 79 | */ |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 80 | explicit BIOSConfig( |
| 81 | const char* jsonDir, const char* tableDir, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 82 | pldm::utils::DBusHandler* const dbusHandler, int fd, uint8_t eid, |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 83 | pldm::InstanceIdDb* instanceIdDb, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 84 | pldm::requester::Handler<pldm::requester::Request>* handler); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 85 | |
| 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 Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 89 | * @param[in] isBMC - indicates if the attribute is set by BMC |
George Liu | 6d6d1e8 | 2021-02-16 11:08:55 +0800 | [diff] [blame] | 90 | * @param[in] updateDBus - update Attr value D-Bus property |
| 91 | * if this is set to true |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 92 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 93 | * if this is set to true |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 94 | * @return pldm_completion_codes |
| 95 | */ |
Sagar Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 96 | int setAttrValue(const void* entry, size_t size, bool isBMC, |
| 97 | bool updateDBus = true, bool updateBaseBIOSTable = true); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 98 | |
| 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 Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 111 | /** @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 Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 116 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 117 | * if this is set to true |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 118 | * @return pldm_completion_codes |
| 119 | */ |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 120 | int setBIOSTable(uint8_t tableType, const Table& table, |
| 121 | bool updateBaseBIOSTable = true); |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 122 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 123 | private: |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 124 | /** @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 Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 138 | const fs::path jsonDir; |
| 139 | const fs::path tableDir; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 140 | pldm::utils::DBusHandler* const dbusHandler; |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 141 | BaseBIOSTable baseBIOSTableMaps; |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 142 | |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 143 | /** @brief socket descriptor to communicate to host */ |
| 144 | int fd; |
| 145 | |
| 146 | /** @brief MCTP EID of host firmware */ |
| 147 | uint8_t eid; |
| 148 | |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 149 | /** @brief pointer to an Instance ID database object, used to obtain PLDM |
| 150 | * instance IDs. |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 151 | */ |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 152 | pldm::InstanceIdDb* instanceIdDb; |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 153 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 154 | /** @brief PLDM request handler */ |
| 155 | pldm::requester::Handler<pldm::requester::Request>* handler; |
| 156 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 157 | // vector persists all attributes |
| 158 | using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>; |
| 159 | BIOSAttributes biosAttributes; |
| 160 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 161 | using propName = std::string; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 162 | using DbusChObjProperties = std::map<propName, pldm::utils::PropertyValue>; |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 163 | |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 164 | using ifaceName = std::string; |
| 165 | using DbusIfacesAdded = std::map<ifaceName, DbusChObjProperties>; |
| 166 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 167 | // vector to catch the D-Bus property change signals for BIOS attributes |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 168 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> biosAttrMatch; |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 169 | |
| 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 Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 180 | /** @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 Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 190 | 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 Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 197 | std::make_unique<sdbusplus::bus::match_t>( |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 198 | pldm::utils::DBusHandler::getBus(), |
| 199 | propertiesChanged(dBusMap->objectPath, |
| 200 | dBusMap->interface), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 201 | [this, biosAttrIndex](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 202 | DbusChObjProperties props; |
| 203 | std::string iface; |
| 204 | msg.read(iface, props); |
| 205 | processBiosAttrChangeNotification(props, biosAttrIndex); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 206 | })); |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 207 | |
| 208 | biosAttrMatch.push_back( |
Patrick Williams | b90fb7f | 2023-04-12 02:31:38 -0500 | [diff] [blame] | 209 | std::make_unique<sdbusplus::bus::match_t>( |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 210 | pldm::utils::DBusHandler::getBus(), |
| 211 | interfacesAdded() + argNpath(0, dBusMap->objectPath), |
| 212 | [this, biosAttrIndex, interface = dBusMap->interface]( |
Patrick Williams | b90fb7f | 2023-04-12 02:31:38 -0500 | [diff] [blame] | 213 | sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 214 | sdbusplus::message::object_path path; |
| 215 | DbusIfacesAdded interfaces; |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 216 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 217 | msg.read(path, interfaces); |
| 218 | auto ifaceIt = interfaces.find(interface); |
| 219 | if (ifaceIt != interfaces.end()) |
| 220 | { |
| 221 | processBiosAttrChangeNotification(ifaceIt->second, |
| 222 | biosAttrIndex); |
| 223 | } |
Matt Spinler | d987c94 | 2023-03-24 10:18:19 -0500 | [diff] [blame] | 224 | })); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 225 | } |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 226 | } |
| 227 | catch (const std::exception& e) |
| 228 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 229 | error("Constructs Attribute Error, {ERR_EXCEP}", "ERR_EXCEP", |
| 230 | e.what()); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 231 | } |
| 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 Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 250 | /** @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 Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 254 | * @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 Wang | 8241b34 | 2020-06-05 10:49:17 +0800 | [diff] [blame] | 269 | |
Sagar Srinivas | cac0ebb | 2021-11-23 07:50:28 -0600 | [diff] [blame] | 270 | /** @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 Wang | 8241b34 | 2020-06-05 10:49:17 +0800 | [diff] [blame] | 302 | /** @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 Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 311 | |
| 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 Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 327 | |
| 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 Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | } // namespace bios |
| 347 | } // namespace responder |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 348 | } // namespace pldm |