Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/bios.h> |
| 4 | #include <libpldm/bios_table.h> |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
| 7 | #include <filesystem> |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 8 | #include <optional> |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 9 | #include <string> |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 12 | namespace pldm |
| 13 | { |
| 14 | |
| 15 | namespace responder |
| 16 | { |
| 17 | |
| 18 | namespace bios |
| 19 | { |
| 20 | |
| 21 | using Table = std::vector<uint8_t>; |
| 22 | using Response = std::vector<uint8_t>; |
| 23 | namespace fs = std::filesystem; |
| 24 | |
| 25 | /** @class BIOSTable |
| 26 | * |
| 27 | * @brief Provides APIs for storing and loading BIOS tables |
| 28 | * |
| 29 | * Typical usage is as follows: |
| 30 | * BIOSTable table(BIOS_STRING_TABLE_FILE_PATH); |
| 31 | * if (table.isEmpty()) { // no persisted table |
| 32 | * // construct BIOSTable 't' |
| 33 | * // prepare Response 'r' |
| 34 | * // send response to GetBIOSTable |
| 35 | * table.store(t); // persisted |
| 36 | * } |
| 37 | * else { // persisted table exists |
| 38 | * // create Response 'r' which has response fields (except |
| 39 | * // the table itself) to a GetBIOSTable command |
| 40 | * table.load(r); // actual table will be pushed back to the vector |
| 41 | * } |
| 42 | */ |
| 43 | class BIOSTable |
| 44 | { |
| 45 | public: |
| 46 | /** @brief Ctor - set file path to persist BIOS table |
| 47 | * |
| 48 | * @param[in] filePath - file where BIOS table should be persisted |
| 49 | */ |
| 50 | BIOSTable(const char* filePath); |
| 51 | |
| 52 | /** @brief Checks if there's a persisted BIOS table |
| 53 | * |
| 54 | * @return bool - true if table exists, false otherwise |
| 55 | */ |
| 56 | bool isEmpty() const noexcept; |
| 57 | |
| 58 | /** @brief Persist a BIOS table(string/attribute/attribute value) |
| 59 | * |
| 60 | * @param[in] table - BIOS table |
| 61 | */ |
| 62 | void store(const Table& table); |
| 63 | |
| 64 | /** @brief Load BIOS table from persistent store to memory |
| 65 | * |
| 66 | * @param[in,out] response - PLDM response message to GetBIOSTable |
| 67 | * (excluding table), table will be pushed back to this. |
| 68 | */ |
| 69 | void load(Response& response) const; |
| 70 | |
| 71 | private: |
| 72 | // file storing PLDM BIOS table |
| 73 | fs::path filePath; |
| 74 | }; |
| 75 | |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 76 | /** @class BIOSStringTableInterface |
| 77 | * @brief Provide interfaces to the BIOS string table operations |
| 78 | */ |
| 79 | class BIOSStringTableInterface |
| 80 | { |
| 81 | public: |
| 82 | virtual ~BIOSStringTableInterface() = default; |
| 83 | |
| 84 | /** @brief Find the string name from the BIOS string table for a string |
| 85 | * handle |
| 86 | * @param[in] handle - string handle |
| 87 | * @return name of the corresponding BIOS string |
| 88 | */ |
| 89 | virtual std::string findString(uint16_t handle) const = 0; |
| 90 | |
| 91 | /** @brief Find the string handle from the BIOS string table by the given |
| 92 | * name |
| 93 | * @param[in] name - name of the BIOS string |
| 94 | * @return handle of the string |
| 95 | */ |
| 96 | virtual uint16_t findHandle(const std::string& name) const = 0; |
| 97 | }; |
| 98 | |
| 99 | /** @class BIOSStringTable |
| 100 | * @brief Collection of BIOS string table operations. |
| 101 | */ |
| 102 | class BIOSStringTable : public BIOSStringTableInterface |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 103 | { |
| 104 | public: |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 105 | /** @brief Constructs BIOSStringTable |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 106 | * |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 107 | * @param[in] stringTable - The stringTable in RAM |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 108 | */ |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 109 | BIOSStringTable(const Table& stringTable); |
| 110 | |
| 111 | /** @brief Constructs BIOSStringTable |
| 112 | * |
| 113 | * @param[in] biosTable - The BIOSTable |
| 114 | */ |
| 115 | BIOSStringTable(const BIOSTable& biosTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 116 | |
| 117 | /** @brief Find the string name from the BIOS string table for a string |
| 118 | * handle |
| 119 | * @param[in] handle - string handle |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 120 | * @return name of the corresponding BIOS string |
| 121 | * @throw std::invalid_argument if the string can not be found. |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 122 | */ |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 123 | std::string findString(uint16_t handle) const override; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 124 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 125 | /** @brief Find the string handle from the BIOS string table by the given |
| 126 | * name |
| 127 | * @param[in] name - name of the BIOS string |
| 128 | * @return handle of the string |
| 129 | * @throw std::invalid_argument if the string can not be found |
| 130 | */ |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 131 | uint16_t findHandle(const std::string& name) const override; |
| 132 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 133 | private: |
| 134 | Table stringTable; |
| 135 | }; |
| 136 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 137 | namespace table |
| 138 | { |
| 139 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 140 | /** @brief Append Pad and Checksum |
| 141 | * |
| 142 | * @param[in,out] table - table to be appended with pad and checksum |
| 143 | */ |
| 144 | void appendPadAndChecksum(Table& table); |
| 145 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 146 | namespace string |
| 147 | { |
| 148 | |
| 149 | /** @brief Get the string handle for the entry |
| 150 | * @param[in] entry - Pointer to a bios string table entry |
| 151 | * @return Handle to identify a string in the bios string table |
| 152 | */ |
| 153 | uint16_t decodeHandle(const pldm_bios_string_table_entry* entry); |
| 154 | |
| 155 | /** @brief Get the string from the entry |
| 156 | * @param[in] entry - Pointer to a bios string table entry |
| 157 | * @return The String |
| 158 | */ |
| 159 | std::string decodeString(const pldm_bios_string_table_entry* entry); |
| 160 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 161 | /** @brief construct entry of string table at the end of the given |
| 162 | * table |
| 163 | * @param[in,out] table - The given table |
| 164 | * @param[in] str - string itself |
| 165 | * @return pointer to the constructed entry |
| 166 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 167 | const pldm_bios_string_table_entry* |
| 168 | constructEntry(Table& table, const std::string& str); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 169 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 170 | } // namespace string |
| 171 | |
| 172 | namespace attribute |
| 173 | { |
| 174 | |
| 175 | /** @struct TableHeader |
| 176 | * @brief Header of attribute table |
| 177 | */ |
| 178 | struct TableHeader |
| 179 | { |
| 180 | uint16_t attrHandle; |
| 181 | uint8_t attrType; |
| 182 | uint16_t stringHandle; |
| 183 | }; |
| 184 | |
| 185 | /** @brief Decode header of attribute table entry |
| 186 | * @param[in] entry - Pointer to an attribute table entry |
| 187 | * @return Attribute table header |
| 188 | */ |
| 189 | TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry); |
| 190 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 191 | /** @brief Find attribute entry by handle |
| 192 | * @param[in] table - attribute table |
| 193 | * @param[in] handle - attribute handle |
| 194 | * @return Pointer to the attribute table entry |
| 195 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 196 | const pldm_bios_attr_table_entry* |
| 197 | findByHandle(const Table& table, uint16_t handle); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 198 | |
John Wang | 45fed20 | 2020-04-01 16:42:26 +0800 | [diff] [blame] | 199 | /** @brief Find attribute entry by string handle |
| 200 | * @param[in] table - attribute table |
| 201 | * @param[in] handle - string handle |
| 202 | * @return Pointer to the attribute table entry |
| 203 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 204 | const pldm_bios_attr_table_entry* |
| 205 | findByStringHandle(const Table& table, uint16_t handle); |
John Wang | 45fed20 | 2020-04-01 16:42:26 +0800 | [diff] [blame] | 206 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 207 | /** @struct StringField |
| 208 | * @brief String field of attribute table |
| 209 | */ |
| 210 | struct StringField |
| 211 | { |
| 212 | uint8_t stringType; |
| 213 | uint16_t minLength; |
| 214 | uint16_t maxLength; |
| 215 | uint16_t defLength; |
| 216 | std::string defString; |
| 217 | }; |
| 218 | |
| 219 | /** @brief decode string entry of attribute table |
| 220 | * @param[in] entry - Pointer to an attribute table entry |
| 221 | * @return String field of the entry |
| 222 | */ |
| 223 | StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry); |
| 224 | |
| 225 | /** @brief construct string entry of attribute table at the end of the given |
| 226 | * table |
| 227 | * @param[in,out] table - The given table |
| 228 | * @param[in] info - string info |
| 229 | * @return pointer to the constructed entry |
| 230 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 231 | const pldm_bios_attr_table_entry* constructStringEntry( |
| 232 | Table& table, pldm_bios_table_attr_entry_string_info* info); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 233 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 234 | /** @struct IntegerField |
| 235 | * @brief Integer field of attribute table |
| 236 | */ |
| 237 | struct IntegerField |
| 238 | { |
| 239 | uint64_t lowerBound; |
| 240 | uint64_t upperBound; |
| 241 | uint32_t scalarIncrement; |
| 242 | uint64_t defaultValue; |
| 243 | }; |
| 244 | |
| 245 | /** @brief construct integer entry of attribute table at the end of the |
| 246 | * given table |
| 247 | * @param[in,out] table - The given table |
| 248 | * @param[in] info - integer info |
| 249 | * @return pointer to the constructed entry |
| 250 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 251 | const pldm_bios_attr_table_entry* constructIntegerEntry( |
| 252 | Table& table, pldm_bios_table_attr_entry_integer_info* info); |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 253 | |
| 254 | /** @brief decode integer entry of attribute table |
| 255 | * @param[in] entry - Pointer to an attribute table entry |
| 256 | * @return Integer field of the entry |
| 257 | */ |
| 258 | IntegerField decodeIntegerEntry(const pldm_bios_attr_table_entry* entry); |
| 259 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 260 | /** @struct EnumField |
| 261 | * @brief Enum field of attribute table |
| 262 | */ |
| 263 | struct EnumField |
| 264 | { |
| 265 | std::vector<uint16_t> possibleValueStringHandle; |
| 266 | std::vector<uint8_t> defaultValueIndex; |
| 267 | }; |
| 268 | |
| 269 | /** @brief decode enum entry of attribute table |
| 270 | * @param[in] entry - Pointer to an attribute table entry |
| 271 | * @return Enum field of the entry |
| 272 | */ |
| 273 | EnumField decodeEnumEntry(const pldm_bios_attr_table_entry* entry); |
| 274 | |
| 275 | /** @brief construct enum entry of attribute table at the end of the |
| 276 | * given table |
| 277 | * @param[in,out] table - The given table |
| 278 | * @param[in] info - enum info |
| 279 | * @return pointer to the constructed entry |
| 280 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 281 | const pldm_bios_attr_table_entry* constructEnumEntry( |
| 282 | Table& table, pldm_bios_table_attr_entry_enum_info* info); |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 283 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 284 | } // namespace attribute |
| 285 | |
| 286 | namespace attribute_value |
| 287 | { |
| 288 | |
| 289 | /** @struct TableHeader |
| 290 | * @brief Header of attribute value table |
| 291 | */ |
| 292 | struct TableHeader |
| 293 | { |
| 294 | uint16_t attrHandle; |
| 295 | uint8_t attrType; |
| 296 | }; |
| 297 | |
| 298 | /** @brief Decode header of attribute value table |
| 299 | * @param[in] entry - Pointer to an attribute value table entry |
| 300 | * @return Attribute value table header |
| 301 | */ |
| 302 | TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry); |
| 303 | |
| 304 | /** @brief Decode string entry of attribute value table |
| 305 | * @param[in] entry - Pointer to an attribute value table entry |
| 306 | * @return The decoded string |
| 307 | */ |
| 308 | std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry); |
| 309 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 310 | /** @brief Decode integer entry of attribute value table |
| 311 | * @param[in] entry - Pointer to an attribute value table entry |
| 312 | * @return The decoded integer |
| 313 | */ |
| 314 | uint64_t decodeIntegerEntry(const pldm_bios_attr_val_table_entry* entry); |
| 315 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 316 | /** @brief Decode enum entry of attribute value table |
| 317 | * @param[in] entry - Pointer to an attribute value table entry |
| 318 | * @return Current value string handle indices |
| 319 | */ |
| 320 | std::vector<uint8_t> |
| 321 | decodeEnumEntry(const pldm_bios_attr_val_table_entry* entry); |
| 322 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 323 | /** @brief Construct string entry of attribute value table at the end of the |
| 324 | * given table |
| 325 | * @param[in] table - The given table |
| 326 | * @param[in] attrHandle - attribute handle |
| 327 | * @param[in] attrType - attribute type |
| 328 | * @param[in] str - The string |
| 329 | * @return Pointer to the constructed entry |
| 330 | */ |
| 331 | const pldm_bios_attr_val_table_entry* |
| 332 | constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType, |
| 333 | const std::string& str); |
| 334 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 335 | /** @brief Construct integer entry of attribute value table at the end of |
| 336 | * the given table |
| 337 | * @param[in] table - The given table |
| 338 | * @param[in] attrHandle - attribute handle |
| 339 | * @param[in] attrType - attribute type |
| 340 | * @param[in] value - The integer |
| 341 | * @return Pointer to the constructed entry |
| 342 | */ |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 343 | const pldm_bios_attr_val_table_entry* constructIntegerEntry( |
| 344 | Table& table, uint16_t attrHandle, uint8_t attrType, uint64_t value); |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 345 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 346 | /** @brief Construct enum entry of attribute value table at the end of |
| 347 | * the given table |
| 348 | * @param[in] table - The given table |
| 349 | * @param[in] attrHandle - attribute handle |
| 350 | * @param[in] attrType - attribute type |
| 351 | * @param[in] handleIndices - handle indices |
| 352 | * @return Pointer to the constructed entry |
| 353 | */ |
| 354 | const pldm_bios_attr_val_table_entry* |
| 355 | constructEnumEntry(Table& table, uint16_t attrHandle, uint8_t attrType, |
| 356 | const std::vector<uint8_t>& handleIndices); |
| 357 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 358 | /** @brief construct a table with an new entry |
| 359 | * @param[in] table - the table need to be updated |
| 360 | * @param[in] entry - the new attribute value entry |
| 361 | * @param[in] size - size of the new entry |
| 362 | * @return newly constructed table, std::nullopt if failed |
| 363 | */ |
| 364 | std::optional<Table> updateTable(const Table& table, const void* entry, |
| 365 | size_t size); |
| 366 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 367 | } // namespace attribute_value |
| 368 | |
| 369 | } // namespace table |
| 370 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 371 | } // namespace bios |
| 372 | } // namespace responder |
| 373 | } // namespace pldm |