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