forked from openbmc/openpower-vpd-parser
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathworker.cpp
1687 lines (1475 loc) · 55.6 KB
/
worker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include "worker.hpp"
#include "backup_restore.hpp"
#include "configuration.hpp"
#include "constants.hpp"
#include "exceptions.hpp"
#include "logger.hpp"
#include "parser.hpp"
#include "parser_factory.hpp"
#include "parser_interface.hpp"
#include <utility/dbus_utility.hpp>
#include <utility/json_utility.hpp>
#include <utility/vpd_specific_utility.hpp>
#include <filesystem>
#include <fstream>
#include <future>
#include <typeindex>
#include <unordered_set>
namespace vpd
{
Worker::Worker(std::string pathToConfigJson) :
m_configJsonPath(pathToConfigJson)
{
// Implies the processing is based on some config JSON
if (!m_configJsonPath.empty())
{
// Check if symlink is already there to confirm fresh boot/factory
// reset.
if (std::filesystem::exists(INVENTORY_JSON_SYM_LINK))
{
logging::logMessage("Sym Link already present");
m_configJsonPath = INVENTORY_JSON_SYM_LINK;
m_isSymlinkPresent = true;
}
try
{
m_parsedJson = jsonUtility::getParsedJson(m_configJsonPath);
// check for mandatory fields at this point itself.
if (!m_parsedJson.contains("frus"))
{
throw std::runtime_error("Mandatory tag(s) missing from JSON");
}
}
catch (const std::exception& ex)
{
throw(JsonException(ex.what(), m_configJsonPath));
}
}
else
{
logging::logMessage("Processing in not based on any config JSON");
}
}
void Worker::enableMuxChips()
{
if (m_parsedJson.empty())
{
// config JSON should not be empty at this point of execution.
throw std::runtime_error("Config JSON is empty. Can't enable muxes");
return;
}
if (!m_parsedJson.contains("muxes"))
{
logging::logMessage("No mux defined for the system in config JSON");
return;
}
// iterate over each MUX detail and enable them.
for (const auto& item : m_parsedJson["muxes"])
{
if (item.contains("holdidlepath"))
{
std::string cmd = "echo 0 > ";
cmd += item["holdidlepath"];
logging::logMessage("Enabling mux with command = " + cmd);
commonUtility::executeCmd(cmd);
continue;
}
logging::logMessage(
"Mux Entry does not have hold idle path. Can't enable the mux");
}
}
#ifdef IBM_SYSTEM
void Worker::primeSystemBlueprint()
{
if (m_parsedJson.empty())
{
return;
}
const nlohmann::json& l_listOfFrus =
m_parsedJson["frus"].get_ref<const nlohmann::json::object_t&>();
for (const auto& l_itemFRUS : l_listOfFrus.items())
{
const std::string& l_vpdFilePath = l_itemFRUS.key();
if (l_vpdFilePath == SYSTEM_VPD_FILE_PATH)
{
continue;
}
// Prime the inventry for FRUs which
// are not present/processing had some error.
if (!primeInventory(l_vpdFilePath))
{
logging::logMessage("Priming of inventory failed for FRU " +
l_vpdFilePath);
}
}
}
void Worker::performInitialSetup()
{
try
{
if (!dbusUtility::isChassisPowerOn())
{
logging::logMessage("Chassis is in Off state.");
setDeviceTreeAndJson();
primeSystemBlueprint();
}
// Enable all mux which are used for connecting to the i2c on the
// pcie slots for pcie cards. These are not enabled by kernel due to
// an issue seen with Castello cards, where the i2c line hangs on a
// probe.
enableMuxChips();
// Nothing needs to be done. Service restarted or BMC re-booted for
// some reason at system power on.
return;
}
catch (const std::exception& ex)
{
if (typeid(ex) == std::type_index(typeid(DataException)))
{
// TODO:Catch logic to be implemented once PEL code goes in.
}
else if (typeid(ex) == std::type_index(typeid(EccException)))
{
// TODO:Catch logic to be implemented once PEL code goes in.
}
else if (typeid(ex) == std::type_index(typeid(JsonException)))
{
// TODO:Catch logic to be implemented once PEL code goes in.
}
logging::logMessage(ex.what());
throw;
}
}
#endif
static std::string readFitConfigValue()
{
std::vector<std::string> output =
commonUtility::executeCmd("/sbin/fw_printenv");
std::string fitConfigValue;
for (const auto& entry : output)
{
auto pos = entry.find("=");
auto key = entry.substr(0, pos);
if (key != "fitconfig")
{
continue;
}
if (pos + 1 < entry.size())
{
fitConfigValue = entry.substr(pos + 1);
}
}
return fitConfigValue;
}
bool Worker::isSystemVPDOnDBus() const
{
const std::string& mboardPath =
m_parsedJson["frus"][SYSTEM_VPD_FILE_PATH].at(0).value("inventoryPath",
"");
if (mboardPath.empty())
{
throw JsonException("System vpd file path missing in JSON",
INVENTORY_JSON_SYM_LINK);
}
std::array<const char*, 1> interfaces = {
"xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
const types::MapperGetObject& objectMap =
dbusUtility::getObjectMap(mboardPath, interfaces);
if (objectMap.empty())
{
return false;
}
return true;
}
std::string Worker::getIMValue(const types::IPZVpdMap& parsedVpd) const
{
if (parsedVpd.empty())
{
throw std::runtime_error("Empty VPD map. Can't Extract IM value");
}
const auto& itrToVSBP = parsedVpd.find("VSBP");
if (itrToVSBP == parsedVpd.end())
{
throw DataException("VSBP record missing.");
}
const auto& itrToIM = (itrToVSBP->second).find("IM");
if (itrToIM == (itrToVSBP->second).end())
{
throw DataException("IM keyword missing.");
}
types::BinaryVector imVal;
std::copy(itrToIM->second.begin(), itrToIM->second.end(),
back_inserter(imVal));
std::ostringstream imData;
for (auto& aByte : imVal)
{
imData << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<int>(aByte);
}
return imData.str();
}
std::string Worker::getHWVersion(const types::IPZVpdMap& parsedVpd) const
{
if (parsedVpd.empty())
{
throw std::runtime_error("Empty VPD map. Can't Extract IM value");
}
const auto& itrToVINI = parsedVpd.find("VINI");
if (itrToVINI == parsedVpd.end())
{
throw DataException("VINI record missing.");
}
const auto& itrToHW = (itrToVINI->second).find("HW");
if (itrToHW == (itrToVINI->second).end())
{
throw DataException("HW keyword missing.");
}
types::BinaryVector hwVal;
std::copy(itrToHW->second.begin(), itrToHW->second.end(),
back_inserter(hwVal));
// The planar pass only comes from the LSB of the HW keyword,
// where as the MSB is used for other purposes such as signifying clock
// termination.
hwVal[0] = 0x00;
std::ostringstream hwString;
for (auto& aByte : hwVal)
{
hwString << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<int>(aByte);
}
return hwString.str();
}
void Worker::fillVPDMap(const std::string& vpdFilePath,
types::VPDMapVariant& vpdMap)
{
logging::logMessage(std::string("Parsing file = ") + vpdFilePath);
if (vpdFilePath.empty())
{
throw std::runtime_error("Invalid file path passed to fillVPDMap API.");
}
if (!std::filesystem::exists(vpdFilePath))
{
throw std::runtime_error("Can't Find physical file");
}
try
{
std::shared_ptr<Parser> vpdParser =
std::make_shared<Parser>(vpdFilePath, m_parsedJson);
vpdMap = vpdParser->parse();
}
catch (const std::exception& ex)
{
if (typeid(ex) == std::type_index(typeid(DataException)))
{
// TODO: Do what needs to be done in case of Data exception.
// Uncomment when PEL implementation goes in.
/* string errorMsg =
"VPD file is either empty or invalid. Parser failed for [";
errorMsg += m_vpdFilePath;
errorMsg += "], with error = " + std::string(ex.what());
additionalData.emplace("DESCRIPTION", errorMsg);
additionalData.emplace("CALLOUT_INVENTORY_PATH",
INVENTORY_PATH + baseFruInventoryPath);
createPEL(additionalData, pelSeverity, errIntfForInvalidVPD,
nullptr);*/
// throw generic error from here to inform main caller about
// failure.
logging::logMessage(ex.what());
throw std::runtime_error(
"Data Exception occurred for file path = " + vpdFilePath);
}
if (typeid(ex) == std::type_index(typeid(EccException)))
{
// TODO: Do what needs to be done in case of ECC exception.
// Uncomment when PEL implementation goes in.
/* additionalData.emplace("DESCRIPTION", "ECC check failed");
additionalData.emplace("CALLOUT_INVENTORY_PATH",
INVENTORY_PATH + baseFruInventoryPath);
createPEL(additionalData, pelSeverity, errIntfForEccCheckFail,
nullptr);
*/
logging::logMessage(ex.what());
// Need to decide once all error handling is implemented.
// vpdSpecificUtility::dumpBadVpd(vpdFilePath,vpdVector);
// throw generic error from here to inform main caller about
// failure.
throw std::runtime_error("Ecc Exception occurred for file path = " +
vpdFilePath);
}
}
}
void Worker::getSystemJson(std::string& systemJson,
const types::VPDMapVariant& parsedVpdMap)
{
if (auto pVal = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
std::string hwKWdValue = getHWVersion(*pVal);
if (hwKWdValue.empty())
{
throw DataException("HW value fetched is empty.");
}
const std::string& imKwdValue = getIMValue(*pVal);
if (imKwdValue.empty())
{
throw DataException("IM value fetched is empty.");
}
auto itrToIM = config::systemType.find(imKwdValue);
if (itrToIM == config::systemType.end())
{
throw DataException("IM keyword does not map to any system type");
}
const types::HWVerList hwVersionList = itrToIM->second.second;
if (!hwVersionList.empty())
{
transform(hwKWdValue.begin(), hwKWdValue.end(), hwKWdValue.begin(),
::toupper);
auto itrToHW = std::find_if(hwVersionList.begin(),
hwVersionList.end(),
[&hwKWdValue](const auto& aPair) {
return aPair.first == hwKWdValue;
});
if (itrToHW != hwVersionList.end())
{
if (!(*itrToHW).second.empty())
{
systemJson += (*itrToIM).first + "_" + (*itrToHW).second +
".json";
}
else
{
systemJson += (*itrToIM).first + ".json";
}
return;
}
}
systemJson += itrToIM->second.first + ".json";
return;
}
throw DataException("Invalid VPD type returned from Parser");
}
static void setEnvAndReboot(const std::string& key, const std::string& value)
{
// set env and reboot and break.
commonUtility::executeCmd("/sbin/fw_setenv", key, value);
logging::logMessage("Rebooting BMC to pick up new device tree");
// make dbus call to reboot
auto bus = sdbusplus::bus::new_default_system();
auto method = bus.new_method_call(
"org.freedesktop.systemd1", "/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager", "Reboot");
bus.call_noreply(method);
}
void Worker::setJsonSymbolicLink(const std::string& i_systemJson)
{
std::error_code l_ec;
l_ec.clear();
if (!std::filesystem::exists(VPD_SYMLIMK_PATH, l_ec))
{
if (l_ec)
{
throw std::runtime_error(
"File system call to exist failed with error = " +
l_ec.message());
}
// implies it is a fresh boot/factory reset.
// Create the directory for hosting the symlink
if (!std::filesystem::create_directories(VPD_SYMLIMK_PATH, l_ec))
{
if (l_ec)
{
throw std::runtime_error(
"File system call to create directory failed with error = " +
l_ec.message());
}
}
}
// create a new symlink based on the system
std::filesystem::create_symlink(i_systemJson, INVENTORY_JSON_SYM_LINK,
l_ec);
if (l_ec)
{
throw std::runtime_error(
"create_symlink system call failed with error: " + l_ec.message());
}
// If the flow is at this point implies the symlink was not present there.
// Considering this as factory reset.
m_isFactoryResetDone = true;
}
void Worker::setDeviceTreeAndJson()
{
// JSON is madatory for processing of this API.
if (m_parsedJson.empty())
{
throw std::runtime_error("JSON is empty");
}
types::VPDMapVariant parsedVpdMap;
fillVPDMap(SYSTEM_VPD_FILE_PATH, parsedVpdMap);
// Implies it is default JSON.
std::string systemJson{JSON_ABSOLUTE_PATH_PREFIX};
// ToDo: Need to check if INVENTORY_JSON_SYM_LINK pointing to correct system
// This is required to support movement from rainier to Blue Ridge on the
// fly.
// Do we have the entry for device tree in parsed JSON?
if (m_parsedJson.find("devTree") == m_parsedJson.end())
{
getSystemJson(systemJson, parsedVpdMap);
if (!systemJson.compare(JSON_ABSOLUTE_PATH_PREFIX))
{
// TODO: Log a PEL saying that "System type not supported"
throw DataException("Error in getting system JSON.");
}
// re-parse the JSON once appropriate JSON has been selected.
try
{
m_parsedJson = jsonUtility::getParsedJson(systemJson);
}
catch (const nlohmann::json::parse_error& ex)
{
throw(JsonException("Json parsing failed", systemJson));
}
}
std::string devTreeFromJson;
if (m_parsedJson.contains("devTree"))
{
devTreeFromJson = m_parsedJson["devTree"];
if (devTreeFromJson.empty())
{
// TODO:: Log a predictive PEL
logging::logMessage(
"Mandatory value for device tree missing from JSON[" +
std::string(INVENTORY_JSON_SYM_LINK) + "]");
}
}
auto fitConfigVal = readFitConfigValue();
if (devTreeFromJson.empty() ||
fitConfigVal.find(devTreeFromJson) != std::string::npos)
{ // Skipping setting device tree as either devtree info is missing from
// Json or it is rightly set.
// avoid setting symlink on every reboot.
if (!m_isSymlinkPresent)
{
setJsonSymbolicLink(systemJson);
}
if (isSystemVPDOnDBus() &&
jsonUtility::isBackupAndRestoreRequired(m_parsedJson))
{
performBackupAndRestore(parsedVpdMap);
}
// proceed to publish system VPD.
publishSystemVPD(parsedVpdMap);
return;
}
setEnvAndReboot("fitconfig", devTreeFromJson);
exit(EXIT_SUCCESS);
}
void Worker::populateIPZVPDpropertyMap(
types::InterfaceMap& interfacePropMap,
const types::IPZKwdValueMap& keyordValueMap,
const std::string& interfaceName)
{
types::PropertyMap propertyValueMap;
for (const auto& kwdVal : keyordValueMap)
{
auto kwd = kwdVal.first;
if (kwd[0] == '#')
{
kwd = std::string("PD_") + kwd[1];
}
else if (isdigit(kwd[0]))
{
kwd = std::string("N_") + kwd;
}
types::BinaryVector value(kwdVal.second.begin(), kwdVal.second.end());
propertyValueMap.emplace(move(kwd), move(value));
}
if (!propertyValueMap.empty())
{
interfacePropMap.emplace(interfaceName, propertyValueMap);
}
}
void Worker::populateKwdVPDpropertyMap(const types::KeywordVpdMap& keyordVPDMap,
types::InterfaceMap& interfaceMap)
{
for (const auto& kwdValMap : keyordVPDMap)
{
types::PropertyMap propertyValueMap;
auto kwd = kwdValMap.first;
if (kwd[0] == '#')
{
kwd = std::string("PD_") + kwd[1];
}
else if (isdigit(kwd[0]))
{
kwd = std::string("N_") + kwd;
}
if (auto keywordValue = get_if<types::BinaryVector>(&kwdValMap.second))
{
types::BinaryVector value((*keywordValue).begin(),
(*keywordValue).end());
propertyValueMap.emplace(move(kwd), move(value));
}
else if (auto keywordValue = get_if<std::string>(&kwdValMap.second))
{
types::BinaryVector value((*keywordValue).begin(),
(*keywordValue).end());
propertyValueMap.emplace(move(kwd), move(value));
}
else if (auto keywordValue = get_if<size_t>(&kwdValMap.second))
{
if (kwd == "MemorySizeInKB")
{
types::PropertyMap memProp;
memProp.emplace(move(kwd), ((*keywordValue)));
interfaceMap.emplace("xyz.openbmc_project.Inventory.Item.Dimm",
move(memProp));
continue;
}
else
{
logging::logMessage("Unknown Keyword =" + kwd +
" found in keyword VPD map");
continue;
}
}
else
{
logging::logMessage(
"Unknown variant type found in keyword VPD map.");
continue;
}
if (!propertyValueMap.empty())
{
vpdSpecificUtility::insertOrMerge(
interfaceMap, constants::kwdVpdInf, move(propertyValueMap));
}
}
}
void Worker::populateInterfaces(const nlohmann::json& interfaceJson,
types::InterfaceMap& interfaceMap,
const types::VPDMapVariant& parsedVpdMap)
{
for (const auto& interfacesPropPair : interfaceJson.items())
{
const std::string& interface = interfacesPropPair.key();
types::PropertyMap propertyMap;
for (const auto& propValuePair : interfacesPropPair.value().items())
{
const std::string property = propValuePair.key();
if (propValuePair.value().is_boolean())
{
propertyMap.emplace(property,
propValuePair.value().get<bool>());
}
else if (propValuePair.value().is_string())
{
if (property.compare("LocationCode") == 0 &&
interface.compare("com.ibm.ipzvpd.Location") == 0)
{
std::string value =
vpdSpecificUtility::getExpandedLocationCode(
propValuePair.value().get<std::string>(),
parsedVpdMap);
propertyMap.emplace(property, value);
auto l_locCodeProperty = propertyMap;
vpdSpecificUtility::insertOrMerge(
interfaceMap,
std::string(constants::xyzLocationCodeInf),
move(l_locCodeProperty));
}
else
{
propertyMap.emplace(
property, propValuePair.value().get<std::string>());
}
}
else if (propValuePair.value().is_array())
{
try
{
propertyMap.emplace(
property,
propValuePair.value().get<types::BinaryVector>());
}
catch (const nlohmann::detail::type_error& e)
{
std::cerr << "Type exception: " << e.what() << "\n";
}
}
else if (propValuePair.value().is_number())
{
// For now assume the value is a size_t. In the future it would
// be nice to come up with a way to get the type from the JSON.
propertyMap.emplace(property,
propValuePair.value().get<size_t>());
}
else if (propValuePair.value().is_object())
{
const std::string& record =
propValuePair.value().value("recordName", "");
const std::string& keyword =
propValuePair.value().value("keywordName", "");
const std::string& encoding =
propValuePair.value().value("encoding", "");
if (auto ipzVpdMap =
std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
if (!record.empty() && !keyword.empty() &&
(*ipzVpdMap).count(record) &&
(*ipzVpdMap).at(record).count(keyword))
{
auto encoded = vpdSpecificUtility::encodeKeyword(
((*ipzVpdMap).at(record).at(keyword)), encoding);
propertyMap.emplace(property, encoded);
}
}
else if (auto kwdVpdMap =
std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
{
if (!keyword.empty() && (*kwdVpdMap).count(keyword))
{
if (auto kwValue = std::get_if<types::BinaryVector>(
&(*kwdVpdMap).at(keyword)))
{
auto encodedValue =
vpdSpecificUtility::encodeKeyword(
std::string((*kwValue).begin(),
(*kwValue).end()),
encoding);
propertyMap.emplace(property, encodedValue);
}
else if (auto kwValue = std::get_if<std::string>(
&(*kwdVpdMap).at(keyword)))
{
auto encodedValue =
vpdSpecificUtility::encodeKeyword(
std::string((*kwValue).begin(),
(*kwValue).end()),
encoding);
propertyMap.emplace(property, encodedValue);
}
else if (auto uintValue = std::get_if<size_t>(
&(*kwdVpdMap).at(keyword)))
{
propertyMap.emplace(property, *uintValue);
}
else
{
logging::logMessage(
"Unknown keyword found, Keywrod = " + keyword);
}
}
}
}
}
vpdSpecificUtility::insertOrMerge(interfaceMap, interface,
move(propertyMap));
}
}
bool Worker::isCPUIOGoodOnly(const std::string& i_pgKeyword)
{
const unsigned char l_io[] = {
0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF,
0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
// EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
// Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
// value equals 0xE7F9FF, then the cpu has no good cores and its treated as
// IO.
if (memcmp(l_io, i_pgKeyword.data() + constants::INDEX_OF_EQ0_IN_PG,
constants::SIZE_OF_8EQ_IN_PG) == 0)
{
return true;
}
// The CPU is not an IO
return false;
}
bool Worker::primeInventory(const std::string& i_vpdFilePath)
{
if (i_vpdFilePath.empty())
{
logging::logMessage("Empty VPD file path given");
return false;
}
if (m_parsedJson.empty())
{
logging::logMessage("Empty JSON detected for " + i_vpdFilePath);
return false;
}
else if (!m_parsedJson["frus"].contains(i_vpdFilePath))
{
logging::logMessage("File " + i_vpdFilePath +
", is not found in the system config JSON file.");
return false;
}
types::ObjectMap l_objectInterfaceMap;
for (const auto& l_Fru : m_parsedJson["frus"][i_vpdFilePath])
{
types::InterfaceMap l_interfaces;
sdbusplus::message::object_path l_fruObjectPath(l_Fru["inventoryPath"]);
if (l_Fru.contains("ccin"))
{
continue;
}
if (l_Fru.contains("noprime") && l_Fru.value("noprime", false))
{
continue;
}
// Clear data under PIM if already exists.
vpdSpecificUtility::resetDataUnderPIM(
std::string(l_Fru["inventoryPath"]), l_interfaces);
// Add extra interfaces mentioned in the Json config file
if (l_Fru.contains("extraInterfaces"))
{
populateInterfaces(l_Fru["extraInterfaces"], l_interfaces,
std::monostate{});
}
types::PropertyMap l_propertyValueMap;
l_propertyValueMap.emplace("Present", false);
if (std::filesystem::exists(i_vpdFilePath))
{
l_propertyValueMap["Present"] = true;
}
vpdSpecificUtility::insertOrMerge(l_interfaces,
"xyz.openbmc_project.Inventory.Item",
move(l_propertyValueMap));
if (l_Fru.value("inherit", true) &&
m_parsedJson.contains("commonInterfaces"))
{
populateInterfaces(m_parsedJson["commonInterfaces"], l_interfaces,
std::monostate{});
}
processFunctionalProperty(l_Fru["inventoryPath"], l_interfaces);
processEnabledProperty(l_Fru["inventoryPath"], l_interfaces);
l_objectInterfaceMap.emplace(std::move(l_fruObjectPath),
std::move(l_interfaces));
}
// Notify PIM
if (!dbusUtility::callPIM(move(l_objectInterfaceMap)))
{
logging::logMessage("Call to PIM failed for VPD file " + i_vpdFilePath);
return false;
}
return true;
}
void Worker::processEmbeddedAndSynthesizedFrus(const nlohmann::json& singleFru,
types::InterfaceMap& interfaces)
{
// embedded property(true or false) says whether the subfru is embedded
// into the parent fru (or) not. VPD sets Present property only for
// embedded frus. If the subfru is not an embedded FRU, the subfru may
// or may not be physically present. Those non embedded frus will always
// have Present=false irrespective of its physical presence or absence.
// Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
// Present to true for such sub frus.
// Eg: ethernet port is embedded into bmc card. So set Present to true
// for such sub frus. Also donot populate present property for embedded
// subfru which is synthesized. Currently there is no subfru which are
// both embedded and synthesized. But still the case is handled here.
// Check if its required to handle presence for this FRU.
if (singleFru.value("handlePresence", true))
{
types::PropertyMap presProp;
presProp.emplace("Present", true);
vpdSpecificUtility::insertOrMerge(
interfaces, "xyz.openbmc_project.Inventory.Item", move(presProp));
}
}
void Worker::processExtraInterfaces(const nlohmann::json& singleFru,
types::InterfaceMap& interfaces,
const types::VPDMapVariant& parsedVpdMap)
{
populateInterfaces(singleFru["extraInterfaces"], interfaces, parsedVpdMap);
if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
if (singleFru["extraInterfaces"].contains(
"xyz.openbmc_project.Inventory.Item.Cpu"))
{
auto itrToRec = (*ipzVpdMap).find("CP00");
if (itrToRec == (*ipzVpdMap).end())
{
return;
}
std::string pgKeywordValue;
vpdSpecificUtility::getKwVal(itrToRec->second, "PG",
pgKeywordValue);
if (!pgKeywordValue.empty())
{
if (isCPUIOGoodOnly(pgKeywordValue))
{
interfaces["xyz.openbmc_project.Inventory.Item"]
["PrettyName"] = "IO Module";
}
}
}
}
}
void Worker::processCopyRecordFlag(const nlohmann::json& singleFru,
const types::VPDMapVariant& parsedVpdMap,
types::InterfaceMap& interfaces)
{
if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
for (const auto& record : singleFru["copyRecords"])
{
const std::string& recordName = record;
if ((*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
{
populateIPZVPDpropertyMap(interfaces,
(*ipzVpdMap).at(recordName),
constants::ipzVpdInf + recordName);
}
}
}
}
void Worker::processInheritFlag(const types::VPDMapVariant& parsedVpdMap,
types::InterfaceMap& interfaces)
{
if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
for (const auto& [recordName, kwdValueMap] : *ipzVpdMap)
{
populateIPZVPDpropertyMap(interfaces, kwdValueMap,
constants::ipzVpdInf + recordName);
}
}
else if (auto kwdVpdMap = std::get_if<types::KeywordVpdMap>(&parsedVpdMap))
{
populateKwdVPDpropertyMap(*kwdVpdMap, interfaces);
}
if (m_parsedJson.contains("commonInterfaces"))
{
populateInterfaces(m_parsedJson["commonInterfaces"], interfaces,
parsedVpdMap);
}
}
bool Worker::processFruWithCCIN(const nlohmann::json& singleFru,
const types::VPDMapVariant& parsedVpdMap)
{
if (auto ipzVPDMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
auto itrToRec = (*ipzVPDMap).find("VINI");
if (itrToRec == (*ipzVPDMap).end())
{
return false;
}
std::string ccinFromVpd;
vpdSpecificUtility::getKwVal(itrToRec->second, "CC", ccinFromVpd);
if (ccinFromVpd.empty())
{
return false;
}
transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
::toupper);
std::vector<std::string> ccinList;
for (std::string ccin : singleFru["ccin"])
{
transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
ccinList.push_back(ccin);
}
if (ccinList.empty())
{
return false;
}