Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 1 | #include "bios_table.hpp" |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/base.h> |
| 4 | #include <libpldm/bios_table.h> |
| 5 | #include <libpldm/utils.h> |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 6 | |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 9 | #include <fstream> |
| 10 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 11 | namespace pldm |
| 12 | { |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 13 | namespace responder |
| 14 | { |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 15 | namespace bios |
| 16 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 17 | BIOSTable::BIOSTable(const char* filePath) : filePath(filePath) {} |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 18 | |
| 19 | bool BIOSTable::isEmpty() const noexcept |
| 20 | { |
| 21 | bool empty = false; |
| 22 | try |
| 23 | { |
| 24 | empty = fs::is_empty(filePath); |
| 25 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 26 | catch (const fs::filesystem_error& e) |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 27 | { |
| 28 | return true; |
| 29 | } |
| 30 | return empty; |
| 31 | } |
| 32 | |
| 33 | void BIOSTable::store(const Table& table) |
| 34 | { |
| 35 | std::ofstream stream(filePath.string(), std::ios::out | std::ios::binary); |
| 36 | stream.write(reinterpret_cast<const char*>(table.data()), table.size()); |
| 37 | } |
| 38 | |
| 39 | void BIOSTable::load(Response& response) const |
| 40 | { |
| 41 | auto currSize = response.size(); |
| 42 | auto fileSize = fs::file_size(filePath); |
| 43 | response.resize(currSize + fileSize); |
| 44 | std::ifstream stream(filePath.string(), std::ios::in | std::ios::binary); |
| 45 | stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize); |
| 46 | } |
| 47 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 48 | BIOSStringTable::BIOSStringTable(const Table& stringTable) : |
| 49 | stringTable(stringTable) |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 50 | {} |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 51 | |
| 52 | BIOSStringTable::BIOSStringTable(const BIOSTable& biosTable) |
| 53 | { |
| 54 | biosTable.load(stringTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | std::string BIOSStringTable::findString(uint16_t handle) const |
| 58 | { |
| 59 | auto stringEntry = pldm_bios_table_string_find_by_handle( |
| 60 | stringTable.data(), stringTable.size(), handle); |
| 61 | if (stringEntry == nullptr) |
| 62 | { |
| 63 | throw std::invalid_argument("Invalid String Handle"); |
| 64 | } |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 65 | return table::string::decodeString(stringEntry); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 66 | } |
| 67 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 68 | uint16_t BIOSStringTable::findHandle(const std::string& name) const |
| 69 | { |
| 70 | auto stringEntry = pldm_bios_table_string_find_by_string( |
| 71 | stringTable.data(), stringTable.size(), name.c_str()); |
| 72 | if (stringEntry == nullptr) |
| 73 | { |
| 74 | throw std::invalid_argument("Invalid String Name"); |
| 75 | } |
| 76 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 77 | return table::string::decodeHandle(stringEntry); |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 78 | } |
| 79 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 80 | namespace table |
| 81 | { |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 82 | void appendPadAndChecksum(Table& table) |
| 83 | { |
| 84 | auto sizeWithoutPad = table.size(); |
| 85 | auto padAndChecksumSize = pldm_bios_table_pad_checksum_size(sizeWithoutPad); |
| 86 | table.resize(table.size() + padAndChecksumSize); |
| 87 | |
| 88 | pldm_bios_table_append_pad_checksum(table.data(), table.size(), |
| 89 | sizeWithoutPad); |
| 90 | } |
| 91 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 92 | namespace string |
| 93 | { |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 94 | uint16_t decodeHandle(const pldm_bios_string_table_entry* entry) |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 95 | { |
| 96 | return pldm_bios_table_string_entry_decode_handle(entry); |
| 97 | } |
| 98 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 99 | std::string decodeString(const pldm_bios_string_table_entry* entry) |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 100 | { |
| 101 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 102 | std::vector<char> buffer(strLength + 1 /* sizeof '\0' */); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 103 | // Preconditions are upheld therefore no error check necessary |
| 104 | pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(), |
| 105 | buffer.size()); |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 106 | return std::string(buffer.data(), buffer.data() + strLength); |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 107 | } |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 108 | const pldm_bios_string_table_entry* constructEntry(Table& table, |
| 109 | const std::string& str) |
| 110 | { |
| 111 | auto tableSize = table.size(); |
| 112 | auto entryLength = pldm_bios_table_string_entry_encode_length(str.length()); |
| 113 | table.resize(tableSize + entryLength); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 114 | // Preconditions are upheld therefore no error check necessary |
| 115 | pldm_bios_table_string_entry_encode_check( |
| 116 | table.data() + tableSize, entryLength, str.c_str(), str.length()); |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 117 | return reinterpret_cast<pldm_bios_string_table_entry*>(table.data() + |
| 118 | tableSize); |
| 119 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 120 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 121 | } // namespace string |
| 122 | |
| 123 | namespace attribute |
| 124 | { |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 125 | TableHeader decodeHeader(const pldm_bios_attr_table_entry* entry) |
| 126 | { |
| 127 | auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry); |
| 128 | auto attrType = pldm_bios_table_attr_entry_decode_attribute_type(entry); |
| 129 | auto stringHandle = pldm_bios_table_attr_entry_decode_string_handle(entry); |
| 130 | return {attrHandle, attrType, stringHandle}; |
| 131 | } |
| 132 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 133 | const pldm_bios_attr_table_entry* findByHandle(const Table& table, |
| 134 | uint16_t handle) |
| 135 | { |
| 136 | return pldm_bios_table_attr_find_by_handle(table.data(), table.size(), |
| 137 | handle); |
| 138 | } |
| 139 | |
John Wang | 45fed20 | 2020-04-01 16:42:26 +0800 | [diff] [blame] | 140 | const pldm_bios_attr_table_entry* findByStringHandle(const Table& table, |
| 141 | uint16_t handle) |
| 142 | { |
| 143 | return pldm_bios_table_attr_find_by_string_handle(table.data(), |
| 144 | table.size(), handle); |
| 145 | } |
| 146 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 147 | const pldm_bios_attr_table_entry* |
| 148 | constructStringEntry(Table& table, |
| 149 | pldm_bios_table_attr_entry_string_info* info) |
| 150 | { |
| 151 | auto entryLength = |
| 152 | pldm_bios_table_attr_entry_string_encode_length(info->def_length); |
| 153 | |
| 154 | auto tableSize = table.size(); |
| 155 | table.resize(tableSize + entryLength, 0); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 156 | int rc = pldm_bios_table_attr_entry_string_encode_check( |
| 157 | table.data() + tableSize, entryLength, info); |
| 158 | if (rc != PLDM_SUCCESS) |
| 159 | { |
| 160 | lg2::error("Failed to encode BIOS table string entry: {LIBPLDM_ERROR}", |
| 161 | "LIBPLDM_ERROR", rc); |
| 162 | throw std::runtime_error("Failed to encode BIOS table string entry"); |
| 163 | } |
| 164 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 165 | return reinterpret_cast<pldm_bios_attr_table_entry*>(table.data() + |
| 166 | tableSize); |
| 167 | } |
| 168 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 169 | const pldm_bios_attr_table_entry* |
| 170 | constructIntegerEntry(Table& table, |
| 171 | pldm_bios_table_attr_entry_integer_info* info) |
| 172 | { |
| 173 | auto entryLength = pldm_bios_table_attr_entry_integer_encode_length(); |
| 174 | auto tableSize = table.size(); |
| 175 | table.resize(tableSize + entryLength, 0); |
| 176 | pldm_bios_table_attr_entry_integer_encode(table.data() + tableSize, |
| 177 | entryLength, info); |
| 178 | return reinterpret_cast<pldm_bios_attr_table_entry*>(table.data() + |
| 179 | tableSize); |
| 180 | } |
| 181 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 182 | StringField decodeStringEntry(const pldm_bios_attr_table_entry* entry) |
| 183 | { |
| 184 | auto strType = pldm_bios_table_attr_entry_string_decode_string_type(entry); |
| 185 | auto minLength = pldm_bios_table_attr_entry_string_decode_min_length(entry); |
| 186 | auto maxLength = pldm_bios_table_attr_entry_string_decode_max_length(entry); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 187 | uint16_t defLength; |
| 188 | int rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 189 | entry, &defLength); |
| 190 | if (rc != PLDM_SUCCESS) |
| 191 | { |
| 192 | lg2::error( |
| 193 | "Failed to decode BIOS table string definition length: {LIBPLDM_ERROR}", |
| 194 | "LIBPLDM_ERROR", rc); |
| 195 | throw std::runtime_error( |
| 196 | "Failed to decode BIOS table string definitionlength"); |
| 197 | } |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 198 | |
| 199 | std::vector<char> buffer(defLength + 1); |
| 200 | pldm_bios_table_attr_entry_string_decode_def_string(entry, buffer.data(), |
| 201 | buffer.size()); |
| 202 | return {strType, minLength, maxLength, defLength, |
| 203 | std::string(buffer.data(), buffer.data() + defLength)}; |
| 204 | } |
| 205 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 206 | IntegerField decodeIntegerEntry(const pldm_bios_attr_table_entry* entry) |
| 207 | { |
| 208 | uint64_t lower, upper, def; |
| 209 | uint32_t scalar; |
| 210 | |
| 211 | pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar, |
| 212 | &def); |
| 213 | return {lower, upper, scalar, def}; |
| 214 | } |
| 215 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 216 | const pldm_bios_attr_table_entry* |
| 217 | constructEnumEntry(Table& table, pldm_bios_table_attr_entry_enum_info* info) |
| 218 | { |
| 219 | auto entryLength = pldm_bios_table_attr_entry_enum_encode_length( |
| 220 | info->pv_num, info->def_num); |
| 221 | |
| 222 | auto tableSize = table.size(); |
| 223 | table.resize(tableSize + entryLength, 0); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 224 | // Preconditions are upheld therefore no error check necessary |
| 225 | pldm_bios_table_attr_entry_enum_encode_check(table.data() + tableSize, |
| 226 | entryLength, info); |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 227 | |
| 228 | return reinterpret_cast<pldm_bios_attr_table_entry*>(table.data() + |
| 229 | tableSize); |
| 230 | } |
| 231 | |
| 232 | EnumField decodeEnumEntry(const pldm_bios_attr_table_entry* entry) |
| 233 | { |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 234 | uint8_t pvNum; |
| 235 | int rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNum); |
| 236 | if (rc != PLDM_SUCCESS) |
| 237 | { |
| 238 | lg2::error( |
| 239 | "Failed to decode the number of possible values for BIOS table enum entry: {LIBPLDM_ERROR}", |
| 240 | "LIBPLDM_ERROR", rc); |
| 241 | throw std::runtime_error( |
| 242 | "Failed to decode the number of possible values for BIOS table enum entry"); |
| 243 | } |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 244 | std::vector<uint16_t> pvHdls(pvNum, 0); |
Andrew Jeffery | 488f19d | 2023-06-13 20:43:39 +0930 | [diff] [blame] | 245 | // Preconditions are upheld therefore no error check necessary |
| 246 | pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, pvHdls.data(), |
| 247 | pvNum); |
| 248 | // Preconditions are upheld therefore no error check necessary |
| 249 | uint8_t defNum; |
| 250 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNum); |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 251 | std::vector<uint8_t> defIndices(defNum, 0); |
| 252 | pldm_bios_table_attr_entry_enum_decode_def_indices(entry, defIndices.data(), |
| 253 | defIndices.size()); |
| 254 | return {pvHdls, defIndices}; |
| 255 | } |
| 256 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 257 | } // namespace attribute |
| 258 | |
| 259 | namespace attribute_value |
| 260 | { |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 261 | TableHeader decodeHeader(const pldm_bios_attr_val_table_entry* entry) |
| 262 | { |
| 263 | auto handle = |
| 264 | pldm_bios_table_attr_value_entry_decode_attribute_handle(entry); |
| 265 | auto type = pldm_bios_table_attr_value_entry_decode_attribute_type(entry); |
| 266 | return {handle, type}; |
| 267 | } |
| 268 | |
| 269 | std::string decodeStringEntry(const pldm_bios_attr_val_table_entry* entry) |
| 270 | { |
| 271 | variable_field currentString{}; |
| 272 | pldm_bios_table_attr_value_entry_string_decode_string(entry, |
| 273 | ¤tString); |
| 274 | return std::string(currentString.ptr, |
| 275 | currentString.ptr + currentString.length); |
| 276 | } |
| 277 | |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 278 | uint64_t decodeIntegerEntry(const pldm_bios_attr_val_table_entry* entry) |
| 279 | { |
| 280 | return pldm_bios_table_attr_value_entry_integer_decode_cv(entry); |
| 281 | } |
| 282 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 283 | std::vector<uint8_t> |
| 284 | decodeEnumEntry(const pldm_bios_attr_val_table_entry* entry) |
| 285 | { |
| 286 | auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry); |
| 287 | std::vector<uint8_t> currHdls(number, 0); |
| 288 | pldm_bios_table_attr_value_entry_enum_decode_handles(entry, currHdls.data(), |
| 289 | currHdls.size()); |
| 290 | return currHdls; |
| 291 | } |
| 292 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 293 | const pldm_bios_attr_val_table_entry* |
| 294 | constructStringEntry(Table& table, uint16_t attrHandle, uint8_t attrType, |
| 295 | const std::string& str) |
| 296 | { |
| 297 | auto strLen = str.size(); |
| 298 | auto entryLength = |
| 299 | pldm_bios_table_attr_value_entry_encode_string_length(strLen); |
| 300 | auto tableSize = table.size(); |
| 301 | table.resize(tableSize + entryLength); |
| 302 | pldm_bios_table_attr_value_entry_encode_string( |
| 303 | table.data() + tableSize, entryLength, attrHandle, attrType, strLen, |
| 304 | str.c_str()); |
| 305 | return reinterpret_cast<pldm_bios_attr_val_table_entry*>(table.data() + |
| 306 | tableSize); |
| 307 | } |
John Wang | 95e6b3c | 2020-02-13 09:43:24 +0800 | [diff] [blame] | 308 | |
| 309 | const pldm_bios_attr_val_table_entry* constructIntegerEntry(Table& table, |
| 310 | uint16_t attrHandle, |
| 311 | uint8_t attrType, |
| 312 | uint64_t value) |
| 313 | { |
| 314 | auto entryLength = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 315 | |
| 316 | auto tableSize = table.size(); |
| 317 | table.resize(tableSize + entryLength); |
| 318 | pldm_bios_table_attr_value_entry_encode_integer( |
| 319 | table.data() + tableSize, entryLength, attrHandle, attrType, value); |
| 320 | return reinterpret_cast<pldm_bios_attr_val_table_entry*>(table.data() + |
| 321 | tableSize); |
| 322 | } |
| 323 | |
John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 324 | const pldm_bios_attr_val_table_entry* |
| 325 | constructEnumEntry(Table& table, uint16_t attrHandle, uint8_t attrType, |
| 326 | const std::vector<uint8_t>& handleIndices) |
| 327 | { |
| 328 | auto entryLength = pldm_bios_table_attr_value_entry_encode_enum_length( |
| 329 | handleIndices.size()); |
| 330 | auto tableSize = table.size(); |
| 331 | table.resize(tableSize + entryLength); |
| 332 | pldm_bios_table_attr_value_entry_encode_enum( |
| 333 | table.data() + tableSize, entryLength, attrHandle, attrType, |
| 334 | handleIndices.size(), handleIndices.data()); |
| 335 | return reinterpret_cast<pldm_bios_attr_val_table_entry*>(table.data() + |
| 336 | tableSize); |
| 337 | } |
| 338 | |
John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 339 | std::optional<Table> updateTable(const Table& table, const void* entry, |
| 340 | size_t size) |
| 341 | { |
| 342 | // Replace the old attribute with the new attribute, the size of table will |
| 343 | // change: |
| 344 | // sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) - |
| 345 | // sizeof(oldAttribute) + pad(4-byte alignment, max = |
| 346 | // 3) |
| 347 | // For simplicity, we use |
| 348 | // sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) + 3 |
| 349 | size_t destBufferLength = table.size() + size + 3; |
| 350 | Table destTable(destBufferLength); |
| 351 | |
| 352 | auto rc = pldm_bios_table_attr_value_copy_and_update( |
| 353 | table.data(), table.size(), destTable.data(), &destBufferLength, entry, |
| 354 | size); |
| 355 | if (rc != PLDM_SUCCESS) |
| 356 | { |
| 357 | return std::nullopt; |
| 358 | } |
| 359 | destTable.resize(destBufferLength); |
| 360 | |
| 361 | return destTable; |
| 362 | } |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 363 | |
| 364 | } // namespace attribute_value |
| 365 | |
| 366 | } // namespace table |
| 367 | |
Deepak Kodihalli | cb7f2d4 | 2019-06-19 13:25:31 +0530 | [diff] [blame] | 368 | } // namespace bios |
| 369 | } // namespace responder |
| 370 | } // namespace pldm |