blob: 089ad9ee455d19be2fc2666b9191056ca99e3ecf [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
Santosh Puranikdf0a1e12021-05-03 16:06:35 +0530741 // Attempt system VPD restore if we have a motherboard
742 // object in the inventory.
743 if ((subTree.size() != 0) &&
744 (subTree.find(pimPath + std::string(objectPath)) !=
745 subTree.end()))
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500746 {
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500747 updatedEeproms = restoreSystemVPD(vpdMap, objectPath);
748 }
749 else
750 {
751 log<level::ERR>("No object path found");
752 }
753 }
754
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530755 // Each record in the VPD becomes an interface and all
756 // keyword within the record are properties under that
757 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530758 for (const auto& record : vpdMap)
759 {
760 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500761 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530762 }
763 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500764 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530765 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500766 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530767 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530768 if (js.find("commonInterfaces") != js.end())
769 {
770 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
771 isSystemVpd);
772 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530773 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500774 else
775 {
776 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500777 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500778 {
779 if (item.find("copyRecords") != item.end())
780 {
781 for (const auto& record : item["copyRecords"])
782 {
783 const string& recordName = record;
784 if (vpdMap.find(recordName) != vpdMap.end())
785 {
786 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500787 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500788 interfaces);
789 }
790 }
791 }
792 }
793 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500794 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530795 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500796 // Populate interfaces and properties that are common to every FRU
797 // and additional interface that might be defined on a per-FRU
798 // basis.
799 if (item.find("extraInterfaces") != item.end())
800 {
801 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
802 isSystemVpd);
803 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530804 }
805 objects.emplace(move(object), move(interfaces));
806 }
807
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530808 if (isSystemVpd)
809 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530810 vector<uint8_t> imVal;
811 if constexpr (is_same<T, Parsed>::value)
812 {
813 auto property = vpdMap.find("VSBP");
814 if (property != vpdMap.end())
815 {
816 auto value = (property->second).find("IM");
817 if (value != (property->second).end())
818 {
819 copy(value->second.begin(), value->second.end(),
820 back_inserter(imVal));
821 }
822 }
823 }
824
825 fs::path target;
826 fs::path link = INVENTORY_JSON_SYM_LINK;
827
828 ostringstream oss;
829 for (auto& i : imVal)
830 {
831 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
832 }
833 string imValStr = oss.str();
834
Andrew Geissler7c9cddf2021-03-19 10:34:31 -0500835 if ((imValStr == RAINIER_4U) || // 4U
836 (imValStr == RAINIER_1S4U))
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530837 {
838 target = INVENTORY_JSON_4U;
839 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500840 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530841 {
842 target = INVENTORY_JSON_2U;
843 }
Santosh Puranik4641bff2020-11-30 20:26:44 +0530844 else if (imValStr == EVEREST)
845 {
846 target = INVENTORY_JSON_EVEREST;
847 }
PriyangaRamasamya504c3e2020-12-06 12:14:52 -0600848 else
849 {
850 PelAdditionalData additionalData{};
851 const string& baseFruInventoryPath =
852 js["frus"][filePath][0]["inventoryPath"].get_ref<string&>();
853 additionalData.emplace("CALLOUT_INVENTORY_PATH",
854 INVENTORY_PATH + baseFruInventoryPath);
855 additionalData.emplace(
856 "DESCRIPTION", "System IM value is erroneous/not supported.");
857 additionalData.emplace("INVALID IM VALUE", imValStr);
858 createPEL(additionalData, PelSeverity::ERROR, errIntfForInvalidVPD);
859 throw runtime_error(
860 "Erroneous/Unsupported IM in System VPD. PEL logged.");
861 }
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530862
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530863 // Create the directory for hosting the symlink
864 fs::create_directories(VPD_FILES_PATH);
865 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530866 remove(INVENTORY_JSON_SYM_LINK);
867 // create a new symlink based on the system
868 fs::create_symlink(target, link);
869
870 // Reloading the json
871 ifstream inventoryJson(link);
872 auto js = json::parse(inventoryJson);
873 inventoryJson.close();
874
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530875 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
876 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500877
878 // set the U-boot environment variable for device-tree
879 setDevTreeEnv(imValStr);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500880
881 // if system VPD has been restored at standby, update the EEPROM
882 for (const auto& item : updatedEeproms)
883 {
884 updateHardware(get<0>(item), get<1>(item), get<2>(item),
885 get<3>(item));
886 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530887 }
888
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530889 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500890 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530891}
892
893int main(int argc, char** argv)
894{
895 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500896 string file{};
897 json js{};
898
899 // map to hold additional data in case of logging pel
900 PelAdditionalData additionalData{};
901
902 // this is needed to hold base fru inventory path in case there is ECC or
903 // vpd exception while parsing the file
904 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530905
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500906 // severity for PEL
907 PelSeverity pelSeverity = PelSeverity::WARNING;
908
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530909 try
910 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530911 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
912 "in DBUS"};
913 string file{};
914
915 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -0500916 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530917
918 CLI11_PARSE(app, argc, argv);
919
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500920 // PEL severity should be ERROR in case of any system VPD failure
921 if (file == systemVpdFilePath)
922 {
923 pelSeverity = PelSeverity::ERROR;
924 }
925
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530926 auto jsonToParse = INVENTORY_JSON_DEFAULT;
927
928 // If the symlink exists, it means it has been setup for us, switch the
929 // path
930 if (fs::exists(INVENTORY_JSON_SYM_LINK))
931 {
932 jsonToParse = INVENTORY_JSON_SYM_LINK;
933 }
934
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530935 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530936 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500937 if (!inventoryJson)
938 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500939 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500940 }
941
942 try
943 {
944 js = json::parse(inventoryJson);
945 }
946 catch (json::parse_error& ex)
947 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500948 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500949 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530950
951 if ((js.find("frus") == js.end()) ||
952 (js["frus"].find(file) == js["frus"].end()))
953 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500954 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530955 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530956 }
957
Alpana Kumari2f793042020-08-18 05:51:03 -0500958 if (!fs::exists(file))
959 {
960 cout << "Device path: " << file
961 << " does not exist. Spurious udev event? Exiting." << endl;
962 return 0;
963 }
964
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500965 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +0530966 // Check if we can read the VPD file based on the power state
967 if (js["frus"][file].at(0).value("powerOffOnly", false))
968 {
969 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
970 getPowerState())
971 {
972 cout << "This VPD cannot be read when power is ON" << endl;
973 return 0;
974 }
975 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500976
Alpana Kumari2f793042020-08-18 05:51:03 -0500977 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530978 {
Alpana Kumari80f15342020-06-09 07:41:02 -0500979 // check if vpd file is empty
980 if (file.empty())
981 {
982 throw(VpdDataException(
983 "VPD file is empty. Can't process with blank file."));
984 }
985
Alpana Kumari2f793042020-08-18 05:51:03 -0500986 Binary vpdVector = getVpdDataInVector(js, file);
987 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500988
Alpana Kumari2f793042020-08-18 05:51:03 -0500989 variant<KeywordVpdMap, Store> parseResult;
990 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -0500991
Alpana Kumari2f793042020-08-18 05:51:03 -0500992 if (auto pVal = get_if<Store>(&parseResult))
993 {
994 populateDbus(pVal->getVpdMap(), js, file);
995 }
996 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
997 {
998 populateDbus(*pVal, js, file);
999 }
1000
1001 // release the parser object
1002 ParserFactory::freeParser(parser);
1003 }
1004 catch (exception& e)
1005 {
1006 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001007 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001008 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301009 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001010 catch (const VpdJsonException& ex)
1011 {
1012 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1013 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001014 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001015
1016 cerr << ex.what() << "\n";
1017 rc = -1;
1018 }
1019 catch (const VpdEccException& ex)
1020 {
1021 additionalData.emplace("DESCRIPTION", "ECC check failed");
1022 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1023 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001024 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001025
1026 cerr << ex.what() << "\n";
1027 rc = -1;
1028 }
1029 catch (const VpdDataException& ex)
1030 {
1031 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1032 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1033 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001034 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001035
1036 cerr << ex.what() << "\n";
1037 rc = -1;
1038 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301039 catch (exception& e)
1040 {
1041 cerr << e.what() << "\n";
1042 rc = -1;
1043 }
1044
1045 return rc;
Alpana Kumari80f15342020-06-09 07:41:02 -05001046}