blob: fed83ade440a676bffbc9fe968d8f01bb7f53222 [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" &&
210 inf == "com.ibm.ipzvpd.Location")
211 {
212 auto prop = expandLocationCode(
213 itr.value().get<string>(), vpdMap, isSystemVpd);
214 props.emplace(busProp, prop);
215 }
216 else
217 {
218 props.emplace(busProp, itr.value().get<string>());
219 }
220 }
221 else
222 {
223 props.emplace(busProp, itr.value().get<string>());
224 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530225 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600226 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530227 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600228 const string& rec = itr.value().value("recordName", "");
229 const string& kw = itr.value().value("keywordName", "");
230 const string& encoding = itr.value().value("encoding", "");
231
Alpana Kumari58e22142020-05-05 00:22:12 -0500232 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530233 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530234 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
235 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600236 {
237 auto encoded =
238 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530239 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600240 }
241 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500242 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600243 {
244 if (!kw.empty() && vpdMap.count(kw))
245 {
246 auto prop =
247 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
248 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530249 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600250 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530251 }
252 }
253 }
254 interfaces.emplace(inf, move(props));
255 }
256}
257
Alpana Kumari2f793042020-08-18 05:51:03 -0500258static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500259{
260 uint32_t offset = 0;
261 // check if offset present?
262 for (const auto& item : js["frus"][file])
263 {
264 if (item.find("offset") != item.end())
265 {
266 offset = item["offset"];
267 }
268 }
269
270 // TODO: Figure out a better way to get max possible VPD size.
271 Binary vpdVector;
272 vpdVector.resize(65504);
273 ifstream vpdFile;
274 vpdFile.open(file, ios::binary);
275
276 vpdFile.seekg(offset, ios_base::cur);
277 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
278 vpdVector.resize(vpdFile.gcount());
279
280 return vpdVector;
281}
282
Alpana Kumari2f793042020-08-18 05:51:03 -0500283/** This API will be called at the end of VPD collection to perform any post
284 * actions.
285 *
286 * @param[in] json - json object
287 * @param[in] file - eeprom file path
288 */
289static void postFailAction(const nlohmann::json& json, const string& file)
290{
291 if ((json["frus"][file].at(0)).find("postActionFail") ==
292 json["frus"][file].at(0).end())
293 {
294 return;
295 }
296
297 uint8_t pinValue = 0;
298 string pinName;
299
300 for (const auto& postAction :
301 (json["frus"][file].at(0))["postActionFail"].items())
302 {
303 if (postAction.key() == "pin")
304 {
305 pinName = postAction.value();
306 }
307 else if (postAction.key() == "value")
308 {
309 // Get the value to set
310 pinValue = postAction.value();
311 }
312 }
313
314 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
315
316 try
317 {
318 gpiod::line outputLine = gpiod::find_line(pinName);
319
320 if (!outputLine)
321 {
322 cout << "Couldn't find output line:" << pinName
323 << " on GPIO. Skipping...\n";
324
325 return;
326 }
327 outputLine.request(
328 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
329 pinValue);
330 }
331 catch (system_error&)
332 {
333 cerr << "Failed to set post-action GPIO" << endl;
334 }
335}
336
337/** Performs any pre-action needed to get the FRU setup for collection.
338 *
339 * @param[in] json - json object
340 * @param[in] file - eeprom file path
341 */
342static void preAction(const nlohmann::json& json, const string& file)
343{
344 if ((json["frus"][file].at(0)).find("preAction") ==
345 json["frus"][file].at(0).end())
346 {
347 return;
348 }
349
350 uint8_t pinValue = 0;
351 string pinName;
352
353 for (const auto& postAction :
354 (json["frus"][file].at(0))["preAction"].items())
355 {
356 if (postAction.key() == "pin")
357 {
358 pinName = postAction.value();
359 }
360 else if (postAction.key() == "value")
361 {
362 // Get the value to set
363 pinValue = postAction.value();
364 }
365 }
366
367 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
368 try
369 {
370 gpiod::line outputLine = gpiod::find_line(pinName);
371
372 if (!outputLine)
373 {
374 cout << "Couldn't find output line:" << pinName
375 << " on GPIO. Skipping...\n";
376
377 return;
378 }
379 outputLine.request(
380 {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
381 pinValue);
382 }
383 catch (system_error&)
384 {
385 cerr << "Failed to set pre-action GPIO" << endl;
386 return;
387 }
388
389 // Now bind the device
390 string bind = json["frus"][file].at(0).value("bind", "");
391 cout << "Binding device " << bind << endl;
392 string bindCmd = string("echo \"") + bind +
393 string("\" > /sys/bus/i2c/drivers/at24/bind");
394 cout << bindCmd << endl;
395 executeCmd(bindCmd);
396
397 // Check if device showed up (test for file)
398 if (!fs::exists(file))
399 {
400 cout << "EEPROM " << file << " does not exist. Take failure action"
401 << endl;
402 // If not, then take failure postAction
403 postFailAction(json, file);
404 }
405}
406
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530407/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530408 * @brief Prime the Inventory
409 * Prime the inventory by populating only the location code,
410 * type interface and the inventory object for the frus
411 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530412 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530413 * @param[in] jsObject - Reference to vpd inventory json object
414 * @param[in] vpdMap - Reference to the parsed vpd map
415 *
416 * @returns Map of items in extraInterface.
417 */
418template <typename T>
419inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
420 const T& vpdMap)
421{
422 inventory::ObjectMap objects;
423
424 for (auto& itemFRUS : jsObject["frus"].items())
425 {
Alpana Kumari2f793042020-08-18 05:51:03 -0500426 // Take pre actions
427 preAction(jsObject, itemFRUS.key());
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530428 for (auto& itemEEPROM : itemFRUS.value())
429 {
430 inventory::InterfaceMap interfaces;
431 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
432 inventory::Object object(itemEEPROM.at("inventoryPath"));
433
434 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
435 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600436 inventory::PropertyMap presProp;
437 presProp.emplace("Present", false);
438 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
439 move(presProp));
440
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530441 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
442 {
443 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
444 {
445 inventory::PropertyMap props;
446 if (eI.key() ==
447 openpower::vpd::constants::LOCATION_CODE_INF)
448 {
449 if constexpr (std::is_same<T, Parsed>::value)
450 {
451 for (auto& lC : eI.value().items())
452 {
453 auto propVal = expandLocationCode(
454 lC.value().get<string>(), vpdMap, true);
455
456 props.emplace(move(lC.key()),
457 move(propVal));
458 interfaces.emplace(move(eI.key()),
459 move(props));
460 }
461 }
462 }
463 else if (eI.key().find("Inventory.Item.") !=
464 string::npos)
465 {
466 interfaces.emplace(move(eI.key()), move(props));
467 }
468 }
469 }
470 objects.emplace(move(object), move(interfaces));
471 }
472 }
473 }
474 return objects;
475}
476
Alpana Kumari65b83602020-09-01 00:24:56 -0500477/**
478 * @brief This API executes command to set environment variable
479 * And then reboot the system
480 * @param[in] key -env key to set new value
481 * @param[in] value -value to set.
482 */
483void setEnvAndReboot(const string& key, const string& value)
484{
485 // set env and reboot and break.
486 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600487 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500488 // make dbus call to reboot
489 auto bus = sdbusplus::bus::new_default_system();
490 auto method = bus.new_method_call(
491 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
492 "org.freedesktop.systemd1.Manager", "Reboot");
493 bus.call_noreply(method);
494}
495
496/*
497 * @brief This API checks for env var fitconfig.
498 * If not initialised OR updated as per the current system type,
499 * update this env var and reboot the system.
500 *
501 * @param[in] systemType IM kwd in vpd tells about which system type it is.
502 * */
503void setDevTreeEnv(const string& systemType)
504{
505 string newDeviceTree;
506
507 if (deviceTreeSystemTypeMap.find(systemType) !=
508 deviceTreeSystemTypeMap.end())
509 {
510 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
511 }
512
513 string readVarValue;
514 bool envVarFound = false;
515
516 vector<string> output = executeCmd("/sbin/fw_printenv");
517 for (const auto& entry : output)
518 {
519 size_t pos = entry.find("=");
520 string key = entry.substr(0, pos);
521 if (key != "fitconfig")
522 {
523 continue;
524 }
525
526 envVarFound = true;
527 if (pos + 1 < entry.size())
528 {
529 readVarValue = entry.substr(pos + 1);
530 if (readVarValue.find(newDeviceTree) != string::npos)
531 {
532 // fitconfig is Updated. No action needed
533 break;
534 }
535 }
536 // set env and reboot and break.
537 setEnvAndReboot(key, newDeviceTree);
538 exit(0);
539 }
540
541 // check If env var Not found
542 if (!envVarFound)
543 {
544 setEnvAndReboot("fitconfig", newDeviceTree);
545 }
546}
547
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530548/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500549 * @brief API to call VPD manager to write VPD to EEPROM.
550 * @param[in] Object path.
551 * @param[in] record to be updated.
552 * @param[in] keyword to be updated.
553 * @param[in] keyword data to be updated
554 */
555void updateHardware(const string& objectName, const string& recName,
556 const string& kwdName, const Binary& data)
557{
558 try
559 {
560 auto bus = sdbusplus::bus::new_default();
561 auto properties =
562 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
563 properties.append(
564 static_cast<sdbusplus::message::object_path>(objectName));
565 properties.append(recName);
566 properties.append(kwdName);
567 properties.append(data);
568 bus.call(properties);
569 }
570 catch (const sdbusplus::exception::SdBusError& e)
571 {
572 std::string what =
573 "VPDManager WriteKeyword api failed for inventory path " +
574 objectName;
575 what += " record " + recName;
576 what += " keyword " + kwdName;
577 what += " with bus error = " + std::string(e.what());
578
579 // map to hold additional data in case of logging pel
580 PelAdditionalData additionalData{};
581 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
582 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500583 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500584 }
585}
586
587/**
588 * @brief API to check if we need to restore system VPD
589 * This functionality is only applicable for IPZ VPD data.
590 * @param[in] vpdMap - IPZ vpd map
591 * @param[in] objectPath - Object path for the FRU
592 * @return EEPROMs with records and keywords updated at standby
593 */
594std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap,
595 const string& objectPath)
596{
597 // the list of keywords for VSYS record is as per the S0 system. Should be
598 // updated for another type of systems
599 static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{
600 {"VSYS", {"BR", "TM", "SE", "SU", "RB"}},
601 {"VCEN", {"FC", "SE"}},
602 {"LXR0", {"LX"}}};
603
604 // vector to hold all the EEPROMs updated at standby
605 std::vector<RestoredEeproms> updatedEeproms = {};
606
607 for (const auto& systemRecKwdPair : svpdKwdMap)
608 {
609 auto it = vpdMap.find(systemRecKwdPair.first);
610
611 // check if record is found in map we got by parser
612 if (it != vpdMap.end())
613 {
614 const auto& kwdListForRecord = systemRecKwdPair.second;
615 for (const auto& keyword : kwdListForRecord)
616 {
617 DbusPropertyMap& kwdValMap = it->second;
618 auto iterator = kwdValMap.find(keyword);
619
620 if (iterator != kwdValMap.end())
621 {
622 string& kwdValue = iterator->second;
623
624 // check bus data
625 const string& recordName = systemRecKwdPair.first;
626 const string& busValue = readBusProperty(
627 objectPath, ipzVpdInf + recordName, keyword);
628
629 if (busValue.find_first_not_of(' ') != string::npos)
630 {
631 if (kwdValue.find_first_not_of(' ') != string::npos)
632 {
633 // both the data are present, check for mismatch
634 if (busValue != kwdValue)
635 {
636 string errMsg = "VPD data mismatch on cache "
637 "and hardware for record: ";
638 errMsg += (*it).first;
639 errMsg += " and keyword: ";
640 errMsg += keyword;
641
642 // data mismatch
643 PelAdditionalData additionalData;
644 additionalData.emplace("CALLOUT_INVENTORY_PATH",
645 objectPath);
646
647 additionalData.emplace("DESCRIPTION", errMsg);
648
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500649 createPEL(additionalData, PelSeverity::WARNING,
650 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500651 }
652 }
653 else
654 {
655 // implies hardware data is blank
656 // update the map
657 Binary busData(busValue.begin(), busValue.end());
658
659 updatedEeproms.push_back(std::make_tuple(
660 objectPath, recordName, keyword, busData));
661 }
662
663 // update the map as well, so that cache data is not
664 // updated as blank while populating VPD map on Dbus in
665 // populateDBus Api
666 kwdValue = busValue;
667 continue;
668 }
669 else if (kwdValue.find_first_not_of(' ') == string::npos)
670 {
671 string errMsg = "VPD is blank on both cache and "
672 "hardware for record: ";
673 errMsg += (*it).first;
674 errMsg += " and keyword: ";
675 errMsg += keyword;
676 errMsg += ". SSR need to update hardware VPD.";
677
678 // both the data are blanks, log PEL
679 PelAdditionalData additionalData;
680 additionalData.emplace("CALLOUT_INVENTORY_PATH",
681 objectPath);
682
683 additionalData.emplace("DESCRIPTION", errMsg);
684
685 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500686 createPEL(additionalData, PelSeverity::ERROR,
687 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500688 continue;
689 }
690 }
691 }
692 }
693 }
694
695 return updatedEeproms;
696}
697
698/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530699 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530700 * This method invokes all the populateInterface functions
701 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530702 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
703 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530704 * @param[in] js - Inventory json object
705 * @param[in] filePath - Path of the vpd file
706 * @param[in] preIntrStr - Interface string
707 */
708template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500709static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530710{
711 inventory::InterfaceMap interfaces;
712 inventory::ObjectMap objects;
713 inventory::PropertyMap prop;
714
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500715 // map to hold all the keywords whose value has been changed at standby
716 vector<RestoredEeproms> updatedEeproms = {};
717
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500718 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530719 for (const auto& item : js["frus"][filePath])
720 {
721 const auto& objectPath = item["inventoryPath"];
722 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530723 isSystemVpd = item.value("isSystemVpd", false);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500724
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530725 // Populate the VPD keywords and the common interfaces only if we
726 // are asked to inherit that data from the VPD, else only add the
727 // extraInterfaces.
728 if (item.value("inherit", true))
729 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500730 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530731 {
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500732 if (isSystemVpd)
733 {
734 std::vector<std::string> interfaces = {
735 motherBoardInterface};
736 // call mapper to check for object path creation
737 MapperResponse subTree =
738 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
739
740 // Skip system vpd restore if object path is not generated
741 // for motherboard, Implies first boot.
742 if (subTree.size() != 0)
743 {
744 assert(
745 (subTree.find(pimPath + std::string(objectPath)) !=
746 subTree.end()));
747
748 updatedEeproms = restoreSystemVPD(vpdMap, objectPath);
749 }
750 else
751 {
752 log<level::ERR>("No object path found");
753 }
754 }
755
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530756 // Each record in the VPD becomes an interface and all
757 // keyword within the record are properties under that
758 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530759 for (const auto& record : vpdMap)
760 {
761 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500762 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530763 }
764 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500765 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530766 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500767 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530768 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530769 if (js.find("commonInterfaces") != js.end())
770 {
771 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
772 isSystemVpd);
773 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530774 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500775 else
776 {
777 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500778 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500779 {
780 if (item.find("copyRecords") != item.end())
781 {
782 for (const auto& record : item["copyRecords"])
783 {
784 const string& recordName = record;
785 if (vpdMap.find(recordName) != vpdMap.end())
786 {
787 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500788 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500789 interfaces);
790 }
791 }
792 }
793 }
794 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500795 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530796 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500797 // Populate interfaces and properties that are common to every FRU
798 // and additional interface that might be defined on a per-FRU
799 // basis.
800 if (item.find("extraInterfaces") != item.end())
801 {
802 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
803 isSystemVpd);
804 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530805 }
806 objects.emplace(move(object), move(interfaces));
807 }
808
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530809 if (isSystemVpd)
810 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530811 vector<uint8_t> imVal;
812 if constexpr (is_same<T, Parsed>::value)
813 {
814 auto property = vpdMap.find("VSBP");
815 if (property != vpdMap.end())
816 {
817 auto value = (property->second).find("IM");
818 if (value != (property->second).end())
819 {
820 copy(value->second.begin(), value->second.end(),
821 back_inserter(imVal));
822 }
823 }
824 }
825
826 fs::path target;
827 fs::path link = INVENTORY_JSON_SYM_LINK;
828
829 ostringstream oss;
830 for (auto& i : imVal)
831 {
832 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
833 }
834 string imValStr = oss.str();
835
Andrew Geissler7c9cddf2021-03-19 10:34:31 -0500836 if ((imValStr == RAINIER_4U) || // 4U
837 (imValStr == RAINIER_1S4U))
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530838 {
839 target = INVENTORY_JSON_4U;
840 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500841 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530842 {
843 target = INVENTORY_JSON_2U;
844 }
Santosh Puranik4641bff2020-11-30 20:26:44 +0530845 else if (imValStr == EVEREST)
846 {
847 target = INVENTORY_JSON_EVEREST;
848 }
PriyangaRamasamya504c3e2020-12-06 12:14:52 -0600849 else
850 {
851 PelAdditionalData additionalData{};
852 const string& baseFruInventoryPath =
853 js["frus"][filePath][0]["inventoryPath"].get_ref<string&>();
854 additionalData.emplace("CALLOUT_INVENTORY_PATH",
855 INVENTORY_PATH + baseFruInventoryPath);
856 additionalData.emplace(
857 "DESCRIPTION", "System IM value is erroneous/not supported.");
858 additionalData.emplace("INVALID IM VALUE", imValStr);
859 createPEL(additionalData, PelSeverity::ERROR, errIntfForInvalidVPD);
860 throw runtime_error(
861 "Erroneous/Unsupported IM in System VPD. PEL logged.");
862 }
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530863
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530864 // Create the directory for hosting the symlink
865 fs::create_directories(VPD_FILES_PATH);
866 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530867 remove(INVENTORY_JSON_SYM_LINK);
868 // create a new symlink based on the system
869 fs::create_symlink(target, link);
870
871 // Reloading the json
872 ifstream inventoryJson(link);
873 auto js = json::parse(inventoryJson);
874 inventoryJson.close();
875
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530876 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
877 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500878
879 // set the U-boot environment variable for device-tree
880 setDevTreeEnv(imValStr);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500881
882 // if system VPD has been restored at standby, update the EEPROM
883 for (const auto& item : updatedEeproms)
884 {
885 updateHardware(get<0>(item), get<1>(item), get<2>(item),
886 get<3>(item));
887 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530888 }
889
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530890 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500891 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530892}
893
894int main(int argc, char** argv)
895{
896 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500897 string file{};
898 json js{};
899
900 // map to hold additional data in case of logging pel
901 PelAdditionalData additionalData{};
902
903 // this is needed to hold base fru inventory path in case there is ECC or
904 // vpd exception while parsing the file
905 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530906
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500907 // severity for PEL
908 PelSeverity pelSeverity = PelSeverity::WARNING;
909
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530910 try
911 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530912 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
913 "in DBUS"};
914 string file{};
915
916 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -0500917 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530918
919 CLI11_PARSE(app, argc, argv);
920
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500921 // PEL severity should be ERROR in case of any system VPD failure
922 if (file == systemVpdFilePath)
923 {
924 pelSeverity = PelSeverity::ERROR;
925 }
926
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530927 auto jsonToParse = INVENTORY_JSON_DEFAULT;
928
929 // If the symlink exists, it means it has been setup for us, switch the
930 // path
931 if (fs::exists(INVENTORY_JSON_SYM_LINK))
932 {
933 jsonToParse = INVENTORY_JSON_SYM_LINK;
934 }
935
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530936 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530937 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500938 if (!inventoryJson)
939 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500940 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500941 }
942
943 try
944 {
945 js = json::parse(inventoryJson);
946 }
947 catch (json::parse_error& ex)
948 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500949 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500950 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530951
952 if ((js.find("frus") == js.end()) ||
953 (js["frus"].find(file) == js["frus"].end()))
954 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500955 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530956 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530957 }
958
Alpana Kumari2f793042020-08-18 05:51:03 -0500959 if (!fs::exists(file))
960 {
961 cout << "Device path: " << file
962 << " does not exist. Spurious udev event? Exiting." << endl;
963 return 0;
964 }
965
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500966 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +0530967 // Check if we can read the VPD file based on the power state
968 if (js["frus"][file].at(0).value("powerOffOnly", false))
969 {
970 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
971 getPowerState())
972 {
973 cout << "This VPD cannot be read when power is ON" << endl;
974 return 0;
975 }
976 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500977
Alpana Kumari2f793042020-08-18 05:51:03 -0500978 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530979 {
Alpana Kumari80f15342020-06-09 07:41:02 -0500980 // check if vpd file is empty
981 if (file.empty())
982 {
983 throw(VpdDataException(
984 "VPD file is empty. Can't process with blank file."));
985 }
986
Alpana Kumari2f793042020-08-18 05:51:03 -0500987 Binary vpdVector = getVpdDataInVector(js, file);
988 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500989
Alpana Kumari2f793042020-08-18 05:51:03 -0500990 variant<KeywordVpdMap, Store> parseResult;
991 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -0500992
Alpana Kumari2f793042020-08-18 05:51:03 -0500993 if (auto pVal = get_if<Store>(&parseResult))
994 {
995 populateDbus(pVal->getVpdMap(), js, file);
996 }
997 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
998 {
999 populateDbus(*pVal, js, file);
1000 }
1001
1002 // release the parser object
1003 ParserFactory::freeParser(parser);
1004 }
1005 catch (exception& e)
1006 {
1007 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001008 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001009 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301010 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001011 catch (const VpdJsonException& ex)
1012 {
1013 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1014 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001015 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001016
1017 cerr << ex.what() << "\n";
1018 rc = -1;
1019 }
1020 catch (const VpdEccException& ex)
1021 {
1022 additionalData.emplace("DESCRIPTION", "ECC check failed");
1023 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1024 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001025 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001026
1027 cerr << ex.what() << "\n";
1028 rc = -1;
1029 }
1030 catch (const VpdDataException& ex)
1031 {
1032 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1033 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1034 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001035 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001036
1037 cerr << ex.what() << "\n";
1038 rc = -1;
1039 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301040 catch (exception& e)
1041 {
1042 cerr << e.what() << "\n";
1043 rc = -1;
1044 }
1045
1046 return rc;
Alpana Kumari80f15342020-06-09 07:41:02 -05001047}