blob: c6e58e601d6b25bd5459cea41e9af41aac1c55ed [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/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530469 * @brief Set certain one time properties in the inventory
470 * Use this function to insert the Functional and Enabled properties into the
471 * inventory map. This function first checks if the object in question already
472 * has these properties hosted on D-Bus, if the property is already there, it is
473 * not modified, hence the name "one time". If the property is not already
474 * present, it will be added to the map with a suitable default value (true for
475 * Functional and false for Enabled)
476 *
477 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
478 * @param[inout] interfaces - Reference to a map of inventory interfaces to
479 * which the properties will be attached.
480 */
481static void setOneTimeProperties(const std::string& object,
482 inventory::InterfaceMap& interfaces)
483{
484 auto bus = sdbusplus::bus::new_default();
485 auto objectPath = INVENTORY_PATH + object;
486 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
487 objectPath.c_str(),
488 "org.freedesktop.DBus.Properties", "Get");
489 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
490 prop.append("Functional");
491 try
492 {
493 auto result = bus.call(prop);
494 }
495 catch (const sdbusplus::exception::SdBusError& e)
496 {
497 // Treat as property unavailable
498 inventory::PropertyMap prop;
499 prop.emplace("Functional", true);
500 interfaces.emplace(
501 "xyz.openbmc_project.State.Decorator.OperationalStatus",
502 move(prop));
503 }
504 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
505 objectPath.c_str(),
506 "org.freedesktop.DBus.Properties", "Get");
507 prop.append("xyz.openbmc_project.Object.Enable");
508 prop.append("Enabled");
509 try
510 {
511 auto result = bus.call(prop);
512 }
513 catch (const sdbusplus::exception::SdBusError& e)
514 {
515 // Treat as property unavailable
516 inventory::PropertyMap prop;
517 prop.emplace("Enabled", false);
518 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
519 }
520}
521
522/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530523 * @brief Prime the Inventory
524 * Prime the inventory by populating only the location code,
525 * type interface and the inventory object for the frus
526 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530527 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530528 * @param[in] jsObject - Reference to vpd inventory json object
529 * @param[in] vpdMap - Reference to the parsed vpd map
530 *
531 * @returns Map of items in extraInterface.
532 */
533template <typename T>
534inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
535 const T& vpdMap)
536{
537 inventory::ObjectMap objects;
538
539 for (auto& itemFRUS : jsObject["frus"].items())
540 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500541 // Take pre actions
542 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530543 for (auto& itemEEPROM : itemFRUS.value())
544 {
545 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530546 inventory::Object object(itemEEPROM.at("inventoryPath"));
547
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530548 if ((itemFRUS.key() != systemVpdFilePath) &&
549 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530550 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600551 inventory::PropertyMap presProp;
552 presProp.emplace("Present", false);
553 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530554 presProp);
555 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530556 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
557 {
558 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
559 {
560 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000561 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530562 {
563 if constexpr (std::is_same<T, Parsed>::value)
564 {
565 for (auto& lC : eI.value().items())
566 {
567 auto propVal = expandLocationCode(
568 lC.value().get<string>(), vpdMap, true);
569
570 props.emplace(move(lC.key()),
571 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530572 interfaces.emplace(XYZ_LOCATION_CODE_INF,
573 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530574 interfaces.emplace(move(eI.key()),
575 move(props));
576 }
577 }
578 }
579 else if (eI.key().find("Inventory.Item.") !=
580 string::npos)
581 {
582 interfaces.emplace(move(eI.key()), move(props));
583 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530584 else if (eI.key() ==
585 "xyz.openbmc_project.Inventory.Item")
586 {
587 for (auto& val : eI.value().items())
588 {
589 if (val.key() == "PrettyName")
590 {
591 presProp.emplace(val.key(),
592 val.value().get<string>());
593 }
594 }
595 // Use insert_or_assign here as we may already have
596 // inserted the present property only earlier in
597 // this function under this same interface.
598 interfaces.insert_or_assign(eI.key(),
599 move(presProp));
600 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530601 }
602 }
603 objects.emplace(move(object), move(interfaces));
604 }
605 }
606 }
607 return objects;
608}
609
Alpana Kumari65b83602020-09-01 00:24:56 -0500610/**
611 * @brief This API executes command to set environment variable
612 * And then reboot the system
613 * @param[in] key -env key to set new value
614 * @param[in] value -value to set.
615 */
616void setEnvAndReboot(const string& key, const string& value)
617{
618 // set env and reboot and break.
619 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600620 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500621 // make dbus call to reboot
622 auto bus = sdbusplus::bus::new_default_system();
623 auto method = bus.new_method_call(
624 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
625 "org.freedesktop.systemd1.Manager", "Reboot");
626 bus.call_noreply(method);
627}
628
629/*
630 * @brief This API checks for env var fitconfig.
631 * If not initialised OR updated as per the current system type,
632 * update this env var and reboot the system.
633 *
634 * @param[in] systemType IM kwd in vpd tells about which system type it is.
635 * */
636void setDevTreeEnv(const string& systemType)
637{
638 string newDeviceTree;
639
640 if (deviceTreeSystemTypeMap.find(systemType) !=
641 deviceTreeSystemTypeMap.end())
642 {
643 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
644 }
645
646 string readVarValue;
647 bool envVarFound = false;
648
649 vector<string> output = executeCmd("/sbin/fw_printenv");
650 for (const auto& entry : output)
651 {
652 size_t pos = entry.find("=");
653 string key = entry.substr(0, pos);
654 if (key != "fitconfig")
655 {
656 continue;
657 }
658
659 envVarFound = true;
660 if (pos + 1 < entry.size())
661 {
662 readVarValue = entry.substr(pos + 1);
663 if (readVarValue.find(newDeviceTree) != string::npos)
664 {
665 // fitconfig is Updated. No action needed
666 break;
667 }
668 }
669 // set env and reboot and break.
670 setEnvAndReboot(key, newDeviceTree);
671 exit(0);
672 }
673
674 // check If env var Not found
675 if (!envVarFound)
676 {
677 setEnvAndReboot("fitconfig", newDeviceTree);
678 }
679}
680
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530681/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500682 * @brief API to call VPD manager to write VPD to EEPROM.
683 * @param[in] Object path.
684 * @param[in] record to be updated.
685 * @param[in] keyword to be updated.
686 * @param[in] keyword data to be updated
687 */
688void updateHardware(const string& objectName, const string& recName,
689 const string& kwdName, const Binary& data)
690{
691 try
692 {
693 auto bus = sdbusplus::bus::new_default();
694 auto properties =
695 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
696 properties.append(
697 static_cast<sdbusplus::message::object_path>(objectName));
698 properties.append(recName);
699 properties.append(kwdName);
700 properties.append(data);
701 bus.call(properties);
702 }
Patrick Williams8be43342021-09-02 09:33:36 -0500703 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500704 {
705 std::string what =
706 "VPDManager WriteKeyword api failed for inventory path " +
707 objectName;
708 what += " record " + recName;
709 what += " keyword " + kwdName;
710 what += " with bus error = " + std::string(e.what());
711
712 // map to hold additional data in case of logging pel
713 PelAdditionalData additionalData{};
714 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
715 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500716 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500717 }
718}
719
720/**
721 * @brief API to check if we need to restore system VPD
722 * This functionality is only applicable for IPZ VPD data.
723 * @param[in] vpdMap - IPZ vpd map
724 * @param[in] objectPath - Object path for the FRU
725 * @return EEPROMs with records and keywords updated at standby
726 */
727std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
728 const string& objectPath)
729{
730 // the list of keywords for VSYS record is as per the S0 system. Should be
731 // updated for another type of systems
732 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
733 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
734 {"VCEN", {"FC", "SE"}},
735 {"LXR0", {"LX"}}};
736
737 // vector to hold all the EEPROMs updated at standby
738 std::vector<RestoredEeproms> updatedEeproms = {};
739
740 for (const auto& systemRecKwdPair : svpdKwdMap)
741 {
742 auto it = vpdMap.find(systemRecKwdPair.first);
743
744 // check if record is found in map we got by parser
745 if (it != vpdMap.end())
746 {
747 const auto& kwdListForRecord = systemRecKwdPair.second;
748 for (const auto& keyword : kwdListForRecord)
749 {
750 DbusPropertyMap& kwdValMap = it->second;
751 auto iterator = kwdValMap.find(keyword);
752
753 if (iterator != kwdValMap.end())
754 {
755 string& kwdValue = iterator->second;
756
757 // check bus data
758 const string& recordName = systemRecKwdPair.first;
759 const string& busValue = readBusProperty(
760 objectPath, ipzVpdInf + recordName, keyword);
761
762 if (busValue.find_first_not_of(' ') != string::npos)
763 {
764 if (kwdValue.find_first_not_of(' ') != string::npos)
765 {
766 // both the data are present, check for mismatch
767 if (busValue != kwdValue)
768 {
769 string errMsg = "VPD data mismatch on cache "
770 "and hardware for record: ";
771 errMsg += (*it).first;
772 errMsg += " and keyword: ";
773 errMsg += keyword;
774
775 // data mismatch
776 PelAdditionalData additionalData;
777 additionalData.emplace("CALLOUT_INVENTORY_PATH",
778 objectPath);
779
780 additionalData.emplace("DESCRIPTION", errMsg);
781
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500782 createPEL(additionalData, PelSeverity::WARNING,
783 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500784 }
785 }
786 else
787 {
788 // implies hardware data is blank
789 // update the map
790 Binary busData(busValue.begin(), busValue.end());
791
792 updatedEeproms.push_back(std::make_tuple(
793 objectPath, recordName, keyword, busData));
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500794
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500795 // update the map as well, so that cache data is not
796 // updated as blank while populating VPD map on Dbus
797 // in populateDBus Api
798 kwdValue = busValue;
799 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500800 }
801 else if (kwdValue.find_first_not_of(' ') == string::npos)
802 {
803 string errMsg = "VPD is blank on both cache and "
804 "hardware for record: ";
805 errMsg += (*it).first;
806 errMsg += " and keyword: ";
807 errMsg += keyword;
808 errMsg += ". SSR need to update hardware VPD.";
809
810 // both the data are blanks, log PEL
811 PelAdditionalData additionalData;
812 additionalData.emplace("CALLOUT_INVENTORY_PATH",
813 objectPath);
814
815 additionalData.emplace("DESCRIPTION", errMsg);
816
817 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500818 createPEL(additionalData, PelSeverity::ERROR,
819 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500820 continue;
821 }
822 }
823 }
824 }
825 }
826
827 return updatedEeproms;
828}
829
830/**
alpana077ce68722021-07-25 13:23:59 -0500831 * @brief This checks for is this FRU a processor
832 * And if yes, then checks for is this primary
833 *
834 * @param[in] js- vpd json to get the information about this FRU
835 * @param[in] filePath- FRU vpd
836 *
837 * @return true/false
838 */
839bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
840{
841 bool isProcessor = false;
842 bool isPrimary = false;
843
844 for (const auto& item : js["frus"][filePath])
845 {
846 if (item.find("extraInterfaces") != item.end())
847 {
848 for (const auto& eI : item["extraInterfaces"].items())
849 {
850 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
851 {
852 isProcessor = true;
853 }
854 }
855 }
856
857 if (isProcessor)
858 {
859 string cpuType = item.value("cpuType", "");
860 if (cpuType == "primary")
861 {
862 isPrimary = true;
863 }
864 }
865 }
866
867 return (isProcessor && isPrimary);
868}
869
870/**
871 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
872 * driver
873 * @param[in] js- vpd json to iterate through and take action if it is DIMM
874 */
875void doEnableAllDimms(nlohmann::json& js)
876{
877 // iterate over each fru
878 for (const auto& eachFru : js["frus"].items())
879 {
880 // skip the driver binding if eeprom already exists
881 if (fs::exists(eachFru.key()))
882 {
883 continue;
884 }
885
886 for (const auto& eachInventory : eachFru.value())
887 {
888 if (eachInventory.find("extraInterfaces") != eachInventory.end())
889 {
890 for (const auto& eI : eachInventory["extraInterfaces"].items())
891 {
892 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
893 {
894 string dimmVpd = eachFru.key();
895 // fetch it from
896 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
897
898 regex matchPatern("([0-9]+-[0-9]{4})");
899 smatch matchFound;
900 if (regex_search(dimmVpd, matchFound, matchPatern))
901 {
902 vector<string> i2cReg;
903 boost::split(i2cReg, matchFound.str(0),
904 boost::is_any_of("-"));
905
906 // remove 0s from begining
907 const regex pattern("^0+(?!$)");
908 for (auto& i : i2cReg)
909 {
910 i = regex_replace(i, pattern, "");
911 }
912
913 if (i2cReg.size() == 2)
914 {
915 // echo 24c32 0x50 >
916 // /sys/bus/i2c/devices/i2c-16/new_device
917 string cmnd = "echo 24c32 0x" + i2cReg[1] +
918 " > /sys/bus/i2c/devices/i2c-" +
919 i2cReg[0] + "/new_device";
920
921 executeCmd(cmnd);
922 }
923 }
924 }
925 }
926 }
927 }
928 }
929}
930
931/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530932 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530933 * This method invokes all the populateInterface functions
934 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530935 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
936 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530937 * @param[in] js - Inventory json object
938 * @param[in] filePath - Path of the vpd file
939 * @param[in] preIntrStr - Interface string
940 */
941template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500942static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530943{
944 inventory::InterfaceMap interfaces;
945 inventory::ObjectMap objects;
946 inventory::PropertyMap prop;
947
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500948 // map to hold all the keywords whose value has been changed at standby
949 vector<RestoredEeproms> updatedEeproms = {};
950
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530951 bool isSystemVpd = (filePath == systemVpdFilePath);
952 if constexpr (is_same<T, Parsed>::value)
953 {
954 if (isSystemVpd)
955 {
956 std::vector<std::string> interfaces = {motherBoardInterface};
957 // call mapper to check for object path creation
958 MapperResponse subTree =
959 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
960 string mboardPath =
961 js["frus"][filePath].at(0).value("inventoryPath", "");
962
963 // Attempt system VPD restore if we have a motherboard
964 // object in the inventory.
965 if ((subTree.size() != 0) &&
966 (subTree.find(pimPath + mboardPath) != subTree.end()))
967 {
968 updatedEeproms = restoreSystemVPD(vpdMap, mboardPath);
969 }
970 else
971 {
972 log<level::ERR>("No object path found");
973 }
974 }
alpana077ce68722021-07-25 13:23:59 -0500975 else
976 {
977 // check if it is processor vpd.
978 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
979
980 if (isPrimaryCpu)
981 {
982 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
983
984 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
985
986 if (chipVersion >= 2)
987 {
988 doEnableAllDimms(js);
989 }
990 }
991 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530992 }
993
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530994 for (const auto& item : js["frus"][filePath])
995 {
996 const auto& objectPath = item["inventoryPath"];
997 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500998
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530999 if (isSystemVpd)
1000 {
1001 // Populate one time properties for the system VPD and its sub-frus.
1002 // For the remaining FRUs, this will get handled as a part of
1003 // priming the inventory.
1004 setOneTimeProperties(objectPath, interfaces);
1005 }
1006
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301007 // Populate the VPD keywords and the common interfaces only if we
1008 // are asked to inherit that data from the VPD, else only add the
1009 // extraInterfaces.
1010 if (item.value("inherit", true))
1011 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001012 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301013 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301014 // Each record in the VPD becomes an interface and all
1015 // keyword within the record are properties under that
1016 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301017 for (const auto& record : vpdMap)
1018 {
1019 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001020 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301021 }
1022 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001023 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301024 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001025 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301026 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301027 if (js.find("commonInterfaces") != js.end())
1028 {
1029 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1030 isSystemVpd);
1031 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301032 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001033 else
1034 {
1035 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001036 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001037 {
1038 if (item.find("copyRecords") != item.end())
1039 {
1040 for (const auto& record : item["copyRecords"])
1041 {
1042 const string& recordName = record;
1043 if (vpdMap.find(recordName) != vpdMap.end())
1044 {
1045 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001046 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001047 interfaces);
1048 }
1049 }
1050 }
1051 }
1052 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001053 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301054 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001055 // Populate interfaces and properties that are common to every FRU
1056 // and additional interface that might be defined on a per-FRU
1057 // basis.
1058 if (item.find("extraInterfaces") != item.end())
1059 {
1060 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1061 isSystemVpd);
1062 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301063 }
1064 objects.emplace(move(object), move(interfaces));
1065 }
1066
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301067 if (isSystemVpd)
1068 {
Santosh Puranikd278df12021-05-10 15:51:42 +05301069 string systemJsonName{};
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301070 if constexpr (is_same<T, Parsed>::value)
1071 {
Alpana Kumarif05effd2021-04-07 07:32:53 -05001072 // pick the right system json
Santosh Puranikd278df12021-05-10 15:51:42 +05301073 systemJsonName = getSystemsJson(vpdMap);
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301074 }
1075
Santosh Puranikd278df12021-05-10 15:51:42 +05301076 fs::path target = systemJsonName;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301077 fs::path link = INVENTORY_JSON_SYM_LINK;
1078
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301079 // Create the directory for hosting the symlink
1080 fs::create_directories(VPD_FILES_PATH);
1081 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301082 remove(INVENTORY_JSON_SYM_LINK);
1083 // create a new symlink based on the system
1084 fs::create_symlink(target, link);
1085
1086 // Reloading the json
1087 ifstream inventoryJson(link);
1088 auto js = json::parse(inventoryJson);
1089 inventoryJson.close();
1090
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301091 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1092 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001093
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001094 // if system VPD has been restored at standby, update the EEPROM
1095 for (const auto& item : updatedEeproms)
1096 {
1097 updateHardware(get<0>(item), get<1>(item), get<2>(item),
1098 get<3>(item));
1099 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001100
1101 // set the U-boot environment variable for device-tree
1102 if constexpr (is_same<T, Parsed>::value)
1103 {
1104 const string imKeyword = getIM(vpdMap);
1105 const string hwKeyword = getHW(vpdMap);
1106 string systemType = imKeyword;
1107
1108 // check If system has constraint then append HW version to it.
1109 ifstream sysJson(SYSTEM_JSON);
1110 if (!sysJson)
1111 {
1112 throw((VpdJsonException("Failed to access Json path",
1113 SYSTEM_JSON)));
1114 }
1115
1116 try
1117 {
1118 auto systemJson = json::parse(sysJson);
Santosh Puranikd278df12021-05-10 15:51:42 +05301119 if (systemJson["system"].find(imKeyword) !=
1120 systemJson["system"].end())
1121 {
1122 if (systemJson["system"][imKeyword].find("constraint") !=
1123 systemJson["system"][imKeyword].end())
1124 {
1125 systemType += "_" + hwKeyword;
1126 }
1127 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001128 }
1129 catch (json::parse_error& ex)
1130 {
1131 throw((VpdJsonException("System Json parsing failed",
1132 SYSTEM_JSON)));
1133 }
1134
Alpana Kumarif05effd2021-04-07 07:32:53 -05001135 setDevTreeEnv(systemType);
1136 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301137 }
1138
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301139 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001140 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301141}
1142
1143int main(int argc, char** argv)
1144{
1145 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001146 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001147 Binary vpdVector{};
1148 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001149 // map to hold additional data in case of logging pel
1150 PelAdditionalData additionalData{};
1151
1152 // this is needed to hold base fru inventory path in case there is ECC or
1153 // vpd exception while parsing the file
1154 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301155
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001156 // severity for PEL
1157 PelSeverity pelSeverity = PelSeverity::WARNING;
1158
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301159 try
1160 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301161 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1162 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301163
1164 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001165 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301166
1167 CLI11_PARSE(app, argc, argv);
1168
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301169 cout << "Parser launched with file: " << file << "\n";
1170
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001171 // PEL severity should be ERROR in case of any system VPD failure
1172 if (file == systemVpdFilePath)
1173 {
1174 pelSeverity = PelSeverity::ERROR;
1175 }
1176
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301177 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1178
1179 // If the symlink exists, it means it has been setup for us, switch the
1180 // path
1181 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1182 {
1183 jsonToParse = INVENTORY_JSON_SYM_LINK;
1184 }
1185
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301186 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301187 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001188 if (!inventoryJson)
1189 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001190 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001191 }
1192
1193 try
1194 {
1195 js = json::parse(inventoryJson);
1196 }
1197 catch (json::parse_error& ex)
1198 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001199 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001200 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301201
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301202 // Do we have the mandatory "frus" section?
1203 if (js.find("frus") == js.end())
1204 {
1205 throw(VpdJsonException("FRUs section not found in JSON",
1206 jsonToParse));
1207 }
1208
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301209 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1210 if (file.find("/ahb:apb") != string::npos)
1211 {
1212 // Translate udev path to a generic /sys/bus/.. file path.
1213 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301214 cout << "Path after translation: " << file << "\n";
1215
1216 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301217 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301218 {
1219 return 0;
1220 }
1221 }
1222
1223 if (file.empty())
1224 {
1225 cerr << "The EEPROM path <" << file << "> is not valid.";
1226 return 0;
1227 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301228 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301229 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001230 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +05301231 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301232 }
1233
Alpana Kumari2f793042020-08-18 05:51:03 -05001234 if (!fs::exists(file))
1235 {
1236 cout << "Device path: " << file
1237 << " does not exist. Spurious udev event? Exiting." << endl;
1238 return 0;
1239 }
1240
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001241 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301242 // Check if we can read the VPD file based on the power state
1243 if (js["frus"][file].at(0).value("powerOffOnly", false))
1244 {
1245 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1246 getPowerState())
1247 {
1248 cout << "This VPD cannot be read when power is ON" << endl;
1249 return 0;
1250 }
1251 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001252
Alpana Kumari2f793042020-08-18 05:51:03 -05001253 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301254 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001255 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001256 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001257 variant<KeywordVpdMap, Store> parseResult;
1258 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001259
Alpana Kumari2f793042020-08-18 05:51:03 -05001260 if (auto pVal = get_if<Store>(&parseResult))
1261 {
1262 populateDbus(pVal->getVpdMap(), js, file);
1263 }
1264 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1265 {
1266 populateDbus(*pVal, js, file);
1267 }
1268
1269 // release the parser object
1270 ParserFactory::freeParser(parser);
1271 }
1272 catch (exception& e)
1273 {
1274 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001275 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001276 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301277 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001278 catch (const VpdJsonException& ex)
1279 {
1280 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1281 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001282 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001283
1284 cerr << ex.what() << "\n";
1285 rc = -1;
1286 }
1287 catch (const VpdEccException& ex)
1288 {
1289 additionalData.emplace("DESCRIPTION", "ECC check failed");
1290 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1291 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001292 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001293 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001294 cerr << ex.what() << "\n";
1295 rc = -1;
1296 }
1297 catch (const VpdDataException& ex)
1298 {
1299 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1300 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1301 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001302 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001303 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001304 cerr << ex.what() << "\n";
1305 rc = -1;
1306 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301307 catch (exception& e)
1308 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001309 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301310 cerr << e.what() << "\n";
1311 rc = -1;
1312 }
1313
1314 return rc;
Alpana Kumari3ab26a72021-04-05 19:09:19 +00001315}