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