blob: 2ba53981536ca89a87c28c97ec17d3943fc11307 [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.
348 if (ex.key() == "xyz.openbmc_project.Inventory.Decorator.Asset")
349 {
350 continue;
351 }
352
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530353 if (!(ex.value().is_null()))
354 {
Priyanga Ramasamydacaa472021-10-07 12:22:41 -0500355 // TODO: Remove this if condition check once inventory json is
356 // updated with xyz location code interface.
357 if (ex.key() == "com.ibm.ipzvpd.Location")
358 {
359 getExtraInterfaceProperties(
360 invPath,
361 "xyz.openbmc_project.Inventory.Decorator.LocationCode",
362 ex.value(), js);
363 }
364 else
365 {
366 getExtraInterfaceProperties(invPath, ex.key(), ex.value(),
367 js);
368 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530369 }
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -0500370 if ((ex.key().find("Item") != string::npos) &&
371 (ex.value().is_null()))
372 {
373 js.emplace("type", ex.key());
374 }
375 subOutput.insert(js.begin(), js.end());
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530376 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530377 }
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500378 return subOutput;
379}
380
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500381json VpdTool::getPresentPropJson(const std::string& invPath)
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500382{
383 std::variant<bool> response;
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500384 std::string presence = "Unknown";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500385
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500386 try
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500387 {
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500388 makeDBusCall(invPath, "xyz.openbmc_project.Inventory.Item", "Present")
389 .read(response);
390
391 if (auto pVal = get_if<bool>(&response))
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500392 {
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500393 presence = *pVal ? "true" : "false";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500394 }
395 }
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500396 catch (const sdbusplus::exception::SdBusError& e)
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500397 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600398 presence = "Unknown";
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500399 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600400
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500401 json js;
402 js.emplace("Present", presence);
403 return js;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530404}
405
406json VpdTool::parseInvJson(const json& jsObject, char flag, string fruPath)
407{
408 json output = json::object({});
409 bool validObject = false;
410
411 if (jsObject.find("frus") == jsObject.end())
412 {
413 throw runtime_error("Frus missing in Inventory json");
414 }
415 else
416 {
417 for (const auto& itemFRUS : jsObject["frus"].items())
418 {
419 for (auto itemEEPROM : itemFRUS.value())
420 {
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500421 json subOutput = json::object({});
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530422 try
423 {
424 if (flag == 'O')
425 {
426 if (itemEEPROM.find("inventoryPath") ==
427 itemEEPROM.end())
428 {
429 throw runtime_error("Inventory Path not found");
430 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530431 else if (itemEEPROM.at("inventoryPath") == fruPath)
432 {
433 validObject = true;
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500434 subOutput = interfaceDecider(itemEEPROM);
435 json presentJs = getPresentPropJson(
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500436 "/xyz/openbmc_project/inventory" + fruPath);
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500437 subOutput.insert(presentJs.begin(),
438 presentJs.end());
439 output.emplace(fruPath, subOutput);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530440 return output;
441 }
442 }
443 else
444 {
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500445 subOutput = interfaceDecider(itemEEPROM);
446 json presentJs = getPresentPropJson(
447 "/xyz/openbmc_project/inventory" +
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500448 string(itemEEPROM.at("inventoryPath")));
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500449 subOutput.insert(presentJs.begin(), presentJs.end());
450 output.emplace(string(itemEEPROM.at("inventoryPath")),
451 subOutput);
452 }
453 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500454 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530455 {
456 cerr << e.what();
457 }
458 }
459 }
460 if ((flag == 'O') && (!validObject))
461 {
462 throw runtime_error(
463 "Invalid object path. Refer --dumpInventory/-i option.");
464 }
465 }
466 return output;
467}
468
469void VpdTool::dumpInventory(const nlohmann::basic_json<>& jsObject)
470{
471 char flag = 'I';
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530472 json output = json::array({});
473 output.emplace_back(parseInvJson(jsObject, flag, ""));
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530474 debugger(output);
475}
476
477void VpdTool::dumpObject(const nlohmann::basic_json<>& jsObject)
478{
479 char flag = 'O';
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530480 json output = json::array({});
Santosh Puranikd5d52bb2020-07-28 15:08:10 +0530481 output.emplace_back(parseInvJson(jsObject, flag, fruPath));
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530482 debugger(output);
483}
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530484
485void VpdTool::readKeyword()
486{
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600487 const std::string& kw = getDbusNameForThisKw(keyword);
488
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530489 string interface = "com.ibm.ipzvpd.";
490 variant<Binary> response;
491
492 try
493 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600494 makeDBusCall(INVENTORY_PATH + fruPath, interface + recordName, kw)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530495 .read(response);
496
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530497 string printableVal{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530498 if (auto vec = get_if<Binary>(&response))
499 {
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530500 printableVal = getPrintableValue(*vec);
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530501 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600502
503 if (!value.empty())
504 {
505 if (copyStringToFile(printableVal))
506 {
507 std::cout << "Value read is saved in the file " << value
508 << std::endl;
509 return;
510 }
511 else
512 {
513 std::cerr << "Error while saving the read value in file. "
514 "Displaying the read value on console"
515 << std::endl;
516 }
517 }
518
519 json output = json::object({});
520 json kwVal = json::object({});
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530521 kwVal.emplace(keyword, printableVal);
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530522
523 output.emplace(fruPath, kwVal);
524
525 debugger(output);
526 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500527 catch (const json::exception& e)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530528 {
GiridhariKrishnan63639102023-03-02 05:55:47 -0600529 std::cout << "Keyword Value: " << keyword << std::endl;
530 std::cout << e.what() << std::endl;
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530531 }
532}
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530533
534int VpdTool::updateKeyword()
535{
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600536 Binary val;
537
538 if (std::filesystem::exists(value))
539 {
540 if (!fileToVector(val))
541 {
542 std::cout << "Keyword " << keyword << " update failed."
543 << std::endl;
544 return 1;
545 }
546 }
547 else
548 {
549 val = toBinary(value);
550 }
551
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530552 auto bus = sdbusplus::bus::new_default();
Patrick Williamsc78d8872023-05-10 07:50:56 -0500553 auto properties = bus.new_method_call(BUSNAME, OBJPATH, IFACE,
554 "WriteKeyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530555 properties.append(static_cast<sdbusplus::message::object_path>(fruPath));
556 properties.append(recordName);
557 properties.append(keyword);
558 properties.append(val);
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600559
560 // When there is a request to write 10K bytes, there occurs a delay in dbus
561 // call which leads to dbus timeout exception. To avoid such exceptions
562 // increase the timeout period from default 25 seconds to 60 seconds.
563 auto timeoutInMicroSeconds = 60 * 1000000L;
564 auto result = bus.call(properties, timeoutInMicroSeconds);
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530565
566 if (result.is_method_error())
567 {
568 throw runtime_error("Get api failed");
569 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600570 std::cout << "Data updated successfully " << std::endl;
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530571 return 0;
572}
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530573
574void VpdTool::forceReset(const nlohmann::basic_json<>& jsObject)
575{
576 for (const auto& itemFRUS : jsObject["frus"].items())
577 {
578 for (const auto& itemEEPROM : itemFRUS.value().items())
579 {
580 string fru = itemEEPROM.value().at("inventoryPath");
581
582 fs::path fruCachePath = INVENTORY_MANAGER_CACHE;
583 fruCachePath += INVENTORY_PATH;
584 fruCachePath += fru;
585
586 try
587 {
588 for (const auto& it : fs::directory_iterator(fruCachePath))
589 {
590 if (fs::is_regular_file(it.status()))
591 {
592 fs::remove(it);
593 }
594 }
595 }
596 catch (const fs::filesystem_error& e)
Patrick Williamsc78d8872023-05-10 07:50:56 -0500597 {}
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530598 }
599 }
600
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600601 cout.flush();
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530602 string udevRemove = "udevadm trigger -c remove -s \"*nvmem*\" -v";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600603 int returnCode = system(udevRemove.c_str());
604 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530605
606 string invManagerRestart =
607 "systemctl restart xyz.openbmc_project.Inventory.Manager.service";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600608 returnCode = system(invManagerRestart.c_str());
609 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530610
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530611 string sysVpdRestart = "systemctl restart system-vpd.service";
612 returnCode = system(sysVpdRestart.c_str());
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600613 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530614
615 string udevAdd = "udevadm trigger -c add -s \"*nvmem*\" -v";
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600616 returnCode = system(udevAdd.c_str());
617 printReturnCode(returnCode);
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530618}
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530619
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500620int VpdTool::updateHardware(const uint32_t offset)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530621{
622 int rc = 0;
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600623 Binary val;
624 if (std::filesystem::exists(value))
625 {
626 if (!fileToVector(val))
627 {
628 std::cout << "Keyword " << keyword << " update failed."
629 << std::endl;
630 return 1;
631 }
632 }
633 else
634 {
635 val = toBinary(value);
636 }
637
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530638 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
639 try
640 {
641 auto json = nlohmann::json::parse(inventoryJson);
642 EditorImpl edit(fruPath, json, recordName, keyword);
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500643
644 edit.updateKeyword(val, offset, false);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530645 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500646 catch (const json::parse_error& ex)
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530647 {
648 throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK));
649 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600650 std::cout << "Data updated successfully " << std::endl;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530651 return rc;
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500652}
653
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500654void VpdTool::readKwFromHw(const uint32_t& startOffset)
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500655{
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500656 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
657 auto jsonFile = nlohmann::json::parse(inventoryJson);
girikbab2bed2023-05-15 04:00:17 -0500658 std::string inventoryPath;
659
660 if (jsonFile["frus"].contains(fruPath))
661 {
662 uint32_t vpdStartOffset = 0;
663
664 for (const auto& item : jsonFile["frus"][fruPath])
665 {
666 if (item.find("offset") != item.end())
667 {
668 vpdStartOffset = item["offset"];
669 break;
670 }
671 }
672
673 if ((startOffset != vpdStartOffset))
674 {
675 std::cerr << "Invalid offset, please correct the offset" << endl;
676 std::cerr << "Recommended Offset is: " << vpdStartOffset << endl;
677 return;
678 }
679 inventoryPath = jsonFile["frus"][fruPath][0]["inventoryPath"];
680 }
681
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000682 Binary completeVPDFile;
683 fstream vpdFileStream;
684
jinuthomas45d54972023-07-03 04:36:29 -0500685 vpdFileStream.exceptions(std::ifstream::badbit | std::ifstream::failbit);
686 try
687 {
688 vpdFileStream.open(fruPath,
689 std::ios::in | std::ios::out | std::ios::binary);
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000690
691 auto vpdFileSize = std::min(std::filesystem::file_size(fruPath),
692 constants::MAX_VPD_SIZE);
693 if (vpdFileSize == 0)
694 {
695 std::cerr << "File size is 0 for " << fruPath << std::endl;
696 throw std::runtime_error("File size is 0.");
697 }
698
699 completeVPDFile.resize(vpdFileSize);
jinuthomas45d54972023-07-03 04:36:29 -0500700 vpdFileStream.seekg(startOffset, ios_base::cur);
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000701 vpdFileStream.read(reinterpret_cast<char*>(&completeVPDFile[0]),
702 vpdFileSize);
jinuthomas45d54972023-07-03 04:36:29 -0500703 vpdFileStream.clear(std::ios_base::eofbit);
704 }
Priyanga Ramasamy4170bda2023-07-19 09:25:36 +0000705 catch (const std::system_error& fail)
jinuthomas45d54972023-07-03 04:36:29 -0500706 {
707 std::cerr << "Exception in file handling [" << fruPath
708 << "] error : " << fail.what();
709 std::cerr << "Stream file size = " << vpdFileStream.gcount()
710 << std::endl;
711 throw;
712 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500713
714 if (completeVPDFile.empty())
715 {
716 throw std::runtime_error("Invalid File");
717 }
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -0500718
girik18bb9852022-11-16 05:48:13 -0600719 Impl obj(completeVPDFile, (constants::pimPath + inventoryPath), fruPath,
girikbab2bed2023-05-15 04:00:17 -0500720 startOffset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500721 std::string keywordVal = obj.readKwFromHw(recordName, keyword);
722
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600723 keywordVal = getPrintableValue(keywordVal);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500724
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600725 if (keywordVal.empty())
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500726 {
727 std::cerr << "The given keyword " << keyword << " or record "
728 << recordName
729 << " or both are not present in the given FRU path "
730 << fruPath << std::endl;
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600731 return;
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500732 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600733
734 if (!value.empty())
735 {
736 if (copyStringToFile(keywordVal))
737 {
738 std::cout << "Value read is saved in the file " << value
739 << std::endl;
740 return;
741 }
742 else
743 {
744 std::cerr
745 << "Error while saving the read value in file. Displaying "
746 "the read value on console"
747 << std::endl;
748 }
749 }
750
751 json output = json::object({});
752 json kwVal = json::object({});
753 kwVal.emplace(keyword, keywordVal);
754 output.emplace(fruPath, kwVal);
755 debugger(output);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500756}
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500757
758void VpdTool::printFixSystemVPDOption(UserOption option)
759{
760 switch (option)
761 {
762 case VpdTool::EXIT:
763 cout << "\nEnter 0 => To exit successfully : ";
764 break;
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600765 case VpdTool::BACKUP_DATA_FOR_ALL:
766 cout << "\n\nEnter 1 => If you choose the data on backup for all "
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500767 "mismatching record-keyword pairs";
768 break;
769 case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600770 cout << "\nEnter 2 => If you choose the data on primary for all "
771 "mismatching record-keyword pairs";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500772 break;
773 case VpdTool::MORE_OPTIONS:
774 cout << "\nEnter 3 => If you wish to explore more options";
775 break;
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600776 case VpdTool::BACKUP_DATA_FOR_CURRENT:
777 cout << "\nEnter 4 => If you choose the data on backup as the "
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500778 "right value";
779 break;
780 case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600781 cout << "\nEnter 5 => If you choose the data on primary as the "
782 "right value";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500783 break;
784 case VpdTool::NEW_VALUE_ON_BOTH:
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600785 cout << "\nEnter 6 => If you wish to enter a new value to update "
786 "both on backup and primary";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500787 break;
788 case VpdTool::SKIP_CURRENT:
789 cout << "\nEnter 7 => If you wish to skip the above "
790 "record-keyword pair";
791 break;
792 }
793}
794
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500795void VpdTool::getSystemDataFromCache(IntfPropMap& svpdBusData)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500796{
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500797 const auto vsys = getAllDBusProperty<GetAllResultType>(
798 constants::pimIntf,
799 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
800 "com.ibm.ipzvpd.VSYS");
801 svpdBusData.emplace("VSYS", vsys);
802
803 const auto vcen = getAllDBusProperty<GetAllResultType>(
804 constants::pimIntf,
805 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
806 "com.ibm.ipzvpd.VCEN");
807 svpdBusData.emplace("VCEN", vcen);
808
809 const auto lxr0 = getAllDBusProperty<GetAllResultType>(
810 constants::pimIntf,
811 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
812 "com.ibm.ipzvpd.LXR0");
813 svpdBusData.emplace("LXR0", lxr0);
814
815 const auto util = getAllDBusProperty<GetAllResultType>(
816 constants::pimIntf,
817 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
818 "com.ibm.ipzvpd.UTIL");
819 svpdBusData.emplace("UTIL", util);
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500820}
821
822int VpdTool::fixSystemVPD()
823{
824 std::string outline(191, '=');
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600825 cout << "\nRestorable record-keyword pairs and their data on backup & "
826 "primary.\n\n"
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500827 << outline << std::endl;
828
829 cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600830 << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left
831 << setw(75) << "Data On Primary" << left << setw(14)
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500832 << "Data Mismatch\n"
833 << outline << std::endl;
834
835 int num = 0;
836
837 // Get system VPD data in map
838 unordered_map<string, DbusPropertyMap> vpdMap;
839 json js;
840 getVPDInMap(constants::systemVpdFilePath, vpdMap, js,
841 constants::pimPath +
842 static_cast<std::string>(constants::SYSTEM_OBJECT));
843
844 // Get system VPD D-Bus Data in a map
845 IntfPropMap svpdBusData;
846 getSystemDataFromCache(svpdBusData);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500847
848 for (const auto& recordKw : svpdKwdMap)
849 {
850 string record = recordKw.first;
851
852 // Extract specific record data from the svpdBusData map.
853 const auto& rec = svpdBusData.find(record);
854
855 if (rec == svpdBusData.end())
856 {
857 std::cerr << record << " not a part of critical system VPD records."
858 << std::endl;
859 continue;
860 }
861
862 const auto& recData = svpdBusData.find(record)->second;
863
864 string busStr{}, hwValStr{};
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500865
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600866 for (const auto& keywordInfo : recordKw.second)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500867 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600868 const auto& keyword = get<0>(keywordInfo);
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500869 string mismatch = "NO"; // no mismatch
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500870 string hardwareValue{};
871 auto recItr = vpdMap.find(record);
872
873 if (recItr != vpdMap.end())
874 {
875 DbusPropertyMap& kwValMap = recItr->second;
876 auto kwItr = kwValMap.find(keyword);
877 if (kwItr != kwValMap.end())
878 {
879 hardwareValue = kwItr->second;
880 }
881 }
882
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500883 inventory::Value kwValue;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500884 for (auto& kwData : recData)
885 {
886 if (kwData.first == keyword)
887 {
888 kwValue = kwData.second;
889 break;
890 }
891 }
892
GiridhariKrishnan63639102023-03-02 05:55:47 -0600893 if (keyword != "SE") // SE to display in Hex string only
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500894 {
895 ostringstream hwValStream;
896 hwValStream << "0x";
897 hwValStr = hwValStream.str();
898
899 for (uint16_t byte : hardwareValue)
900 {
901 hwValStream << setfill('0') << setw(2) << hex << byte;
902 hwValStr = hwValStream.str();
903 }
904
905 if (const auto value = get_if<Binary>(&kwValue))
906 {
GiridhariKrishnan63639102023-03-02 05:55:47 -0600907 busStr = hexString(*value);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500908 }
909 if (busStr != hwValStr)
910 {
911 mismatch = "YES";
912 }
913 }
914 else
915 {
916 if (const auto value = get_if<Binary>(&kwValue))
917 {
918 busStr = getPrintableValue(*value);
919 }
920 if (busStr != hardwareValue)
921 {
922 mismatch = "YES";
923 }
924 hwValStr = hardwareValue;
925 }
926 recKwData.push_back(
927 make_tuple(++num, record, keyword, busStr, hwValStr, mismatch));
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500928
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500929 std::string splitLine(191, '-');
930 cout << left << setw(6) << num << left << setw(8) << record << left
931 << setw(9) << keyword << left << setw(75) << setfill(' ')
932 << busStr << left << setw(75) << setfill(' ') << hwValStr
933 << left << setw(14) << mismatch << '\n'
934 << splitLine << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500935 }
936 }
937 parseSVPDOptions(js);
938 return 0;
939}
940
941void VpdTool::parseSVPDOptions(const nlohmann::json& json)
942{
943 do
944 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600945 printFixSystemVPDOption(VpdTool::BACKUP_DATA_FOR_ALL);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500946 printFixSystemVPDOption(VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL);
947 printFixSystemVPDOption(VpdTool::MORE_OPTIONS);
948 printFixSystemVPDOption(VpdTool::EXIT);
949
950 int option = 0;
951 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -0500952
953 std::string outline(191, '=');
954 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500955
956 if (json.find("frus") == json.end())
957 {
958 throw runtime_error("Frus not found in json");
959 }
960
961 bool mismatchFound = false;
962
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600963 if (option == VpdTool::BACKUP_DATA_FOR_ALL)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500964 {
965 for (const auto& data : recKwData)
966 {
967 if (get<5>(data) == "YES")
968 {
969 EditorImpl edit(constants::systemVpdFilePath, json,
970 get<1>(data), get<2>(data));
971 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
972 mismatchFound = true;
973 }
974 }
975
976 if (mismatchFound)
977 {
978 cout << "\nData updated successfully for all mismatching "
979 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600980 "data from backup. Exit successfully.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500981 << endl;
982 }
983 else
984 {
985 cout << "\nNo mismatch found for any of the above mentioned "
986 "record-keyword pair. Exit successfully.\n";
987 }
988
989 exit(0);
990 }
991 else if (option == VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL)
992 {
993 for (const auto& data : recKwData)
994 {
995 if (get<5>(data) == "YES")
996 {
997 EditorImpl edit(constants::systemVpdFilePath, json,
998 get<1>(data), get<2>(data));
999 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1000 mismatchFound = true;
1001 }
1002 }
1003
1004 if (mismatchFound)
1005 {
1006 cout << "\nData updated successfully for all mismatching "
1007 "record-keyword pairs by choosing their corresponding "
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001008 "data from primary VPD.\n"
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001009 << endl;
1010 }
1011 else
1012 {
1013 cout << "\nNo mismatch found for any of the above mentioned "
1014 "record-keyword pair. Exit successfully.\n";
1015 }
1016
1017 exit(0);
1018 }
1019 else if (option == VpdTool::MORE_OPTIONS)
1020 {
1021 cout << "\nIterate through all restorable record-keyword pairs\n";
1022
1023 for (const auto& data : recKwData)
1024 {
1025 do
1026 {
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001027 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001028
1029 cout << left << setw(6) << "S.No" << left << setw(8)
1030 << "Record" << left << setw(9) << "Keyword" << left
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001031 << setw(75) << setfill(' ') << "Backup Data" << left
1032 << setw(75) << setfill(' ') << "Primary Data" << left
1033 << setw(14) << "Data Mismatch" << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001034
1035 cout << left << setw(6) << get<0>(data) << left << setw(8)
1036 << get<1>(data) << left << setw(9) << get<2>(data)
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001037 << left << setw(75) << setfill(' ') << get<3>(data)
1038 << left << setw(75) << setfill(' ') << get<4>(data)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001039 << left << setw(14) << get<5>(data);
1040
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001041 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001042
1043 if (get<5>(data) == "NO")
1044 {
1045 cout << "\nNo mismatch found.\n";
1046 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1047 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1048 printFixSystemVPDOption(VpdTool::EXIT);
1049 }
1050 else
1051 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001052 printFixSystemVPDOption(
1053 VpdTool::BACKUP_DATA_FOR_CURRENT);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001054 printFixSystemVPDOption(
1055 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT);
1056 printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH);
1057 printFixSystemVPDOption(VpdTool::SKIP_CURRENT);
1058 printFixSystemVPDOption(VpdTool::EXIT);
1059 }
1060
1061 cin >> option;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001062 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001063
1064 EditorImpl edit(constants::systemVpdFilePath, json,
1065 get<1>(data), get<2>(data));
1066
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001067 if (option == VpdTool::BACKUP_DATA_FOR_CURRENT)
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001068 {
1069 edit.updateKeyword(toBinary(get<3>(data)), 0, true);
1070 cout << "\nData updated successfully.\n";
1071 break;
1072 }
1073 else if (option ==
1074 VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT)
1075 {
1076 edit.updateKeyword(toBinary(get<4>(data)), 0, true);
1077 cout << "\nData updated successfully.\n";
1078 break;
1079 }
1080 else if (option == VpdTool::NEW_VALUE_ON_BOTH)
1081 {
1082 string value;
Priyanga Ramasamy24942232023-01-05 04:54:59 -06001083 cout << "\nEnter the new value to update on both "
1084 "primary & backup. Value should be in ASCII or "
1085 "in HEX(prefixed with 0x) : ";
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001086 cin >> value;
Priyanga Ramasamy0ac10852022-10-26 05:35:42 -05001087 cout << '\n' << outline << endl;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -05001088
1089 edit.updateKeyword(toBinary(value), 0, true);
1090 cout << "\nData updated successfully.\n";
1091 break;
1092 }
1093 else if (option == VpdTool::SKIP_CURRENT)
1094 {
1095 cout << "\nSkipped the above record-keyword pair. "
1096 "Continue to the next available pair.\n";
1097 break;
1098 }
1099 else if (option == VpdTool::EXIT)
1100 {
1101 cout << "\nExit successfully\n";
1102 exit(0);
1103 }
1104 else
1105 {
1106 cout << "\nProvide a valid option. Retrying for the "
1107 "current record-keyword pair\n";
1108 }
1109 } while (1);
1110 }
1111 exit(0);
1112 }
1113 else if (option == VpdTool::EXIT)
1114 {
1115 cout << "\nExit successfully";
1116 exit(0);
1117 }
1118 else
1119 {
1120 cout << "\nProvide a valid option. Retry.";
1121 continue;
1122 }
1123
1124 } while (true);
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001125}
1126
1127int VpdTool::cleanSystemVPD()
1128{
1129 try
1130 {
1131 // Get system VPD hardware data in map
1132 unordered_map<string, DbusPropertyMap> vpdMap;
1133 json js;
1134 getVPDInMap(constants::systemVpdFilePath, vpdMap, js,
1135 constants::pimPath +
1136 static_cast<std::string>(constants::SYSTEM_OBJECT));
1137
1138 RecKwValMap kwdsToBeUpdated;
1139
1140 for (auto recordMap : svpdKwdMap)
1141 {
1142 const auto& record = recordMap.first;
1143 std::unordered_map<std::string, Binary> kwDefault;
1144 for (auto keywordMap : recordMap.second)
1145 {
1146 // Skip those keywords which cannot be reset at manufacturing
1147 if (!std::get<3>(keywordMap))
1148 {
1149 continue;
1150 }
1151 const auto& keyword = std::get<0>(keywordMap);
1152
1153 // Get hardware value for this keyword from vpdMap
1154 Binary hardwareValue;
1155
1156 auto recItr = vpdMap.find(record);
1157
1158 if (recItr != vpdMap.end())
1159 {
1160 DbusPropertyMap& kwValMap = recItr->second;
1161 auto kwItr = kwValMap.find(keyword);
1162 if (kwItr != kwValMap.end())
1163 {
1164 hardwareValue = toBinary(kwItr->second);
1165 }
1166 }
1167
1168 // compare hardware value with the keyword's default value
1169 auto defaultValue = std::get<1>(keywordMap);
1170 if (hardwareValue != defaultValue)
1171 {
1172 EditorImpl edit(constants::systemVpdFilePath, js, record,
1173 keyword);
1174 edit.updateKeyword(defaultValue, 0, true);
1175 }
1176 }
1177 }
1178
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -06001179 std::cout << "\n The critical keywords from system backplane VPD has "
1180 "been reset successfully."
1181 << std::endl;
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -05001182 }
1183 catch (const std::exception& e)
1184 {
1185 std::cerr << e.what();
1186 std::cerr
1187 << "\nManufacturing reset on system vpd keywords is unsuccessful";
1188 }
1189 return 0;
Patrick Williamsc78d8872023-05-10 07:50:56 -05001190}