Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file fru_utils.cpp |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 17 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 18 | #include "fru_utils.hpp" |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 19 | |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 20 | #include <phosphor-logging/lg2.hpp> |
| 21 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 22 | #include <array> |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 23 | #include <cstddef> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 24 | #include <cstdint> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 25 | #include <filesystem> |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 26 | #include <iomanip> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 27 | #include <iostream> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 28 | #include <numeric> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 29 | #include <set> |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 30 | #include <sstream> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | extern "C" |
| 35 | { |
| 36 | // Include for I2C_SMBUS_BLOCK_MAX |
| 37 | #include <linux/i2c.h> |
| 38 | } |
| 39 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 40 | constexpr size_t fruVersion = 1; // Current FRU spec version number is 1 |
| 41 | |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 42 | std::tm intelEpoch() |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 43 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 44 | std::tm val = {}; |
| 45 | val.tm_year = 1996 - 1900; |
Scron-Chang | 9e5a675 | 2021-03-16 10:51:50 +0800 | [diff] [blame] | 46 | val.tm_mday = 1; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 47 | return val; |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 50 | char sixBitToChar(uint8_t val) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 51 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 52 | return static_cast<char>((val & 0x3f) + ' '); |
| 53 | } |
| 54 | |
| 55 | char bcdPlusToChar(uint8_t val) |
| 56 | { |
| 57 | val &= 0xf; |
| 58 | return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10]; |
| 59 | } |
| 60 | |
| 61 | enum FRUDataEncoding |
| 62 | { |
| 63 | binary = 0x0, |
| 64 | bcdPlus = 0x1, |
| 65 | sixBitASCII = 0x2, |
| 66 | languageDependent = 0x3, |
| 67 | }; |
| 68 | |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 69 | enum MultiRecordType : uint8_t |
| 70 | { |
| 71 | powerSupplyInfo = 0x00, |
| 72 | dcOutput = 0x01, |
| 73 | dcLoad = 0x02, |
| 74 | managementAccessRecord = 0x03, |
| 75 | baseCompatibilityRecord = 0x04, |
| 76 | extendedCompatibilityRecord = 0x05, |
| 77 | resvASFSMBusDeviceRecord = 0x06, |
| 78 | resvASFLegacyDeviceAlerts = 0x07, |
| 79 | resvASFRemoteControl = 0x08, |
| 80 | extendedDCOutput = 0x09, |
| 81 | extendedDCLoad = 0x0A |
| 82 | }; |
| 83 | |
| 84 | enum SubManagementAccessRecord : uint8_t |
| 85 | { |
| 86 | systemManagementURL = 0x01, |
| 87 | systemName = 0x02, |
| 88 | systemPingAddress = 0x03, |
| 89 | componentManagementURL = 0x04, |
| 90 | componentName = 0x05, |
| 91 | componentPingAddress = 0x06, |
| 92 | systemUniqueID = 0x07 |
| 93 | }; |
| 94 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 95 | /* Decode FRU data into a std::string, given an input iterator and end. If the |
| 96 | * state returned is fruDataOk, then the resulting string is the decoded FRU |
| 97 | * data. The input iterator is advanced past the data consumed. |
| 98 | * |
| 99 | * On fruDataErr, we have lost synchronisation with the length bytes, so the |
| 100 | * iterator is no longer usable. |
| 101 | */ |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 102 | std::pair<DecodeState, std::string> decodeFRUData( |
| 103 | std::vector<uint8_t>::const_iterator& iter, |
| 104 | const std::vector<uint8_t>::const_iterator& end, bool isLangEng) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 105 | { |
| 106 | std::string value; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 107 | unsigned int i = 0; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 108 | |
| 109 | /* we need at least one byte to decode the type/len header */ |
| 110 | if (iter == end) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 111 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 112 | std::cerr << "Truncated FRU data\n"; |
| 113 | return make_pair(DecodeState::err, value); |
| 114 | } |
| 115 | |
| 116 | uint8_t c = *(iter++); |
| 117 | |
| 118 | /* 0xc1 is the end marker */ |
| 119 | if (c == 0xc1) |
| 120 | { |
| 121 | return make_pair(DecodeState::end, value); |
| 122 | } |
| 123 | |
| 124 | /* decode type/len byte */ |
| 125 | uint8_t type = static_cast<uint8_t>(c >> 6); |
| 126 | uint8_t len = static_cast<uint8_t>(c & 0x3f); |
| 127 | |
| 128 | /* we should have at least len bytes of data available overall */ |
| 129 | if (iter + len > end) |
| 130 | { |
| 131 | std::cerr << "FRU data field extends past end of FRU area data\n"; |
| 132 | return make_pair(DecodeState::err, value); |
| 133 | } |
| 134 | |
| 135 | switch (type) |
| 136 | { |
| 137 | case FRUDataEncoding::binary: |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 138 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 139 | std::stringstream ss; |
| 140 | ss << std::hex << std::setfill('0'); |
| 141 | for (i = 0; i < len; i++, iter++) |
| 142 | { |
| 143 | uint8_t val = static_cast<uint8_t>(*iter); |
| 144 | ss << std::setw(2) << static_cast<int>(val); |
| 145 | } |
| 146 | value = ss.str(); |
| 147 | break; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 148 | } |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 149 | case FRUDataEncoding::languageDependent: |
| 150 | /* For language-code dependent encodings, assume 8-bit ASCII */ |
| 151 | value = std::string(iter, iter + len); |
| 152 | iter += len; |
| 153 | |
| 154 | /* English text is encoded in 8-bit ASCII + Latin 1. All other |
| 155 | * languages are required to use 2-byte unicode. FruDevice does not |
| 156 | * handle unicode. |
| 157 | */ |
| 158 | if (!isLangEng) |
| 159 | { |
| 160 | std::cerr << "Error: Non english string is not supported \n"; |
| 161 | return make_pair(DecodeState::err, value); |
| 162 | } |
| 163 | |
| 164 | break; |
| 165 | |
| 166 | case FRUDataEncoding::bcdPlus: |
| 167 | value = std::string(); |
| 168 | for (i = 0; i < len; i++, iter++) |
| 169 | { |
| 170 | uint8_t val = *iter; |
| 171 | value.push_back(bcdPlusToChar(val >> 4)); |
| 172 | value.push_back(bcdPlusToChar(val & 0xf)); |
| 173 | } |
| 174 | break; |
| 175 | |
| 176 | case FRUDataEncoding::sixBitASCII: |
| 177 | { |
| 178 | unsigned int accum = 0; |
| 179 | unsigned int accumBitLen = 0; |
| 180 | value = std::string(); |
| 181 | for (i = 0; i < len; i++, iter++) |
| 182 | { |
| 183 | accum |= *iter << accumBitLen; |
| 184 | accumBitLen += 8; |
| 185 | while (accumBitLen >= 6) |
| 186 | { |
| 187 | value.push_back(sixBitToChar(accum & 0x3f)); |
| 188 | accum >>= 6; |
| 189 | accumBitLen -= 6; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | break; |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 194 | |
| 195 | default: |
| 196 | { |
| 197 | return make_pair(DecodeState::err, value); |
| 198 | } |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return make_pair(DecodeState::ok, value); |
| 202 | } |
| 203 | |
| 204 | bool checkLangEng(uint8_t lang) |
| 205 | { |
| 206 | // If Lang is not English then the encoding is defined as 2-byte UNICODE, |
| 207 | // but we don't support that. |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 208 | if ((lang != 0U) && lang != 25) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 209 | { |
| 210 | std::cerr << "Warning: languages other than English is not " |
| 211 | "supported\n"; |
| 212 | // Return language flag as non english |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 213 | return false; |
| 214 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 215 | return true; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 218 | /* This function verifies for other offsets to check if they are not |
| 219 | * falling under other field area |
| 220 | * |
| 221 | * fruBytes: Start of Fru data |
| 222 | * currentArea: Index of current area offset to be compared against all area |
| 223 | * offset and it is a multiple of 8 bytes as per specification |
| 224 | * len: Length of current area space and it is a multiple of 8 bytes |
| 225 | * as per specification |
| 226 | */ |
| 227 | bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea, |
| 228 | uint8_t len) |
| 229 | { |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 230 | unsigned int fruBytesSize = fruBytes.size(); |
| 231 | |
| 232 | // check if Fru data has at least 8 byte header |
| 233 | if (fruBytesSize <= fruBlockSize) |
| 234 | { |
| 235 | std::cerr << "Error: trying to parse empty FRU\n"; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // Check range of passed currentArea value |
| 240 | if (currentArea > fruAreas::fruAreaMultirecord) |
| 241 | { |
| 242 | std::cerr << "Error: Fru area is out of range\n"; |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | unsigned int currentAreaIndex = getHeaderAreaFieldOffset(currentArea); |
| 247 | if (currentAreaIndex > fruBytesSize) |
| 248 | { |
| 249 | std::cerr << "Error: Fru area index is out of range\n"; |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | unsigned int start = fruBytes[currentAreaIndex]; |
| 254 | unsigned int end = start + len; |
| 255 | |
| 256 | /* Verify each offset within the range of start and end */ |
| 257 | for (fruAreas area = fruAreas::fruAreaInternal; |
| 258 | area <= fruAreas::fruAreaMultirecord; ++area) |
| 259 | { |
| 260 | // skip the current offset |
| 261 | if (area == currentArea) |
| 262 | { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | unsigned int areaIndex = getHeaderAreaFieldOffset(area); |
| 267 | if (areaIndex > fruBytesSize) |
| 268 | { |
| 269 | std::cerr << "Error: Fru area index is out of range\n"; |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | unsigned int areaOffset = fruBytes[areaIndex]; |
| 274 | // if areaOffset is 0 means this area is not available so skip |
| 275 | if (areaOffset == 0) |
| 276 | { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | // check for overlapping of current offset with given areaoffset |
| 281 | if (areaOffset == start || (areaOffset > start && areaOffset < end)) |
| 282 | { |
| 283 | std::cerr << getFruAreaName(currentArea) |
| 284 | << " offset is overlapping with " << getFruAreaName(area) |
| 285 | << " offset\n"; |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 292 | static void parseMultirecordUUID( |
| 293 | const std::vector<uint8_t>& device, |
| 294 | boost::container::flat_map<std::string, std::string>& result) |
| 295 | { |
| 296 | constexpr size_t uuidDataLen = 16; |
| 297 | constexpr size_t multiRecordHeaderLen = 5; |
| 298 | /* UUID record data, plus one to skip past the sub-record type byte */ |
| 299 | constexpr size_t uuidRecordData = multiRecordHeaderLen + 1; |
| 300 | constexpr size_t multiRecordEndOfListMask = 0x80; |
| 301 | /* The UUID {00112233-4455-6677-8899-AABBCCDDEEFF} would thus be represented |
| 302 | * as: 0x33 0x22 0x11 0x00 0x55 0x44 0x77 0x66 0x88 0x99 0xAA 0xBB 0xCC 0xDD |
| 303 | * 0xEE 0xFF |
| 304 | */ |
| 305 | const std::array<uint8_t, uuidDataLen> uuidCharOrder = { |
| 306 | 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15}; |
| 307 | uint32_t areaOffset = |
| 308 | device.at(getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord)); |
| 309 | |
| 310 | if (areaOffset == 0) |
| 311 | { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | areaOffset *= fruBlockSize; |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 316 | std::vector<uint8_t>::const_iterator fruBytesIter = |
| 317 | device.begin() + areaOffset; |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 318 | |
| 319 | /* Verify area offset */ |
| 320 | if (!verifyOffset(device, fruAreas::fruAreaMultirecord, *fruBytesIter)) |
| 321 | { |
| 322 | return; |
| 323 | } |
| 324 | while (areaOffset + uuidRecordData + uuidDataLen <= device.size()) |
| 325 | { |
| 326 | if ((areaOffset < device.size()) && |
| 327 | (device[areaOffset] == |
| 328 | (uint8_t)MultiRecordType::managementAccessRecord)) |
| 329 | { |
| 330 | if ((areaOffset + multiRecordHeaderLen < device.size()) && |
| 331 | (device[areaOffset + multiRecordHeaderLen] == |
| 332 | (uint8_t)SubManagementAccessRecord::systemUniqueID)) |
| 333 | { |
| 334 | /* Layout of UUID: |
| 335 | * source: https://www.ietf.org/rfc/rfc4122.txt |
| 336 | * |
| 337 | * UUID binary format (16 bytes): |
| 338 | * 4B-2B-2B-2B-6B (big endian) |
| 339 | * |
| 340 | * UUID string is 36 length of characters (36 bytes): |
| 341 | * 0 9 14 19 24 |
| 342 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 343 | * be be be be be |
| 344 | * be means it should be converted to big endian. |
| 345 | */ |
| 346 | /* Get UUID bytes to UUID string */ |
| 347 | std::stringstream tmp; |
| 348 | tmp << std::hex << std::setfill('0'); |
| 349 | for (size_t i = 0; i < uuidDataLen; i++) |
| 350 | { |
| 351 | tmp << std::setw(2) |
| 352 | << static_cast<uint16_t>( |
| 353 | device[areaOffset + uuidRecordData + |
| 354 | uuidCharOrder[i]]); |
| 355 | } |
| 356 | std::string uuidStr = tmp.str(); |
| 357 | result["MULTIRECORD_UUID"] = |
| 358 | uuidStr.substr(0, 8) + '-' + uuidStr.substr(8, 4) + '-' + |
| 359 | uuidStr.substr(12, 4) + '-' + uuidStr.substr(16, 4) + '-' + |
| 360 | uuidStr.substr(20, 12); |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | if ((device[areaOffset + 1] & multiRecordEndOfListMask) != 0) |
| 365 | { |
| 366 | break; |
| 367 | } |
| 368 | areaOffset = areaOffset + device[areaOffset + 2] + multiRecordHeaderLen; |
| 369 | } |
| 370 | } |
| 371 | |
Michael Shen | 0961b11 | 2022-02-22 11:06:33 +0800 | [diff] [blame] | 372 | resCodes |
| 373 | formatIPMIFRU(const std::vector<uint8_t>& fruBytes, |
| 374 | boost::container::flat_map<std::string, std::string>& result) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 375 | { |
| 376 | resCodes ret = resCodes::resOK; |
| 377 | if (fruBytes.size() <= fruBlockSize) |
| 378 | { |
| 379 | std::cerr << "Error: trying to parse empty FRU \n"; |
| 380 | return resCodes::resErr; |
| 381 | } |
| 382 | result["Common_Format_Version"] = |
| 383 | std::to_string(static_cast<int>(*fruBytes.begin())); |
| 384 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 385 | const std::vector<std::string>* fruAreaFieldNames = nullptr; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 386 | |
| 387 | // Don't parse Internal and Multirecord areas |
| 388 | for (fruAreas area = fruAreas::fruAreaChassis; |
| 389 | area <= fruAreas::fruAreaProduct; ++area) |
| 390 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 391 | size_t offset = *(fruBytes.begin() + getHeaderAreaFieldOffset(area)); |
| 392 | if (offset == 0) |
| 393 | { |
| 394 | continue; |
| 395 | } |
| 396 | offset *= fruBlockSize; |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 397 | std::vector<uint8_t>::const_iterator fruBytesIter = |
| 398 | fruBytes.begin() + offset; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 399 | if (fruBytesIter + fruBlockSize >= fruBytes.end()) |
| 400 | { |
| 401 | std::cerr << "Not enough data to parse \n"; |
| 402 | return resCodes::resErr; |
| 403 | } |
| 404 | // check for format version 1 |
| 405 | if (*fruBytesIter != 0x01) |
| 406 | { |
| 407 | std::cerr << "Unexpected version " << *fruBytesIter << "\n"; |
| 408 | return resCodes::resErr; |
| 409 | } |
| 410 | ++fruBytesIter; |
| 411 | |
| 412 | /* Verify other area offset for overlap with current area by passing |
| 413 | * length of current area offset pointed by *fruBytesIter |
| 414 | */ |
| 415 | if (!verifyOffset(fruBytes, area, *fruBytesIter)) |
| 416 | { |
| 417 | return resCodes::resErr; |
| 418 | } |
| 419 | |
Andrew Jeffery | 65ed664 | 2021-08-02 22:32:23 +0930 | [diff] [blame] | 420 | size_t fruAreaSize = *fruBytesIter * fruBlockSize; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 421 | std::vector<uint8_t>::const_iterator fruBytesIterEndArea = |
| 422 | fruBytes.begin() + offset + fruAreaSize - 1; |
| 423 | ++fruBytesIter; |
| 424 | |
| 425 | uint8_t fruComputedChecksum = |
| 426 | calculateChecksum(fruBytes.begin() + offset, fruBytesIterEndArea); |
| 427 | if (fruComputedChecksum != *fruBytesIterEndArea) |
| 428 | { |
| 429 | std::stringstream ss; |
| 430 | ss << std::hex << std::setfill('0'); |
| 431 | ss << "Checksum error in FRU area " << getFruAreaName(area) << "\n"; |
| 432 | ss << "\tComputed checksum: 0x" << std::setw(2) |
| 433 | << static_cast<int>(fruComputedChecksum) << "\n"; |
| 434 | ss << "\tThe read checksum: 0x" << std::setw(2) |
| 435 | << static_cast<int>(*fruBytesIterEndArea) << "\n"; |
| 436 | std::cerr << ss.str(); |
| 437 | ret = resCodes::resWarn; |
| 438 | } |
| 439 | |
| 440 | /* Set default language flag to true as Chassis Fru area are always |
| 441 | * encoded in English defined in Section 10 of Fru specification |
| 442 | */ |
| 443 | |
| 444 | bool isLangEng = true; |
| 445 | switch (area) |
| 446 | { |
| 447 | case fruAreas::fruAreaChassis: |
| 448 | { |
| 449 | result["CHASSIS_TYPE"] = |
| 450 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 451 | fruBytesIter += 1; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 452 | fruAreaFieldNames = &chassisFruAreas; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | case fruAreas::fruAreaBoard: |
| 456 | { |
| 457 | uint8_t lang = *fruBytesIter; |
| 458 | result["BOARD_LANGUAGE_CODE"] = |
| 459 | std::to_string(static_cast<int>(lang)); |
| 460 | isLangEng = checkLangEng(lang); |
| 461 | fruBytesIter += 1; |
| 462 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 463 | unsigned int minutes = |
| 464 | *fruBytesIter | *(fruBytesIter + 1) << 8 | |
| 465 | *(fruBytesIter + 2) << 16; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 466 | std::tm fruTime = intelEpoch(); |
Willy Tu | 3f98b5e | 2023-04-12 08:16:52 -0700 | [diff] [blame] | 467 | std::time_t timeValue = timegm(&fruTime); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 468 | timeValue += static_cast<long>(minutes) * 60; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 469 | fruTime = *std::gmtime(&timeValue); |
| 470 | |
| 471 | // Tue Nov 20 23:08:00 2018 |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 472 | std::array<char, 32> timeString = {}; |
| 473 | auto bytes = std::strftime(timeString.data(), timeString.size(), |
Yi-Shum | 536665b | 2024-05-14 10:08:27 +0800 | [diff] [blame] | 474 | "%Y%m%dT%H%M%SZ", &fruTime); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 475 | if (bytes == 0) |
| 476 | { |
| 477 | std::cerr << "invalid time string encountered\n"; |
| 478 | return resCodes::resErr; |
| 479 | } |
| 480 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 481 | result["BOARD_MANUFACTURE_DATE"] = |
| 482 | std::string_view(timeString.data(), bytes); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 483 | fruBytesIter += 3; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 484 | fruAreaFieldNames = &boardFruAreas; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 485 | break; |
| 486 | } |
| 487 | case fruAreas::fruAreaProduct: |
| 488 | { |
| 489 | uint8_t lang = *fruBytesIter; |
| 490 | result["PRODUCT_LANGUAGE_CODE"] = |
| 491 | std::to_string(static_cast<int>(lang)); |
| 492 | isLangEng = checkLangEng(lang); |
| 493 | fruBytesIter += 1; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 494 | fruAreaFieldNames = &productFruAreas; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | default: |
| 498 | { |
| 499 | std::cerr << "Internal error: unexpected FRU area index: " |
| 500 | << static_cast<int>(area) << " \n"; |
| 501 | return resCodes::resErr; |
| 502 | } |
| 503 | } |
| 504 | size_t fieldIndex = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 505 | DecodeState state = DecodeState::ok; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 506 | do |
| 507 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 508 | auto res = |
| 509 | decodeFRUData(fruBytesIter, fruBytesIterEndArea, isLangEng); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 510 | state = res.first; |
| 511 | std::string value = res.second; |
| 512 | std::string name; |
| 513 | if (fieldIndex < fruAreaFieldNames->size()) |
| 514 | { |
| 515 | name = std::string(getFruAreaName(area)) + "_" + |
| 516 | fruAreaFieldNames->at(fieldIndex); |
| 517 | } |
Scron-Chang | 7798712 | 2021-03-30 20:53:52 +0800 | [diff] [blame] | 518 | else |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 519 | { |
| 520 | name = |
| 521 | std::string(getFruAreaName(area)) + "_" + |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 522 | fruCustomFieldName + |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 523 | std::to_string(fieldIndex - fruAreaFieldNames->size() + 1); |
| 524 | } |
| 525 | |
| 526 | if (state == DecodeState::ok) |
| 527 | { |
| 528 | // Strip non null characters from the end |
| 529 | value.erase(std::find_if(value.rbegin(), value.rend(), |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 530 | [](char ch) { return ch != 0; }) |
| 531 | .base(), |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 532 | value.end()); |
| 533 | |
| 534 | result[name] = std::move(value); |
| 535 | ++fieldIndex; |
| 536 | } |
| 537 | else if (state == DecodeState::err) |
| 538 | { |
| 539 | std::cerr << "Error while parsing " << name << "\n"; |
| 540 | ret = resCodes::resWarn; |
| 541 | // Cancel decoding if failed to parse any of mandatory |
| 542 | // fields |
| 543 | if (fieldIndex < fruAreaFieldNames->size()) |
| 544 | { |
| 545 | std::cerr << "Failed to parse mandatory field \n"; |
| 546 | return resCodes::resErr; |
| 547 | } |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | if (fieldIndex < fruAreaFieldNames->size()) |
| 552 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 553 | std::cerr |
| 554 | << "Mandatory fields absent in FRU area " |
| 555 | << getFruAreaName(area) << " after " << name << "\n"; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 556 | ret = resCodes::resWarn; |
| 557 | } |
| 558 | } |
| 559 | } while (state == DecodeState::ok); |
| 560 | for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++) |
| 561 | { |
| 562 | uint8_t c = *fruBytesIter; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 563 | if (c != 0U) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 564 | { |
| 565 | std::cerr << "Non-zero byte after EndOfFields in FRU area " |
| 566 | << getFruAreaName(area) << "\n"; |
| 567 | ret = resCodes::resWarn; |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
Hieu Huynh | 5286afe | 2022-10-12 11:41:12 +0000 | [diff] [blame] | 573 | /* Parsing the Multirecord UUID */ |
| 574 | parseMultirecordUUID(fruBytes, result); |
| 575 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | // Calculate new checksum for fru info area |
| 580 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 581 | std::vector<uint8_t>::const_iterator end) |
| 582 | { |
| 583 | constexpr int checksumMod = 256; |
Andrew Jeffery | 499e7aa | 2021-08-02 22:18:22 +0930 | [diff] [blame] | 584 | uint8_t sum = std::accumulate(iter, end, static_cast<uint8_t>(0)); |
| 585 | return (checksumMod - sum) % checksumMod; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData) |
| 589 | { |
| 590 | return calculateChecksum(fruAreaData.begin(), fruAreaData.end()); |
| 591 | } |
| 592 | |
| 593 | // Update new fru area length & |
| 594 | // Update checksum at new checksum location |
| 595 | // Return the offset of the area checksum byte |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 596 | unsigned int updateFRUAreaLenAndChecksum( |
| 597 | std::vector<uint8_t>& fruData, size_t fruAreaStart, |
| 598 | size_t fruAreaEndOfFieldsOffset, size_t fruAreaEndOffset) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 599 | { |
| 600 | size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart; |
| 601 | |
| 602 | // fill zeros for any remaining unused space |
| 603 | std::fill(fruData.begin() + fruAreaEndOfFieldsOffset, |
| 604 | fruData.begin() + fruAreaEndOffset, 0); |
| 605 | |
| 606 | size_t mod = traverseFRUAreaIndex % fruBlockSize; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 607 | size_t checksumLoc = 0; |
| 608 | if (mod == 0U) |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 609 | { |
| 610 | traverseFRUAreaIndex += (fruBlockSize); |
| 611 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1); |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | traverseFRUAreaIndex += (fruBlockSize - mod); |
| 616 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1); |
| 617 | } |
| 618 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 619 | size_t newFRUAreaLen = |
| 620 | (traverseFRUAreaIndex / fruBlockSize) + |
| 621 | static_cast<unsigned long>((traverseFRUAreaIndex % fruBlockSize) != 0); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 622 | size_t fruAreaLengthLoc = fruAreaStart + 1; |
| 623 | fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen); |
| 624 | |
| 625 | // Calculate new checksum |
| 626 | std::vector<uint8_t> finalFRUData; |
| 627 | std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart, |
| 628 | std::back_inserter(finalFRUData)); |
| 629 | |
| 630 | fruData[checksumLoc] = calculateChecksum(finalFRUData); |
| 631 | return checksumLoc; |
| 632 | } |
| 633 | |
| 634 | ssize_t getFieldLength(uint8_t fruFieldTypeLenValue) |
| 635 | { |
| 636 | constexpr uint8_t typeLenMask = 0x3F; |
| 637 | constexpr uint8_t endOfFields = 0xC1; |
| 638 | if (fruFieldTypeLenValue == endOfFields) |
| 639 | { |
| 640 | return -1; |
| 641 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 642 | return fruFieldTypeLenValue & typeLenMask; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData) |
| 646 | { |
| 647 | // ipmi spec format version number is currently at 1, verify it |
| 648 | if (blockData[0] != fruVersion) |
| 649 | { |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 650 | lg2::debug( |
| 651 | "FRU spec version {VERSION} not supported. Supported version is {SUPPORTED_VERSION}", |
| 652 | "VERSION", lg2::hex, blockData[0], "SUPPORTED_VERSION", lg2::hex, |
| 653 | fruVersion); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | |
| 657 | // verify pad is set to 0 |
| 658 | if (blockData[6] != 0x0) |
| 659 | { |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 660 | lg2::debug("Pad value in header is non zero, value is {VALUE}", "VALUE", |
| 661 | lg2::hex, blockData[6]); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 662 | return false; |
| 663 | } |
| 664 | |
| 665 | // verify offsets are 0, or don't point to another offset |
| 666 | std::set<uint8_t> foundOffsets; |
| 667 | for (int ii = 1; ii < 6; ii++) |
| 668 | { |
| 669 | if (blockData[ii] == 0) |
| 670 | { |
| 671 | continue; |
| 672 | } |
| 673 | auto inserted = foundOffsets.insert(blockData[ii]); |
| 674 | if (!inserted.second) |
| 675 | { |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | // validate checksum |
| 681 | size_t sum = 0; |
| 682 | for (int jj = 0; jj < 7; jj++) |
| 683 | { |
| 684 | sum += blockData[jj]; |
| 685 | } |
| 686 | sum = (256 - sum) & 0xFF; |
| 687 | |
| 688 | if (sum != blockData[7]) |
| 689 | { |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 690 | lg2::debug( |
| 691 | "Checksum {CHECKSUM} is invalid. calculated checksum is {CALCULATED_CHECKSUM}", |
| 692 | "CHECKSUM", lg2::hex, blockData[7], "CALCULATED_CHECKSUM", lg2::hex, |
| 693 | sum); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 694 | return false; |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 699 | bool findFRUHeader(FRUReader& reader, const std::string& errorHelp, |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 700 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData, |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 701 | off_t& baseOffset) |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 702 | { |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 703 | if (reader.read(baseOffset, 0x8, blockData.data()) < 0) |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 704 | { |
| 705 | std::cerr << "failed to read " << errorHelp << " base offset " |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 706 | << baseOffset << "\n"; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | |
| 710 | // check the header checksum |
| 711 | if (validateHeader(blockData)) |
| 712 | { |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | // only continue the search if we just looked at 0x0. |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 717 | if (baseOffset != 0) |
| 718 | { |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
| 722 | // now check for special cases where the IPMI data is at an offset |
| 723 | |
| 724 | // check if blockData starts with tyanHeader |
| 725 | const std::vector<uint8_t> tyanHeader = {'$', 'T', 'Y', 'A', 'N', '$'}; |
| 726 | if (blockData.size() >= tyanHeader.size() && |
| 727 | std::equal(tyanHeader.begin(), tyanHeader.end(), blockData.begin())) |
| 728 | { |
| 729 | // look for the FRU header at offset 0x6000 |
| 730 | baseOffset = 0x6000; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 731 | return findFRUHeader(reader, errorHelp, blockData, baseOffset); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 732 | } |
| 733 | |
Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 734 | lg2::debug("Illegal header {HEADER} base offset {OFFSET}", "HEADER", |
| 735 | errorHelp, "OFFSET", baseOffset); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 736 | |
| 737 | return false; |
| 738 | } |
| 739 | |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 740 | std::pair<std::vector<uint8_t>, bool> |
| 741 | readFRUContents(FRUReader& reader, const std::string& errorHelp) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 742 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 743 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 744 | off_t baseOffset = 0x0; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 745 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 746 | if (!findFRUHeader(reader, errorHelp, blockData, baseOffset)) |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 747 | { |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 748 | return {{}, false}; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | std::vector<uint8_t> device; |
| 752 | device.insert(device.end(), blockData.begin(), blockData.begin() + 8); |
| 753 | |
| 754 | bool hasMultiRecords = false; |
| 755 | size_t fruLength = fruBlockSize; // At least FRU header is present |
Vijay Khemka | 7792e39 | 2021-01-25 13:03:56 -0800 | [diff] [blame] | 756 | unsigned int prevOffset = 0; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 757 | for (fruAreas area = fruAreas::fruAreaInternal; |
| 758 | area <= fruAreas::fruAreaMultirecord; ++area) |
| 759 | { |
| 760 | // Offset value can be 255. |
| 761 | unsigned int areaOffset = device[getHeaderAreaFieldOffset(area)]; |
| 762 | if (areaOffset == 0) |
| 763 | { |
| 764 | continue; |
| 765 | } |
| 766 | |
Vijay Khemka | 7792e39 | 2021-01-25 13:03:56 -0800 | [diff] [blame] | 767 | /* Check for offset order, as per Section 17 of FRU specification, FRU |
| 768 | * information areas are required to be in order in FRU data layout |
| 769 | * which means all offset value should be in increasing order or can be |
| 770 | * 0 if that area is not present |
| 771 | */ |
| 772 | if (areaOffset <= prevOffset) |
| 773 | { |
| 774 | std::cerr << "Fru area offsets are not in required order as per " |
| 775 | "Section 17 of Fru specification\n"; |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 776 | return {{}, true}; |
Vijay Khemka | 7792e39 | 2021-01-25 13:03:56 -0800 | [diff] [blame] | 777 | } |
| 778 | prevOffset = areaOffset; |
| 779 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 780 | // MultiRecords are different. area is not tracking section, it's |
| 781 | // walking the common header. |
| 782 | if (area == fruAreas::fruAreaMultirecord) |
| 783 | { |
| 784 | hasMultiRecords = true; |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | areaOffset *= fruBlockSize; |
| 789 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 790 | if (reader.read(baseOffset + areaOffset, 0x2, blockData.data()) < 0) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 791 | { |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 792 | std::cerr << "failed to read " << errorHelp << " base offset " |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 793 | << baseOffset << "\n"; |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 794 | return {{}, true}; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | // Ignore data type (blockData is already unsigned). |
| 798 | size_t length = blockData[1] * fruBlockSize; |
| 799 | areaOffset += length; |
| 800 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 801 | } |
| 802 | |
| 803 | if (hasMultiRecords) |
| 804 | { |
| 805 | // device[area count] is the index to the last area because the 0th |
| 806 | // entry is not an offset in the common header. |
| 807 | unsigned int areaOffset = |
| 808 | device[getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord)]; |
| 809 | areaOffset *= fruBlockSize; |
| 810 | |
| 811 | // the multi-area record header is 5 bytes long. |
| 812 | constexpr size_t multiRecordHeaderSize = 5; |
| 813 | constexpr uint8_t multiRecordEndOfListMask = 0x80; |
| 814 | |
| 815 | // Sanity hard-limit to 64KB. |
| 816 | while (areaOffset < std::numeric_limits<uint16_t>::max()) |
| 817 | { |
| 818 | // In multi-area, the area offset points to the 0th record, each |
| 819 | // record has 3 bytes of the header we care about. |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 820 | if (reader.read(baseOffset + areaOffset, 0x3, blockData.data()) < 0) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 821 | { |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 822 | std::cerr << "failed to read " << errorHelp << " base offset " |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 823 | << baseOffset << "\n"; |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 824 | return {{}, true}; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Ok, let's check the record length, which is in bytes (unsigned, |
| 828 | // up to 255, so blockData should hold uint8_t not char) |
| 829 | size_t recordLength = blockData[2]; |
| 830 | areaOffset += (recordLength + multiRecordHeaderSize); |
| 831 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 832 | |
| 833 | // If this is the end of the list bail. |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 834 | if ((blockData[1] & multiRecordEndOfListMask) != 0) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 835 | { |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // You already copied these first 8 bytes (the ipmi fru header size) |
| 842 | fruLength -= std::min(fruBlockSize, fruLength); |
| 843 | |
| 844 | int readOffset = fruBlockSize; |
| 845 | |
| 846 | while (fruLength > 0) |
| 847 | { |
| 848 | size_t requestLength = |
| 849 | std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength); |
| 850 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 851 | if (reader.read(baseOffset + readOffset, requestLength, |
| 852 | blockData.data()) < 0) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 853 | { |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 854 | std::cerr << "failed to read " << errorHelp << " base offset " |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 855 | << baseOffset << "\n"; |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 856 | return {{}, true}; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | device.insert(device.end(), blockData.begin(), |
| 860 | blockData.begin() + requestLength); |
| 861 | |
| 862 | readOffset += requestLength; |
| 863 | fruLength -= std::min(requestLength, fruLength); |
| 864 | } |
| 865 | |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 866 | return {device, true}; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | unsigned int getHeaderAreaFieldOffset(fruAreas area) |
| 870 | { |
| 871 | return static_cast<unsigned int>(area) + 1; |
| 872 | } |
Kumar Thangavel | 7135f3d | 2022-02-04 12:14:24 +0530 | [diff] [blame] | 873 | |
krishnar4 | 213ee21 | 2022-11-11 15:39:30 +0530 | [diff] [blame] | 874 | std::vector<uint8_t>& getFRUInfo(const uint16_t& bus, const uint8_t& address) |
Kumar Thangavel | 7135f3d | 2022-02-04 12:14:24 +0530 | [diff] [blame] | 875 | { |
| 876 | auto deviceMap = busMap.find(bus); |
| 877 | if (deviceMap == busMap.end()) |
| 878 | { |
| 879 | throw std::invalid_argument("Invalid Bus."); |
| 880 | } |
| 881 | auto device = deviceMap->second->find(address); |
| 882 | if (device == deviceMap->second->end()) |
| 883 | { |
| 884 | throw std::invalid_argument("Invalid Address."); |
| 885 | } |
| 886 | std::vector<uint8_t>& ret = device->second; |
| 887 | |
| 888 | return ret; |
| 889 | } |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 890 | |
| 891 | // Iterate FruArea Names and find start and size of the fru area that contains |
| 892 | // the propertyName and the field start location for the property. fruAreaParams |
| 893 | // struct values fruAreaStart, fruAreaSize, fruAreaEnd, fieldLoc values gets |
| 894 | // updated/returned if successful. |
| 895 | |
| 896 | bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData, |
| 897 | const std::string& propertyName, |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 898 | struct FruArea& fruAreaParams) |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 899 | { |
| 900 | const std::vector<std::string>* fruAreaFieldNames = nullptr; |
| 901 | |
| 902 | uint8_t fruAreaOffsetFieldValue = 0; |
| 903 | size_t offset = 0; |
| 904 | std::string areaName = propertyName.substr(0, propertyName.find('_')); |
| 905 | std::string propertyNamePrefix = areaName + "_"; |
| 906 | auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName); |
| 907 | if (it == fruAreaNames.end()) |
| 908 | { |
| 909 | std::cerr << "Can't parse area name for property " << propertyName |
| 910 | << " \n"; |
| 911 | return false; |
| 912 | } |
| 913 | fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin()); |
| 914 | fruAreaOffsetFieldValue = |
| 915 | fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)]; |
| 916 | switch (fruAreaToUpdate) |
| 917 | { |
| 918 | case fruAreas::fruAreaChassis: |
| 919 | offset = 3; // chassis part number offset. Skip fixed first 3 bytes |
| 920 | fruAreaFieldNames = &chassisFruAreas; |
| 921 | break; |
| 922 | case fruAreas::fruAreaBoard: |
| 923 | offset = 6; // board manufacturer offset. Skip fixed first 6 bytes |
| 924 | fruAreaFieldNames = &boardFruAreas; |
| 925 | break; |
| 926 | case fruAreas::fruAreaProduct: |
| 927 | // Manufacturer name offset. Skip fixed first 3 product fru bytes |
| 928 | // i.e. version, area length and language code |
| 929 | offset = 3; |
| 930 | fruAreaFieldNames = &productFruAreas; |
| 931 | break; |
| 932 | default: |
| 933 | std::cerr << "Invalid PropertyName " << propertyName << " \n"; |
| 934 | return false; |
| 935 | } |
| 936 | if (fruAreaOffsetFieldValue == 0) |
| 937 | { |
| 938 | std::cerr << "FRU Area for " << propertyName << " not present \n"; |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | fruAreaParams.start = fruAreaOffsetFieldValue * fruBlockSize; |
| 943 | fruAreaParams.size = fruData[fruAreaParams.start + 1] * fruBlockSize; |
| 944 | fruAreaParams.end = fruAreaParams.start + fruAreaParams.size; |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 945 | size_t fruDataIter = fruAreaParams.start + offset; |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 946 | size_t skipToFRUUpdateField = 0; |
| 947 | ssize_t fieldLength = 0; |
| 948 | |
| 949 | bool found = false; |
| 950 | for (const auto& field : *fruAreaFieldNames) |
| 951 | { |
| 952 | skipToFRUUpdateField++; |
| 953 | if (propertyName == propertyNamePrefix + field) |
| 954 | { |
| 955 | found = true; |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | if (!found) |
| 960 | { |
| 961 | std::size_t pos = propertyName.find(fruCustomFieldName); |
| 962 | if (pos == std::string::npos) |
| 963 | { |
| 964 | std::cerr << "PropertyName doesn't exist in FRU Area Vectors: " |
| 965 | << propertyName << "\n"; |
| 966 | return false; |
| 967 | } |
| 968 | std::string fieldNumStr = |
| 969 | propertyName.substr(pos + fruCustomFieldName.length()); |
| 970 | size_t fieldNum = std::stoi(fieldNumStr); |
| 971 | if (fieldNum == 0) |
| 972 | { |
| 973 | std::cerr << "PropertyName not recognized: " << propertyName |
| 974 | << "\n"; |
| 975 | return false; |
| 976 | } |
| 977 | skipToFRUUpdateField += fieldNum; |
| 978 | } |
| 979 | |
| 980 | for (size_t i = 1; i < skipToFRUUpdateField; i++) |
| 981 | { |
| 982 | if (fruDataIter < fruData.size()) |
| 983 | { |
| 984 | fieldLength = getFieldLength(fruData[fruDataIter]); |
| 985 | |
| 986 | if (fieldLength < 0) |
| 987 | { |
| 988 | break; |
| 989 | } |
| 990 | fruDataIter += 1 + fieldLength; |
| 991 | } |
| 992 | } |
| 993 | fruAreaParams.updateFieldLoc = fruDataIter; |
| 994 | |
| 995 | return true; |
| 996 | } |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 997 | |
| 998 | // Copy the FRU Area fields and properties into restFRUAreaFieldsData vector. |
| 999 | // Return true for success and false for failure. |
| 1000 | |
| 1001 | bool copyRestFRUArea(std::vector<uint8_t>& fruData, |
| 1002 | const std::string& propertyName, |
| 1003 | struct FruArea& fruAreaParams, |
| 1004 | std::vector<uint8_t>& restFRUAreaFieldsData) |
| 1005 | { |
| 1006 | size_t fieldLoc = fruAreaParams.updateFieldLoc; |
| 1007 | size_t start = fruAreaParams.start; |
| 1008 | size_t fruAreaSize = fruAreaParams.size; |
| 1009 | |
| 1010 | // Push post update fru field bytes to a vector |
| 1011 | ssize_t fieldLength = getFieldLength(fruData[fieldLoc]); |
| 1012 | if (fieldLength < 0) |
| 1013 | { |
| 1014 | std::cerr << "Property " << propertyName << " not present \n"; |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | size_t fruDataIter = 0; |
| 1019 | fruDataIter = fieldLoc; |
| 1020 | fruDataIter += 1 + fieldLength; |
| 1021 | size_t restFRUFieldsLoc = fruDataIter; |
| 1022 | size_t endOfFieldsLoc = 0; |
| 1023 | |
| 1024 | if (fruDataIter < fruData.size()) |
| 1025 | { |
| 1026 | while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0) |
| 1027 | { |
| 1028 | if (fruDataIter >= (start + fruAreaSize)) |
| 1029 | { |
| 1030 | fruDataIter = start + fruAreaSize; |
| 1031 | break; |
| 1032 | } |
| 1033 | fruDataIter += 1 + fieldLength; |
| 1034 | } |
| 1035 | endOfFieldsLoc = fruDataIter; |
| 1036 | } |
| 1037 | |
| 1038 | std::copy_n(fruData.begin() + restFRUFieldsLoc, |
| 1039 | endOfFieldsLoc - restFRUFieldsLoc + 1, |
| 1040 | std::back_inserter(restFRUAreaFieldsData)); |
| 1041 | |
| 1042 | fruAreaParams.restFieldsLoc = restFRUFieldsLoc; |
| 1043 | fruAreaParams.restFieldsEnd = endOfFieldsLoc; |
| 1044 | |
| 1045 | return true; |
| 1046 | } |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 1047 | |
| 1048 | // Get all device dbus path and match path with product name using |
| 1049 | // regular expression and find the device index for all devices. |
| 1050 | |
| 1051 | std::optional<int> findIndexForFRU( |
| 1052 | boost::container::flat_map< |
| 1053 | std::pair<size_t, size_t>, |
| 1054 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 1055 | std::string& productName) |
| 1056 | { |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 1057 | int highest = -1; |
| 1058 | bool found = false; |
| 1059 | |
Patrick Williams | df19061 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1060 | for (const auto& busIface : dbusInterfaceMap) |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 1061 | { |
| 1062 | std::string path = busIface.second->get_object_path(); |
| 1063 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
| 1064 | { |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 1065 | // Check if the match named has extra information. |
| 1066 | found = true; |
| 1067 | std::smatch baseMatch; |
| 1068 | |
| 1069 | bool match = std::regex_match(path, baseMatch, |
| 1070 | std::regex(productName + "_(\\d+)$")); |
| 1071 | if (match) |
| 1072 | { |
| 1073 | if (baseMatch.size() == 2) |
| 1074 | { |
| 1075 | std::ssub_match baseSubMatch = baseMatch[1]; |
| 1076 | std::string base = baseSubMatch.str(); |
| 1077 | |
| 1078 | int value = std::stoi(base); |
| 1079 | highest = (value > highest) ? value : highest; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | } // end searching objects |
| 1084 | |
| 1085 | if (!found) |
| 1086 | { |
| 1087 | return std::nullopt; |
| 1088 | } |
| 1089 | return highest; |
| 1090 | } |
Kumar Thangavel | 9f2162a | 2022-08-10 18:05:20 +0530 | [diff] [blame] | 1091 | |
| 1092 | // This function does format fru data as per IPMI format and find the |
| 1093 | // productName in the formatted fru data, get that productName and return |
| 1094 | // productName if found or return NULL. |
| 1095 | |
| 1096 | std::optional<std::string> getProductName( |
| 1097 | std::vector<uint8_t>& device, |
| 1098 | boost::container::flat_map<std::string, std::string>& formattedFRU, |
| 1099 | uint32_t bus, uint32_t address, size_t& unknownBusObjectCount) |
| 1100 | { |
| 1101 | std::string productName; |
| 1102 | |
| 1103 | resCodes res = formatIPMIFRU(device, formattedFRU); |
| 1104 | if (res == resCodes::resErr) |
| 1105 | { |
| 1106 | std::cerr << "failed to parse FRU for device at bus " << bus |
| 1107 | << " address " << address << "\n"; |
| 1108 | return std::nullopt; |
| 1109 | } |
| 1110 | if (res == resCodes::resWarn) |
| 1111 | { |
| 1112 | std::cerr << "Warnings while parsing FRU for device at bus " << bus |
| 1113 | << " address " << address << "\n"; |
| 1114 | } |
| 1115 | |
| 1116 | auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME"); |
| 1117 | // Not found under Board section or an empty string. |
| 1118 | if (productNameFind == formattedFRU.end() || |
| 1119 | productNameFind->second.empty()) |
| 1120 | { |
| 1121 | productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME"); |
| 1122 | } |
| 1123 | // Found under Product section and not an empty string. |
| 1124 | if (productNameFind != formattedFRU.end() && |
| 1125 | !productNameFind->second.empty()) |
| 1126 | { |
| 1127 | productName = productNameFind->second; |
| 1128 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 1129 | productName = std::regex_replace(productName, illegalObject, "_"); |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | productName = "UNKNOWN" + std::to_string(unknownBusObjectCount); |
| 1134 | unknownBusObjectCount++; |
| 1135 | } |
| 1136 | return productName; |
| 1137 | } |
Kumar Thangavel | 9d6f590 | 2022-08-26 16:52:14 +0530 | [diff] [blame] | 1138 | |
| 1139 | bool getFruData(std::vector<uint8_t>& fruData, uint32_t bus, uint32_t address) |
| 1140 | { |
| 1141 | try |
| 1142 | { |
| 1143 | fruData = getFRUInfo(static_cast<uint16_t>(bus), |
| 1144 | static_cast<uint8_t>(address)); |
| 1145 | } |
| 1146 | catch (const std::invalid_argument& e) |
| 1147 | { |
| 1148 | std::cerr << "Failure getting FRU Info" << e.what() << "\n"; |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | return !fruData.empty(); |
| 1153 | } |