blob: b7de1a6decb3b2804091bcf300b3dae38ab5b61b [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05003#include "common_utility.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05304#include "defines.hpp"
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05005#include "ibm_vpd_utils.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05006#include "ipz_parser.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05307#include "keyword_vpd_parser.hpp"
Alpana Kumaria00936f2020-04-14 07:15:46 -05008#include "memory_vpd_parser.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05009#include "parser_factory.hpp"
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050010#include "vpd_exceptions.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053011
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012#include <assert.h>
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -050013#include <ctype.h>
14
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053015#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053016#include <algorithm>
alpana077ce68722021-07-25 13:23:59 -050017#include <boost/algorithm/string.hpp>
Alpana Kumari65b83602020-09-01 00:24:56 -050018#include <cstdarg>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053019#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053020#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053021#include <fstream>
Alpana Kumari2f793042020-08-18 05:51:03 -050022#include <gpiod.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053023#include <iostream>
24#include <iterator>
25#include <nlohmann/json.hpp>
Andrew Geissler280197e2020-12-08 20:51:49 -060026#include <phosphor-logging/log.hpp>
alpana077ce68722021-07-25 13:23:59 -050027#include <regex>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053028
29using namespace std;
30using namespace openpower::vpd;
31using namespace CLI;
32using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053033using namespace openpower::vpd::constants;
34namespace fs = filesystem;
35using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050036using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050037using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050038using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050039using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050040using namespace openpower::vpd::exceptions;
Andrew Geissler280197e2020-12-08 20:51:49 -060041using namespace phosphor::logging;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053042
Alpana Kumari65b83602020-09-01 00:24:56 -050043static const deviceTreeMap deviceTreeSystemTypeMap = {
Joel Stanley6fb0ef92021-05-26 13:14:32 +093044 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
45 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
46 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
47 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
Andrew Geissler2fe709f2021-03-25 10:59:07 -050048 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
49 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -050050
Santosh Puranik88edeb62020-03-02 12:00:09 +053051/**
Santosh Puranik85893752020-11-10 21:31:43 +053052 * @brief Returns the power state for chassis0
53 */
54static auto getPowerState()
55{
56 // TODO: How do we handle multiple chassis?
57 string powerState{};
58 auto bus = sdbusplus::bus::new_default();
59 auto properties =
60 bus.new_method_call("xyz.openbmc_project.State.Chassis",
61 "/xyz/openbmc_project/state/chassis0",
62 "org.freedesktop.DBus.Properties", "Get");
63 properties.append("xyz.openbmc_project.State.Chassis");
64 properties.append("CurrentPowerState");
65 auto result = bus.call(properties);
66 if (!result.is_method_error())
67 {
68 variant<string> val;
69 result.read(val);
70 if (auto pVal = get_if<string>(&val))
71 {
72 powerState = *pVal;
73 }
74 }
75 cout << "Power state is: " << powerState << endl;
76 return powerState;
77}
78
79/**
Santosh Puranik88edeb62020-03-02 12:00:09 +053080 * @brief Expands location codes
81 */
82static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
83 bool isSystemVpd)
84{
85 auto expanded{unexpanded};
86 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
87 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
88 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
89 size_t idx = expanded.find("fcs");
90 try
91 {
92 if (idx != string::npos)
93 {
94 string fc{};
95 string se{};
96 if (isSystemVpd)
97 {
98 const auto& fcData = vpdMap.at("VCEN").at("FC");
99 const auto& seData = vpdMap.at("VCEN").at("SE");
100 fc = string(fcData.data(), fcData.size());
101 se = string(seData.data(), seData.size());
102 }
103 else
104 {
105 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
106 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
107 }
108
Alpana Kumari81671f62021-02-10 02:21:59 -0600109 // TODO: See if ND0 can be placed in the JSON
110 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530111 }
112 else
113 {
114 idx = expanded.find("mts");
115 if (idx != string::npos)
116 {
117 string mt{};
118 string se{};
119 if (isSystemVpd)
120 {
121 const auto& mtData = vpdMap.at("VSYS").at("TM");
122 const auto& seData = vpdMap.at("VSYS").at("SE");
123 mt = string(mtData.data(), mtData.size());
124 se = string(seData.data(), seData.size());
125 }
126 else
127 {
128 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
129 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
130 }
131
132 replace(mt.begin(), mt.end(), '-', '.');
133 expanded.replace(idx, 3, mt + "." + se);
134 }
135 }
136 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500137 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530138 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500139 cerr << "Failed to expand location code with exception: " << e.what()
140 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530141 }
142 return expanded;
143}
Alpana Kumari2f793042020-08-18 05:51:03 -0500144
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530145/**
146 * @brief Populate FRU specific interfaces.
147 *
148 * This is a common method which handles both
149 * ipz and keyword specific interfaces thus,
150 * reducing the code redundancy.
151 * @param[in] map - Reference to the innermost keyword-value map.
152 * @param[in] preIntrStr - Reference to the interface string.
153 * @param[out] interfaces - Reference to interface map.
154 */
155template <typename T>
156static void populateFruSpecificInterfaces(const T& map,
157 const string& preIntrStr,
158 inventory::InterfaceMap& interfaces)
159{
160 inventory::PropertyMap prop;
161
162 for (const auto& kwVal : map)
163 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530164 auto kw = kwVal.first;
165
166 if (kw[0] == '#')
167 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500168 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530169 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500170 else if (isdigit(kw[0]))
171 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500172 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500173 }
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000174 if constexpr (is_same<T, KeywordVpdMap>::value)
175 {
176 if (get_if<Binary>(&kwVal.second))
177 {
178 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
179 get_if<Binary>(&kwVal.second)->end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000180 prop.emplace(move(kw), move(vec));
181 }
182 else
183 {
184 if (kw == "MemorySizeInKB")
185 {
186 inventory::PropertyMap memProp;
187 auto memVal = get_if<size_t>(&kwVal.second);
188 if (memVal)
189 {
190 memProp.emplace(move(kw),
191 ((*memVal) * CONVERT_MB_TO_KB));
192 interfaces.emplace(
193 "xyz.openbmc_project.Inventory.Item.Dimm",
194 move(memProp));
195 }
196 else
197 {
198 cerr << "MemorySizeInKB value not found in vpd map\n";
199 }
200 }
201 }
202 }
203 else
204 {
205 Binary vec(kwVal.second.begin(), kwVal.second.end());
206 prop.emplace(move(kw), move(vec));
207 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530208 }
209
210 interfaces.emplace(preIntrStr, move(prop));
211}
212
213/**
214 * @brief Populate Interfaces.
215 *
216 * This method populates common and extra interfaces to dbus.
217 * @param[in] js - json object
218 * @param[out] interfaces - Reference to interface map
219 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530220 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530221 */
222template <typename T>
223static void populateInterfaces(const nlohmann::json& js,
224 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530225 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530226{
227 for (const auto& ifs : js.items())
228 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530229 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530230 inventory::PropertyMap props;
231
232 for (const auto& itr : ifs.value().items())
233 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530234 const string& busProp = itr.key();
235
Alpana Kumari31970de2020-02-17 06:49:57 -0600236 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530237 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530238 props.emplace(busProp, itr.value().get<bool>());
239 }
240 else if (itr.value().is_string())
241 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500242 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530243 {
244 if (busProp == "LocationCode" &&
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000245 inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530246 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000247 // TODO deprecate the com.ibm interface later
Santosh Puranik88edeb62020-03-02 12:00:09 +0530248 auto prop = expandLocationCode(
249 itr.value().get<string>(), vpdMap, isSystemVpd);
250 props.emplace(busProp, prop);
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000251 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530252 }
253 else
254 {
255 props.emplace(busProp, itr.value().get<string>());
256 }
257 }
258 else
259 {
260 props.emplace(busProp, itr.value().get<string>());
261 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530262 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530263 else if (itr.value().is_array())
264 {
265 try
266 {
267 props.emplace(busProp, itr.value().get<Binary>());
268 }
269 catch (nlohmann::detail::type_error& e)
270 {
271 std::cerr << "Type exception: " << e.what() << "\n";
272 // Ignore any type errors
273 }
274 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600275 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530276 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600277 const string& rec = itr.value().value("recordName", "");
278 const string& kw = itr.value().value("keywordName", "");
279 const string& encoding = itr.value().value("encoding", "");
280
Alpana Kumari58e22142020-05-05 00:22:12 -0500281 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530282 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530283 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
284 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600285 {
286 auto encoded =
287 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530288 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600289 }
290 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500291 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600292 {
293 if (!kw.empty() && vpdMap.count(kw))
294 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000295 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
296 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
297
298 if (kwValue)
299 {
300 auto prop =
301 string((*kwValue).begin(), (*kwValue).end());
302
303 auto encoded = encodeKeyword(prop, encoding);
304
305 props.emplace(busProp, encoded);
306 }
307 else if (uintValue)
308 {
309 props.emplace(busProp, *uintValue);
310 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600311 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530312 }
313 }
314 }
315 interfaces.emplace(inf, move(props));
316 }
317}
318
Alpana Kumari2f793042020-08-18 05:51:03 -0500319static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500320{
321 uint32_t offset = 0;
322 // check if offset present?
323 for (const auto& item : js["frus"][file])
324 {
325 if (item.find("offset") != item.end())
326 {
327 offset = item["offset"];
328 }
329 }
330
331 // TODO: Figure out a better way to get max possible VPD size.
332 Binary vpdVector;
333 vpdVector.resize(65504);
334 ifstream vpdFile;
335 vpdFile.open(file, ios::binary);
336
337 vpdFile.seekg(offset, ios_base::cur);
338 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
339 vpdVector.resize(vpdFile.gcount());
340
341 return vpdVector;
342}
343
Alpana Kumari2f793042020-08-18 05:51:03 -0500344/** This API will be called at the end of VPD collection to perform any post
345 * actions.
346 *
347 * @param[in] json - json object
348 * @param[in] file - eeprom file path
349 */
350static void postFailAction(const nlohmann::json& json, const string& file)
351{
352 if ((json["frus"][file].at(0)).find("postActionFail") ==
353 json["frus"][file].at(0).end())
354 {
355 return;
356 }
357
358 uint8_t pinValue = 0;
359 string pinName;
360
361 for (const auto& postAction :
362 (json["frus"][file].at(0))["postActionFail"].items())
363 {
364 if (postAction.key() == "pin")
365 {
366 pinName = postAction.value();
367 }
368 else if (postAction.key() == "value")
369 {
370 // Get the value to set
371 pinValue = postAction.value();
372 }
373 }
374
375 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
376
377 try
378 {
379 gpiod::line outputLine = gpiod::find_line(pinName);
380
381 if (!outputLine)
382 {
383 cout << "Couldn't find output line:" << pinName
384 << " on GPIO. Skipping...\n";
385
386 return;
387 }
388 outputLine.request(
389 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
390 pinValue);
391 }
392 catch (system_error&)
393 {
394 cerr << "Failed to set post-action GPIO" << endl;
395 }
396}
397
398/** Performs any pre-action needed to get the FRU setup for collection.
399 *
400 * @param[in] json - json object
401 * @param[in] file - eeprom file path
402 */
403static void preAction(const nlohmann::json& json, const string& file)
404{
405 if ((json["frus"][file].at(0)).find("preAction") ==
406 json["frus"][file].at(0).end())
407 {
408 return;
409 }
410
411 uint8_t pinValue = 0;
412 string pinName;
413
414 for (const auto& postAction :
415 (json["frus"][file].at(0))["preAction"].items())
416 {
417 if (postAction.key() == "pin")
418 {
419 pinName = postAction.value();
420 }
421 else if (postAction.key() == "value")
422 {
423 // Get the value to set
424 pinValue = postAction.value();
425 }
426 }
427
428 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
429 try
430 {
431 gpiod::line outputLine = gpiod::find_line(pinName);
432
433 if (!outputLine)
434 {
435 cout << "Couldn't find output line:" << pinName
436 << " on GPIO. Skipping...\n";
437
438 return;
439 }
440 outputLine.request(
441 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
442 pinValue);
443 }
444 catch (system_error&)
445 {
446 cerr << "Failed to set pre-action GPIO" << endl;
447 return;
448 }
449
450 // Now bind the device
451 string bind = json["frus"][file].at(0).value("bind", "");
452 cout << "Binding device " << bind << endl;
453 string bindCmd = string("echo \"") + bind +
454 string("\" > /sys/bus/i2c/drivers/at24/bind");
455 cout << bindCmd << endl;
456 executeCmd(bindCmd);
457
458 // Check if device showed up (test for file)
459 if (!fs::exists(file))
460 {
461 cout << "EEPROM " << file << " does not exist. Take failure action"
462 << endl;
463 // If not, then take failure postAction
464 postFailAction(json, file);
465 }
466}
467
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530468/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530469 * @brief Prime the Inventory
470 * Prime the inventory by populating only the location code,
471 * type interface and the inventory object for the frus
472 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530473 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530474 * @param[in] jsObject - Reference to vpd inventory json object
475 * @param[in] vpdMap - Reference to the parsed vpd map
476 *
477 * @returns Map of items in extraInterface.
478 */
479template <typename T>
480inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
481 const T& vpdMap)
482{
483 inventory::ObjectMap objects;
484
485 for (auto& itemFRUS : jsObject["frus"].items())
486 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500487 // Take pre actions
488 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530489 for (auto& itemEEPROM : itemFRUS.value())
490 {
491 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530492 inventory::Object object(itemEEPROM.at("inventoryPath"));
493
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530494 if ((itemFRUS.key() != systemVpdFilePath) &&
495 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530496 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600497 inventory::PropertyMap presProp;
498 presProp.emplace("Present", false);
499 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
500 move(presProp));
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530501 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
502 {
503 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
504 {
505 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000506 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530507 {
508 if constexpr (std::is_same<T, Parsed>::value)
509 {
510 for (auto& lC : eI.value().items())
511 {
512 auto propVal = expandLocationCode(
513 lC.value().get<string>(), vpdMap, true);
514
515 props.emplace(move(lC.key()),
516 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530517 interfaces.emplace(XYZ_LOCATION_CODE_INF,
518 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530519 interfaces.emplace(move(eI.key()),
520 move(props));
521 }
522 }
523 }
524 else if (eI.key().find("Inventory.Item.") !=
525 string::npos)
526 {
527 interfaces.emplace(move(eI.key()), move(props));
528 }
529 }
530 }
531 objects.emplace(move(object), move(interfaces));
532 }
533 }
534 }
535 return objects;
536}
537
Alpana Kumari65b83602020-09-01 00:24:56 -0500538/**
539 * @brief This API executes command to set environment variable
540 * And then reboot the system
541 * @param[in] key -env key to set new value
542 * @param[in] value -value to set.
543 */
544void setEnvAndReboot(const string& key, const string& value)
545{
546 // set env and reboot and break.
547 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600548 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500549 // make dbus call to reboot
550 auto bus = sdbusplus::bus::new_default_system();
551 auto method = bus.new_method_call(
552 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
553 "org.freedesktop.systemd1.Manager", "Reboot");
554 bus.call_noreply(method);
555}
556
557/*
558 * @brief This API checks for env var fitconfig.
559 * If not initialised OR updated as per the current system type,
560 * update this env var and reboot the system.
561 *
562 * @param[in] systemType IM kwd in vpd tells about which system type it is.
563 * */
564void setDevTreeEnv(const string& systemType)
565{
566 string newDeviceTree;
567
568 if (deviceTreeSystemTypeMap.find(systemType) !=
569 deviceTreeSystemTypeMap.end())
570 {
571 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
572 }
573
574 string readVarValue;
575 bool envVarFound = false;
576
577 vector<string> output = executeCmd("/sbin/fw_printenv");
578 for (const auto& entry : output)
579 {
580 size_t pos = entry.find("=");
581 string key = entry.substr(0, pos);
582 if (key != "fitconfig")
583 {
584 continue;
585 }
586
587 envVarFound = true;
588 if (pos + 1 < entry.size())
589 {
590 readVarValue = entry.substr(pos + 1);
591 if (readVarValue.find(newDeviceTree) != string::npos)
592 {
593 // fitconfig is Updated. No action needed
594 break;
595 }
596 }
597 // set env and reboot and break.
598 setEnvAndReboot(key, newDeviceTree);
599 exit(0);
600 }
601
602 // check If env var Not found
603 if (!envVarFound)
604 {
605 setEnvAndReboot("fitconfig", newDeviceTree);
606 }
607}
608
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530609/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500610 * @brief API to call VPD manager to write VPD to EEPROM.
611 * @param[in] Object path.
612 * @param[in] record to be updated.
613 * @param[in] keyword to be updated.
614 * @param[in] keyword data to be updated
615 */
616void updateHardware(const string& objectName, const string& recName,
617 const string& kwdName, const Binary& data)
618{
619 try
620 {
621 auto bus = sdbusplus::bus::new_default();
622 auto properties =
623 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
624 properties.append(
625 static_cast<sdbusplus::message::object_path>(objectName));
626 properties.append(recName);
627 properties.append(kwdName);
628 properties.append(data);
629 bus.call(properties);
630 }
Patrick Williams8be43342021-09-02 09:33:36 -0500631 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500632 {
633 std::string what =
634 "VPDManager WriteKeyword api failed for inventory path " +
635 objectName;
636 what += " record " + recName;
637 what += " keyword " + kwdName;
638 what += " with bus error = " + std::string(e.what());
639
640 // map to hold additional data in case of logging pel
641 PelAdditionalData additionalData{};
642 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
643 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500644 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500645 }
646}
647
648/**
649 * @brief API to check if we need to restore system VPD
650 * This functionality is only applicable for IPZ VPD data.
651 * @param[in] vpdMap - IPZ vpd map
652 * @param[in] objectPath - Object path for the FRU
653 * @return EEPROMs with records and keywords updated at standby
654 */
655std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
656 const string& objectPath)
657{
658 // the list of keywords for VSYS record is as per the S0 system. Should be
659 // updated for another type of systems
660 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
661 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
662 {"VCEN", {"FC", "SE"}},
663 {"LXR0", {"LX"}}};
664
665 // vector to hold all the EEPROMs updated at standby
666 std::vector<RestoredEeproms> updatedEeproms = {};
667
668 for (const auto& systemRecKwdPair : svpdKwdMap)
669 {
670 auto it = vpdMap.find(systemRecKwdPair.first);
671
672 // check if record is found in map we got by parser
673 if (it != vpdMap.end())
674 {
675 const auto& kwdListForRecord = systemRecKwdPair.second;
676 for (const auto& keyword : kwdListForRecord)
677 {
678 DbusPropertyMap& kwdValMap = it->second;
679 auto iterator = kwdValMap.find(keyword);
680
681 if (iterator != kwdValMap.end())
682 {
683 string& kwdValue = iterator->second;
684
685 // check bus data
686 const string& recordName = systemRecKwdPair.first;
687 const string& busValue = readBusProperty(
688 objectPath, ipzVpdInf + recordName, keyword);
689
690 if (busValue.find_first_not_of(' ') != string::npos)
691 {
692 if (kwdValue.find_first_not_of(' ') != string::npos)
693 {
694 // both the data are present, check for mismatch
695 if (busValue != kwdValue)
696 {
697 string errMsg = "VPD data mismatch on cache "
698 "and hardware for record: ";
699 errMsg += (*it).first;
700 errMsg += " and keyword: ";
701 errMsg += keyword;
702
703 // data mismatch
704 PelAdditionalData additionalData;
705 additionalData.emplace("CALLOUT_INVENTORY_PATH",
706 objectPath);
707
708 additionalData.emplace("DESCRIPTION", errMsg);
709
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500710 createPEL(additionalData, PelSeverity::WARNING,
711 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500712 }
713 }
714 else
715 {
716 // implies hardware data is blank
717 // update the map
718 Binary busData(busValue.begin(), busValue.end());
719
720 updatedEeproms.push_back(std::make_tuple(
721 objectPath, recordName, keyword, busData));
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500722
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500723 // update the map as well, so that cache data is not
724 // updated as blank while populating VPD map on Dbus
725 // in populateDBus Api
726 kwdValue = busValue;
727 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500728 }
729 else if (kwdValue.find_first_not_of(' ') == string::npos)
730 {
731 string errMsg = "VPD is blank on both cache and "
732 "hardware for record: ";
733 errMsg += (*it).first;
734 errMsg += " and keyword: ";
735 errMsg += keyword;
736 errMsg += ". SSR need to update hardware VPD.";
737
738 // both the data are blanks, log PEL
739 PelAdditionalData additionalData;
740 additionalData.emplace("CALLOUT_INVENTORY_PATH",
741 objectPath);
742
743 additionalData.emplace("DESCRIPTION", errMsg);
744
745 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500746 createPEL(additionalData, PelSeverity::ERROR,
747 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500748 continue;
749 }
750 }
751 }
752 }
753 }
754
755 return updatedEeproms;
756}
757
758/**
alpana077ce68722021-07-25 13:23:59 -0500759 * @brief This checks for is this FRU a processor
760 * And if yes, then checks for is this primary
761 *
762 * @param[in] js- vpd json to get the information about this FRU
763 * @param[in] filePath- FRU vpd
764 *
765 * @return true/false
766 */
767bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
768{
769 bool isProcessor = false;
770 bool isPrimary = false;
771
772 for (const auto& item : js["frus"][filePath])
773 {
774 if (item.find("extraInterfaces") != item.end())
775 {
776 for (const auto& eI : item["extraInterfaces"].items())
777 {
778 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
779 {
780 isProcessor = true;
781 }
782 }
783 }
784
785 if (isProcessor)
786 {
787 string cpuType = item.value("cpuType", "");
788 if (cpuType == "primary")
789 {
790 isPrimary = true;
791 }
792 }
793 }
794
795 return (isProcessor && isPrimary);
796}
797
798/**
799 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
800 * driver
801 * @param[in] js- vpd json to iterate through and take action if it is DIMM
802 */
803void doEnableAllDimms(nlohmann::json& js)
804{
805 // iterate over each fru
806 for (const auto& eachFru : js["frus"].items())
807 {
808 // skip the driver binding if eeprom already exists
809 if (fs::exists(eachFru.key()))
810 {
811 continue;
812 }
813
814 for (const auto& eachInventory : eachFru.value())
815 {
816 if (eachInventory.find("extraInterfaces") != eachInventory.end())
817 {
818 for (const auto& eI : eachInventory["extraInterfaces"].items())
819 {
820 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
821 {
822 string dimmVpd = eachFru.key();
823 // fetch it from
824 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
825
826 regex matchPatern("([0-9]+-[0-9]{4})");
827 smatch matchFound;
828 if (regex_search(dimmVpd, matchFound, matchPatern))
829 {
830 vector<string> i2cReg;
831 boost::split(i2cReg, matchFound.str(0),
832 boost::is_any_of("-"));
833
834 // remove 0s from begining
835 const regex pattern("^0+(?!$)");
836 for (auto& i : i2cReg)
837 {
838 i = regex_replace(i, pattern, "");
839 }
840
841 if (i2cReg.size() == 2)
842 {
843 // echo 24c32 0x50 >
844 // /sys/bus/i2c/devices/i2c-16/new_device
845 string cmnd = "echo 24c32 0x" + i2cReg[1] +
846 " > /sys/bus/i2c/devices/i2c-" +
847 i2cReg[0] + "/new_device";
848
849 executeCmd(cmnd);
850 }
851 }
852 }
853 }
854 }
855 }
856 }
857}
858
859/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530860 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530861 * This method invokes all the populateInterface functions
862 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530863 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
864 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530865 * @param[in] js - Inventory json object
866 * @param[in] filePath - Path of the vpd file
867 * @param[in] preIntrStr - Interface string
868 */
869template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500870static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530871{
872 inventory::InterfaceMap interfaces;
873 inventory::ObjectMap objects;
874 inventory::PropertyMap prop;
875
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500876 // map to hold all the keywords whose value has been changed at standby
877 vector<RestoredEeproms> updatedEeproms = {};
878
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530879 bool isSystemVpd = (filePath == systemVpdFilePath);
880 if constexpr (is_same<T, Parsed>::value)
881 {
882 if (isSystemVpd)
883 {
884 std::vector<std::string> interfaces = {motherBoardInterface};
885 // call mapper to check for object path creation
886 MapperResponse subTree =
887 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
888 string mboardPath =
889 js["frus"][filePath].at(0).value("inventoryPath", "");
890
891 // Attempt system VPD restore if we have a motherboard
892 // object in the inventory.
893 if ((subTree.size() != 0) &&
894 (subTree.find(pimPath + mboardPath) != subTree.end()))
895 {
896 updatedEeproms = restoreSystemVPD(vpdMap, mboardPath);
897 }
898 else
899 {
900 log<level::ERR>("No object path found");
901 }
902 }
alpana077ce68722021-07-25 13:23:59 -0500903 else
904 {
905 // check if it is processor vpd.
906 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
907
908 if (isPrimaryCpu)
909 {
910 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
911
912 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
913
914 if (chipVersion >= 2)
915 {
916 doEnableAllDimms(js);
917 }
918 }
919 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530920 }
921
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530922 for (const auto& item : js["frus"][filePath])
923 {
924 const auto& objectPath = item["inventoryPath"];
925 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500926
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530927 // Populate the VPD keywords and the common interfaces only if we
928 // are asked to inherit that data from the VPD, else only add the
929 // extraInterfaces.
930 if (item.value("inherit", true))
931 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500932 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530933 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530934 // Each record in the VPD becomes an interface and all
935 // keyword within the record are properties under that
936 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530937 for (const auto& record : vpdMap)
938 {
939 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500940 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530941 }
942 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500943 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530944 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500945 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530946 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530947 if (js.find("commonInterfaces") != js.end())
948 {
949 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
950 isSystemVpd);
951 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530952 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500953 else
954 {
955 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500956 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500957 {
958 if (item.find("copyRecords") != item.end())
959 {
960 for (const auto& record : item["copyRecords"])
961 {
962 const string& recordName = record;
963 if (vpdMap.find(recordName) != vpdMap.end())
964 {
965 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500966 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500967 interfaces);
968 }
969 }
970 }
971 }
972 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500973 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530974 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500975 // Populate interfaces and properties that are common to every FRU
976 // and additional interface that might be defined on a per-FRU
977 // basis.
978 if (item.find("extraInterfaces") != item.end())
979 {
980 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
981 isSystemVpd);
982 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530983 }
984 objects.emplace(move(object), move(interfaces));
985 }
986
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530987 if (isSystemVpd)
988 {
Santosh Puranikd278df12021-05-10 15:51:42 +0530989 string systemJsonName{};
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530990 if constexpr (is_same<T, Parsed>::value)
991 {
Alpana Kumarif05effd2021-04-07 07:32:53 -0500992 // pick the right system json
Santosh Puranikd278df12021-05-10 15:51:42 +0530993 systemJsonName = getSystemsJson(vpdMap);
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530994 }
995
Santosh Puranikd278df12021-05-10 15:51:42 +0530996 fs::path target = systemJsonName;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530997 fs::path link = INVENTORY_JSON_SYM_LINK;
998
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530999 // Create the directory for hosting the symlink
1000 fs::create_directories(VPD_FILES_PATH);
1001 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301002 remove(INVENTORY_JSON_SYM_LINK);
1003 // create a new symlink based on the system
1004 fs::create_symlink(target, link);
1005
1006 // Reloading the json
1007 ifstream inventoryJson(link);
1008 auto js = json::parse(inventoryJson);
1009 inventoryJson.close();
1010
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301011 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1012 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001013
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001014 // if system VPD has been restored at standby, update the EEPROM
1015 for (const auto& item : updatedEeproms)
1016 {
1017 updateHardware(get<0>(item), get<1>(item), get<2>(item),
1018 get<3>(item));
1019 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001020
1021 // set the U-boot environment variable for device-tree
1022 if constexpr (is_same<T, Parsed>::value)
1023 {
1024 const string imKeyword = getIM(vpdMap);
1025 const string hwKeyword = getHW(vpdMap);
1026 string systemType = imKeyword;
1027
1028 // check If system has constraint then append HW version to it.
1029 ifstream sysJson(SYSTEM_JSON);
1030 if (!sysJson)
1031 {
1032 throw((VpdJsonException("Failed to access Json path",
1033 SYSTEM_JSON)));
1034 }
1035
1036 try
1037 {
1038 auto systemJson = json::parse(sysJson);
Santosh Puranikd278df12021-05-10 15:51:42 +05301039 if (systemJson["system"].find(imKeyword) !=
1040 systemJson["system"].end())
1041 {
1042 if (systemJson["system"][imKeyword].find("constraint") !=
1043 systemJson["system"][imKeyword].end())
1044 {
1045 systemType += "_" + hwKeyword;
1046 }
1047 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001048 }
1049 catch (json::parse_error& ex)
1050 {
1051 throw((VpdJsonException("System Json parsing failed",
1052 SYSTEM_JSON)));
1053 }
1054
Alpana Kumarif05effd2021-04-07 07:32:53 -05001055 setDevTreeEnv(systemType);
1056 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301057 }
1058
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301059 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001060 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301061}
1062
1063int main(int argc, char** argv)
1064{
1065 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001066 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001067 Binary vpdVector{};
1068 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001069 // map to hold additional data in case of logging pel
1070 PelAdditionalData additionalData{};
1071
1072 // this is needed to hold base fru inventory path in case there is ECC or
1073 // vpd exception while parsing the file
1074 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301075
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001076 // severity for PEL
1077 PelSeverity pelSeverity = PelSeverity::WARNING;
1078
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301079 try
1080 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301081 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1082 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301083
1084 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001085 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301086
1087 CLI11_PARSE(app, argc, argv);
1088
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301089 cout << "Parser launched with file: " << file << "\n";
1090
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001091 // PEL severity should be ERROR in case of any system VPD failure
1092 if (file == systemVpdFilePath)
1093 {
1094 pelSeverity = PelSeverity::ERROR;
1095 }
1096
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301097 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1098
1099 // If the symlink exists, it means it has been setup for us, switch the
1100 // path
1101 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1102 {
1103 jsonToParse = INVENTORY_JSON_SYM_LINK;
1104 }
1105
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301106 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301107 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001108 if (!inventoryJson)
1109 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001110 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001111 }
1112
1113 try
1114 {
1115 js = json::parse(inventoryJson);
1116 }
1117 catch (json::parse_error& ex)
1118 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001119 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001120 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301121
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301122 // Do we have the mandatory "frus" section?
1123 if (js.find("frus") == js.end())
1124 {
1125 throw(VpdJsonException("FRUs section not found in JSON",
1126 jsonToParse));
1127 }
1128
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301129 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1130 if (file.find("/ahb:apb") != string::npos)
1131 {
1132 // Translate udev path to a generic /sys/bus/.. file path.
1133 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301134 cout << "Path after translation: " << file << "\n";
1135
1136 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301137 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301138 {
1139 return 0;
1140 }
1141 }
1142
1143 if (file.empty())
1144 {
1145 cerr << "The EEPROM path <" << file << "> is not valid.";
1146 return 0;
1147 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301148 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301149 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001150 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +05301151 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301152 }
1153
Alpana Kumari2f793042020-08-18 05:51:03 -05001154 if (!fs::exists(file))
1155 {
1156 cout << "Device path: " << file
1157 << " does not exist. Spurious udev event? Exiting." << endl;
1158 return 0;
1159 }
1160
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001161 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301162 // Check if we can read the VPD file based on the power state
1163 if (js["frus"][file].at(0).value("powerOffOnly", false))
1164 {
1165 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1166 getPowerState())
1167 {
1168 cout << "This VPD cannot be read when power is ON" << endl;
1169 return 0;
1170 }
1171 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001172
Alpana Kumari2f793042020-08-18 05:51:03 -05001173 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301174 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001175 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001176 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001177 variant<KeywordVpdMap, Store> parseResult;
1178 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001179
Alpana Kumari2f793042020-08-18 05:51:03 -05001180 if (auto pVal = get_if<Store>(&parseResult))
1181 {
1182 populateDbus(pVal->getVpdMap(), js, file);
1183 }
1184 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1185 {
1186 populateDbus(*pVal, js, file);
1187 }
1188
1189 // release the parser object
1190 ParserFactory::freeParser(parser);
1191 }
1192 catch (exception& e)
1193 {
1194 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001195 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001196 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301197 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001198 catch (const VpdJsonException& ex)
1199 {
1200 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1201 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001202 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001203
1204 cerr << ex.what() << "\n";
1205 rc = -1;
1206 }
1207 catch (const VpdEccException& ex)
1208 {
1209 additionalData.emplace("DESCRIPTION", "ECC check failed");
1210 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1211 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001212 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001213 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001214 cerr << ex.what() << "\n";
1215 rc = -1;
1216 }
1217 catch (const VpdDataException& ex)
1218 {
1219 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1220 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1221 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001222 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001223 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001224 cerr << ex.what() << "\n";
1225 rc = -1;
1226 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301227 catch (exception& e)
1228 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001229 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301230 cerr << e.what() << "\n";
1231 rc = -1;
1232 }
1233
1234 return rc;
Alpana Kumari3ab26a72021-04-05 19:09:19 +00001235}