blob: 9c020c52fd37873f26ca2d41b8fe7f3a56aca25c [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
Rekha Aparnac6159a22025-10-09 12:20:20 +053015#include <utility/common_utility.hpp>
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050016#include <utility/dbus_utility.hpp>
17#include <utility/json_utility.hpp>
18#include <utility/vpd_specific_utility.hpp>
19
20#include <filesystem>
21#include <fstream>
22#include <future>
23#include <typeindex>
24#include <unordered_set>
25
26namespace vpd
27{
28
Sunny Srivastava765cf7b2025-02-04 05:24:11 -060029Worker::Worker(std::string pathToConfigJson, uint8_t i_maxThreadCount) :
30 m_configJsonPath(pathToConfigJson), m_semaphore(i_maxThreadCount)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050031{
32 // Implies the processing is based on some config JSON
33 if (!m_configJsonPath.empty())
34 {
35 // Check if symlink is already there to confirm fresh boot/factory
36 // reset.
37 if (std::filesystem::exists(INVENTORY_JSON_SYM_LINK))
38 {
39 logging::logMessage("Sym Link already present");
40 m_configJsonPath = INVENTORY_JSON_SYM_LINK;
41 m_isSymlinkPresent = true;
42 }
43
44 try
45 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -050046 uint16_t l_errCode = 0;
47 m_parsedJson =
48 jsonUtility::getParsedJson(m_configJsonPath, l_errCode);
49
50 if (l_errCode)
51 {
52 throw std::runtime_error(
53 "JSON parsing failed for file [ " + m_configJsonPath +
Rekha Aparnac6159a22025-10-09 12:20:20 +053054 " ], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnaca9a0862025-08-29 04:08:33 -050055 }
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
Anupama B R68a70432025-09-25 02:09:37 -0500110 std::vector<std::string> interfaces = {
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500111 "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 +
Rekha Aparnac6159a22025-10-09 12:20:20 +0530398 " ], error : " + commonUtility::getErrCodeMsg(l_errCode),
Rekha Aparnaca9a0862025-08-29 04:08:33 -0500399 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
Rekha Aparna196e3082025-09-08 20:40:35 -0500427 if (isSystemVPDOnDBus())
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500428 {
Rekha Aparna196e3082025-09-08 20:40:35 -0500429 uint16_t l_errCode = 0;
430 if (jsonUtility::isBackupAndRestoreRequired(m_parsedJson,
431 l_errCode))
432 {
433 performBackupAndRestore(parsedVpdMap);
434 }
435 else if (l_errCode)
436 {
437 logging::logMessage(
438 "Failed to check if backup and restore required. Reason : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +0530439 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna196e3082025-09-08 20:40:35 -0500440 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500441 }
442
443 // proceed to publish system VPD.
444 publishSystemVPD(parsedVpdMap);
Anupama B R4c65fcd2025-09-01 08:09:00 -0500445 setCollectionStatusProperty(SYSTEM_VPD_FILE_PATH,
446 constants::vpdCollectionCompleted);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500447 return;
448 }
449
450 setEnvAndReboot("fitconfig", devTreeFromJson);
451 exit(EXIT_SUCCESS);
452}
453
454void Worker::populateIPZVPDpropertyMap(
455 types::InterfaceMap& interfacePropMap,
456 const types::IPZKwdValueMap& keyordValueMap,
457 const std::string& interfaceName)
458{
459 types::PropertyMap propertyValueMap;
460 for (const auto& kwdVal : keyordValueMap)
461 {
462 auto kwd = kwdVal.first;
463
464 if (kwd[0] == '#')
465 {
466 kwd = std::string("PD_") + kwd[1];
467 }
468 else if (isdigit(kwd[0]))
469 {
470 kwd = std::string("N_") + kwd;
471 }
472
473 types::BinaryVector value(kwdVal.second.begin(), kwdVal.second.end());
474 propertyValueMap.emplace(move(kwd), move(value));
475 }
476
477 if (!propertyValueMap.empty())
478 {
479 interfacePropMap.emplace(interfaceName, propertyValueMap);
480 }
481}
482
483void Worker::populateKwdVPDpropertyMap(const types::KeywordVpdMap& keyordVPDMap,
484 types::InterfaceMap& interfaceMap)
485{
486 for (const auto& kwdValMap : keyordVPDMap)
487 {
488 types::PropertyMap propertyValueMap;
489 auto kwd = kwdValMap.first;
490
491 if (kwd[0] == '#')
492 {
493 kwd = std::string("PD_") + kwd[1];
494 }
495 else if (isdigit(kwd[0]))
496 {
497 kwd = std::string("N_") + kwd;
498 }
499
500 if (auto keywordValue = get_if<types::BinaryVector>(&kwdValMap.second))
501 {
502 types::BinaryVector value((*keywordValue).begin(),
503 (*keywordValue).end());
504 propertyValueMap.emplace(move(kwd), move(value));
505 }
506 else if (auto keywordValue = get_if<std::string>(&kwdValMap.second))
507 {
508 types::BinaryVector value((*keywordValue).begin(),
509 (*keywordValue).end());
510 propertyValueMap.emplace(move(kwd), move(value));
511 }
512 else if (auto keywordValue = get_if<size_t>(&kwdValMap.second))
513 {
514 if (kwd == "MemorySizeInKB")
515 {
516 types::PropertyMap memProp;
517 memProp.emplace(move(kwd), ((*keywordValue)));
518 interfaceMap.emplace("xyz.openbmc_project.Inventory.Item.Dimm",
519 move(memProp));
520 continue;
521 }
522 else
523 {
524 logging::logMessage(
525 "Unknown Keyword =" + kwd + " found in keyword VPD map");
526 continue;
527 }
528 }
529 else
530 {
531 logging::logMessage(
532 "Unknown variant type found in keyword VPD map.");
533 continue;
534 }
535
536 if (!propertyValueMap.empty())
537 {
538 vpdSpecificUtility::insertOrMerge(
539 interfaceMap, constants::kwdVpdInf, move(propertyValueMap));
540 }
541 }
542}
543
544void Worker::populateInterfaces(const nlohmann::json& interfaceJson,
545 types::InterfaceMap& interfaceMap,
546 const types::VPDMapVariant& parsedVpdMap)
547{
548 for (const auto& interfacesPropPair : interfaceJson.items())
549 {
550 const std::string& interface = interfacesPropPair.key();
551 types::PropertyMap propertyMap;
552
553 for (const auto& propValuePair : interfacesPropPair.value().items())
554 {
555 const std::string property = propValuePair.key();
556
557 if (propValuePair.value().is_boolean())
558 {
559 propertyMap.emplace(property,
560 propValuePair.value().get<bool>());
561 }
562 else if (propValuePair.value().is_string())
563 {
564 if (property.compare("LocationCode") == 0 &&
565 interface.compare("com.ibm.ipzvpd.Location") == 0)
566 {
567 std::string value =
568 vpdSpecificUtility::getExpandedLocationCode(
569 propValuePair.value().get<std::string>(),
570 parsedVpdMap);
571 propertyMap.emplace(property, value);
572
573 auto l_locCodeProperty = propertyMap;
574 vpdSpecificUtility::insertOrMerge(
575 interfaceMap,
576 std::string(constants::xyzLocationCodeInf),
577 move(l_locCodeProperty));
578 }
579 else
580 {
581 propertyMap.emplace(
582 property, propValuePair.value().get<std::string>());
583 }
584 }
585 else if (propValuePair.value().is_array())
586 {
587 try
588 {
589 propertyMap.emplace(
590 property,
591 propValuePair.value().get<types::BinaryVector>());
592 }
593 catch (const nlohmann::detail::type_error& e)
594 {
595 std::cerr << "Type exception: " << e.what() << "\n";
596 }
597 }
598 else if (propValuePair.value().is_number())
599 {
600 // For now assume the value is a size_t. In the future it would
601 // be nice to come up with a way to get the type from the JSON.
602 propertyMap.emplace(property,
603 propValuePair.value().get<size_t>());
604 }
605 else if (propValuePair.value().is_object())
606 {
607 const std::string& record =
608 propValuePair.value().value("recordName", "");
609 const std::string& keyword =
610 propValuePair.value().value("keywordName", "");
611 const std::string& encoding =
612 propValuePair.value().value("encoding", "");
613
614 if (auto ipzVpdMap =
615 std::get_if<types::IPZVpdMap>(&parsedVpdMap))
616 {
617 if (!record.empty() && !keyword.empty() &&
618 (*ipzVpdMap).count(record) &&
619 (*ipzVpdMap).at(record).count(keyword))
620 {
621 auto encoded = vpdSpecificUtility::encodeKeyword(
622 ((*ipzVpdMap).at(record).at(keyword)), encoding);
623 propertyMap.emplace(property, encoded);
624 }
625 }
626 else if (auto kwdVpdMap =
627 std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
628 {
629 if (!keyword.empty() && (*kwdVpdMap).count(keyword))
630 {
631 if (auto kwValue = std::get_if<types::BinaryVector>(
632 &(*kwdVpdMap).at(keyword)))
633 {
634 auto encodedValue =
635 vpdSpecificUtility::encodeKeyword(
636 std::string((*kwValue).begin(),
637 (*kwValue).end()),
638 encoding);
639
640 propertyMap.emplace(property, encodedValue);
641 }
642 else if (auto kwValue = std::get_if<std::string>(
643 &(*kwdVpdMap).at(keyword)))
644 {
645 auto encodedValue =
646 vpdSpecificUtility::encodeKeyword(
647 std::string((*kwValue).begin(),
648 (*kwValue).end()),
649 encoding);
650
651 propertyMap.emplace(property, encodedValue);
652 }
653 else if (auto uintValue = std::get_if<size_t>(
654 &(*kwdVpdMap).at(keyword)))
655 {
656 propertyMap.emplace(property, *uintValue);
657 }
658 else
659 {
660 logging::logMessage(
661 "Unknown keyword found, Keywrod = " + keyword);
662 }
663 }
664 }
665 }
666 }
667 vpdSpecificUtility::insertOrMerge(interfaceMap, interface,
668 move(propertyMap));
669 }
670}
671
672bool Worker::isCPUIOGoodOnly(const std::string& i_pgKeyword)
673{
674 const unsigned char l_io[] = {
675 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF,
676 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
677
678 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
679 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
680 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
681 // IO.
682 if (memcmp(l_io, i_pgKeyword.data() + constants::INDEX_OF_EQ0_IN_PG,
683 constants::SIZE_OF_8EQ_IN_PG) == 0)
684 {
685 return true;
686 }
687
688 // The CPU is not an IO
689 return false;
690}
691
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500692void Worker::processEmbeddedAndSynthesizedFrus(const nlohmann::json& singleFru,
693 types::InterfaceMap& interfaces)
694{
695 // embedded property(true or false) says whether the subfru is embedded
696 // into the parent fru (or) not. VPD sets Present property only for
697 // embedded frus. If the subfru is not an embedded FRU, the subfru may
698 // or may not be physically present. Those non embedded frus will always
699 // have Present=false irrespective of its physical presence or absence.
700 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
701 // Present to true for such sub frus.
702 // Eg: ethernet port is embedded into bmc card. So set Present to true
703 // for such sub frus. Also donot populate present property for embedded
704 // subfru which is synthesized. Currently there is no subfru which are
705 // both embedded and synthesized. But still the case is handled here.
706
707 // Check if its required to handle presence for this FRU.
708 if (singleFru.value("handlePresence", true))
709 {
710 types::PropertyMap presProp;
711 presProp.emplace("Present", true);
712 vpdSpecificUtility::insertOrMerge(
713 interfaces, "xyz.openbmc_project.Inventory.Item", move(presProp));
714 }
715}
716
717void Worker::processExtraInterfaces(const nlohmann::json& singleFru,
718 types::InterfaceMap& interfaces,
719 const types::VPDMapVariant& parsedVpdMap)
720{
721 populateInterfaces(singleFru["extraInterfaces"], interfaces, parsedVpdMap);
722 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
723 {
724 if (singleFru["extraInterfaces"].contains(
725 "xyz.openbmc_project.Inventory.Item.Cpu"))
726 {
727 auto itrToRec = (*ipzVpdMap).find("CP00");
728 if (itrToRec == (*ipzVpdMap).end())
729 {
730 return;
731 }
732
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500733 uint16_t l_errCode = 0;
734 const std::string pgKeywordValue{vpdSpecificUtility::getKwVal(
735 itrToRec->second, "PG", l_errCode)};
Souvik Roya55fcca2025-02-19 01:33:58 -0600736
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500737 if (!pgKeywordValue.empty())
738 {
739 if (isCPUIOGoodOnly(pgKeywordValue))
740 {
741 interfaces["xyz.openbmc_project.Inventory.Item"]
742 ["PrettyName"] = "IO Module";
743 }
744 }
Souvik Roya55fcca2025-02-19 01:33:58 -0600745 else
746 {
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500747 throw DataException(
748 std::string(__FUNCTION__) +
749 "Failed to get value for keyword PG, error : " +
750 commonUtility::getErrCodeMsg(l_errCode));
Souvik Roya55fcca2025-02-19 01:33:58 -0600751 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500752 }
753 }
754}
755
756void Worker::processCopyRecordFlag(const nlohmann::json& singleFru,
757 const types::VPDMapVariant& parsedVpdMap,
758 types::InterfaceMap& interfaces)
759{
760 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
761 {
762 for (const auto& record : singleFru["copyRecords"])
763 {
764 const std::string& recordName = record;
765 if ((*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
766 {
767 populateIPZVPDpropertyMap(interfaces,
768 (*ipzVpdMap).at(recordName),
769 constants::ipzVpdInf + recordName);
770 }
771 }
772 }
773}
774
775void Worker::processInheritFlag(const types::VPDMapVariant& parsedVpdMap,
776 types::InterfaceMap& interfaces)
777{
778 if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
779 {
780 for (const auto& [recordName, kwdValueMap] : *ipzVpdMap)
781 {
782 populateIPZVPDpropertyMap(interfaces, kwdValueMap,
783 constants::ipzVpdInf + recordName);
784 }
785 }
786 else if (auto kwdVpdMap = std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
787 {
788 populateKwdVPDpropertyMap(*kwdVpdMap, interfaces);
789 }
790
791 if (m_parsedJson.contains("commonInterfaces"))
792 {
793 populateInterfaces(m_parsedJson["commonInterfaces"], interfaces,
794 parsedVpdMap);
795 }
796}
797
798bool Worker::processFruWithCCIN(const nlohmann::json& singleFru,
799 const types::VPDMapVariant& parsedVpdMap)
800{
801 if (auto ipzVPDMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
802 {
803 auto itrToRec = (*ipzVPDMap).find("VINI");
804 if (itrToRec == (*ipzVPDMap).end())
805 {
806 return false;
807 }
808
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500809 uint16_t l_errCode = 0;
Souvik Roya55fcca2025-02-19 01:33:58 -0600810 std::string ccinFromVpd{
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500811 vpdSpecificUtility::getKwVal(itrToRec->second, "CC", l_errCode)};
Souvik Roya55fcca2025-02-19 01:33:58 -0600812
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500813 if (ccinFromVpd.empty())
814 {
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500815 logging::logMessage("Failed to get CCIN kwd value, error : " +
816 commonUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500817 return false;
818 }
819
820 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
821 ::toupper);
822
823 std::vector<std::string> ccinList;
824 for (std::string ccin : singleFru["ccin"])
825 {
826 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
827 ccinList.push_back(ccin);
828 }
829
830 if (ccinList.empty())
831 {
832 return false;
833 }
834
835 if (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
836 ccinList.end())
837 {
838 return false;
839 }
840 }
841 return true;
842}
843
844void Worker::processFunctionalProperty(const std::string& i_inventoryObjPath,
845 types::InterfaceMap& io_interfaces)
846{
847 if (!dbusUtility::isChassisPowerOn())
848 {
Anupama B R68a70432025-09-25 02:09:37 -0500849 std::vector<std::string> l_operationalStatusInf = {
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500850 constants::operationalStatusInf};
851
852 auto mapperObjectMap = dbusUtility::getObjectMap(
853 i_inventoryObjPath, l_operationalStatusInf);
854
855 // If the object has been found. Check if it is under PIM.
856 if (mapperObjectMap.size() != 0)
857 {
858 for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap)
859 {
860 if (l_serviceName == constants::pimServiceName)
861 {
862 // The object is already under PIM. No need to process
863 // again. Retain the old value.
864 return;
865 }
866 }
867 }
868
869 // Implies value is not there in D-Bus. Populate it with default
870 // value "true".
871 types::PropertyMap l_functionalProp;
872 l_functionalProp.emplace("Functional", true);
873 vpdSpecificUtility::insertOrMerge(io_interfaces,
874 constants::operationalStatusInf,
875 move(l_functionalProp));
876 }
877
878 // if chassis is power on. Functional property should be there on D-Bus.
879 // Don't process.
880 return;
881}
882
883void Worker::processEnabledProperty(const std::string& i_inventoryObjPath,
884 types::InterfaceMap& io_interfaces)
885{
886 if (!dbusUtility::isChassisPowerOn())
887 {
Anupama B R68a70432025-09-25 02:09:37 -0500888 std::vector<std::string> l_enableInf = {constants::enableInf};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500889
890 auto mapperObjectMap =
891 dbusUtility::getObjectMap(i_inventoryObjPath, l_enableInf);
892
893 // If the object has been found. Check if it is under PIM.
894 if (mapperObjectMap.size() != 0)
895 {
896 for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap)
897 {
898 if (l_serviceName == constants::pimServiceName)
899 {
900 // The object is already under PIM. No need to process
901 // again. Retain the old value.
902 return;
903 }
904 }
905 }
906
907 // Implies value is not there in D-Bus. Populate it with default
908 // value "true".
909 types::PropertyMap l_enabledProp;
910 l_enabledProp.emplace("Enabled", true);
911 vpdSpecificUtility::insertOrMerge(io_interfaces, constants::enableInf,
912 move(l_enabledProp));
913 }
914
915 // if chassis is power on. Enabled property should be there on D-Bus.
916 // Don't process.
917 return;
918}
919
920void Worker::populateDbus(const types::VPDMapVariant& parsedVpdMap,
921 types::ObjectMap& objectInterfaceMap,
922 const std::string& vpdFilePath)
923{
924 if (vpdFilePath.empty())
925 {
926 throw std::runtime_error(
Sunny Srivastava4c509c22025-03-25 12:43:40 +0530927 std::string(__FUNCTION__) +
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500928 "Invalid parameter passed to populateDbus API.");
929 }
930
931 // JSON config is mandatory for processing of "if". Add "else" for any
932 // processing without config JSON.
933 if (!m_parsedJson.empty())
934 {
935 types::InterfaceMap interfaces;
936
937 for (const auto& aFru : m_parsedJson["frus"][vpdFilePath])
938 {
939 const auto& inventoryPath = aFru["inventoryPath"];
940 sdbusplus::message::object_path fruObjectPath(inventoryPath);
941 if (aFru.contains("ccin"))
942 {
943 if (!processFruWithCCIN(aFru, parsedVpdMap))
944 {
945 continue;
946 }
947 }
948
949 if (aFru.value("inherit", true))
950 {
951 processInheritFlag(parsedVpdMap, interfaces);
952 }
953
954 // If specific record needs to be copied.
955 if (aFru.contains("copyRecords"))
956 {
957 processCopyRecordFlag(aFru, parsedVpdMap, interfaces);
958 }
959
960 if (aFru.contains("extraInterfaces"))
961 {
962 // Process extra interfaces w.r.t a FRU.
963 processExtraInterfaces(aFru, interfaces, parsedVpdMap);
964 }
965
966 // Process FRUS which are embedded in the parent FRU and whose VPD
967 // will be synthesized.
968 if ((aFru.value("embedded", true)) &&
969 (!aFru.value("synthesized", false)))
970 {
971 processEmbeddedAndSynthesizedFrus(aFru, interfaces);
972 }
973
974 processFunctionalProperty(inventoryPath, interfaces);
975 processEnabledProperty(inventoryPath, interfaces);
976
977 objectInterfaceMap.emplace(std::move(fruObjectPath),
978 std::move(interfaces));
979 }
980 }
981}
982
Patrick Williams43fedab2025-02-03 14:28:05 -0500983std::string Worker::createAssetTagString(
984 const types::VPDMapVariant& i_parsedVpdMap)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500985{
986 std::string l_assetTag;
987
988 // system VPD will be in IPZ format.
989 if (auto l_parsedVpdMap = std::get_if<types::IPZVpdMap>(&i_parsedVpdMap))
990 {
991 auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS);
992 if (l_itrToVsys != (*l_parsedVpdMap).end())
993 {
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500994 uint16_t l_errCode = 0;
Souvik Roya55fcca2025-02-19 01:33:58 -0600995 const std::string l_tmKwdValue{vpdSpecificUtility::getKwVal(
Rekha Aparna7d9a7062025-10-07 04:14:42 -0500996 l_itrToVsys->second, constants::kwdTM, l_errCode)};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500997
Souvik Roya55fcca2025-02-19 01:33:58 -0600998 if (l_tmKwdValue.empty())
999 {
1000 throw std::runtime_error(
1001 std::string("Failed to get value for keyword [") +
1002 constants::kwdTM +
Rekha Aparna7d9a7062025-10-07 04:14:42 -05001003 std::string("] while creating Asset tag. Error : " +
1004 commonUtility::getErrCodeMsg(l_errCode)));
Souvik Roya55fcca2025-02-19 01:33:58 -06001005 }
1006
1007 const std::string l_seKwdValue{vpdSpecificUtility::getKwVal(
Rekha Aparna7d9a7062025-10-07 04:14:42 -05001008 l_itrToVsys->second, constants::kwdSE, l_errCode)};
Souvik Roya55fcca2025-02-19 01:33:58 -06001009
1010 if (l_seKwdValue.empty())
1011 {
1012 throw std::runtime_error(
1013 std::string("Failed to get value for keyword [") +
1014 constants::kwdSE +
Rekha Aparna7d9a7062025-10-07 04:14:42 -05001015 std::string("] while creating Asset tag. Error : " +
1016 commonUtility::getErrCodeMsg(l_errCode)));
Souvik Roya55fcca2025-02-19 01:33:58 -06001017 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001018
1019 l_assetTag = std::string{"Server-"} + l_tmKwdValue +
1020 std::string{"-"} + l_seKwdValue;
1021 }
1022 else
1023 {
1024 throw std::runtime_error(
1025 "VSYS record not found in parsed VPD map to create Asset tag.");
1026 }
1027 }
1028 else
1029 {
1030 throw std::runtime_error(
1031 "Invalid VPD type recieved to create Asset tag.");
1032 }
1033
1034 return l_assetTag;
1035}
1036
1037void Worker::publishSystemVPD(const types::VPDMapVariant& parsedVpdMap)
1038{
1039 types::ObjectMap objectInterfaceMap;
1040
1041 if (std::get_if<types::IPZVpdMap>(&parsedVpdMap))
1042 {
1043 populateDbus(parsedVpdMap, objectInterfaceMap, SYSTEM_VPD_FILE_PATH);
1044
1045 try
1046 {
1047 if (m_isFactoryResetDone)
1048 {
1049 const auto& l_assetTag = createAssetTagString(parsedVpdMap);
1050
1051 auto l_itrToSystemPath = objectInterfaceMap.find(
1052 sdbusplus::message::object_path(constants::systemInvPath));
1053 if (l_itrToSystemPath == objectInterfaceMap.end())
1054 {
1055 throw std::runtime_error(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301056 "Asset tag update failed. System Path not found in object map.");
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001057 }
1058
1059 types::PropertyMap l_assetTagProperty;
1060 l_assetTagProperty.emplace("AssetTag", l_assetTag);
1061
1062 (l_itrToSystemPath->second)
1063 .emplace(constants::assetTagInf,
1064 std::move(l_assetTagProperty));
1065 }
1066 }
1067 catch (const std::exception& l_ex)
1068 {
1069 EventLogger::createSyncPel(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301070 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
1071 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001072 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
1073 }
1074
1075 // Notify PIM
1076 if (!dbusUtility::callPIM(move(objectInterfaceMap)))
1077 {
1078 throw std::runtime_error("Call to PIM failed for system VPD");
1079 }
1080 }
1081 else
1082 {
1083 throw DataException("Invalid format of parsed VPD map.");
1084 }
1085}
1086
1087bool Worker::processPreAction(const std::string& i_vpdFilePath,
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001088 const std::string& i_flagToProcess,
1089 uint16_t& i_errCode)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001090{
1091 if (i_vpdFilePath.empty() || i_flagToProcess.empty())
1092 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001093 i_errCode = error_code::INVALID_INPUT_PARAMETER;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001094 return false;
1095 }
1096
1097 if ((!jsonUtility::executeBaseAction(m_parsedJson, "preAction",
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001098 i_vpdFilePath, i_flagToProcess,
1099 i_errCode)) &&
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001100 (i_flagToProcess.compare("collection") == constants::STR_CMP_SUCCESS))
1101 {
1102 // TODO: Need a way to delete inventory object from Dbus and persisted
1103 // data section in case any FRU is not present or there is any
1104 // problem in collecting it. Once it has been deleted, it can be
1105 // re-created in the flow of priming the inventory. This needs to be
1106 // done either here or in the exception section of "parseAndPublishVPD"
1107 // API. Any failure in the process of collecting FRU will land up in the
1108 // excpetion of "parseAndPublishVPD".
1109
1110 // If the FRU is not there, clear the VINI/CCIN data.
1111 // Enity manager probes for this keyword to look for this
1112 // FRU, now if the data is persistent on BMC and FRU is
1113 // removed this can lead to ambiguity. Hence clearing this
1114 // Keyword if FRU is absent.
1115 const auto& inventoryPath =
1116 m_parsedJson["frus"][i_vpdFilePath].at(0).value("inventoryPath",
1117 "");
1118
1119 if (!inventoryPath.empty())
1120 {
1121 types::ObjectMap l_pimObjMap{
1122 {inventoryPath,
1123 {{constants::kwdVpdInf,
1124 {{constants::kwdCCIN, types::BinaryVector{}}}}}}};
1125
1126 if (!dbusUtility::callPIM(std::move(l_pimObjMap)))
1127 {
1128 logging::logMessage(
1129 "Call to PIM failed for file " + i_vpdFilePath);
1130 }
1131 }
1132 else
1133 {
1134 logging::logMessage(
1135 "Inventory path is empty in Json for file " + i_vpdFilePath);
1136 }
1137
1138 return false;
1139 }
1140 return true;
1141}
1142
1143bool Worker::processPostAction(
1144 const std::string& i_vpdFruPath, const std::string& i_flagToProcess,
1145 const std::optional<types::VPDMapVariant> i_parsedVpd)
1146{
1147 if (i_vpdFruPath.empty() || i_flagToProcess.empty())
1148 {
1149 logging::logMessage(
1150 "Invalid input parameter. Abort processing post action");
1151 return false;
1152 }
1153
1154 // Check if post action tag is to be triggered in the flow of collection
1155 // based on some CCIN value?
1156 if (m_parsedJson["frus"][i_vpdFruPath]
1157 .at(0)["postAction"][i_flagToProcess]
1158 .contains("ccin"))
1159 {
1160 if (!i_parsedVpd.has_value())
1161 {
1162 logging::logMessage("Empty VPD Map");
1163 return false;
1164 }
1165
1166 // CCIN match is required to process post action for this FRU as it
1167 // contains the flag.
1168 if (!vpdSpecificUtility::findCcinInVpd(
1169 m_parsedJson["frus"][i_vpdFruPath].at(
1170 0)["postAction"]["collection"],
1171 i_parsedVpd.value()))
1172 {
1173 // If CCIN is not found, implies post action processing is not
1174 // required for this FRU. Let the flow continue.
1175 return true;
1176 }
1177 }
1178
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001179 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001180 if (!jsonUtility::executeBaseAction(m_parsedJson, "postAction",
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001181 i_vpdFruPath, i_flagToProcess,
1182 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001183 {
1184 logging::logMessage(
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001185 "Execution of post action failed for path: " + i_vpdFruPath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301186 " . Reason: " + commonUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001187
1188 // If post action was required and failed only in that case return
1189 // false. In all other case post action is considered passed.
1190 return false;
1191 }
1192
1193 return true;
1194}
1195
1196types::VPDMapVariant Worker::parseVpdFile(const std::string& i_vpdFilePath)
1197{
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001198 try
1199 {
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001200 uint16_t l_errCode = 0;
1201
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301202 if (i_vpdFilePath.empty())
1203 {
1204 throw std::runtime_error(
1205 std::string(__FUNCTION__) +
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301206 " Empty VPD file path passed. Abort processing");
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301207 }
1208
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301209 bool isPreActionRequired = false;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001210 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001211 "preAction", "collection", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001212 {
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001213 l_errCode = 0;
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301214 isPreActionRequired = true;
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001215 if (!processPreAction(i_vpdFilePath, "collection", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001216 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001217 if (l_errCode == error_code::DEVICE_NOT_PRESENT)
1218 {
1219 logging::logMessage(
Rekha Aparnac6159a22025-10-09 12:20:20 +05301220 commonUtility::getErrCodeMsg(l_errCode) +
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001221 i_vpdFilePath);
1222 // Presence pin has been read successfully and has been read
1223 // as false, so this is not a failure case, hence returning
1224 // empty variant so that pre action is not marked as failed.
1225 return types::VPDMapVariant{};
1226 }
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301227 throw std::runtime_error(
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001228 std::string(__FUNCTION__) +
1229 " Pre-Action failed with error: " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301230 commonUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001231 }
1232 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001233 else if (l_errCode)
1234 {
1235 logging::logMessage(
1236 "Failed to check if pre action required for FRU [" +
1237 i_vpdFilePath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301238 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001239 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001240
1241 if (!std::filesystem::exists(i_vpdFilePath))
1242 {
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301243 if (isPreActionRequired)
1244 {
1245 throw std::runtime_error(
1246 std::string(__FUNCTION__) + " Could not find file path " +
1247 i_vpdFilePath + "Skipping parser trigger for the EEPROM");
1248 }
1249 return types::VPDMapVariant{};
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001250 }
1251
1252 std::shared_ptr<Parser> vpdParser =
1253 std::make_shared<Parser>(i_vpdFilePath, m_parsedJson);
1254
1255 types::VPDMapVariant l_parsedVpd = vpdParser->parse();
1256
1257 // Before returning, as collection is over, check if FRU qualifies for
1258 // any post action in the flow of collection.
1259 // Note: Don't change the order, post action needs to be processed only
1260 // after collection for FRU is successfully done.
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001261 l_errCode = 0;
1262
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001263 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001264 "postAction", "collection",
1265 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001266 {
1267 if (!processPostAction(i_vpdFilePath, "collection", l_parsedVpd))
1268 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301269 // Post action was required but failed while executing.
1270 // Behaviour can be undefined.
1271 EventLogger::createSyncPel(
1272 types::ErrorType::InternalFailure,
1273 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
1274 std::string("Required post action failed for path [" +
1275 i_vpdFilePath + "]"),
1276 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001277 }
1278 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001279 else if (l_errCode)
1280 {
1281 logging::logMessage(
1282 "Error while checking if post action required for FRU [" +
1283 i_vpdFilePath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301284 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001285 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001286
1287 return l_parsedVpd;
1288 }
1289 catch (std::exception& l_ex)
1290 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001291 uint16_t l_errCode = 0;
Souvik Roy37c6bef2025-07-17 00:55:59 -05001292 std::string l_exMsg{
1293 std::string(__FUNCTION__) + " : VPD parsing failed for " +
1294 i_vpdFilePath + " due to error: " + l_ex.what()};
1295
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001296 // If post fail action is required, execute it.
1297 if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001298 "postFailAction", "collection",
1299 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001300 {
1301 if (!jsonUtility::executePostFailAction(m_parsedJson, i_vpdFilePath,
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001302 "collection", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001303 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001304 l_exMsg += ". Post fail action also failed. Error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301305 commonUtility::getErrCodeMsg(l_errCode) +
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001306 " Aborting collection for this FRU.";
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001307 }
1308 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001309 else if (l_errCode)
1310 {
1311 l_exMsg +=
1312 ". Failed to check if post fail action required, error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301313 commonUtility::getErrCodeMsg(l_errCode);
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001314 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001315
Souvik Roy37c6bef2025-07-17 00:55:59 -05001316 if (typeid(l_ex) == typeid(DataException))
1317 {
1318 throw DataException(l_exMsg);
1319 }
1320 else if (typeid(l_ex) == typeid(EccException))
1321 {
1322 throw EccException(l_exMsg);
1323 }
1324 throw std::runtime_error(l_exMsg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001325 }
1326}
1327
Patrick Williams43fedab2025-02-03 14:28:05 -05001328std::tuple<bool, std::string> Worker::parseAndPublishVPD(
1329 const std::string& i_vpdFilePath)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001330{
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001331 std::string l_inventoryPath{};
1332
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001333 try
1334 {
1335 m_semaphore.acquire();
1336
1337 // Thread launched.
1338 m_mutex.lock();
1339 m_activeCollectionThreadCount++;
1340 m_mutex.unlock();
1341
Anupama B R4c65fcd2025-09-01 08:09:00 -05001342 setCollectionStatusProperty(i_vpdFilePath,
1343 constants::vpdCollectionInProgress);
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001344
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001345 const types::VPDMapVariant& parsedVpdMap = parseVpdFile(i_vpdFilePath);
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301346 if (!std::holds_alternative<std::monostate>(parsedVpdMap))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001347 {
Sunny Srivastava0a5fce12025-04-09 11:09:51 +05301348 types::ObjectMap objectInterfaceMap;
1349 populateDbus(parsedVpdMap, objectInterfaceMap, i_vpdFilePath);
1350
1351 // Notify PIM
1352 if (!dbusUtility::callPIM(move(objectInterfaceMap)))
1353 {
1354 throw std::runtime_error(
1355 std::string(__FUNCTION__) +
1356 "Call to PIM failed while publishing VPD.");
1357 }
1358 }
1359 else
1360 {
1361 logging::logMessage("Empty parsedVpdMap recieved for path [" +
1362 i_vpdFilePath + "]. Check PEL for reason.");
Anupama B R4c65fcd2025-09-01 08:09:00 -05001363
1364 // As empty parsedVpdMap recieved for some reason, but still
1365 // considered VPD collection is completed. Hence FRU collection
1366 // Status will be set as completed.
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001367 }
1368 }
1369 catch (const std::exception& ex)
1370 {
Anupama B R24691d22025-05-21 08:14:15 -05001371 setCollectionStatusProperty(i_vpdFilePath,
Anupama B R5cd1b2d2025-08-05 04:57:40 -05001372 constants::vpdCollectionFailed);
Priyanga Ramasamy1aad7832024-12-12 22:13:52 -06001373
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001374 // handle all the exceptions internally. Return only true/false
1375 // based on status of execution.
1376 if (typeid(ex) == std::type_index(typeid(DataException)))
1377 {
Rekha Aparna017567a2025-08-13 02:07:06 -05001378 uint16_t l_errCode = 0;
Sunny Srivastava78c91072025-02-05 14:09:50 +05301379 // In case of pass1 planar, VPD can be corrupted on PCIe cards. Skip
1380 // logging error for these cases.
1381 if (vpdSpecificUtility::isPass1Planar())
1382 {
Rekha Aparna017567a2025-08-13 02:07:06 -05001383 std::string l_invPath =
1384 jsonUtility::getInventoryObjPathFromJson(
1385 m_parsedJson, i_vpdFilePath, l_errCode);
1386
1387 if (l_errCode != 0)
1388 {
1389 logging::logMessage(
1390 "Failed to get inventory object path from JSON for FRU [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301391 i_vpdFilePath +
1392 "], error: " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna017567a2025-08-13 02:07:06 -05001393 }
1394
RekhaAparna011ef21002025-02-18 23:47:36 -06001395 const std::string& l_invPathLeafValue =
Rekha Aparna017567a2025-08-13 02:07:06 -05001396 sdbusplus::message::object_path(l_invPath).filename();
Sunny Srivastava78c91072025-02-05 14:09:50 +05301397
RekhaAparna011ef21002025-02-18 23:47:36 -06001398 if ((l_invPathLeafValue.find("pcie_card", 0) !=
1399 std::string::npos))
Sunny Srivastava78c91072025-02-05 14:09:50 +05301400 {
1401 // skip logging any PEL for PCIe cards on pass 1 planar.
1402 return std::make_tuple(false, i_vpdFilePath);
1403 }
1404 }
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301405 }
Sunny Srivastava78c91072025-02-05 14:09:50 +05301406
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301407 EventLogger::createSyncPel(
Souvik Roy37c6bef2025-07-17 00:55:59 -05001408 EventLogger::getErrorType(ex),
1409 (typeid(ex) == typeid(DataException)) ||
1410 (typeid(ex) == typeid(EccException))
1411 ? types::SeverityType::Warning
1412 : types::SeverityType::Informational,
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301413 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(ex),
1414 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001415
1416 // TODO: Figure out a way to clear data in case of any failure at
1417 // runtime.
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301418
1419 // set present property to false for any error case. In future this will
1420 // be replaced by presence logic.
Souvik Roy6a9553c2025-02-07 01:16:32 -06001421 // Update Present property for this FRU only if we handle Present
1422 // property for the FRU.
1423 if (isPresentPropertyHandlingRequired(
1424 m_parsedJson["frus"][i_vpdFilePath].at(0)))
1425 {
1426 setPresentProperty(i_vpdFilePath, false);
1427 }
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301428
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001429 m_semaphore.release();
1430 return std::make_tuple(false, i_vpdFilePath);
1431 }
Anupama B R4c65fcd2025-09-01 08:09:00 -05001432
1433 setCollectionStatusProperty(i_vpdFilePath,
1434 constants::vpdCollectionCompleted);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001435 m_semaphore.release();
1436 return std::make_tuple(true, i_vpdFilePath);
1437}
1438
Sunny Srivastava61611752025-02-04 00:29:33 -06001439bool Worker::skipPathForCollection(const std::string& i_vpdFilePath)
1440{
1441 if (i_vpdFilePath.empty())
1442 {
1443 return true;
1444 }
1445
1446 // skip processing of system VPD again as it has been already collected.
1447 if (i_vpdFilePath == SYSTEM_VPD_FILE_PATH)
1448 {
1449 return true;
1450 }
1451
1452 if (dbusUtility::isChassisPowerOn())
1453 {
1454 // If chassis is powered on, skip collecting FRUs which are
1455 // powerOffOnly.
Rekha Aparna52041882025-09-01 20:48:07 -05001456
1457 uint16_t l_errCode = 0;
1458 if (jsonUtility::isFruPowerOffOnly(m_parsedJson, i_vpdFilePath,
1459 l_errCode))
Sunny Srivastava61611752025-02-04 00:29:33 -06001460 {
1461 return true;
1462 }
Rekha Aparna52041882025-09-01 20:48:07 -05001463 else if (l_errCode)
1464 {
1465 logging::logMessage(
1466 "Failed to check if FRU is power off only for FRU [" +
1467 i_vpdFilePath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301468 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna52041882025-09-01 20:48:07 -05001469 }
Sunny Srivastava61611752025-02-04 00:29:33 -06001470
Rekha Aparna52041882025-09-01 20:48:07 -05001471 l_errCode = 0;
Rekha Aparna017567a2025-08-13 02:07:06 -05001472 std::string l_invPath = jsonUtility::getInventoryObjPathFromJson(
1473 m_parsedJson, i_vpdFilePath, l_errCode);
1474
1475 if (l_errCode)
1476 {
1477 logging::logMessage(
1478 "Failed to get inventory path from JSON for FRU [" +
1479 i_vpdFilePath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301480 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna017567a2025-08-13 02:07:06 -05001481
1482 return false;
1483 }
1484
Sunny Srivastava61611752025-02-04 00:29:33 -06001485 const std::string& l_invPathLeafValue =
Rekha Aparna017567a2025-08-13 02:07:06 -05001486 sdbusplus::message::object_path(l_invPath).filename();
Sunny Srivastava61611752025-02-04 00:29:33 -06001487
1488 if ((l_invPathLeafValue.find("pcie_card", 0) != std::string::npos))
1489 {
1490 return true;
1491 }
1492 }
1493
1494 return false;
1495}
1496
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001497void Worker::collectFrusFromJson()
1498{
1499 // A parsed JSON file should be present to pick FRUs EEPROM paths
1500 if (m_parsedJson.empty())
1501 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301502 throw JsonException(
1503 std::string(__FUNCTION__) +
1504 ": Config JSON is mandatory for processing of FRUs through this API.",
1505 m_configJsonPath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001506 }
1507
1508 const nlohmann::json& listOfFrus =
1509 m_parsedJson["frus"].get_ref<const nlohmann::json::object_t&>();
1510
1511 for (const auto& itemFRUS : listOfFrus.items())
1512 {
1513 const std::string& vpdFilePath = itemFRUS.key();
1514
Sunny Srivastava61611752025-02-04 00:29:33 -06001515 if (skipPathForCollection(vpdFilePath))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001516 {
1517 continue;
1518 }
1519
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001520 try
1521 {
1522 std::thread{[vpdFilePath, this]() {
1523 const auto& l_parseResult = parseAndPublishVPD(vpdFilePath);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001524
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001525 m_mutex.lock();
1526 m_activeCollectionThreadCount--;
1527 m_mutex.unlock();
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001528
Souvik Roy1f4c8f82025-01-23 00:37:43 -06001529 if (!m_activeCollectionThreadCount)
1530 {
1531 m_isAllFruCollected = true;
1532 }
1533 }}.detach();
1534 }
1535 catch (const std::exception& l_ex)
1536 {
1537 // add vpdFilePath(EEPROM path) to failed list
1538 m_failedEepromPaths.push_front(vpdFilePath);
1539 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001540 }
1541}
1542
1543// ToDo: Move the API under IBM_SYSTEM
1544void Worker::performBackupAndRestore(types::VPDMapVariant& io_srcVpdMap)
1545{
1546 try
1547 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001548 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001549 std::string l_backupAndRestoreCfgFilePath =
1550 m_parsedJson.value("backupRestoreConfigPath", "");
1551
1552 nlohmann::json l_backupAndRestoreCfgJsonObj =
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001553 jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath,
1554 l_errCode);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001555
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001556 if (l_errCode)
RekhaAparna011ef21002025-02-18 23:47:36 -06001557 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001558 throw JsonException(
1559 "JSON parsing failed for file [ " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301560 l_backupAndRestoreCfgFilePath +
1561 " ], error : " + commonUtility::getErrCodeMsg(l_errCode),
Rekha Aparnaca9a0862025-08-29 04:08:33 -05001562 l_backupAndRestoreCfgFilePath);
RekhaAparna011ef21002025-02-18 23:47:36 -06001563 }
1564
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001565 // check if either of "source" or "destination" has inventory path.
1566 // this indicates that this sytem has System VPD on hardware
1567 // and other copy on D-Bus (BMC cache).
1568 if (!l_backupAndRestoreCfgJsonObj.empty() &&
1569 ((l_backupAndRestoreCfgJsonObj.contains("source") &&
1570 l_backupAndRestoreCfgJsonObj["source"].contains(
1571 "inventoryPath")) ||
1572 (l_backupAndRestoreCfgJsonObj.contains("destination") &&
1573 l_backupAndRestoreCfgJsonObj["destination"].contains(
1574 "inventoryPath"))))
1575 {
1576 BackupAndRestore l_backupAndRestoreObj(m_parsedJson);
1577 auto [l_srcVpdVariant,
1578 l_dstVpdVariant] = l_backupAndRestoreObj.backupAndRestore();
1579
1580 // ToDo: Revisit is this check is required or not.
1581 if (auto l_srcVpdMap =
1582 std::get_if<types::IPZVpdMap>(&l_srcVpdVariant);
1583 l_srcVpdMap && !(*l_srcVpdMap).empty())
1584 {
1585 io_srcVpdMap = std::move(l_srcVpdVariant);
1586 }
1587 }
1588 }
1589 catch (const std::exception& l_ex)
1590 {
1591 EventLogger::createSyncPel(
Sunny Srivastava043955d2025-01-21 18:04:49 +05301592 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
Sunny Srivastava15a189a2025-02-26 16:53:19 +05301593 __FILE__, __FUNCTION__, 0,
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001594 std::string(
1595 "Exception caught while backup and restore VPD keyword's.") +
Sunny Srivastava15a189a2025-02-26 16:53:19 +05301596 EventLogger::getErrorMsg(l_ex),
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001597 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
1598 }
1599}
1600
1601void Worker::deleteFruVpd(const std::string& i_dbusObjPath)
1602{
1603 if (i_dbusObjPath.empty())
1604 {
1605 throw std::runtime_error("Given DBus object path is empty.");
1606 }
1607
Rekha Aparna0578dd22025-09-02 08:20:21 -05001608 uint16_t l_errCode = 0;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001609 const std::string& l_fruPath =
Rekha Aparna0578dd22025-09-02 08:20:21 -05001610 jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath, l_errCode);
1611
1612 if (l_errCode)
1613 {
1614 logging::logMessage(
1615 "Failed to get FRU path for inventory path [" + i_dbusObjPath +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301616 "], error : " + commonUtility::getErrCodeMsg(l_errCode) +
Rekha Aparna0578dd22025-09-02 08:20:21 -05001617 " Aborting FRU VPD deletion.");
1618 return;
1619 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001620
1621 try
1622 {
1623 auto l_presentPropValue = dbusUtility::readDbusProperty(
1624 constants::pimServiceName, i_dbusObjPath,
1625 constants::inventoryItemInf, "Present");
1626
1627 if (auto l_value = std::get_if<bool>(&l_presentPropValue))
1628 {
Rekha Aparnaa1187a52025-09-01 12:42:19 -05001629 uint16_t l_errCode = 0;
Souvik Roye9120152025-07-02 08:24:38 -05001630 // check if FRU's Present property is handled by vpd-manager
1631 const auto& l_isFruPresenceHandled =
Rekha Aparnaa1187a52025-09-01 12:42:19 -05001632 jsonUtility::isFruPresenceHandled(m_parsedJson, l_fruPath,
1633 l_errCode);
1634
1635 if (l_errCode)
1636 {
1637 throw std::runtime_error(
1638 "Failed to check if FRU's presence is handled, reason: " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301639 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnaa1187a52025-09-01 12:42:19 -05001640 }
Souvik Roye9120152025-07-02 08:24:38 -05001641
1642 if (!(*l_value) && l_isFruPresenceHandled)
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001643 {
1644 throw std::runtime_error("Given FRU is not present");
1645 }
Souvik Roye9120152025-07-02 08:24:38 -05001646 else if (*l_value && !l_isFruPresenceHandled)
1647 {
1648 throw std::runtime_error(
1649 "Given FRU is present and its presence is not handled by vpd-manager.");
1650 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001651 else
1652 {
1653 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001654 "preAction", "deletion",
1655 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001656 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001657 if (!processPreAction(l_fruPath, "deletion", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001658 {
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001659 std::string l_msg = "Pre action failed";
1660 if (l_errCode)
1661 {
Rekha Aparnac6159a22025-10-09 12:20:20 +05301662 l_msg += " Reason: " +
1663 commonUtility::getErrCodeMsg(l_errCode);
Sunny Srivastava4f053df2025-09-03 02:27:37 -05001664 }
1665 throw std::runtime_error(l_msg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001666 }
1667 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001668 else if (l_errCode)
1669 {
1670 logging::logMessage(
1671 "Failed to check if pre action required for FRU [" +
1672 l_fruPath + "], error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301673 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001674 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001675
1676 std::vector<std::string> l_interfaceList{
1677 constants::operationalStatusInf};
1678
1679 types::MapperGetSubTree l_subTreeMap =
1680 dbusUtility::getObjectSubTree(i_dbusObjPath, 0,
1681 l_interfaceList);
1682
1683 types::ObjectMap l_objectMap;
1684
1685 // Updates VPD specific interfaces property value under PIM for
1686 // sub FRUs.
1687 for (const auto& [l_objectPath, l_serviceInterfaceMap] :
1688 l_subTreeMap)
1689 {
1690 types::InterfaceMap l_interfaceMap;
1691 vpdSpecificUtility::resetDataUnderPIM(l_objectPath,
1692 l_interfaceMap);
1693 l_objectMap.emplace(l_objectPath,
1694 std::move(l_interfaceMap));
1695 }
1696
1697 types::InterfaceMap l_interfaceMap;
1698 vpdSpecificUtility::resetDataUnderPIM(i_dbusObjPath,
1699 l_interfaceMap);
1700
1701 l_objectMap.emplace(i_dbusObjPath, std::move(l_interfaceMap));
1702
1703 if (!dbusUtility::callPIM(std::move(l_objectMap)))
1704 {
1705 throw std::runtime_error("Call to PIM failed.");
1706 }
1707
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001708 l_errCode = 0;
1709
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001710 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001711 "postAction", "deletion",
1712 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001713 {
1714 if (!processPostAction(l_fruPath, "deletion"))
1715 {
1716 throw std::runtime_error("Post action failed");
1717 }
1718 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001719 else if (l_errCode)
1720 {
1721 logging::logMessage(
1722 "Failed to check if post action required during deletion for FRU [" +
1723 l_fruPath + "], error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301724 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001725 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001726 }
1727 }
1728 else
1729 {
1730 logging::logMessage(
1731 "Can't process delete VPD for FRU [" + i_dbusObjPath +
1732 "] as unable to read present property");
1733 return;
1734 }
1735
1736 logging::logMessage(
1737 "Successfully completed deletion of FRU VPD for " + i_dbusObjPath);
1738 }
1739 catch (const std::exception& l_ex)
1740 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001741 uint16_t l_errCode = 0;
1742 std::string l_errMsg =
1743 "Failed to delete VPD for FRU : " + i_dbusObjPath +
1744 " error: " + std::string(l_ex.what());
1745
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001746 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001747 "postFailAction", "deletion",
1748 l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001749 {
1750 if (!jsonUtility::executePostFailAction(m_parsedJson, l_fruPath,
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001751 "deletion", l_errCode))
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001752 {
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001753 l_errMsg += ". Post fail action also failed, error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301754 commonUtility::getErrCodeMsg(l_errCode);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001755 }
1756 }
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001757 else if (l_errCode)
1758 {
1759 l_errMsg +=
1760 ". Failed to check if post fail action required, error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301761 commonUtility::getErrCodeMsg(l_errCode);
Rekha Aparnab50bf0e2025-09-02 21:13:26 -05001762 }
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001763
Rekha Aparnaff7d7992025-09-01 11:08:53 -05001764 logging::logMessage(l_errMsg);
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001765 }
1766}
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301767
1768void Worker::setPresentProperty(const std::string& i_vpdPath,
1769 const bool& i_value)
1770{
1771 try
1772 {
1773 if (i_vpdPath.empty())
1774 {
1775 throw std::runtime_error(
1776 "Path is empty. Can't set present property");
1777 }
1778
1779 types::ObjectMap l_objectInterfaceMap;
1780
1781 // If the given path is EEPROM path.
1782 if (m_parsedJson["frus"].contains(i_vpdPath))
1783 {
1784 for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath])
1785 {
1786 sdbusplus::message::object_path l_fruObjectPath(
1787 l_Fru["inventoryPath"]);
1788
1789 types::PropertyMap l_propertyValueMap;
1790 l_propertyValueMap.emplace("Present", i_value);
1791
1792 types::InterfaceMap l_interfaces;
1793 vpdSpecificUtility::insertOrMerge(l_interfaces,
1794 constants::inventoryItemInf,
1795 move(l_propertyValueMap));
1796
1797 l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
1798 std::move(l_interfaces));
1799 }
1800 }
1801 else
1802 {
1803 // consider it as an inventory path.
1804 if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0)
1805 {
1806 throw std::runtime_error(
1807 "Invalid inventory path: " + i_vpdPath);
1808 }
1809
1810 types::PropertyMap l_propertyValueMap;
1811 l_propertyValueMap.emplace("Present", i_value);
1812
1813 types::InterfaceMap l_interfaces;
1814 vpdSpecificUtility::insertOrMerge(l_interfaces,
1815 constants::inventoryItemInf,
1816 move(l_propertyValueMap));
1817
1818 l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces));
1819 }
1820
1821 // Notify PIM
1822 if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
1823 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301824 throw DbusException(
1825 std::string(__FUNCTION__) +
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301826 "Call to PIM failed while setting present property for path " +
1827 i_vpdPath);
1828 }
1829 }
1830 catch (const std::exception& l_ex)
1831 {
Sunny Srivastava4c509c22025-03-25 12:43:40 +05301832 EventLogger::createSyncPel(
1833 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
1834 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
1835 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
Sunny Srivastavad159bb42025-01-09 11:13:50 +05301836 }
1837}
1838
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301839void Worker::performVpdRecollection()
1840{
1841 try
1842 {
1843 // Check if system config JSON is present
1844 if (m_parsedJson.empty())
1845 {
1846 throw std::runtime_error(
1847 "System config json object is empty, can't process recollection.");
1848 }
1849
Rekha Aparna88d53302025-09-01 18:16:55 -05001850 uint16_t l_errCode = 0;
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301851 const auto& l_frusReplaceableAtStandby =
Rekha Aparna88d53302025-09-01 18:16:55 -05001852 jsonUtility::getListOfFrusReplaceableAtStandby(m_parsedJson,
1853 l_errCode);
1854
1855 if (l_errCode)
1856 {
1857 logging::logMessage(
1858 "Failed to get list of FRUs replaceable at runtime, error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301859 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna88d53302025-09-01 18:16:55 -05001860 return;
1861 }
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301862
1863 for (const auto& l_fruInventoryPath : l_frusReplaceableAtStandby)
1864 {
1865 // ToDo: Add some logic/trace to know the flow to
1866 // collectSingleFruVpd has been directed via
1867 // performVpdRecollection.
1868 collectSingleFruVpd(l_fruInventoryPath);
1869 }
1870 return;
1871 }
1872
1873 catch (const std::exception& l_ex)
1874 {
1875 // TODO Log PEL
1876 logging::logMessage(
1877 "VPD recollection failed with error: " + std::string(l_ex.what()));
1878 }
1879}
1880
1881void Worker::collectSingleFruVpd(
1882 const sdbusplus::message::object_path& i_dbusObjPath)
1883{
Anupama B R48f297b2025-08-13 04:29:06 -05001884 std::string l_fruPath{};
Rekha Aparna0578dd22025-09-02 08:20:21 -05001885 uint16_t l_errCode = 0;
1886
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301887 try
1888 {
1889 // Check if system config JSON is present
1890 if (m_parsedJson.empty())
1891 {
1892 logging::logMessage(
1893 "System config JSON object not present. Single FRU VPD collection is not performed for " +
1894 std::string(i_dbusObjPath));
1895 return;
1896 }
1897
1898 // Get FRU path for the given D-bus object path from JSON
Rekha Aparna0578dd22025-09-02 08:20:21 -05001899 l_fruPath = jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath,
1900 l_errCode);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301901
1902 if (l_fruPath.empty())
1903 {
Rekha Aparna0578dd22025-09-02 08:20:21 -05001904 if (l_errCode)
1905 {
1906 logging::logMessage(
1907 "Failed to get FRU path for [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301908 std::string(i_dbusObjPath) +
1909 "], error : " + commonUtility::getErrCodeMsg(l_errCode) +
Rekha Aparna0578dd22025-09-02 08:20:21 -05001910 " Aborting single FRU VPD collection.");
1911 return;
1912 }
1913
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301914 logging::logMessage(
1915 "D-bus object path not present in JSON. Single FRU VPD collection is not performed for " +
1916 std::string(i_dbusObjPath));
1917 return;
1918 }
1919
1920 // Check if host is up and running
1921 if (dbusUtility::isHostRunning())
1922 {
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001923 uint16_t l_errCode = 0;
1924 bool isFruReplaceableAtRuntime =
1925 jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath,
1926 l_errCode);
1927
1928 if (l_errCode)
1929 {
1930 logging::logMessage(
1931 "Failed to check if FRU is replaceable at runtime for FRU : [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301932 std::string(i_dbusObjPath) +
1933 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001934 return;
1935 }
1936
1937 if (!isFruReplaceableAtRuntime)
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301938 {
1939 logging::logMessage(
1940 "Given FRU is not replaceable at host runtime. Single FRU VPD collection is not performed for " +
1941 std::string(i_dbusObjPath));
1942 return;
1943 }
1944 }
1945 else if (dbusUtility::isBMCReady())
1946 {
Rekha Aparna40845612025-09-01 19:58:56 -05001947 uint16_t l_errCode = 0;
1948 bool isFruReplaceableAtStandby =
1949 jsonUtility::isFruReplaceableAtStandby(m_parsedJson, l_fruPath,
1950 l_errCode);
1951
1952 if (l_errCode)
1953 {
1954 logging::logMessage(
1955 "Error while checking if FRU is replaceable at standby for FRU [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301956 std::string(i_dbusObjPath) +
1957 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna40845612025-09-01 19:58:56 -05001958 }
1959
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001960 l_errCode = 0;
1961 bool isFruReplaceableAtRuntime =
1962 jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath,
1963 l_errCode);
1964
1965 if (l_errCode)
1966 {
1967 logging::logMessage(
1968 "Failed to check if FRU is replaceable at runtime for FRU : [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +05301969 std::string(i_dbusObjPath) +
1970 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparnaad0db9e2025-09-01 20:29:18 -05001971 return;
1972 }
1973
1974 if (!isFruReplaceableAtStandby && (!isFruReplaceableAtRuntime))
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301975 {
1976 logging::logMessage(
1977 "Given FRU is neither replaceable at standby nor replaceable at runtime. Single FRU VPD collection is not performed for " +
1978 std::string(i_dbusObjPath));
1979 return;
1980 }
1981 }
1982
Anupama B R5cd1b2d2025-08-05 04:57:40 -05001983 // Set collection Status as InProgress. Since it's an intermediate state
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301984 // D-bus set-property call is good enough to update the status.
Anupama B R5cd1b2d2025-08-05 04:57:40 -05001985 const std::string& l_collStatusProp = "Status";
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301986
Anupama B R4c65fcd2025-09-01 08:09:00 -05001987 setCollectionStatusProperty(l_fruPath,
1988 constants::vpdCollectionInProgress);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05301989
1990 // Parse VPD
1991 types::VPDMapVariant l_parsedVpd = parseVpdFile(l_fruPath);
1992
1993 // If l_parsedVpd is pointing to std::monostate
1994 if (l_parsedVpd.index() == 0)
1995 {
1996 throw std::runtime_error(
1997 "VPD parsing failed for " + std::string(i_dbusObjPath));
1998 }
1999
2000 // Get D-bus object map from worker class
2001 types::ObjectMap l_dbusObjectMap;
2002 populateDbus(l_parsedVpd, l_dbusObjectMap, l_fruPath);
2003
2004 if (l_dbusObjectMap.empty())
2005 {
2006 throw std::runtime_error(
2007 "Failed to create D-bus object map. Single FRU VPD collection failed for " +
2008 std::string(i_dbusObjPath));
2009 }
2010
2011 // Call PIM's Notify method
2012 if (!dbusUtility::callPIM(move(l_dbusObjectMap)))
2013 {
2014 throw std::runtime_error(
2015 "Notify PIM failed. Single FRU VPD collection failed for " +
2016 std::string(i_dbusObjPath));
2017 }
Anupama B R4c65fcd2025-09-01 08:09:00 -05002018 setCollectionStatusProperty(l_fruPath,
2019 constants::vpdCollectionCompleted);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302020 }
2021 catch (const std::exception& l_error)
2022 {
Anupama B R48f297b2025-08-13 04:29:06 -05002023 setCollectionStatusProperty(l_fruPath, constants::vpdCollectionFailed);
Sunny Srivastava380efbb2025-04-25 10:28:30 +05302024 // TODO: Log PEL
2025 logging::logMessage(std::string(l_error.what()));
2026 }
2027}
Anupama B R24691d22025-05-21 08:14:15 -05002028
2029void Worker::setCollectionStatusProperty(
2030 const std::string& i_vpdPath, const std::string& i_value) const noexcept
2031{
2032 try
2033 {
2034 if (i_vpdPath.empty())
2035 {
2036 throw std::runtime_error(
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002037 "Given path is empty. Can't set collection Status property");
Anupama B R24691d22025-05-21 08:14:15 -05002038 }
2039
Anupama B R4c65fcd2025-09-01 08:09:00 -05002040 types::PropertyMap l_timeStampMap;
2041 if (i_value == constants::vpdCollectionCompleted ||
2042 i_value == constants::vpdCollectionFailed)
2043 {
2044 l_timeStampMap.emplace(
2045 "CompletedTime",
2046 types::DbusVariantType{
2047 commonUtility::getCurrentTimeSinceEpoch()});
2048 }
2049 else if (i_value == constants::vpdCollectionInProgress)
2050 {
2051 l_timeStampMap.emplace(
2052 "StartTime", types::DbusVariantType{
2053 commonUtility::getCurrentTimeSinceEpoch()});
2054 }
2055 else if (i_value == constants::vpdCollectionNotStarted)
2056 {
2057 l_timeStampMap.emplace("StartTime", 0);
2058 l_timeStampMap.emplace("CompletedTime", 0);
2059 }
2060
Anupama B R24691d22025-05-21 08:14:15 -05002061 types::ObjectMap l_objectInterfaceMap;
2062
2063 if (m_parsedJson["frus"].contains(i_vpdPath))
2064 {
2065 for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath])
2066 {
2067 sdbusplus::message::object_path l_fruObjectPath(
2068 l_Fru["inventoryPath"]);
2069
2070 types::PropertyMap l_propertyValueMap;
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002071 l_propertyValueMap.emplace("Status", i_value);
Anupama B R4c65fcd2025-09-01 08:09:00 -05002072 l_propertyValueMap.insert(l_timeStampMap.begin(),
2073 l_timeStampMap.end());
Anupama B R24691d22025-05-21 08:14:15 -05002074
2075 types::InterfaceMap l_interfaces;
2076 vpdSpecificUtility::insertOrMerge(
2077 l_interfaces, constants::vpdCollectionInterface,
2078 move(l_propertyValueMap));
2079
2080 l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
2081 std::move(l_interfaces));
2082 }
2083 }
2084 else
2085 {
2086 // consider it as an inventory path.
2087 if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0)
2088 {
2089 throw std::runtime_error(
2090 "Invalid inventory path: " + i_vpdPath +
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002091 ". Can't set collection Status property");
Anupama B R24691d22025-05-21 08:14:15 -05002092 }
2093
2094 types::PropertyMap l_propertyValueMap;
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002095 l_propertyValueMap.emplace("Status", i_value);
Anupama B R4c65fcd2025-09-01 08:09:00 -05002096 l_propertyValueMap.insert(l_timeStampMap.begin(),
2097 l_timeStampMap.end());
Anupama B R24691d22025-05-21 08:14:15 -05002098
2099 types::InterfaceMap l_interfaces;
2100 vpdSpecificUtility::insertOrMerge(l_interfaces,
2101 constants::vpdCollectionInterface,
2102 move(l_propertyValueMap));
2103
2104 l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces));
2105 }
2106
2107 // Notify PIM
2108 if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
2109 {
2110 throw DbusException(
2111 std::string(__FUNCTION__) +
Anupama B R5cd1b2d2025-08-05 04:57:40 -05002112 "Call to PIM failed while setting collection Status property for path " +
Anupama B R24691d22025-05-21 08:14:15 -05002113 i_vpdPath);
2114 }
2115 }
2116 catch (const std::exception& l_ex)
2117 {
2118 EventLogger::createSyncPel(
2119 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
2120 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
2121 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
2122 }
2123}
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05002124} // namespace vpd