blob: 8b96641611a78d0a8ff008122f86884115e48c18 [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;
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500120 vpdVector = getVpdDataInVector(js, constants::systemVpdFilePath);
girik18bb9852022-11-16 05:48:13 -0600121 ParserInterface* parser = ParserFactory::getParser(
122 vpdVector, invPath, constants::systemVpdFilePath, 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 Williamsc78d8872023-05-10 07:50:56 -0500132 std::string err = vpdPath +
133 " 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 Williamsc78d8872023-05-10 07:50:56 -0500555 auto properties = bus.new_method_call(BUSNAME, OBJPATH, IFACE,
556 "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
837 int num = 0;
838
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, '-');
932 cout << left << setw(6) << num << left << setw(8) << record << left
933 << setw(9) << keyword << left << setw(75) << setfill(' ')
934 << busStr << left << setw(75) << setfill(' ') << hwValStr
935 << left << setw(14) << mismatch << '\n'
936 << splitLine << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500937 }
938 }
939 parseSVPDOptions(js);
940 return 0;
941}
942
943void VpdTool::parseSVPDOptions(const nlohmann::json& json)
944{
945 do
946 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600947 printFixSystemVPDOption(VpdTool::BACKUP_DATA_FOR_ALL);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500948 printFixSystemVPDOption(VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL);
949 printFixSystemVPDOption(VpdTool::MORE_OPTIONS);
950 printFixSystemVPDOption(VpdTool::EXIT);
951
952 int option = 0;
953 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500954
955 std::string outline(191, '=');
956 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500957
958 if (json.find("frus") == json.end())
959 {
960 throw runtime_error("Frus not found in json");
961 }
962
963 bool mismatchFound = false;
964
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600965 if (option == VpdTool::BACKUP_DATA_FOR_ALL)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500966 {
967 for (const auto& data : recKwData)
968 {
969 if (get<5>(data) == "YES")
970 {
971 EditorImpl edit(constants::systemVpdFilePath, json,
972 get<1>(data), get<2>(data));
973 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
974 mismatchFound = true;
975 }
976 }
977
978 if (mismatchFound)
979 {
980 cout << "\nData updated successfully for all mismatching "
981 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600982 "data from backup. Exit successfully.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500983 << endl;
984 }
985 else
986 {
987 cout << "\nNo mismatch found for any of the above mentioned "
988 "record-keyword pair. Exit successfully.\n";
989 }
990
991 exit(0);
992 }
993 else if (option == VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL)
994 {
995 for (const auto& data : recKwData)
996 {
997 if (get<5>(data) == "YES")
998 {
999 EditorImpl edit(constants::systemVpdFilePath, json,
1000 get<1>(data), get<2>(data));
1001 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1002 mismatchFound = true;
1003 }
1004 }
1005
1006 if (mismatchFound)
1007 {
1008 cout << "\nData updated successfully for all mismatching "
1009 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001010 "data from primary VPD.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001011 << endl;
1012 }
1013 else
1014 {
1015 cout << "\nNo mismatch found for any of the above mentioned "
1016 "record-keyword pair. Exit successfully.\n";
1017 }
1018
1019 exit(0);
1020 }
1021 else if (option == VpdTool::MORE_OPTIONS)
1022 {
1023 cout << "\nIterate through all restorable record-keyword pairs\n";
1024
1025 for (const auto& data : recKwData)
1026 {
1027 do
1028 {
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001029 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001030
1031 cout << left << setw(6) << "S.No" << left << setw(8)
1032 << "Record" << left << setw(9) << "Keyword" << left
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001033 << setw(75) << setfill(' ') << "Backup Data" << left
1034 << setw(75) << setfill(' ') << "Primary Data" << left
1035 << setw(14) << "Data Mismatch" << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001036
1037 cout << left << setw(6) << get<0>(data) << left << setw(8)
1038 << get<1>(data) << left << setw(9) << get<2>(data)
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001039 << left << setw(75) << setfill(' ') << get<3>(data)
1040 << left << setw(75) << setfill(' ') << get<4>(data)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001041 << left << setw(14) << get<5>(data);
1042
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001043 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001044
1045 if (get<5>(data) == "NO")
1046 {
1047 cout << "\nNo mismatch found.\n";
1048 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1049 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1050 printFixSystemVPDOption(VpdTool::EXIT);
1051 }
1052 else
1053 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001054 printFixSystemVPDOption(
1055 VpdTool::BACKUP_DATA_FOR_CURRENT);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001056 printFixSystemVPDOption(
1057 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT);
1058 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1059 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1060 printFixSystemVPDOption(VpdTool::EXIT);
1061 }
1062
1063 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001064 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001065
1066 EditorImpl edit(constants::systemVpdFilePath, json,
1067 get<1>(data), get<2>(data));
1068
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001069 if (option == VpdTool::BACKUP_DATA_FOR_CURRENT)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001070 {
1071 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
1072 cout << "\nData updated successfully.\n";
1073 break;
1074 }
1075 else if (option ==
1076 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT)
1077 {
1078 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1079 cout << "\nData updated successfully.\n";
1080 break;
1081 }
1082 else if (option == VpdTool::NEW_VALUE_ON_BOTH)
1083 {
1084 string value;
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001085 cout << "\nEnter the new value to update on both "
1086 "primary & backup. Value should be in ASCII or "
1087 "in HEX(prefixed with 0x) : ";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001088 cin >> value;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001089 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001090
1091 edit.updateKeyword(toBinary(value), 0, true);
1092 cout << "\nData updated successfully.\n";
1093 break;
1094 }
1095 else if (option == VpdTool::SKIP_CURRENT)
1096 {
1097 cout << "\nSkipped the above record-keyword pair. "
1098 "Continue to the next available pair.\n";
1099 break;
1100 }
1101 else if (option == VpdTool::EXIT)
1102 {
1103 cout << "\nExit successfully\n";
1104 exit(0);
1105 }
1106 else
1107 {
1108 cout << "\nProvide a valid option. Retrying for the "
1109 "current record-keyword pair\n";
1110 }
1111 } while (1);
1112 }
1113 exit(0);
1114 }
1115 else if (option == VpdTool::EXIT)
1116 {
1117 cout << "\nExit successfully";
1118 exit(0);
1119 }
1120 else
1121 {
1122 cout << "\nProvide a valid option. Retry.";
1123 continue;
1124 }
1125
1126 } while (true);
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001127}
1128
1129int VpdTool::cleanSystemVPD()
1130{
1131 try
1132 {
1133 // Get system VPD hardware data in map
1134 unordered_map<string, DbusPropertyMap> vpdMap;
1135 json js;
1136 getVPDInMap(constants::systemVpdFilePath, vpdMap, js,
1137 constants::pimPath +
1138 static_cast<std::string>(constants::SYSTEM_OBJECT));
1139
1140 RecKwValMap kwdsToBeUpdated;
1141
1142 for (auto recordMap : svpdKwdMap)
1143 {
1144 const auto& record = recordMap.first;
1145 std::unordered_map<std::string, Binary> kwDefault;
1146 for (auto keywordMap : recordMap.second)
1147 {
1148 // Skip those keywords which cannot be reset at manufacturing
1149 if (!std::get<3>(keywordMap))
1150 {
1151 continue;
1152 }
1153 const auto& keyword = std::get<0>(keywordMap);
1154
1155 // Get hardware value for this keyword from vpdMap
1156 Binary hardwareValue;
1157
1158 auto recItr = vpdMap.find(record);
1159
1160 if (recItr != vpdMap.end())
1161 {
1162 DbusPropertyMap& kwValMap = recItr->second;
1163 auto kwItr = kwValMap.find(keyword);
1164 if (kwItr != kwValMap.end())
1165 {
1166 hardwareValue = toBinary(kwItr->second);
1167 }
1168 }
1169
1170 // compare hardware value with the keyword's default value
1171 auto defaultValue = std::get<1>(keywordMap);
1172 if (hardwareValue != defaultValue)
1173 {
1174 EditorImpl edit(constants::systemVpdFilePath, js, record,
1175 keyword);
1176 edit.updateKeyword(defaultValue, 0, true);
1177 }
1178 }
1179 }
1180
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -06001181 std::cout << "\n The critical keywords from system backplane VPD has "
1182 "been reset successfully."
1183 << std::endl;
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001184 }
1185 catch (const std::exception& e)
1186 {
1187 std::cerr << e.what();
1188 std::cerr
1189 << "\nManufacturing reset on system vpd keywords is unsuccessful";
1190 }
1191 return 0;
Patrick Williamsc78d8872023-05-10 07:50:56 -05001192}