John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "bios_table.h" |
| 4 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 5 | #include "bios_attribute.hpp" |
| 6 | #include "bios_table.hpp" |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 7 | #include "pldmd/dbus_impl_requester.hpp" |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 8 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
| 10 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 11 | #include <functional> |
| 12 | #include <iostream> |
| 13 | #include <memory> |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 14 | #include <optional> |
| 15 | #include <set> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 19 | namespace pldm |
| 20 | { |
| 21 | namespace responder |
| 22 | { |
| 23 | namespace bios |
| 24 | { |
| 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 |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 75 | */ |
| 76 | explicit BIOSConfig(const char* jsonDir, const char* tableDir, |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 77 | DBusHandler* const dbusHandler, int fd, uint8_t eid, |
| 78 | dbus_api::Requester* requester); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 79 | |
| 80 | /** @brief Set attribute value on dbus and attribute value table |
| 81 | * @param[in] entry - attribute value entry |
| 82 | * @param[in] size - size of the attribute value entry |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 83 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 84 | * if this is set to true |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 85 | * @return pldm_completion_codes |
| 86 | */ |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 87 | int setAttrValue(const void* entry, size_t size, |
| 88 | bool updateBaseBIOSTable = true); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 89 | |
| 90 | /** @brief Remove the persistent tables */ |
| 91 | void removeTables(); |
| 92 | |
| 93 | /** @brief Build bios tables(string,attribute,attribute value table)*/ |
| 94 | void buildTables(); |
| 95 | |
| 96 | /** @brief Get BIOS table of specified type |
| 97 | * @param[in] tableType - The table type |
| 98 | * @return The bios table, std::nullopt if the table is unaviliable |
| 99 | */ |
| 100 | std::optional<Table> getBIOSTable(pldm_bios_table_types tableType); |
| 101 | |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 102 | /** @brief set BIOS table |
| 103 | * @param[in] tableType - Indicates what table is being transferred |
| 104 | * {BIOSStringTable=0x0, BIOSAttributeTable=0x1, |
| 105 | * BIOSAttributeValueTable=0x2} |
| 106 | * @param[in] table - table data |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 107 | * @param[in] updateBaseBIOSTable - update BaseBIOSTable D-Bus property |
| 108 | * if this is set to true |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 109 | * @return pldm_completion_codes |
| 110 | */ |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 111 | int setBIOSTable(uint8_t tableType, const Table& table, |
| 112 | bool updateBaseBIOSTable = true); |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 113 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 114 | private: |
| 115 | const fs::path jsonDir; |
| 116 | const fs::path tableDir; |
| 117 | DBusHandler* const dbusHandler; |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 118 | BaseBIOSTable baseBIOSTableMaps; |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 119 | |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 120 | /** @brief socket descriptor to communicate to host */ |
| 121 | int fd; |
| 122 | |
| 123 | /** @brief MCTP EID of host firmware */ |
| 124 | uint8_t eid; |
| 125 | |
| 126 | /** @brief pointer to Requester object, primarily used to access API to |
| 127 | * obtain PLDM instance id. |
| 128 | */ |
| 129 | dbus_api::Requester* requester; |
| 130 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 131 | // vector persists all attributes |
| 132 | using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>; |
| 133 | BIOSAttributes biosAttributes; |
| 134 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 135 | using propName = std::string; |
| 136 | using DbusChObjProperties = std::map<propName, PropertyValue>; |
| 137 | |
| 138 | // vector to catch the D-Bus property change signals for BIOS attributes |
| 139 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch; |
| 140 | |
| 141 | /** @brief Method to update a BIOS attribute when the corresponding Dbus |
| 142 | * property is changed |
| 143 | * @param[in] chProperties - list of properties which have changed |
| 144 | * @param[in] biosAttrIndex - Index of BIOSAttribute pointer in |
| 145 | * biosAttributes |
| 146 | * @return - none |
| 147 | */ |
| 148 | void processBiosAttrChangeNotification( |
| 149 | const DbusChObjProperties& chProperties, uint32_t biosAttrIndex); |
| 150 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 151 | /** @brief Construct an attribute and persist it |
| 152 | * @tparam T - attribute type |
| 153 | * @param[in] entry - json entry |
| 154 | */ |
| 155 | template <typename T> |
| 156 | void constructAttribute(const Json& entry) |
| 157 | { |
| 158 | try |
| 159 | { |
| 160 | biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler)); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 161 | auto biosAttrIndex = biosAttributes.size() - 1; |
| 162 | auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap(); |
| 163 | |
| 164 | if (dBusMap.has_value()) |
| 165 | { |
| 166 | using namespace sdbusplus::bus::match::rules; |
| 167 | biosAttrMatch.push_back( |
| 168 | std::make_unique<sdbusplus::bus::match::match>( |
| 169 | pldm::utils::DBusHandler::getBus(), |
| 170 | propertiesChanged(dBusMap->objectPath, |
| 171 | dBusMap->interface), |
| 172 | [this, |
| 173 | biosAttrIndex](sdbusplus::message::message& msg) { |
| 174 | DbusChObjProperties props; |
| 175 | std::string iface; |
| 176 | msg.read(iface, props); |
| 177 | processBiosAttrChangeNotification(props, |
| 178 | biosAttrIndex); |
| 179 | })); |
| 180 | } |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 181 | } |
| 182 | catch (const std::exception& e) |
| 183 | { |
| 184 | std::cerr << "Constructs Attribute Error, " << e.what() |
| 185 | << std::endl; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** Construct attributes and persist them */ |
| 190 | void constructAttributes(); |
| 191 | |
| 192 | using ParseHandler = std::function<void(const Json& entry)>; |
| 193 | |
| 194 | /** @brief Helper function to parse json |
| 195 | * @param[in] filePath - Path of json file |
| 196 | * @param[in] handler - Handler to process each entry in the json |
| 197 | */ |
| 198 | void load(const fs::path& filePath, ParseHandler handler); |
| 199 | |
| 200 | /** @brief Build String Table and persist it |
| 201 | * @return The built string table, std::nullopt if it fails. |
| 202 | */ |
| 203 | std::optional<Table> buildAndStoreStringTable(); |
| 204 | |
| 205 | /** @brief Build attr table and attr value table and persist them |
| 206 | * @param[in] stringTable - The string Table |
| 207 | */ |
| 208 | void buildAndStoreAttrTables(const Table& stringTable); |
| 209 | |
| 210 | /** @brief Persist the table |
| 211 | * @param[in] path - Path to persist the table |
| 212 | * @param[in] table - The table |
| 213 | */ |
| 214 | void storeTable(const fs::path& path, const Table& table); |
| 215 | |
| 216 | /** @brief Load bios table to ram |
| 217 | * @param[in] path - Path of the table |
| 218 | * @return The table, std::nullopt if loading fails |
| 219 | */ |
| 220 | std::optional<Table> loadTable(const fs::path& path); |
John Wang | 8241b34 | 2020-06-05 10:49:17 +0800 | [diff] [blame] | 221 | |
| 222 | /** @brief Check the attribute value to update |
| 223 | * @param[in] attrValueEntry - The attribute value entry to update |
| 224 | * @param[in] attrEntry - The attribute table entry |
| 225 | * @param[in] stringTable - The string table |
| 226 | * @return pldm_completion_codes |
| 227 | */ |
| 228 | int checkAttrValueToUpdate( |
| 229 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 230 | const pldm_bios_attr_table_entry* attrEntry, Table& stringTable); |
George Liu | 1b180d8 | 2020-07-23 14:01:58 +0800 | [diff] [blame] | 231 | |
| 232 | /** @brief Check the attribute table |
| 233 | * @param[in] table - The table |
| 234 | * @return pldm_completion_codes |
| 235 | */ |
| 236 | int checkAttributeTable(const Table& table); |
| 237 | |
| 238 | /** @brief Check the attribute value table |
| 239 | * @param[in] table - The table |
| 240 | * @return pldm_completion_codes |
| 241 | */ |
| 242 | int checkAttributeValueTable(const Table& table); |
| 243 | |
| 244 | /** @brief Update the BaseBIOSTable property of the D-Bus interface |
| 245 | */ |
| 246 | void updateBaseBIOSTableProperty(); |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 247 | |
| 248 | /** @brief Listen the PendingAttributes property of the D-Bus interface and |
| 249 | * update BaseBIOSTable |
| 250 | */ |
| 251 | void listenPendingAttributes(); |
| 252 | |
| 253 | /** @brief Find attribute handle from bios attribute table |
| 254 | * @param[in] attrName - attribute name |
| 255 | * @return attribute handle |
| 256 | */ |
| 257 | uint16_t findAttrHandle(const std::string& attrName); |
| 258 | |
| 259 | /** @brief Listen the PendingAttributes property of the D-Bus interface |
| 260 | * and update BaseBIOSTable |
| 261 | * @param[in] msg - Data associated with subscribed signal |
| 262 | */ |
| 263 | void constructPendingAttribute(const PendingAttributes& pendingAttributes); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | } // namespace bios |
| 267 | } // namespace responder |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 268 | } // namespace pldm |