Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #include "bios.hpp" |
| 2 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
Xiaochao Ma | 60227a0 | 2019-12-04 09:00:12 +0800 | [diff] [blame] | 6 | #include <time.h> |
| 7 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 8 | #include <array> |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 9 | #include <boost/crc.hpp> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <ctime> |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 12 | #include <filesystem> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 13 | #include <iostream> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 14 | #include <memory> |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 15 | #include <numeric> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 16 | #include <stdexcept> |
| 17 | #include <string> |
| 18 | #include <variant> |
| 19 | #include <vector> |
| 20 | |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 21 | namespace fs = std::filesystem; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 22 | using namespace pldm::responder::bios; |
| 23 | using namespace bios_parser; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 24 | |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 25 | constexpr auto stringTableFile = "stringTable"; |
| 26 | constexpr auto attrTableFile = "attributeTable"; |
| 27 | constexpr auto attrValTableFile = "attributeValueTable"; |
| 28 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 29 | namespace pldm |
| 30 | { |
| 31 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 32 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 33 | using EpochTimeUS = uint64_t; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 34 | using BIOSTableRow = std::vector<uint8_t>; |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 35 | using BIOSJsonName = std::string; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 36 | |
| 37 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 38 | constexpr auto padChksumMax = 7; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 39 | |
| 40 | namespace responder |
| 41 | { |
| 42 | |
| 43 | namespace utils |
| 44 | { |
| 45 | |
| 46 | void epochToBCDTime(uint64_t timeSec, uint8_t& seconds, uint8_t& minutes, |
| 47 | uint8_t& hours, uint8_t& day, uint8_t& month, |
| 48 | uint16_t& year) |
| 49 | { |
| 50 | auto t = time_t(timeSec); |
| 51 | auto time = localtime(&t); |
| 52 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 53 | seconds = pldm::utils::decimalToBcd(time->tm_sec); |
| 54 | minutes = pldm::utils::decimalToBcd(time->tm_min); |
| 55 | hours = pldm::utils::decimalToBcd(time->tm_hour); |
| 56 | day = pldm::utils::decimalToBcd(time->tm_mday); |
| 57 | month = pldm::utils::decimalToBcd(time->tm_mon + |
| 58 | 1); // The number of months in the range |
| 59 | // 0 to 11.PLDM expects range 1 to 12 |
| 60 | year = pldm::utils::decimalToBcd(time->tm_year + |
| 61 | 1900); // The number of years since 1900 |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Xiaochao Ma | 60227a0 | 2019-12-04 09:00:12 +0800 | [diff] [blame] | 64 | std::time_t timeToEpoch(uint8_t seconds, uint8_t minutes, uint8_t hours, |
| 65 | uint8_t day, uint8_t month, uint16_t year) |
| 66 | { |
| 67 | struct std::tm stm; |
| 68 | |
| 69 | stm.tm_year = year - 1900; |
| 70 | stm.tm_mon = month - 1; |
| 71 | stm.tm_mday = day; |
| 72 | stm.tm_hour = hours; |
| 73 | stm.tm_min = minutes; |
| 74 | stm.tm_sec = seconds; |
| 75 | stm.tm_isdst = -1; |
| 76 | |
| 77 | // It will get the time in seconds since |
| 78 | // Epoch, 1970.1.1 00:00:00 +0000,UTC. |
| 79 | return timegm(&stm); |
| 80 | } |
| 81 | |
John Wang | c293835 | 2019-09-30 13:58:41 +0800 | [diff] [blame] | 82 | size_t getTableTotalsize(size_t sizeWithoutPad) |
| 83 | { |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 84 | return sizeWithoutPad + pldm_bios_table_pad_checksum_size(sizeWithoutPad); |
John Wang | c293835 | 2019-09-30 13:58:41 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void padAndChecksum(Table& table) |
| 88 | { |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 89 | auto sizeWithoutPad = table.size(); |
| 90 | auto padAndChecksumSize = pldm_bios_table_pad_checksum_size(sizeWithoutPad); |
| 91 | table.resize(table.size() + padAndChecksumSize); |
John Wang | c293835 | 2019-09-30 13:58:41 +0800 | [diff] [blame] | 92 | |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 93 | pldm_bios_table_append_pad_checksum(table.data(), table.size(), |
| 94 | sizeWithoutPad); |
John Wang | c293835 | 2019-09-30 13:58:41 +0800 | [diff] [blame] | 95 | } |
| 96 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 97 | } // namespace utils |
| 98 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 99 | namespace bios |
| 100 | { |
| 101 | |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 102 | Handler::Handler() |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | fs::remove( |
| 107 | fs::path(std::string(BIOS_TABLES_DIR) + "/" + stringTableFile)); |
| 108 | fs::remove( |
| 109 | fs::path(std::string(BIOS_TABLES_DIR) + "/" + attrTableFile)); |
| 110 | fs::remove( |
| 111 | fs::path(std::string(BIOS_TABLES_DIR) + "/" + attrValTableFile)); |
| 112 | } |
| 113 | catch (const std::exception& e) |
| 114 | { |
| 115 | } |
Xiaochao Ma | 60227a0 | 2019-12-04 09:00:12 +0800 | [diff] [blame] | 116 | handlers.emplace(PLDM_SET_DATE_TIME, |
| 117 | [this](const pldm_msg* request, size_t payloadLength) { |
| 118 | return this->setDateTime(request, payloadLength); |
| 119 | }); |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 120 | handlers.emplace(PLDM_GET_DATE_TIME, |
| 121 | [this](const pldm_msg* request, size_t payloadLength) { |
| 122 | return this->getDateTime(request, payloadLength); |
| 123 | }); |
| 124 | handlers.emplace(PLDM_GET_BIOS_TABLE, |
| 125 | [this](const pldm_msg* request, size_t payloadLength) { |
| 126 | return this->getBIOSTable(request, payloadLength); |
| 127 | }); |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 128 | handlers.emplace(PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE, |
| 129 | [this](const pldm_msg* request, size_t payloadLength) { |
| 130 | return this->getBIOSAttributeCurrentValueByHandle( |
| 131 | request, payloadLength); |
| 132 | }); |
John Wang | 4217488 | 2019-12-20 14:56:59 +0800 | [diff] [blame] | 133 | handlers.emplace(PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE, |
| 134 | [this](const pldm_msg* request, size_t payloadLength) { |
| 135 | return this->setBIOSAttributeCurrentValue( |
| 136 | request, payloadLength); |
| 137 | }); |
Deepak Kodihalli | d9fb152 | 2019-11-28 10:06:34 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 140 | Response Handler::getDateTime(const pldm_msg* request, size_t /*payloadLength*/) |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 141 | { |
| 142 | uint8_t seconds = 0; |
| 143 | uint8_t minutes = 0; |
| 144 | uint8_t hours = 0; |
| 145 | uint8_t day = 0; |
| 146 | uint8_t month = 0; |
| 147 | uint16_t year = 0; |
| 148 | |
| 149 | constexpr auto timeInterface = "xyz.openbmc_project.Time.EpochTime"; |
George Liu | 408c3c4 | 2019-10-16 16:49:15 +0800 | [diff] [blame] | 150 | constexpr auto hostTimePath = "/xyz/openbmc_project/time/host"; |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 151 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES, 0); |
| 152 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | 782d37f | 2020-01-24 09:02:17 +0800 | [diff] [blame] | 153 | EpochTimeUS timeUsec; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 154 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 155 | try |
| 156 | { |
George Liu | 782d37f | 2020-01-24 09:02:17 +0800 | [diff] [blame] | 157 | timeUsec = pldm::utils::DBusHandler().getDbusProperty<EpochTimeUS>( |
| 158 | hostTimePath, "Elapsed", timeInterface); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 159 | } |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 160 | catch (const sdbusplus::exception::SdBusError& e) |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 161 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 162 | std::cerr << "Error getting time, PATH=" << hostTimePath |
| 163 | << " TIME INTERACE=" << timeInterface << "\n"; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 164 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 165 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 168 | uint64_t timeSec = std::chrono::duration_cast<std::chrono::seconds>( |
| 169 | std::chrono::microseconds(timeUsec)) |
| 170 | .count(); |
| 171 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 172 | pldm::responder::utils::epochToBCDTime(timeSec, seconds, minutes, hours, |
| 173 | day, month, year); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 174 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 175 | auto rc = encode_get_date_time_resp(request->hdr.instance_id, PLDM_SUCCESS, |
| 176 | seconds, minutes, hours, day, month, |
| 177 | year, responsePtr); |
| 178 | if (rc != PLDM_SUCCESS) |
| 179 | { |
| 180 | return ccOnlyResponse(request, rc); |
| 181 | } |
| 182 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 183 | return response; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 184 | } |
| 185 | |
Xiaochao Ma | 60227a0 | 2019-12-04 09:00:12 +0800 | [diff] [blame] | 186 | Response Handler::setDateTime(const pldm_msg* request, size_t payloadLength) |
| 187 | { |
| 188 | uint8_t seconds = 0; |
| 189 | uint8_t minutes = 0; |
| 190 | uint8_t hours = 0; |
| 191 | uint8_t day = 0; |
| 192 | uint8_t month = 0; |
| 193 | uint16_t year = 0; |
| 194 | std::time_t timeSec; |
| 195 | |
| 196 | constexpr auto setTimeInterface = "xyz.openbmc_project.Time.EpochTime"; |
| 197 | constexpr auto setTimePath = "/xyz/openbmc_project/time/host"; |
| 198 | constexpr auto timeSetPro = "Elapsed"; |
| 199 | |
| 200 | auto rc = decode_set_date_time_req(request, payloadLength, &seconds, |
| 201 | &minutes, &hours, &day, &month, &year); |
| 202 | if (rc != PLDM_SUCCESS) |
| 203 | { |
| 204 | return ccOnlyResponse(request, rc); |
| 205 | } |
| 206 | timeSec = pldm::responder::utils::timeToEpoch(seconds, minutes, hours, day, |
| 207 | month, year); |
| 208 | uint64_t timeUsec = std::chrono::duration_cast<std::chrono::microseconds>( |
| 209 | std::chrono::seconds(timeSec)) |
| 210 | .count(); |
| 211 | std::variant<uint64_t> value{timeUsec}; |
| 212 | try |
| 213 | { |
| 214 | pldm::utils::DBusHandler().setDbusProperty(setTimePath, timeSetPro, |
| 215 | setTimeInterface, value); |
| 216 | } |
| 217 | catch (std::exception& e) |
| 218 | { |
| 219 | |
| 220 | std::cerr << "Error Setting time,PATH=" << setTimePath |
| 221 | << "TIME INTERFACE=" << setTimeInterface |
| 222 | << "ERROR=" << e.what() << "\n"; |
| 223 | |
| 224 | return ccOnlyResponse(request, PLDM_ERROR); |
| 225 | } |
| 226 | |
| 227 | return ccOnlyResponse(request, PLDM_SUCCESS); |
| 228 | } |
| 229 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 230 | /** @brief Construct the BIOS string table |
| 231 | * |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 232 | * @param[in,out] biosStringTable - the string table |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 233 | * @param[in] request - Request message |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 234 | */ |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 235 | Response getBIOSStringTable(BIOSTable& biosStringTable, const pldm_msg* request) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 236 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 237 | { |
| 238 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 239 | 0); |
| 240 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 241 | if (!biosStringTable.isEmpty()) |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 242 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 243 | auto rc = encode_get_bios_table_resp( |
| 244 | request->hdr.instance_id, PLDM_SUCCESS, |
| 245 | 0, /* next transfer handle */ |
| 246 | PLDM_START_AND_END, nullptr, response.size(), |
| 247 | responsePtr); // filling up the header here |
| 248 | if (rc != PLDM_SUCCESS) |
| 249 | { |
| 250 | return CmdHandler::ccOnlyResponse(request, rc); |
| 251 | } |
| 252 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 253 | biosStringTable.load(response); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 254 | return response; |
| 255 | } |
| 256 | auto biosStrings = bios_parser::getStrings(); |
| 257 | std::sort(biosStrings.begin(), biosStrings.end()); |
| 258 | // remove all duplicate strings received from bios json |
| 259 | biosStrings.erase(std::unique(biosStrings.begin(), biosStrings.end()), |
| 260 | biosStrings.end()); |
| 261 | |
| 262 | size_t sizeWithoutPad = std::accumulate( |
| 263 | biosStrings.begin(), biosStrings.end(), 0, |
| 264 | [](size_t sum, const std::string& elem) { |
| 265 | return sum + |
| 266 | pldm_bios_table_string_entry_encode_length(elem.length()); |
| 267 | }); |
| 268 | |
| 269 | Table stringTable; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 270 | stringTable.reserve( |
| 271 | pldm::responder::utils::getTableTotalsize(sizeWithoutPad)); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 272 | |
| 273 | stringTable.resize(sizeWithoutPad); |
| 274 | auto tablePtr = stringTable.data(); |
| 275 | for (const auto& elem : biosStrings) |
| 276 | { |
| 277 | auto entry_length = |
| 278 | pldm_bios_table_string_entry_encode_length(elem.length()); |
| 279 | pldm_bios_table_string_entry_encode(tablePtr, sizeWithoutPad, |
| 280 | elem.c_str(), elem.length()); |
| 281 | tablePtr += entry_length; |
| 282 | sizeWithoutPad -= entry_length; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 285 | pldm::responder::utils::padAndChecksum(stringTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 286 | biosStringTable.store(stringTable); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 287 | response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + |
| 288 | stringTable.size(), |
| 289 | 0); |
| 290 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 291 | auto rc = encode_get_bios_table_resp( |
| 292 | request->hdr.instance_id, PLDM_SUCCESS, 0 /* nxtTransferHandle */, |
| 293 | PLDM_START_AND_END, stringTable.data(), response.size(), responsePtr); |
| 294 | if (rc != PLDM_SUCCESS) |
| 295 | { |
| 296 | return CmdHandler::ccOnlyResponse(request, rc); |
| 297 | } |
| 298 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 299 | return response; |
| 300 | } |
| 301 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 302 | namespace bios_type_enum |
| 303 | { |
| 304 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 305 | using namespace bios_parser::bios_enum; |
| 306 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 307 | /** @brief Find the indices into the array of the possible values of string |
| 308 | * handles for the current values.This is used in attribute value table |
| 309 | * |
| 310 | * @param[in] possiVals - vector of string handles comprising all the possible |
| 311 | * values for an attribute |
| 312 | * @param[in] currVals - vector of strings comprising all current values |
| 313 | * for an attribute |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 314 | * @param[in] biosStringTable - the string table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 315 | * |
| 316 | * @return - std::vector<uint8_t> - indices into the array of the possible |
| 317 | * values of string handles |
| 318 | */ |
| 319 | std::vector<uint8_t> findStrIndices(PossibleValuesByHandle possiVals, |
| 320 | CurrentValues currVals, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 321 | const BIOSStringTable& biosStringTable) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 322 | { |
| 323 | std::vector<uint8_t> stringIndices; |
| 324 | |
| 325 | for (const auto& currVal : currVals) |
| 326 | { |
| 327 | StringHandle curHdl; |
| 328 | try |
| 329 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 330 | curHdl = biosStringTable.findHandle(currVal); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 331 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 332 | catch (const std::exception& e) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 333 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 334 | std::cerr << "Exception fetching handle for the string, STRING=" |
| 335 | << currVal.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 336 | continue; |
| 337 | } |
| 338 | |
| 339 | uint8_t i = 0; |
| 340 | for (auto possiHdl : possiVals) |
| 341 | { |
| 342 | if (possiHdl == curHdl) |
| 343 | { |
| 344 | stringIndices.push_back(i); |
| 345 | break; |
| 346 | } |
| 347 | i++; |
| 348 | } |
| 349 | } |
| 350 | return stringIndices; |
| 351 | } |
| 352 | |
| 353 | /** @brief Find the indices into the array of the possible values of string |
| 354 | * handles for the default values. This is used in attribute table |
| 355 | * |
| 356 | * @param[in] possiVals - vector of strings comprising all the possible values |
| 357 | * for an attribute |
| 358 | * @param[in] defVals - vector of strings comprising all the default values |
| 359 | * for an attribute |
| 360 | * @return - std::vector<uint8_t> - indices into the array of the possible |
| 361 | * values of string |
| 362 | */ |
| 363 | std::vector<uint8_t> findDefaultValHandle(const PossibleValues& possiVals, |
| 364 | const DefaultValues& defVals) |
| 365 | { |
| 366 | std::vector<uint8_t> defHdls; |
| 367 | for (const auto& defs : defVals) |
| 368 | { |
John Wang | 59ef607 | 2020-01-21 13:37:01 +0800 | [diff] [blame] | 369 | auto index = std::find_if(possiVals.begin(), possiVals.end(), |
| 370 | [&defs](const auto& v) { return defs == v; }); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 371 | if (index != possiVals.end()) |
| 372 | { |
| 373 | defHdls.push_back(index - possiVals.begin()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return defHdls; |
| 378 | } |
| 379 | |
| 380 | /** @brief Construct the attibute table for BIOS type Enumeration and |
| 381 | * Enumeration ReadOnly |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 382 | * @param[in] biosStringTable - the string table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 383 | * @param[in] biosJsonDir - path where the BIOS json files are present |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 384 | * @param[in,out] attributeTable - the attribute table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 385 | * |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 386 | */ |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 387 | void constructAttrTable(const BIOSStringTable& biosStringTable, |
| 388 | Table& attributeTable) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 389 | { |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 390 | const auto& attributeMap = getValues(); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 391 | StringHandle strHandle; |
| 392 | |
| 393 | for (const auto& [key, value] : attributeMap) |
| 394 | { |
| 395 | try |
| 396 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 397 | strHandle = biosStringTable.findHandle(key); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 398 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 399 | catch (const std::exception& e) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 400 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 401 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 402 | << key.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 403 | continue; |
| 404 | } |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 405 | bool readOnly = (std::get<0>(value)); |
John Wang | 59ef607 | 2020-01-21 13:37:01 +0800 | [diff] [blame] | 406 | const PossibleValues& possiVals = std::get<1>(value); |
| 407 | const DefaultValues& defVals = std::get<2>(value); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 408 | |
| 409 | std::vector<StringHandle> possiValsByHdl; |
| 410 | for (const auto& elem : possiVals) |
| 411 | { |
| 412 | try |
| 413 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 414 | auto hdl = biosStringTable.findHandle(elem); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 415 | possiValsByHdl.push_back(std::move(hdl)); |
| 416 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 417 | catch (const std::exception& e) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 418 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 419 | std::cerr << "Could not find handle for BIOS string, STRING=" |
| 420 | << elem.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 421 | continue; |
| 422 | } |
| 423 | } |
| 424 | auto defValsByHdl = findDefaultValHandle(possiVals, defVals); |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 425 | auto entryLength = pldm_bios_table_attr_entry_enum_encode_length( |
| 426 | possiValsByHdl.size(), defValsByHdl.size()); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 427 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 428 | auto attrTableSize = attributeTable.size(); |
| 429 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 430 | struct pldm_bios_table_attr_entry_enum_info info = { |
| 431 | strHandle, |
| 432 | readOnly, |
| 433 | (uint8_t)possiValsByHdl.size(), |
| 434 | possiValsByHdl.data(), |
| 435 | (uint8_t)defValsByHdl.size(), |
| 436 | defValsByHdl.data(), |
| 437 | }; |
| 438 | pldm_bios_table_attr_entry_enum_encode( |
| 439 | attributeTable.data() + attrTableSize, entryLength, &info); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 440 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 441 | } |
| 442 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 443 | void constructAttrValueEntry( |
| 444 | const struct pldm_bios_attr_table_entry* attrTableEntry, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 445 | const std::string& attrName, const BIOSStringTable& biosStringTable, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 446 | Table& attrValueTable) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 447 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 448 | CurrentValues currVals; |
| 449 | try |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 450 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 451 | currVals = getAttrValue(attrName); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 452 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 453 | catch (const std::exception& e) |
| 454 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 455 | std::cerr << "getAttrValue returned error for attribute, NAME=" |
| 456 | << attrName.c_str() << " ERROR=" << e.what() << "\n"; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | uint8_t pv_num = |
| 460 | pldm_bios_table_attr_entry_enum_decode_pv_num(attrTableEntry); |
| 461 | PossibleValuesByHandle pvHdls(pv_num, 0); |
| 462 | pldm_bios_table_attr_entry_enum_decode_pv_hdls(attrTableEntry, |
| 463 | pvHdls.data(), pv_num); |
| 464 | std::sort(currVals.begin(), currVals.end()); |
| 465 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 466 | auto currValStrIndices = findStrIndices(pvHdls, currVals, biosStringTable); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 467 | |
| 468 | auto entryLength = pldm_bios_table_attr_value_entry_encode_enum_length( |
| 469 | currValStrIndices.size()); |
| 470 | auto tableSize = attrValueTable.size(); |
| 471 | attrValueTable.resize(tableSize + entryLength); |
| 472 | pldm_bios_table_attr_value_entry_encode_enum( |
| 473 | attrValueTable.data() + tableSize, entryLength, |
| 474 | attrTableEntry->attr_handle, attrTableEntry->attr_type, |
| 475 | currValStrIndices.size(), currValStrIndices.data()); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | } // end namespace bios_type_enum |
| 479 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 480 | namespace bios_type_string |
| 481 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 482 | |
| 483 | using namespace bios_parser::bios_string; |
| 484 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 485 | /** @brief Construct the attibute table for BIOS type String and |
| 486 | * String ReadOnly |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 487 | * @param[in] biosStringTable - the string table |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 488 | * @param[in] biosJsonDir - path where the BIOS json files are present |
| 489 | * @param[in,out] attributeTable - the attribute table |
| 490 | * |
| 491 | */ |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 492 | void constructAttrTable(const BIOSStringTable& biosStringTable, |
| 493 | Table& attributeTable) |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 494 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 495 | const auto& attributeMap = getValues(); |
| 496 | StringHandle strHandle; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 497 | for (const auto& [key, value] : attributeMap) |
| 498 | { |
| 499 | try |
| 500 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 501 | strHandle = biosStringTable.findHandle(key); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 502 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 503 | catch (const std::exception& e) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 504 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 505 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 506 | << key.c_str() << "\n"; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 507 | continue; |
| 508 | } |
| 509 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 510 | const auto& [readOnly, strType, minStrLen, maxStrLen, defaultStrLen, |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 511 | defaultStr] = value; |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 512 | auto entryLength = |
| 513 | pldm_bios_table_attr_entry_string_encode_length(defaultStrLen); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 514 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 515 | struct pldm_bios_table_attr_entry_string_info info = { |
| 516 | strHandle, readOnly, strType, minStrLen, |
| 517 | maxStrLen, defaultStrLen, defaultStr.data(), |
| 518 | }; |
| 519 | auto attrTableSize = attributeTable.size(); |
| 520 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 521 | pldm_bios_table_attr_entry_string_encode( |
| 522 | attributeTable.data() + attrTableSize, entryLength, &info); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 523 | } |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 524 | } |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 525 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 526 | void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry, |
| 527 | const std::string& attrName, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 528 | const BIOSStringTable& biosStringTable, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 529 | Table& attrValueTable) |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 530 | { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 531 | std::ignore = biosStringTable; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 532 | std::string currStr; |
| 533 | uint16_t currStrLen = 0; |
| 534 | try |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 535 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 536 | currStr = getAttrValue(attrName); |
| 537 | currStrLen = currStr.size(); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 538 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 539 | catch (const std::exception& e) |
| 540 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 541 | std::cerr << "getAttrValue returned error for attribute, NAME=" |
| 542 | << attrName.c_str() << " ERROR=" << e.what() << "\n"; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | auto entryLength = |
| 546 | pldm_bios_table_attr_value_entry_encode_string_length(currStrLen); |
| 547 | auto tableSize = attrValueTable.size(); |
| 548 | attrValueTable.resize(tableSize + entryLength); |
| 549 | pldm_bios_table_attr_value_entry_encode_string( |
| 550 | attrValueTable.data() + tableSize, entryLength, |
| 551 | attrTableEntry->attr_handle, attrTableEntry->attr_type, currStrLen, |
| 552 | currStr.c_str()); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 553 | } |
| 554 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 555 | } // end namespace bios_type_string |
| 556 | |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 557 | namespace bios_type_integer |
| 558 | { |
| 559 | |
| 560 | using namespace bios_parser::bios_integer; |
| 561 | |
| 562 | /** @brief Construct the attibute table for BIOS type Integer and |
| 563 | * Integer ReadOnly |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 564 | * @param[in] biosStringTable - the string table |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 565 | * @param[in,out] attributeTable - the attribute table |
| 566 | * |
| 567 | */ |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 568 | void constructAttrTable(const BIOSStringTable& biosStringTable, |
| 569 | Table& attributeTable) |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 570 | { |
| 571 | const auto& attributeMap = getValues(); |
| 572 | StringHandle strHandle; |
| 573 | for (const auto& [key, value] : attributeMap) |
| 574 | { |
| 575 | try |
| 576 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 577 | strHandle = biosStringTable.findHandle(key); |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 578 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 579 | catch (const std::exception& e) |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 580 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 581 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 582 | << key.c_str() << "\n"; |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 583 | continue; |
| 584 | } |
| 585 | |
| 586 | const auto& [readOnly, lowerBound, upperBound, scalarIncrement, |
| 587 | defaultValue] = value; |
| 588 | auto entryLength = pldm_bios_table_attr_entry_integer_encode_length(); |
| 589 | |
| 590 | struct pldm_bios_table_attr_entry_integer_info info = { |
| 591 | strHandle, readOnly, lowerBound, |
| 592 | upperBound, scalarIncrement, defaultValue, |
| 593 | }; |
| 594 | auto attrTableSize = attributeTable.size(); |
| 595 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 596 | pldm_bios_table_attr_entry_integer_encode( |
| 597 | attributeTable.data() + attrTableSize, entryLength, &info); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry, |
| 602 | const std::string& attrName, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 603 | const BIOSStringTable& biosStringTable, |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 604 | Table& attrValueTable) |
| 605 | { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 606 | std::ignore = biosStringTable; |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 607 | uint64_t currentValue; |
| 608 | try |
| 609 | { |
| 610 | currentValue = getAttrValue(attrName); |
| 611 | } |
| 612 | catch (const std::exception& e) |
| 613 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 614 | std::cerr << "Failed to get attribute value, NAME=" << attrName.c_str() |
| 615 | << " ERROR=" << e.what() << "\n"; |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | auto entryLength = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 619 | auto tableSize = attrValueTable.size(); |
| 620 | attrValueTable.resize(tableSize + entryLength); |
| 621 | pldm_bios_table_attr_value_entry_encode_integer( |
| 622 | attrValueTable.data() + tableSize, entryLength, |
| 623 | attrTableEntry->attr_handle, attrTableEntry->attr_type, currentValue); |
| 624 | } |
| 625 | |
| 626 | } // namespace bios_type_integer |
| 627 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 628 | void traverseBIOSAttrTable(const Table& biosAttrTable, |
| 629 | AttrTableEntryHandler handler) |
| 630 | { |
| 631 | std::unique_ptr<pldm_bios_table_iter, decltype(&pldm_bios_table_iter_free)> |
| 632 | iter(pldm_bios_table_iter_create(biosAttrTable.data(), |
| 633 | biosAttrTable.size(), |
| 634 | PLDM_BIOS_ATTR_TABLE), |
| 635 | pldm_bios_table_iter_free); |
| 636 | while (!pldm_bios_table_iter_is_end(iter.get())) |
| 637 | { |
| 638 | auto table_entry = pldm_bios_table_iter_attr_entry_value(iter.get()); |
| 639 | try |
| 640 | { |
| 641 | handler(table_entry); |
| 642 | } |
| 643 | catch (const std::exception& e) |
| 644 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 645 | std::cerr << "handler fails when traversing BIOSAttrTable, ERROR=" |
| 646 | << e.what() << "\n"; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 647 | } |
| 648 | pldm_bios_table_iter_next(iter.get()); |
| 649 | } |
| 650 | } |
| 651 | |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 652 | using typeHandler = std::function<void(const BIOSStringTable& biosStringTable, |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 653 | Table& attributeTable)>; |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 654 | std::map<BIOSJsonName, typeHandler> attrTypeHandlers{ |
| 655 | {bios_parser::bIOSEnumJson, bios_type_enum::constructAttrTable}, |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 656 | {bios_parser::bIOSStrJson, bios_type_string::constructAttrTable}, |
| 657 | {bios_parser::bIOSIntegerJson, bios_type_integer::constructAttrTable}, |
| 658 | }; |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 659 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 660 | /** @brief Construct the BIOS attribute table |
| 661 | * |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 662 | * @param[in,out] biosAttributeTable - the attribute table |
| 663 | * @param[in] biosStringTable - the string table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 664 | * @param[in] biosJsonDir - path where the BIOS json files are present |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 665 | * @param[in] request - Request message |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 666 | */ |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 667 | Response getBIOSAttributeTable(BIOSTable& biosAttributeTable, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 668 | const BIOSStringTable& biosStringTable, |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 669 | const char* biosJsonDir, const pldm_msg* request) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 670 | { |
| 671 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 672 | 0); |
| 673 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 674 | uint32_t nxtTransferHandle = 0; |
| 675 | uint8_t transferFlag = PLDM_START_AND_END; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 676 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 677 | if (biosAttributeTable.isEmpty()) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 678 | { // no persisted table, constructing fresh table and response |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 679 | Table attributeTable; |
| 680 | fs::path dir(biosJsonDir); |
| 681 | |
| 682 | for (auto it = attrTypeHandlers.begin(); it != attrTypeHandlers.end(); |
| 683 | it++) |
| 684 | { |
| 685 | fs::path file = dir / it->first; |
| 686 | if (fs::exists(file)) |
| 687 | { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 688 | it->second(biosStringTable, attributeTable); |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | if (attributeTable.empty()) |
| 693 | { // no available json file is found |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 694 | return CmdHandler::ccOnlyResponse(request, |
| 695 | PLDM_BIOS_TABLE_UNAVAILABLE); |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 696 | } |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 697 | pldm::responder::utils::padAndChecksum(attributeTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 698 | biosAttributeTable.store(attributeTable); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 699 | response.resize(sizeof(pldm_msg_hdr) + |
| 700 | PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + |
| 701 | attributeTable.size()); |
| 702 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 703 | |
| 704 | auto rc = encode_get_bios_table_resp( |
| 705 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 706 | transferFlag, attributeTable.data(), response.size(), responsePtr); |
| 707 | if (rc != PLDM_SUCCESS) |
| 708 | { |
| 709 | return CmdHandler::ccOnlyResponse(request, rc); |
| 710 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 711 | } |
| 712 | else |
| 713 | { // persisted table present, constructing response |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 714 | auto rc = encode_get_bios_table_resp( |
| 715 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 716 | transferFlag, nullptr, response.size(), |
| 717 | responsePtr); // filling up the header here |
| 718 | if (rc != PLDM_SUCCESS) |
| 719 | { |
| 720 | return CmdHandler::ccOnlyResponse(request, rc); |
| 721 | } |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 722 | biosAttributeTable.load(response); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return response; |
| 726 | } |
| 727 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 728 | using AttrValTableEntryConstructHandler = |
| 729 | std::function<void(const struct pldm_bios_attr_table_entry* tableEntry, |
| 730 | const std::string& attrName, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 731 | const BIOSStringTable& biosStringTable, Table& table)>; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 732 | |
| 733 | using AttrType = uint8_t; |
| 734 | const std::map<AttrType, AttrValTableEntryConstructHandler> |
| 735 | AttrValTableConstructMap{ |
| 736 | {PLDM_BIOS_STRING, bios_type_string::constructAttrValueEntry}, |
| 737 | {PLDM_BIOS_STRING_READ_ONLY, bios_type_string::constructAttrValueEntry}, |
| 738 | {PLDM_BIOS_ENUMERATION, bios_type_enum::constructAttrValueEntry}, |
| 739 | {PLDM_BIOS_ENUMERATION_READ_ONLY, |
| 740 | bios_type_enum::constructAttrValueEntry}, |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 741 | {PLDM_BIOS_INTEGER, bios_type_integer::constructAttrValueEntry}, |
| 742 | {PLDM_BIOS_INTEGER_READ_ONLY, |
| 743 | bios_type_integer::constructAttrValueEntry}, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | void constructAttrValueTableEntry( |
| 747 | const struct pldm_bios_attr_table_entry* attrEntry, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 748 | const BIOSStringTable& biosStringTable, Table& attributeValueTable) |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 749 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 750 | auto stringHandle = |
| 751 | pldm_bios_table_attr_entry_decode_string_handle(attrEntry); |
| 752 | try |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 753 | { |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 754 | auto attrName = biosStringTable.findString(stringHandle); |
| 755 | AttrValTableConstructMap.at(attrEntry->attr_type)( |
| 756 | attrEntry, attrName, biosStringTable, attributeValueTable); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 757 | } |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 758 | catch (const std::exception& e) |
| 759 | { |
| 760 | std::cerr << "constructAttrValueTableEntry Error: " << e.what() |
| 761 | << std::endl; |
| 762 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 763 | } |
| 764 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 765 | /** @brief Construct the BIOS attribute value table |
| 766 | * |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 767 | * @param[in,out] biosAttributeValueTable - the attribute value table |
| 768 | * @param[in] biosAttributeTable - the attribute table |
| 769 | * @param[in] biosStringTable - the string table |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 770 | * @param[in] request - Request message |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 771 | */ |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 772 | Response getBIOSAttributeValueTable(BIOSTable& biosAttributeValueTable, |
| 773 | const BIOSTable& biosAttributeTable, |
John Wang | e297b9f | 2020-02-03 10:18:13 +0800 | [diff] [blame] | 774 | const BIOSStringTable& biosStringTable, |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 775 | const pldm_msg* request) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 776 | { |
| 777 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 778 | 0); |
| 779 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 780 | uint32_t nxtTransferHandle = 0; |
| 781 | uint8_t transferFlag = PLDM_START_AND_END; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 782 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 783 | if (!biosAttributeValueTable.isEmpty()) |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 784 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 785 | auto rc = encode_get_bios_table_resp( |
| 786 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 787 | transferFlag, nullptr, response.size(), |
| 788 | responsePtr); // filling up the header here |
| 789 | if (rc != PLDM_SUCCESS) |
| 790 | { |
| 791 | return CmdHandler::ccOnlyResponse(request, rc); |
| 792 | } |
| 793 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 794 | biosAttributeValueTable.load(response); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 795 | return response; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 796 | } |
| 797 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 798 | Table attributeValueTable; |
| 799 | Table attributeTable; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 800 | biosAttributeTable.load(attributeTable); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 801 | traverseBIOSAttrTable( |
| 802 | attributeTable, |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 803 | [&biosStringTable, &attributeValueTable]( |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 804 | const struct pldm_bios_attr_table_entry* tableEntry) { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 805 | constructAttrValueTableEntry(tableEntry, biosStringTable, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 806 | attributeValueTable); |
| 807 | }); |
| 808 | if (attributeValueTable.empty()) |
| 809 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 810 | return CmdHandler::ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 811 | } |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 812 | pldm::responder::utils::padAndChecksum(attributeValueTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 813 | biosAttributeValueTable.store(attributeValueTable); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 814 | |
| 815 | response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + |
| 816 | attributeValueTable.size()); |
| 817 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 818 | auto rc = encode_get_bios_table_resp( |
| 819 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, transferFlag, |
| 820 | attributeValueTable.data(), response.size(), responsePtr); |
| 821 | if (rc != PLDM_SUCCESS) |
| 822 | { |
| 823 | return CmdHandler::ccOnlyResponse(request, rc); |
| 824 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 825 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 826 | return response; |
| 827 | } |
| 828 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 829 | Response Handler::getBIOSTable(const pldm_msg* request, size_t payloadLength) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 830 | { |
Deepak Kodihalli | c3d2089 | 2019-08-01 05:38:31 -0500 | [diff] [blame] | 831 | fs::create_directory(BIOS_TABLES_DIR); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 832 | auto response = internal::buildBIOSTables(request, payloadLength, |
| 833 | BIOS_JSONS_DIR, BIOS_TABLES_DIR); |
| 834 | |
| 835 | return response; |
| 836 | } |
| 837 | |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 838 | Response Handler::getBIOSAttributeCurrentValueByHandle(const pldm_msg* request, |
| 839 | size_t payloadLength) |
| 840 | { |
| 841 | uint32_t transferHandle; |
| 842 | uint8_t transferOpFlag; |
| 843 | uint16_t attributeHandle; |
| 844 | |
| 845 | auto rc = decode_get_bios_attribute_current_value_by_handle_req( |
| 846 | request, payloadLength, &transferHandle, &transferOpFlag, |
| 847 | &attributeHandle); |
| 848 | if (rc != PLDM_SUCCESS) |
| 849 | { |
| 850 | return ccOnlyResponse(request, rc); |
| 851 | } |
| 852 | |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 853 | fs::path tablesPath(BIOS_TABLES_DIR); |
| 854 | auto stringTablePath = tablesPath / stringTableFile; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 855 | BIOSTable biosStringTable(stringTablePath.c_str()); |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 856 | auto attrTablePath = tablesPath / attrTableFile; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 857 | BIOSTable biosAttributeTable(attrTablePath.c_str()); |
| 858 | if (biosAttributeTable.isEmpty() || biosStringTable.isEmpty()) |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 859 | { |
| 860 | return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
| 861 | } |
| 862 | |
| 863 | auto attrValueTablePath = tablesPath / attrValTableFile; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 864 | BIOSTable biosAttributeValueTable(attrValueTablePath.c_str()); |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 865 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 866 | if (biosAttributeValueTable.isEmpty()) |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 867 | { |
| 868 | Table attributeValueTable; |
| 869 | Table attributeTable; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 870 | biosAttributeTable.load(attributeTable); |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 871 | traverseBIOSAttrTable( |
| 872 | attributeTable, |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 873 | [&biosStringTable, &attributeValueTable]( |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 874 | const struct pldm_bios_attr_table_entry* tableEntry) { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 875 | constructAttrValueTableEntry(tableEntry, biosStringTable, |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 876 | attributeValueTable); |
| 877 | }); |
| 878 | if (attributeValueTable.empty()) |
| 879 | { |
| 880 | return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
| 881 | } |
| 882 | pldm::responder::utils::padAndChecksum(attributeValueTable); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 883 | biosAttributeValueTable.store(attributeValueTable); |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 884 | } |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 885 | |
| 886 | Response table; |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 887 | biosAttributeValueTable.load(table); |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 888 | |
| 889 | auto entry = pldm_bios_table_attr_value_find_by_handle( |
| 890 | table.data(), table.size(), attributeHandle); |
| 891 | if (entry == nullptr) |
| 892 | { |
| 893 | return ccOnlyResponse(request, PLDM_INVALID_BIOS_ATTR_HANDLE); |
| 894 | } |
| 895 | |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 896 | auto entryLength = pldm_bios_table_attr_value_entry_length(entry); |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 897 | Response response(sizeof(pldm_msg_hdr) + |
| 898 | PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES + |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 899 | entryLength, |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 900 | 0); |
| 901 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 902 | rc = encode_get_bios_current_value_by_handle_resp( |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 903 | request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END, |
| 904 | reinterpret_cast<const uint8_t*>(entry), entryLength, responsePtr); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 905 | if (rc != PLDM_SUCCESS) |
| 906 | { |
| 907 | return ccOnlyResponse(request, rc); |
| 908 | } |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 909 | |
| 910 | return response; |
| 911 | } |
| 912 | |
John Wang | 4217488 | 2019-12-20 14:56:59 +0800 | [diff] [blame] | 913 | Response Handler::setBIOSAttributeCurrentValue(const pldm_msg* request, |
| 914 | size_t payloadLength) |
| 915 | { |
| 916 | uint32_t transferHandle; |
| 917 | uint8_t transferOpFlag; |
| 918 | variable_field attributeField; |
| 919 | |
| 920 | auto rc = decode_set_bios_attribute_current_value_req( |
| 921 | request, payloadLength, &transferHandle, &transferOpFlag, |
| 922 | &attributeField); |
| 923 | if (rc != PLDM_SUCCESS) |
| 924 | { |
| 925 | return ccOnlyResponse(request, rc); |
| 926 | } |
| 927 | |
| 928 | fs::path tablesPath(BIOS_TABLES_DIR); |
| 929 | auto stringTablePath = tablesPath / stringTableFile; |
| 930 | BIOSStringTable biosStringTable(stringTablePath.c_str()); |
| 931 | auto attrTablePath = tablesPath / attrTableFile; |
| 932 | BIOSTable biosAttributeTable(attrTablePath.c_str()); |
| 933 | auto attrValueTablePath = tablesPath / attrValTableFile; |
| 934 | BIOSTable biosAttributeValueTable(attrValueTablePath.c_str()); |
| 935 | // TODO: Construct attribute value table if it's empty. (another commit) |
| 936 | |
| 937 | Response srcTable; |
| 938 | biosAttributeValueTable.load(srcTable); |
| 939 | |
| 940 | // Replace the old attribute with the new attribute, the size of table will |
| 941 | // change: |
| 942 | // sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) - |
| 943 | // sizeof(oldAttribute) + pad(4-byte alignment, max = |
| 944 | // 3) |
| 945 | // For simplicity, we use |
| 946 | // sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) + 3 |
| 947 | size_t destBufferLength = srcTable.size() + attributeField.length + 3; |
| 948 | Response destTable(destBufferLength); |
| 949 | size_t destTableLen = destTable.size(); |
| 950 | |
| 951 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 952 | srcTable.data(), srcTable.size(), destTable.data(), &destTableLen, |
| 953 | attributeField.ptr, attributeField.length); |
| 954 | destTable.resize(destTableLen); |
| 955 | |
| 956 | if (rc != PLDM_SUCCESS) |
| 957 | { |
| 958 | return ccOnlyResponse(request, rc); |
| 959 | } |
| 960 | |
| 961 | rc = setAttributeValueOnDbus(&attributeField, biosAttributeTable, |
| 962 | biosStringTable); |
| 963 | if (rc != PLDM_SUCCESS) |
| 964 | { |
| 965 | return ccOnlyResponse(request, rc); |
| 966 | } |
| 967 | |
| 968 | biosAttributeValueTable.store(destTable); |
| 969 | |
| 970 | return ccOnlyResponse(request, PLDM_SUCCESS); |
| 971 | } |
| 972 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 973 | namespace internal |
| 974 | { |
| 975 | |
| 976 | Response buildBIOSTables(const pldm_msg* request, size_t payloadLength, |
| 977 | const char* biosJsonDir, const char* biosTablePath) |
| 978 | { |
| 979 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 980 | 0); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 981 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 982 | if (setupConfig(biosJsonDir) != 0) |
| 983 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 984 | return CmdHandler::ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 985 | } |
| 986 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 987 | uint32_t transferHandle{}; |
| 988 | uint8_t transferOpFlag{}; |
| 989 | uint8_t tableType{}; |
| 990 | |
| 991 | auto rc = decode_get_bios_table_req(request, payloadLength, &transferHandle, |
| 992 | &transferOpFlag, &tableType); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 993 | if (rc != PLDM_SUCCESS) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 994 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 995 | return CmdHandler::ccOnlyResponse(request, rc); |
| 996 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 997 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 998 | BIOSTable biosStringTable( |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 999 | (std::string(biosTablePath) + "/" + stringTableFile).c_str()); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1000 | BIOSTable biosAttributeTable( |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1001 | (std::string(biosTablePath) + "/" + attrTableFile).c_str()); |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1002 | BIOSTable biosAttributeValueTable( |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1003 | (std::string(biosTablePath) + "/" + attrValTableFile).c_str()); |
| 1004 | switch (tableType) |
| 1005 | { |
| 1006 | case PLDM_BIOS_STRING_TABLE: |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1007 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1008 | response = getBIOSStringTable(biosStringTable, request); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1009 | break; |
| 1010 | case PLDM_BIOS_ATTR_TABLE: |
| 1011 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1012 | if (biosStringTable.isEmpty()) |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1013 | { |
| 1014 | rc = PLDM_BIOS_TABLE_UNAVAILABLE; |
| 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | response = getBIOSAttributeTable( |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1019 | biosAttributeTable, biosStringTable, biosJsonDir, request); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1020 | } |
| 1021 | break; |
| 1022 | case PLDM_BIOS_ATTR_VAL_TABLE: |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1023 | if (biosAttributeTable.isEmpty() || biosStringTable.isEmpty()) |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1024 | { |
| 1025 | rc = PLDM_BIOS_TABLE_UNAVAILABLE; |
| 1026 | } |
| 1027 | else |
| 1028 | { |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1029 | response = getBIOSAttributeValueTable(biosAttributeValueTable, |
| 1030 | biosAttributeTable, |
| 1031 | biosStringTable, request); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1032 | } |
| 1033 | break; |
| 1034 | default: |
| 1035 | rc = PLDM_INVALID_BIOS_TABLE_TYPE; |
| 1036 | break; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | if (rc != PLDM_SUCCESS) |
| 1040 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1041 | return CmdHandler::ccOnlyResponse(request, rc); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | return response; |
| 1045 | } |
| 1046 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1047 | } // namespace internal |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1048 | |
John Wang | f719f3b | 2020-01-17 08:46:22 +0800 | [diff] [blame] | 1049 | } // namespace bios |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1050 | } // namespace responder |
| 1051 | } // namespace pldm |