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 | */ |
| 16 | /// \file FruUtils.cpp |
| 17 | |
| 18 | #include "FruUtils.hpp" |
| 19 | |
| 20 | #include <array> |
| 21 | #include <cstdint> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 22 | #include <filesystem> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 23 | #include <iostream> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 24 | #include <numeric> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | extern "C" |
| 30 | { |
| 31 | // Include for I2C_SMBUS_BLOCK_MAX |
| 32 | #include <linux/i2c.h> |
| 33 | } |
| 34 | |
| 35 | static constexpr bool DEBUG = false; |
| 36 | constexpr size_t fruVersion = 1; // Current FRU spec version number is 1 |
| 37 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 38 | const std::tm intelEpoch(void) |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 39 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 40 | std::tm val = {}; |
| 41 | val.tm_year = 1996 - 1900; |
Scron-Chang | 9e5a675 | 2021-03-16 10:51:50 +0800 | [diff] [blame^] | 42 | val.tm_mday = 1; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 43 | return val; |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 46 | char sixBitToChar(uint8_t val) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 47 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 48 | return static_cast<char>((val & 0x3f) + ' '); |
| 49 | } |
| 50 | |
| 51 | char bcdPlusToChar(uint8_t val) |
| 52 | { |
| 53 | val &= 0xf; |
| 54 | return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10]; |
| 55 | } |
| 56 | |
| 57 | enum FRUDataEncoding |
| 58 | { |
| 59 | binary = 0x0, |
| 60 | bcdPlus = 0x1, |
| 61 | sixBitASCII = 0x2, |
| 62 | languageDependent = 0x3, |
| 63 | }; |
| 64 | |
| 65 | /* Decode FRU data into a std::string, given an input iterator and end. If the |
| 66 | * state returned is fruDataOk, then the resulting string is the decoded FRU |
| 67 | * data. The input iterator is advanced past the data consumed. |
| 68 | * |
| 69 | * On fruDataErr, we have lost synchronisation with the length bytes, so the |
| 70 | * iterator is no longer usable. |
| 71 | */ |
| 72 | std::pair<DecodeState, std::string> |
| 73 | decodeFRUData(std::vector<uint8_t>::const_iterator& iter, |
| 74 | const std::vector<uint8_t>::const_iterator& end, |
| 75 | bool isLangEng) |
| 76 | { |
| 77 | std::string value; |
| 78 | unsigned int i; |
| 79 | |
| 80 | /* we need at least one byte to decode the type/len header */ |
| 81 | if (iter == end) |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 82 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 83 | std::cerr << "Truncated FRU data\n"; |
| 84 | return make_pair(DecodeState::err, value); |
| 85 | } |
| 86 | |
| 87 | uint8_t c = *(iter++); |
| 88 | |
| 89 | /* 0xc1 is the end marker */ |
| 90 | if (c == 0xc1) |
| 91 | { |
| 92 | return make_pair(DecodeState::end, value); |
| 93 | } |
| 94 | |
| 95 | /* decode type/len byte */ |
| 96 | uint8_t type = static_cast<uint8_t>(c >> 6); |
| 97 | uint8_t len = static_cast<uint8_t>(c & 0x3f); |
| 98 | |
| 99 | /* we should have at least len bytes of data available overall */ |
| 100 | if (iter + len > end) |
| 101 | { |
| 102 | std::cerr << "FRU data field extends past end of FRU area data\n"; |
| 103 | return make_pair(DecodeState::err, value); |
| 104 | } |
| 105 | |
| 106 | switch (type) |
| 107 | { |
| 108 | case FRUDataEncoding::binary: |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 109 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 110 | std::stringstream ss; |
| 111 | ss << std::hex << std::setfill('0'); |
| 112 | for (i = 0; i < len; i++, iter++) |
| 113 | { |
| 114 | uint8_t val = static_cast<uint8_t>(*iter); |
| 115 | ss << std::setw(2) << static_cast<int>(val); |
| 116 | } |
| 117 | value = ss.str(); |
| 118 | break; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 119 | } |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 120 | case FRUDataEncoding::languageDependent: |
| 121 | /* For language-code dependent encodings, assume 8-bit ASCII */ |
| 122 | value = std::string(iter, iter + len); |
| 123 | iter += len; |
| 124 | |
| 125 | /* English text is encoded in 8-bit ASCII + Latin 1. All other |
| 126 | * languages are required to use 2-byte unicode. FruDevice does not |
| 127 | * handle unicode. |
| 128 | */ |
| 129 | if (!isLangEng) |
| 130 | { |
| 131 | std::cerr << "Error: Non english string is not supported \n"; |
| 132 | return make_pair(DecodeState::err, value); |
| 133 | } |
| 134 | |
| 135 | break; |
| 136 | |
| 137 | case FRUDataEncoding::bcdPlus: |
| 138 | value = std::string(); |
| 139 | for (i = 0; i < len; i++, iter++) |
| 140 | { |
| 141 | uint8_t val = *iter; |
| 142 | value.push_back(bcdPlusToChar(val >> 4)); |
| 143 | value.push_back(bcdPlusToChar(val & 0xf)); |
| 144 | } |
| 145 | break; |
| 146 | |
| 147 | case FRUDataEncoding::sixBitASCII: |
| 148 | { |
| 149 | unsigned int accum = 0; |
| 150 | unsigned int accumBitLen = 0; |
| 151 | value = std::string(); |
| 152 | for (i = 0; i < len; i++, iter++) |
| 153 | { |
| 154 | accum |= *iter << accumBitLen; |
| 155 | accumBitLen += 8; |
| 156 | while (accumBitLen >= 6) |
| 157 | { |
| 158 | value.push_back(sixBitToChar(accum & 0x3f)); |
| 159 | accum >>= 6; |
| 160 | accumBitLen -= 6; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return make_pair(DecodeState::ok, value); |
| 168 | } |
| 169 | |
| 170 | bool checkLangEng(uint8_t lang) |
| 171 | { |
| 172 | // If Lang is not English then the encoding is defined as 2-byte UNICODE, |
| 173 | // but we don't support that. |
| 174 | if (lang && lang != 25) |
| 175 | { |
| 176 | std::cerr << "Warning: languages other than English is not " |
| 177 | "supported\n"; |
| 178 | // Return language flag as non english |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 179 | return false; |
| 180 | } |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 181 | else |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 182 | { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 183 | return true; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 184 | } |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Vijay Khemka | 06d1b4a | 2021-02-09 18:39:11 +0000 | [diff] [blame] | 187 | /* This function verifies for other offsets to check if they are not |
| 188 | * falling under other field area |
| 189 | * |
| 190 | * fruBytes: Start of Fru data |
| 191 | * currentArea: Index of current area offset to be compared against all area |
| 192 | * offset and it is a multiple of 8 bytes as per specification |
| 193 | * len: Length of current area space and it is a multiple of 8 bytes |
| 194 | * as per specification |
| 195 | */ |
| 196 | bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea, |
| 197 | uint8_t len) |
| 198 | { |
| 199 | |
| 200 | unsigned int fruBytesSize = fruBytes.size(); |
| 201 | |
| 202 | // check if Fru data has at least 8 byte header |
| 203 | if (fruBytesSize <= fruBlockSize) |
| 204 | { |
| 205 | std::cerr << "Error: trying to parse empty FRU\n"; |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // Check range of passed currentArea value |
| 210 | if (currentArea > fruAreas::fruAreaMultirecord) |
| 211 | { |
| 212 | std::cerr << "Error: Fru area is out of range\n"; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | unsigned int currentAreaIndex = getHeaderAreaFieldOffset(currentArea); |
| 217 | if (currentAreaIndex > fruBytesSize) |
| 218 | { |
| 219 | std::cerr << "Error: Fru area index is out of range\n"; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | unsigned int start = fruBytes[currentAreaIndex]; |
| 224 | unsigned int end = start + len; |
| 225 | |
| 226 | /* Verify each offset within the range of start and end */ |
| 227 | for (fruAreas area = fruAreas::fruAreaInternal; |
| 228 | area <= fruAreas::fruAreaMultirecord; ++area) |
| 229 | { |
| 230 | // skip the current offset |
| 231 | if (area == currentArea) |
| 232 | { |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | unsigned int areaIndex = getHeaderAreaFieldOffset(area); |
| 237 | if (areaIndex > fruBytesSize) |
| 238 | { |
| 239 | std::cerr << "Error: Fru area index is out of range\n"; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | unsigned int areaOffset = fruBytes[areaIndex]; |
| 244 | // if areaOffset is 0 means this area is not available so skip |
| 245 | if (areaOffset == 0) |
| 246 | { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | // check for overlapping of current offset with given areaoffset |
| 251 | if (areaOffset == start || (areaOffset > start && areaOffset < end)) |
| 252 | { |
| 253 | std::cerr << getFruAreaName(currentArea) |
| 254 | << " offset is overlapping with " << getFruAreaName(area) |
| 255 | << " offset\n"; |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 262 | resCodes formatFRU(const std::vector<uint8_t>& fruBytes, |
| 263 | boost::container::flat_map<std::string, std::string>& result) |
| 264 | { |
| 265 | resCodes ret = resCodes::resOK; |
| 266 | if (fruBytes.size() <= fruBlockSize) |
| 267 | { |
| 268 | std::cerr << "Error: trying to parse empty FRU \n"; |
| 269 | return resCodes::resErr; |
| 270 | } |
| 271 | result["Common_Format_Version"] = |
| 272 | std::to_string(static_cast<int>(*fruBytes.begin())); |
| 273 | |
| 274 | const std::vector<std::string>* fruAreaFieldNames; |
| 275 | |
| 276 | // Don't parse Internal and Multirecord areas |
| 277 | for (fruAreas area = fruAreas::fruAreaChassis; |
| 278 | area <= fruAreas::fruAreaProduct; ++area) |
| 279 | { |
| 280 | |
| 281 | size_t offset = *(fruBytes.begin() + getHeaderAreaFieldOffset(area)); |
| 282 | if (offset == 0) |
| 283 | { |
| 284 | continue; |
| 285 | } |
| 286 | offset *= fruBlockSize; |
| 287 | std::vector<uint8_t>::const_iterator fruBytesIter = |
| 288 | fruBytes.begin() + offset; |
| 289 | if (fruBytesIter + fruBlockSize >= fruBytes.end()) |
| 290 | { |
| 291 | std::cerr << "Not enough data to parse \n"; |
| 292 | return resCodes::resErr; |
| 293 | } |
| 294 | // check for format version 1 |
| 295 | if (*fruBytesIter != 0x01) |
| 296 | { |
| 297 | std::cerr << "Unexpected version " << *fruBytesIter << "\n"; |
| 298 | return resCodes::resErr; |
| 299 | } |
| 300 | ++fruBytesIter; |
| 301 | |
| 302 | /* Verify other area offset for overlap with current area by passing |
| 303 | * length of current area offset pointed by *fruBytesIter |
| 304 | */ |
| 305 | if (!verifyOffset(fruBytes, area, *fruBytesIter)) |
| 306 | { |
| 307 | return resCodes::resErr; |
| 308 | } |
| 309 | |
| 310 | uint8_t fruAreaSize = *fruBytesIter * fruBlockSize; |
| 311 | std::vector<uint8_t>::const_iterator fruBytesIterEndArea = |
| 312 | fruBytes.begin() + offset + fruAreaSize - 1; |
| 313 | ++fruBytesIter; |
| 314 | |
| 315 | uint8_t fruComputedChecksum = |
| 316 | calculateChecksum(fruBytes.begin() + offset, fruBytesIterEndArea); |
| 317 | if (fruComputedChecksum != *fruBytesIterEndArea) |
| 318 | { |
| 319 | std::stringstream ss; |
| 320 | ss << std::hex << std::setfill('0'); |
| 321 | ss << "Checksum error in FRU area " << getFruAreaName(area) << "\n"; |
| 322 | ss << "\tComputed checksum: 0x" << std::setw(2) |
| 323 | << static_cast<int>(fruComputedChecksum) << "\n"; |
| 324 | ss << "\tThe read checksum: 0x" << std::setw(2) |
| 325 | << static_cast<int>(*fruBytesIterEndArea) << "\n"; |
| 326 | std::cerr << ss.str(); |
| 327 | ret = resCodes::resWarn; |
| 328 | } |
| 329 | |
| 330 | /* Set default language flag to true as Chassis Fru area are always |
| 331 | * encoded in English defined in Section 10 of Fru specification |
| 332 | */ |
| 333 | |
| 334 | bool isLangEng = true; |
| 335 | switch (area) |
| 336 | { |
| 337 | case fruAreas::fruAreaChassis: |
| 338 | { |
| 339 | result["CHASSIS_TYPE"] = |
| 340 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 341 | fruBytesIter += 1; |
| 342 | fruAreaFieldNames = &CHASSIS_FRU_AREAS; |
| 343 | break; |
| 344 | } |
| 345 | case fruAreas::fruAreaBoard: |
| 346 | { |
| 347 | uint8_t lang = *fruBytesIter; |
| 348 | result["BOARD_LANGUAGE_CODE"] = |
| 349 | std::to_string(static_cast<int>(lang)); |
| 350 | isLangEng = checkLangEng(lang); |
| 351 | fruBytesIter += 1; |
| 352 | |
| 353 | unsigned int minutes = *fruBytesIter | |
| 354 | *(fruBytesIter + 1) << 8 | |
| 355 | *(fruBytesIter + 2) << 16; |
| 356 | std::tm fruTime = intelEpoch(); |
| 357 | std::time_t timeValue = std::mktime(&fruTime); |
| 358 | timeValue += minutes * 60; |
| 359 | fruTime = *std::gmtime(&timeValue); |
| 360 | |
| 361 | // Tue Nov 20 23:08:00 2018 |
| 362 | char timeString[32] = {0}; |
| 363 | auto bytes = std::strftime(timeString, sizeof(timeString), |
| 364 | "%Y-%m-%d - %H:%M:%S", &fruTime); |
| 365 | if (bytes == 0) |
| 366 | { |
| 367 | std::cerr << "invalid time string encountered\n"; |
| 368 | return resCodes::resErr; |
| 369 | } |
| 370 | |
| 371 | result["BOARD_MANUFACTURE_DATE"] = std::string(timeString); |
| 372 | fruBytesIter += 3; |
| 373 | fruAreaFieldNames = &BOARD_FRU_AREAS; |
| 374 | break; |
| 375 | } |
| 376 | case fruAreas::fruAreaProduct: |
| 377 | { |
| 378 | uint8_t lang = *fruBytesIter; |
| 379 | result["PRODUCT_LANGUAGE_CODE"] = |
| 380 | std::to_string(static_cast<int>(lang)); |
| 381 | isLangEng = checkLangEng(lang); |
| 382 | fruBytesIter += 1; |
| 383 | fruAreaFieldNames = &PRODUCT_FRU_AREAS; |
| 384 | break; |
| 385 | } |
| 386 | default: |
| 387 | { |
| 388 | std::cerr << "Internal error: unexpected FRU area index: " |
| 389 | << static_cast<int>(area) << " \n"; |
| 390 | return resCodes::resErr; |
| 391 | } |
| 392 | } |
| 393 | size_t fieldIndex = 0; |
| 394 | DecodeState state; |
| 395 | do |
| 396 | { |
| 397 | auto res = |
| 398 | decodeFRUData(fruBytesIter, fruBytesIterEndArea, isLangEng); |
| 399 | state = res.first; |
| 400 | std::string value = res.second; |
| 401 | std::string name; |
| 402 | if (fieldIndex < fruAreaFieldNames->size()) |
| 403 | { |
| 404 | name = std::string(getFruAreaName(area)) + "_" + |
| 405 | fruAreaFieldNames->at(fieldIndex); |
| 406 | } |
| 407 | { |
| 408 | name = |
| 409 | std::string(getFruAreaName(area)) + "_" + |
| 410 | FRU_CUSTOM_FIELD_NAME + |
| 411 | std::to_string(fieldIndex - fruAreaFieldNames->size() + 1); |
| 412 | } |
| 413 | |
| 414 | if (state == DecodeState::ok) |
| 415 | { |
| 416 | // Strip non null characters from the end |
| 417 | value.erase(std::find_if(value.rbegin(), value.rend(), |
| 418 | [](char ch) { return ch != 0; }) |
| 419 | .base(), |
| 420 | value.end()); |
| 421 | |
| 422 | result[name] = std::move(value); |
| 423 | ++fieldIndex; |
| 424 | } |
| 425 | else if (state == DecodeState::err) |
| 426 | { |
| 427 | std::cerr << "Error while parsing " << name << "\n"; |
| 428 | ret = resCodes::resWarn; |
| 429 | // Cancel decoding if failed to parse any of mandatory |
| 430 | // fields |
| 431 | if (fieldIndex < fruAreaFieldNames->size()) |
| 432 | { |
| 433 | std::cerr << "Failed to parse mandatory field \n"; |
| 434 | return resCodes::resErr; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | if (fieldIndex < fruAreaFieldNames->size()) |
| 440 | { |
| 441 | std::cerr << "Mandatory fields absent in FRU area " |
| 442 | << getFruAreaName(area) << " after " << name |
| 443 | << "\n"; |
| 444 | ret = resCodes::resWarn; |
| 445 | } |
| 446 | } |
| 447 | } while (state == DecodeState::ok); |
| 448 | for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++) |
| 449 | { |
| 450 | uint8_t c = *fruBytesIter; |
| 451 | if (c) |
| 452 | { |
| 453 | std::cerr << "Non-zero byte after EndOfFields in FRU area " |
| 454 | << getFruAreaName(area) << "\n"; |
| 455 | ret = resCodes::resWarn; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | // Calculate new checksum for fru info area |
| 465 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 466 | std::vector<uint8_t>::const_iterator end) |
| 467 | { |
| 468 | constexpr int checksumMod = 256; |
| 469 | constexpr uint8_t modVal = 0xFF; |
| 470 | int sum = std::accumulate(iter, end, 0); |
| 471 | int checksum = (checksumMod - sum) & modVal; |
| 472 | return static_cast<uint8_t>(checksum); |
| 473 | } |
| 474 | |
| 475 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData) |
| 476 | { |
| 477 | return calculateChecksum(fruAreaData.begin(), fruAreaData.end()); |
| 478 | } |
| 479 | |
| 480 | // Update new fru area length & |
| 481 | // Update checksum at new checksum location |
| 482 | // Return the offset of the area checksum byte |
| 483 | unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData, |
| 484 | size_t fruAreaStart, |
| 485 | size_t fruAreaEndOfFieldsOffset, |
| 486 | size_t fruAreaEndOffset) |
| 487 | { |
| 488 | size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart; |
| 489 | |
| 490 | // fill zeros for any remaining unused space |
| 491 | std::fill(fruData.begin() + fruAreaEndOfFieldsOffset, |
| 492 | fruData.begin() + fruAreaEndOffset, 0); |
| 493 | |
| 494 | size_t mod = traverseFRUAreaIndex % fruBlockSize; |
| 495 | size_t checksumLoc; |
| 496 | if (!mod) |
| 497 | { |
| 498 | traverseFRUAreaIndex += (fruBlockSize); |
| 499 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1); |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | traverseFRUAreaIndex += (fruBlockSize - mod); |
| 504 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1); |
| 505 | } |
| 506 | |
| 507 | size_t newFRUAreaLen = (traverseFRUAreaIndex / fruBlockSize) + |
| 508 | ((traverseFRUAreaIndex % fruBlockSize) != 0); |
| 509 | size_t fruAreaLengthLoc = fruAreaStart + 1; |
| 510 | fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen); |
| 511 | |
| 512 | // Calculate new checksum |
| 513 | std::vector<uint8_t> finalFRUData; |
| 514 | std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart, |
| 515 | std::back_inserter(finalFRUData)); |
| 516 | |
| 517 | fruData[checksumLoc] = calculateChecksum(finalFRUData); |
| 518 | return checksumLoc; |
| 519 | } |
| 520 | |
| 521 | ssize_t getFieldLength(uint8_t fruFieldTypeLenValue) |
| 522 | { |
| 523 | constexpr uint8_t typeLenMask = 0x3F; |
| 524 | constexpr uint8_t endOfFields = 0xC1; |
| 525 | if (fruFieldTypeLenValue == endOfFields) |
| 526 | { |
| 527 | return -1; |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | return fruFieldTypeLenValue & typeLenMask; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData) |
| 536 | { |
| 537 | // ipmi spec format version number is currently at 1, verify it |
| 538 | if (blockData[0] != fruVersion) |
| 539 | { |
| 540 | if (DEBUG) |
| 541 | { |
| 542 | std::cerr << "FRU spec version " << (int)(blockData[0]) |
| 543 | << " not supported. Supported version is " |
| 544 | << (int)(fruVersion) << "\n"; |
| 545 | } |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | // verify pad is set to 0 |
| 550 | if (blockData[6] != 0x0) |
| 551 | { |
| 552 | if (DEBUG) |
| 553 | { |
| 554 | std::cerr << "PAD value in header is non zero, value is " |
| 555 | << (int)(blockData[6]) << "\n"; |
| 556 | } |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | // verify offsets are 0, or don't point to another offset |
| 561 | std::set<uint8_t> foundOffsets; |
| 562 | for (int ii = 1; ii < 6; ii++) |
| 563 | { |
| 564 | if (blockData[ii] == 0) |
| 565 | { |
| 566 | continue; |
| 567 | } |
| 568 | auto inserted = foundOffsets.insert(blockData[ii]); |
| 569 | if (!inserted.second) |
| 570 | { |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // validate checksum |
| 576 | size_t sum = 0; |
| 577 | for (int jj = 0; jj < 7; jj++) |
| 578 | { |
| 579 | sum += blockData[jj]; |
| 580 | } |
| 581 | sum = (256 - sum) & 0xFF; |
| 582 | |
| 583 | if (sum != blockData[7]) |
| 584 | { |
| 585 | if (DEBUG) |
| 586 | { |
| 587 | std::cerr << "Checksum " << (int)(blockData[7]) |
| 588 | << " is invalid. calculated checksum is " << (int)(sum) |
| 589 | << "\n"; |
| 590 | } |
| 591 | return false; |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 596 | std::vector<uint8_t> readFRUContents(int flag, int file, uint16_t address, |
| 597 | ReadBlockFunc readBlock, |
| 598 | const std::string& errorHelp) |
| 599 | { |
| 600 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
| 601 | |
| 602 | if (readBlock(flag, file, address, 0x0, 0x8, blockData.data()) < 0) |
| 603 | { |
| 604 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 605 | return {}; |
| 606 | } |
| 607 | |
| 608 | // check the header checksum |
| 609 | if (!validateHeader(blockData)) |
| 610 | { |
| 611 | if (DEBUG) |
| 612 | { |
| 613 | std::cerr << "Illegal header " << errorHelp << "\n"; |
| 614 | } |
| 615 | |
| 616 | return {}; |
| 617 | } |
| 618 | |
| 619 | std::vector<uint8_t> device; |
| 620 | device.insert(device.end(), blockData.begin(), blockData.begin() + 8); |
| 621 | |
| 622 | bool hasMultiRecords = false; |
| 623 | size_t fruLength = fruBlockSize; // At least FRU header is present |
Vijay Khemka | 7792e39 | 2021-01-25 13:03:56 -0800 | [diff] [blame] | 624 | unsigned int prevOffset = 0; |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 625 | for (fruAreas area = fruAreas::fruAreaInternal; |
| 626 | area <= fruAreas::fruAreaMultirecord; ++area) |
| 627 | { |
| 628 | // Offset value can be 255. |
| 629 | unsigned int areaOffset = device[getHeaderAreaFieldOffset(area)]; |
| 630 | if (areaOffset == 0) |
| 631 | { |
| 632 | continue; |
| 633 | } |
| 634 | |
Vijay Khemka | 7792e39 | 2021-01-25 13:03:56 -0800 | [diff] [blame] | 635 | /* Check for offset order, as per Section 17 of FRU specification, FRU |
| 636 | * information areas are required to be in order in FRU data layout |
| 637 | * which means all offset value should be in increasing order or can be |
| 638 | * 0 if that area is not present |
| 639 | */ |
| 640 | if (areaOffset <= prevOffset) |
| 641 | { |
| 642 | std::cerr << "Fru area offsets are not in required order as per " |
| 643 | "Section 17 of Fru specification\n"; |
| 644 | return {}; |
| 645 | } |
| 646 | prevOffset = areaOffset; |
| 647 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 648 | // MultiRecords are different. area is not tracking section, it's |
| 649 | // walking the common header. |
| 650 | if (area == fruAreas::fruAreaMultirecord) |
| 651 | { |
| 652 | hasMultiRecords = true; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | areaOffset *= fruBlockSize; |
| 657 | |
| 658 | if (readBlock(flag, file, address, static_cast<uint16_t>(areaOffset), |
| 659 | 0x2, blockData.data()) < 0) |
| 660 | { |
| 661 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 662 | return {}; |
| 663 | } |
| 664 | |
| 665 | // Ignore data type (blockData is already unsigned). |
| 666 | size_t length = blockData[1] * fruBlockSize; |
| 667 | areaOffset += length; |
| 668 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 669 | } |
| 670 | |
| 671 | if (hasMultiRecords) |
| 672 | { |
| 673 | // device[area count] is the index to the last area because the 0th |
| 674 | // entry is not an offset in the common header. |
| 675 | unsigned int areaOffset = |
| 676 | device[getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord)]; |
| 677 | areaOffset *= fruBlockSize; |
| 678 | |
| 679 | // the multi-area record header is 5 bytes long. |
| 680 | constexpr size_t multiRecordHeaderSize = 5; |
| 681 | constexpr uint8_t multiRecordEndOfListMask = 0x80; |
| 682 | |
| 683 | // Sanity hard-limit to 64KB. |
| 684 | while (areaOffset < std::numeric_limits<uint16_t>::max()) |
| 685 | { |
| 686 | // In multi-area, the area offset points to the 0th record, each |
| 687 | // record has 3 bytes of the header we care about. |
| 688 | if (readBlock(flag, file, address, |
| 689 | static_cast<uint16_t>(areaOffset), 0x3, |
| 690 | blockData.data()) < 0) |
| 691 | { |
| 692 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 693 | return {}; |
| 694 | } |
| 695 | |
| 696 | // Ok, let's check the record length, which is in bytes (unsigned, |
| 697 | // up to 255, so blockData should hold uint8_t not char) |
| 698 | size_t recordLength = blockData[2]; |
| 699 | areaOffset += (recordLength + multiRecordHeaderSize); |
| 700 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 701 | |
| 702 | // If this is the end of the list bail. |
| 703 | if ((blockData[1] & multiRecordEndOfListMask)) |
| 704 | { |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | // You already copied these first 8 bytes (the ipmi fru header size) |
| 711 | fruLength -= std::min(fruBlockSize, fruLength); |
| 712 | |
| 713 | int readOffset = fruBlockSize; |
| 714 | |
| 715 | while (fruLength > 0) |
| 716 | { |
| 717 | size_t requestLength = |
| 718 | std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength); |
| 719 | |
| 720 | if (readBlock(flag, file, address, static_cast<uint16_t>(readOffset), |
| 721 | static_cast<uint8_t>(requestLength), |
| 722 | blockData.data()) < 0) |
| 723 | { |
| 724 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 725 | return {}; |
| 726 | } |
| 727 | |
| 728 | device.insert(device.end(), blockData.begin(), |
| 729 | blockData.begin() + requestLength); |
| 730 | |
| 731 | readOffset += requestLength; |
| 732 | fruLength -= std::min(requestLength, fruLength); |
| 733 | } |
| 734 | |
| 735 | return device; |
| 736 | } |
| 737 | |
| 738 | unsigned int getHeaderAreaFieldOffset(fruAreas area) |
| 739 | { |
| 740 | return static_cast<unsigned int>(area) + 1; |
| 741 | } |