blob: 30f72941a408d760f5b50c5fc8e0be14dd4f06b4 [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "vpd_tool_impl.hpp"
2
Priyanga Ramasamy38031312021-10-07 16:39:13 -05003#include "impl.hpp"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05004#include "parser_factory.hpp"
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05305#include "vpd_exceptions.hpp"
6
Patrick Williamsc78d8872023-05-10 07:50:56 -05007#include <sdbusplus/bus.hpp>
8
PriyangaRamasamycdf943c2020-03-18 02:25:30 +05309#include <cstdlib>
10#include <filesystem>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053011#include <iostream>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053012#include <variant>
13#include <vector>
14
15using namespace std;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053016using namespace openpower::vpd;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053017using namespace inventory;
18using namespace openpower::vpd::manager::editor;
PriyangaRamasamycdf943c2020-03-18 02:25:30 +053019namespace fs = std::filesystem;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053020using json = nlohmann::json;
21using namespace openpower::vpd::exceptions;
Priyanga Ramasamy38031312021-10-07 16:39:13 -050022using namespace openpower::vpd::parser;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050023using namespace openpower::vpd::parser::factory;
24using namespace openpower::vpd::parser::interface;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053025
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060026bool VpdTool::fileToVector(Binary& data)
27{
28 try
29 {
30 std::ifstream file(value, std::ifstream::in);
31
32 if (file)
33 {
34 std::string line;
35 while (std::getline(file, line))
36 {
37 std::istringstream iss(line);
38 std::string byteStr;
39 while (iss >> std::setw(2) >> std::hex >> byteStr)
40 {
41 uint8_t byte = strtoul(byteStr.c_str(), nullptr, 16);
42 data.emplace(data.end(), byte);
43 }
44 }
45 return true;
46 }
47 else
48 {
49 std::cerr << "Unable to open the given file " << value << std::endl;
50 }
51 }
52 catch (std::exception& e)
53 {
54 std::cerr << e.what();
55 }
56 return false;
57}
58
59bool VpdTool::copyStringToFile(const std::string& input)
60{
61 try
62 {
63 std::ofstream outFile(value, std::ofstream::out);
64
65 if (outFile.is_open())
66 {
67 std::string hexString = input;
68 if (input.substr(0, 2) == "0x")
69 {
70 // truncating prefix 0x
71 hexString = input.substr(2);
72 }
73 outFile.write(hexString.c_str(), hexString.length());
74 }
75 else
76 {
77 std::cerr << "Error opening output file " << value << std::endl;
78 return false;
79 }
80
81 outFile.close();
82 }
83 catch (std::exception& e)
84 {
85 std::cerr << e.what();
86 return false;
87 }
88 return true;
89}
90
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -050091static void
92 getVPDInMap(const std::string& vpdPath,
93 std::unordered_map<std::string, DbusPropertyMap>& vpdMap,
94 json& js, const std::string& invPath)
95{
96 auto jsonToParse = INVENTORY_JSON_DEFAULT;
97 if (fs::exists(INVENTORY_JSON_SYM_LINK))
98 {
99 jsonToParse = INVENTORY_JSON_SYM_LINK;
100 }
101
102 std::ifstream inventoryJson(jsonToParse);
103 if (!inventoryJson)
104 {
105 throw std::runtime_error("VPD JSON file not found");
106 }
107
108 try
109 {
110 js = json::parse(inventoryJson);
111 }
112 catch (const json::parse_error& ex)
113 {
114 throw std::runtime_error("VPD JSON parsing failed");
115 }
116
117 Binary vpdVector{};
118
girik18bb9852022-11-16 05:48:13 -0600119 uint32_t vpdStartOffset = 0;
priyaram0eb8cac2023-09-20 09:16:46 +0530120 vpdVector = getVpdDataInVector(js, vpdPath);
Patrick Williams08dc31c2024-08-16 15:21:06 -0400121 ParserInterface* parser =
122 ParserFactory::getParser(vpdVector, invPath, vpdPath, vpdStartOffset);
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500123 auto parseResult = parser->parse();
124 ParserFactory::freeParser(parser);
125
126 if (auto pVal = std::get_if<Store>(&parseResult))
127 {
128 vpdMap = pVal->getVpdMap();
129 }
130 else
131 {
Patrick Williams08dc31c2024-08-16 15:21:06 -0400132 std::string err =
133 vpdPath + " is not of type IPZ VPD. Unable to parse the VPD.";
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500134 throw std::runtime_error(err);
135 }
136}
137
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530138Binary VpdTool::toBinary(const std::string& value)
139{
140 Binary val{};
141 if (value.find("0x") == string::npos)
142 {
143 val.assign(value.begin(), value.end());
144 }
145 else if (value.find("0x") != string::npos)
146 {
147 stringstream ss;
148 ss.str(value.substr(2));
149 string byteStr{};
150
Priyanga Ramasamyec912e62021-12-15 22:47:51 -0600151 if (value.length() % 2 != 0)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530152 {
Priyanga Ramasamyec912e62021-12-15 22:47:51 -0600153 throw runtime_error(
154 "VPD-TOOL write option accepts 2 digit hex numbers. (Eg. 0x1 "
155 "should be given as 0x01). Aborting the write operation.");
156 }
157
158 if (value.find_first_not_of("0123456789abcdefABCDEF", 2) !=
159 std::string::npos)
160 {
161 throw runtime_error("Provide a valid hexadecimal input.");
162 }
163
164 while (ss >> setw(2) >> byteStr)
165 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530166 uint8_t byte = strtoul(byteStr.c_str(), nullptr, 16);
167
168 val.push_back(byte);
169 }
170 }
171
172 else
173 {
174 throw runtime_error("The value to be updated should be either in ascii "
175 "or in hex. Refer --help option");
176 }
177 return val;
178}
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530179
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600180void VpdTool::printReturnCode(int returnCode)
181{
182 if (returnCode)
183 {
184 cout << "\n Command failed with the return code " << returnCode
185 << ". Continuing the execution. " << endl;
186 }
187}
188
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530189void VpdTool::eraseInventoryPath(string& fru)
190{
191 // Power supply frupath comes with INVENTORY_PATH appended in prefix.
192 // Stripping it off inorder to avoid INVENTORY_PATH duplication
193 // during getVINIProperties() execution.
194 fru.erase(0, sizeof(INVENTORY_PATH) - 1);
195}
196
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530197void VpdTool::debugger(json output)
198{
199 cout << output.dump(4) << '\n';
200}
201
202auto VpdTool::makeDBusCall(const string& objectName, const string& interface,
203 const string& kw)
204{
205 auto bus = sdbusplus::bus::new_default();
206 auto properties =
207 bus.new_method_call(INVENTORY_MANAGER_SERVICE, objectName.c_str(),
208 "org.freedesktop.DBus.Properties", "Get");
209 properties.append(interface);
210 properties.append(kw);
211 auto result = bus.call(properties);
212
213 if (result.is_method_error())
214 {
215 throw runtime_error("Get api failed");
216 }
217 return result;
218}
219
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500220json VpdTool::getVINIProperties(string invPath)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530221{
222 variant<Binary> response;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530223 json kwVal = json::object({});
224
225 vector<string> keyword{"CC", "SN", "PN", "FN", "DR"};
226 string interface = "com.ibm.ipzvpd.VINI";
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530227 string objectName = {};
228
229 if (invPath.find(INVENTORY_PATH) != string::npos)
230 {
231 objectName = invPath;
232 eraseInventoryPath(invPath);
233 }
234 else
235 {
236 objectName = INVENTORY_PATH + invPath;
237 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530238 for (string kw : keyword)
239 {
240 try
241 {
242 makeDBusCall(objectName, interface, kw).read(response);
243
244 if (auto vec = get_if<Binary>(&response))
245 {
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530246 string printableVal = getPrintableValue(*vec);
247 kwVal.emplace(kw, printableVal);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530248 }
249 }
Patrick Williams2eb01762022-07-22 19:26:56 -0500250 catch (const sdbusplus::exception_t& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530251 {
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500252 if (string(e.name()) ==
253 string("org.freedesktop.DBus.Error.UnknownObject"))
254 {
255 kwVal.emplace(invPath, json::object({}));
256 objFound = false;
257 break;
258 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530259 }
260 }
261
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500262 return kwVal;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530263}
264
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500265void VpdTool::getExtraInterfaceProperties(const string& invPath,
266 const string& extraInterface,
267 const json& prop, json& output)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530268{
269 variant<string> response;
270
271 string objectName = INVENTORY_PATH + invPath;
272
273 for (const auto& itProp : prop.items())
274 {
275 string kw = itProp.key();
276 try
277 {
278 makeDBusCall(objectName, extraInterface, kw).read(response);
279
280 if (auto str = get_if<string>(&response))
281 {
282 output.emplace(kw, *str);
283 }
284 }
Patrick Williams2eb01762022-07-22 19:26:56 -0500285 catch (const sdbusplus::exception_t& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530286 {
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500287 if (std::string(e.name()) ==
288 std::string("org.freedesktop.DBus.Error.UnknownObject"))
289 {
290 objFound = false;
291 break;
292 }
293 else if (std::string(e.name()) ==
294 std::string("org.freedesktop.DBus.Error.UnknownProperty"))
295 {
296 output.emplace(kw, "");
297 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530298 }
299 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530300}
301
302json VpdTool::interfaceDecider(json& itemEEPROM)
303{
304 if (itemEEPROM.find("inventoryPath") == itemEEPROM.end())
305 {
306 throw runtime_error("Inventory Path not found");
307 }
308
309 if (itemEEPROM.find("extraInterfaces") == itemEEPROM.end())
310 {
311 throw runtime_error("Extra Interfaces not found");
312 }
313
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500314 json subOutput = json::object({});
Alpana Kumarib6965f12020-06-01 00:32:21 -0500315 fruType = "FRU";
316
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500317 json j;
318 objFound = true;
319 string invPath = itemEEPROM.at("inventoryPath");
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530320
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500321 j = getVINIProperties(invPath);
322
323 if (objFound)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530324 {
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500325 subOutput.insert(j.begin(), j.end());
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530326 json js;
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500327 if (itemEEPROM.find("type") != itemEEPROM.end())
328 {
329 fruType = itemEEPROM.at("type");
330 }
331 js.emplace("TYPE", fruType);
332
333 if (invPath.find("powersupply") != string::npos)
334 {
335 js.emplace("type", POWER_SUPPLY_TYPE_INTERFACE);
336 }
337 else if (invPath.find("fan") != string::npos)
338 {
339 js.emplace("type", FAN_INTERFACE);
340 }
341
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530342 for (const auto& ex : itemEEPROM["extraInterfaces"].items())
343 {
Priyanga Ramasamy051b34f2023-06-28 00:37:43 -0500344 // Properties under Decorator.Asset interface are derived from VINI
345 // keywords. Displaying VINI keywords and skipping Decorator.Asset
346 // interface's properties will avoid duplicate entries in vpd-tool
347 // output.
Priyanga Ramasamy2dfdbdd2023-08-18 10:07:37 +0000348 if (ex.key() == "xyz.openbmc_project.Inventory.Decorator.Asset" &&
349 itemEEPROM["extraInterfaces"].find(constants::kwdVpdInf) !=
350 itemEEPROM["extraInterfaces"].end())
Priyanga Ramasamy051b34f2023-06-28 00:37:43 -0500351 {
352 continue;
353 }
354
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530355 if (!(ex.value().is_null()))
356 {
Priyanga Ramasamydacaa472021-10-07 12:22:41 -0500357 // TODO: Remove this if condition check once inventory json is
358 // updated with xyz location code interface.
359 if (ex.key() == "com.ibm.ipzvpd.Location")
360 {
361 getExtraInterfaceProperties(
362 invPath,
363 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
364 ex.value(), js);
365 }
366 else
367 {
368 getExtraInterfaceProperties(invPath, ex.key(), ex.value(),
369 js);
370 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530371 }
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500372 if ((ex.key().find("Item") != string::npos) &&
373 (ex.value().is_null()))
374 {
375 js.emplace("type", ex.key());
376 }
377 subOutput.insert(js.begin(), js.end());
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530378 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530379 }
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500380 return subOutput;
381}
382
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500383json VpdTool::getPresentPropJson(const std::string& invPath)
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500384{
385 std::variant<bool> response;
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500386 std::string presence = "Unknown";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500387
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500388 try
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500389 {
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500390 makeDBusCall(invPath, "xyz.openbmc_project.Inventory.Item", "Present")
391 .read(response);
392
393 if (auto pVal = get_if<bool>(&response))
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500394 {
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500395 presence = *pVal ? "true" : "false";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500396 }
397 }
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500398 catch (const sdbusplus::exception::SdBusError& e)
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500399 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600400 presence = "Unknown";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500401 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600402
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500403 json js;
404 js.emplace("Present", presence);
405 return js;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530406}
407
408json VpdTool::parseInvJson(const json& jsObject, char flag, string fruPath)
409{
410 json output = json::object({});
411 bool validObject = false;
412
413 if (jsObject.find("frus") == jsObject.end())
414 {
415 throw runtime_error("Frus missing in Inventory json");
416 }
417 else
418 {
419 for (const auto& itemFRUS : jsObject["frus"].items())
420 {
421 for (auto itemEEPROM : itemFRUS.value())
422 {
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500423 json subOutput = json::object({});
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530424 try
425 {
426 if (flag == 'O')
427 {
428 if (itemEEPROM.find("inventoryPath") ==
429 itemEEPROM.end())
430 {
431 throw runtime_error("Inventory Path not found");
432 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530433 else if (itemEEPROM.at("inventoryPath") == fruPath)
434 {
435 validObject = true;
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500436 subOutput = interfaceDecider(itemEEPROM);
437 json presentJs = getPresentPropJson(
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500438 "/xyz/openbmc_project/inventory" + fruPath);
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500439 subOutput.insert(presentJs.begin(),
440 presentJs.end());
441 output.emplace(fruPath, subOutput);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530442 return output;
443 }
444 }
445 else
446 {
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500447 subOutput = interfaceDecider(itemEEPROM);
448 json presentJs = getPresentPropJson(
449 "/xyz/openbmc_project/inventory" +
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500450 string(itemEEPROM.at("inventoryPath")));
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500451 subOutput.insert(presentJs.begin(), presentJs.end());
452 output.emplace(string(itemEEPROM.at("inventoryPath")),
453 subOutput);
454 }
455 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500456 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530457 {
458 cerr << e.what();
459 }
460 }
461 }
462 if ((flag == 'O') && (!validObject))
463 {
464 throw runtime_error(
465 "Invalid object path. Refer --dumpInventory/-i option.");
466 }
467 }
468 return output;
469}
470
471void VpdTool::dumpInventory(const nlohmann::basic_json<>& jsObject)
472{
473 char flag = 'I';
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530474 json output = json::array({});
475 output.emplace_back(parseInvJson(jsObject, flag, ""));
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530476 debugger(output);
477}
478
479void VpdTool::dumpObject(const nlohmann::basic_json<>& jsObject)
480{
481 char flag = 'O';
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530482 json output = json::array({});
Santosh Puranikd5d52bb2020-07-28 15:08:10 +0530483 output.emplace_back(parseInvJson(jsObject, flag, fruPath));
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530484 debugger(output);
485}
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530486
487void VpdTool::readKeyword()
488{
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600489 const std::string& kw = getDbusNameForThisKw(keyword);
490
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530491 string interface = "com.ibm.ipzvpd.";
492 variant<Binary> response;
493
494 try
495 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600496 makeDBusCall(INVENTORY_PATH + fruPath, interface + recordName, kw)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530497 .read(response);
498
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530499 string printableVal{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530500 if (auto vec = get_if<Binary>(&response))
501 {
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530502 printableVal = getPrintableValue(*vec);
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530503 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600504
505 if (!value.empty())
506 {
507 if (copyStringToFile(printableVal))
508 {
509 std::cout << "Value read is saved in the file " << value
510 << std::endl;
511 return;
512 }
513 else
514 {
515 std::cerr << "Error while saving the read value in file. "
516 "Displaying the read value on console"
517 << std::endl;
518 }
519 }
520
521 json output = json::object({});
522 json kwVal = json::object({});
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530523 kwVal.emplace(keyword, printableVal);
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530524
525 output.emplace(fruPath, kwVal);
526
527 debugger(output);
528 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500529 catch (const json::exception& e)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530530 {
GiridhariKrishnan63639102023-03-02 05:55:47 -0600531 std::cout << "Keyword Value: " << keyword << std::endl;
532 std::cout << e.what() << std::endl;
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530533 }
534}
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530535
536int VpdTool::updateKeyword()
537{
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600538 Binary val;
539
540 if (std::filesystem::exists(value))
541 {
542 if (!fileToVector(val))
543 {
544 std::cout << "Keyword " << keyword << " update failed."
545 << std::endl;
546 return 1;
547 }
548 }
549 else
550 {
551 val = toBinary(value);
552 }
553
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530554 auto bus = sdbusplus::bus::new_default();
Patrick Williams08dc31c2024-08-16 15:21:06 -0400555 auto properties =
556 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530557 properties.append(static_cast<sdbusplus::message::object_path>(fruPath));
558 properties.append(recordName);
559 properties.append(keyword);
560 properties.append(val);
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600561
562 // When there is a request to write 10K bytes, there occurs a delay in dbus
563 // call which leads to dbus timeout exception. To avoid such exceptions
564 // increase the timeout period from default 25 seconds to 60 seconds.
565 auto timeoutInMicroSeconds = 60 * 1000000L;
566 auto result = bus.call(properties, timeoutInMicroSeconds);
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530567
568 if (result.is_method_error())
569 {
570 throw runtime_error("Get api failed");
571 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600572 std::cout << "Data updated successfully " << std::endl;
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530573 return 0;
574}
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530575
576void VpdTool::forceReset(const nlohmann::basic_json<>& jsObject)
577{
578 for (const auto& itemFRUS : jsObject["frus"].items())
579 {
580 for (const auto& itemEEPROM : itemFRUS.value().items())
581 {
582 string fru = itemEEPROM.value().at("inventoryPath");
583
584 fs::path fruCachePath = INVENTORY_MANAGER_CACHE;
585 fruCachePath += INVENTORY_PATH;
586 fruCachePath += fru;
587
588 try
589 {
590 for (const auto& it : fs::directory_iterator(fruCachePath))
591 {
592 if (fs::is_regular_file(it.status()))
593 {
594 fs::remove(it);
595 }
596 }
597 }
598 catch (const fs::filesystem_error& e)
Patrick Williamsc78d8872023-05-10 07:50:56 -0500599 {}
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530600 }
601 }
602
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600603 cout.flush();
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530604 string udevRemove = "udevadm trigger -c remove -s \"*nvmem*\" -v";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600605 int returnCode = system(udevRemove.c_str());
606 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530607
608 string invManagerRestart =
609 "systemctl restart xyz.openbmc_project.Inventory.Manager.service";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600610 returnCode = system(invManagerRestart.c_str());
611 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530612
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530613 string sysVpdRestart = "systemctl restart system-vpd.service";
614 returnCode = system(sysVpdRestart.c_str());
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600615 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530616
617 string udevAdd = "udevadm trigger -c add -s \"*nvmem*\" -v";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600618 returnCode = system(udevAdd.c_str());
619 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530620}
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530621
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500622int VpdTool::updateHardware(const uint32_t offset)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530623{
624 int rc = 0;
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600625 Binary val;
626 if (std::filesystem::exists(value))
627 {
628 if (!fileToVector(val))
629 {
630 std::cout << "Keyword " << keyword << " update failed."
631 << std::endl;
632 return 1;
633 }
634 }
635 else
636 {
637 val = toBinary(value);
638 }
639
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530640 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
641 try
642 {
643 auto json = nlohmann::json::parse(inventoryJson);
644 EditorImpl edit(fruPath, json, recordName, keyword);
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500645
646 edit.updateKeyword(val, offset, false);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530647 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500648 catch (const json::parse_error& ex)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530649 {
650 throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK));
651 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600652 std::cout << "Data updated successfully " << std::endl;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530653 return rc;
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500654}
655
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500656void VpdTool::readKwFromHw(const uint32_t& startOffset)
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500657{
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500658 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
659 auto jsonFile = nlohmann::json::parse(inventoryJson);
girikbab2bed2023-05-15 04:00:17 -0500660 std::string inventoryPath;
661
662 if (jsonFile["frus"].contains(fruPath))
663 {
664 uint32_t vpdStartOffset = 0;
665
666 for (const auto& item : jsonFile["frus"][fruPath])
667 {
668 if (item.find("offset") != item.end())
669 {
670 vpdStartOffset = item["offset"];
671 break;
672 }
673 }
674
675 if ((startOffset != vpdStartOffset))
676 {
677 std::cerr << "Invalid offset, please correct the offset" << endl;
678 std::cerr << "Recommended Offset is: " << vpdStartOffset << endl;
679 return;
680 }
681 inventoryPath = jsonFile["frus"][fruPath][0]["inventoryPath"];
682 }
683
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000684 Binary completeVPDFile;
685 fstream vpdFileStream;
686
jinuthomas45d54972023-07-03 04:36:29 -0500687 vpdFileStream.exceptions(std::ifstream::badbit | std::ifstream::failbit);
688 try
689 {
690 vpdFileStream.open(fruPath,
691 std::ios::in | std::ios::out | std::ios::binary);
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000692
693 auto vpdFileSize = std::min(std::filesystem::file_size(fruPath),
694 constants::MAX_VPD_SIZE);
695 if (vpdFileSize == 0)
696 {
697 std::cerr << "File size is 0 for " << fruPath << std::endl;
698 throw std::runtime_error("File size is 0.");
699 }
700
701 completeVPDFile.resize(vpdFileSize);
jinuthomas45d54972023-07-03 04:36:29 -0500702 vpdFileStream.seekg(startOffset, ios_base::cur);
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000703 vpdFileStream.read(reinterpret_cast<char*>(&completeVPDFile[0]),
704 vpdFileSize);
jinuthomas45d54972023-07-03 04:36:29 -0500705 vpdFileStream.clear(std::ios_base::eofbit);
706 }
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000707 catch (const std::system_error& fail)
jinuthomas45d54972023-07-03 04:36:29 -0500708 {
709 std::cerr << "Exception in file handling [" << fruPath
710 << "] error : " << fail.what();
711 std::cerr << "Stream file size = " << vpdFileStream.gcount()
712 << std::endl;
713 throw;
714 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500715
716 if (completeVPDFile.empty())
717 {
718 throw std::runtime_error("Invalid File");
719 }
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500720
girik18bb9852022-11-16 05:48:13 -0600721 Impl obj(completeVPDFile, (constants::pimPath + inventoryPath), fruPath,
girikbab2bed2023-05-15 04:00:17 -0500722 startOffset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500723 std::string keywordVal = obj.readKwFromHw(recordName, keyword);
724
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600725 keywordVal = getPrintableValue(keywordVal);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500726
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600727 if (keywordVal.empty())
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500728 {
729 std::cerr << "The given keyword " << keyword << " or record "
730 << recordName
731 << " or both are not present in the given FRU path "
732 << fruPath << std::endl;
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600733 return;
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500734 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600735
736 if (!value.empty())
737 {
738 if (copyStringToFile(keywordVal))
739 {
740 std::cout << "Value read is saved in the file " << value
741 << std::endl;
742 return;
743 }
744 else
745 {
746 std::cerr
747 << "Error while saving the read value in file. Displaying "
748 "the read value on console"
749 << std::endl;
750 }
751 }
752
753 json output = json::object({});
754 json kwVal = json::object({});
755 kwVal.emplace(keyword, keywordVal);
756 output.emplace(fruPath, kwVal);
757 debugger(output);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500758}
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500759
760void VpdTool::printFixSystemVPDOption(UserOption option)
761{
762 switch (option)
763 {
764 case VpdTool::EXIT:
765 cout << "\nEnter 0 => To exit successfully : ";
766 break;
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600767 case VpdTool::BACKUP_DATA_FOR_ALL:
768 cout << "\n\nEnter 1 => If you choose the data on backup for all "
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500769 "mismatching record-keyword pairs";
770 break;
771 case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600772 cout << "\nEnter 2 => If you choose the data on primary for all "
773 "mismatching record-keyword pairs";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500774 break;
775 case VpdTool::MORE_OPTIONS:
776 cout << "\nEnter 3 => If you wish to explore more options";
777 break;
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600778 case VpdTool::BACKUP_DATA_FOR_CURRENT:
779 cout << "\nEnter 4 => If you choose the data on backup as the "
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500780 "right value";
781 break;
782 case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600783 cout << "\nEnter 5 => If you choose the data on primary as the "
784 "right value";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500785 break;
786 case VpdTool::NEW_VALUE_ON_BOTH:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600787 cout << "\nEnter 6 => If you wish to enter a new value to update "
788 "both on backup and primary";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500789 break;
790 case VpdTool::SKIP_CURRENT:
791 cout << "\nEnter 7 => If you wish to skip the above "
792 "record-keyword pair";
793 break;
794 }
795}
796
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500797void VpdTool::getSystemDataFromCache(IntfPropMap& svpdBusData)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500798{
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500799 const auto vsys = getAllDBusProperty<GetAllResultType>(
800 constants::pimIntf,
801 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
802 "com.ibm.ipzvpd.VSYS");
803 svpdBusData.emplace("VSYS", vsys);
804
805 const auto vcen = getAllDBusProperty<GetAllResultType>(
806 constants::pimIntf,
807 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
808 "com.ibm.ipzvpd.VCEN");
809 svpdBusData.emplace("VCEN", vcen);
810
811 const auto lxr0 = getAllDBusProperty<GetAllResultType>(
812 constants::pimIntf,
813 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
814 "com.ibm.ipzvpd.LXR0");
815 svpdBusData.emplace("LXR0", lxr0);
816
817 const auto util = getAllDBusProperty<GetAllResultType>(
818 constants::pimIntf,
819 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
820 "com.ibm.ipzvpd.UTIL");
821 svpdBusData.emplace("UTIL", util);
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500822}
823
824int VpdTool::fixSystemVPD()
825{
826 std::string outline(191, '=');
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600827 cout << "\nRestorable record-keyword pairs and their data on backup & "
828 "primary.\n\n"
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500829 << outline << std::endl;
830
831 cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600832 << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left
833 << setw(75) << "Data On Primary" << left << setw(14)
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500834 << "Data Mismatch\n"
835 << outline << std::endl;
836
priyaram0eb8cac2023-09-20 09:16:46 +0530837 uint8_t num = 0;
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500838
839 // Get system VPD data in map
840 unordered_map<string, DbusPropertyMap> vpdMap;
841 json js;
842 getVPDInMap(constants::systemVpdFilePath, vpdMap, js,
843 constants::pimPath +
844 static_cast<std::string>(constants::SYSTEM_OBJECT));
845
846 // Get system VPD D-Bus Data in a map
847 IntfPropMap svpdBusData;
848 getSystemDataFromCache(svpdBusData);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500849
850 for (const auto& recordKw : svpdKwdMap)
851 {
852 string record = recordKw.first;
853
854 // Extract specific record data from the svpdBusData map.
855 const auto& rec = svpdBusData.find(record);
856
857 if (rec == svpdBusData.end())
858 {
859 std::cerr << record << " not a part of critical system VPD records."
860 << std::endl;
861 continue;
862 }
863
864 const auto& recData = svpdBusData.find(record)->second;
865
866 string busStr{}, hwValStr{};
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500867
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600868 for (const auto& keywordInfo : recordKw.second)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500869 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600870 const auto& keyword = get<0>(keywordInfo);
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500871 string mismatch = "NO"; // no mismatch
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500872 string hardwareValue{};
873 auto recItr = vpdMap.find(record);
874
875 if (recItr != vpdMap.end())
876 {
877 DbusPropertyMap& kwValMap = recItr->second;
878 auto kwItr = kwValMap.find(keyword);
879 if (kwItr != kwValMap.end())
880 {
881 hardwareValue = kwItr->second;
882 }
883 }
884
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500885 inventory::Value kwValue;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500886 for (auto& kwData : recData)
887 {
888 if (kwData.first == keyword)
889 {
890 kwValue = kwData.second;
891 break;
892 }
893 }
894
GiridhariKrishnan63639102023-03-02 05:55:47 -0600895 if (keyword != "SE") // SE to display in Hex string only
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500896 {
897 ostringstream hwValStream;
898 hwValStream << "0x";
899 hwValStr = hwValStream.str();
900
901 for (uint16_t byte : hardwareValue)
902 {
903 hwValStream << setfill('0') << setw(2) << hex << byte;
904 hwValStr = hwValStream.str();
905 }
906
907 if (const auto value = get_if<Binary>(&kwValue))
908 {
GiridhariKrishnan63639102023-03-02 05:55:47 -0600909 busStr = hexString(*value);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500910 }
911 if (busStr != hwValStr)
912 {
913 mismatch = "YES";
914 }
915 }
916 else
917 {
918 if (const auto value = get_if<Binary>(&kwValue))
919 {
920 busStr = getPrintableValue(*value);
921 }
922 if (busStr != hardwareValue)
923 {
924 mismatch = "YES";
925 }
926 hwValStr = hardwareValue;
927 }
928 recKwData.push_back(
929 make_tuple(++num, record, keyword, busStr, hwValStr, mismatch));
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500930
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500931 std::string splitLine(191, '-');
priyaram0eb8cac2023-09-20 09:16:46 +0530932 cout << left << setw(6) << static_cast<int>(num) << left << setw(8)
933 << record << left << setw(9) << keyword << left << setw(75)
934 << setfill(' ') << busStr << left << setw(75) << setfill(' ')
935 << hwValStr << left << setw(14) << mismatch << '\n'
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500936 << splitLine << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500937 }
938 }
priyaram0eb8cac2023-09-20 09:16:46 +0530939 parseSVPDOptions(js, std::string());
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500940 return 0;
941}
942
priyaram0eb8cac2023-09-20 09:16:46 +0530943void VpdTool::parseSVPDOptions(const nlohmann::json& json,
944 const std::string& backupEEPROMPath)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500945{
946 do
947 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600948 printFixSystemVPDOption(VpdTool::BACKUP_DATA_FOR_ALL);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500949 printFixSystemVPDOption(VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL);
950 printFixSystemVPDOption(VpdTool::MORE_OPTIONS);
951 printFixSystemVPDOption(VpdTool::EXIT);
952
953 int option = 0;
954 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500955
956 std::string outline(191, '=');
957 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500958
959 if (json.find("frus") == json.end())
960 {
961 throw runtime_error("Frus not found in json");
962 }
963
964 bool mismatchFound = false;
965
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600966 if (option == VpdTool::BACKUP_DATA_FOR_ALL)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500967 {
968 for (const auto& data : recKwData)
969 {
970 if (get<5>(data) == "YES")
971 {
972 EditorImpl edit(constants::systemVpdFilePath, json,
973 get<1>(data), get<2>(data));
974 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
975 mismatchFound = true;
976 }
977 }
978
979 if (mismatchFound)
980 {
981 cout << "\nData updated successfully for all mismatching "
982 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600983 "data from backup. Exit successfully.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500984 << endl;
985 }
986 else
987 {
988 cout << "\nNo mismatch found for any of the above mentioned "
989 "record-keyword pair. Exit successfully.\n";
990 }
991
992 exit(0);
993 }
994 else if (option == VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL)
995 {
priyaram0eb8cac2023-09-20 09:16:46 +0530996 std::string hardwarePath = constants::systemVpdFilePath;
997 if (!backupEEPROMPath.empty())
998 {
999 hardwarePath = backupEEPROMPath;
1000 }
1001
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001002 for (const auto& data : recKwData)
1003 {
1004 if (get<5>(data) == "YES")
1005 {
priyaram0eb8cac2023-09-20 09:16:46 +05301006 std::string record = get<1>(data), keyword = get<2>(data);
1007
1008 if (!backupEEPROMPath.empty())
1009 {
1010 getBackupRecordKeyword(record, keyword);
1011 }
1012
1013 EditorImpl edit(hardwarePath, json, record, keyword);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001014 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1015 mismatchFound = true;
1016 }
1017 }
1018
1019 if (mismatchFound)
1020 {
1021 cout << "\nData updated successfully for all mismatching "
1022 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001023 "data from primary VPD.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001024 << endl;
1025 }
1026 else
1027 {
1028 cout << "\nNo mismatch found for any of the above mentioned "
1029 "record-keyword pair. Exit successfully.\n";
1030 }
1031
1032 exit(0);
1033 }
1034 else if (option == VpdTool::MORE_OPTIONS)
1035 {
1036 cout << "\nIterate through all restorable record-keyword pairs\n";
1037
1038 for (const auto& data : recKwData)
1039 {
1040 do
1041 {
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001042 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001043
1044 cout << left << setw(6) << "S.No" << left << setw(8)
1045 << "Record" << left << setw(9) << "Keyword" << left
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001046 << setw(75) << setfill(' ') << "Backup Data" << left
1047 << setw(75) << setfill(' ') << "Primary Data" << left
1048 << setw(14) << "Data Mismatch" << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001049
priyaram0eb8cac2023-09-20 09:16:46 +05301050 cout << left << setw(6) << static_cast<int>(get<0>(data))
1051 << left << setw(8) << get<1>(data) << left << setw(9)
1052 << get<2>(data) << left << setw(75) << setfill(' ')
1053 << get<3>(data) << left << setw(75) << setfill(' ')
1054 << get<4>(data) << left << setw(14) << get<5>(data);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001055
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001056 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001057
1058 if (get<5>(data) == "NO")
1059 {
1060 cout << "\nNo mismatch found.\n";
1061 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1062 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1063 printFixSystemVPDOption(VpdTool::EXIT);
1064 }
1065 else
1066 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001067 printFixSystemVPDOption(
1068 VpdTool::BACKUP_DATA_FOR_CURRENT);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001069 printFixSystemVPDOption(
1070 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT);
1071 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1072 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1073 printFixSystemVPDOption(VpdTool::EXIT);
1074 }
1075
1076 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001077 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001078
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001079 if (option == VpdTool::BACKUP_DATA_FOR_CURRENT)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001080 {
priyaram0eb8cac2023-09-20 09:16:46 +05301081 EditorImpl edit(constants::systemVpdFilePath, json,
1082 get<1>(data), get<2>(data));
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001083 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
1084 cout << "\nData updated successfully.\n";
1085 break;
1086 }
1087 else if (option ==
1088 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT)
1089 {
priyaram0eb8cac2023-09-20 09:16:46 +05301090 std::string hardwarePath = constants::systemVpdFilePath;
1091 std::string record = get<1>(data);
1092 std::string keyword = get<2>(data);
1093
1094 if (!backupEEPROMPath.empty())
1095 {
1096 hardwarePath = backupEEPROMPath;
1097 getBackupRecordKeyword(record, keyword);
1098 }
1099
1100 EditorImpl edit(hardwarePath, json, record, keyword);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001101 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1102 cout << "\nData updated successfully.\n";
1103 break;
1104 }
1105 else if (option == VpdTool::NEW_VALUE_ON_BOTH)
1106 {
1107 string value;
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001108 cout << "\nEnter the new value to update on both "
1109 "primary & backup. Value should be in ASCII or "
1110 "in HEX(prefixed with 0x) : ";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001111 cin >> value;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001112 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001113
priyaram0eb8cac2023-09-20 09:16:46 +05301114 EditorImpl edit(constants::systemVpdFilePath, json,
1115 get<1>(data), get<2>(data));
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001116 edit.updateKeyword(toBinary(value), 0, true);
priyaram0eb8cac2023-09-20 09:16:46 +05301117
1118 if (!backupEEPROMPath.empty())
1119 {
1120 std::string record = get<1>(data);
1121 std::string keyword = get<2>(data);
1122
1123 getBackupRecordKeyword(record, keyword);
1124 EditorImpl edit(backupEEPROMPath, json, record,
1125 keyword);
1126 edit.updateKeyword(toBinary(value), 0, true);
1127 }
1128
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001129 cout << "\nData updated successfully.\n";
1130 break;
1131 }
1132 else if (option == VpdTool::SKIP_CURRENT)
1133 {
1134 cout << "\nSkipped the above record-keyword pair. "
1135 "Continue to the next available pair.\n";
1136 break;
1137 }
1138 else if (option == VpdTool::EXIT)
1139 {
1140 cout << "\nExit successfully\n";
1141 exit(0);
1142 }
1143 else
1144 {
1145 cout << "\nProvide a valid option. Retrying for the "
1146 "current record-keyword pair\n";
1147 }
1148 } while (1);
1149 }
1150 exit(0);
1151 }
1152 else if (option == VpdTool::EXIT)
1153 {
1154 cout << "\nExit successfully";
1155 exit(0);
1156 }
1157 else
1158 {
1159 cout << "\nProvide a valid option. Retry.";
1160 continue;
1161 }
1162
1163 } while (true);
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001164}
1165
1166int VpdTool::cleanSystemVPD()
1167{
1168 try
1169 {
1170 // Get system VPD hardware data in map
1171 unordered_map<string, DbusPropertyMap> vpdMap;
1172 json js;
1173 getVPDInMap(constants::systemVpdFilePath, vpdMap, js,
1174 constants::pimPath +
1175 static_cast<std::string>(constants::SYSTEM_OBJECT));
1176
1177 RecKwValMap kwdsToBeUpdated;
1178
1179 for (auto recordMap : svpdKwdMap)
1180 {
1181 const auto& record = recordMap.first;
1182 std::unordered_map<std::string, Binary> kwDefault;
1183 for (auto keywordMap : recordMap.second)
1184 {
1185 // Skip those keywords which cannot be reset at manufacturing
1186 if (!std::get<3>(keywordMap))
1187 {
1188 continue;
1189 }
1190 const auto& keyword = std::get<0>(keywordMap);
1191
1192 // Get hardware value for this keyword from vpdMap
1193 Binary hardwareValue;
1194
1195 auto recItr = vpdMap.find(record);
1196
1197 if (recItr != vpdMap.end())
1198 {
1199 DbusPropertyMap& kwValMap = recItr->second;
1200 auto kwItr = kwValMap.find(keyword);
1201 if (kwItr != kwValMap.end())
1202 {
1203 hardwareValue = toBinary(kwItr->second);
1204 }
1205 }
1206
1207 // compare hardware value with the keyword's default value
1208 auto defaultValue = std::get<1>(keywordMap);
1209 if (hardwareValue != defaultValue)
1210 {
1211 EditorImpl edit(constants::systemVpdFilePath, js, record,
1212 keyword);
1213 edit.updateKeyword(defaultValue, 0, true);
1214 }
1215 }
1216 }
1217
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -06001218 std::cout << "\n The critical keywords from system backplane VPD has "
1219 "been reset successfully."
1220 << std::endl;
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001221 }
1222 catch (const std::exception& e)
1223 {
1224 std::cerr << e.what();
1225 std::cerr
1226 << "\nManufacturing reset on system vpd keywords is unsuccessful";
1227 }
1228 return 0;
Patrick Williamsc78d8872023-05-10 07:50:56 -05001229}
priyaram0eb8cac2023-09-20 09:16:46 +05301230
1231int VpdTool::fixSystemBackupVPD(const std::string& backupEepromPath,
1232 const std::string& backupInvPath)
1233{
1234 std::string outline(191, '=');
1235 cout << "\nRestorable record-keyword pairs and their data on backup & "
1236 "primary.\n\n"
1237 << outline << std::endl;
1238
1239 cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left
1240 << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left
1241 << setw(75) << "Data On Primary" << left << setw(14)
1242 << "Data Mismatch\n"
1243 << outline << std::endl;
1244
1245 uint8_t num = 0;
1246 // Get system VPD data in map
1247 unordered_map<string, DbusPropertyMap> systemVPDMap;
1248 json js;
1249 getVPDInMap(constants::systemVpdFilePath, systemVPDMap, js,
1250 constants::pimPath +
1251 static_cast<std::string>(constants::SYSTEM_OBJECT));
1252
1253 // Get backup VPD data in map
1254 unordered_map<string, DbusPropertyMap> backupVPDMap;
1255 getVPDInMap(backupEepromPath, backupVPDMap, js,
1256 constants::pimPath + backupInvPath);
1257
1258 for (const auto& recordKw : svpdKwdMap)
1259 {
1260 const std::string& primaryRecord = recordKw.first;
1261
1262 std::string primaryValStr{}, backupValStr{};
1263
1264 for (const auto& keywordInfo : recordKw.second)
1265 {
1266 const auto& primaryKeyword = get<0>(keywordInfo);
1267 const auto& bkRecord = get<4>(keywordInfo);
1268 const auto& bkKeyword = get<5>(keywordInfo);
1269 string mismatch = "NO";
1270 string primaryValue{};
1271 string backupValue{};
1272
1273 // Find keyword value for system VPD (primary VPD)
1274 auto primaryRecItr = systemVPDMap.find(primaryRecord);
1275 if (primaryRecItr != systemVPDMap.end())
1276 {
1277 DbusPropertyMap& primaryKwValMap = primaryRecItr->second;
1278 auto kwItr = primaryKwValMap.find(primaryKeyword);
1279 if (kwItr != primaryKwValMap.end())
1280 {
1281 primaryValue = kwItr->second;
1282 }
1283 }
1284
1285 // Find keyword value for backup VPD
1286 auto bkRecItr = backupVPDMap.find(bkRecord);
1287 if (bkRecItr != backupVPDMap.end())
1288 {
1289 DbusPropertyMap& bkKwValMap = bkRecItr->second;
1290 auto kwItr = bkKwValMap.find(bkKeyword);
1291 if (kwItr != bkKwValMap.end())
1292 {
1293 backupValue = kwItr->second;
1294 }
1295 }
1296
1297 // SE to display in hex string only
1298 if (primaryKeyword != "SE")
1299 {
1300 ostringstream hwValStream;
1301 hwValStream << "0x";
1302 primaryValStr = hwValStream.str();
1303
1304 for (uint16_t byte : primaryValue)
1305 {
1306 hwValStream << setfill('0') << setw(2) << hex << byte;
1307 primaryValStr = hwValStream.str();
1308 }
1309
1310 hwValStream.str(std::string());
1311 hwValStream << "0x";
1312 backupValStr = hwValStream.str();
1313
1314 for (uint16_t byte : backupValue)
1315 {
1316 hwValStream << setfill('0') << setw(2) << hex << byte;
1317 backupValStr = hwValStream.str();
1318 }
1319 if (primaryValStr != backupValStr)
1320 {
1321 mismatch = "YES";
1322 }
1323 }
1324 else
1325 {
1326 if (primaryValue != backupValue)
1327 {
1328 mismatch = "YES";
1329 }
1330
1331 primaryValStr = primaryValue;
1332 backupValStr = backupValue;
1333 }
1334
Patrick Williams08dc31c2024-08-16 15:21:06 -04001335 recKwData.push_back(
1336 make_tuple(++num, primaryRecord, primaryKeyword, backupValStr,
1337 primaryValStr, mismatch));
priyaram0eb8cac2023-09-20 09:16:46 +05301338
1339 std::string splitLine(191, '-');
1340 cout << left << setw(6) << static_cast<int>(num) << left << setw(8)
1341 << primaryRecord << left << setw(9) << primaryKeyword << left
1342 << setw(75) << setfill(' ') << backupValStr << left << setw(75)
1343 << setfill(' ') << primaryValStr << left << setw(14)
1344 << mismatch << '\n'
1345 << splitLine << endl;
1346 }
1347 }
1348
1349 parseSVPDOptions(js, backupEepromPath);
1350 return 0;
1351}