Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 1 | #include "impl.hpp" |
| 2 | |
SunnySrivastava1984 | 19be6d3 | 2020-03-03 07:21:45 -0600 | [diff] [blame] | 3 | #include "const.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 4 | #include "defines.hpp" |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 5 | #include "ibm_vpd_utils.hpp" |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 6 | #include "types.hpp" |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 7 | #include "vpd_exceptions.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 10 | #include <exception> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 11 | #include <iomanip> |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 12 | #include <iostream> |
| 13 | #include <iterator> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 14 | #include <sstream> |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 15 | #include <tuple> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 16 | #include <unordered_map> |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 17 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 18 | #include "vpdecc/vpdecc.h" |
| 19 | |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 20 | namespace openpower |
| 21 | { |
| 22 | namespace vpd |
| 23 | { |
| 24 | namespace parser |
| 25 | { |
SunnySrivastava1984 | 19be6d3 | 2020-03-03 07:21:45 -0600 | [diff] [blame] | 26 | using namespace openpower::vpd::constants; |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 27 | using namespace openpower::vpd::exceptions; |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 28 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 29 | static const std::unordered_map<std::string, Record> supportedRecords = { |
| 30 | {"VINI", Record::VINI}, {"OPFR", Record::OPFR}, {"OSYS", Record::OSYS}}; |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 31 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 32 | static const std::unordered_map<std::string, internal::KeywordInfo> |
| 33 | supportedKeywords = { |
| 34 | {"DR", std::make_tuple(record::Keyword::DR, keyword::Encoding::ASCII)}, |
| 35 | {"PN", std::make_tuple(record::Keyword::PN, keyword::Encoding::ASCII)}, |
| 36 | {"SN", std::make_tuple(record::Keyword::SN, keyword::Encoding::ASCII)}, |
| 37 | {"CC", std::make_tuple(record::Keyword::CC, keyword::Encoding::ASCII)}, |
| 38 | {"HW", std::make_tuple(record::Keyword::HW, keyword::Encoding::RAW)}, |
| 39 | {"B1", std::make_tuple(record::Keyword::B1, keyword::Encoding::B1)}, |
| 40 | {"VN", std::make_tuple(record::Keyword::VN, keyword::Encoding::ASCII)}, |
George Liu | ee79ca8 | 2019-07-12 11:05:33 +0800 | [diff] [blame] | 41 | {"MB", std::make_tuple(record::Keyword::MB, keyword::Encoding::MB)}, |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 42 | {"MM", std::make_tuple(record::Keyword::MM, keyword::Encoding::ASCII)}, |
| 43 | {"UD", std::make_tuple(record::Keyword::UD, keyword::Encoding::UD)}, |
| 44 | {"VP", std::make_tuple(record::Keyword::VP, keyword::Encoding::ASCII)}, |
| 45 | {"VS", std::make_tuple(record::Keyword::VS, keyword::Encoding::ASCII)}, |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 46 | }; |
| 47 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 48 | namespace |
| 49 | { |
| 50 | constexpr auto toHex(size_t c) |
| 51 | { |
| 52 | constexpr auto map = "0123456789abcdef"; |
| 53 | return map[c]; |
| 54 | } |
| 55 | } // namespace |
| 56 | |
| 57 | /*readUInt16LE: Read 2 bytes LE data*/ |
| 58 | static LE2ByteData readUInt16LE(Binary::const_iterator iterator) |
| 59 | { |
| 60 | LE2ByteData lowByte = *iterator; |
| 61 | LE2ByteData highByte = *(iterator + 1); |
| 62 | lowByte |= (highByte << 8); |
| 63 | return lowByte; |
| 64 | } |
| 65 | |
| 66 | RecordOffset Impl::getVtocOffset() const |
| 67 | { |
| 68 | auto vpdPtr = vpd.cbegin(); |
| 69 | std::advance(vpdPtr, offsets::VTOC_PTR); |
| 70 | // Get VTOC Offset |
| 71 | auto vtocOffset = readUInt16LE(vpdPtr); |
| 72 | |
| 73 | return vtocOffset; |
| 74 | } |
| 75 | |
| 76 | #ifdef IPZ_PARSER |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 77 | int Impl::vhdrEccCheck() const |
| 78 | { |
| 79 | int rc = eccStatus::SUCCESS; |
| 80 | auto vpdPtr = vpd.cbegin(); |
| 81 | |
| 82 | auto l_status = |
| 83 | vpdecc_check_data(const_cast<uint8_t*>(&vpdPtr[offsets::VHDR_RECORD]), |
| 84 | lengths::VHDR_RECORD_LENGTH, |
| 85 | const_cast<uint8_t*>(&vpdPtr[offsets::VHDR_ECC]), |
| 86 | lengths::VHDR_ECC_LENGTH); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 87 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 88 | if (l_status != VPD_ECC_OK) |
| 89 | { |
| 90 | rc = eccStatus::FAILED; |
| 91 | } |
| 92 | |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | int Impl::vtocEccCheck() const |
| 97 | { |
| 98 | int rc = eccStatus::SUCCESS; |
| 99 | // Use another pointer to get ECC information from VHDR, |
| 100 | // actual pointer is pointing to VTOC data |
| 101 | |
| 102 | auto vpdPtr = vpd.cbegin(); |
| 103 | |
| 104 | // Get VTOC Offset |
| 105 | auto vtocOffset = getVtocOffset(); |
| 106 | |
| 107 | // Get the VTOC Length |
| 108 | std::advance(vpdPtr, offsets::VTOC_PTR + sizeof(RecordOffset)); |
| 109 | auto vtocLength = readUInt16LE(vpdPtr); |
| 110 | |
| 111 | // Get the ECC Offset |
| 112 | std::advance(vpdPtr, sizeof(RecordLength)); |
| 113 | auto vtocECCOffset = readUInt16LE(vpdPtr); |
| 114 | |
| 115 | // Get the ECC length |
| 116 | std::advance(vpdPtr, sizeof(ECCOffset)); |
| 117 | auto vtocECCLength = readUInt16LE(vpdPtr); |
| 118 | |
| 119 | // Reset pointer to start of the vpd, |
| 120 | // so that Offset will point to correct address |
| 121 | vpdPtr = vpd.cbegin(); |
| 122 | auto l_status = vpdecc_check_data( |
| 123 | const_cast<uint8_t*>(&vpdPtr[vtocOffset]), vtocLength, |
| 124 | const_cast<uint8_t*>(&vpdPtr[vtocECCOffset]), vtocECCLength); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 125 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 126 | if (l_status != VPD_ECC_OK) |
| 127 | { |
| 128 | rc = eccStatus::FAILED; |
| 129 | } |
| 130 | |
| 131 | return rc; |
| 132 | } |
| 133 | |
| 134 | int Impl::recordEccCheck(Binary::const_iterator iterator) const |
| 135 | { |
| 136 | int rc = eccStatus::SUCCESS; |
| 137 | |
| 138 | auto recordOffset = readUInt16LE(iterator); |
| 139 | |
| 140 | std::advance(iterator, sizeof(RecordOffset)); |
| 141 | auto recordLength = readUInt16LE(iterator); |
| 142 | |
| 143 | std::advance(iterator, sizeof(RecordLength)); |
| 144 | auto eccOffset = readUInt16LE(iterator); |
| 145 | |
| 146 | std::advance(iterator, sizeof(ECCOffset)); |
| 147 | auto eccLength = readUInt16LE(iterator); |
| 148 | |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 149 | if (eccLength == 0 || eccOffset == 0) |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 150 | { |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 151 | throw(VpdEccException("Could not find ECC's offset or Length.")); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (recordOffset == 0 || recordLength == 0) |
| 155 | { |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 156 | throw(VpdDataException("Could not find VPD record offset or VPD record " |
| 157 | "length.")); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | auto vpdPtr = vpd.cbegin(); |
| 161 | |
| 162 | auto l_status = vpdecc_check_data( |
| 163 | const_cast<uint8_t*>(&vpdPtr[recordOffset]), recordLength, |
| 164 | const_cast<uint8_t*>(&vpdPtr[eccOffset]), eccLength); |
| 165 | if (l_status != VPD_ECC_OK) |
| 166 | { |
| 167 | rc = eccStatus::FAILED; |
| 168 | } |
| 169 | |
| 170 | return rc; |
| 171 | } |
| 172 | #endif |
| 173 | |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 174 | void Impl::checkHeader() const |
| 175 | { |
| 176 | if (vpd.empty() || (lengths::RECORD_MIN > vpd.size())) |
| 177 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 178 | throw(VpdDataException("Malformed VPD")); |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
| 182 | auto iterator = vpd.cbegin(); |
| 183 | std::advance(iterator, offsets::VHDR); |
| 184 | auto stop = std::next(iterator, lengths::RECORD_NAME); |
| 185 | std::string record(iterator, stop); |
| 186 | if ("VHDR" != record) |
| 187 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 188 | throw(VpdDataException("VHDR record not found")); |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 189 | } |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 190 | |
| 191 | #ifdef IPZ_PARSER |
| 192 | // Check ECC |
| 193 | int rc = eccStatus::FAILED; |
| 194 | rc = vhdrEccCheck(); |
| 195 | if (rc != eccStatus::SUCCESS) |
| 196 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 197 | throw(VpdEccException("ERROR: VHDR ECC check Failed")); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 198 | } |
| 199 | #endif |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 203 | std::size_t Impl::readTOC(Binary::const_iterator& iterator) const |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 204 | { |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 205 | // The offset to VTOC could be 1 or 2 bytes long |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 206 | RecordOffset vtocOffset = getVtocOffset(); |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 207 | |
| 208 | // Got the offset to VTOC, skip past record header and keyword header |
| 209 | // to get to the record name. |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 210 | std::advance(iterator, vtocOffset + sizeof(RecordId) + sizeof(RecordSize) + |
| 211 | // Skip past the RT keyword, which contains |
| 212 | // the record name. |
| 213 | lengths::KW_NAME + sizeof(KwSize)); |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 214 | |
| 215 | auto stop = std::next(iterator, lengths::RECORD_NAME); |
| 216 | std::string record(iterator, stop); |
| 217 | if ("VTOC" != record) |
| 218 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 219 | throw(VpdDataException("VTOC record not found")); |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 220 | } |
| 221 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 222 | #ifdef IPZ_PARSER |
| 223 | // Check ECC |
| 224 | int rc = eccStatus::FAILED; |
| 225 | rc = vtocEccCheck(); |
| 226 | if (rc != eccStatus::SUCCESS) |
| 227 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 228 | throw(VpdEccException("ERROR: VTOC ECC check Failed")); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 229 | } |
| 230 | #endif |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 231 | // VTOC record name is good, now read through the TOC, stored in the PT |
| 232 | // PT keyword; vpdBuffer is now pointing at the first character of the |
| 233 | // name 'VTOC', jump to PT data. |
| 234 | // Skip past record name and KW name, 'PT' |
| 235 | std::advance(iterator, lengths::RECORD_NAME + lengths::KW_NAME); |
| 236 | // Note size of PT |
| 237 | std::size_t ptLen = *iterator; |
| 238 | // Skip past PT size |
| 239 | std::advance(iterator, sizeof(KwSize)); |
| 240 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 241 | // length of PT keyword |
| 242 | return ptLen; |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | internal::OffsetList Impl::readPT(Binary::const_iterator iterator, |
| 246 | std::size_t ptLength) const |
| 247 | { |
| 248 | internal::OffsetList offsets{}; |
| 249 | |
| 250 | auto end = iterator; |
| 251 | std::advance(end, ptLength); |
| 252 | |
| 253 | // Look at each entry in the PT keyword. In the entry, |
| 254 | // we care only about the record offset information. |
| 255 | while (iterator < end) |
| 256 | { |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 257 | #ifdef IPZ_PARSER |
| 258 | auto iteratorToRecName = iterator; |
| 259 | #endif |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 260 | // Skip record name and record type |
| 261 | std::advance(iterator, lengths::RECORD_NAME + sizeof(RecordType)); |
| 262 | |
| 263 | // Get record offset |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 264 | auto offset = readUInt16LE(iterator); |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 265 | offsets.push_back(offset); |
| 266 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 267 | #ifdef IPZ_PARSER |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 268 | std::string recordName(iteratorToRecName, |
| 269 | iteratorToRecName + lengths::RECORD_NAME); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 270 | |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 271 | try |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 272 | { |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 273 | // Verify the ECC for this Record |
| 274 | int rc = recordEccCheck(iterator); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 275 | |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 276 | if (rc != eccStatus::SUCCESS) |
| 277 | { |
| 278 | std::string errorMsg = |
| 279 | std::string("ERROR: ECC check did not pass."); |
| 280 | throw(VpdEccException(errorMsg)); |
| 281 | } |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 282 | } |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 283 | catch (const VpdEccException& ex) |
| 284 | { |
| 285 | std::string errMsg = |
| 286 | std::string{ex.what()} + " Record: " + recordName; |
| 287 | |
| 288 | inventory::PelAdditionalData additionalData{}; |
| 289 | additionalData.emplace("DESCRIPTION", errMsg); |
| 290 | additionalData.emplace("CALLOUT_INVENTORY_PATH", inventoryPath); |
| 291 | createPEL(additionalData, PelSeverity::WARNING, |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame^] | 292 | errIntfForEccCheckFail, nullptr); |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 293 | } |
| 294 | catch (const VpdDataException& ex) |
| 295 | { |
| 296 | std::string errMsg = |
| 297 | std::string{ex.what()} + " Record: " + recordName; |
| 298 | |
| 299 | inventory::PelAdditionalData additionalData{}; |
| 300 | additionalData.emplace("DESCRIPTION", errMsg); |
| 301 | additionalData.emplace("CALLOUT_INVENTORY_PATH", inventoryPath); |
| 302 | createPEL(additionalData, PelSeverity::WARNING, |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame^] | 303 | errIntfForInvalidVPD, nullptr); |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 306 | #endif |
| 307 | |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 308 | // Jump record size, record length, ECC offset and ECC length |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 309 | std::advance(iterator, sizeof(RecordOffset) + sizeof(RecordLength) + |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 310 | sizeof(ECCOffset) + sizeof(ECCLength)); |
Deepak Kodihalli | 023112f | 2016-11-22 22:02:14 -0600 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return offsets; |
| 314 | } |
| 315 | |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 316 | void Impl::processRecord(std::size_t recordOffset) |
| 317 | { |
| 318 | // Jump to record name |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 319 | auto nameOffset = recordOffset + sizeof(RecordId) + sizeof(RecordSize) + |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 320 | // Skip past the RT keyword, which contains |
| 321 | // the record name. |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 322 | lengths::KW_NAME + sizeof(KwSize); |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 323 | // Get record name |
| 324 | auto iterator = vpd.cbegin(); |
| 325 | std::advance(iterator, nameOffset); |
| 326 | |
| 327 | std::string name(iterator, iterator + lengths::RECORD_NAME); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 328 | |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 329 | #ifndef IPZ_PARSER |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 330 | if (supportedRecords.end() != supportedRecords.find(name)) |
| 331 | { |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 332 | #endif |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 333 | // If it's a record we're interested in, proceed to find |
| 334 | // contained keywords and their values. |
| 335 | std::advance(iterator, lengths::RECORD_NAME); |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 336 | |
| 337 | #ifdef IPZ_PARSER |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 338 | |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 339 | // Reverse back to RT Kw, in ipz vpd, to Read RT KW & value |
| 340 | std::advance(iterator, -(lengths::KW_NAME + sizeof(KwSize) + |
| 341 | lengths::RECORD_NAME)); |
| 342 | #endif |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 343 | auto kwMap = readKeywords(iterator); |
| 344 | // Add entry for this record (and contained keyword:value pairs) |
| 345 | // to the parsed vpd output. |
| 346 | out.emplace(std::move(name), std::move(kwMap)); |
Alpana Kumari | c0aeac3 | 2019-11-28 05:20:10 -0600 | [diff] [blame] | 347 | |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 348 | #ifndef IPZ_PARSER |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 349 | } |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 350 | #endif |
Deepak Kodihalli | a114346 | 2016-11-24 06:28:45 -0600 | [diff] [blame] | 351 | } |
| 352 | |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 353 | std::string Impl::readKwData(const internal::KeywordInfo& keyword, |
| 354 | std::size_t dataLength, |
| 355 | Binary::const_iterator iterator) |
| 356 | { |
Santosh Puranik | bd011b2 | 2020-01-23 04:05:25 -0600 | [diff] [blame] | 357 | using namespace openpower::vpd; |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 358 | switch (std::get<keyword::Encoding>(keyword)) |
| 359 | { |
| 360 | case keyword::Encoding::ASCII: |
| 361 | { |
| 362 | auto stop = std::next(iterator, dataLength); |
| 363 | return std::string(iterator, stop); |
| 364 | } |
| 365 | |
| 366 | case keyword::Encoding::RAW: |
| 367 | { |
| 368 | auto stop = std::next(iterator, dataLength); |
| 369 | std::string data(iterator, stop); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 370 | std::string result{}; |
| 371 | std::for_each(data.cbegin(), data.cend(), [&result](size_t c) { |
| 372 | result += toHex(c >> 4); |
| 373 | result += toHex(c & 0x0F); |
| 374 | }); |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 375 | return result; |
| 376 | } |
| 377 | |
George Liu | ee79ca8 | 2019-07-12 11:05:33 +0800 | [diff] [blame] | 378 | case keyword::Encoding::MB: |
| 379 | { |
| 380 | // MB is BuildDate, represent as |
| 381 | // 1997-01-01-08:30:00 |
| 382 | // <year>-<month>-<day>-<hour>:<min>:<sec> |
| 383 | auto stop = std::next(iterator, MB_LEN_BYTES); |
| 384 | std::string data(iterator, stop); |
| 385 | std::string result; |
| 386 | result.reserve(MB_LEN_BYTES); |
| 387 | auto strItr = data.cbegin(); |
| 388 | std::advance(strItr, 1); |
| 389 | std::for_each(strItr, data.cend(), [&result](size_t c) { |
| 390 | result += toHex(c >> 4); |
| 391 | result += toHex(c & 0x0F); |
| 392 | }); |
| 393 | |
| 394 | result.insert(MB_YEAR_END, 1, '-'); |
| 395 | result.insert(MB_MONTH_END, 1, '-'); |
| 396 | result.insert(MB_DAY_END, 1, '-'); |
| 397 | result.insert(MB_HOUR_END, 1, ':'); |
| 398 | result.insert(MB_MIN_END, 1, ':'); |
| 399 | |
| 400 | return result; |
| 401 | } |
| 402 | |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 403 | case keyword::Encoding::B1: |
| 404 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 405 | // B1 is MAC address, represent as AA:BB:CC:DD:EE:FF |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 406 | auto stop = std::next(iterator, MAC_ADDRESS_LEN_BYTES); |
| 407 | std::string data(iterator, stop); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 408 | std::string result{}; |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 409 | auto strItr = data.cbegin(); |
| 410 | size_t firstDigit = *strItr; |
| 411 | result += toHex(firstDigit >> 4); |
| 412 | result += toHex(firstDigit & 0x0F); |
| 413 | std::advance(strItr, 1); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 414 | std::for_each(strItr, data.cend(), [&result](size_t c) { |
| 415 | result += ":"; |
| 416 | result += toHex(c >> 4); |
| 417 | result += toHex(c & 0x0F); |
| 418 | }); |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 419 | return result; |
| 420 | } |
| 421 | |
Dinesh Chinari | c576b48 | 2017-07-17 16:34:10 -0500 | [diff] [blame] | 422 | case keyword::Encoding::UD: |
| 423 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 424 | // UD, the UUID info, represented as |
| 425 | // 123e4567-e89b-12d3-a456-426655440000 |
Dinesh Chinari | c576b48 | 2017-07-17 16:34:10 -0500 | [diff] [blame] | 426 | //<time_low>-<time_mid>-<time hi and version> |
| 427 | //-<clock_seq_hi_and_res clock_seq_low>-<48 bits node id> |
| 428 | auto stop = std::next(iterator, UUID_LEN_BYTES); |
| 429 | std::string data(iterator, stop); |
| 430 | std::string result{}; |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 431 | std::for_each(data.cbegin(), data.cend(), [&result](size_t c) { |
| 432 | result += toHex(c >> 4); |
| 433 | result += toHex(c & 0x0F); |
| 434 | }); |
Dinesh Chinari | c576b48 | 2017-07-17 16:34:10 -0500 | [diff] [blame] | 435 | result.insert(UUID_TIME_LOW_END, 1, '-'); |
| 436 | result.insert(UUID_TIME_MID_END, 1, '-'); |
| 437 | result.insert(UUID_TIME_HIGH_END, 1, '-'); |
| 438 | result.insert(UUID_CLK_SEQ_END, 1, '-'); |
| 439 | |
| 440 | return result; |
| 441 | } |
Deepak Kodihalli | 4a475bd | 2016-11-24 07:08:20 -0600 | [diff] [blame] | 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return {}; |
| 447 | } |
| 448 | |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 449 | internal::KeywordMap Impl::readKeywords(Binary::const_iterator iterator) |
| 450 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 451 | internal::KeywordMap map{}; |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 452 | while (true) |
| 453 | { |
| 454 | // Note keyword name |
| 455 | std::string kw(iterator, iterator + lengths::KW_NAME); |
| 456 | if (LAST_KW == kw) |
| 457 | { |
| 458 | // We're done |
| 459 | break; |
| 460 | } |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 461 | // Check if the Keyword is '#kw' |
| 462 | char kwNameStart = *iterator; |
| 463 | |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 464 | // Jump past keyword name |
| 465 | std::advance(iterator, lengths::KW_NAME); |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 466 | |
| 467 | std::size_t length; |
| 468 | std::size_t lengthHighByte; |
| 469 | if (POUND_KW == kwNameStart) |
| 470 | { |
| 471 | // Note keyword data length |
| 472 | length = *iterator; |
| 473 | lengthHighByte = *(iterator + 1); |
| 474 | length |= (lengthHighByte << 8); |
| 475 | |
| 476 | // Jump past 2Byte keyword length |
| 477 | std::advance(iterator, sizeof(PoundKwSize)); |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | // Note keyword data length |
| 482 | length = *iterator; |
| 483 | |
| 484 | // Jump past keyword length |
| 485 | std::advance(iterator, sizeof(KwSize)); |
| 486 | } |
| 487 | |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 488 | // Pointing to keyword data now |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 489 | #ifndef IPZ_PARSER |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 490 | if (supportedKeywords.end() != supportedKeywords.find(kw)) |
| 491 | { |
| 492 | // Keyword is of interest to us |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 493 | std::string data = readKwData((supportedKeywords.find(kw))->second, |
| 494 | length, iterator); |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 495 | map.emplace(std::move(kw), std::move(data)); |
| 496 | } |
Alpana Kumari | 26a74af | 2019-09-10 23:53:58 -0500 | [diff] [blame] | 497 | |
| 498 | #else |
| 499 | // support all the Keywords |
| 500 | auto stop = std::next(iterator, length); |
| 501 | std::string kwdata(iterator, stop); |
| 502 | map.emplace(std::move(kw), std::move(kwdata)); |
| 503 | |
| 504 | #endif |
Deepak Kodihalli | 683bf72 | 2016-11-24 06:50:43 -0600 | [diff] [blame] | 505 | // Jump past keyword data length |
| 506 | std::advance(iterator, length); |
| 507 | } |
| 508 | |
| 509 | return map; |
| 510 | } |
| 511 | |
Deepak Kodihalli | 174caf6 | 2016-11-25 05:41:19 -0600 | [diff] [blame] | 512 | Store Impl::run() |
| 513 | { |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 514 | // Check if the VHDR record is present |
| 515 | checkHeader(); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 516 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 517 | auto iterator = vpd.cbegin(); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 518 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 519 | // Read the table of contents record |
| 520 | std::size_t ptLen = readTOC(iterator); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 521 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 522 | // Read the table of contents record, to get offsets |
| 523 | // to other records. |
| 524 | auto offsets = readPT(iterator, ptLen); |
| 525 | for (const auto& offset : offsets) |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 526 | { |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 527 | processRecord(offset); |
SunnySrivastava1984 | 9d4f112 | 2020-08-26 02:41:46 -0500 | [diff] [blame] | 528 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 529 | // Return a Store object, which has interfaces to |
| 530 | // access parsed VPD by record:keyword |
| 531 | return Store(std::move(out)); |
Deepak Kodihalli | 174caf6 | 2016-11-25 05:41:19 -0600 | [diff] [blame] | 532 | } |
| 533 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 534 | void Impl::checkVPDHeader() |
| 535 | { |
| 536 | // Check if the VHDR record is present and is valid |
| 537 | checkHeader(); |
| 538 | } |
| 539 | |
Priyanga Ramasamy | 28079c8 | 2021-10-07 16:30:51 -0500 | [diff] [blame] | 540 | std::string Impl::readKwFromHw(const std::string& record, |
| 541 | const std::string& keyword) |
| 542 | { |
| 543 | // Check if the VHDR record is present |
| 544 | checkHeader(); |
| 545 | |
| 546 | auto iterator = vpd.cbegin(); |
| 547 | |
| 548 | // Read the table of contents record |
| 549 | std::size_t ptLen = readTOC(iterator); |
| 550 | |
| 551 | // Read the table of contents record, to get offsets |
| 552 | // to other records. |
| 553 | auto offsets = readPT(iterator, ptLen); |
| 554 | for (const auto& offset : offsets) |
| 555 | { |
| 556 | // Jump to record name |
| 557 | auto nameOffset = offset + sizeof(RecordId) + sizeof(RecordSize) + |
| 558 | // Skip past the RT keyword, which contains |
| 559 | // the record name. |
| 560 | lengths::KW_NAME + sizeof(KwSize); |
| 561 | // Get record name |
| 562 | auto iterator = vpd.cbegin(); |
| 563 | std::advance(iterator, nameOffset); |
| 564 | |
| 565 | std::string name(iterator, iterator + lengths::RECORD_NAME); |
| 566 | if (name != record) |
| 567 | { |
| 568 | continue; |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | processRecord(offset); |
| 573 | const auto& itr = out.find(record); |
| 574 | if (itr != out.end()) |
| 575 | { |
| 576 | const auto& kwValItr = (itr->second).find(keyword); |
| 577 | if (kwValItr != (itr->second).end()) |
| 578 | { |
| 579 | return kwValItr->second; |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | return ""; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | return ""; |
| 589 | } |
| 590 | |
Deepak Kodihalli | 810c9de | 2016-11-22 11:42:51 -0600 | [diff] [blame] | 591 | } // namespace parser |
| 592 | } // namespace vpd |
Priyanga Ramasamy | 28079c8 | 2021-10-07 16:30:51 -0500 | [diff] [blame] | 593 | } // namespace openpower |