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); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 334 | if (stringEntry == nullptr) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 335 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 336 | std::cerr << "Reached end of BIOS string table,did not find " |
| 337 | << "string name for handle, STRING_HANDLE=" << stringHdl |
| 338 | << "\n"; |
John Wang | 88a349c | 2020-01-20 14:19:49 +0800 | [diff] [blame^] | 339 | throw InternalFailure(); |
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); |
John Wang | 88a349c | 2020-01-20 14:19:49 +0800 | [diff] [blame^] | 343 | std::vector<char> buffer(strLength + 1); |
| 344 | pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(), |
| 345 | buffer.size()); |
| 346 | |
| 347 | return std::string(buffer.data(), buffer.data() + strLength); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | namespace bios_type_enum |
| 351 | { |
| 352 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 353 | using namespace bios_parser::bios_enum; |
| 354 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 355 | /** @brief Find the indices into the array of the possible values of string |
| 356 | * handles for the current values.This is used in attribute value table |
| 357 | * |
| 358 | * @param[in] possiVals - vector of string handles comprising all the possible |
| 359 | * values for an attribute |
| 360 | * @param[in] currVals - vector of strings comprising all current values |
| 361 | * for an attribute |
| 362 | * @param[in] BIOSStringTable - the string table |
| 363 | * |
| 364 | * @return - std::vector<uint8_t> - indices into the array of the possible |
| 365 | * values of string handles |
| 366 | */ |
| 367 | std::vector<uint8_t> findStrIndices(PossibleValuesByHandle possiVals, |
| 368 | CurrentValues currVals, |
| 369 | const BIOSTable& BIOSStringTable) |
| 370 | { |
| 371 | std::vector<uint8_t> stringIndices; |
| 372 | |
| 373 | for (const auto& currVal : currVals) |
| 374 | { |
| 375 | StringHandle curHdl; |
| 376 | try |
| 377 | { |
| 378 | curHdl = findStringHandle(currVal, BIOSStringTable); |
| 379 | } |
| 380 | catch (InternalFailure& e) |
| 381 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 382 | std::cerr << "Exception fetching handle for the string, STRING=" |
| 383 | << currVal.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | |
| 387 | uint8_t i = 0; |
| 388 | for (auto possiHdl : possiVals) |
| 389 | { |
| 390 | if (possiHdl == curHdl) |
| 391 | { |
| 392 | stringIndices.push_back(i); |
| 393 | break; |
| 394 | } |
| 395 | i++; |
| 396 | } |
| 397 | } |
| 398 | return stringIndices; |
| 399 | } |
| 400 | |
| 401 | /** @brief Find the indices into the array of the possible values of string |
| 402 | * handles for the default values. This is used in attribute table |
| 403 | * |
| 404 | * @param[in] possiVals - vector of strings comprising all the possible values |
| 405 | * for an attribute |
| 406 | * @param[in] defVals - vector of strings comprising all the default values |
| 407 | * for an attribute |
| 408 | * @return - std::vector<uint8_t> - indices into the array of the possible |
| 409 | * values of string |
| 410 | */ |
| 411 | std::vector<uint8_t> findDefaultValHandle(const PossibleValues& possiVals, |
| 412 | const DefaultValues& defVals) |
| 413 | { |
| 414 | std::vector<uint8_t> defHdls; |
| 415 | for (const auto& defs : defVals) |
| 416 | { |
| 417 | auto index = std::lower_bound(possiVals.begin(), possiVals.end(), defs); |
| 418 | if (index != possiVals.end()) |
| 419 | { |
| 420 | defHdls.push_back(index - possiVals.begin()); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return defHdls; |
| 425 | } |
| 426 | |
| 427 | /** @brief Construct the attibute table for BIOS type Enumeration and |
| 428 | * Enumeration ReadOnly |
| 429 | * @param[in] BIOSStringTable - the string table |
| 430 | * @param[in] biosJsonDir - path where the BIOS json files are present |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 431 | * @param[in,out] attributeTable - the attribute table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 432 | * |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 433 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 434 | void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 435 | { |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 436 | const auto& attributeMap = getValues(); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 437 | StringHandle strHandle; |
| 438 | |
| 439 | for (const auto& [key, value] : attributeMap) |
| 440 | { |
| 441 | try |
| 442 | { |
| 443 | strHandle = findStringHandle(key, BIOSStringTable); |
| 444 | } |
| 445 | catch (InternalFailure& e) |
| 446 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 447 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 448 | << key.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 449 | continue; |
| 450 | } |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 451 | bool readOnly = (std::get<0>(value)); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 452 | PossibleValues possiVals = std::get<1>(value); |
| 453 | DefaultValues defVals = std::get<2>(value); |
| 454 | // both the possible and default values are stored in sorted manner to |
| 455 | // ease in fetching back/comparison |
| 456 | std::sort(possiVals.begin(), possiVals.end()); |
| 457 | std::sort(defVals.begin(), defVals.end()); |
| 458 | |
| 459 | std::vector<StringHandle> possiValsByHdl; |
| 460 | for (const auto& elem : possiVals) |
| 461 | { |
| 462 | try |
| 463 | { |
| 464 | auto hdl = findStringHandle(elem, BIOSStringTable); |
| 465 | possiValsByHdl.push_back(std::move(hdl)); |
| 466 | } |
| 467 | catch (InternalFailure& e) |
| 468 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 469 | std::cerr << "Could not find handle for BIOS string, STRING=" |
| 470 | << elem.c_str() << "\n"; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | } |
| 474 | auto defValsByHdl = findDefaultValHandle(possiVals, defVals); |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 475 | auto entryLength = pldm_bios_table_attr_entry_enum_encode_length( |
| 476 | possiValsByHdl.size(), defValsByHdl.size()); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 477 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 478 | auto attrTableSize = attributeTable.size(); |
| 479 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 480 | struct pldm_bios_table_attr_entry_enum_info info = { |
| 481 | strHandle, |
| 482 | readOnly, |
| 483 | (uint8_t)possiValsByHdl.size(), |
| 484 | possiValsByHdl.data(), |
| 485 | (uint8_t)defValsByHdl.size(), |
| 486 | defValsByHdl.data(), |
| 487 | }; |
| 488 | pldm_bios_table_attr_entry_enum_encode( |
| 489 | attributeTable.data() + attrTableSize, entryLength, &info); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 490 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 491 | } |
| 492 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 493 | void constructAttrValueEntry( |
| 494 | const struct pldm_bios_attr_table_entry* attrTableEntry, |
| 495 | const std::string& attrName, const BIOSTable& BIOSStringTable, |
| 496 | Table& attrValueTable) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 497 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 498 | CurrentValues currVals; |
| 499 | try |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 500 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 501 | currVals = getAttrValue(attrName); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 502 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 503 | catch (const std::exception& e) |
| 504 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 505 | std::cerr << "getAttrValue returned error for attribute, NAME=" |
| 506 | << attrName.c_str() << " ERROR=" << e.what() << "\n"; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | uint8_t pv_num = |
| 510 | pldm_bios_table_attr_entry_enum_decode_pv_num(attrTableEntry); |
| 511 | PossibleValuesByHandle pvHdls(pv_num, 0); |
| 512 | pldm_bios_table_attr_entry_enum_decode_pv_hdls(attrTableEntry, |
| 513 | pvHdls.data(), pv_num); |
| 514 | std::sort(currVals.begin(), currVals.end()); |
| 515 | |
| 516 | auto currValStrIndices = findStrIndices(pvHdls, currVals, BIOSStringTable); |
| 517 | |
| 518 | auto entryLength = pldm_bios_table_attr_value_entry_encode_enum_length( |
| 519 | currValStrIndices.size()); |
| 520 | auto tableSize = attrValueTable.size(); |
| 521 | attrValueTable.resize(tableSize + entryLength); |
| 522 | pldm_bios_table_attr_value_entry_encode_enum( |
| 523 | attrValueTable.data() + tableSize, entryLength, |
| 524 | attrTableEntry->attr_handle, attrTableEntry->attr_type, |
| 525 | currValStrIndices.size(), currValStrIndices.data()); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | } // end namespace bios_type_enum |
| 529 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 530 | namespace bios_type_string |
| 531 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 532 | |
| 533 | using namespace bios_parser::bios_string; |
| 534 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 535 | /** @brief Construct the attibute table for BIOS type String and |
| 536 | * String ReadOnly |
| 537 | * @param[in] BIOSStringTable - the string table |
| 538 | * @param[in] biosJsonDir - path where the BIOS json files are present |
| 539 | * @param[in,out] attributeTable - the attribute table |
| 540 | * |
| 541 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 542 | void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable) |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 543 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 544 | const auto& attributeMap = getValues(); |
| 545 | StringHandle strHandle; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 546 | for (const auto& [key, value] : attributeMap) |
| 547 | { |
| 548 | try |
| 549 | { |
| 550 | strHandle = findStringHandle(key, BIOSStringTable); |
| 551 | } |
| 552 | catch (InternalFailure& e) |
| 553 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 554 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 555 | << key.c_str() << "\n"; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 556 | continue; |
| 557 | } |
| 558 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 559 | const auto& [readOnly, strType, minStrLen, maxStrLen, defaultStrLen, |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 560 | defaultStr] = value; |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 561 | auto entryLength = |
| 562 | pldm_bios_table_attr_entry_string_encode_length(defaultStrLen); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 563 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 564 | struct pldm_bios_table_attr_entry_string_info info = { |
| 565 | strHandle, readOnly, strType, minStrLen, |
| 566 | maxStrLen, defaultStrLen, defaultStr.data(), |
| 567 | }; |
| 568 | auto attrTableSize = attributeTable.size(); |
| 569 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 570 | pldm_bios_table_attr_entry_string_encode( |
| 571 | attributeTable.data() + attrTableSize, entryLength, &info); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 572 | } |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 573 | } |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 574 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 575 | void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry, |
| 576 | const std::string& attrName, |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 577 | const BIOSTable& BIOSStringTable, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 578 | Table& attrValueTable) |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 579 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 580 | std::ignore = BIOSStringTable; |
| 581 | std::string currStr; |
| 582 | uint16_t currStrLen = 0; |
| 583 | try |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 584 | { |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 585 | currStr = getAttrValue(attrName); |
| 586 | currStrLen = currStr.size(); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 587 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 588 | catch (const std::exception& e) |
| 589 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 590 | std::cerr << "getAttrValue returned error for attribute, NAME=" |
| 591 | << attrName.c_str() << " ERROR=" << e.what() << "\n"; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 592 | return; |
| 593 | } |
| 594 | auto entryLength = |
| 595 | pldm_bios_table_attr_value_entry_encode_string_length(currStrLen); |
| 596 | auto tableSize = attrValueTable.size(); |
| 597 | attrValueTable.resize(tableSize + entryLength); |
| 598 | pldm_bios_table_attr_value_entry_encode_string( |
| 599 | attrValueTable.data() + tableSize, entryLength, |
| 600 | attrTableEntry->attr_handle, attrTableEntry->attr_type, currStrLen, |
| 601 | currStr.c_str()); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 602 | } |
| 603 | |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 604 | } // end namespace bios_type_string |
| 605 | |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 606 | namespace bios_type_integer |
| 607 | { |
| 608 | |
| 609 | using namespace bios_parser::bios_integer; |
| 610 | |
| 611 | /** @brief Construct the attibute table for BIOS type Integer and |
| 612 | * Integer ReadOnly |
| 613 | * @param[in] BIOSStringTable - the string table |
| 614 | * @param[in,out] attributeTable - the attribute table |
| 615 | * |
| 616 | */ |
| 617 | void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable) |
| 618 | { |
| 619 | const auto& attributeMap = getValues(); |
| 620 | StringHandle strHandle; |
| 621 | for (const auto& [key, value] : attributeMap) |
| 622 | { |
| 623 | try |
| 624 | { |
| 625 | strHandle = findStringHandle(key, BIOSStringTable); |
| 626 | } |
| 627 | catch (InternalFailure& e) |
| 628 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 629 | std::cerr << "Could not find handle for BIOS string, ATTRIBUTE=" |
| 630 | << key.c_str() << "\n"; |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 631 | continue; |
| 632 | } |
| 633 | |
| 634 | const auto& [readOnly, lowerBound, upperBound, scalarIncrement, |
| 635 | defaultValue] = value; |
| 636 | auto entryLength = pldm_bios_table_attr_entry_integer_encode_length(); |
| 637 | |
| 638 | struct pldm_bios_table_attr_entry_integer_info info = { |
| 639 | strHandle, readOnly, lowerBound, |
| 640 | upperBound, scalarIncrement, defaultValue, |
| 641 | }; |
| 642 | auto attrTableSize = attributeTable.size(); |
| 643 | attributeTable.resize(attrTableSize + entryLength, 0); |
| 644 | pldm_bios_table_attr_entry_integer_encode( |
| 645 | attributeTable.data() + attrTableSize, entryLength, &info); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry, |
| 650 | const std::string& attrName, |
| 651 | const BIOSTable& BIOSStringTable, |
| 652 | Table& attrValueTable) |
| 653 | { |
| 654 | std::ignore = BIOSStringTable; |
| 655 | uint64_t currentValue; |
| 656 | try |
| 657 | { |
| 658 | currentValue = getAttrValue(attrName); |
| 659 | } |
| 660 | catch (const std::exception& e) |
| 661 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 662 | std::cerr << "Failed to get attribute value, NAME=" << attrName.c_str() |
| 663 | << " ERROR=" << e.what() << "\n"; |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | auto entryLength = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 667 | auto tableSize = attrValueTable.size(); |
| 668 | attrValueTable.resize(tableSize + entryLength); |
| 669 | pldm_bios_table_attr_value_entry_encode_integer( |
| 670 | attrValueTable.data() + tableSize, entryLength, |
| 671 | attrTableEntry->attr_handle, attrTableEntry->attr_type, currentValue); |
| 672 | } |
| 673 | |
| 674 | } // namespace bios_type_integer |
| 675 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 676 | void traverseBIOSAttrTable(const Table& biosAttrTable, |
| 677 | AttrTableEntryHandler handler) |
| 678 | { |
| 679 | std::unique_ptr<pldm_bios_table_iter, decltype(&pldm_bios_table_iter_free)> |
| 680 | iter(pldm_bios_table_iter_create(biosAttrTable.data(), |
| 681 | biosAttrTable.size(), |
| 682 | PLDM_BIOS_ATTR_TABLE), |
| 683 | pldm_bios_table_iter_free); |
| 684 | while (!pldm_bios_table_iter_is_end(iter.get())) |
| 685 | { |
| 686 | auto table_entry = pldm_bios_table_iter_attr_entry_value(iter.get()); |
| 687 | try |
| 688 | { |
| 689 | handler(table_entry); |
| 690 | } |
| 691 | catch (const std::exception& e) |
| 692 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 693 | std::cerr << "handler fails when traversing BIOSAttrTable, ERROR=" |
| 694 | << e.what() << "\n"; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 695 | } |
| 696 | pldm_bios_table_iter_next(iter.get()); |
| 697 | } |
| 698 | } |
| 699 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 700 | using typeHandler = std::function<void(const BIOSTable& BIOSStringTable, |
| 701 | Table& attributeTable)>; |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 702 | std::map<BIOSJsonName, typeHandler> attrTypeHandlers{ |
| 703 | {bios_parser::bIOSEnumJson, bios_type_enum::constructAttrTable}, |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 704 | {bios_parser::bIOSStrJson, bios_type_string::constructAttrTable}, |
| 705 | {bios_parser::bIOSIntegerJson, bios_type_integer::constructAttrTable}, |
| 706 | }; |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 707 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 708 | /** @brief Construct the BIOS attribute table |
| 709 | * |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 710 | * @param[in,out] BIOSAttributeTable - the attribute table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 711 | * @param[in] BIOSStringTable - the string table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 712 | * @param[in] biosJsonDir - path where the BIOS json files are present |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 713 | * @param[in] request - Request message |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 714 | */ |
| 715 | Response getBIOSAttributeTable(BIOSTable& BIOSAttributeTable, |
| 716 | const BIOSTable& BIOSStringTable, |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 717 | const char* biosJsonDir, const pldm_msg* request) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 718 | { |
| 719 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 720 | 0); |
| 721 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 722 | uint32_t nxtTransferHandle = 0; |
| 723 | uint8_t transferFlag = PLDM_START_AND_END; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 724 | |
| 725 | if (BIOSAttributeTable.isEmpty()) |
| 726 | { // no persisted table, constructing fresh table and response |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 727 | Table attributeTable; |
| 728 | fs::path dir(biosJsonDir); |
| 729 | |
| 730 | for (auto it = attrTypeHandlers.begin(); it != attrTypeHandlers.end(); |
| 731 | it++) |
| 732 | { |
| 733 | fs::path file = dir / it->first; |
| 734 | if (fs::exists(file)) |
| 735 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 736 | it->second(BIOSStringTable, attributeTable); |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | if (attributeTable.empty()) |
| 741 | { // no available json file is found |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 742 | return CmdHandler::ccOnlyResponse(request, |
| 743 | PLDM_BIOS_TABLE_UNAVAILABLE); |
Carol Wang | dc220c8 | 2019-08-26 13:31:31 +0800 | [diff] [blame] | 744 | } |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 745 | pldm::responder::utils::padAndChecksum(attributeTable); |
John Wang | c293835 | 2019-09-30 13:58:41 +0800 | [diff] [blame] | 746 | BIOSAttributeTable.store(attributeTable); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 747 | response.resize(sizeof(pldm_msg_hdr) + |
| 748 | PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + |
| 749 | attributeTable.size()); |
| 750 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 751 | |
| 752 | auto rc = encode_get_bios_table_resp( |
| 753 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 754 | transferFlag, attributeTable.data(), response.size(), responsePtr); |
| 755 | if (rc != PLDM_SUCCESS) |
| 756 | { |
| 757 | return CmdHandler::ccOnlyResponse(request, rc); |
| 758 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 759 | } |
| 760 | else |
| 761 | { // persisted table present, constructing response |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 762 | auto rc = encode_get_bios_table_resp( |
| 763 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 764 | transferFlag, nullptr, response.size(), |
| 765 | responsePtr); // filling up the header here |
| 766 | if (rc != PLDM_SUCCESS) |
| 767 | { |
| 768 | return CmdHandler::ccOnlyResponse(request, rc); |
| 769 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 770 | BIOSAttributeTable.load(response); |
| 771 | } |
| 772 | |
| 773 | return response; |
| 774 | } |
| 775 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 776 | using AttrValTableEntryConstructHandler = |
| 777 | std::function<void(const struct pldm_bios_attr_table_entry* tableEntry, |
| 778 | const std::string& attrName, |
| 779 | const BIOSTable& BIOSStringTable, Table& table)>; |
| 780 | |
| 781 | using AttrType = uint8_t; |
| 782 | const std::map<AttrType, AttrValTableEntryConstructHandler> |
| 783 | AttrValTableConstructMap{ |
| 784 | {PLDM_BIOS_STRING, bios_type_string::constructAttrValueEntry}, |
| 785 | {PLDM_BIOS_STRING_READ_ONLY, bios_type_string::constructAttrValueEntry}, |
| 786 | {PLDM_BIOS_ENUMERATION, bios_type_enum::constructAttrValueEntry}, |
| 787 | {PLDM_BIOS_ENUMERATION_READ_ONLY, |
| 788 | bios_type_enum::constructAttrValueEntry}, |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 789 | {PLDM_BIOS_INTEGER, bios_type_integer::constructAttrValueEntry}, |
| 790 | {PLDM_BIOS_INTEGER_READ_ONLY, |
| 791 | bios_type_integer::constructAttrValueEntry}, |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | void constructAttrValueTableEntry( |
| 795 | const struct pldm_bios_attr_table_entry* attrEntry, |
| 796 | const BIOSTable& BIOSStringTable, Table& attributeValueTable) |
| 797 | { |
| 798 | auto attrName = findStringName(attrEntry->string_handle, BIOSStringTable); |
| 799 | if (attrName.empty()) |
| 800 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 801 | std::cerr << "invalid string handle, STRING_HANDLE=" |
| 802 | << attrEntry->string_handle << "\n"; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | |
| 806 | AttrValTableConstructMap.at(attrEntry->attr_type)( |
| 807 | attrEntry, attrName, BIOSStringTable, attributeValueTable); |
| 808 | } |
| 809 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 810 | /** @brief Construct the BIOS attribute value table |
| 811 | * |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 812 | * @param[in,out] BIOSAttributeValueTable - the attribute value table |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 813 | * @param[in] BIOSAttributeTable - the attribute table |
| 814 | * @param[in] BIOSStringTable - the string table |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 815 | * @param[in] request - Request message |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 816 | */ |
| 817 | Response getBIOSAttributeValueTable(BIOSTable& BIOSAttributeValueTable, |
| 818 | const BIOSTable& BIOSAttributeTable, |
| 819 | const BIOSTable& BIOSStringTable, |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 820 | const pldm_msg* request) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 821 | { |
| 822 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 823 | 0); |
| 824 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 825 | uint32_t nxtTransferHandle = 0; |
| 826 | uint8_t transferFlag = PLDM_START_AND_END; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 827 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 828 | if (!BIOSAttributeValueTable.isEmpty()) |
| 829 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 830 | auto rc = encode_get_bios_table_resp( |
| 831 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, |
| 832 | transferFlag, nullptr, response.size(), |
| 833 | responsePtr); // filling up the header here |
| 834 | if (rc != PLDM_SUCCESS) |
| 835 | { |
| 836 | return CmdHandler::ccOnlyResponse(request, rc); |
| 837 | } |
| 838 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 839 | BIOSAttributeValueTable.load(response); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 840 | return response; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 841 | } |
| 842 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 843 | Table attributeValueTable; |
| 844 | Table attributeTable; |
| 845 | BIOSAttributeTable.load(attributeTable); |
| 846 | traverseBIOSAttrTable( |
| 847 | attributeTable, |
| 848 | [&BIOSStringTable, &attributeValueTable]( |
| 849 | const struct pldm_bios_attr_table_entry* tableEntry) { |
| 850 | constructAttrValueTableEntry(tableEntry, BIOSStringTable, |
| 851 | attributeValueTable); |
| 852 | }); |
| 853 | if (attributeValueTable.empty()) |
| 854 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 855 | return CmdHandler::ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 856 | } |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 857 | pldm::responder::utils::padAndChecksum(attributeValueTable); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 858 | BIOSAttributeValueTable.store(attributeValueTable); |
| 859 | |
| 860 | response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + |
| 861 | attributeValueTable.size()); |
| 862 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 863 | auto rc = encode_get_bios_table_resp( |
| 864 | request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle, transferFlag, |
| 865 | attributeValueTable.data(), response.size(), responsePtr); |
| 866 | if (rc != PLDM_SUCCESS) |
| 867 | { |
| 868 | return CmdHandler::ccOnlyResponse(request, rc); |
| 869 | } |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 870 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 871 | return response; |
| 872 | } |
| 873 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 874 | Response Handler::getBIOSTable(const pldm_msg* request, size_t payloadLength) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 875 | { |
Deepak Kodihalli | c3d2089 | 2019-08-01 05:38:31 -0500 | [diff] [blame] | 876 | fs::create_directory(BIOS_TABLES_DIR); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 877 | auto response = internal::buildBIOSTables(request, payloadLength, |
| 878 | BIOS_JSONS_DIR, BIOS_TABLES_DIR); |
| 879 | |
| 880 | return response; |
| 881 | } |
| 882 | |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 883 | Response Handler::getBIOSAttributeCurrentValueByHandle(const pldm_msg* request, |
| 884 | size_t payloadLength) |
| 885 | { |
| 886 | uint32_t transferHandle; |
| 887 | uint8_t transferOpFlag; |
| 888 | uint16_t attributeHandle; |
| 889 | |
| 890 | auto rc = decode_get_bios_attribute_current_value_by_handle_req( |
| 891 | request, payloadLength, &transferHandle, &transferOpFlag, |
| 892 | &attributeHandle); |
| 893 | if (rc != PLDM_SUCCESS) |
| 894 | { |
| 895 | return ccOnlyResponse(request, rc); |
| 896 | } |
| 897 | |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 898 | fs::path tablesPath(BIOS_TABLES_DIR); |
| 899 | auto stringTablePath = tablesPath / stringTableFile; |
| 900 | BIOSTable BIOSStringTable(stringTablePath.c_str()); |
| 901 | auto attrTablePath = tablesPath / attrTableFile; |
| 902 | BIOSTable BIOSAttributeTable(attrTablePath.c_str()); |
| 903 | if (BIOSAttributeTable.isEmpty() || BIOSStringTable.isEmpty()) |
| 904 | { |
| 905 | return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
| 906 | } |
| 907 | |
| 908 | auto attrValueTablePath = tablesPath / attrValTableFile; |
| 909 | BIOSTable BIOSAttributeValueTable(attrValueTablePath.c_str()); |
| 910 | |
| 911 | if (BIOSAttributeValueTable.isEmpty()) |
| 912 | { |
| 913 | Table attributeValueTable; |
| 914 | Table attributeTable; |
| 915 | BIOSAttributeTable.load(attributeTable); |
| 916 | traverseBIOSAttrTable( |
| 917 | attributeTable, |
| 918 | [&BIOSStringTable, &attributeValueTable]( |
| 919 | const struct pldm_bios_attr_table_entry* tableEntry) { |
| 920 | constructAttrValueTableEntry(tableEntry, BIOSStringTable, |
| 921 | attributeValueTable); |
| 922 | }); |
| 923 | if (attributeValueTable.empty()) |
| 924 | { |
| 925 | return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
| 926 | } |
| 927 | pldm::responder::utils::padAndChecksum(attributeValueTable); |
| 928 | BIOSAttributeValueTable.store(attributeValueTable); |
| 929 | } |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 930 | |
| 931 | Response table; |
Deepak Kodihalli | 4976a68 | 2020-01-07 05:48:29 -0600 | [diff] [blame] | 932 | BIOSAttributeValueTable.load(table); |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 933 | |
| 934 | auto entry = pldm_bios_table_attr_value_find_by_handle( |
| 935 | table.data(), table.size(), attributeHandle); |
| 936 | if (entry == nullptr) |
| 937 | { |
| 938 | return ccOnlyResponse(request, PLDM_INVALID_BIOS_ATTR_HANDLE); |
| 939 | } |
| 940 | |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 941 | auto entryLength = pldm_bios_table_attr_value_entry_length(entry); |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 942 | Response response(sizeof(pldm_msg_hdr) + |
| 943 | PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES + |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 944 | entryLength, |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 945 | 0); |
| 946 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 947 | rc = encode_get_bios_current_value_by_handle_resp( |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 948 | request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END, |
| 949 | reinterpret_cast<const uint8_t*>(entry), entryLength, responsePtr); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 950 | if (rc != PLDM_SUCCESS) |
| 951 | { |
| 952 | return ccOnlyResponse(request, rc); |
| 953 | } |
John Wang | 8721ed6 | 2019-12-05 14:44:43 +0800 | [diff] [blame] | 954 | |
| 955 | return response; |
| 956 | } |
| 957 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 958 | namespace internal |
| 959 | { |
| 960 | |
| 961 | Response buildBIOSTables(const pldm_msg* request, size_t payloadLength, |
| 962 | const char* biosJsonDir, const char* biosTablePath) |
| 963 | { |
| 964 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES, |
| 965 | 0); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 966 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 967 | if (setupConfig(biosJsonDir) != 0) |
| 968 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 969 | return CmdHandler::ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 970 | } |
| 971 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 972 | uint32_t transferHandle{}; |
| 973 | uint8_t transferOpFlag{}; |
| 974 | uint8_t tableType{}; |
| 975 | |
| 976 | auto rc = decode_get_bios_table_req(request, payloadLength, &transferHandle, |
| 977 | &transferOpFlag, &tableType); |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 978 | if (rc != PLDM_SUCCESS) |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 979 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 980 | return CmdHandler::ccOnlyResponse(request, rc); |
| 981 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 982 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 983 | BIOSTable BIOSStringTable( |
| 984 | (std::string(biosTablePath) + "/" + stringTableFile).c_str()); |
| 985 | BIOSTable BIOSAttributeTable( |
| 986 | (std::string(biosTablePath) + "/" + attrTableFile).c_str()); |
| 987 | BIOSTable BIOSAttributeValueTable( |
| 988 | (std::string(biosTablePath) + "/" + attrValTableFile).c_str()); |
| 989 | switch (tableType) |
| 990 | { |
| 991 | case PLDM_BIOS_STRING_TABLE: |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 992 | |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 993 | response = getBIOSStringTable(BIOSStringTable, request); |
| 994 | break; |
| 995 | case PLDM_BIOS_ATTR_TABLE: |
| 996 | |
| 997 | if (BIOSStringTable.isEmpty()) |
| 998 | { |
| 999 | rc = PLDM_BIOS_TABLE_UNAVAILABLE; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | response = getBIOSAttributeTable( |
| 1004 | BIOSAttributeTable, BIOSStringTable, biosJsonDir, request); |
| 1005 | } |
| 1006 | break; |
| 1007 | case PLDM_BIOS_ATTR_VAL_TABLE: |
| 1008 | if (BIOSAttributeTable.isEmpty() || BIOSStringTable.isEmpty()) |
| 1009 | { |
| 1010 | rc = PLDM_BIOS_TABLE_UNAVAILABLE; |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | response = getBIOSAttributeValueTable(BIOSAttributeValueTable, |
| 1015 | BIOSAttributeTable, |
| 1016 | BIOSStringTable, request); |
| 1017 | } |
| 1018 | break; |
| 1019 | default: |
| 1020 | rc = PLDM_INVALID_BIOS_TABLE_TYPE; |
| 1021 | break; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | if (rc != PLDM_SUCCESS) |
| 1025 | { |
George Liu | fb8611d | 2019-12-06 10:14:15 +0800 | [diff] [blame] | 1026 | return CmdHandler::ccOnlyResponse(request, rc); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | return response; |
| 1030 | } |
| 1031 | |
| 1032 | } // end namespace internal |
| 1033 | } // namespace bios |
| 1034 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1035 | } // namespace responder |
| 1036 | } // namespace pldm |