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