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