blob: b7257455a72cbdf54e8a4a30c9fa53244cbbc253 [file] [log] [blame]
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001#include "config.h"
2
3#include "worker.hpp"
4
5#include "backup_restore.hpp"
6#include "configuration.hpp"
7#include "constants.hpp"
8#include "event_logger.hpp"
9#include "exceptions.hpp"
10#include "logger.hpp"
11#include "parser.hpp"
12#include "parser_factory.hpp"
13#include "parser_interface.hpp"
14
15#include <utility/dbus_utility.hpp>
16#include <utility/json_utility.hpp>
17#include <utility/vpd_specific_utility.hpp>
18
19#include <filesystem>
20#include <fstream>
21#include <future>
22#include <typeindex>
23#include <unordered_set>
24
25namespace vpd
26{
27
Sunny Srivastava765cf7b2025-02-04 05:24:11 -060028Worker::Worker(std::string pathToConfigJson, uint8_t i_maxThreadCount) :
29 m_configJsonPath(pathToConfigJson), m_semaphore(i_maxThreadCount)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050030{
31 // Implies the processing is based on some config JSON
32 if (!m_configJsonPath.empty())
33 {
34 // Check if symlink is already there to confirm fresh boot/factory
35 // reset.
36 if (std::filesystem::exists(INVENTORY_JSON_SYM_LINK))
37 {
38 logging::logMessage("Sym Link already present");
39 m_configJsonPath = INVENTORY_JSON_SYM_LINK;
40 m_isSymlinkPresent = true;
41 }
42
43 try
44 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -050045 uint16_t l_errCode = 0;
46 m_parsedJson =
47 jsonUtility::getParsedJson(m_configJsonPath, l_errCode);
48
49 if (l_errCode)
50 {
51 throw std::runtime_error(
52 "JSON parsing failed for file [ " + m_configJsonPath +
53 " ], error : " +
54 vpdSpecificUtility::getErrCodeMsg(l_errCode));
55 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050056
57 // check for mandatory fields at this point itself.
58 if (!m_parsedJson.contains("frus"))
59 {
60 throw std::runtime_error("Mandatory tag(s) missing from JSON");
61 }
62 }
63 catch (const std::exception& ex)
64 {
65 throw(JsonException(ex.what(), m_configJsonPath));
66 }
67 }
68 else
69 {
70 logging::logMessage("Processing in not based on any config JSON");
71 }
72}
73
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050074static std::string readFitConfigValue()
75{
76 std::vector<std::string> output =
77 commonUtility::executeCmd("/sbin/fw_printenv");
78 std::string fitConfigValue;
79
80 for (const auto& entry : output)
81 {
82 auto pos = entry.find("=");
83 auto key = entry.substr(0, pos);
84 if (key != "fitconfig")
85 {
86 continue;
87 }
88
89 if (pos + 1 < entry.size())
90 {
91 fitConfigValue = entry.substr(pos + 1);
92 }
93 }
94
95 return fitConfigValue;
96}
97
98bool Worker::isSystemVPDOnDBus() const
99{
100 const std::string& mboardPath =
101 m_parsedJson["frus"][SYSTEM_VPD_FILE_PATH].at(0).value(
102 "inventoryPath", "");
103
104 if (mboardPath.empty())
105 {
106 throw JsonException("System vpd file path missing in JSON",
107 INVENTORY_JSON_SYM_LINK);
108 }
109
110 std::array<const char*, 1> interfaces = {
111 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
112
113 const types::MapperGetObject& objectMap =
114 dbusUtility::getObjectMap(mboardPath, interfaces);
115
116 if (objectMap.empty())
117 {
118 return false;
119 }
120 return true;
121}
122
123std::string Worker::getIMValue(const types::IPZVpdMap& parsedVpd) const
124{
125 if (parsedVpd.empty())
126 {
127 throw std::runtime_error("Empty VPD map. Can't Extract IM value");
128 }
129
130 const auto& itrToVSBP = parsedVpd.find("VSBP");
131 if (itrToVSBP == parsedVpd.end())
132 {
133 throw DataException("VSBP record missing.");
134 }
135
136 const auto& itrToIM = (itrToVSBP->second).find("IM");
137 if (itrToIM == (itrToVSBP->second).end())
138 {
139 throw DataException("IM keyword missing.");
140 }
141
142 types::BinaryVector imVal;
143 std::copy(itrToIM->second.begin(), itrToIM->second.end(),
144 back_inserter(imVal));
145
146 std::ostringstream imData;
147 for (auto& aByte : imVal)
148 {
149 imData << std::setw(2) << std::setfill('0') << std::hex
150 << static_cast<int>(aByte);
151 }
152
153 return imData.str();
154}
155
156std::string Worker::getHWVersion(const types::IPZVpdMap& parsedVpd) const
157{
158 if (parsedVpd.empty())
159 {
160 throw std::runtime_error("Empty VPD map. Can't Extract HW value");
161 }
162
163 const auto& itrToVINI = parsedVpd.find("VINI");
164 if (itrToVINI == parsedVpd.end())
165 {
166 throw DataException("VINI record missing.");
167 }
168
169 const auto& itrToHW = (itrToVINI->second).find("HW");
170 if (itrToHW == (itrToVINI->second).end())
171 {
172 throw DataException("HW keyword missing.");
173 }
174
175 types::BinaryVector hwVal;
176 std::copy(itrToHW->second.begin(), itrToHW->second.end(),
177 back_inserter(hwVal));
178
179 // The planar pass only comes from the LSB of the HW keyword,
180 // where as the MSB is used for other purposes such as signifying clock
181 // termination.
182 hwVal[0] = 0x00;
183
184 std::ostringstream hwString;
185 for (auto& aByte : hwVal)
186 {
187 hwString << std::setw(2) << std::setfill('0') << std::hex
188 << static_cast<int>(aByte);
189 }
190
191 return hwString.str();
192}
193
194void Worker::fillVPDMap(const std::string& vpdFilePath,
195 types::VPDMapVariant& vpdMap)
196{
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500197 if (vpdFilePath.empty())
198 {
199 throw std::runtime_error("Invalid file path passed to fillVPDMap API.");
200 }
201
202 if (!std::filesystem::exists(vpdFilePath))
203 {
204 throw std::runtime_error("Can't Find physical file");
205 }
206
Sunny Srivastava043955d2025-01-21 18:04:49 +0530207 std::shared_ptr<Parser> vpdParser =
208 std::make_shared<Parser>(vpdFilePath, m_parsedJson);
209 vpdMap = vpdParser->parse();
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500210}
211
212void Worker::getSystemJson(std::string& systemJson,
213 const types::VPDMapVariant& parsedVpdMap)
214{
215 if (auto pVal = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
216 {
217 std::string hwKWdValue = getHWVersion(*pVal);
218 if (hwKWdValue.empty())
219 {
220 throw DataException("HW value fetched is empty.");
221 }
222
223 const std::string& imKwdValue = getIMValue(*pVal);
224 if (imKwdValue.empty())
225 {
226 throw DataException("IM value fetched is empty.");
227 }
228
229 auto itrToIM = config::systemType.find(imKwdValue);
230 if (itrToIM == config::systemType.end())
231 {
232 throw DataException("IM keyword does not map to any system type");
233 }
234
235 const types::HWVerList hwVersionList = itrToIM->second.second;
236 if (!hwVersionList.empty())
237 {
238 transform(hwKWdValue.begin(), hwKWdValue.end(), hwKWdValue.begin(),
239 ::toupper);
240
241 auto itrToHW =
242 std::find_if(hwVersionList.begin(), hwVersionList.end(),
243 [&hwKWdValue](const auto& aPair) {
244 return aPair.first == hwKWdValue;
245 });
246
247 if (itrToHW != hwVersionList.end())
248 {
249 if (!(*itrToHW).second.empty())
250 {
251 systemJson += (*itrToIM).first + "_" + (*itrToHW).second +
252 ".json";
253 }
254 else
255 {
256 systemJson += (*itrToIM).first + ".json";
257 }
258 return;
259 }
260 }
261 systemJson += itrToIM->second.first + ".json";
262 return;
263 }
264
Sunny Srivastava043955d2025-01-21 18:04:49 +0530265 throw DataException(
266 "Invalid VPD type returned from Parser. Can't get system JSON.");
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500267}
268
269static void setEnvAndReboot(const std::string& key, const std::string& value)
270{
271 // set env and reboot and break.
272 commonUtility::executeCmd("/sbin/fw_setenv", key, value);
273 logging::logMessage("Rebooting BMC to pick up new device tree");
274
275 // make dbus call to reboot
276 auto bus = sdbusplus::bus::new_default_system();
277 auto method = bus.new_method_call(
278 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
279 "org.freedesktop.systemd1.Manager", "Reboot");
280 bus.call_noreply(method);
281}
282
283void Worker::setJsonSymbolicLink(const std::string& i_systemJson)
284{
285 std::error_code l_ec;
286 l_ec.clear();
Sunny Srivastavaadff7882025-03-13 11:41:05 +0530287
288 // Check if symlink file path exists and if the JSON at this location is a
289 // symlink.
290 if (m_isSymlinkPresent &&
291 std::filesystem::is_symlink(INVENTORY_JSON_SYM_LINK, l_ec))
292 { // Don't care about exception in "is_symlink". Will continue with creation
293 // of symlink.
294
295 const auto& l_symlinkFilePth =
296 std::filesystem::read_symlink(INVENTORY_JSON_SYM_LINK, l_ec);
297
298 if (l_ec)
299 {
300 logging::logMessage(
301 "Can't read existing symlink. Error =" + l_ec.message() +
302 "Trying removal of symlink and creation of new symlink.");
303 }
304
305 // If currently set JSON is the required one. No further processing
306 // required.
307 if (i_systemJson == l_symlinkFilePth)
308 {
309 // Correct symlink already set.
310 return;
311 }
312
313 if (!std::filesystem::remove(INVENTORY_JSON_SYM_LINK, l_ec))
314 {
315 // No point going further. If removal fails for existing symlink,
316 // create will anyways throw.
317 throw std::runtime_error(
318 "Removal of symlink failed with Error = " + l_ec.message() +
319 ". Can't proceed with create_symlink.");
320 }
321 }
322
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500323 if (!std::filesystem::exists(VPD_SYMLIMK_PATH, l_ec))
324 {
325 if (l_ec)
326 {
327 throw std::runtime_error(
328 "File system call to exist failed with error = " +
329 l_ec.message());
330 }
331
332 // implies it is a fresh boot/factory reset.
333 // Create the directory for hosting the symlink
334 if (!std::filesystem::create_directories(VPD_SYMLIMK_PATH, l_ec))
335 {
336 if (l_ec)
337 {
338 throw std::runtime_error(
339 "File system call to create directory failed with error = " +
340 l_ec.message());
341 }
342 }
343 }
344
345 // create a new symlink based on the system
346 std::filesystem::create_symlink(i_systemJson, INVENTORY_JSON_SYM_LINK,
347 l_ec);
348
349 if (l_ec)
350 {
351 throw std::runtime_error(
352 "create_symlink system call failed with error: " + l_ec.message());
353 }
354
355 // If the flow is at this point implies the symlink was not present there.
356 // Considering this as factory reset.
357 m_isFactoryResetDone = true;
358}
359
360void Worker::setDeviceTreeAndJson()
361{
Anupama B R4c65fcd2025-09-01 08:09:00 -0500362 setCollectionStatusProperty(SYSTEM_VPD_FILE_PATH,
363 constants::vpdCollectionInProgress);
364
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500365 // JSON is madatory for processing of this API.
366 if (m_parsedJson.empty())
367 {
Sunny Srivastava043955d2025-01-21 18:04:49 +0530368 throw JsonException("System config JSON is empty", m_configJsonPath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500369 }
370
371 types::VPDMapVariant parsedVpdMap;
372 fillVPDMap(SYSTEM_VPD_FILE_PATH, parsedVpdMap);
373
374 // Implies it is default JSON.
375 std::string systemJson{JSON_ABSOLUTE_PATH_PREFIX};
376
377 // ToDo: Need to check if INVENTORY_JSON_SYM_LINK pointing to correct system
378 // This is required to support movement from rainier to Blue Ridge on the
379 // fly.
380
Sunny Srivastavaadff7882025-03-13 11:41:05 +0530381 getSystemJson(systemJson, parsedVpdMap);
382
383 if (!systemJson.compare(JSON_ABSOLUTE_PATH_PREFIX))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500384 {
Sunny Srivastava043955d2025-01-21 18:04:49 +0530385 throw DataException(
386 "No system JSON found corresponding to IM read from VPD.");
Sunny Srivastavaadff7882025-03-13 11:41:05 +0530387 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500388
Rekha Aparnaca9a0862025-08-29 04:08:33 -0500389 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500390
Rekha Aparnaca9a0862025-08-29 04:08:33 -0500391 // re-parse the JSON once appropriate JSON has been selected.
392 m_parsedJson = jsonUtility::getParsedJson(systemJson, l_errCode);
393
394 if (l_errCode)
Sunny Srivastavaadff7882025-03-13 11:41:05 +0530395 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -0500396 throw(JsonException(
397 "JSON parsing failed for file [ " + systemJson +
398 " ], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode),
399 systemJson));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500400 }
401
402 std::string devTreeFromJson;
403 if (m_parsedJson.contains("devTree"))
404 {
405 devTreeFromJson = m_parsedJson["devTree"];
406
407 if (devTreeFromJson.empty())
408 {
Sunny Srivastava043955d2025-01-21 18:04:49 +0530409 EventLogger::createSyncPel(
410 types::ErrorType::JsonFailure, types::SeverityType::Error,
411 __FILE__, __FUNCTION__, 0,
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500412 "Mandatory value for device tree missing from JSON[" +
Sunny Srivastava043955d2025-01-21 18:04:49 +0530413 systemJson + "]",
414 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500415 }
416 }
417
418 auto fitConfigVal = readFitConfigValue();
419
420 if (devTreeFromJson.empty() ||
421 fitConfigVal.find(devTreeFromJson) != std::string::npos)
422 { // Skipping setting device tree as either devtree info is missing from
423 // Json or it is rightly set.
424
Sunny Srivastavaadff7882025-03-13 11:41:05 +0530425 setJsonSymbolicLink(systemJson);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500426
427 if (isSystemVPDOnDBus() &&
428 jsonUtility::isBackupAndRestoreRequired(m_parsedJson))
429 {
430 performBackupAndRestore(parsedVpdMap);
431 }
432
433 // proceed to publish system VPD.
434 publishSystemVPD(parsedVpdMap);
Anupama B R4c65fcd2025-09-01 08:09:00 -0500435 setCollectionStatusProperty(SYSTEM_VPD_FILE_PATH,
436 constants::vpdCollectionCompleted);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500437 return;
438 }
439
440 setEnvAndReboot("fitconfig", devTreeFromJson);
441 exit(EXIT_SUCCESS);
442}
443
444void Worker::populateIPZVPDpropertyMap(
445 types::InterfaceMap& interfacePropMap,
446 const types::IPZKwdValueMap& keyordValueMap,
447 const std::string& interfaceName)
448{
449 types::PropertyMap propertyValueMap;
450 for (const auto& kwdVal : keyordValueMap)
451 {
452 auto kwd = kwdVal.first;
453
454 if (kwd[0] == '#')
455 {
456 kwd = std::string("PD_") + kwd[1];
457 }
458 else if (isdigit(kwd[0]))
459 {
460 kwd = std::string("N_") + kwd;
461 }
462
463 types::BinaryVector value(kwdVal.second.begin(), kwdVal.second.end());
464 propertyValueMap.emplace(move(kwd), move(value));
465 }
466
467 if (!propertyValueMap.empty())
468 {
469 interfacePropMap.emplace(interfaceName, propertyValueMap);
470 }
471}
472
473void Worker::populateKwdVPDpropertyMap(const types::KeywordVpdMap& keyordVPDMap,
474 types::InterfaceMap& interfaceMap)
475{
476 for (const auto& kwdValMap : keyordVPDMap)
477 {
478 types::PropertyMap propertyValueMap;
479 auto kwd = kwdValMap.first;
480
481 if (kwd[0] == '#')
482 {
483 kwd = std::string("PD_") + kwd[1];
484 }
485 else if (isdigit(kwd[0]))
486 {
487 kwd = std::string("N_") + kwd;
488 }
489
490 if (auto keywordValue = get_if<types::BinaryVector>(&kwdValMap.second))
491 {
492 types::BinaryVector value((*keywordValue).begin(),
493 (*keywordValue).end());
494 propertyValueMap.emplace(move(kwd), move(value));
495 }
496 else if (auto keywordValue = get_if<std::string>(&kwdValMap.second))
497 {
498 types::BinaryVector value((*keywordValue).begin(),
499 (*keywordValue).end());
500 propertyValueMap.emplace(move(kwd), move(value));
501 }
502 else if (auto keywordValue = get_if<size_t>(&kwdValMap.second))
503 {
504 if (kwd == "MemorySizeInKB")
505 {
506 types::PropertyMap memProp;
507 memProp.emplace(move(kwd), ((*keywordValue)));
508 interfaceMap.emplace("xyz.openbmc_project.Inventory.Item.Dimm",
509 move(memProp));
510 continue;
511 }
512 else
513 {
514 logging::logMessage(
515 "Unknown Keyword =" + kwd + " found in keyword VPD map");
516 continue;
517 }
518 }
519 else
520 {
521 logging::logMessage(
522 "Unknown variant type found in keyword VPD map.");
523 continue;
524 }
525
526 if (!propertyValueMap.empty())
527 {
528 vpdSpecificUtility::insertOrMerge(
529 interfaceMap, constants::kwdVpdInf, move(propertyValueMap));
530 }
531 }
532}
533
534void Worker::populateInterfaces(const nlohmann::json& interfaceJson,
535 types::InterfaceMap& interfaceMap,
536 const types::VPDMapVariant& parsedVpdMap)
537{
538 for (const auto& interfacesPropPair : interfaceJson.items())
539 {
540 const std::string& interface = interfacesPropPair.key();
541 types::PropertyMap propertyMap;
542
543 for (const auto& propValuePair : interfacesPropPair.value().items())
544 {
545 const std::string property = propValuePair.key();
546
547 if (propValuePair.value().is_boolean())
548 {
549 propertyMap.emplace(property,
550 propValuePair.value().get<bool>());
551 }
552 else if (propValuePair.value().is_string())
553 {
554 if (property.compare("LocationCode") == 0 &&
555 interface.compare("com.ibm.ipzvpd.Location") == 0)
556 {
557 std::string value =
558 vpdSpecificUtility::getExpandedLocationCode(
559 propValuePair.value().get<std::string>(),
560 parsedVpdMap);
561 propertyMap.emplace(property, value);
562
563 auto l_locCodeProperty = propertyMap;
564 vpdSpecificUtility::insertOrMerge(
565 interfaceMap,
566 std::string(constants::xyzLocationCodeInf),
567 move(l_locCodeProperty));
568 }
569 else
570 {
571 propertyMap.emplace(
572 property, propValuePair.value().get<std::string>());
573 }
574 }
575 else if (propValuePair.value().is_array())
576 {
577 try
578 {
579 propertyMap.emplace(
580 property,
581 propValuePair.value().get<types::BinaryVector>());
582 }
583 catch (const nlohmann::detail::type_error& e)
584 {
585 std::cerr << "Type exception: " << e.what() << "\n";
586 }
587 }
588 else if (propValuePair.value().is_number())
589 {
590 // For now assume the value is a size_t. In the future it would
591 // be nice to come up with a way to get the type from the JSON.
592 propertyMap.emplace(property,
593 propValuePair.value().get<size_t>());
594 }
595 else if (propValuePair.value().is_object())
596 {
597 const std::string& record =
598 propValuePair.value().value("recordName", "");
599 const std::string& keyword =
600 propValuePair.value().value("keywordName", "");
601 const std::string& encoding =
602 propValuePair.value().value("encoding", "");
603
604 if (auto ipzVpdMap =
605 std::get_if<types::IPZVpdMap>(&parsedVpdMap))
606 {
607 if (!record.empty() && !keyword.empty() &&
608 (*ipzVpdMap).count(record) &&
609 (*ipzVpdMap).at(record).count(keyword))
610 {
611 auto encoded = vpdSpecificUtility::encodeKeyword(
612 ((*ipzVpdMap).at(record).at(keyword)), encoding);
613 propertyMap.emplace(property, encoded);
614 }
615 }
616 else if (auto kwdVpdMap =
617 std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
618 {
619 if (!keyword.empty() && (*kwdVpdMap).count(keyword))
620 {
621 if (auto kwValue = std::get_if<types::BinaryVector>(
622 &(*kwdVpdMap).at(keyword)))
623 {
624 auto encodedValue =
625 vpdSpecificUtility::encodeKeyword(
626 std::string((*kwValue).begin(),
627 (*kwValue).end()),
628 encoding);
629
630 propertyMap.emplace(property, encodedValue);
631 }
632 else if (auto kwValue = std::get_if<std::string>(
633 &(*kwdVpdMap).at(keyword)))
634 {
635 auto encodedValue =
636 vpdSpecificUtility::encodeKeyword(
637 std::string((*kwValue).begin(),
638 (*kwValue).end()),
639 encoding);
640
641 propertyMap.emplace(property, encodedValue);
642 }
643 else if (auto uintValue = std::get_if<size_t>(
644 &(*kwdVpdMap).at(keyword)))
645 {
646 propertyMap.emplace(property, *uintValue);
647 }
648 else
649 {
650 logging::logMessage(
651 "Unknown keyword found, Keywrod = " + keyword);
652 }
653 }
654 }
655 }
656 }
657 vpdSpecificUtility::insertOrMerge(interfaceMap, interface,
658 move(propertyMap));
659 }
660}
661
662bool Worker::isCPUIOGoodOnly(const std::string& i_pgKeyword)
663{
664 const unsigned char l_io[] = {
665 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF,
666 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
667
668 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
669 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
670 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
671 // IO.
672 if (memcmp(l_io, i_pgKeyword.data() + constants::INDEX_OF_EQ0_IN_PG,
673 constants::SIZE_OF_8EQ_IN_PG) == 0)
674 {
675 return true;
676 }
677
678 // The CPU is not an IO
679 return false;
680}
681
682bool Worker::primeInventory(const std::string& i_vpdFilePath)
683{
684 if (i_vpdFilePath.empty())
685 {
686 logging::logMessage("Empty VPD file path given");
687 return false;
688 }
689
690 if (m_parsedJson.empty())
691 {
692 logging::logMessage("Empty JSON detected for " + i_vpdFilePath);
693 return false;
694 }
695 else if (!m_parsedJson["frus"].contains(i_vpdFilePath))
696 {
697 logging::logMessage("File " + i_vpdFilePath +
698 ", is not found in the system config JSON file.");
699 return false;
700 }
701
702 types::ObjectMap l_objectInterfaceMap;
703 for (const auto& l_Fru : m_parsedJson["frus"][i_vpdFilePath])
704 {
705 types::InterfaceMap l_interfaces;
706 sdbusplus::message::object_path l_fruObjectPath(l_Fru["inventoryPath"]);
707
708 if (l_Fru.contains("ccin"))
709 {
710 continue;
711 }
712
713 if (l_Fru.contains("noprime") && l_Fru.value("noprime", false))
714 {
715 continue;
716 }
717
Souvik Roy6a9553c2025-02-07 01:16:32 -0600718 // Reset data under PIM for this FRU only if the FRU is not synthesized
719 // and we handle it's Present property.
720 if (isPresentPropertyHandlingRequired(l_Fru))
721 {
722 // Clear data under PIM if already exists.
723 vpdSpecificUtility::resetDataUnderPIM(
724 std::string(l_Fru["inventoryPath"]), l_interfaces);
725 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500726
727 // Add extra interfaces mentioned in the Json config file
728 if (l_Fru.contains("extraInterfaces"))
729 {
730 populateInterfaces(l_Fru["extraInterfaces"], l_interfaces,
731 std::monostate{});
732 }
733
734 types::PropertyMap l_propertyValueMap;
Sunny Srivastavad159bb42025-01-09 11:13:50 +0530735
Souvik Roy6a9553c2025-02-07 01:16:32 -0600736 // Update Present property for this FRU only if we handle Present
737 // property for the FRU.
738 if (isPresentPropertyHandlingRequired(l_Fru))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500739 {
Souvik Roy6a9553c2025-02-07 01:16:32 -0600740 l_propertyValueMap.emplace("Present", false);
741
742 // TODO: Present based on file will be taken care in future.
743 // By default present is set to false for FRU at the time of
744 // priming. Once collection goes through, it will be set to true in
745 // that flow.
746 /*if (std::filesystem::exists(i_vpdFilePath))
747 {
748 l_propertyValueMap["Present"] = true;
749 }*/
750 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500751
752 vpdSpecificUtility::insertOrMerge(l_interfaces,
753 "xyz.openbmc_project.Inventory.Item",
754 move(l_propertyValueMap));
755
756 if (l_Fru.value("inherit", true) &&
757 m_parsedJson.contains("commonInterfaces"))
758 {
759 populateInterfaces(m_parsedJson["commonInterfaces"], l_interfaces,
760 std::monostate{});
761 }
762
763 processFunctionalProperty(l_Fru["inventoryPath"], l_interfaces);
764 processEnabledProperty(l_Fru["inventoryPath"], l_interfaces);
765
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -0600766 // Emplace the default state of FRU VPD collection
767 types::PropertyMap l_fruCollectionProperty = {
Anupama B R5cd1b2d2025-08-05 04:57:40 -0500768 {"Status", constants::vpdCollectionNotStarted}};
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -0600769
770 vpdSpecificUtility::insertOrMerge(l_interfaces,
771 constants::vpdCollectionInterface,
772 std::move(l_fruCollectionProperty));
773
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500774 l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
775 std::move(l_interfaces));
776 }
777
778 // Notify PIM
779 if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
780 {
781 logging::logMessage("Call to PIM failed for VPD file " + i_vpdFilePath);
782 return false;
783 }
784
785 return true;
786}
787
788void Worker::processEmbeddedAndSynthesizedFrus(const nlohmann::json& singleFru,
789 types::InterfaceMap& interfaces)
790{
791 // embedded property(true or false) says whether the subfru is embedded
792 // into the parent fru (or) not. VPD sets Present property only for
793 // embedded frus. If the subfru is not an embedded FRU, the subfru may
794 // or may not be physically present. Those non embedded frus will always
795 // have Present=false irrespective of its physical presence or absence.
796 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
797 // Present to true for such sub frus.
798 // Eg: ethernet port is embedded into bmc card. So set Present to true
799 // for such sub frus. Also donot populate present property for embedded
800 // subfru which is synthesized. Currently there is no subfru which are
801 // both embedded and synthesized. But still the case is handled here.
802
803 // Check if its required to handle presence for this FRU.
804 if (singleFru.value("handlePresence", true))
805 {
806 types::PropertyMap presProp;
807 presProp.emplace("Present", true);
808 vpdSpecificUtility::insertOrMerge(
809 interfaces, "xyz.openbmc_project.Inventory.Item", move(presProp));
810 }
811}
812
813void Worker::processExtraInterfaces(const nlohmann::json& singleFru,
814 types::InterfaceMap& interfaces,
815 const types::VPDMapVariant& parsedVpdMap)
816{
817 populateInterfaces(singleFru["extraInterfaces"], interfaces, parsedVpdMap);
818 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
819 {
820 if (singleFru["extraInterfaces"].contains(
821 "xyz.openbmc_project.Inventory.Item.Cpu"))
822 {
823 auto itrToRec = (*ipzVpdMap).find("CP00");
824 if (itrToRec == (*ipzVpdMap).end())
825 {
826 return;
827 }
828
Souvik Roya55fcca2025-02-19 01:33:58 -0600829 const std::string pgKeywordValue{
830 vpdSpecificUtility::getKwVal(itrToRec->second, "PG")};
831
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500832 if (!pgKeywordValue.empty())
833 {
834 if (isCPUIOGoodOnly(pgKeywordValue))
835 {
836 interfaces["xyz.openbmc_project.Inventory.Item"]
837 ["PrettyName"] = "IO Module";
838 }
839 }
Souvik Roya55fcca2025-02-19 01:33:58 -0600840 else
841 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +0530842 throw DataException(std::string(__FUNCTION__) +
843 "Failed to get value for keyword PG");
Souvik Roya55fcca2025-02-19 01:33:58 -0600844 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500845 }
846 }
847}
848
849void Worker::processCopyRecordFlag(const nlohmann::json& singleFru,
850 const types::VPDMapVariant& parsedVpdMap,
851 types::InterfaceMap& interfaces)
852{
853 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
854 {
855 for (const auto& record : singleFru["copyRecords"])
856 {
857 const std::string& recordName = record;
858 if ((*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
859 {
860 populateIPZVPDpropertyMap(interfaces,
861 (*ipzVpdMap).at(recordName),
862 constants::ipzVpdInf + recordName);
863 }
864 }
865 }
866}
867
868void Worker::processInheritFlag(const types::VPDMapVariant& parsedVpdMap,
869 types::InterfaceMap& interfaces)
870{
871 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
872 {
873 for (const auto& [recordName, kwdValueMap] : *ipzVpdMap)
874 {
875 populateIPZVPDpropertyMap(interfaces, kwdValueMap,
876 constants::ipzVpdInf + recordName);
877 }
878 }
879 else if (auto kwdVpdMap = std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
880 {
881 populateKwdVPDpropertyMap(*kwdVpdMap, interfaces);
882 }
883
884 if (m_parsedJson.contains("commonInterfaces"))
885 {
886 populateInterfaces(m_parsedJson["commonInterfaces"], interfaces,
887 parsedVpdMap);
888 }
889}
890
891bool Worker::processFruWithCCIN(const nlohmann::json& singleFru,
892 const types::VPDMapVariant& parsedVpdMap)
893{
894 if (auto ipzVPDMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
895 {
896 auto itrToRec = (*ipzVPDMap).find("VINI");
897 if (itrToRec == (*ipzVPDMap).end())
898 {
899 return false;
900 }
901
Souvik Roya55fcca2025-02-19 01:33:58 -0600902 std::string ccinFromVpd{
903 vpdSpecificUtility::getKwVal(itrToRec->second, "CC")};
904
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500905 if (ccinFromVpd.empty())
906 {
907 return false;
908 }
909
910 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
911 ::toupper);
912
913 std::vector<std::string> ccinList;
914 for (std::string ccin : singleFru["ccin"])
915 {
916 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
917 ccinList.push_back(ccin);
918 }
919
920 if (ccinList.empty())
921 {
922 return false;
923 }
924
925 if (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
926 ccinList.end())
927 {
928 return false;
929 }
930 }
931 return true;
932}
933
934void Worker::processFunctionalProperty(const std::string& i_inventoryObjPath,
935 types::InterfaceMap& io_interfaces)
936{
937 if (!dbusUtility::isChassisPowerOn())
938 {
939 std::array<const char*, 1> l_operationalStatusInf = {
940 constants::operationalStatusInf};
941
942 auto mapperObjectMap = dbusUtility::getObjectMap(
943 i_inventoryObjPath, l_operationalStatusInf);
944
945 // If the object has been found. Check if it is under PIM.
946 if (mapperObjectMap.size() != 0)
947 {
948 for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap)
949 {
950 if (l_serviceName == constants::pimServiceName)
951 {
952 // The object is already under PIM. No need to process
953 // again. Retain the old value.
954 return;
955 }
956 }
957 }
958
959 // Implies value is not there in D-Bus. Populate it with default
960 // value "true".
961 types::PropertyMap l_functionalProp;
962 l_functionalProp.emplace("Functional", true);
963 vpdSpecificUtility::insertOrMerge(io_interfaces,
964 constants::operationalStatusInf,
965 move(l_functionalProp));
966 }
967
968 // if chassis is power on. Functional property should be there on D-Bus.
969 // Don't process.
970 return;
971}
972
973void Worker::processEnabledProperty(const std::string& i_inventoryObjPath,
974 types::InterfaceMap& io_interfaces)
975{
976 if (!dbusUtility::isChassisPowerOn())
977 {
978 std::array<const char*, 1> l_enableInf = {constants::enableInf};
979
980 auto mapperObjectMap =
981 dbusUtility::getObjectMap(i_inventoryObjPath, l_enableInf);
982
983 // If the object has been found. Check if it is under PIM.
984 if (mapperObjectMap.size() != 0)
985 {
986 for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap)
987 {
988 if (l_serviceName == constants::pimServiceName)
989 {
990 // The object is already under PIM. No need to process
991 // again. Retain the old value.
992 return;
993 }
994 }
995 }
996
997 // Implies value is not there in D-Bus. Populate it with default
998 // value "true".
999 types::PropertyMap l_enabledProp;
1000 l_enabledProp.emplace("Enabled", true);
1001 vpdSpecificUtility::insertOrMerge(io_interfaces, constants::enableInf,
1002 move(l_enabledProp));
1003 }
1004
1005 // if chassis is power on. Enabled property should be there on D-Bus.
1006 // Don't process.
1007 return;
1008}
1009
1010void Worker::populateDbus(const types::VPDMapVariant& parsedVpdMap,
1011 types::ObjectMap& objectInterfaceMap,
1012 const std::string& vpdFilePath)
1013{
1014 if (vpdFilePath.empty())
1015 {
1016 throw std::runtime_error(
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301017 std::string(__FUNCTION__) +
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001018 "Invalid parameter passed to populateDbus API.");
1019 }
1020
1021 // JSON config is mandatory for processing of "if". Add "else" for any
1022 // processing without config JSON.
1023 if (!m_parsedJson.empty())
1024 {
1025 types::InterfaceMap interfaces;
1026
1027 for (const auto& aFru : m_parsedJson["frus"][vpdFilePath])
1028 {
1029 const auto& inventoryPath = aFru["inventoryPath"];
1030 sdbusplus::message::object_path fruObjectPath(inventoryPath);
1031 if (aFru.contains("ccin"))
1032 {
1033 if (!processFruWithCCIN(aFru, parsedVpdMap))
1034 {
1035 continue;
1036 }
1037 }
1038
1039 if (aFru.value("inherit", true))
1040 {
1041 processInheritFlag(parsedVpdMap, interfaces);
1042 }
1043
1044 // If specific record needs to be copied.
1045 if (aFru.contains("copyRecords"))
1046 {
1047 processCopyRecordFlag(aFru, parsedVpdMap, interfaces);
1048 }
1049
1050 if (aFru.contains("extraInterfaces"))
1051 {
1052 // Process extra interfaces w.r.t a FRU.
1053 processExtraInterfaces(aFru, interfaces, parsedVpdMap);
1054 }
1055
1056 // Process FRUS which are embedded in the parent FRU and whose VPD
1057 // will be synthesized.
1058 if ((aFru.value("embedded", true)) &&
1059 (!aFru.value("synthesized", false)))
1060 {
1061 processEmbeddedAndSynthesizedFrus(aFru, interfaces);
1062 }
1063
1064 processFunctionalProperty(inventoryPath, interfaces);
1065 processEnabledProperty(inventoryPath, interfaces);
1066
1067 objectInterfaceMap.emplace(std::move(fruObjectPath),
1068 std::move(interfaces));
1069 }
1070 }
1071}
1072
Patrick Williams43fedab2025-02-03 14:28:05 -05001073std::string Worker::createAssetTagString(
1074 const types::VPDMapVariant& i_parsedVpdMap)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001075{
1076 std::string l_assetTag;
1077
1078 // system VPD will be in IPZ format.
1079 if (auto l_parsedVpdMap = std::get_if<types::IPZVpdMap>(&i_parsedVpdMap))
1080 {
1081 auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS);
1082 if (l_itrToVsys != (*l_parsedVpdMap).end())
1083 {
Souvik Roya55fcca2025-02-19 01:33:58 -06001084 const std::string l_tmKwdValue{vpdSpecificUtility::getKwVal(
1085 l_itrToVsys->second, constants::kwdTM)};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001086
Souvik Roya55fcca2025-02-19 01:33:58 -06001087 if (l_tmKwdValue.empty())
1088 {
1089 throw std::runtime_error(
1090 std::string("Failed to get value for keyword [") +
1091 constants::kwdTM +
1092 std::string("] while creating Asset tag."));
1093 }
1094
1095 const std::string l_seKwdValue{vpdSpecificUtility::getKwVal(
1096 l_itrToVsys->second, constants::kwdSE)};
1097
1098 if (l_seKwdValue.empty())
1099 {
1100 throw std::runtime_error(
1101 std::string("Failed to get value for keyword [") +
1102 constants::kwdSE +
1103 std::string("] while creating Asset tag."));
1104 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001105
1106 l_assetTag = std::string{"Server-"} + l_tmKwdValue +
1107 std::string{"-"} + l_seKwdValue;
1108 }
1109 else
1110 {
1111 throw std::runtime_error(
1112 "VSYS record not found in parsed VPD map to create Asset tag.");
1113 }
1114 }
1115 else
1116 {
1117 throw std::runtime_error(
1118 "Invalid VPD type recieved to create Asset tag.");
1119 }
1120
1121 return l_assetTag;
1122}
1123
1124void Worker::publishSystemVPD(const types::VPDMapVariant& parsedVpdMap)
1125{
1126 types::ObjectMap objectInterfaceMap;
1127
1128 if (std::get_if<types::IPZVpdMap>(&parsedVpdMap))
1129 {
1130 populateDbus(parsedVpdMap, objectInterfaceMap, SYSTEM_VPD_FILE_PATH);
1131
1132 try
1133 {
1134 if (m_isFactoryResetDone)
1135 {
1136 const auto& l_assetTag = createAssetTagString(parsedVpdMap);
1137
1138 auto l_itrToSystemPath = objectInterfaceMap.find(
1139 sdbusplus::message::object_path(constants::systemInvPath));
1140 if (l_itrToSystemPath == objectInterfaceMap.end())
1141 {
1142 throw std::runtime_error(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301143 "Asset tag update failed. System Path not found in object map.");
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001144 }
1145
1146 types::PropertyMap l_assetTagProperty;
1147 l_assetTagProperty.emplace("AssetTag", l_assetTag);
1148
1149 (l_itrToSystemPath->second)
1150 .emplace(constants::assetTagInf,
1151 std::move(l_assetTagProperty));
1152 }
1153 }
1154 catch (const std::exception& l_ex)
1155 {
1156 EventLogger::createSyncPel(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301157 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
1158 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001159 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
1160 }
1161
1162 // Notify PIM
1163 if (!dbusUtility::callPIM(move(objectInterfaceMap)))
1164 {
1165 throw std::runtime_error("Call to PIM failed for system VPD");
1166 }
1167 }
1168 else
1169 {
1170 throw DataException("Invalid format of parsed VPD map.");
1171 }
1172}
1173
1174bool Worker::processPreAction(const std::string& i_vpdFilePath,
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001175 const std::string& i_flagToProcess,
1176 uint16_t& i_errCode)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001177{
1178 if (i_vpdFilePath.empty() || i_flagToProcess.empty())
1179 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001180 i_errCode = error_code::INVALID_INPUT_PARAMETER;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001181 return false;
1182 }
1183
1184 if ((!jsonUtility::executeBaseAction(m_parsedJson, "preAction",
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001185 i_vpdFilePath, i_flagToProcess,
1186 i_errCode)) &&
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001187 (i_flagToProcess.compare("collection") == constants::STR_CMP_SUCCESS))
1188 {
1189 // TODO: Need a way to delete inventory object from Dbus and persisted
1190 // data section in case any FRU is not present or there is any
1191 // problem in collecting it. Once it has been deleted, it can be
1192 // re-created in the flow of priming the inventory. This needs to be
1193 // done either here or in the exception section of "parseAndPublishVPD"
1194 // API. Any failure in the process of collecting FRU will land up in the
1195 // excpetion of "parseAndPublishVPD".
1196
1197 // If the FRU is not there, clear the VINI/CCIN data.
1198 // Enity manager probes for this keyword to look for this
1199 // FRU, now if the data is persistent on BMC and FRU is
1200 // removed this can lead to ambiguity. Hence clearing this
1201 // Keyword if FRU is absent.
1202 const auto& inventoryPath =
1203 m_parsedJson["frus"][i_vpdFilePath].at(0).value("inventoryPath",
1204 "");
1205
1206 if (!inventoryPath.empty())
1207 {
1208 types::ObjectMap l_pimObjMap{
1209 {inventoryPath,
1210 {{constants::kwdVpdInf,
1211 {{constants::kwdCCIN, types::BinaryVector{}}}}}}};
1212
1213 if (!dbusUtility::callPIM(std::move(l_pimObjMap)))
1214 {
1215 logging::logMessage(
1216 "Call to PIM failed for file " + i_vpdFilePath);
1217 }
1218 }
1219 else
1220 {
1221 logging::logMessage(
1222 "Inventory path is empty in Json for file " + i_vpdFilePath);
1223 }
1224
1225 return false;
1226 }
1227 return true;
1228}
1229
1230bool Worker::processPostAction(
1231 const std::string& i_vpdFruPath, const std::string& i_flagToProcess,
1232 const std::optional<types::VPDMapVariant> i_parsedVpd)
1233{
1234 if (i_vpdFruPath.empty() || i_flagToProcess.empty())
1235 {
1236 logging::logMessage(
1237 "Invalid input parameter. Abort processing post action");
1238 return false;
1239 }
1240
1241 // Check if post action tag is to be triggered in the flow of collection
1242 // based on some CCIN value?
1243 if (m_parsedJson["frus"][i_vpdFruPath]
1244 .at(0)["postAction"][i_flagToProcess]
1245 .contains("ccin"))
1246 {
1247 if (!i_parsedVpd.has_value())
1248 {
1249 logging::logMessage("Empty VPD Map");
1250 return false;
1251 }
1252
1253 // CCIN match is required to process post action for this FRU as it
1254 // contains the flag.
1255 if (!vpdSpecificUtility::findCcinInVpd(
1256 m_parsedJson["frus"][i_vpdFruPath].at(
1257 0)["postAction"]["collection"],
1258 i_parsedVpd.value()))
1259 {
1260 // If CCIN is not found, implies post action processing is not
1261 // required for this FRU. Let the flow continue.
1262 return true;
1263 }
1264 }
1265
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001266 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001267 if (!jsonUtility::executeBaseAction(m_parsedJson, "postAction",
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001268 i_vpdFruPath, i_flagToProcess,
1269 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001270 {
1271 logging::logMessage(
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001272 "Execution of post action failed for path: " + i_vpdFruPath +
1273 " . Reason: " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001274
1275 // If post action was required and failed only in that case return
1276 // false. In all other case post action is considered passed.
1277 return false;
1278 }
1279
1280 return true;
1281}
1282
1283types::VPDMapVariant Worker::parseVpdFile(const std::string& i_vpdFilePath)
1284{
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001285 try
1286 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301287 if (i_vpdFilePath.empty())
1288 {
1289 throw std::runtime_error(
1290 std::string(__FUNCTION__) +
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301291 " Empty VPD file path passed. Abort processing");
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301292 }
1293
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301294 bool isPreActionRequired = false;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001295 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
1296 "preAction", "collection"))
1297 {
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301298 isPreActionRequired = true;
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001299 uint16_t l_errCode = 0;
1300 if (!processPreAction(i_vpdFilePath, "collection", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001301 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001302 if (l_errCode == error_code::DEVICE_NOT_PRESENT)
1303 {
1304 logging::logMessage(
1305 vpdSpecificUtility::getErrCodeMsg(l_errCode) +
1306 i_vpdFilePath);
1307 // Presence pin has been read successfully and has been read
1308 // as false, so this is not a failure case, hence returning
1309 // empty variant so that pre action is not marked as failed.
1310 return types::VPDMapVariant{};
1311 }
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301312 throw std::runtime_error(
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001313 std::string(__FUNCTION__) +
1314 " Pre-Action failed with error: " +
1315 vpdSpecificUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001316 }
1317 }
1318
1319 if (!std::filesystem::exists(i_vpdFilePath))
1320 {
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301321 if (isPreActionRequired)
1322 {
1323 throw std::runtime_error(
1324 std::string(__FUNCTION__) + " Could not find file path " +
1325 i_vpdFilePath + "Skipping parser trigger for the EEPROM");
1326 }
1327 return types::VPDMapVariant{};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001328 }
1329
1330 std::shared_ptr<Parser> vpdParser =
1331 std::make_shared<Parser>(i_vpdFilePath, m_parsedJson);
1332
1333 types::VPDMapVariant l_parsedVpd = vpdParser->parse();
1334
1335 // Before returning, as collection is over, check if FRU qualifies for
1336 // any post action in the flow of collection.
1337 // Note: Don't change the order, post action needs to be processed only
1338 // after collection for FRU is successfully done.
1339 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
1340 "postAction", "collection"))
1341 {
1342 if (!processPostAction(i_vpdFilePath, "collection", l_parsedVpd))
1343 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301344 // Post action was required but failed while executing.
1345 // Behaviour can be undefined.
1346 EventLogger::createSyncPel(
1347 types::ErrorType::InternalFailure,
1348 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
1349 std::string("Required post action failed for path [" +
1350 i_vpdFilePath + "]"),
1351 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001352 }
1353 }
1354
1355 return l_parsedVpd;
1356 }
1357 catch (std::exception& l_ex)
1358 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001359 uint16_t l_errCode = 0;
Souvik Roy37c6bef2025-07-17 00:55:59 -05001360 std::string l_exMsg{
1361 std::string(__FUNCTION__) + " : VPD parsing failed for " +
1362 i_vpdFilePath + " due to error: " + l_ex.what()};
1363
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001364 // If post fail action is required, execute it.
1365 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
Sunny Srivastava4c164382025-01-28 03:17:33 -06001366 "postFailAction", "collection"))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001367 {
1368 if (!jsonUtility::executePostFailAction(m_parsedJson, i_vpdFilePath,
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001369 "collection", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001370 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001371 l_exMsg += ". Post fail action also failed. Error : " +
1372 vpdSpecificUtility::getErrCodeMsg(l_errCode) +
1373 " Aborting collection for this FRU.";
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001374 }
1375 }
1376
Souvik Roy37c6bef2025-07-17 00:55:59 -05001377 if (typeid(l_ex) == typeid(DataException))
1378 {
1379 throw DataException(l_exMsg);
1380 }
1381 else if (typeid(l_ex) == typeid(EccException))
1382 {
1383 throw EccException(l_exMsg);
1384 }
1385 throw std::runtime_error(l_exMsg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001386 }
1387}
1388
Patrick Williams43fedab2025-02-03 14:28:05 -05001389std::tuple<bool, std::string> Worker::parseAndPublishVPD(
1390 const std::string& i_vpdFilePath)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001391{
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001392 std::string l_inventoryPath{};
1393
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001394 try
1395 {
1396 m_semaphore.acquire();
1397
1398 // Thread launched.
1399 m_mutex.lock();
1400 m_activeCollectionThreadCount++;
1401 m_mutex.unlock();
1402
Anupama B R4c65fcd2025-09-01 08:09:00 -05001403 setCollectionStatusProperty(i_vpdFilePath,
1404 constants::vpdCollectionInProgress);
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001405
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001406 const types::VPDMapVariant& parsedVpdMap = parseVpdFile(i_vpdFilePath);
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301407 if (!std::holds_alternative<std::monostate>(parsedVpdMap))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001408 {
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301409 types::ObjectMap objectInterfaceMap;
1410 populateDbus(parsedVpdMap, objectInterfaceMap, i_vpdFilePath);
1411
1412 // Notify PIM
1413 if (!dbusUtility::callPIM(move(objectInterfaceMap)))
1414 {
1415 throw std::runtime_error(
1416 std::string(__FUNCTION__) +
1417 "Call to PIM failed while publishing VPD.");
1418 }
1419 }
1420 else
1421 {
1422 logging::logMessage("Empty parsedVpdMap recieved for path [" +
1423 i_vpdFilePath + "]. Check PEL for reason.");
Anupama B R4c65fcd2025-09-01 08:09:00 -05001424
1425 // As empty parsedVpdMap recieved for some reason, but still
1426 // considered VPD collection is completed. Hence FRU collection
1427 // Status will be set as completed.
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001428 }
1429 }
1430 catch (const std::exception& ex)
1431 {
Anupama B R24691d22025-05-21 08:14:15 -05001432 setCollectionStatusProperty(i_vpdFilePath,
Anupama B R5cd1b2d2025-08-05 04:57:40 -05001433 constants::vpdCollectionFailed);
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001434
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001435 // handle all the exceptions internally. Return only true/false
1436 // based on status of execution.
1437 if (typeid(ex) == std::type_index(typeid(DataException)))
1438 {
Rekha Aparna017567a2025-08-13 02:07:06 -05001439 uint16_t l_errCode = 0;
Sunny Srivastava78c91072025-02-05 14:09:50 +05301440 // In case of pass1 planar, VPD can be corrupted on PCIe cards. Skip
1441 // logging error for these cases.
1442 if (vpdSpecificUtility::isPass1Planar())
1443 {
Rekha Aparna017567a2025-08-13 02:07:06 -05001444 std::string l_invPath =
1445 jsonUtility::getInventoryObjPathFromJson(
1446 m_parsedJson, i_vpdFilePath, l_errCode);
1447
1448 if (l_errCode != 0)
1449 {
1450 logging::logMessage(
1451 "Failed to get inventory object path from JSON for FRU [" +
1452 i_vpdFilePath + "], error: " +
1453 vpdSpecificUtility::getErrCodeMsg(l_errCode));
1454 }
1455
RekhaAparna011ef21002025-02-18 23:47:36 -06001456 const std::string& l_invPathLeafValue =
Rekha Aparna017567a2025-08-13 02:07:06 -05001457 sdbusplus::message::object_path(l_invPath).filename();
Sunny Srivastava78c91072025-02-05 14:09:50 +05301458
RekhaAparna011ef21002025-02-18 23:47:36 -06001459 if ((l_invPathLeafValue.find("pcie_card", 0) !=
1460 std::string::npos))
Sunny Srivastava78c91072025-02-05 14:09:50 +05301461 {
1462 // skip logging any PEL for PCIe cards on pass 1 planar.
1463 return std::make_tuple(false, i_vpdFilePath);
1464 }
1465 }
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301466 }
Sunny Srivastava78c91072025-02-05 14:09:50 +05301467
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301468 EventLogger::createSyncPel(
Souvik Roy37c6bef2025-07-17 00:55:59 -05001469 EventLogger::getErrorType(ex),
1470 (typeid(ex) == typeid(DataException)) ||
1471 (typeid(ex) == typeid(EccException))
1472 ? types::SeverityType::Warning
1473 : types::SeverityType::Informational,
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301474 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(ex),
1475 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001476
1477 // TODO: Figure out a way to clear data in case of any failure at
1478 // runtime.
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301479
1480 // set present property to false for any error case. In future this will
1481 // be replaced by presence logic.
Souvik Roy6a9553c2025-02-07 01:16:32 -06001482 // Update Present property for this FRU only if we handle Present
1483 // property for the FRU.
1484 if (isPresentPropertyHandlingRequired(
1485 m_parsedJson["frus"][i_vpdFilePath].at(0)))
1486 {
1487 setPresentProperty(i_vpdFilePath, false);
1488 }
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301489
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001490 m_semaphore.release();
1491 return std::make_tuple(false, i_vpdFilePath);
1492 }
Anupama B R4c65fcd2025-09-01 08:09:00 -05001493
1494 setCollectionStatusProperty(i_vpdFilePath,
1495 constants::vpdCollectionCompleted);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001496 m_semaphore.release();
1497 return std::make_tuple(true, i_vpdFilePath);
1498}
1499
Sunny Srivastava61611752025-02-04 00:29:33 -06001500bool Worker::skipPathForCollection(const std::string& i_vpdFilePath)
1501{
1502 if (i_vpdFilePath.empty())
1503 {
1504 return true;
1505 }
1506
1507 // skip processing of system VPD again as it has been already collected.
1508 if (i_vpdFilePath == SYSTEM_VPD_FILE_PATH)
1509 {
1510 return true;
1511 }
1512
1513 if (dbusUtility::isChassisPowerOn())
1514 {
1515 // If chassis is powered on, skip collecting FRUs which are
1516 // powerOffOnly.
1517 if (jsonUtility::isFruPowerOffOnly(m_parsedJson, i_vpdFilePath))
1518 {
1519 return true;
1520 }
1521
Rekha Aparna017567a2025-08-13 02:07:06 -05001522 uint16_t l_errCode = 0;
1523 std::string l_invPath = jsonUtility::getInventoryObjPathFromJson(
1524 m_parsedJson, i_vpdFilePath, l_errCode);
1525
1526 if (l_errCode)
1527 {
1528 logging::logMessage(
1529 "Failed to get inventory path from JSON for FRU [" +
1530 i_vpdFilePath +
1531 "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
1532
1533 return false;
1534 }
1535
Sunny Srivastava61611752025-02-04 00:29:33 -06001536 const std::string& l_invPathLeafValue =
Rekha Aparna017567a2025-08-13 02:07:06 -05001537 sdbusplus::message::object_path(l_invPath).filename();
Sunny Srivastava61611752025-02-04 00:29:33 -06001538
1539 if ((l_invPathLeafValue.find("pcie_card", 0) != std::string::npos))
1540 {
1541 return true;
1542 }
1543 }
1544
1545 return false;
1546}
1547
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001548void Worker::collectFrusFromJson()
1549{
1550 // A parsed JSON file should be present to pick FRUs EEPROM paths
1551 if (m_parsedJson.empty())
1552 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301553 throw JsonException(
1554 std::string(__FUNCTION__) +
1555 ": Config JSON is mandatory for processing of FRUs through this API.",
1556 m_configJsonPath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001557 }
1558
1559 const nlohmann::json& listOfFrus =
1560 m_parsedJson["frus"].get_ref<const nlohmann::json::object_t&>();
1561
1562 for (const auto& itemFRUS : listOfFrus.items())
1563 {
1564 const std::string& vpdFilePath = itemFRUS.key();
1565
Sunny Srivastava61611752025-02-04 00:29:33 -06001566 if (skipPathForCollection(vpdFilePath))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001567 {
1568 continue;
1569 }
1570
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001571 try
1572 {
1573 std::thread{[vpdFilePath, this]() {
1574 const auto& l_parseResult = parseAndPublishVPD(vpdFilePath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001575
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001576 m_mutex.lock();
1577 m_activeCollectionThreadCount--;
1578 m_mutex.unlock();
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001579
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001580 if (!m_activeCollectionThreadCount)
1581 {
1582 m_isAllFruCollected = true;
1583 }
1584 }}.detach();
1585 }
1586 catch (const std::exception& l_ex)
1587 {
1588 // add vpdFilePath(EEPROM path) to failed list
1589 m_failedEepromPaths.push_front(vpdFilePath);
1590 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001591 }
1592}
1593
1594// ToDo: Move the API under IBM_SYSTEM
1595void Worker::performBackupAndRestore(types::VPDMapVariant& io_srcVpdMap)
1596{
1597 try
1598 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001599 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001600 std::string l_backupAndRestoreCfgFilePath =
1601 m_parsedJson.value("backupRestoreConfigPath", "");
1602
1603 nlohmann::json l_backupAndRestoreCfgJsonObj =
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001604 jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath,
1605 l_errCode);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001606
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001607 if (l_errCode)
RekhaAparna011ef21002025-02-18 23:47:36 -06001608 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001609 throw JsonException(
1610 "JSON parsing failed for file [ " +
1611 l_backupAndRestoreCfgFilePath + " ], error : " +
1612 vpdSpecificUtility::getErrCodeMsg(l_errCode),
1613 l_backupAndRestoreCfgFilePath);
RekhaAparna011ef21002025-02-18 23:47:36 -06001614 }
1615
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001616 // check if either of "source" or "destination" has inventory path.
1617 // this indicates that this sytem has System VPD on hardware
1618 // and other copy on D-Bus (BMC cache).
1619 if (!l_backupAndRestoreCfgJsonObj.empty() &&
1620 ((l_backupAndRestoreCfgJsonObj.contains("source") &&
1621 l_backupAndRestoreCfgJsonObj["source"].contains(
1622 "inventoryPath")) ||
1623 (l_backupAndRestoreCfgJsonObj.contains("destination") &&
1624 l_backupAndRestoreCfgJsonObj["destination"].contains(
1625 "inventoryPath"))))
1626 {
1627 BackupAndRestore l_backupAndRestoreObj(m_parsedJson);
1628 auto [l_srcVpdVariant,
1629 l_dstVpdVariant] = l_backupAndRestoreObj.backupAndRestore();
1630
1631 // ToDo: Revisit is this check is required or not.
1632 if (auto l_srcVpdMap =
1633 std::get_if<types::IPZVpdMap>(&l_srcVpdVariant);
1634 l_srcVpdMap && !(*l_srcVpdMap).empty())
1635 {
1636 io_srcVpdMap = std::move(l_srcVpdVariant);
1637 }
1638 }
1639 }
1640 catch (const std::exception& l_ex)
1641 {
1642 EventLogger::createSyncPel(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301643 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
Sunny Srivastava15a189a2025-02-26 16:53:19 +05301644 __FILE__, __FUNCTION__, 0,
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001645 std::string(
1646 "Exception caught while backup and restore VPD keyword's.") +
Sunny Srivastava15a189a2025-02-26 16:53:19 +05301647 EventLogger::getErrorMsg(l_ex),
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001648 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
1649 }
1650}
1651
1652void Worker::deleteFruVpd(const std::string& i_dbusObjPath)
1653{
1654 if (i_dbusObjPath.empty())
1655 {
1656 throw std::runtime_error("Given DBus object path is empty.");
1657 }
1658
Rekha Aparna0578dd22025-09-02 08:20:21 -05001659 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001660 const std::string& l_fruPath =
Rekha Aparna0578dd22025-09-02 08:20:21 -05001661 jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath, l_errCode);
1662
1663 if (l_errCode)
1664 {
1665 logging::logMessage(
1666 "Failed to get FRU path for inventory path [" + i_dbusObjPath +
1667 "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode) +
1668 " Aborting FRU VPD deletion.");
1669 return;
1670 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001671
1672 try
1673 {
1674 auto l_presentPropValue = dbusUtility::readDbusProperty(
1675 constants::pimServiceName, i_dbusObjPath,
1676 constants::inventoryItemInf, "Present");
1677
1678 if (auto l_value = std::get_if<bool>(&l_presentPropValue))
1679 {
Rekha Aparnaa1187a52025-09-01 12:42:19 -05001680 uint16_t l_errCode = 0;
Souvik Roye9120152025-07-02 08:24:38 -05001681 // check if FRU's Present property is handled by vpd-manager
1682 const auto& l_isFruPresenceHandled =
Rekha Aparnaa1187a52025-09-01 12:42:19 -05001683 jsonUtility::isFruPresenceHandled(m_parsedJson, l_fruPath,
1684 l_errCode);
1685
1686 if (l_errCode)
1687 {
1688 throw std::runtime_error(
1689 "Failed to check if FRU's presence is handled, reason: " +
1690 vpdSpecificUtility::getErrCodeMsg(l_errCode));
1691 }
Souvik Roye9120152025-07-02 08:24:38 -05001692
1693 if (!(*l_value) && l_isFruPresenceHandled)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001694 {
1695 throw std::runtime_error("Given FRU is not present");
1696 }
Souvik Roye9120152025-07-02 08:24:38 -05001697 else if (*l_value && !l_isFruPresenceHandled)
1698 {
1699 throw std::runtime_error(
1700 "Given FRU is present and its presence is not handled by vpd-manager.");
1701 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001702 else
1703 {
1704 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
1705 "preAction", "deletion"))
1706 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001707 uint16_t l_errCode = 0;
1708 if (!processPreAction(l_fruPath, "deletion", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001709 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001710 std::string l_msg = "Pre action failed";
1711 if (l_errCode)
1712 {
1713 l_msg +=
1714 " Reason: " +
1715 vpdSpecificUtility::getErrCodeMsg(l_errCode);
1716 }
1717 throw std::runtime_error(l_msg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001718 }
1719 }
1720
1721 std::vector<std::string> l_interfaceList{
1722 constants::operationalStatusInf};
1723
1724 types::MapperGetSubTree l_subTreeMap =
1725 dbusUtility::getObjectSubTree(i_dbusObjPath, 0,
1726 l_interfaceList);
1727
1728 types::ObjectMap l_objectMap;
1729
1730 // Updates VPD specific interfaces property value under PIM for
1731 // sub FRUs.
1732 for (const auto& [l_objectPath, l_serviceInterfaceMap] :
1733 l_subTreeMap)
1734 {
1735 types::InterfaceMap l_interfaceMap;
1736 vpdSpecificUtility::resetDataUnderPIM(l_objectPath,
1737 l_interfaceMap);
1738 l_objectMap.emplace(l_objectPath,
1739 std::move(l_interfaceMap));
1740 }
1741
1742 types::InterfaceMap l_interfaceMap;
1743 vpdSpecificUtility::resetDataUnderPIM(i_dbusObjPath,
1744 l_interfaceMap);
1745
1746 l_objectMap.emplace(i_dbusObjPath, std::move(l_interfaceMap));
1747
1748 if (!dbusUtility::callPIM(std::move(l_objectMap)))
1749 {
1750 throw std::runtime_error("Call to PIM failed.");
1751 }
1752
1753 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
1754 "postAction", "deletion"))
1755 {
1756 if (!processPostAction(l_fruPath, "deletion"))
1757 {
1758 throw std::runtime_error("Post action failed");
1759 }
1760 }
1761 }
1762 }
1763 else
1764 {
1765 logging::logMessage(
1766 "Can't process delete VPD for FRU [" + i_dbusObjPath +
1767 "] as unable to read present property");
1768 return;
1769 }
1770
1771 logging::logMessage(
1772 "Successfully completed deletion of FRU VPD for " + i_dbusObjPath);
1773 }
1774 catch (const std::exception& l_ex)
1775 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001776 uint16_t l_errCode = 0;
1777 std::string l_errMsg =
1778 "Failed to delete VPD for FRU : " + i_dbusObjPath +
1779 " error: " + std::string(l_ex.what());
1780
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001781 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
1782 "postFailAction", "deletion"))
1783 {
1784 if (!jsonUtility::executePostFailAction(m_parsedJson, l_fruPath,
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001785 "deletion", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001786 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001787 l_errMsg += ". Post fail action also failed, error : " +
1788 vpdSpecificUtility::getErrCodeMsg(l_errCode);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001789 }
1790 }
1791
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001792 logging::logMessage(l_errMsg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001793 }
1794}
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301795
1796void Worker::setPresentProperty(const std::string& i_vpdPath,
1797 const bool& i_value)
1798{
1799 try
1800 {
1801 if (i_vpdPath.empty())
1802 {
1803 throw std::runtime_error(
1804 "Path is empty. Can't set present property");
1805 }
1806
1807 types::ObjectMap l_objectInterfaceMap;
1808
1809 // If the given path is EEPROM path.
1810 if (m_parsedJson["frus"].contains(i_vpdPath))
1811 {
1812 for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath])
1813 {
1814 sdbusplus::message::object_path l_fruObjectPath(
1815 l_Fru["inventoryPath"]);
1816
1817 types::PropertyMap l_propertyValueMap;
1818 l_propertyValueMap.emplace("Present", i_value);
1819
1820 types::InterfaceMap l_interfaces;
1821 vpdSpecificUtility::insertOrMerge(l_interfaces,
1822 constants::inventoryItemInf,
1823 move(l_propertyValueMap));
1824
1825 l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
1826 std::move(l_interfaces));
1827 }
1828 }
1829 else
1830 {
1831 // consider it as an inventory path.
1832 if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0)
1833 {
1834 throw std::runtime_error(
1835 "Invalid inventory path: " + i_vpdPath);
1836 }
1837
1838 types::PropertyMap l_propertyValueMap;
1839 l_propertyValueMap.emplace("Present", i_value);
1840
1841 types::InterfaceMap l_interfaces;
1842 vpdSpecificUtility::insertOrMerge(l_interfaces,
1843 constants::inventoryItemInf,
1844 move(l_propertyValueMap));
1845
1846 l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces));
1847 }
1848
1849 // Notify PIM
1850 if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
1851 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301852 throw DbusException(
1853 std::string(__FUNCTION__) +
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301854 "Call to PIM failed while setting present property for path " +
1855 i_vpdPath);
1856 }
1857 }
1858 catch (const std::exception& l_ex)
1859 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301860 EventLogger::createSyncPel(
1861 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
1862 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
1863 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301864 }
1865}
1866
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301867void Worker::performVpdRecollection()
1868{
1869 try
1870 {
1871 // Check if system config JSON is present
1872 if (m_parsedJson.empty())
1873 {
1874 throw std::runtime_error(
1875 "System config json object is empty, can't process recollection.");
1876 }
1877
1878 const auto& l_frusReplaceableAtStandby =
1879 jsonUtility::getListOfFrusReplaceableAtStandby(m_parsedJson);
1880
1881 for (const auto& l_fruInventoryPath : l_frusReplaceableAtStandby)
1882 {
1883 // ToDo: Add some logic/trace to know the flow to
1884 // collectSingleFruVpd has been directed via
1885 // performVpdRecollection.
1886 collectSingleFruVpd(l_fruInventoryPath);
1887 }
1888 return;
1889 }
1890
1891 catch (const std::exception& l_ex)
1892 {
1893 // TODO Log PEL
1894 logging::logMessage(
1895 "VPD recollection failed with error: " + std::string(l_ex.what()));
1896 }
1897}
1898
1899void Worker::collectSingleFruVpd(
1900 const sdbusplus::message::object_path& i_dbusObjPath)
1901{
Anupama B R48f297b2025-08-13 04:29:06 -05001902 std::string l_fruPath{};
Rekha Aparna0578dd22025-09-02 08:20:21 -05001903 uint16_t l_errCode = 0;
1904
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301905 try
1906 {
1907 // Check if system config JSON is present
1908 if (m_parsedJson.empty())
1909 {
1910 logging::logMessage(
1911 "System config JSON object not present. Single FRU VPD collection is not performed for " +
1912 std::string(i_dbusObjPath));
1913 return;
1914 }
1915
1916 // Get FRU path for the given D-bus object path from JSON
Rekha Aparna0578dd22025-09-02 08:20:21 -05001917 l_fruPath = jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath,
1918 l_errCode);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301919
1920 if (l_fruPath.empty())
1921 {
Rekha Aparna0578dd22025-09-02 08:20:21 -05001922 if (l_errCode)
1923 {
1924 logging::logMessage(
1925 "Failed to get FRU path for [" +
1926 std::string(i_dbusObjPath) + "], error : " +
1927 vpdSpecificUtility::getErrCodeMsg(l_errCode) +
1928 " Aborting single FRU VPD collection.");
1929 return;
1930 }
1931
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301932 logging::logMessage(
1933 "D-bus object path not present in JSON. Single FRU VPD collection is not performed for " +
1934 std::string(i_dbusObjPath));
1935 return;
1936 }
1937
1938 // Check if host is up and running
1939 if (dbusUtility::isHostRunning())
1940 {
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001941 uint16_t l_errCode = 0;
1942 bool isFruReplaceableAtRuntime =
1943 jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath,
1944 l_errCode);
1945
1946 if (l_errCode)
1947 {
1948 logging::logMessage(
1949 "Failed to check if FRU is replaceable at runtime for FRU : [" +
1950 std::string(i_dbusObjPath) + "], error : " +
1951 vpdSpecificUtility::getErrCodeMsg(l_errCode));
1952 return;
1953 }
1954
1955 if (!isFruReplaceableAtRuntime)
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301956 {
1957 logging::logMessage(
1958 "Given FRU is not replaceable at host runtime. Single FRU VPD collection is not performed for " +
1959 std::string(i_dbusObjPath));
1960 return;
1961 }
1962 }
1963 else if (dbusUtility::isBMCReady())
1964 {
Rekha Aparna40845612025-09-01 19:58:56 -05001965 uint16_t l_errCode = 0;
1966 bool isFruReplaceableAtStandby =
1967 jsonUtility::isFruReplaceableAtStandby(m_parsedJson, l_fruPath,
1968 l_errCode);
1969
1970 if (l_errCode)
1971 {
1972 logging::logMessage(
1973 "Error while checking if FRU is replaceable at standby for FRU [" +
1974 std::string(i_dbusObjPath) + "], error : " +
1975 vpdSpecificUtility::getErrCodeMsg(l_errCode));
1976 }
1977
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001978 l_errCode = 0;
1979 bool isFruReplaceableAtRuntime =
1980 jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath,
1981 l_errCode);
1982
1983 if (l_errCode)
1984 {
1985 logging::logMessage(
1986 "Failed to check if FRU is replaceable at runtime for FRU : [" +
1987 std::string(i_dbusObjPath) + "], error : " +
1988 vpdSpecificUtility::getErrCodeMsg(l_errCode));
1989 return;
1990 }
1991
1992 if (!isFruReplaceableAtStandby && (!isFruReplaceableAtRuntime))
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301993 {
1994 logging::logMessage(
1995 "Given FRU is neither replaceable at standby nor replaceable at runtime. Single FRU VPD collection is not performed for " +
1996 std::string(i_dbusObjPath));
1997 return;
1998 }
1999 }
2000
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002001 // Set collection Status as InProgress. Since it's an intermediate state
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302002 // D-bus set-property call is good enough to update the status.
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002003 const std::string& l_collStatusProp = "Status";
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302004
Anupama B R4c65fcd2025-09-01 08:09:00 -05002005 setCollectionStatusProperty(l_fruPath,
2006 constants::vpdCollectionInProgress);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302007
2008 // Parse VPD
2009 types::VPDMapVariant l_parsedVpd = parseVpdFile(l_fruPath);
2010
2011 // If l_parsedVpd is pointing to std::monostate
2012 if (l_parsedVpd.index() == 0)
2013 {
2014 throw std::runtime_error(
2015 "VPD parsing failed for " + std::string(i_dbusObjPath));
2016 }
2017
2018 // Get D-bus object map from worker class
2019 types::ObjectMap l_dbusObjectMap;
2020 populateDbus(l_parsedVpd, l_dbusObjectMap, l_fruPath);
2021
2022 if (l_dbusObjectMap.empty())
2023 {
2024 throw std::runtime_error(
2025 "Failed to create D-bus object map. Single FRU VPD collection failed for " +
2026 std::string(i_dbusObjPath));
2027 }
2028
2029 // Call PIM's Notify method
2030 if (!dbusUtility::callPIM(move(l_dbusObjectMap)))
2031 {
2032 throw std::runtime_error(
2033 "Notify PIM failed. Single FRU VPD collection failed for " +
2034 std::string(i_dbusObjPath));
2035 }
Anupama B R4c65fcd2025-09-01 08:09:00 -05002036 setCollectionStatusProperty(l_fruPath,
2037 constants::vpdCollectionCompleted);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302038 }
2039 catch (const std::exception& l_error)
2040 {
Anupama B R48f297b2025-08-13 04:29:06 -05002041 setCollectionStatusProperty(l_fruPath, constants::vpdCollectionFailed);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302042 // TODO: Log PEL
2043 logging::logMessage(std::string(l_error.what()));
2044 }
2045}
Anupama B R24691d22025-05-21 08:14:15 -05002046
2047void Worker::setCollectionStatusProperty(
2048 const std::string& i_vpdPath, const std::string& i_value) const noexcept
2049{
2050 try
2051 {
2052 if (i_vpdPath.empty())
2053 {
2054 throw std::runtime_error(
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002055 "Given path is empty. Can't set collection Status property");
Anupama B R24691d22025-05-21 08:14:15 -05002056 }
2057
Anupama B R4c65fcd2025-09-01 08:09:00 -05002058 types::PropertyMap l_timeStampMap;
2059 if (i_value == constants::vpdCollectionCompleted ||
2060 i_value == constants::vpdCollectionFailed)
2061 {
2062 l_timeStampMap.emplace(
2063 "CompletedTime",
2064 types::DbusVariantType{
2065 commonUtility::getCurrentTimeSinceEpoch()});
2066 }
2067 else if (i_value == constants::vpdCollectionInProgress)
2068 {
2069 l_timeStampMap.emplace(
2070 "StartTime", types::DbusVariantType{
2071 commonUtility::getCurrentTimeSinceEpoch()});
2072 }
2073 else if (i_value == constants::vpdCollectionNotStarted)
2074 {
2075 l_timeStampMap.emplace("StartTime", 0);
2076 l_timeStampMap.emplace("CompletedTime", 0);
2077 }
2078
Anupama B R24691d22025-05-21 08:14:15 -05002079 types::ObjectMap l_objectInterfaceMap;
2080
2081 if (m_parsedJson["frus"].contains(i_vpdPath))
2082 {
2083 for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath])
2084 {
2085 sdbusplus::message::object_path l_fruObjectPath(
2086 l_Fru["inventoryPath"]);
2087
2088 types::PropertyMap l_propertyValueMap;
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002089 l_propertyValueMap.emplace("Status", i_value);
Anupama B R4c65fcd2025-09-01 08:09:00 -05002090 l_propertyValueMap.insert(l_timeStampMap.begin(),
2091 l_timeStampMap.end());
Anupama B R24691d22025-05-21 08:14:15 -05002092
2093 types::InterfaceMap l_interfaces;
2094 vpdSpecificUtility::insertOrMerge(
2095 l_interfaces, constants::vpdCollectionInterface,
2096 move(l_propertyValueMap));
2097
2098 l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
2099 std::move(l_interfaces));
2100 }
2101 }
2102 else
2103 {
2104 // consider it as an inventory path.
2105 if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0)
2106 {
2107 throw std::runtime_error(
2108 "Invalid inventory path: " + i_vpdPath +
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002109 ". Can't set collection Status property");
Anupama B R24691d22025-05-21 08:14:15 -05002110 }
2111
2112 types::PropertyMap l_propertyValueMap;
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002113 l_propertyValueMap.emplace("Status", i_value);
Anupama B R4c65fcd2025-09-01 08:09:00 -05002114 l_propertyValueMap.insert(l_timeStampMap.begin(),
2115 l_timeStampMap.end());
Anupama B R24691d22025-05-21 08:14:15 -05002116
2117 types::InterfaceMap l_interfaces;
2118 vpdSpecificUtility::insertOrMerge(l_interfaces,
2119 constants::vpdCollectionInterface,
2120 move(l_propertyValueMap));
2121
2122 l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces));
2123 }
2124
2125 // Notify PIM
2126 if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
2127 {
2128 throw DbusException(
2129 std::string(__FUNCTION__) +
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002130 "Call to PIM failed while setting collection Status property for path " +
Anupama B R24691d22025-05-21 08:14:15 -05002131 i_vpdPath);
2132 }
2133 }
2134 catch (const std::exception& l_ex)
2135 {
2136 EventLogger::createSyncPel(
2137 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
2138 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
2139 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
2140 }
2141}
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05002142} // namespace vpd