blob: e18d55b8fab2d5f92842b7a0b551563031f04ecf [file] [log] [blame]
Patrick Ventureab296412020-12-30 13:39:37 -08001/*
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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_utils.cpp
Patrick Ventureab296412020-12-30 13:39:37 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "fru_utils.hpp"
Patrick Ventureab296412020-12-30 13:39:37 -080019
20#include <array>
Ed Tanous3013fb42022-07-09 08:27:06 -070021#include <cstddef>
Patrick Ventureab296412020-12-30 13:39:37 -080022#include <cstdint>
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053023#include <filesystem>
Hieu Huynh5286afe2022-10-12 11:41:12 +000024#include <iomanip>
Patrick Ventureab296412020-12-30 13:39:37 -080025#include <iostream>
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053026#include <numeric>
Patrick Ventureab296412020-12-30 13:39:37 -080027#include <set>
Hieu Huynh5286afe2022-10-12 11:41:12 +000028#include <sstream>
Patrick Ventureab296412020-12-30 13:39:37 -080029#include <string>
30#include <vector>
31
32extern "C"
33{
34// Include for I2C_SMBUS_BLOCK_MAX
35#include <linux/i2c.h>
36}
37
Ed Tanous07d467b2021-02-23 14:48:37 -080038static constexpr bool debug = false;
Patrick Ventureab296412020-12-30 13:39:37 -080039constexpr size_t fruVersion = 1; // Current FRU spec version number is 1
40
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +080041std::tm intelEpoch()
Vijay Khemka06d1b4a2021-02-09 18:39:11 +000042{
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053043 std::tm val = {};
44 val.tm_year = 1996 - 1900;
Scron-Chang9e5a6752021-03-16 10:51:50 +080045 val.tm_mday = 1;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053046 return val;
Vijay Khemka06d1b4a2021-02-09 18:39:11 +000047}
48
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053049char sixBitToChar(uint8_t val)
Patrick Ventureab296412020-12-30 13:39:37 -080050{
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053051 return static_cast<char>((val & 0x3f) + ' ');
52}
53
54char bcdPlusToChar(uint8_t val)
55{
56 val &= 0xf;
57 return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10];
58}
59
60enum FRUDataEncoding
61{
62 binary = 0x0,
63 bcdPlus = 0x1,
64 sixBitASCII = 0x2,
65 languageDependent = 0x3,
66};
67
Hieu Huynh5286afe2022-10-12 11:41:12 +000068enum MultiRecordType : uint8_t
69{
70 powerSupplyInfo = 0x00,
71 dcOutput = 0x01,
72 dcLoad = 0x02,
73 managementAccessRecord = 0x03,
74 baseCompatibilityRecord = 0x04,
75 extendedCompatibilityRecord = 0x05,
76 resvASFSMBusDeviceRecord = 0x06,
77 resvASFLegacyDeviceAlerts = 0x07,
78 resvASFRemoteControl = 0x08,
79 extendedDCOutput = 0x09,
80 extendedDCLoad = 0x0A
81};
82
83enum SubManagementAccessRecord : uint8_t
84{
85 systemManagementURL = 0x01,
86 systemName = 0x02,
87 systemPingAddress = 0x03,
88 componentManagementURL = 0x04,
89 componentName = 0x05,
90 componentPingAddress = 0x06,
91 systemUniqueID = 0x07
92};
93
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053094/* Decode FRU data into a std::string, given an input iterator and end. If the
95 * state returned is fruDataOk, then the resulting string is the decoded FRU
96 * data. The input iterator is advanced past the data consumed.
97 *
98 * On fruDataErr, we have lost synchronisation with the length bytes, so the
99 * iterator is no longer usable.
100 */
101std::pair<DecodeState, std::string>
102 decodeFRUData(std::vector<uint8_t>::const_iterator& iter,
103 const std::vector<uint8_t>::const_iterator& end,
104 bool isLangEng)
105{
106 std::string value;
Ed Tanous3013fb42022-07-09 08:27:06 -0700107 unsigned int i = 0;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530108
109 /* we need at least one byte to decode the type/len header */
110 if (iter == end)
Patrick Ventureab296412020-12-30 13:39:37 -0800111 {
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530112 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 Ventureab296412020-12-30 13:39:37 -0800138 {
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530139 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 Ventureab296412020-12-30 13:39:37 -0800148 }
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530149 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;
194 }
195
196 return make_pair(DecodeState::ok, value);
197}
198
199bool checkLangEng(uint8_t lang)
200{
201 // If Lang is not English then the encoding is defined as 2-byte UNICODE,
202 // but we don't support that.
Ed Tanous3013fb42022-07-09 08:27:06 -0700203 if ((lang != 0U) && lang != 25)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530204 {
205 std::cerr << "Warning: languages other than English is not "
206 "supported\n";
207 // Return language flag as non english
Patrick Ventureab296412020-12-30 13:39:37 -0800208 return false;
209 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800210 return true;
Patrick Ventureab296412020-12-30 13:39:37 -0800211}
212
Vijay Khemka06d1b4a2021-02-09 18:39:11 +0000213/* This function verifies for other offsets to check if they are not
214 * falling under other field area
215 *
216 * fruBytes: Start of Fru data
217 * currentArea: Index of current area offset to be compared against all area
218 * offset and it is a multiple of 8 bytes as per specification
219 * len: Length of current area space and it is a multiple of 8 bytes
220 * as per specification
221 */
222bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea,
223 uint8_t len)
224{
Vijay Khemka06d1b4a2021-02-09 18:39:11 +0000225 unsigned int fruBytesSize = fruBytes.size();
226
227 // check if Fru data has at least 8 byte header
228 if (fruBytesSize <= fruBlockSize)
229 {
230 std::cerr << "Error: trying to parse empty FRU\n";
231 return false;
232 }
233
234 // Check range of passed currentArea value
235 if (currentArea > fruAreas::fruAreaMultirecord)
236 {
237 std::cerr << "Error: Fru area is out of range\n";
238 return false;
239 }
240
241 unsigned int currentAreaIndex = getHeaderAreaFieldOffset(currentArea);
242 if (currentAreaIndex > fruBytesSize)
243 {
244 std::cerr << "Error: Fru area index is out of range\n";
245 return false;
246 }
247
248 unsigned int start = fruBytes[currentAreaIndex];
249 unsigned int end = start + len;
250
251 /* Verify each offset within the range of start and end */
252 for (fruAreas area = fruAreas::fruAreaInternal;
253 area <= fruAreas::fruAreaMultirecord; ++area)
254 {
255 // skip the current offset
256 if (area == currentArea)
257 {
258 continue;
259 }
260
261 unsigned int areaIndex = getHeaderAreaFieldOffset(area);
262 if (areaIndex > fruBytesSize)
263 {
264 std::cerr << "Error: Fru area index is out of range\n";
265 return false;
266 }
267
268 unsigned int areaOffset = fruBytes[areaIndex];
269 // if areaOffset is 0 means this area is not available so skip
270 if (areaOffset == 0)
271 {
272 continue;
273 }
274
275 // check for overlapping of current offset with given areaoffset
276 if (areaOffset == start || (areaOffset > start && areaOffset < end))
277 {
278 std::cerr << getFruAreaName(currentArea)
279 << " offset is overlapping with " << getFruAreaName(area)
280 << " offset\n";
281 return false;
282 }
283 }
284 return true;
285}
286
Hieu Huynh5286afe2022-10-12 11:41:12 +0000287static void parseMultirecordUUID(
288 const std::vector<uint8_t>& device,
289 boost::container::flat_map<std::string, std::string>& result)
290{
291 constexpr size_t uuidDataLen = 16;
292 constexpr size_t multiRecordHeaderLen = 5;
293 /* UUID record data, plus one to skip past the sub-record type byte */
294 constexpr size_t uuidRecordData = multiRecordHeaderLen + 1;
295 constexpr size_t multiRecordEndOfListMask = 0x80;
296 /* The UUID {00112233-4455-6677-8899-AABBCCDDEEFF} would thus be represented
297 * as: 0x33 0x22 0x11 0x00 0x55 0x44 0x77 0x66 0x88 0x99 0xAA 0xBB 0xCC 0xDD
298 * 0xEE 0xFF
299 */
300 const std::array<uint8_t, uuidDataLen> uuidCharOrder = {
301 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
302 uint32_t areaOffset =
303 device.at(getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord));
304
305 if (areaOffset == 0)
306 {
307 return;
308 }
309
310 areaOffset *= fruBlockSize;
311 std::vector<uint8_t>::const_iterator fruBytesIter = device.begin() +
312 areaOffset;
313
314 /* Verify area offset */
315 if (!verifyOffset(device, fruAreas::fruAreaMultirecord, *fruBytesIter))
316 {
317 return;
318 }
319 while (areaOffset + uuidRecordData + uuidDataLen <= device.size())
320 {
321 if ((areaOffset < device.size()) &&
322 (device[areaOffset] ==
323 (uint8_t)MultiRecordType::managementAccessRecord))
324 {
325 if ((areaOffset + multiRecordHeaderLen < device.size()) &&
326 (device[areaOffset + multiRecordHeaderLen] ==
327 (uint8_t)SubManagementAccessRecord::systemUniqueID))
328 {
329 /* Layout of UUID:
330 * source: https://www.ietf.org/rfc/rfc4122.txt
331 *
332 * UUID binary format (16 bytes):
333 * 4B-2B-2B-2B-6B (big endian)
334 *
335 * UUID string is 36 length of characters (36 bytes):
336 * 0 9 14 19 24
337 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
338 * be be be be be
339 * be means it should be converted to big endian.
340 */
341 /* Get UUID bytes to UUID string */
342 std::stringstream tmp;
343 tmp << std::hex << std::setfill('0');
344 for (size_t i = 0; i < uuidDataLen; i++)
345 {
346 tmp << std::setw(2)
347 << static_cast<uint16_t>(
348 device[areaOffset + uuidRecordData +
349 uuidCharOrder[i]]);
350 }
351 std::string uuidStr = tmp.str();
352 result["MULTIRECORD_UUID"] =
353 uuidStr.substr(0, 8) + '-' + uuidStr.substr(8, 4) + '-' +
354 uuidStr.substr(12, 4) + '-' + uuidStr.substr(16, 4) + '-' +
355 uuidStr.substr(20, 12);
356 break;
357 }
358 }
359 if ((device[areaOffset + 1] & multiRecordEndOfListMask) != 0)
360 {
361 break;
362 }
363 areaOffset = areaOffset + device[areaOffset + 2] + multiRecordHeaderLen;
364 }
365}
366
Michael Shen0961b112022-02-22 11:06:33 +0800367resCodes
368 formatIPMIFRU(const std::vector<uint8_t>& fruBytes,
369 boost::container::flat_map<std::string, std::string>& result)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530370{
371 resCodes ret = resCodes::resOK;
372 if (fruBytes.size() <= fruBlockSize)
373 {
374 std::cerr << "Error: trying to parse empty FRU \n";
375 return resCodes::resErr;
376 }
377 result["Common_Format_Version"] =
378 std::to_string(static_cast<int>(*fruBytes.begin()));
379
Ed Tanous3013fb42022-07-09 08:27:06 -0700380 const std::vector<std::string>* fruAreaFieldNames = nullptr;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530381
382 // Don't parse Internal and Multirecord areas
383 for (fruAreas area = fruAreas::fruAreaChassis;
384 area <= fruAreas::fruAreaProduct; ++area)
385 {
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530386 size_t offset = *(fruBytes.begin() + getHeaderAreaFieldOffset(area));
387 if (offset == 0)
388 {
389 continue;
390 }
391 offset *= fruBlockSize;
Patrick Williamsdf190612023-05-10 07:51:34 -0500392 std::vector<uint8_t>::const_iterator fruBytesIter = fruBytes.begin() +
393 offset;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530394 if (fruBytesIter + fruBlockSize >= fruBytes.end())
395 {
396 std::cerr << "Not enough data to parse \n";
397 return resCodes::resErr;
398 }
399 // check for format version 1
400 if (*fruBytesIter != 0x01)
401 {
402 std::cerr << "Unexpected version " << *fruBytesIter << "\n";
403 return resCodes::resErr;
404 }
405 ++fruBytesIter;
406
407 /* Verify other area offset for overlap with current area by passing
408 * length of current area offset pointed by *fruBytesIter
409 */
410 if (!verifyOffset(fruBytes, area, *fruBytesIter))
411 {
412 return resCodes::resErr;
413 }
414
Andrew Jeffery65ed6642021-08-02 22:32:23 +0930415 size_t fruAreaSize = *fruBytesIter * fruBlockSize;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530416 std::vector<uint8_t>::const_iterator fruBytesIterEndArea =
417 fruBytes.begin() + offset + fruAreaSize - 1;
418 ++fruBytesIter;
419
420 uint8_t fruComputedChecksum =
421 calculateChecksum(fruBytes.begin() + offset, fruBytesIterEndArea);
422 if (fruComputedChecksum != *fruBytesIterEndArea)
423 {
424 std::stringstream ss;
425 ss << std::hex << std::setfill('0');
426 ss << "Checksum error in FRU area " << getFruAreaName(area) << "\n";
427 ss << "\tComputed checksum: 0x" << std::setw(2)
428 << static_cast<int>(fruComputedChecksum) << "\n";
429 ss << "\tThe read checksum: 0x" << std::setw(2)
430 << static_cast<int>(*fruBytesIterEndArea) << "\n";
431 std::cerr << ss.str();
432 ret = resCodes::resWarn;
433 }
434
435 /* Set default language flag to true as Chassis Fru area are always
436 * encoded in English defined in Section 10 of Fru specification
437 */
438
439 bool isLangEng = true;
440 switch (area)
441 {
442 case fruAreas::fruAreaChassis:
443 {
444 result["CHASSIS_TYPE"] =
445 std::to_string(static_cast<int>(*fruBytesIter));
446 fruBytesIter += 1;
Ed Tanous07d467b2021-02-23 14:48:37 -0800447 fruAreaFieldNames = &chassisFruAreas;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530448 break;
449 }
450 case fruAreas::fruAreaBoard:
451 {
452 uint8_t lang = *fruBytesIter;
453 result["BOARD_LANGUAGE_CODE"] =
454 std::to_string(static_cast<int>(lang));
455 isLangEng = checkLangEng(lang);
456 fruBytesIter += 1;
457
458 unsigned int minutes = *fruBytesIter |
459 *(fruBytesIter + 1) << 8 |
460 *(fruBytesIter + 2) << 16;
461 std::tm fruTime = intelEpoch();
Willy Tu3f98b5e2023-04-12 08:16:52 -0700462 std::time_t timeValue = timegm(&fruTime);
Ed Tanous3013fb42022-07-09 08:27:06 -0700463 timeValue += static_cast<long>(minutes) * 60;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530464 fruTime = *std::gmtime(&timeValue);
465
466 // Tue Nov 20 23:08:00 2018
Ed Tanous3013fb42022-07-09 08:27:06 -0700467 std::array<char, 32> timeString = {};
468 auto bytes = std::strftime(timeString.data(), timeString.size(),
Willy Tu3f98b5e2023-04-12 08:16:52 -0700469 "%Y-%m-%d - %H:%M:%S UTC", &fruTime);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530470 if (bytes == 0)
471 {
472 std::cerr << "invalid time string encountered\n";
473 return resCodes::resErr;
474 }
475
Ed Tanous3013fb42022-07-09 08:27:06 -0700476 result["BOARD_MANUFACTURE_DATE"] =
477 std::string_view(timeString.data(), bytes);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530478 fruBytesIter += 3;
Ed Tanous07d467b2021-02-23 14:48:37 -0800479 fruAreaFieldNames = &boardFruAreas;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530480 break;
481 }
482 case fruAreas::fruAreaProduct:
483 {
484 uint8_t lang = *fruBytesIter;
485 result["PRODUCT_LANGUAGE_CODE"] =
486 std::to_string(static_cast<int>(lang));
487 isLangEng = checkLangEng(lang);
488 fruBytesIter += 1;
Ed Tanous07d467b2021-02-23 14:48:37 -0800489 fruAreaFieldNames = &productFruAreas;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530490 break;
491 }
492 default:
493 {
494 std::cerr << "Internal error: unexpected FRU area index: "
495 << static_cast<int>(area) << " \n";
496 return resCodes::resErr;
497 }
498 }
499 size_t fieldIndex = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700500 DecodeState state = DecodeState::ok;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530501 do
502 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500503 auto res = decodeFRUData(fruBytesIter, fruBytesIterEndArea,
504 isLangEng);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530505 state = res.first;
506 std::string value = res.second;
507 std::string name;
508 if (fieldIndex < fruAreaFieldNames->size())
509 {
510 name = std::string(getFruAreaName(area)) + "_" +
511 fruAreaFieldNames->at(fieldIndex);
512 }
Scron-Chang77987122021-03-30 20:53:52 +0800513 else
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530514 {
515 name =
516 std::string(getFruAreaName(area)) + "_" +
Ed Tanous07d467b2021-02-23 14:48:37 -0800517 fruCustomFieldName +
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530518 std::to_string(fieldIndex - fruAreaFieldNames->size() + 1);
519 }
520
521 if (state == DecodeState::ok)
522 {
523 // Strip non null characters from the end
524 value.erase(std::find_if(value.rbegin(), value.rend(),
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500525 [](char ch) {
526 return ch != 0;
527 }).base(),
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530528 value.end());
529
530 result[name] = std::move(value);
531 ++fieldIndex;
532 }
533 else if (state == DecodeState::err)
534 {
535 std::cerr << "Error while parsing " << name << "\n";
536 ret = resCodes::resWarn;
537 // Cancel decoding if failed to parse any of mandatory
538 // fields
539 if (fieldIndex < fruAreaFieldNames->size())
540 {
541 std::cerr << "Failed to parse mandatory field \n";
542 return resCodes::resErr;
543 }
544 }
545 else
546 {
547 if (fieldIndex < fruAreaFieldNames->size())
548 {
549 std::cerr << "Mandatory fields absent in FRU area "
550 << getFruAreaName(area) << " after " << name
551 << "\n";
552 ret = resCodes::resWarn;
553 }
554 }
555 } while (state == DecodeState::ok);
556 for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++)
557 {
558 uint8_t c = *fruBytesIter;
Ed Tanous3013fb42022-07-09 08:27:06 -0700559 if (c != 0U)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530560 {
561 std::cerr << "Non-zero byte after EndOfFields in FRU area "
562 << getFruAreaName(area) << "\n";
563 ret = resCodes::resWarn;
564 break;
565 }
566 }
567 }
568
Hieu Huynh5286afe2022-10-12 11:41:12 +0000569 /* Parsing the Multirecord UUID */
570 parseMultirecordUUID(fruBytes, result);
571
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530572 return ret;
573}
574
575// Calculate new checksum for fru info area
576uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
577 std::vector<uint8_t>::const_iterator end)
578{
579 constexpr int checksumMod = 256;
Andrew Jeffery499e7aa2021-08-02 22:18:22 +0930580 uint8_t sum = std::accumulate(iter, end, static_cast<uint8_t>(0));
581 return (checksumMod - sum) % checksumMod;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530582}
583
584uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData)
585{
586 return calculateChecksum(fruAreaData.begin(), fruAreaData.end());
587}
588
589// Update new fru area length &
590// Update checksum at new checksum location
591// Return the offset of the area checksum byte
592unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData,
593 size_t fruAreaStart,
594 size_t fruAreaEndOfFieldsOffset,
595 size_t fruAreaEndOffset)
596{
597 size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart;
598
599 // fill zeros for any remaining unused space
600 std::fill(fruData.begin() + fruAreaEndOfFieldsOffset,
601 fruData.begin() + fruAreaEndOffset, 0);
602
603 size_t mod = traverseFRUAreaIndex % fruBlockSize;
Ed Tanous3013fb42022-07-09 08:27:06 -0700604 size_t checksumLoc = 0;
605 if (mod == 0U)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530606 {
607 traverseFRUAreaIndex += (fruBlockSize);
608 checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1);
609 }
610 else
611 {
612 traverseFRUAreaIndex += (fruBlockSize - mod);
613 checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1);
614 }
615
Ed Tanous3013fb42022-07-09 08:27:06 -0700616 size_t newFRUAreaLen =
617 (traverseFRUAreaIndex / fruBlockSize) +
618 static_cast<unsigned long>((traverseFRUAreaIndex % fruBlockSize) != 0);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530619 size_t fruAreaLengthLoc = fruAreaStart + 1;
620 fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen);
621
622 // Calculate new checksum
623 std::vector<uint8_t> finalFRUData;
624 std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart,
625 std::back_inserter(finalFRUData));
626
627 fruData[checksumLoc] = calculateChecksum(finalFRUData);
628 return checksumLoc;
629}
630
631ssize_t getFieldLength(uint8_t fruFieldTypeLenValue)
632{
633 constexpr uint8_t typeLenMask = 0x3F;
634 constexpr uint8_t endOfFields = 0xC1;
635 if (fruFieldTypeLenValue == endOfFields)
636 {
637 return -1;
638 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800639 return fruFieldTypeLenValue & typeLenMask;
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530640}
641
642bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData)
643{
644 // ipmi spec format version number is currently at 1, verify it
645 if (blockData[0] != fruVersion)
646 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800647 if (debug)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530648 {
649 std::cerr << "FRU spec version " << (int)(blockData[0])
650 << " not supported. Supported version is "
651 << (int)(fruVersion) << "\n";
652 }
653 return false;
654 }
655
656 // verify pad is set to 0
657 if (blockData[6] != 0x0)
658 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800659 if (debug)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530660 {
661 std::cerr << "PAD value in header is non zero, value is "
662 << (int)(blockData[6]) << "\n";
663 }
664 return false;
665 }
666
667 // verify offsets are 0, or don't point to another offset
668 std::set<uint8_t> foundOffsets;
669 for (int ii = 1; ii < 6; ii++)
670 {
671 if (blockData[ii] == 0)
672 {
673 continue;
674 }
675 auto inserted = foundOffsets.insert(blockData[ii]);
676 if (!inserted.second)
677 {
678 return false;
679 }
680 }
681
682 // validate checksum
683 size_t sum = 0;
684 for (int jj = 0; jj < 7; jj++)
685 {
686 sum += blockData[jj];
687 }
688 sum = (256 - sum) & 0xFF;
689
690 if (sum != blockData[7])
691 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800692 if (debug)
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530693 {
694 std::cerr << "Checksum " << (int)(blockData[7])
695 << " is invalid. calculated checksum is " << (int)(sum)
696 << "\n";
697 }
698 return false;
699 }
700 return true;
701}
702
Zev Weiss309c0b12022-02-25 01:44:12 +0000703bool findFRUHeader(FRUReader& reader, const std::string& errorHelp,
Oskar Senftbd4075f2021-10-05 23:42:43 -0400704 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData,
Zev Weiss1525e852022-03-22 22:27:43 +0000705 off_t& baseOffset)
Oskar Senftbd4075f2021-10-05 23:42:43 -0400706{
Zev Weiss309c0b12022-02-25 01:44:12 +0000707 if (reader.read(baseOffset, 0x8, blockData.data()) < 0)
Oskar Senftbd4075f2021-10-05 23:42:43 -0400708 {
709 std::cerr << "failed to read " << errorHelp << " base offset "
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030710 << baseOffset << "\n";
Oskar Senftbd4075f2021-10-05 23:42:43 -0400711 return false;
712 }
713
714 // check the header checksum
715 if (validateHeader(blockData))
716 {
717 return true;
718 }
719
720 // only continue the search if we just looked at 0x0.
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030721 if (baseOffset != 0)
722 {
Oskar Senftbd4075f2021-10-05 23:42:43 -0400723 return false;
724 }
725
726 // now check for special cases where the IPMI data is at an offset
727
728 // check if blockData starts with tyanHeader
729 const std::vector<uint8_t> tyanHeader = {'$', 'T', 'Y', 'A', 'N', '$'};
730 if (blockData.size() >= tyanHeader.size() &&
731 std::equal(tyanHeader.begin(), tyanHeader.end(), blockData.begin()))
732 {
733 // look for the FRU header at offset 0x6000
734 baseOffset = 0x6000;
Zev Weiss309c0b12022-02-25 01:44:12 +0000735 return findFRUHeader(reader, errorHelp, blockData, baseOffset);
Oskar Senftbd4075f2021-10-05 23:42:43 -0400736 }
737
738 if (debug)
739 {
740 std::cerr << "Illegal header " << errorHelp << " base offset "
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030741 << baseOffset << "\n";
Oskar Senftbd4075f2021-10-05 23:42:43 -0400742 }
743
744 return false;
745}
746
Marvin Drees2b3ed302023-04-14 16:35:14 +0200747std::pair<std::vector<uint8_t>, bool>
748 readFRUContents(FRUReader& reader, const std::string& errorHelp)
Patrick Ventureab296412020-12-30 13:39:37 -0800749{
Ed Tanous3013fb42022-07-09 08:27:06 -0700750 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
Zev Weiss1525e852022-03-22 22:27:43 +0000751 off_t baseOffset = 0x0;
Patrick Ventureab296412020-12-30 13:39:37 -0800752
Zev Weiss309c0b12022-02-25 01:44:12 +0000753 if (!findFRUHeader(reader, errorHelp, blockData, baseOffset))
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030754 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200755 return {{}, false};
Patrick Ventureab296412020-12-30 13:39:37 -0800756 }
757
758 std::vector<uint8_t> device;
759 device.insert(device.end(), blockData.begin(), blockData.begin() + 8);
760
761 bool hasMultiRecords = false;
762 size_t fruLength = fruBlockSize; // At least FRU header is present
Vijay Khemka7792e392021-01-25 13:03:56 -0800763 unsigned int prevOffset = 0;
Patrick Ventureab296412020-12-30 13:39:37 -0800764 for (fruAreas area = fruAreas::fruAreaInternal;
765 area <= fruAreas::fruAreaMultirecord; ++area)
766 {
767 // Offset value can be 255.
768 unsigned int areaOffset = device[getHeaderAreaFieldOffset(area)];
769 if (areaOffset == 0)
770 {
771 continue;
772 }
773
Vijay Khemka7792e392021-01-25 13:03:56 -0800774 /* Check for offset order, as per Section 17 of FRU specification, FRU
775 * information areas are required to be in order in FRU data layout
776 * which means all offset value should be in increasing order or can be
777 * 0 if that area is not present
778 */
779 if (areaOffset <= prevOffset)
780 {
781 std::cerr << "Fru area offsets are not in required order as per "
782 "Section 17 of Fru specification\n";
Marvin Drees2b3ed302023-04-14 16:35:14 +0200783 return {{}, true};
Vijay Khemka7792e392021-01-25 13:03:56 -0800784 }
785 prevOffset = areaOffset;
786
Patrick Ventureab296412020-12-30 13:39:37 -0800787 // MultiRecords are different. area is not tracking section, it's
788 // walking the common header.
789 if (area == fruAreas::fruAreaMultirecord)
790 {
791 hasMultiRecords = true;
792 break;
793 }
794
795 areaOffset *= fruBlockSize;
796
Zev Weiss309c0b12022-02-25 01:44:12 +0000797 if (reader.read(baseOffset + areaOffset, 0x2, blockData.data()) < 0)
Patrick Ventureab296412020-12-30 13:39:37 -0800798 {
Oskar Senftbd4075f2021-10-05 23:42:43 -0400799 std::cerr << "failed to read " << errorHelp << " base offset "
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030800 << baseOffset << "\n";
Marvin Drees2b3ed302023-04-14 16:35:14 +0200801 return {{}, true};
Patrick Ventureab296412020-12-30 13:39:37 -0800802 }
803
804 // Ignore data type (blockData is already unsigned).
805 size_t length = blockData[1] * fruBlockSize;
806 areaOffset += length;
807 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
808 }
809
810 if (hasMultiRecords)
811 {
812 // device[area count] is the index to the last area because the 0th
813 // entry is not an offset in the common header.
814 unsigned int areaOffset =
815 device[getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord)];
816 areaOffset *= fruBlockSize;
817
818 // the multi-area record header is 5 bytes long.
819 constexpr size_t multiRecordHeaderSize = 5;
820 constexpr uint8_t multiRecordEndOfListMask = 0x80;
821
822 // Sanity hard-limit to 64KB.
823 while (areaOffset < std::numeric_limits<uint16_t>::max())
824 {
825 // In multi-area, the area offset points to the 0th record, each
826 // record has 3 bytes of the header we care about.
Zev Weiss309c0b12022-02-25 01:44:12 +0000827 if (reader.read(baseOffset + areaOffset, 0x3, blockData.data()) < 0)
Patrick Ventureab296412020-12-30 13:39:37 -0800828 {
Oskar Senftbd4075f2021-10-05 23:42:43 -0400829 std::cerr << "failed to read " << errorHelp << " base offset "
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030830 << baseOffset << "\n";
Marvin Drees2b3ed302023-04-14 16:35:14 +0200831 return {{}, true};
Patrick Ventureab296412020-12-30 13:39:37 -0800832 }
833
834 // Ok, let's check the record length, which is in bytes (unsigned,
835 // up to 255, so blockData should hold uint8_t not char)
836 size_t recordLength = blockData[2];
837 areaOffset += (recordLength + multiRecordHeaderSize);
838 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
839
840 // If this is the end of the list bail.
Ed Tanous3013fb42022-07-09 08:27:06 -0700841 if ((blockData[1] & multiRecordEndOfListMask) != 0)
Patrick Ventureab296412020-12-30 13:39:37 -0800842 {
843 break;
844 }
845 }
846 }
847
848 // You already copied these first 8 bytes (the ipmi fru header size)
849 fruLength -= std::min(fruBlockSize, fruLength);
850
851 int readOffset = fruBlockSize;
852
853 while (fruLength > 0)
854 {
855 size_t requestLength =
856 std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength);
857
Zev Weiss309c0b12022-02-25 01:44:12 +0000858 if (reader.read(baseOffset + readOffset, requestLength,
859 blockData.data()) < 0)
Patrick Ventureab296412020-12-30 13:39:37 -0800860 {
Oskar Senftbd4075f2021-10-05 23:42:43 -0400861 std::cerr << "failed to read " << errorHelp << " base offset "
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030862 << baseOffset << "\n";
Marvin Drees2b3ed302023-04-14 16:35:14 +0200863 return {{}, true};
Patrick Ventureab296412020-12-30 13:39:37 -0800864 }
865
866 device.insert(device.end(), blockData.begin(),
867 blockData.begin() + requestLength);
868
869 readOffset += requestLength;
870 fruLength -= std::min(requestLength, fruLength);
871 }
872
Marvin Drees2b3ed302023-04-14 16:35:14 +0200873 return {device, true};
Patrick Ventureab296412020-12-30 13:39:37 -0800874}
875
876unsigned int getHeaderAreaFieldOffset(fruAreas area)
877{
878 return static_cast<unsigned int>(area) + 1;
879}
Kumar Thangavel7135f3d2022-02-04 12:14:24 +0530880
krishnar4213ee212022-11-11 15:39:30 +0530881std::vector<uint8_t>& getFRUInfo(const uint16_t& bus, const uint8_t& address)
Kumar Thangavel7135f3d2022-02-04 12:14:24 +0530882{
883 auto deviceMap = busMap.find(bus);
884 if (deviceMap == busMap.end())
885 {
886 throw std::invalid_argument("Invalid Bus.");
887 }
888 auto device = deviceMap->second->find(address);
889 if (device == deviceMap->second->end())
890 {
891 throw std::invalid_argument("Invalid Address.");
892 }
893 std::vector<uint8_t>& ret = device->second;
894
895 return ret;
896}
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +0530897
898// Iterate FruArea Names and find start and size of the fru area that contains
899// the propertyName and the field start location for the property. fruAreaParams
900// struct values fruAreaStart, fruAreaSize, fruAreaEnd, fieldLoc values gets
901// updated/returned if successful.
902
903bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData,
904 const std::string& propertyName,
Kumar Thangavel51b557b2022-09-13 13:40:47 +0530905 struct FruArea& fruAreaParams)
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +0530906{
907 const std::vector<std::string>* fruAreaFieldNames = nullptr;
908
909 uint8_t fruAreaOffsetFieldValue = 0;
910 size_t offset = 0;
911 std::string areaName = propertyName.substr(0, propertyName.find('_'));
912 std::string propertyNamePrefix = areaName + "_";
913 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
914 if (it == fruAreaNames.end())
915 {
916 std::cerr << "Can't parse area name for property " << propertyName
917 << " \n";
918 return false;
919 }
920 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
921 fruAreaOffsetFieldValue =
922 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
923 switch (fruAreaToUpdate)
924 {
925 case fruAreas::fruAreaChassis:
926 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
927 fruAreaFieldNames = &chassisFruAreas;
928 break;
929 case fruAreas::fruAreaBoard:
930 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
931 fruAreaFieldNames = &boardFruAreas;
932 break;
933 case fruAreas::fruAreaProduct:
934 // Manufacturer name offset. Skip fixed first 3 product fru bytes
935 // i.e. version, area length and language code
936 offset = 3;
937 fruAreaFieldNames = &productFruAreas;
938 break;
939 default:
940 std::cerr << "Invalid PropertyName " << propertyName << " \n";
941 return false;
942 }
943 if (fruAreaOffsetFieldValue == 0)
944 {
945 std::cerr << "FRU Area for " << propertyName << " not present \n";
946 return false;
947 }
948
949 fruAreaParams.start = fruAreaOffsetFieldValue * fruBlockSize;
950 fruAreaParams.size = fruData[fruAreaParams.start + 1] * fruBlockSize;
951 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Kumar Thangavel51b557b2022-09-13 13:40:47 +0530952 size_t fruDataIter = fruAreaParams.start + offset;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +0530953 size_t skipToFRUUpdateField = 0;
954 ssize_t fieldLength = 0;
955
956 bool found = false;
957 for (const auto& field : *fruAreaFieldNames)
958 {
959 skipToFRUUpdateField++;
960 if (propertyName == propertyNamePrefix + field)
961 {
962 found = true;
963 break;
964 }
965 }
966 if (!found)
967 {
968 std::size_t pos = propertyName.find(fruCustomFieldName);
969 if (pos == std::string::npos)
970 {
971 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
972 << propertyName << "\n";
973 return false;
974 }
975 std::string fieldNumStr =
976 propertyName.substr(pos + fruCustomFieldName.length());
977 size_t fieldNum = std::stoi(fieldNumStr);
978 if (fieldNum == 0)
979 {
980 std::cerr << "PropertyName not recognized: " << propertyName
981 << "\n";
982 return false;
983 }
984 skipToFRUUpdateField += fieldNum;
985 }
986
987 for (size_t i = 1; i < skipToFRUUpdateField; i++)
988 {
989 if (fruDataIter < fruData.size())
990 {
991 fieldLength = getFieldLength(fruData[fruDataIter]);
992
993 if (fieldLength < 0)
994 {
995 break;
996 }
997 fruDataIter += 1 + fieldLength;
998 }
999 }
1000 fruAreaParams.updateFieldLoc = fruDataIter;
1001
1002 return true;
1003}
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301004
1005// Copy the FRU Area fields and properties into restFRUAreaFieldsData vector.
1006// Return true for success and false for failure.
1007
1008bool copyRestFRUArea(std::vector<uint8_t>& fruData,
1009 const std::string& propertyName,
1010 struct FruArea& fruAreaParams,
1011 std::vector<uint8_t>& restFRUAreaFieldsData)
1012{
1013 size_t fieldLoc = fruAreaParams.updateFieldLoc;
1014 size_t start = fruAreaParams.start;
1015 size_t fruAreaSize = fruAreaParams.size;
1016
1017 // Push post update fru field bytes to a vector
1018 ssize_t fieldLength = getFieldLength(fruData[fieldLoc]);
1019 if (fieldLength < 0)
1020 {
1021 std::cerr << "Property " << propertyName << " not present \n";
1022 return false;
1023 }
1024
1025 size_t fruDataIter = 0;
1026 fruDataIter = fieldLoc;
1027 fruDataIter += 1 + fieldLength;
1028 size_t restFRUFieldsLoc = fruDataIter;
1029 size_t endOfFieldsLoc = 0;
1030
1031 if (fruDataIter < fruData.size())
1032 {
1033 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1034 {
1035 if (fruDataIter >= (start + fruAreaSize))
1036 {
1037 fruDataIter = start + fruAreaSize;
1038 break;
1039 }
1040 fruDataIter += 1 + fieldLength;
1041 }
1042 endOfFieldsLoc = fruDataIter;
1043 }
1044
1045 std::copy_n(fruData.begin() + restFRUFieldsLoc,
1046 endOfFieldsLoc - restFRUFieldsLoc + 1,
1047 std::back_inserter(restFRUAreaFieldsData));
1048
1049 fruAreaParams.restFieldsLoc = restFRUFieldsLoc;
1050 fruAreaParams.restFieldsEnd = endOfFieldsLoc;
1051
1052 return true;
1053}
Kumar Thangaveld79d0252022-08-24 14:26:01 +05301054
1055// Get all device dbus path and match path with product name using
1056// regular expression and find the device index for all devices.
1057
1058std::optional<int> findIndexForFRU(
1059 boost::container::flat_map<
1060 std::pair<size_t, size_t>,
1061 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
1062 std::string& productName)
1063{
Kumar Thangaveld79d0252022-08-24 14:26:01 +05301064 int highest = -1;
1065 bool found = false;
1066
Patrick Williamsdf190612023-05-10 07:51:34 -05001067 for (const auto& busIface : dbusInterfaceMap)
Kumar Thangaveld79d0252022-08-24 14:26:01 +05301068 {
1069 std::string path = busIface.second->get_object_path();
1070 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
1071 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +05301072 // Check if the match named has extra information.
1073 found = true;
1074 std::smatch baseMatch;
1075
1076 bool match = std::regex_match(path, baseMatch,
1077 std::regex(productName + "_(\\d+)$"));
1078 if (match)
1079 {
1080 if (baseMatch.size() == 2)
1081 {
1082 std::ssub_match baseSubMatch = baseMatch[1];
1083 std::string base = baseSubMatch.str();
1084
1085 int value = std::stoi(base);
1086 highest = (value > highest) ? value : highest;
1087 }
1088 }
1089 }
1090 } // end searching objects
1091
1092 if (!found)
1093 {
1094 return std::nullopt;
1095 }
1096 return highest;
1097}
Kumar Thangavel9f2162a2022-08-10 18:05:20 +05301098
1099// This function does format fru data as per IPMI format and find the
1100// productName in the formatted fru data, get that productName and return
1101// productName if found or return NULL.
1102
1103std::optional<std::string> getProductName(
1104 std::vector<uint8_t>& device,
1105 boost::container::flat_map<std::string, std::string>& formattedFRU,
1106 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount)
1107{
1108 std::string productName;
1109
1110 resCodes res = formatIPMIFRU(device, formattedFRU);
1111 if (res == resCodes::resErr)
1112 {
1113 std::cerr << "failed to parse FRU for device at bus " << bus
1114 << " address " << address << "\n";
1115 return std::nullopt;
1116 }
1117 if (res == resCodes::resWarn)
1118 {
1119 std::cerr << "Warnings while parsing FRU for device at bus " << bus
1120 << " address " << address << "\n";
1121 }
1122
1123 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
1124 // Not found under Board section or an empty string.
1125 if (productNameFind == formattedFRU.end() ||
1126 productNameFind->second.empty())
1127 {
1128 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
1129 }
1130 // Found under Product section and not an empty string.
1131 if (productNameFind != formattedFRU.end() &&
1132 !productNameFind->second.empty())
1133 {
1134 productName = productNameFind->second;
1135 std::regex illegalObject("[^A-Za-z0-9_]");
1136 productName = std::regex_replace(productName, illegalObject, "_");
1137 }
1138 else
1139 {
1140 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
1141 unknownBusObjectCount++;
1142 }
1143 return productName;
1144}
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301145
1146bool getFruData(std::vector<uint8_t>& fruData, uint32_t bus, uint32_t address)
1147{
1148 try
1149 {
1150 fruData = getFRUInfo(static_cast<uint16_t>(bus),
1151 static_cast<uint8_t>(address));
1152 }
1153 catch (const std::invalid_argument& e)
1154 {
1155 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
1156 return false;
1157 }
1158
1159 return !fruData.empty();
1160}