blob: 14d405fb3112b97d303437f23895e2e24ea1a5d3 [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
Sunny Srivastava3c244142022-01-11 08:47:04 -060043// Map to hold record, kwd pair which can be re-stored at standby.
44// The list of keywords for VSYS record is as per the S0 system. Should
45// be updated for another type of systems
46static const std::unordered_map<std::string, std::vector<std::string>>
47 svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
48 {"VCEN", {"FC", "SE"}},
49 {"LXR0", {"LX"}}};
50
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 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600242 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530243 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600244 std::string prop;
245 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530246 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000247 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600248 prop = expandLocationCode(itr.value().get<string>(),
249 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530250 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600251 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530252 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600253 // Send empty Parsed object to expandLocationCode api.
254 prop = expandLocationCode(itr.value().get<string>(),
255 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530256 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600257 props.emplace(busProp, prop);
258 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
259 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530260 }
261 else
262 {
263 props.emplace(busProp, itr.value().get<string>());
264 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530265 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530266 else if (itr.value().is_array())
267 {
268 try
269 {
270 props.emplace(busProp, itr.value().get<Binary>());
271 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500272 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530273 {
274 std::cerr << "Type exception: " << e.what() << "\n";
275 // Ignore any type errors
276 }
277 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600278 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530279 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600280 const string& rec = itr.value().value("recordName", "");
281 const string& kw = itr.value().value("keywordName", "");
282 const string& encoding = itr.value().value("encoding", "");
283
Alpana Kumari58e22142020-05-05 00:22:12 -0500284 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530285 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530286 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
287 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600288 {
289 auto encoded =
290 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530291 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600292 }
293 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500294 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600295 {
296 if (!kw.empty() && vpdMap.count(kw))
297 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000298 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
299 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
300
301 if (kwValue)
302 {
303 auto prop =
304 string((*kwValue).begin(), (*kwValue).end());
305
306 auto encoded = encodeKeyword(prop, encoding);
307
308 props.emplace(busProp, encoded);
309 }
310 else if (uintValue)
311 {
312 props.emplace(busProp, *uintValue);
313 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600314 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530315 }
316 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500317 else if (itr.value().is_number())
318 {
319 // For now assume the value is a size_t. In the future it would
320 // be nice to come up with a way to get the type from the JSON.
321 props.emplace(busProp, itr.value().get<size_t>());
322 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530323 }
324 interfaces.emplace(inf, move(props));
325 }
326}
327
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600328/*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
329 * Currently do reset only for DIMM VPD.*/
330static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
331 ifstream& vpdFile)
332{
333 for (const auto& item : js["frus"][file])
334 {
335 if (item.find("extraInterfaces") != item.end())
336 {
337 if (item["extraInterfaces"].find(
338 "xyz.openbmc_project.Inventory.Item.Dimm") !=
339 item["extraInterfaces"].end())
340 {
341 // moves the EEPROM pointer to 2048 'th byte.
342 vpdFile.seekg(2047, std::ios::beg);
343 // Read that byte and discard - to affirm the move
344 // operation.
345 char ch;
346 vpdFile.read(&ch, sizeof(ch));
347 }
348 return;
349 }
350 }
351}
352
Alpana Kumari2f793042020-08-18 05:51:03 -0500353static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500354{
355 uint32_t offset = 0;
356 // check if offset present?
357 for (const auto& item : js["frus"][file])
358 {
359 if (item.find("offset") != item.end())
360 {
361 offset = item["offset"];
362 }
363 }
364
365 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600366 auto maxVPDSize = std::min(std::filesystem::file_size(file),
367 static_cast<uintmax_t>(65504));
368
Alpana Kumari58e22142020-05-05 00:22:12 -0500369 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600370 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500371 ifstream vpdFile;
372 vpdFile.open(file, ios::binary);
373
374 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600375 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500376 vpdVector.resize(vpdFile.gcount());
377
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600378 resetEEPROMPointer(js, file, vpdFile);
379
Alpana Kumari58e22142020-05-05 00:22:12 -0500380 return vpdVector;
381}
382
Alpana Kumari2f793042020-08-18 05:51:03 -0500383/** This API will be called at the end of VPD collection to perform any post
384 * actions.
385 *
386 * @param[in] json - json object
387 * @param[in] file - eeprom file path
388 */
389static void postFailAction(const nlohmann::json& json, const string& file)
390{
391 if ((json["frus"][file].at(0)).find("postActionFail") ==
392 json["frus"][file].at(0).end())
393 {
394 return;
395 }
396
397 uint8_t pinValue = 0;
398 string pinName;
399
400 for (const auto& postAction :
401 (json["frus"][file].at(0))["postActionFail"].items())
402 {
403 if (postAction.key() == "pin")
404 {
405 pinName = postAction.value();
406 }
407 else if (postAction.key() == "value")
408 {
409 // Get the value to set
410 pinValue = postAction.value();
411 }
412 }
413
414 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
415
416 try
417 {
418 gpiod::line outputLine = gpiod::find_line(pinName);
419
420 if (!outputLine)
421 {
422 cout << "Couldn't find output line:" << pinName
423 << " on GPIO. Skipping...\n";
424
425 return;
426 }
427 outputLine.request(
428 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
429 pinValue);
430 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500431 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500432 {
433 cerr << "Failed to set post-action GPIO" << endl;
434 }
435}
436
437/** Performs any pre-action needed to get the FRU setup for collection.
438 *
439 * @param[in] json - json object
440 * @param[in] file - eeprom file path
441 */
442static void preAction(const nlohmann::json& json, const string& file)
443{
444 if ((json["frus"][file].at(0)).find("preAction") ==
445 json["frus"][file].at(0).end())
446 {
447 return;
448 }
449
450 uint8_t pinValue = 0;
451 string pinName;
452
453 for (const auto& postAction :
454 (json["frus"][file].at(0))["preAction"].items())
455 {
456 if (postAction.key() == "pin")
457 {
458 pinName = postAction.value();
459 }
460 else if (postAction.key() == "value")
461 {
462 // Get the value to set
463 pinValue = postAction.value();
464 }
465 }
466
467 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
468 try
469 {
470 gpiod::line outputLine = gpiod::find_line(pinName);
471
472 if (!outputLine)
473 {
474 cout << "Couldn't find output line:" << pinName
475 << " on GPIO. Skipping...\n";
476
477 return;
478 }
479 outputLine.request(
480 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
481 pinValue);
482 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500483 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500484 {
485 cerr << "Failed to set pre-action GPIO" << endl;
486 return;
487 }
488
489 // Now bind the device
Alpana Kumari4c3bf5b2021-09-16 07:24:58 -0500490 string bind = json["frus"][file].at(0).value("devAddress", "");
Alpana Kumari2f793042020-08-18 05:51:03 -0500491 cout << "Binding device " << bind << endl;
492 string bindCmd = string("echo \"") + bind +
493 string("\" > /sys/bus/i2c/drivers/at24/bind");
494 cout << bindCmd << endl;
495 executeCmd(bindCmd);
496
497 // Check if device showed up (test for file)
498 if (!fs::exists(file))
499 {
500 cout << "EEPROM " << file << " does not exist. Take failure action"
501 << endl;
502 // If not, then take failure postAction
503 postFailAction(json, file);
504 }
505}
506
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530507/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530508 * @brief Set certain one time properties in the inventory
509 * Use this function to insert the Functional and Enabled properties into the
510 * inventory map. This function first checks if the object in question already
511 * has these properties hosted on D-Bus, if the property is already there, it is
512 * not modified, hence the name "one time". If the property is not already
513 * present, it will be added to the map with a suitable default value (true for
514 * Functional and false for Enabled)
515 *
516 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
517 * @param[inout] interfaces - Reference to a map of inventory interfaces to
518 * which the properties will be attached.
519 */
520static void setOneTimeProperties(const std::string& object,
521 inventory::InterfaceMap& interfaces)
522{
523 auto bus = sdbusplus::bus::new_default();
524 auto objectPath = INVENTORY_PATH + object;
525 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
526 objectPath.c_str(),
527 "org.freedesktop.DBus.Properties", "Get");
528 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
529 prop.append("Functional");
530 try
531 {
532 auto result = bus.call(prop);
533 }
534 catch (const sdbusplus::exception::SdBusError& e)
535 {
536 // Treat as property unavailable
537 inventory::PropertyMap prop;
538 prop.emplace("Functional", true);
539 interfaces.emplace(
540 "xyz.openbmc_project.State.Decorator.OperationalStatus",
541 move(prop));
542 }
543 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
544 objectPath.c_str(),
545 "org.freedesktop.DBus.Properties", "Get");
546 prop.append("xyz.openbmc_project.Object.Enable");
547 prop.append("Enabled");
548 try
549 {
550 auto result = bus.call(prop);
551 }
552 catch (const sdbusplus::exception::SdBusError& e)
553 {
554 // Treat as property unavailable
555 inventory::PropertyMap prop;
556 prop.emplace("Enabled", false);
557 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
558 }
559}
560
561/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530562 * @brief Prime the Inventory
563 * Prime the inventory by populating only the location code,
564 * type interface and the inventory object for the frus
565 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530566 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530567 * @param[in] jsObject - Reference to vpd inventory json object
568 * @param[in] vpdMap - Reference to the parsed vpd map
569 *
570 * @returns Map of items in extraInterface.
571 */
572template <typename T>
573inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
574 const T& vpdMap)
575{
576 inventory::ObjectMap objects;
577
578 for (auto& itemFRUS : jsObject["frus"].items())
579 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500580 // Take pre actions
581 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530582 for (auto& itemEEPROM : itemFRUS.value())
583 {
584 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530585 inventory::Object object(itemEEPROM.at("inventoryPath"));
586
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530587 if ((itemFRUS.key() != systemVpdFilePath) &&
588 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530589 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600590 inventory::PropertyMap presProp;
591 presProp.emplace("Present", false);
592 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530593 presProp);
594 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530595 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
596 {
597 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
598 {
599 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000600 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530601 {
602 if constexpr (std::is_same<T, Parsed>::value)
603 {
604 for (auto& lC : eI.value().items())
605 {
606 auto propVal = expandLocationCode(
607 lC.value().get<string>(), vpdMap, true);
608
609 props.emplace(move(lC.key()),
610 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530611 interfaces.emplace(XYZ_LOCATION_CODE_INF,
612 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530613 interfaces.emplace(move(eI.key()),
614 move(props));
615 }
616 }
617 }
618 else if (eI.key().find("Inventory.Item.") !=
619 string::npos)
620 {
621 interfaces.emplace(move(eI.key()), move(props));
622 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530623 else if (eI.key() ==
624 "xyz.openbmc_project.Inventory.Item")
625 {
626 for (auto& val : eI.value().items())
627 {
628 if (val.key() == "PrettyName")
629 {
630 presProp.emplace(val.key(),
631 val.value().get<string>());
632 }
633 }
634 // Use insert_or_assign here as we may already have
635 // inserted the present property only earlier in
636 // this function under this same interface.
637 interfaces.insert_or_assign(eI.key(),
638 move(presProp));
639 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530640 }
641 }
642 objects.emplace(move(object), move(interfaces));
643 }
644 }
645 }
646 return objects;
647}
648
Alpana Kumari65b83602020-09-01 00:24:56 -0500649/**
650 * @brief This API executes command to set environment variable
651 * And then reboot the system
652 * @param[in] key -env key to set new value
653 * @param[in] value -value to set.
654 */
655void setEnvAndReboot(const string& key, const string& value)
656{
657 // set env and reboot and break.
658 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600659 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500660 // make dbus call to reboot
661 auto bus = sdbusplus::bus::new_default_system();
662 auto method = bus.new_method_call(
663 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
664 "org.freedesktop.systemd1.Manager", "Reboot");
665 bus.call_noreply(method);
666}
667
668/*
669 * @brief This API checks for env var fitconfig.
670 * If not initialised OR updated as per the current system type,
671 * update this env var and reboot the system.
672 *
673 * @param[in] systemType IM kwd in vpd tells about which system type it is.
674 * */
675void setDevTreeEnv(const string& systemType)
676{
Alpana Kumari37e72702021-11-18 11:18:04 -0600677 // Init with default dtb
678 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530679 static const deviceTreeMap deviceTreeSystemTypeMap = {
680 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
681 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
682 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
683 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
684 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
685 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500686
687 if (deviceTreeSystemTypeMap.find(systemType) !=
688 deviceTreeSystemTypeMap.end())
689 {
690 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
691 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600692 else
693 {
694 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600695 string err = "This System type not found/supported in dtb table " +
696 systemType +
697 ".Please check the HW and IM keywords in the system "
698 "VPD.Breaking...";
699
700 // map to hold additional data in case of logging pel
701 PelAdditionalData additionalData{};
702 additionalData.emplace("DESCRIPTION", err);
703 createPEL(additionalData, PelSeverity::WARNING,
704 errIntfForInvalidSystemType);
705 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600706 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500707
708 string readVarValue;
709 bool envVarFound = false;
710
711 vector<string> output = executeCmd("/sbin/fw_printenv");
712 for (const auto& entry : output)
713 {
714 size_t pos = entry.find("=");
715 string key = entry.substr(0, pos);
716 if (key != "fitconfig")
717 {
718 continue;
719 }
720
721 envVarFound = true;
722 if (pos + 1 < entry.size())
723 {
724 readVarValue = entry.substr(pos + 1);
725 if (readVarValue.find(newDeviceTree) != string::npos)
726 {
727 // fitconfig is Updated. No action needed
728 break;
729 }
730 }
731 // set env and reboot and break.
732 setEnvAndReboot(key, newDeviceTree);
733 exit(0);
734 }
735
736 // check If env var Not found
737 if (!envVarFound)
738 {
739 setEnvAndReboot("fitconfig", newDeviceTree);
740 }
741}
742
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530743/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500744 * @brief API to call VPD manager to write VPD to EEPROM.
745 * @param[in] Object path.
746 * @param[in] record to be updated.
747 * @param[in] keyword to be updated.
748 * @param[in] keyword data to be updated
749 */
750void updateHardware(const string& objectName, const string& recName,
751 const string& kwdName, const Binary& data)
752{
753 try
754 {
755 auto bus = sdbusplus::bus::new_default();
756 auto properties =
757 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
758 properties.append(
759 static_cast<sdbusplus::message::object_path>(objectName));
760 properties.append(recName);
761 properties.append(kwdName);
762 properties.append(data);
763 bus.call(properties);
764 }
Patrick Williams8be43342021-09-02 09:33:36 -0500765 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500766 {
767 std::string what =
768 "VPDManager WriteKeyword api failed for inventory path " +
769 objectName;
770 what += " record " + recName;
771 what += " keyword " + kwdName;
772 what += " with bus error = " + std::string(e.what());
773
774 // map to hold additional data in case of logging pel
775 PelAdditionalData additionalData{};
776 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
777 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500778 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500779 }
780}
781
782/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600783 * @brief An api to get list of blank system VPD properties.
784 * @param[in] vpdMap - IPZ vpd map.
785 * @param[in] objectPath - Object path for the FRU.
786 * @param[out] blankPropertyList - Properties which are blank in System VPD and
787 * needs to be updated as standby.
788 */
789void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
790 std::vector<RestoredEeproms>& blankPropertyList)
791{
792 for (const auto& systemRecKwdPair : svpdKwdMap)
793 {
794 auto it = vpdMap.find(systemRecKwdPair.first);
795
796 // check if record is found in map we got by parser
797 if (it != vpdMap.end())
798 {
799 const auto& kwdListForRecord = systemRecKwdPair.second;
800 for (const auto& keyword : kwdListForRecord)
801 {
802 DbusPropertyMap& kwdValMap = it->second;
803 auto iterator = kwdValMap.find(keyword);
804
805 if (iterator != kwdValMap.end())
806 {
807 string& kwdValue = iterator->second;
808
809 // check bus data
810 const string& recordName = systemRecKwdPair.first;
811 const string& busValue = readBusProperty(
812 objectPath, ipzVpdInf + recordName, keyword);
813
814 if (busValue.find_first_not_of(' ') != string::npos)
815 {
816 if (kwdValue.find_first_not_of(' ') == string::npos)
817 {
818 // implies data is blank on EEPROM but not on cache.
819 // So EEPROM vpd update is required.
820 Binary busData(busValue.begin(), busValue.end());
821
822 blankPropertyList.push_back(std::make_tuple(
823 objectPath, recordName, keyword, busData));
824 }
825 }
826 }
827 }
828 }
829 }
830}
831
832/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500833 * @brief API to check if we need to restore system VPD
834 * This functionality is only applicable for IPZ VPD data.
835 * @param[in] vpdMap - IPZ vpd map
836 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500837 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600838void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500839{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500840 for (const auto& systemRecKwdPair : svpdKwdMap)
841 {
842 auto it = vpdMap.find(systemRecKwdPair.first);
843
844 // check if record is found in map we got by parser
845 if (it != vpdMap.end())
846 {
847 const auto& kwdListForRecord = systemRecKwdPair.second;
848 for (const auto& keyword : kwdListForRecord)
849 {
850 DbusPropertyMap& kwdValMap = it->second;
851 auto iterator = kwdValMap.find(keyword);
852
853 if (iterator != kwdValMap.end())
854 {
855 string& kwdValue = iterator->second;
856
857 // check bus data
858 const string& recordName = systemRecKwdPair.first;
859 const string& busValue = readBusProperty(
860 objectPath, ipzVpdInf + recordName, keyword);
861
862 if (busValue.find_first_not_of(' ') != string::npos)
863 {
864 if (kwdValue.find_first_not_of(' ') != string::npos)
865 {
866 // both the data are present, check for mismatch
867 if (busValue != kwdValue)
868 {
869 string errMsg = "VPD data mismatch on cache "
870 "and hardware for record: ";
871 errMsg += (*it).first;
872 errMsg += " and keyword: ";
873 errMsg += keyword;
874
875 // data mismatch
876 PelAdditionalData additionalData;
877 additionalData.emplace("CALLOUT_INVENTORY_PATH",
878 objectPath);
879
880 additionalData.emplace("DESCRIPTION", errMsg);
881
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500882 createPEL(additionalData, PelSeverity::WARNING,
883 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500884 }
885 }
886 else
887 {
888 // implies hardware data is blank
889 // update the map
890 Binary busData(busValue.begin(), busValue.end());
891
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500892 // update the map as well, so that cache data is not
893 // updated as blank while populating VPD map on Dbus
894 // in populateDBus Api
895 kwdValue = busValue;
896 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500897 }
898 else if (kwdValue.find_first_not_of(' ') == string::npos)
899 {
900 string errMsg = "VPD is blank on both cache and "
901 "hardware for record: ";
902 errMsg += (*it).first;
903 errMsg += " and keyword: ";
904 errMsg += keyword;
905 errMsg += ". SSR need to update hardware VPD.";
906
907 // both the data are blanks, log PEL
908 PelAdditionalData additionalData;
909 additionalData.emplace("CALLOUT_INVENTORY_PATH",
910 objectPath);
911
912 additionalData.emplace("DESCRIPTION", errMsg);
913
914 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500915 createPEL(additionalData, PelSeverity::ERROR,
916 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500917 continue;
918 }
919 }
920 }
921 }
922 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500923}
924
925/**
alpana077ce68722021-07-25 13:23:59 -0500926 * @brief This checks for is this FRU a processor
927 * And if yes, then checks for is this primary
928 *
929 * @param[in] js- vpd json to get the information about this FRU
930 * @param[in] filePath- FRU vpd
931 *
932 * @return true/false
933 */
934bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
935{
936 bool isProcessor = false;
937 bool isPrimary = false;
938
939 for (const auto& item : js["frus"][filePath])
940 {
941 if (item.find("extraInterfaces") != item.end())
942 {
943 for (const auto& eI : item["extraInterfaces"].items())
944 {
945 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
946 {
947 isProcessor = true;
948 }
949 }
950 }
951
952 if (isProcessor)
953 {
954 string cpuType = item.value("cpuType", "");
955 if (cpuType == "primary")
956 {
957 isPrimary = true;
958 }
959 }
960 }
961
962 return (isProcessor && isPrimary);
963}
964
965/**
966 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
967 * driver
968 * @param[in] js- vpd json to iterate through and take action if it is DIMM
969 */
970void doEnableAllDimms(nlohmann::json& js)
971{
972 // iterate over each fru
973 for (const auto& eachFru : js["frus"].items())
974 {
975 // skip the driver binding if eeprom already exists
976 if (fs::exists(eachFru.key()))
977 {
978 continue;
979 }
980
981 for (const auto& eachInventory : eachFru.value())
982 {
983 if (eachInventory.find("extraInterfaces") != eachInventory.end())
984 {
985 for (const auto& eI : eachInventory["extraInterfaces"].items())
986 {
987 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
988 {
989 string dimmVpd = eachFru.key();
990 // fetch it from
991 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
992
993 regex matchPatern("([0-9]+-[0-9]{4})");
994 smatch matchFound;
995 if (regex_search(dimmVpd, matchFound, matchPatern))
996 {
997 vector<string> i2cReg;
998 boost::split(i2cReg, matchFound.str(0),
999 boost::is_any_of("-"));
1000
1001 // remove 0s from begining
1002 const regex pattern("^0+(?!$)");
1003 for (auto& i : i2cReg)
1004 {
1005 i = regex_replace(i, pattern, "");
1006 }
1007
1008 if (i2cReg.size() == 2)
1009 {
1010 // echo 24c32 0x50 >
1011 // /sys/bus/i2c/devices/i2c-16/new_device
1012 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1013 " > /sys/bus/i2c/devices/i2c-" +
1014 i2cReg[0] + "/new_device";
1015
1016 executeCmd(cmnd);
1017 }
1018 }
1019 }
1020 }
1021 }
1022 }
1023 }
1024}
1025
1026/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301027 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301028 * This method invokes all the populateInterface functions
1029 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301030 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1031 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301032 * @param[in] js - Inventory json object
1033 * @param[in] filePath - Path of the vpd file
1034 * @param[in] preIntrStr - Interface string
1035 */
1036template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001037static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301038{
1039 inventory::InterfaceMap interfaces;
1040 inventory::ObjectMap objects;
1041 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001042 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301043
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301044 bool isSystemVpd = (filePath == systemVpdFilePath);
1045 if constexpr (is_same<T, Parsed>::value)
1046 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001047 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1048 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1049 ::toupper);
1050
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301051 if (isSystemVpd)
1052 {
1053 std::vector<std::string> interfaces = {motherBoardInterface};
1054 // call mapper to check for object path creation
1055 MapperResponse subTree =
1056 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1057 string mboardPath =
1058 js["frus"][filePath].at(0).value("inventoryPath", "");
1059
1060 // Attempt system VPD restore if we have a motherboard
1061 // object in the inventory.
1062 if ((subTree.size() != 0) &&
1063 (subTree.find(pimPath + mboardPath) != subTree.end()))
1064 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001065 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301066 }
1067 else
1068 {
1069 log<level::ERR>("No object path found");
1070 }
1071 }
alpana077ce68722021-07-25 13:23:59 -05001072 else
1073 {
1074 // check if it is processor vpd.
1075 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1076
1077 if (isPrimaryCpu)
1078 {
1079 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1080
1081 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1082
1083 if (chipVersion >= 2)
1084 {
1085 doEnableAllDimms(js);
1086 }
1087 }
1088 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301089 }
1090
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001091 if (isSystemVpd)
1092 {
1093 string systemJsonName{};
1094 if constexpr (is_same<T, Parsed>::value)
1095 {
1096 // pick the right system json
1097 systemJsonName = getSystemsJson(vpdMap);
1098 }
1099
1100 fs::path target = systemJsonName;
1101 fs::path link = INVENTORY_JSON_SYM_LINK;
1102
1103 // Create the directory for hosting the symlink
1104 fs::create_directories(VPD_FILES_PATH);
1105 // unlink the symlink previously created (if any)
1106 remove(INVENTORY_JSON_SYM_LINK);
1107 // create a new symlink based on the system
1108 fs::create_symlink(target, link);
1109
1110 // Reloading the json
1111 ifstream inventoryJson(link);
1112 js = json::parse(inventoryJson);
1113 inventoryJson.close();
1114 }
1115
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301116 for (const auto& item : js["frus"][filePath])
1117 {
1118 const auto& objectPath = item["inventoryPath"];
1119 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001120
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001121 vector<string> ccinList;
1122 if (item.find("ccin") != item.end())
1123 {
1124 for (const auto& cc : item["ccin"])
1125 {
1126 string ccin = cc;
1127 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1128 ccinList.push_back(ccin);
1129 }
1130 }
1131
1132 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1133 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1134 ccinList.end()))
1135 {
1136 continue;
1137 }
1138
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001139 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301140 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001141
1142 // Populate one time properties for the system VPD and its sub-frus
1143 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301144 // For the remaining FRUs, this will get handled as a part of
1145 // priming the inventory.
1146 setOneTimeProperties(objectPath, interfaces);
1147 }
1148
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301149 // Populate the VPD keywords and the common interfaces only if we
1150 // are asked to inherit that data from the VPD, else only add the
1151 // extraInterfaces.
1152 if (item.value("inherit", true))
1153 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001154 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301155 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301156 // Each record in the VPD becomes an interface and all
1157 // keyword within the record are properties under that
1158 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301159 for (const auto& record : vpdMap)
1160 {
1161 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001162 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301163 }
1164 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001165 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301166 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001167 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301168 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301169 if (js.find("commonInterfaces") != js.end())
1170 {
1171 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1172 isSystemVpd);
1173 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301174 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001175 else
1176 {
1177 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001178 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001179 {
1180 if (item.find("copyRecords") != item.end())
1181 {
1182 for (const auto& record : item["copyRecords"])
1183 {
1184 const string& recordName = record;
1185 if (vpdMap.find(recordName) != vpdMap.end())
1186 {
1187 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001188 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001189 interfaces);
1190 }
1191 }
1192 }
1193 }
1194 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301195 // Populate interfaces and properties that are common to every FRU
1196 // and additional interface that might be defined on a per-FRU
1197 // basis.
1198 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301199 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301200 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1201 isSystemVpd);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301202 }
1203 objects.emplace(move(object), move(interfaces));
1204 }
1205
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301206 if (isSystemVpd)
1207 {
1208 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1209 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001210
Alpana Kumarif05effd2021-04-07 07:32:53 -05001211 // set the U-boot environment variable for device-tree
1212 if constexpr (is_same<T, Parsed>::value)
1213 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301214 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001215 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301216 }
1217
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301218 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001219 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301220}
1221
1222int main(int argc, char** argv)
1223{
1224 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001225 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001226 Binary vpdVector{};
1227 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001228 // map to hold additional data in case of logging pel
1229 PelAdditionalData additionalData{};
1230
1231 // this is needed to hold base fru inventory path in case there is ECC or
1232 // vpd exception while parsing the file
1233 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301234
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001235 // severity for PEL
1236 PelSeverity pelSeverity = PelSeverity::WARNING;
1237
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301238 try
1239 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301240 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1241 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301242
1243 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001244 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301245
1246 CLI11_PARSE(app, argc, argv);
1247
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001248 // PEL severity should be ERROR in case of any system VPD failure
1249 if (file == systemVpdFilePath)
1250 {
1251 pelSeverity = PelSeverity::ERROR;
1252 }
1253
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301254 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1255
1256 // If the symlink exists, it means it has been setup for us, switch the
1257 // path
1258 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1259 {
1260 jsonToParse = INVENTORY_JSON_SYM_LINK;
1261 }
1262
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301263 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301264 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001265 if (!inventoryJson)
1266 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001267 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001268 }
1269
1270 try
1271 {
1272 js = json::parse(inventoryJson);
1273 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001274 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001275 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001276 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001277 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301278
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301279 // Do we have the mandatory "frus" section?
1280 if (js.find("frus") == js.end())
1281 {
1282 throw(VpdJsonException("FRUs section not found in JSON",
1283 jsonToParse));
1284 }
1285
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301286 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1287 if (file.find("/ahb:apb") != string::npos)
1288 {
1289 // Translate udev path to a generic /sys/bus/.. file path.
1290 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301291
1292 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301293 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301294 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001295 // We need manager service active to process restoring of
1296 // system VPD on hardware. So in case any system restore is
1297 // required, update hardware in the second trigger of parser
1298 // code for system vpd file path.
1299
1300 std::vector<std::string> interfaces{motherBoardInterface};
1301
1302 // call mapper to check for object path creation
1303 MapperResponse subTree =
1304 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1305 string mboardPath =
1306 js["frus"][file].at(0).value("inventoryPath", "");
1307
1308 // Attempt system VPD restore if we have a motherboard
1309 // object in the inventory.
1310 if ((subTree.size() != 0) &&
1311 (subTree.find(pimPath + mboardPath) != subTree.end()))
1312 {
1313 vpdVector = getVpdDataInVector(js, file);
1314 ParserInterface* parser =
1315 ParserFactory::getParser(vpdVector);
1316 variant<KeywordVpdMap, Store> parseResult;
1317 parseResult = parser->parse();
1318
1319 if (auto pVal = get_if<Store>(&parseResult))
1320 {
1321 // map to hold all the keywords whose value is blank and
1322 // needs to be updated at standby.
1323 vector<RestoredEeproms> blankSystemVpdProperties{};
1324 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1325 blankSystemVpdProperties);
1326
1327 // if system VPD restore is required, update the
1328 // EEPROM
1329 for (const auto& item : blankSystemVpdProperties)
1330 {
1331 updateHardware(get<0>(item), get<1>(item),
1332 get<2>(item), get<3>(item));
1333 }
1334 }
1335 else
1336 {
1337 std::cout << "Not a valid format to restore system VPD"
1338 << std::endl;
1339 }
1340 // release the parser object
1341 ParserFactory::freeParser(parser);
1342 }
1343 else
1344 {
1345 log<level::ERR>("No object path found");
1346 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301347 return 0;
1348 }
1349 }
1350
1351 if (file.empty())
1352 {
1353 cerr << "The EEPROM path <" << file << "> is not valid.";
1354 return 0;
1355 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301356 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301357 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301358 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301359 }
1360
Alpana Kumari2f793042020-08-18 05:51:03 -05001361 if (!fs::exists(file))
1362 {
1363 cout << "Device path: " << file
1364 << " does not exist. Spurious udev event? Exiting." << endl;
1365 return 0;
1366 }
1367
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001368 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301369 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001370 // We skip reading VPD when the power is ON in two scenarios:
1371 // 1) The eeprom we are trying to read is that of the system VPD
1372 // 2) The JSON tells us that the FRU EEPROM cannot be read when
1373 // we are powered ON.
1374 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
1375 (file == systemVpdFilePath))
Santosh Puranik85893752020-11-10 21:31:43 +05301376 {
1377 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1378 getPowerState())
1379 {
1380 cout << "This VPD cannot be read when power is ON" << endl;
1381 return 0;
1382 }
1383 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001384
Alpana Kumari2f793042020-08-18 05:51:03 -05001385 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301386 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001387 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001388 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001389 variant<KeywordVpdMap, Store> parseResult;
1390 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001391
Alpana Kumari2f793042020-08-18 05:51:03 -05001392 if (auto pVal = get_if<Store>(&parseResult))
1393 {
1394 populateDbus(pVal->getVpdMap(), js, file);
1395 }
1396 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1397 {
1398 populateDbus(*pVal, js, file);
1399 }
1400
1401 // release the parser object
1402 ParserFactory::freeParser(parser);
1403 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001404 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001405 {
1406 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001407 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001408 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301409 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001410 catch (const VpdJsonException& ex)
1411 {
1412 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1413 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001414 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001415
1416 cerr << ex.what() << "\n";
1417 rc = -1;
1418 }
1419 catch (const VpdEccException& ex)
1420 {
1421 additionalData.emplace("DESCRIPTION", "ECC check failed");
1422 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1423 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001424 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001425 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001426 cerr << ex.what() << "\n";
1427 rc = -1;
1428 }
1429 catch (const VpdDataException& ex)
1430 {
1431 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1432 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1433 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001434 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001435 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001436 cerr << ex.what() << "\n";
1437 rc = -1;
1438 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001439 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301440 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001441 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301442 cerr << e.what() << "\n";
1443 rc = -1;
1444 }
1445
1446 return rc;
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -06001447}