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