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