blob: c5c1452a4c6abcb164a1ad9634817fe0f3aeec7f [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);
Priyanga Ramasamy69f76022022-01-05 07:10:36 +0000252 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530253 }
254 else
255 {
256 props.emplace(busProp, itr.value().get<string>());
257 }
258 }
259 else
260 {
261 props.emplace(busProp, itr.value().get<string>());
262 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530263 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530264 else if (itr.value().is_array())
265 {
266 try
267 {
268 props.emplace(busProp, itr.value().get<Binary>());
269 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500270 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530271 {
272 std::cerr << "Type exception: " << e.what() << "\n";
273 // Ignore any type errors
274 }
275 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600276 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530277 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600278 const string& rec = itr.value().value("recordName", "");
279 const string& kw = itr.value().value("keywordName", "");
280 const string& encoding = itr.value().value("encoding", "");
281
Alpana Kumari58e22142020-05-05 00:22:12 -0500282 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530283 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530284 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
285 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600286 {
287 auto encoded =
288 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530289 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600290 }
291 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500292 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600293 {
294 if (!kw.empty() && vpdMap.count(kw))
295 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000296 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
297 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
298
299 if (kwValue)
300 {
301 auto prop =
302 string((*kwValue).begin(), (*kwValue).end());
303
304 auto encoded = encodeKeyword(prop, encoding);
305
306 props.emplace(busProp, encoded);
307 }
308 else if (uintValue)
309 {
310 props.emplace(busProp, *uintValue);
311 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600312 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530313 }
314 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500315 else if (itr.value().is_number())
316 {
317 // For now assume the value is a size_t. In the future it would
318 // be nice to come up with a way to get the type from the JSON.
319 props.emplace(busProp, itr.value().get<size_t>());
320 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530321 }
322 interfaces.emplace(inf, move(props));
323 }
324}
325
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600326/*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
327 * Currently do reset only for DIMM VPD.*/
328static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
329 ifstream& vpdFile)
330{
331 for (const auto& item : js["frus"][file])
332 {
333 if (item.find("extraInterfaces") != item.end())
334 {
335 if (item["extraInterfaces"].find(
336 "xyz.openbmc_project.Inventory.Item.Dimm") !=
337 item["extraInterfaces"].end())
338 {
339 // moves the EEPROM pointer to 2048 'th byte.
340 vpdFile.seekg(2047, std::ios::beg);
341 // Read that byte and discard - to affirm the move
342 // operation.
343 char ch;
344 vpdFile.read(&ch, sizeof(ch));
345 }
346 return;
347 }
348 }
349}
350
Alpana Kumari2f793042020-08-18 05:51:03 -0500351static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500352{
353 uint32_t offset = 0;
354 // check if offset present?
355 for (const auto& item : js["frus"][file])
356 {
357 if (item.find("offset") != item.end())
358 {
359 offset = item["offset"];
360 }
361 }
362
363 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600364 auto maxVPDSize = std::min(std::filesystem::file_size(file),
365 static_cast<uintmax_t>(65504));
366
Alpana Kumari58e22142020-05-05 00:22:12 -0500367 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600368 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500369 ifstream vpdFile;
370 vpdFile.open(file, ios::binary);
371
372 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600373 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500374 vpdVector.resize(vpdFile.gcount());
375
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600376 resetEEPROMPointer(js, file, vpdFile);
377
Alpana Kumari58e22142020-05-05 00:22:12 -0500378 return vpdVector;
379}
380
Alpana Kumari2f793042020-08-18 05:51:03 -0500381/** This API will be called at the end of VPD collection to perform any post
382 * actions.
383 *
384 * @param[in] json - json object
385 * @param[in] file - eeprom file path
386 */
387static void postFailAction(const nlohmann::json& json, const string& file)
388{
389 if ((json["frus"][file].at(0)).find("postActionFail") ==
390 json["frus"][file].at(0).end())
391 {
392 return;
393 }
394
395 uint8_t pinValue = 0;
396 string pinName;
397
398 for (const auto& postAction :
399 (json["frus"][file].at(0))["postActionFail"].items())
400 {
401 if (postAction.key() == "pin")
402 {
403 pinName = postAction.value();
404 }
405 else if (postAction.key() == "value")
406 {
407 // Get the value to set
408 pinValue = postAction.value();
409 }
410 }
411
412 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
413
414 try
415 {
416 gpiod::line outputLine = gpiod::find_line(pinName);
417
418 if (!outputLine)
419 {
420 cout << "Couldn't find output line:" << pinName
421 << " on GPIO. Skipping...\n";
422
423 return;
424 }
425 outputLine.request(
426 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
427 pinValue);
428 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500429 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500430 {
431 cerr << "Failed to set post-action GPIO" << endl;
432 }
433}
434
435/** Performs any pre-action needed to get the FRU setup for collection.
436 *
437 * @param[in] json - json object
438 * @param[in] file - eeprom file path
439 */
440static void preAction(const nlohmann::json& json, const string& file)
441{
442 if ((json["frus"][file].at(0)).find("preAction") ==
443 json["frus"][file].at(0).end())
444 {
445 return;
446 }
447
448 uint8_t pinValue = 0;
449 string pinName;
450
451 for (const auto& postAction :
452 (json["frus"][file].at(0))["preAction"].items())
453 {
454 if (postAction.key() == "pin")
455 {
456 pinName = postAction.value();
457 }
458 else if (postAction.key() == "value")
459 {
460 // Get the value to set
461 pinValue = postAction.value();
462 }
463 }
464
465 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
466 try
467 {
468 gpiod::line outputLine = gpiod::find_line(pinName);
469
470 if (!outputLine)
471 {
472 cout << "Couldn't find output line:" << pinName
473 << " on GPIO. Skipping...\n";
474
475 return;
476 }
477 outputLine.request(
478 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
479 pinValue);
480 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500481 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500482 {
483 cerr << "Failed to set pre-action GPIO" << endl;
484 return;
485 }
486
487 // Now bind the device
Alpana Kumari4c3bf5b2021-09-16 07:24:58 -0500488 string bind = json["frus"][file].at(0).value("devAddress", "");
Alpana Kumari2f793042020-08-18 05:51:03 -0500489 cout << "Binding device " << bind << endl;
490 string bindCmd = string("echo \"") + bind +
491 string("\" > /sys/bus/i2c/drivers/at24/bind");
492 cout << bindCmd << endl;
493 executeCmd(bindCmd);
494
495 // Check if device showed up (test for file)
496 if (!fs::exists(file))
497 {
498 cout << "EEPROM " << file << " does not exist. Take failure action"
499 << endl;
500 // If not, then take failure postAction
501 postFailAction(json, file);
502 }
503}
504
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530505/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530506 * @brief Set certain one time properties in the inventory
507 * Use this function to insert the Functional and Enabled properties into the
508 * inventory map. This function first checks if the object in question already
509 * has these properties hosted on D-Bus, if the property is already there, it is
510 * not modified, hence the name "one time". If the property is not already
511 * present, it will be added to the map with a suitable default value (true for
512 * Functional and false for Enabled)
513 *
514 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
515 * @param[inout] interfaces - Reference to a map of inventory interfaces to
516 * which the properties will be attached.
517 */
518static void setOneTimeProperties(const std::string& object,
519 inventory::InterfaceMap& interfaces)
520{
521 auto bus = sdbusplus::bus::new_default();
522 auto objectPath = INVENTORY_PATH + object;
523 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
524 objectPath.c_str(),
525 "org.freedesktop.DBus.Properties", "Get");
526 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
527 prop.append("Functional");
528 try
529 {
530 auto result = bus.call(prop);
531 }
532 catch (const sdbusplus::exception::SdBusError& e)
533 {
534 // Treat as property unavailable
535 inventory::PropertyMap prop;
536 prop.emplace("Functional", true);
537 interfaces.emplace(
538 "xyz.openbmc_project.State.Decorator.OperationalStatus",
539 move(prop));
540 }
541 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
542 objectPath.c_str(),
543 "org.freedesktop.DBus.Properties", "Get");
544 prop.append("xyz.openbmc_project.Object.Enable");
545 prop.append("Enabled");
546 try
547 {
548 auto result = bus.call(prop);
549 }
550 catch (const sdbusplus::exception::SdBusError& e)
551 {
552 // Treat as property unavailable
553 inventory::PropertyMap prop;
554 prop.emplace("Enabled", false);
555 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
556 }
557}
558
559/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530560 * @brief Prime the Inventory
561 * Prime the inventory by populating only the location code,
562 * type interface and the inventory object for the frus
563 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530564 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530565 * @param[in] jsObject - Reference to vpd inventory json object
566 * @param[in] vpdMap - Reference to the parsed vpd map
567 *
568 * @returns Map of items in extraInterface.
569 */
570template <typename T>
571inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
572 const T& vpdMap)
573{
574 inventory::ObjectMap objects;
575
576 for (auto& itemFRUS : jsObject["frus"].items())
577 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500578 // Take pre actions
579 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530580 for (auto& itemEEPROM : itemFRUS.value())
581 {
582 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530583 inventory::Object object(itemEEPROM.at("inventoryPath"));
584
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530585 if ((itemFRUS.key() != systemVpdFilePath) &&
586 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530587 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600588 inventory::PropertyMap presProp;
589 presProp.emplace("Present", false);
590 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530591 presProp);
592 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530593 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
594 {
595 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
596 {
597 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000598 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530599 {
600 if constexpr (std::is_same<T, Parsed>::value)
601 {
602 for (auto& lC : eI.value().items())
603 {
604 auto propVal = expandLocationCode(
605 lC.value().get<string>(), vpdMap, true);
606
607 props.emplace(move(lC.key()),
608 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530609 interfaces.emplace(XYZ_LOCATION_CODE_INF,
610 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530611 interfaces.emplace(move(eI.key()),
612 move(props));
613 }
614 }
615 }
616 else if (eI.key().find("Inventory.Item.") !=
617 string::npos)
618 {
619 interfaces.emplace(move(eI.key()), move(props));
620 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530621 else if (eI.key() ==
622 "xyz.openbmc_project.Inventory.Item")
623 {
624 for (auto& val : eI.value().items())
625 {
626 if (val.key() == "PrettyName")
627 {
628 presProp.emplace(val.key(),
629 val.value().get<string>());
630 }
631 }
632 // Use insert_or_assign here as we may already have
633 // inserted the present property only earlier in
634 // this function under this same interface.
635 interfaces.insert_or_assign(eI.key(),
636 move(presProp));
637 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530638 }
639 }
640 objects.emplace(move(object), move(interfaces));
641 }
642 }
643 }
644 return objects;
645}
646
Alpana Kumari65b83602020-09-01 00:24:56 -0500647/**
648 * @brief This API executes command to set environment variable
649 * And then reboot the system
650 * @param[in] key -env key to set new value
651 * @param[in] value -value to set.
652 */
653void setEnvAndReboot(const string& key, const string& value)
654{
655 // set env and reboot and break.
656 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600657 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500658 // make dbus call to reboot
659 auto bus = sdbusplus::bus::new_default_system();
660 auto method = bus.new_method_call(
661 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
662 "org.freedesktop.systemd1.Manager", "Reboot");
663 bus.call_noreply(method);
664}
665
666/*
667 * @brief This API checks for env var fitconfig.
668 * If not initialised OR updated as per the current system type,
669 * update this env var and reboot the system.
670 *
671 * @param[in] systemType IM kwd in vpd tells about which system type it is.
672 * */
673void setDevTreeEnv(const string& systemType)
674{
Alpana Kumari37e72702021-11-18 11:18:04 -0600675 // Init with default dtb
676 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Alpana Kumari65b83602020-09-01 00:24:56 -0500677
678 if (deviceTreeSystemTypeMap.find(systemType) !=
679 deviceTreeSystemTypeMap.end())
680 {
681 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
682 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600683 else
684 {
685 // System type not supported
686 cerr << "This System type not found/supported in dtb table "
687 << systemType << ". so system will be booted with default dtb.\n";
688 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500689
690 string readVarValue;
691 bool envVarFound = false;
692
693 vector<string> output = executeCmd("/sbin/fw_printenv");
694 for (const auto& entry : output)
695 {
696 size_t pos = entry.find("=");
697 string key = entry.substr(0, pos);
698 if (key != "fitconfig")
699 {
700 continue;
701 }
702
703 envVarFound = true;
704 if (pos + 1 < entry.size())
705 {
706 readVarValue = entry.substr(pos + 1);
707 if (readVarValue.find(newDeviceTree) != string::npos)
708 {
709 // fitconfig is Updated. No action needed
710 break;
711 }
712 }
713 // set env and reboot and break.
714 setEnvAndReboot(key, newDeviceTree);
715 exit(0);
716 }
717
718 // check If env var Not found
719 if (!envVarFound)
720 {
721 setEnvAndReboot("fitconfig", newDeviceTree);
722 }
723}
724
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530725/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500726 * @brief API to call VPD manager to write VPD to EEPROM.
727 * @param[in] Object path.
728 * @param[in] record to be updated.
729 * @param[in] keyword to be updated.
730 * @param[in] keyword data to be updated
731 */
732void updateHardware(const string& objectName, const string& recName,
733 const string& kwdName, const Binary& data)
734{
735 try
736 {
737 auto bus = sdbusplus::bus::new_default();
738 auto properties =
739 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
740 properties.append(
741 static_cast<sdbusplus::message::object_path>(objectName));
742 properties.append(recName);
743 properties.append(kwdName);
744 properties.append(data);
745 bus.call(properties);
746 }
Patrick Williams8be43342021-09-02 09:33:36 -0500747 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500748 {
749 std::string what =
750 "VPDManager WriteKeyword api failed for inventory path " +
751 objectName;
752 what += " record " + recName;
753 what += " keyword " + kwdName;
754 what += " with bus error = " + std::string(e.what());
755
756 // map to hold additional data in case of logging pel
757 PelAdditionalData additionalData{};
758 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
759 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500760 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500761 }
762}
763
764/**
765 * @brief API to check if we need to restore system VPD
766 * This functionality is only applicable for IPZ VPD data.
767 * @param[in] vpdMap - IPZ vpd map
768 * @param[in] objectPath - Object path for the FRU
769 * @return EEPROMs with records and keywords updated at standby
770 */
771std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
772 const string& objectPath)
773{
774 // the list of keywords for VSYS record is as per the S0 system. Should be
775 // updated for another type of systems
776 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
777 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
778 {"VCEN", {"FC", "SE"}},
779 {"LXR0", {"LX"}}};
780
781 // vector to hold all the EEPROMs updated at standby
782 std::vector<RestoredEeproms> updatedEeproms = {};
783
784 for (const auto& systemRecKwdPair : svpdKwdMap)
785 {
786 auto it = vpdMap.find(systemRecKwdPair.first);
787
788 // check if record is found in map we got by parser
789 if (it != vpdMap.end())
790 {
791 const auto& kwdListForRecord = systemRecKwdPair.second;
792 for (const auto& keyword : kwdListForRecord)
793 {
794 DbusPropertyMap& kwdValMap = it->second;
795 auto iterator = kwdValMap.find(keyword);
796
797 if (iterator != kwdValMap.end())
798 {
799 string& kwdValue = iterator->second;
800
801 // check bus data
802 const string& recordName = systemRecKwdPair.first;
803 const string& busValue = readBusProperty(
804 objectPath, ipzVpdInf + recordName, keyword);
805
806 if (busValue.find_first_not_of(' ') != string::npos)
807 {
808 if (kwdValue.find_first_not_of(' ') != string::npos)
809 {
810 // both the data are present, check for mismatch
811 if (busValue != kwdValue)
812 {
813 string errMsg = "VPD data mismatch on cache "
814 "and hardware for record: ";
815 errMsg += (*it).first;
816 errMsg += " and keyword: ";
817 errMsg += keyword;
818
819 // data mismatch
820 PelAdditionalData additionalData;
821 additionalData.emplace("CALLOUT_INVENTORY_PATH",
822 objectPath);
823
824 additionalData.emplace("DESCRIPTION", errMsg);
825
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500826 createPEL(additionalData, PelSeverity::WARNING,
827 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500828 }
829 }
830 else
831 {
832 // implies hardware data is blank
833 // update the map
834 Binary busData(busValue.begin(), busValue.end());
835
836 updatedEeproms.push_back(std::make_tuple(
837 objectPath, recordName, keyword, busData));
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500838
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500839 // update the map as well, so that cache data is not
840 // updated as blank while populating VPD map on Dbus
841 // in populateDBus Api
842 kwdValue = busValue;
843 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500844 }
845 else if (kwdValue.find_first_not_of(' ') == string::npos)
846 {
847 string errMsg = "VPD is blank on both cache and "
848 "hardware for record: ";
849 errMsg += (*it).first;
850 errMsg += " and keyword: ";
851 errMsg += keyword;
852 errMsg += ". SSR need to update hardware VPD.";
853
854 // both the data are blanks, log PEL
855 PelAdditionalData additionalData;
856 additionalData.emplace("CALLOUT_INVENTORY_PATH",
857 objectPath);
858
859 additionalData.emplace("DESCRIPTION", errMsg);
860
861 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500862 createPEL(additionalData, PelSeverity::ERROR,
863 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500864 continue;
865 }
866 }
867 }
868 }
869 }
870
871 return updatedEeproms;
872}
873
874/**
alpana077ce68722021-07-25 13:23:59 -0500875 * @brief This checks for is this FRU a processor
876 * And if yes, then checks for is this primary
877 *
878 * @param[in] js- vpd json to get the information about this FRU
879 * @param[in] filePath- FRU vpd
880 *
881 * @return true/false
882 */
883bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
884{
885 bool isProcessor = false;
886 bool isPrimary = false;
887
888 for (const auto& item : js["frus"][filePath])
889 {
890 if (item.find("extraInterfaces") != item.end())
891 {
892 for (const auto& eI : item["extraInterfaces"].items())
893 {
894 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
895 {
896 isProcessor = true;
897 }
898 }
899 }
900
901 if (isProcessor)
902 {
903 string cpuType = item.value("cpuType", "");
904 if (cpuType == "primary")
905 {
906 isPrimary = true;
907 }
908 }
909 }
910
911 return (isProcessor && isPrimary);
912}
913
914/**
915 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
916 * driver
917 * @param[in] js- vpd json to iterate through and take action if it is DIMM
918 */
919void doEnableAllDimms(nlohmann::json& js)
920{
921 // iterate over each fru
922 for (const auto& eachFru : js["frus"].items())
923 {
924 // skip the driver binding if eeprom already exists
925 if (fs::exists(eachFru.key()))
926 {
927 continue;
928 }
929
930 for (const auto& eachInventory : eachFru.value())
931 {
932 if (eachInventory.find("extraInterfaces") != eachInventory.end())
933 {
934 for (const auto& eI : eachInventory["extraInterfaces"].items())
935 {
936 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
937 {
938 string dimmVpd = eachFru.key();
939 // fetch it from
940 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
941
942 regex matchPatern("([0-9]+-[0-9]{4})");
943 smatch matchFound;
944 if (regex_search(dimmVpd, matchFound, matchPatern))
945 {
946 vector<string> i2cReg;
947 boost::split(i2cReg, matchFound.str(0),
948 boost::is_any_of("-"));
949
950 // remove 0s from begining
951 const regex pattern("^0+(?!$)");
952 for (auto& i : i2cReg)
953 {
954 i = regex_replace(i, pattern, "");
955 }
956
957 if (i2cReg.size() == 2)
958 {
959 // echo 24c32 0x50 >
960 // /sys/bus/i2c/devices/i2c-16/new_device
961 string cmnd = "echo 24c32 0x" + i2cReg[1] +
962 " > /sys/bus/i2c/devices/i2c-" +
963 i2cReg[0] + "/new_device";
964
965 executeCmd(cmnd);
966 }
967 }
968 }
969 }
970 }
971 }
972 }
973}
974
975/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530976 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530977 * This method invokes all the populateInterface functions
978 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530979 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
980 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530981 * @param[in] js - Inventory json object
982 * @param[in] filePath - Path of the vpd file
983 * @param[in] preIntrStr - Interface string
984 */
985template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500986static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530987{
988 inventory::InterfaceMap interfaces;
989 inventory::ObjectMap objects;
990 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -0600991 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530992
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500993 // map to hold all the keywords whose value has been changed at standby
994 vector<RestoredEeproms> updatedEeproms = {};
995
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530996 bool isSystemVpd = (filePath == systemVpdFilePath);
997 if constexpr (is_same<T, Parsed>::value)
998 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -0600999 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1000 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1001 ::toupper);
1002
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301003 if (isSystemVpd)
1004 {
1005 std::vector<std::string> interfaces = {motherBoardInterface};
1006 // call mapper to check for object path creation
1007 MapperResponse subTree =
1008 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1009 string mboardPath =
1010 js["frus"][filePath].at(0).value("inventoryPath", "");
1011
1012 // Attempt system VPD restore if we have a motherboard
1013 // object in the inventory.
1014 if ((subTree.size() != 0) &&
1015 (subTree.find(pimPath + mboardPath) != subTree.end()))
1016 {
1017 updatedEeproms = restoreSystemVPD(vpdMap, mboardPath);
1018 }
1019 else
1020 {
1021 log<level::ERR>("No object path found");
1022 }
1023 }
alpana077ce68722021-07-25 13:23:59 -05001024 else
1025 {
1026 // check if it is processor vpd.
1027 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1028
1029 if (isPrimaryCpu)
1030 {
1031 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1032
1033 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1034
1035 if (chipVersion >= 2)
1036 {
1037 doEnableAllDimms(js);
1038 }
1039 }
1040 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301041 }
1042
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001043 if (isSystemVpd)
1044 {
1045 string systemJsonName{};
1046 if constexpr (is_same<T, Parsed>::value)
1047 {
1048 // pick the right system json
1049 systemJsonName = getSystemsJson(vpdMap);
1050 }
1051
1052 fs::path target = systemJsonName;
1053 fs::path link = INVENTORY_JSON_SYM_LINK;
1054
1055 // Create the directory for hosting the symlink
1056 fs::create_directories(VPD_FILES_PATH);
1057 // unlink the symlink previously created (if any)
1058 remove(INVENTORY_JSON_SYM_LINK);
1059 // create a new symlink based on the system
1060 fs::create_symlink(target, link);
1061
1062 // Reloading the json
1063 ifstream inventoryJson(link);
1064 js = json::parse(inventoryJson);
1065 inventoryJson.close();
1066 }
1067
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301068 for (const auto& item : js["frus"][filePath])
1069 {
1070 const auto& objectPath = item["inventoryPath"];
1071 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001072
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001073 vector<string> ccinList;
1074 if (item.find("ccin") != item.end())
1075 {
1076 for (const auto& cc : item["ccin"])
1077 {
1078 string ccin = cc;
1079 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1080 ccinList.push_back(ccin);
1081 }
1082 }
1083
1084 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1085 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1086 ccinList.end()))
1087 {
1088 continue;
1089 }
1090
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301091 if (isSystemVpd)
1092 {
1093 // Populate one time properties for the system VPD and its sub-frus.
1094 // For the remaining FRUs, this will get handled as a part of
1095 // priming the inventory.
1096 setOneTimeProperties(objectPath, interfaces);
1097 }
1098
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301099 // Populate the VPD keywords and the common interfaces only if we
1100 // are asked to inherit that data from the VPD, else only add the
1101 // extraInterfaces.
1102 if (item.value("inherit", true))
1103 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001104 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301105 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301106 // Each record in the VPD becomes an interface and all
1107 // keyword within the record are properties under that
1108 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301109 for (const auto& record : vpdMap)
1110 {
1111 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001112 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301113 }
1114 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001115 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301116 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001117 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301118 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301119 if (js.find("commonInterfaces") != js.end())
1120 {
1121 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1122 isSystemVpd);
1123 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301124 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001125 else
1126 {
1127 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001128 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001129 {
1130 if (item.find("copyRecords") != item.end())
1131 {
1132 for (const auto& record : item["copyRecords"])
1133 {
1134 const string& recordName = record;
1135 if (vpdMap.find(recordName) != vpdMap.end())
1136 {
1137 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001138 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001139 interfaces);
1140 }
1141 }
1142 }
1143 }
1144 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001145 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301146 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001147 // Populate interfaces and properties that are common to every FRU
1148 // and additional interface that might be defined on a per-FRU
1149 // basis.
1150 if (item.find("extraInterfaces") != item.end())
1151 {
1152 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1153 isSystemVpd);
1154 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301155 }
1156 objects.emplace(move(object), move(interfaces));
1157 }
1158
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301159 if (isSystemVpd)
1160 {
1161 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1162 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001163
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001164 // if system VPD has been restored at standby, update the EEPROM
1165 for (const auto& item : updatedEeproms)
1166 {
1167 updateHardware(get<0>(item), get<1>(item), get<2>(item),
1168 get<3>(item));
1169 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001170
1171 // set the U-boot environment variable for device-tree
1172 if constexpr (is_same<T, Parsed>::value)
1173 {
1174 const string imKeyword = getIM(vpdMap);
1175 const string hwKeyword = getHW(vpdMap);
1176 string systemType = imKeyword;
1177
1178 // check If system has constraint then append HW version to it.
1179 ifstream sysJson(SYSTEM_JSON);
1180 if (!sysJson)
1181 {
1182 throw((VpdJsonException("Failed to access Json path",
1183 SYSTEM_JSON)));
1184 }
1185
1186 try
1187 {
1188 auto systemJson = json::parse(sysJson);
Santosh Puranikd278df12021-05-10 15:51:42 +05301189 if (systemJson["system"].find(imKeyword) !=
1190 systemJson["system"].end())
1191 {
1192 if (systemJson["system"][imKeyword].find("constraint") !=
1193 systemJson["system"][imKeyword].end())
1194 {
1195 systemType += "_" + hwKeyword;
1196 }
1197 }
Alpana Kumarif05effd2021-04-07 07:32:53 -05001198 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001199 catch (const json::parse_error& ex)
Alpana Kumarif05effd2021-04-07 07:32:53 -05001200 {
1201 throw((VpdJsonException("System Json parsing failed",
1202 SYSTEM_JSON)));
1203 }
1204
Alpana Kumarif05effd2021-04-07 07:32:53 -05001205 setDevTreeEnv(systemType);
1206 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301207 }
1208
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301209 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001210 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301211}
1212
1213int main(int argc, char** argv)
1214{
1215 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001216 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001217 Binary vpdVector{};
1218 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001219 // map to hold additional data in case of logging pel
1220 PelAdditionalData additionalData{};
1221
1222 // this is needed to hold base fru inventory path in case there is ECC or
1223 // vpd exception while parsing the file
1224 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301225
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001226 // severity for PEL
1227 PelSeverity pelSeverity = PelSeverity::WARNING;
1228
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301229 try
1230 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301231 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1232 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301233
1234 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001235 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301236
1237 CLI11_PARSE(app, argc, argv);
1238
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001239 // PEL severity should be ERROR in case of any system VPD failure
1240 if (file == systemVpdFilePath)
1241 {
1242 pelSeverity = PelSeverity::ERROR;
1243 }
1244
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301245 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1246
1247 // If the symlink exists, it means it has been setup for us, switch the
1248 // path
1249 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1250 {
1251 jsonToParse = INVENTORY_JSON_SYM_LINK;
1252 }
1253
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301254 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301255 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001256 if (!inventoryJson)
1257 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001258 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001259 }
1260
1261 try
1262 {
1263 js = json::parse(inventoryJson);
1264 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001265 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001266 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001267 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001268 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301269
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301270 // Do we have the mandatory "frus" section?
1271 if (js.find("frus") == js.end())
1272 {
1273 throw(VpdJsonException("FRUs section not found in JSON",
1274 jsonToParse));
1275 }
1276
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301277 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1278 if (file.find("/ahb:apb") != string::npos)
1279 {
1280 // Translate udev path to a generic /sys/bus/.. file path.
1281 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301282
1283 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301284 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301285 {
1286 return 0;
1287 }
1288 }
1289
1290 if (file.empty())
1291 {
1292 cerr << "The EEPROM path <" << file << "> is not valid.";
1293 return 0;
1294 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301295 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301296 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301297 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301298 }
1299
Alpana Kumari2f793042020-08-18 05:51:03 -05001300 if (!fs::exists(file))
1301 {
1302 cout << "Device path: " << file
1303 << " does not exist. Spurious udev event? Exiting." << endl;
1304 return 0;
1305 }
1306
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001307 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301308 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001309 // We skip reading VPD when the power is ON in two scenarios:
1310 // 1) The eeprom we are trying to read is that of the system VPD
1311 // 2) The JSON tells us that the FRU EEPROM cannot be read when
1312 // we are powered ON.
1313 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
1314 (file == systemVpdFilePath))
Santosh Puranik85893752020-11-10 21:31:43 +05301315 {
1316 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1317 getPowerState())
1318 {
1319 cout << "This VPD cannot be read when power is ON" << endl;
1320 return 0;
1321 }
1322 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001323
Alpana Kumari2f793042020-08-18 05:51:03 -05001324 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301325 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001326 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001327 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001328 variant<KeywordVpdMap, Store> parseResult;
1329 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001330
Alpana Kumari2f793042020-08-18 05:51:03 -05001331 if (auto pVal = get_if<Store>(&parseResult))
1332 {
1333 populateDbus(pVal->getVpdMap(), js, file);
1334 }
1335 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1336 {
1337 populateDbus(*pVal, js, file);
1338 }
1339
1340 // release the parser object
1341 ParserFactory::freeParser(parser);
1342 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001343 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001344 {
1345 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001346 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001347 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301348 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001349 catch (const VpdJsonException& ex)
1350 {
1351 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1352 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001353 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001354
1355 cerr << ex.what() << "\n";
1356 rc = -1;
1357 }
1358 catch (const VpdEccException& ex)
1359 {
1360 additionalData.emplace("DESCRIPTION", "ECC check failed");
1361 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1362 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001363 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001364 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001365 cerr << ex.what() << "\n";
1366 rc = -1;
1367 }
1368 catch (const VpdDataException& ex)
1369 {
1370 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1371 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1372 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001373 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001374 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001375 cerr << ex.what() << "\n";
1376 rc = -1;
1377 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001378 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301379 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001380 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301381 cerr << e.what() << "\n";
1382 rc = -1;
1383 }
1384
1385 return rc;
Alpana Kumari37e72702021-11-18 11:18:04 -06001386}