blob: a678adb031c9edee1356a71364c933a7d748e18c [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>
Alpana Kumari65b83602020-09-01 00:24:56 -050017#include <cstdarg>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053018#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053019#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053020#include <fstream>
Alpana Kumari2f793042020-08-18 05:51:03 -050021#include <gpiod.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053022#include <iostream>
23#include <iterator>
24#include <nlohmann/json.hpp>
Andrew Geissler280197e2020-12-08 20:51:49 -060025#include <phosphor-logging/log.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053026
27using namespace std;
28using namespace openpower::vpd;
29using namespace CLI;
30using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053031using namespace openpower::vpd::constants;
32namespace fs = filesystem;
33using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050034using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050035using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050036using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050037using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050038using namespace openpower::vpd::exceptions;
Andrew Geissler280197e2020-12-08 20:51:49 -060039using namespace phosphor::logging;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053040
Alpana Kumari65b83602020-09-01 00:24:56 -050041static const deviceTreeMap deviceTreeSystemTypeMap = {
Andrew Geissler2fe709f2021-03-25 10:59:07 -050042 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier.dtb"},
43 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
44 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
45 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -050046
Santosh Puranik88edeb62020-03-02 12:00:09 +053047/**
Santosh Puranik85893752020-11-10 21:31:43 +053048 * @brief Returns the power state for chassis0
49 */
50static auto getPowerState()
51{
52 // TODO: How do we handle multiple chassis?
53 string powerState{};
54 auto bus = sdbusplus::bus::new_default();
55 auto properties =
56 bus.new_method_call("xyz.openbmc_project.State.Chassis",
57 "/xyz/openbmc_project/state/chassis0",
58 "org.freedesktop.DBus.Properties", "Get");
59 properties.append("xyz.openbmc_project.State.Chassis");
60 properties.append("CurrentPowerState");
61 auto result = bus.call(properties);
62 if (!result.is_method_error())
63 {
64 variant<string> val;
65 result.read(val);
66 if (auto pVal = get_if<string>(&val))
67 {
68 powerState = *pVal;
69 }
70 }
71 cout << "Power state is: " << powerState << endl;
72 return powerState;
73}
74
75/**
Santosh Puranik88edeb62020-03-02 12:00:09 +053076 * @brief Expands location codes
77 */
78static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
79 bool isSystemVpd)
80{
81 auto expanded{unexpanded};
82 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
83 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
84 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
85 size_t idx = expanded.find("fcs");
86 try
87 {
88 if (idx != string::npos)
89 {
90 string fc{};
91 string se{};
92 if (isSystemVpd)
93 {
94 const auto& fcData = vpdMap.at("VCEN").at("FC");
95 const auto& seData = vpdMap.at("VCEN").at("SE");
96 fc = string(fcData.data(), fcData.size());
97 se = string(seData.data(), seData.size());
98 }
99 else
100 {
101 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
102 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
103 }
104
Alpana Kumari81671f62021-02-10 02:21:59 -0600105 // TODO: See if ND0 can be placed in the JSON
106 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530107 }
108 else
109 {
110 idx = expanded.find("mts");
111 if (idx != string::npos)
112 {
113 string mt{};
114 string se{};
115 if (isSystemVpd)
116 {
117 const auto& mtData = vpdMap.at("VSYS").at("TM");
118 const auto& seData = vpdMap.at("VSYS").at("SE");
119 mt = string(mtData.data(), mtData.size());
120 se = string(seData.data(), seData.size());
121 }
122 else
123 {
124 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
125 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
126 }
127
128 replace(mt.begin(), mt.end(), '-', '.');
129 expanded.replace(idx, 3, mt + "." + se);
130 }
131 }
132 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500133 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530134 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500135 cerr << "Failed to expand location code with exception: " << e.what()
136 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530137 }
138 return expanded;
139}
Alpana Kumari2f793042020-08-18 05:51:03 -0500140
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530141/**
142 * @brief Populate FRU specific interfaces.
143 *
144 * This is a common method which handles both
145 * ipz and keyword specific interfaces thus,
146 * reducing the code redundancy.
147 * @param[in] map - Reference to the innermost keyword-value map.
148 * @param[in] preIntrStr - Reference to the interface string.
149 * @param[out] interfaces - Reference to interface map.
150 */
151template <typename T>
152static void populateFruSpecificInterfaces(const T& map,
153 const string& preIntrStr,
154 inventory::InterfaceMap& interfaces)
155{
156 inventory::PropertyMap prop;
157
158 for (const auto& kwVal : map)
159 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500160 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530161
162 auto kw = kwVal.first;
163
164 if (kw[0] == '#')
165 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500166 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530167 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500168 else if (isdigit(kw[0]))
169 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500170 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500171 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530172 prop.emplace(move(kw), move(vec));
173 }
174
175 interfaces.emplace(preIntrStr, move(prop));
176}
177
178/**
179 * @brief Populate Interfaces.
180 *
181 * This method populates common and extra interfaces to dbus.
182 * @param[in] js - json object
183 * @param[out] interfaces - Reference to interface map
184 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530185 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530186 */
187template <typename T>
188static void populateInterfaces(const nlohmann::json& js,
189 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530190 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530191{
192 for (const auto& ifs : js.items())
193 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530194 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530195 inventory::PropertyMap props;
196
197 for (const auto& itr : ifs.value().items())
198 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530199 const string& busProp = itr.key();
200
Alpana Kumari31970de2020-02-17 06:49:57 -0600201 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530202 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530203 props.emplace(busProp, itr.value().get<bool>());
204 }
205 else if (itr.value().is_string())
206 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500207 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530208 {
209 if (busProp == "LocationCode" &&
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000210 inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530211 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000212 // TODO deprecate the com.ibm interface later
Santosh Puranik88edeb62020-03-02 12:00:09 +0530213 auto prop = expandLocationCode(
214 itr.value().get<string>(), vpdMap, isSystemVpd);
215 props.emplace(busProp, prop);
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000216 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530217 }
218 else
219 {
220 props.emplace(busProp, itr.value().get<string>());
221 }
222 }
223 else
224 {
225 props.emplace(busProp, itr.value().get<string>());
226 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530227 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600228 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530229 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600230 const string& rec = itr.value().value("recordName", "");
231 const string& kw = itr.value().value("keywordName", "");
232 const string& encoding = itr.value().value("encoding", "");
233
Alpana Kumari58e22142020-05-05 00:22:12 -0500234 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530235 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530236 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
237 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600238 {
239 auto encoded =
240 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530241 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600242 }
243 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500244 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600245 {
246 if (!kw.empty() && vpdMap.count(kw))
247 {
248 auto prop =
249 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
250 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530251 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600252 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530253 }
254 }
255 }
256 interfaces.emplace(inf, move(props));
257 }
258}
259
Alpana Kumari2f793042020-08-18 05:51:03 -0500260static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500261{
262 uint32_t offset = 0;
263 // check if offset present?
264 for (const auto& item : js["frus"][file])
265 {
266 if (item.find("offset") != item.end())
267 {
268 offset = item["offset"];
269 }
270 }
271
272 // TODO: Figure out a better way to get max possible VPD size.
273 Binary vpdVector;
274 vpdVector.resize(65504);
275 ifstream vpdFile;
276 vpdFile.open(file, ios::binary);
277
278 vpdFile.seekg(offset, ios_base::cur);
279 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
280 vpdVector.resize(vpdFile.gcount());
281
282 return vpdVector;
283}
284
Alpana Kumari2f793042020-08-18 05:51:03 -0500285/** This API will be called at the end of VPD collection to perform any post
286 * actions.
287 *
288 * @param[in] json - json object
289 * @param[in] file - eeprom file path
290 */
291static void postFailAction(const nlohmann::json& json, const string& file)
292{
293 if ((json["frus"][file].at(0)).find("postActionFail") ==
294 json["frus"][file].at(0).end())
295 {
296 return;
297 }
298
299 uint8_t pinValue = 0;
300 string pinName;
301
302 for (const auto& postAction :
303 (json["frus"][file].at(0))["postActionFail"].items())
304 {
305 if (postAction.key() == "pin")
306 {
307 pinName = postAction.value();
308 }
309 else if (postAction.key() == "value")
310 {
311 // Get the value to set
312 pinValue = postAction.value();
313 }
314 }
315
316 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
317
318 try
319 {
320 gpiod::line outputLine = gpiod::find_line(pinName);
321
322 if (!outputLine)
323 {
324 cout << "Couldn't find output line:" << pinName
325 << " on GPIO. Skipping...\n";
326
327 return;
328 }
329 outputLine.request(
330 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
331 pinValue);
332 }
333 catch (system_error&)
334 {
335 cerr << "Failed to set post-action GPIO" << endl;
336 }
337}
338
339/** Performs any pre-action needed to get the FRU setup for collection.
340 *
341 * @param[in] json - json object
342 * @param[in] file - eeprom file path
343 */
344static void preAction(const nlohmann::json& json, const string& file)
345{
346 if ((json["frus"][file].at(0)).find("preAction") ==
347 json["frus"][file].at(0).end())
348 {
349 return;
350 }
351
352 uint8_t pinValue = 0;
353 string pinName;
354
355 for (const auto& postAction :
356 (json["frus"][file].at(0))["preAction"].items())
357 {
358 if (postAction.key() == "pin")
359 {
360 pinName = postAction.value();
361 }
362 else if (postAction.key() == "value")
363 {
364 // Get the value to set
365 pinValue = postAction.value();
366 }
367 }
368
369 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
370 try
371 {
372 gpiod::line outputLine = gpiod::find_line(pinName);
373
374 if (!outputLine)
375 {
376 cout << "Couldn't find output line:" << pinName
377 << " on GPIO. Skipping...\n";
378
379 return;
380 }
381 outputLine.request(
382 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
383 pinValue);
384 }
385 catch (system_error&)
386 {
387 cerr << "Failed to set pre-action GPIO" << endl;
388 return;
389 }
390
391 // Now bind the device
392 string bind = json["frus"][file].at(0).value("bind", "");
393 cout << "Binding device " << bind << endl;
394 string bindCmd = string("echo \"") + bind +
395 string("\" > /sys/bus/i2c/drivers/at24/bind");
396 cout << bindCmd << endl;
397 executeCmd(bindCmd);
398
399 // Check if device showed up (test for file)
400 if (!fs::exists(file))
401 {
402 cout << "EEPROM " << file << " does not exist. Take failure action"
403 << endl;
404 // If not, then take failure postAction
405 postFailAction(json, file);
406 }
407}
408
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530409/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530410 * @brief Prime the Inventory
411 * Prime the inventory by populating only the location code,
412 * type interface and the inventory object for the frus
413 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530414 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530415 * @param[in] jsObject - Reference to vpd inventory json object
416 * @param[in] vpdMap - Reference to the parsed vpd map
417 *
418 * @returns Map of items in extraInterface.
419 */
420template <typename T>
421inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
422 const T& vpdMap)
423{
424 inventory::ObjectMap objects;
425
426 for (auto& itemFRUS : jsObject["frus"].items())
427 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500428 // Take pre actions
429 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530430 for (auto& itemEEPROM : itemFRUS.value())
431 {
432 inventory::InterfaceMap interfaces;
433 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
434 inventory::Object object(itemEEPROM.at("inventoryPath"));
435
436 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
437 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600438 inventory::PropertyMap presProp;
439 presProp.emplace("Present", false);
440 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
441 move(presProp));
442
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530443 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
444 {
445 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
446 {
447 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000448 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530449 {
450 if constexpr (std::is_same<T, Parsed>::value)
451 {
452 for (auto& lC : eI.value().items())
453 {
454 auto propVal = expandLocationCode(
455 lC.value().get<string>(), vpdMap, true);
456
457 props.emplace(move(lC.key()),
458 move(propVal));
459 interfaces.emplace(move(eI.key()),
460 move(props));
461 }
462 }
463 }
464 else if (eI.key().find("Inventory.Item.") !=
465 string::npos)
466 {
467 interfaces.emplace(move(eI.key()), move(props));
468 }
469 }
470 }
471 objects.emplace(move(object), move(interfaces));
472 }
473 }
474 }
475 return objects;
476}
477
Alpana Kumari65b83602020-09-01 00:24:56 -0500478/**
479 * @brief This API executes command to set environment variable
480 * And then reboot the system
481 * @param[in] key -env key to set new value
482 * @param[in] value -value to set.
483 */
484void setEnvAndReboot(const string& key, const string& value)
485{
486 // set env and reboot and break.
487 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600488 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500489 // make dbus call to reboot
490 auto bus = sdbusplus::bus::new_default_system();
491 auto method = bus.new_method_call(
492 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
493 "org.freedesktop.systemd1.Manager", "Reboot");
494 bus.call_noreply(method);
495}
496
497/*
498 * @brief This API checks for env var fitconfig.
499 * If not initialised OR updated as per the current system type,
500 * update this env var and reboot the system.
501 *
502 * @param[in] systemType IM kwd in vpd tells about which system type it is.
503 * */
504void setDevTreeEnv(const string& systemType)
505{
506 string newDeviceTree;
507
508 if (deviceTreeSystemTypeMap.find(systemType) !=
509 deviceTreeSystemTypeMap.end())
510 {
511 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
512 }
513
514 string readVarValue;
515 bool envVarFound = false;
516
517 vector<string> output = executeCmd("/sbin/fw_printenv");
518 for (const auto& entry : output)
519 {
520 size_t pos = entry.find("=");
521 string key = entry.substr(0, pos);
522 if (key != "fitconfig")
523 {
524 continue;
525 }
526
527 envVarFound = true;
528 if (pos + 1 < entry.size())
529 {
530 readVarValue = entry.substr(pos + 1);
531 if (readVarValue.find(newDeviceTree) != string::npos)
532 {
533 // fitconfig is Updated. No action needed
534 break;
535 }
536 }
537 // set env and reboot and break.
538 setEnvAndReboot(key, newDeviceTree);
539 exit(0);
540 }
541
542 // check If env var Not found
543 if (!envVarFound)
544 {
545 setEnvAndReboot("fitconfig", newDeviceTree);
546 }
547}
548
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530549/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500550 * @brief API to call VPD manager to write VPD to EEPROM.
551 * @param[in] Object path.
552 * @param[in] record to be updated.
553 * @param[in] keyword to be updated.
554 * @param[in] keyword data to be updated
555 */
556void updateHardware(const string& objectName, const string& recName,
557 const string& kwdName, const Binary& data)
558{
559 try
560 {
561 auto bus = sdbusplus::bus::new_default();
562 auto properties =
563 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
564 properties.append(
565 static_cast<sdbusplus::message::object_path>(objectName));
566 properties.append(recName);
567 properties.append(kwdName);
568 properties.append(data);
569 bus.call(properties);
570 }
571 catch (const sdbusplus::exception::SdBusError& e)
572 {
573 std::string what =
574 "VPDManager WriteKeyword api failed for inventory path " +
575 objectName;
576 what += " record " + recName;
577 what += " keyword " + kwdName;
578 what += " with bus error = " + std::string(e.what());
579
580 // map to hold additional data in case of logging pel
581 PelAdditionalData additionalData{};
582 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
583 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500584 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500585 }
586}
587
588/**
589 * @brief API to check if we need to restore system VPD
590 * This functionality is only applicable for IPZ VPD data.
591 * @param[in] vpdMap - IPZ vpd map
592 * @param[in] objectPath - Object path for the FRU
593 * @return EEPROMs with records and keywords updated at standby
594 */
595std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
596 const string& objectPath)
597{
598 // the list of keywords for VSYS record is as per the S0 system. Should be
599 // updated for another type of systems
600 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
601 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
602 {"VCEN", {"FC", "SE"}},
603 {"LXR0", {"LX"}}};
604
605 // vector to hold all the EEPROMs updated at standby
606 std::vector<RestoredEeproms> updatedEeproms = {};
607
608 for (const auto& systemRecKwdPair : svpdKwdMap)
609 {
610 auto it = vpdMap.find(systemRecKwdPair.first);
611
612 // check if record is found in map we got by parser
613 if (it != vpdMap.end())
614 {
615 const auto& kwdListForRecord = systemRecKwdPair.second;
616 for (const auto& keyword : kwdListForRecord)
617 {
618 DbusPropertyMap& kwdValMap = it->second;
619 auto iterator = kwdValMap.find(keyword);
620
621 if (iterator != kwdValMap.end())
622 {
623 string& kwdValue = iterator->second;
624
625 // check bus data
626 const string& recordName = systemRecKwdPair.first;
627 const string& busValue = readBusProperty(
628 objectPath, ipzVpdInf + recordName, keyword);
629
630 if (busValue.find_first_not_of(' ') != string::npos)
631 {
632 if (kwdValue.find_first_not_of(' ') != string::npos)
633 {
634 // both the data are present, check for mismatch
635 if (busValue != kwdValue)
636 {
637 string errMsg = "VPD data mismatch on cache "
638 "and hardware for record: ";
639 errMsg += (*it).first;
640 errMsg += " and keyword: ";
641 errMsg += keyword;
642
643 // data mismatch
644 PelAdditionalData additionalData;
645 additionalData.emplace("CALLOUT_INVENTORY_PATH",
646 objectPath);
647
648 additionalData.emplace("DESCRIPTION", errMsg);
649
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500650 createPEL(additionalData, PelSeverity::WARNING,
651 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500652 }
653 }
654 else
655 {
656 // implies hardware data is blank
657 // update the map
658 Binary busData(busValue.begin(), busValue.end());
659
660 updatedEeproms.push_back(std::make_tuple(
661 objectPath, recordName, keyword, busData));
662 }
663
664 // update the map as well, so that cache data is not
665 // updated as blank while populating VPD map on Dbus in
666 // populateDBus Api
667 kwdValue = busValue;
668 continue;
669 }
670 else if (kwdValue.find_first_not_of(' ') == string::npos)
671 {
672 string errMsg = "VPD is blank on both cache and "
673 "hardware for record: ";
674 errMsg += (*it).first;
675 errMsg += " and keyword: ";
676 errMsg += keyword;
677 errMsg += ". SSR need to update hardware VPD.";
678
679 // both the data are blanks, log PEL
680 PelAdditionalData additionalData;
681 additionalData.emplace("CALLOUT_INVENTORY_PATH",
682 objectPath);
683
684 additionalData.emplace("DESCRIPTION", errMsg);
685
686 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500687 createPEL(additionalData, PelSeverity::ERROR,
688 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500689 continue;
690 }
691 }
692 }
693 }
694 }
695
696 return updatedEeproms;
697}
698
699/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530700 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530701 * This method invokes all the populateInterface functions
702 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530703 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
704 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530705 * @param[in] js - Inventory json object
706 * @param[in] filePath - Path of the vpd file
707 * @param[in] preIntrStr - Interface string
708 */
709template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500710static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530711{
712 inventory::InterfaceMap interfaces;
713 inventory::ObjectMap objects;
714 inventory::PropertyMap prop;
715
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500716 // map to hold all the keywords whose value has been changed at standby
717 vector<RestoredEeproms> updatedEeproms = {};
718
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500719 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530720 for (const auto& item : js["frus"][filePath])
721 {
722 const auto& objectPath = item["inventoryPath"];
723 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530724 isSystemVpd = item.value("isSystemVpd", false);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500725
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530726 // Populate the VPD keywords and the common interfaces only if we
727 // are asked to inherit that data from the VPD, else only add the
728 // extraInterfaces.
729 if (item.value("inherit", true))
730 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500731 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530732 {
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500733 if (isSystemVpd)
734 {
735 std::vector<std::string> interfaces = {
736 motherBoardInterface};
737 // call mapper to check for object path creation
738 MapperResponse subTree =
739 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
740
741 // Skip system vpd restore if object path is not generated
742 // for motherboard, Implies first boot.
743 if (subTree.size() != 0)
744 {
745 assert(
746 (subTree.find(pimPath + std::string(objectPath)) !=
747 subTree.end()));
748
749 updatedEeproms = restoreSystemVPD(vpdMap, objectPath);
750 }
751 else
752 {
753 log<level::ERR>("No object path found");
754 }
755 }
756
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530757 // Each record in the VPD becomes an interface and all
758 // keyword within the record are properties under that
759 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530760 for (const auto& record : vpdMap)
761 {
762 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500763 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530764 }
765 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500766 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530767 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500768 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530769 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530770 if (js.find("commonInterfaces") != js.end())
771 {
772 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
773 isSystemVpd);
774 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530775 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500776 else
777 {
778 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500779 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500780 {
781 if (item.find("copyRecords") != item.end())
782 {
783 for (const auto& record : item["copyRecords"])
784 {
785 const string& recordName = record;
786 if (vpdMap.find(recordName) != vpdMap.end())
787 {
788 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500789 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500790 interfaces);
791 }
792 }
793 }
794 }
795 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500796 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530797 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500798 // Populate interfaces and properties that are common to every FRU
799 // and additional interface that might be defined on a per-FRU
800 // basis.
801 if (item.find("extraInterfaces") != item.end())
802 {
803 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
804 isSystemVpd);
805 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530806 }
807 objects.emplace(move(object), move(interfaces));
808 }
809
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530810 if (isSystemVpd)
811 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530812 vector<uint8_t> imVal;
813 if constexpr (is_same<T, Parsed>::value)
814 {
815 auto property = vpdMap.find("VSBP");
816 if (property != vpdMap.end())
817 {
818 auto value = (property->second).find("IM");
819 if (value != (property->second).end())
820 {
821 copy(value->second.begin(), value->second.end(),
822 back_inserter(imVal));
823 }
824 }
825 }
826
827 fs::path target;
828 fs::path link = INVENTORY_JSON_SYM_LINK;
829
830 ostringstream oss;
831 for (auto& i : imVal)
832 {
833 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
834 }
835 string imValStr = oss.str();
836
Andrew Geissler7c9cddf2021-03-19 10:34:31 -0500837 if ((imValStr == RAINIER_4U) || // 4U
838 (imValStr == RAINIER_1S4U))
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530839 {
840 target = INVENTORY_JSON_4U;
841 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500842 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530843 {
844 target = INVENTORY_JSON_2U;
845 }
Santosh Puranik4641bff2020-11-30 20:26:44 +0530846 else if (imValStr == EVEREST)
847 {
848 target = INVENTORY_JSON_EVEREST;
849 }
PriyangaRamasamya504c3e2020-12-06 12:14:52 -0600850 else
851 {
852 PelAdditionalData additionalData{};
853 const string& baseFruInventoryPath =
854 js["frus"][filePath][0]["inventoryPath"].get_ref<string&>();
855 additionalData.emplace("CALLOUT_INVENTORY_PATH",
856 INVENTORY_PATH + baseFruInventoryPath);
857 additionalData.emplace(
858 "DESCRIPTION", "System IM value is erroneous/not supported.");
859 additionalData.emplace("INVALID IM VALUE", imValStr);
860 createPEL(additionalData, PelSeverity::ERROR, errIntfForInvalidVPD);
861 throw runtime_error(
862 "Erroneous/Unsupported IM in System VPD. PEL logged.");
863 }
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530864
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530865 // Create the directory for hosting the symlink
866 fs::create_directories(VPD_FILES_PATH);
867 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530868 remove(INVENTORY_JSON_SYM_LINK);
869 // create a new symlink based on the system
870 fs::create_symlink(target, link);
871
872 // Reloading the json
873 ifstream inventoryJson(link);
874 auto js = json::parse(inventoryJson);
875 inventoryJson.close();
876
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530877 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
878 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500879
880 // set the U-boot environment variable for device-tree
881 setDevTreeEnv(imValStr);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500882
883 // if system VPD has been restored at standby, update the EEPROM
884 for (const auto& item : updatedEeproms)
885 {
886 updateHardware(get<0>(item), get<1>(item), get<2>(item),
887 get<3>(item));
888 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530889 }
890
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530891 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500892 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530893}
894
895int main(int argc, char** argv)
896{
897 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500898 string file{};
899 json js{};
900
901 // map to hold additional data in case of logging pel
902 PelAdditionalData additionalData{};
903
904 // this is needed to hold base fru inventory path in case there is ECC or
905 // vpd exception while parsing the file
906 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530907
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500908 // severity for PEL
909 PelSeverity pelSeverity = PelSeverity::WARNING;
910
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530911 try
912 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530913 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
914 "in DBUS"};
915 string file{};
916
917 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -0500918 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530919
920 CLI11_PARSE(app, argc, argv);
921
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500922 // PEL severity should be ERROR in case of any system VPD failure
923 if (file == systemVpdFilePath)
924 {
925 pelSeverity = PelSeverity::ERROR;
926 }
927
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530928 auto jsonToParse = INVENTORY_JSON_DEFAULT;
929
930 // If the symlink exists, it means it has been setup for us, switch the
931 // path
932 if (fs::exists(INVENTORY_JSON_SYM_LINK))
933 {
934 jsonToParse = INVENTORY_JSON_SYM_LINK;
935 }
936
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530937 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530938 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500939 if (!inventoryJson)
940 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500941 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500942 }
943
944 try
945 {
946 js = json::parse(inventoryJson);
947 }
948 catch (json::parse_error& ex)
949 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500950 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500951 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530952
953 if ((js.find("frus") == js.end()) ||
954 (js["frus"].find(file) == js["frus"].end()))
955 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500956 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530957 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530958 }
959
Alpana Kumari2f793042020-08-18 05:51:03 -0500960 if (!fs::exists(file))
961 {
962 cout << "Device path: " << file
963 << " does not exist. Spurious udev event? Exiting." << endl;
964 return 0;
965 }
966
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500967 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +0530968 // Check if we can read the VPD file based on the power state
969 if (js["frus"][file].at(0).value("powerOffOnly", false))
970 {
971 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
972 getPowerState())
973 {
974 cout << "This VPD cannot be read when power is ON" << endl;
975 return 0;
976 }
977 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500978
Alpana Kumari2f793042020-08-18 05:51:03 -0500979 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530980 {
Alpana Kumari80f15342020-06-09 07:41:02 -0500981 // check if vpd file is empty
982 if (file.empty())
983 {
984 throw(VpdDataException(
985 "VPD file is empty. Can't process with blank file."));
986 }
987
Alpana Kumari2f793042020-08-18 05:51:03 -0500988 Binary vpdVector = getVpdDataInVector(js, file);
989 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500990
Alpana Kumari2f793042020-08-18 05:51:03 -0500991 variant<KeywordVpdMap, Store> parseResult;
992 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -0500993
Alpana Kumari2f793042020-08-18 05:51:03 -0500994 if (auto pVal = get_if<Store>(&parseResult))
995 {
996 populateDbus(pVal->getVpdMap(), js, file);
997 }
998 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
999 {
1000 populateDbus(*pVal, js, file);
1001 }
1002
1003 // release the parser object
1004 ParserFactory::freeParser(parser);
1005 }
1006 catch (exception& e)
1007 {
1008 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001009 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001010 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301011 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001012 catch (const VpdJsonException& ex)
1013 {
1014 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1015 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001016 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001017
1018 cerr << ex.what() << "\n";
1019 rc = -1;
1020 }
1021 catch (const VpdEccException& ex)
1022 {
1023 additionalData.emplace("DESCRIPTION", "ECC check failed");
1024 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1025 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001026 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001027
1028 cerr << ex.what() << "\n";
1029 rc = -1;
1030 }
1031 catch (const VpdDataException& ex)
1032 {
1033 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1034 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1035 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001036 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001037
1038 cerr << ex.what() << "\n";
1039 rc = -1;
1040 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301041 catch (exception& e)
1042 {
1043 cerr << e.what() << "\n";
1044 rc = -1;
1045 }
1046
1047 return rc;
Alpana Kumari80f15342020-06-09 07:41:02 -05001048}