blob: cb842fa8b406d46e209581bfa5fedab41fbc62dd [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 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500137 catch (const 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 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500269 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530270 {
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 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500314 else if (itr.value().is_number())
315 {
316 // For now assume the value is a size_t. In the future it would
317 // be nice to come up with a way to get the type from the JSON.
318 props.emplace(busProp, itr.value().get<size_t>());
319 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530320 }
321 interfaces.emplace(inf, move(props));
322 }
323}
324
Alpana Kumari2f793042020-08-18 05:51:03 -0500325static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500326{
327 uint32_t offset = 0;
328 // check if offset present?
329 for (const auto& item : js["frus"][file])
330 {
331 if (item.find("offset") != item.end())
332 {
333 offset = item["offset"];
334 }
335 }
336
337 // TODO: Figure out a better way to get max possible VPD size.
338 Binary vpdVector;
339 vpdVector.resize(65504);
340 ifstream vpdFile;
341 vpdFile.open(file, ios::binary);
342
343 vpdFile.seekg(offset, ios_base::cur);
344 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
345 vpdVector.resize(vpdFile.gcount());
346
347 return vpdVector;
348}
349
Alpana Kumari2f793042020-08-18 05:51:03 -0500350/** This API will be called at the end of VPD collection to perform any post
351 * actions.
352 *
353 * @param[in] json - json object
354 * @param[in] file - eeprom file path
355 */
356static void postFailAction(const nlohmann::json& json, const string& file)
357{
358 if ((json["frus"][file].at(0)).find("postActionFail") ==
359 json["frus"][file].at(0).end())
360 {
361 return;
362 }
363
364 uint8_t pinValue = 0;
365 string pinName;
366
367 for (const auto& postAction :
368 (json["frus"][file].at(0))["postActionFail"].items())
369 {
370 if (postAction.key() == "pin")
371 {
372 pinName = postAction.value();
373 }
374 else if (postAction.key() == "value")
375 {
376 // Get the value to set
377 pinValue = postAction.value();
378 }
379 }
380
381 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
382
383 try
384 {
385 gpiod::line outputLine = gpiod::find_line(pinName);
386
387 if (!outputLine)
388 {
389 cout << "Couldn't find output line:" << pinName
390 << " on GPIO. Skipping...\n";
391
392 return;
393 }
394 outputLine.request(
395 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
396 pinValue);
397 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500398 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500399 {
400 cerr << "Failed to set post-action GPIO" << endl;
401 }
402}
403
404/** Performs any pre-action needed to get the FRU setup for collection.
405 *
406 * @param[in] json - json object
407 * @param[in] file - eeprom file path
408 */
409static void preAction(const nlohmann::json& json, const string& file)
410{
411 if ((json["frus"][file].at(0)).find("preAction") ==
412 json["frus"][file].at(0).end())
413 {
414 return;
415 }
416
417 uint8_t pinValue = 0;
418 string pinName;
419
420 for (const auto& postAction :
421 (json["frus"][file].at(0))["preAction"].items())
422 {
423 if (postAction.key() == "pin")
424 {
425 pinName = postAction.value();
426 }
427 else if (postAction.key() == "value")
428 {
429 // Get the value to set
430 pinValue = postAction.value();
431 }
432 }
433
434 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
435 try
436 {
437 gpiod::line outputLine = gpiod::find_line(pinName);
438
439 if (!outputLine)
440 {
441 cout << "Couldn't find output line:" << pinName
442 << " on GPIO. Skipping...\n";
443
444 return;
445 }
446 outputLine.request(
447 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
448 pinValue);
449 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500450 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500451 {
452 cerr << "Failed to set pre-action GPIO" << endl;
453 return;
454 }
455
456 // Now bind the device
Alpana Kumari4c3bf5b2021-09-16 07:24:58 -0500457 string bind = json["frus"][file].at(0).value("devAddress", "");
Alpana Kumari2f793042020-08-18 05:51:03 -0500458 cout << "Binding device " << bind << endl;
459 string bindCmd = string("echo \"") + bind +
460 string("\" > /sys/bus/i2c/drivers/at24/bind");
461 cout << bindCmd << endl;
462 executeCmd(bindCmd);
463
464 // Check if device showed up (test for file)
465 if (!fs::exists(file))
466 {
467 cout << "EEPROM " << file << " does not exist. Take failure action"
468 << endl;
469 // If not, then take failure postAction
470 postFailAction(json, file);
471 }
472}
473
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530474/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530475 * @brief Set certain one time properties in the inventory
476 * Use this function to insert the Functional and Enabled properties into the
477 * inventory map. This function first checks if the object in question already
478 * has these properties hosted on D-Bus, if the property is already there, it is
479 * not modified, hence the name "one time". If the property is not already
480 * present, it will be added to the map with a suitable default value (true for
481 * Functional and false for Enabled)
482 *
483 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
484 * @param[inout] interfaces - Reference to a map of inventory interfaces to
485 * which the properties will be attached.
486 */
487static void setOneTimeProperties(const std::string& object,
488 inventory::InterfaceMap& interfaces)
489{
490 auto bus = sdbusplus::bus::new_default();
491 auto objectPath = INVENTORY_PATH + object;
492 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
493 objectPath.c_str(),
494 "org.freedesktop.DBus.Properties", "Get");
495 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
496 prop.append("Functional");
497 try
498 {
499 auto result = bus.call(prop);
500 }
501 catch (const sdbusplus::exception::SdBusError& e)
502 {
503 // Treat as property unavailable
504 inventory::PropertyMap prop;
505 prop.emplace("Functional", true);
506 interfaces.emplace(
507 "xyz.openbmc_project.State.Decorator.OperationalStatus",
508 move(prop));
509 }
510 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
511 objectPath.c_str(),
512 "org.freedesktop.DBus.Properties", "Get");
513 prop.append("xyz.openbmc_project.Object.Enable");
514 prop.append("Enabled");
515 try
516 {
517 auto result = bus.call(prop);
518 }
519 catch (const sdbusplus::exception::SdBusError& e)
520 {
521 // Treat as property unavailable
522 inventory::PropertyMap prop;
523 prop.emplace("Enabled", false);
524 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
525 }
526}
527
528/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530529 * @brief Prime the Inventory
530 * Prime the inventory by populating only the location code,
531 * type interface and the inventory object for the frus
532 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530533 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530534 * @param[in] jsObject - Reference to vpd inventory json object
535 * @param[in] vpdMap - Reference to the parsed vpd map
536 *
537 * @returns Map of items in extraInterface.
538 */
539template <typename T>
540inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
541 const T& vpdMap)
542{
543 inventory::ObjectMap objects;
544
545 for (auto& itemFRUS : jsObject["frus"].items())
546 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500547 // Take pre actions
548 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530549 for (auto& itemEEPROM : itemFRUS.value())
550 {
551 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530552 inventory::Object object(itemEEPROM.at("inventoryPath"));
553
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530554 if ((itemFRUS.key() != systemVpdFilePath) &&
555 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530556 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600557 inventory::PropertyMap presProp;
558 presProp.emplace("Present", false);
559 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530560 presProp);
561 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530562 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
563 {
564 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
565 {
566 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000567 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530568 {
569 if constexpr (std::is_same<T, Parsed>::value)
570 {
571 for (auto& lC : eI.value().items())
572 {
573 auto propVal = expandLocationCode(
574 lC.value().get<string>(), vpdMap, true);
575
576 props.emplace(move(lC.key()),
577 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530578 interfaces.emplace(XYZ_LOCATION_CODE_INF,
579 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530580 interfaces.emplace(move(eI.key()),
581 move(props));
582 }
583 }
584 }
585 else if (eI.key().find("Inventory.Item.") !=
586 string::npos)
587 {
588 interfaces.emplace(move(eI.key()), move(props));
589 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530590 else if (eI.key() ==
591 "xyz.openbmc_project.Inventory.Item")
592 {
593 for (auto& val : eI.value().items())
594 {
595 if (val.key() == "PrettyName")
596 {
597 presProp.emplace(val.key(),
598 val.value().get<string>());
599 }
600 }
601 // Use insert_or_assign here as we may already have
602 // inserted the present property only earlier in
603 // this function under this same interface.
604 interfaces.insert_or_assign(eI.key(),
605 move(presProp));
606 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530607 }
608 }
609 objects.emplace(move(object), move(interfaces));
610 }
611 }
612 }
613 return objects;
614}
615
Alpana Kumari65b83602020-09-01 00:24:56 -0500616/**
617 * @brief This API executes command to set environment variable
618 * And then reboot the system
619 * @param[in] key -env key to set new value
620 * @param[in] value -value to set.
621 */
622void setEnvAndReboot(const string& key, const string& value)
623{
624 // set env and reboot and break.
625 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600626 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500627 // make dbus call to reboot
628 auto bus = sdbusplus::bus::new_default_system();
629 auto method = bus.new_method_call(
630 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
631 "org.freedesktop.systemd1.Manager", "Reboot");
632 bus.call_noreply(method);
633}
634
635/*
636 * @brief This API checks for env var fitconfig.
637 * If not initialised OR updated as per the current system type,
638 * update this env var and reboot the system.
639 *
640 * @param[in] systemType IM kwd in vpd tells about which system type it is.
641 * */
642void setDevTreeEnv(const string& systemType)
643{
Alpana Kumari37e72702021-11-18 11:18:04 -0600644 // Init with default dtb
645 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Alpana Kumari65b83602020-09-01 00:24:56 -0500646
647 if (deviceTreeSystemTypeMap.find(systemType) !=
648 deviceTreeSystemTypeMap.end())
649 {
650 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
651 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600652 else
653 {
654 // System type not supported
655 cerr << "This System type not found/supported in dtb table "
656 << systemType << ". so system will be booted with default dtb.\n";
657 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500658
659 string readVarValue;
660 bool envVarFound = false;
661
662 vector<string> output = executeCmd("/sbin/fw_printenv");
663 for (const auto& entry : output)
664 {
665 size_t pos = entry.find("=");
666 string key = entry.substr(0, pos);
667 if (key != "fitconfig")
668 {
669 continue;
670 }
671
672 envVarFound = true;
673 if (pos + 1 < entry.size())
674 {
675 readVarValue = entry.substr(pos + 1);
676 if (readVarValue.find(newDeviceTree) != string::npos)
677 {
678 // fitconfig is Updated. No action needed
679 break;
680 }
681 }
682 // set env and reboot and break.
683 setEnvAndReboot(key, newDeviceTree);
684 exit(0);
685 }
686
687 // check If env var Not found
688 if (!envVarFound)
689 {
690 setEnvAndReboot("fitconfig", newDeviceTree);
691 }
692}
693
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530694/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500695 * @brief API to call VPD manager to write VPD to EEPROM.
696 * @param[in] Object path.
697 * @param[in] record to be updated.
698 * @param[in] keyword to be updated.
699 * @param[in] keyword data to be updated
700 */
701void updateHardware(const string& objectName, const string& recName,
702 const string& kwdName, const Binary& data)
703{
704 try
705 {
706 auto bus = sdbusplus::bus::new_default();
707 auto properties =
708 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
709 properties.append(
710 static_cast<sdbusplus::message::object_path>(objectName));
711 properties.append(recName);
712 properties.append(kwdName);
713 properties.append(data);
714 bus.call(properties);
715 }
Patrick Williams8be43342021-09-02 09:33:36 -0500716 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500717 {
718 std::string what =
719 "VPDManager WriteKeyword api failed for inventory path " +
720 objectName;
721 what += " record " + recName;
722 what += " keyword " + kwdName;
723 what += " with bus error = " + std::string(e.what());
724
725 // map to hold additional data in case of logging pel
726 PelAdditionalData additionalData{};
727 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
728 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500729 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500730 }
731}
732
733/**
734 * @brief API to check if we need to restore system VPD
735 * This functionality is only applicable for IPZ VPD data.
736 * @param[in] vpdMap - IPZ vpd map
737 * @param[in] objectPath - Object path for the FRU
738 * @return EEPROMs with records and keywords updated at standby
739 */
740std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
741 const string& objectPath)
742{
743 // the list of keywords for VSYS record is as per the S0 system. Should be
744 // updated for another type of systems
745 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
746 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
747 {"VCEN", {"FC", "SE"}},
748 {"LXR0", {"LX"}}};
749
750 // vector to hold all the EEPROMs updated at standby
751 std::vector<RestoredEeproms> updatedEeproms = {};
752
753 for (const auto& systemRecKwdPair : svpdKwdMap)
754 {
755 auto it = vpdMap.find(systemRecKwdPair.first);
756
757 // check if record is found in map we got by parser
758 if (it != vpdMap.end())
759 {
760 const auto& kwdListForRecord = systemRecKwdPair.second;
761 for (const auto& keyword : kwdListForRecord)
762 {
763 DbusPropertyMap& kwdValMap = it->second;
764 auto iterator = kwdValMap.find(keyword);
765
766 if (iterator != kwdValMap.end())
767 {
768 string& kwdValue = iterator->second;
769
770 // check bus data
771 const string& recordName = systemRecKwdPair.first;
772 const string& busValue = readBusProperty(
773 objectPath, ipzVpdInf + recordName, keyword);
774
775 if (busValue.find_first_not_of(' ') != string::npos)
776 {
777 if (kwdValue.find_first_not_of(' ') != string::npos)
778 {
779 // both the data are present, check for mismatch
780 if (busValue != kwdValue)
781 {
782 string errMsg = "VPD data mismatch on cache "
783 "and hardware for record: ";
784 errMsg += (*it).first;
785 errMsg += " and keyword: ";
786 errMsg += keyword;
787
788 // data mismatch
789 PelAdditionalData additionalData;
790 additionalData.emplace("CALLOUT_INVENTORY_PATH",
791 objectPath);
792
793 additionalData.emplace("DESCRIPTION", errMsg);
794
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500795 createPEL(additionalData, PelSeverity::WARNING,
796 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500797 }
798 }
799 else
800 {
801 // implies hardware data is blank
802 // update the map
803 Binary busData(busValue.begin(), busValue.end());
804
805 updatedEeproms.push_back(std::make_tuple(
806 objectPath, recordName, keyword, busData));
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500807
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500808 // update the map as well, so that cache data is not
809 // updated as blank while populating VPD map on Dbus
810 // in populateDBus Api
811 kwdValue = busValue;
812 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500813 }
814 else if (kwdValue.find_first_not_of(' ') == string::npos)
815 {
816 string errMsg = "VPD is blank on both cache and "
817 "hardware for record: ";
818 errMsg += (*it).first;
819 errMsg += " and keyword: ";
820 errMsg += keyword;
821 errMsg += ". SSR need to update hardware VPD.";
822
823 // both the data are blanks, log PEL
824 PelAdditionalData additionalData;
825 additionalData.emplace("CALLOUT_INVENTORY_PATH",
826 objectPath);
827
828 additionalData.emplace("DESCRIPTION", errMsg);
829
830 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500831 createPEL(additionalData, PelSeverity::ERROR,
832 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500833 continue;
834 }
835 }
836 }
837 }
838 }
839
840 return updatedEeproms;
841}
842
843/**
alpana077ce68722021-07-25 13:23:59 -0500844 * @brief This checks for is this FRU a processor
845 * And if yes, then checks for is this primary
846 *
847 * @param[in] js- vpd json to get the information about this FRU
848 * @param[in] filePath- FRU vpd
849 *
850 * @return true/false
851 */
852bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
853{
854 bool isProcessor = false;
855 bool isPrimary = false;
856
857 for (const auto& item : js["frus"][filePath])
858 {
859 if (item.find("extraInterfaces") != item.end())
860 {
861 for (const auto& eI : item["extraInterfaces"].items())
862 {
863 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
864 {
865 isProcessor = true;
866 }
867 }
868 }
869
870 if (isProcessor)
871 {
872 string cpuType = item.value("cpuType", "");
873 if (cpuType == "primary")
874 {
875 isPrimary = true;
876 }
877 }
878 }
879
880 return (isProcessor && isPrimary);
881}
882
883/**
884 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
885 * driver
886 * @param[in] js- vpd json to iterate through and take action if it is DIMM
887 */
888void doEnableAllDimms(nlohmann::json& js)
889{
890 // iterate over each fru
891 for (const auto& eachFru : js["frus"].items())
892 {
893 // skip the driver binding if eeprom already exists
894 if (fs::exists(eachFru.key()))
895 {
896 continue;
897 }
898
899 for (const auto& eachInventory : eachFru.value())
900 {
901 if (eachInventory.find("extraInterfaces") != eachInventory.end())
902 {
903 for (const auto& eI : eachInventory["extraInterfaces"].items())
904 {
905 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
906 {
907 string dimmVpd = eachFru.key();
908 // fetch it from
909 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
910
911 regex matchPatern("([0-9]+-[0-9]{4})");
912 smatch matchFound;
913 if (regex_search(dimmVpd, matchFound, matchPatern))
914 {
915 vector<string> i2cReg;
916 boost::split(i2cReg, matchFound.str(0),
917 boost::is_any_of("-"));
918
919 // remove 0s from begining
920 const regex pattern("^0+(?!$)");
921 for (auto& i : i2cReg)
922 {
923 i = regex_replace(i, pattern, "");
924 }
925
926 if (i2cReg.size() == 2)
927 {
928 // echo 24c32 0x50 >
929 // /sys/bus/i2c/devices/i2c-16/new_device
930 string cmnd = "echo 24c32 0x" + i2cReg[1] +
931 " > /sys/bus/i2c/devices/i2c-" +
932 i2cReg[0] + "/new_device";
933
934 executeCmd(cmnd);
935 }
936 }
937 }
938 }
939 }
940 }
941 }
942}
943
944/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530945 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530946 * This method invokes all the populateInterface functions
947 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530948 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
949 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530950 * @param[in] js - Inventory json object
951 * @param[in] filePath - Path of the vpd file
952 * @param[in] preIntrStr - Interface string
953 */
954template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500955static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530956{
957 inventory::InterfaceMap interfaces;
958 inventory::ObjectMap objects;
959 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -0600960 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530961
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500962 // map to hold all the keywords whose value has been changed at standby
963 vector<RestoredEeproms> updatedEeproms = {};
964
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530965 bool isSystemVpd = (filePath == systemVpdFilePath);
966 if constexpr (is_same<T, Parsed>::value)
967 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -0600968 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
969 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
970 ::toupper);
971
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530972 if (isSystemVpd)
973 {
974 std::vector<std::string> interfaces = {motherBoardInterface};
975 // call mapper to check for object path creation
976 MapperResponse subTree =
977 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
978 string mboardPath =
979 js["frus"][filePath].at(0).value("inventoryPath", "");
980
981 // Attempt system VPD restore if we have a motherboard
982 // object in the inventory.
983 if ((subTree.size() != 0) &&
984 (subTree.find(pimPath + mboardPath) != subTree.end()))
985 {
986 updatedEeproms = restoreSystemVPD(vpdMap, mboardPath);
987 }
988 else
989 {
990 log<level::ERR>("No object path found");
991 }
992 }
alpana077ce68722021-07-25 13:23:59 -0500993 else
994 {
995 // check if it is processor vpd.
996 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
997
998 if (isPrimaryCpu)
999 {
1000 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1001
1002 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1003
1004 if (chipVersion >= 2)
1005 {
1006 doEnableAllDimms(js);
1007 }
1008 }
1009 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301010 }
1011
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301012 for (const auto& item : js["frus"][filePath])
1013 {
1014 const auto& objectPath = item["inventoryPath"];
1015 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001016
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001017 vector<string> ccinList;
1018 if (item.find("ccin") != item.end())
1019 {
1020 for (const auto& cc : item["ccin"])
1021 {
1022 string ccin = cc;
1023 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1024 ccinList.push_back(ccin);
1025 }
1026 }
1027
1028 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1029 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1030 ccinList.end()))
1031 {
1032 continue;
1033 }
1034
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301035 if (isSystemVpd)
1036 {
1037 // Populate one time properties for the system VPD and its sub-frus.
1038 // For the remaining FRUs, this will get handled as a part of
1039 // priming the inventory.
1040 setOneTimeProperties(objectPath, interfaces);
1041 }
1042
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301043 // Populate the VPD keywords and the common interfaces only if we
1044 // are asked to inherit that data from the VPD, else only add the
1045 // extraInterfaces.
1046 if (item.value("inherit", true))
1047 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001048 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301049 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301050 // Each record in the VPD becomes an interface and all
1051 // keyword within the record are properties under that
1052 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301053 for (const auto& record : vpdMap)
1054 {
1055 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001056 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301057 }
1058 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001059 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301060 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001061 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301062 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301063 if (js.find("commonInterfaces") != js.end())
1064 {
1065 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1066 isSystemVpd);
1067 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301068 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001069 else
1070 {
1071 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001072 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001073 {
1074 if (item.find("copyRecords") != item.end())
1075 {
1076 for (const auto& record : item["copyRecords"])
1077 {
1078 const string& recordName = record;
1079 if (vpdMap.find(recordName) != vpdMap.end())
1080 {
1081 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001082 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001083 interfaces);
1084 }
1085 }
1086 }
1087 }
1088 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001089 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301090 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001091 // Populate interfaces and properties that are common to every FRU
1092 // and additional interface that might be defined on a per-FRU
1093 // basis.
1094 if (item.find("extraInterfaces") != item.end())
1095 {
1096 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1097 isSystemVpd);
1098 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301099 }
1100 objects.emplace(move(object), move(interfaces));
1101 }
1102
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301103 if (isSystemVpd)
1104 {
Santosh Puranikd278df12021-05-10 15:51:42 +05301105 string systemJsonName{};
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301106 if constexpr (is_same<T, Parsed>::value)
1107 {
Alpana Kumarif05effd2021-04-07 07:32:53 -05001108 // pick the right system json
Santosh Puranikd278df12021-05-10 15:51:42 +05301109 systemJsonName = getSystemsJson(vpdMap);
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301110 }
1111
Santosh Puranikd278df12021-05-10 15:51:42 +05301112 fs::path target = systemJsonName;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301113 fs::path link = INVENTORY_JSON_SYM_LINK;
1114
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301115 // Create the directory for hosting the symlink
1116 fs::create_directories(VPD_FILES_PATH);
1117 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +05301118 remove(INVENTORY_JSON_SYM_LINK);
1119 // create a new symlink based on the system
1120 fs::create_symlink(target, link);
1121
1122 // Reloading the json
1123 ifstream inventoryJson(link);
1124 auto js = json::parse(inventoryJson);
1125 inventoryJson.close();
1126
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301127 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1128 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001129
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001130 // if system VPD has been restored at standby, update the EEPROM
1131 for (const auto& item : updatedEeproms)
1132 {
1133 updateHardware(get<0>(item), get<1>(item), get<2>(item),
1134 get<3>(item));
1135 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001136
1137 // set the U-boot environment variable for device-tree
1138 if constexpr (is_same<T, Parsed>::value)
1139 {
1140 const string imKeyword = getIM(vpdMap);
1141 const string hwKeyword = getHW(vpdMap);
1142 string systemType = imKeyword;
1143
1144 // check If system has constraint then append HW version to it.
1145 ifstream sysJson(SYSTEM_JSON);
1146 if (!sysJson)
1147 {
1148 throw((VpdJsonException("Failed to access Json path",
1149 SYSTEM_JSON)));
1150 }
1151
1152 try
1153 {
1154 auto systemJson = json::parse(sysJson);
Santosh Puranikd278df12021-05-10 15:51:42 +05301155 if (systemJson["system"].find(imKeyword) !=
1156 systemJson["system"].end())
1157 {
1158 if (systemJson["system"][imKeyword].find("constraint") !=
1159 systemJson["system"][imKeyword].end())
1160 {
1161 systemType += "_" + hwKeyword;
1162 }
1163 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001164 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001165 catch (const json::parse_error& ex)
Alpana Kumarif05effd2021-04-07 07:32:53 -05001166 {
1167 throw((VpdJsonException("System Json parsing failed",
1168 SYSTEM_JSON)));
1169 }
1170
Alpana Kumarif05effd2021-04-07 07:32:53 -05001171 setDevTreeEnv(systemType);
1172 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301173 }
1174
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301175 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001176 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301177}
1178
1179int main(int argc, char** argv)
1180{
1181 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001182 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001183 Binary vpdVector{};
1184 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001185 // map to hold additional data in case of logging pel
1186 PelAdditionalData additionalData{};
1187
1188 // this is needed to hold base fru inventory path in case there is ECC or
1189 // vpd exception while parsing the file
1190 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301191
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001192 // severity for PEL
1193 PelSeverity pelSeverity = PelSeverity::WARNING;
1194
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301195 try
1196 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301197 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1198 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301199
1200 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001201 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301202
1203 CLI11_PARSE(app, argc, argv);
1204
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001205 // PEL severity should be ERROR in case of any system VPD failure
1206 if (file == systemVpdFilePath)
1207 {
1208 pelSeverity = PelSeverity::ERROR;
1209 }
1210
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301211 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1212
1213 // If the symlink exists, it means it has been setup for us, switch the
1214 // path
1215 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1216 {
1217 jsonToParse = INVENTORY_JSON_SYM_LINK;
1218 }
1219
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301220 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301221 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001222 if (!inventoryJson)
1223 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001224 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001225 }
1226
1227 try
1228 {
1229 js = json::parse(inventoryJson);
1230 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001231 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001232 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001233 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001234 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301235
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301236 // Do we have the mandatory "frus" section?
1237 if (js.find("frus") == js.end())
1238 {
1239 throw(VpdJsonException("FRUs section not found in JSON",
1240 jsonToParse));
1241 }
1242
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301243 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1244 if (file.find("/ahb:apb") != string::npos)
1245 {
1246 // Translate udev path to a generic /sys/bus/.. file path.
1247 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301248
1249 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301250 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301251 {
1252 return 0;
1253 }
1254 }
1255
1256 if (file.empty())
1257 {
1258 cerr << "The EEPROM path <" << file << "> is not valid.";
1259 return 0;
1260 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301261 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301262 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301263 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301264 }
1265
Alpana Kumari2f793042020-08-18 05:51:03 -05001266 if (!fs::exists(file))
1267 {
1268 cout << "Device path: " << file
1269 << " does not exist. Spurious udev event? Exiting." << endl;
1270 return 0;
1271 }
1272
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001273 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301274 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001275 // We skip reading VPD when the power is ON in two scenarios:
1276 // 1) The eeprom we are trying to read is that of the system VPD
1277 // 2) The JSON tells us that the FRU EEPROM cannot be read when
1278 // we are powered ON.
1279 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
1280 (file == systemVpdFilePath))
Santosh Puranik85893752020-11-10 21:31:43 +05301281 {
1282 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1283 getPowerState())
1284 {
1285 cout << "This VPD cannot be read when power is ON" << endl;
1286 return 0;
1287 }
1288 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001289
Alpana Kumari2f793042020-08-18 05:51:03 -05001290 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301291 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001292 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001293 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001294 variant<KeywordVpdMap, Store> parseResult;
1295 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001296
Alpana Kumari2f793042020-08-18 05:51:03 -05001297 if (auto pVal = get_if<Store>(&parseResult))
1298 {
1299 populateDbus(pVal->getVpdMap(), js, file);
1300 }
1301 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1302 {
1303 populateDbus(*pVal, js, file);
1304 }
1305
1306 // release the parser object
1307 ParserFactory::freeParser(parser);
1308 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001309 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001310 {
1311 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001312 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001313 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301314 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001315 catch (const VpdJsonException& ex)
1316 {
1317 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1318 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001319 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001320
1321 cerr << ex.what() << "\n";
1322 rc = -1;
1323 }
1324 catch (const VpdEccException& ex)
1325 {
1326 additionalData.emplace("DESCRIPTION", "ECC check failed");
1327 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1328 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001329 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001330 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001331 cerr << ex.what() << "\n";
1332 rc = -1;
1333 }
1334 catch (const VpdDataException& ex)
1335 {
1336 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1337 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1338 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001339 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001340 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001341 cerr << ex.what() << "\n";
1342 rc = -1;
1343 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001344 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301345 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001346 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301347 cerr << e.what() << "\n";
1348 rc = -1;
1349 }
1350
1351 return rc;
Alpana Kumari37e72702021-11-18 11:18:04 -06001352}