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