blob: 2d460d630a8e1d33ae9a9c21247b10346b8f4443 [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05003#include "common_utility.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05304#include "defines.hpp"
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05005#include "ibm_vpd_utils.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05006#include "ipz_parser.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05307#include "keyword_vpd_parser.hpp"
Alpana Kumaria00936f2020-04-14 07:15:46 -05008#include "memory_vpd_parser.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05009#include "parser_factory.hpp"
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050010#include "vpd_exceptions.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053011
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012#include <assert.h>
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -050013#include <ctype.h>
14
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053015#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053016#include <algorithm>
alpana077ce68722021-07-25 13:23:59 -050017#include <boost/algorithm/string.hpp>
Alpana Kumari65b83602020-09-01 00:24:56 -050018#include <cstdarg>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053019#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053020#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053021#include <fstream>
Alpana Kumari2f793042020-08-18 05:51:03 -050022#include <gpiod.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053023#include <iostream>
24#include <iterator>
25#include <nlohmann/json.hpp>
Andrew Geissler280197e2020-12-08 20:51:49 -060026#include <phosphor-logging/log.hpp>
alpana077ce68722021-07-25 13:23:59 -050027#include <regex>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053028
29using namespace std;
30using namespace openpower::vpd;
31using namespace CLI;
32using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053033using namespace openpower::vpd::constants;
34namespace fs = filesystem;
35using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050036using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050037using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050038using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050039using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050040using namespace openpower::vpd::exceptions;
Andrew Geissler280197e2020-12-08 20:51:49 -060041using namespace phosphor::logging;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053042
Sunny Srivastava3c244142022-01-11 08:47:04 -060043// Map to hold record, kwd pair which can be re-stored at standby.
44// The list of keywords for VSYS record is as per the S0 system. Should
45// be updated for another type of systems
46static const std::unordered_map<std::string, std::vector<std::string>>
Sunny Srivastava01e6c632022-02-28 03:38:46 -060047 svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN"}},
Sunny Srivastava3c244142022-01-11 08:47:04 -060048 {"VCEN", {"FC", "SE"}},
49 {"LXR0", {"LX"}}};
50
Santosh Puranik88edeb62020-03-02 12:00:09 +053051/**
Santosh Puranik85893752020-11-10 21:31:43 +053052 * @brief Returns the power state for chassis0
53 */
54static auto getPowerState()
55{
56 // TODO: How do we handle multiple chassis?
57 string powerState{};
58 auto bus = sdbusplus::bus::new_default();
59 auto properties =
60 bus.new_method_call("xyz.openbmc_project.State.Chassis",
61 "/xyz/openbmc_project/state/chassis0",
62 "org.freedesktop.DBus.Properties", "Get");
63 properties.append("xyz.openbmc_project.State.Chassis");
64 properties.append("CurrentPowerState");
65 auto result = bus.call(properties);
66 if (!result.is_method_error())
67 {
68 variant<string> val;
69 result.read(val);
70 if (auto pVal = get_if<string>(&val))
71 {
72 powerState = *pVal;
73 }
74 }
75 cout << "Power state is: " << powerState << endl;
76 return powerState;
77}
78
79/**
Santosh Puranike9c57532022-03-15 16:51:51 +053080 * @brief Returns the BMC state
81 */
82static auto getBMCState()
83{
84 std::string bmcState;
85 try
86 {
87 auto bus = sdbusplus::bus::new_default();
88 auto properties = bus.new_method_call(
89 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
90 "org.freedesktop.DBus.Properties", "Get");
91 properties.append("xyz.openbmc_project.State.BMC");
92 properties.append("CurrentBMCState");
93 auto result = bus.call(properties);
94 std::variant<std::string> val;
95 result.read(val);
96 if (auto pVal = std::get_if<std::string>(&val))
97 {
98 bmcState = *pVal;
99 }
100 }
101 catch (const sdbusplus::exception::SdBusError& e)
102 {
103 // Ignore any error
104 std::cerr << "Failed to get BMC state: " << e.what() << "\n";
105 }
106 return bmcState;
107}
108
109/**
110 * @brief Check if the FRU is in the cache
111 *
112 * Checks if the FRU associated with the supplied D-Bus object path is already
113 * on D-Bus. This can be used to test if a VPD collection is required for this
114 * FRU. It uses the "xyz.openbmc_project.Inventory.Item, Present" property to
115 * determine the presence of a FRU in the cache.
116 *
117 * @param objectPath - The D-Bus object path without the PIM prefix.
118 * @return true if the object exists on D-Bus, false otherwise.
119 */
120static auto isFruInVpdCache(const std::string& objectPath)
121{
122 try
123 {
124 auto bus = sdbusplus::bus::new_default();
125 auto invPath = std::string{pimPath} + objectPath;
126 auto props = bus.new_method_call(
127 "xyz.openbmc_project.Inventory.Manager", invPath.c_str(),
128 "org.freedesktop.DBus.Properties", "Get");
129 props.append("xyz.openbmc_project.Inventory.Item");
130 props.append("Present");
131 auto result = bus.call(props);
132 std::variant<bool> present;
133 result.read(present);
134 if (auto pVal = std::get_if<bool>(&present))
135 {
136 return *pVal;
137 }
138 return false;
139 }
140 catch (const sdbusplus::exception::SdBusError& e)
141 {
142 std::cout << "FRU: " << objectPath << " not in D-Bus\n";
143 // Assume not present in case of an error
144 return false;
145 }
146}
147
148/**
149 * @brief Check if VPD recollection is needed for the given EEPROM
150 *
151 * Not all FRUs can be swapped at BMC ready state. This function does the
152 * following:
153 * -- Check if the FRU is marked as "pluggableAtStandby" OR
154 * "concurrentlyMaintainable". If so, return true.
155 * -- Check if we are at BMC NotReady state. If we are, then return true.
156 * -- Else check if the FRU is not present in the VPD cache (to cover for VPD
157 * force collection). If not found in the cache, return true.
158 * -- Else return false.
159 *
160 * @param js - JSON Object.
161 * @param filePath - The EEPROM file.
162 * @return true if collection should be attempted, false otherwise.
163 */
164static auto needsRecollection(const nlohmann::json& js, const string& filePath)
165{
166 if (js["frus"][filePath].at(0).value("pluggableAtStandby", false) ||
167 js["frus"][filePath].at(0).value("concurrentlyMaintainable", false))
168 {
169 return true;
170 }
171 if (getBMCState() == "xyz.openbmc_project.State.BMC.BMCState.NotReady")
172 {
173 return true;
174 }
175 if (!isFruInVpdCache(js["frus"][filePath].at(0).value("inventoryPath", "")))
176 {
177 return true;
178 }
179 return false;
180}
181
182/**
Santosh Puranik88edeb62020-03-02 12:00:09 +0530183 * @brief Expands location codes
184 */
185static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
186 bool isSystemVpd)
187{
188 auto expanded{unexpanded};
189 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
190 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
191 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
192 size_t idx = expanded.find("fcs");
193 try
194 {
195 if (idx != string::npos)
196 {
197 string fc{};
198 string se{};
199 if (isSystemVpd)
200 {
201 const auto& fcData = vpdMap.at("VCEN").at("FC");
202 const auto& seData = vpdMap.at("VCEN").at("SE");
203 fc = string(fcData.data(), fcData.size());
204 se = string(seData.data(), seData.size());
205 }
206 else
207 {
208 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
209 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
210 }
211
Alpana Kumari81671f62021-02-10 02:21:59 -0600212 // TODO: See if ND0 can be placed in the JSON
213 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530214 }
215 else
216 {
217 idx = expanded.find("mts");
218 if (idx != string::npos)
219 {
220 string mt{};
221 string se{};
222 if (isSystemVpd)
223 {
224 const auto& mtData = vpdMap.at("VSYS").at("TM");
225 const auto& seData = vpdMap.at("VSYS").at("SE");
226 mt = string(mtData.data(), mtData.size());
227 se = string(seData.data(), seData.size());
228 }
229 else
230 {
231 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
232 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
233 }
234
235 replace(mt.begin(), mt.end(), '-', '.');
236 expanded.replace(idx, 3, mt + "." + se);
237 }
238 }
239 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500240 catch (const exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530241 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500242 cerr << "Failed to expand location code with exception: " << e.what()
243 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530244 }
245 return expanded;
246}
Alpana Kumari2f793042020-08-18 05:51:03 -0500247
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530248/**
249 * @brief Populate FRU specific interfaces.
250 *
251 * This is a common method which handles both
252 * ipz and keyword specific interfaces thus,
253 * reducing the code redundancy.
254 * @param[in] map - Reference to the innermost keyword-value map.
255 * @param[in] preIntrStr - Reference to the interface string.
256 * @param[out] interfaces - Reference to interface map.
257 */
258template <typename T>
259static void populateFruSpecificInterfaces(const T& map,
260 const string& preIntrStr,
261 inventory::InterfaceMap& interfaces)
262{
263 inventory::PropertyMap prop;
264
265 for (const auto& kwVal : map)
266 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530267 auto kw = kwVal.first;
268
269 if (kw[0] == '#')
270 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500271 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530272 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500273 else if (isdigit(kw[0]))
274 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500275 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500276 }
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000277 if constexpr (is_same<T, KeywordVpdMap>::value)
278 {
279 if (get_if<Binary>(&kwVal.second))
280 {
281 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
282 get_if<Binary>(&kwVal.second)->end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000283 prop.emplace(move(kw), move(vec));
284 }
285 else
286 {
287 if (kw == "MemorySizeInKB")
288 {
289 inventory::PropertyMap memProp;
290 auto memVal = get_if<size_t>(&kwVal.second);
291 if (memVal)
292 {
293 memProp.emplace(move(kw),
294 ((*memVal) * CONVERT_MB_TO_KB));
295 interfaces.emplace(
296 "xyz.openbmc_project.Inventory.Item.Dimm",
297 move(memProp));
298 }
299 else
300 {
301 cerr << "MemorySizeInKB value not found in vpd map\n";
302 }
303 }
304 }
305 }
306 else
307 {
308 Binary vec(kwVal.second.begin(), kwVal.second.end());
309 prop.emplace(move(kw), move(vec));
310 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530311 }
312
313 interfaces.emplace(preIntrStr, move(prop));
314}
315
316/**
317 * @brief Populate Interfaces.
318 *
319 * This method populates common and extra interfaces to dbus.
320 * @param[in] js - json object
321 * @param[out] interfaces - Reference to interface map
322 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530323 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530324 */
325template <typename T>
326static void populateInterfaces(const nlohmann::json& js,
327 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530328 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530329{
330 for (const auto& ifs : js.items())
331 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530332 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530333 inventory::PropertyMap props;
334
335 for (const auto& itr : ifs.value().items())
336 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530337 const string& busProp = itr.key();
338
Alpana Kumari31970de2020-02-17 06:49:57 -0600339 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530340 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530341 props.emplace(busProp, itr.value().get<bool>());
342 }
343 else if (itr.value().is_string())
344 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600345 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530346 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600347 std::string prop;
348 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530349 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000350 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600351 prop = expandLocationCode(itr.value().get<string>(),
352 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530353 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600354 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530355 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600356 // Send empty Parsed object to expandLocationCode api.
357 prop = expandLocationCode(itr.value().get<string>(),
358 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530359 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600360 props.emplace(busProp, prop);
361 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
362 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530363 }
364 else
365 {
366 props.emplace(busProp, itr.value().get<string>());
367 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530368 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530369 else if (itr.value().is_array())
370 {
371 try
372 {
373 props.emplace(busProp, itr.value().get<Binary>());
374 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500375 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530376 {
377 std::cerr << "Type exception: " << e.what() << "\n";
378 // Ignore any type errors
379 }
380 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600381 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530382 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600383 const string& rec = itr.value().value("recordName", "");
384 const string& kw = itr.value().value("keywordName", "");
385 const string& encoding = itr.value().value("encoding", "");
386
Alpana Kumari58e22142020-05-05 00:22:12 -0500387 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530388 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530389 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
390 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600391 {
392 auto encoded =
393 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530394 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600395 }
396 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500397 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600398 {
399 if (!kw.empty() && vpdMap.count(kw))
400 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000401 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
402 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
403
404 if (kwValue)
405 {
406 auto prop =
407 string((*kwValue).begin(), (*kwValue).end());
408
409 auto encoded = encodeKeyword(prop, encoding);
410
411 props.emplace(busProp, encoded);
412 }
413 else if (uintValue)
414 {
415 props.emplace(busProp, *uintValue);
416 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600417 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530418 }
419 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500420 else if (itr.value().is_number())
421 {
422 // For now assume the value is a size_t. In the future it would
423 // be nice to come up with a way to get the type from the JSON.
424 props.emplace(busProp, itr.value().get<size_t>());
425 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530426 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600427 insertOrMerge(interfaces, inf, move(props));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530428 }
429}
430
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600431/*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
432 * Currently do reset only for DIMM VPD.*/
433static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
434 ifstream& vpdFile)
435{
436 for (const auto& item : js["frus"][file])
437 {
438 if (item.find("extraInterfaces") != item.end())
439 {
440 if (item["extraInterfaces"].find(
441 "xyz.openbmc_project.Inventory.Item.Dimm") !=
442 item["extraInterfaces"].end())
443 {
444 // moves the EEPROM pointer to 2048 'th byte.
445 vpdFile.seekg(2047, std::ios::beg);
446 // Read that byte and discard - to affirm the move
447 // operation.
448 char ch;
449 vpdFile.read(&ch, sizeof(ch));
450 }
451 return;
452 }
453 }
454}
455
alpana075cb3b1f2021-12-16 11:19:36 -0600456/**
457 * @brief This API checks if this FRU is pcie_devices. If yes then it further
458 * checks whether it is PASS1 planar.
459 */
460static bool isThisPcieOnPass1planar(const nlohmann::json& js,
461 const string& file)
462{
463 auto isThisPCIeDev = false;
464 auto isPASS1 = false;
465
466 // Check if it is a PCIE device
467 if (js["frus"].find(file) != js["frus"].end())
468 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530469 if ((js["frus"][file].at(0).find("extraInterfaces") !=
470 js["frus"][file].at(0).end()))
alpana075cb3b1f2021-12-16 11:19:36 -0600471 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530472 if (js["frus"][file].at(0)["extraInterfaces"].find(
alpana075cb3b1f2021-12-16 11:19:36 -0600473 "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
Santosh Puranikc03f3902022-04-14 10:58:26 +0530474 js["frus"][file].at(0)["extraInterfaces"].end())
alpana075cb3b1f2021-12-16 11:19:36 -0600475 {
476 isThisPCIeDev = true;
477 }
478 }
479 }
480
481 if (isThisPCIeDev)
482 {
483 // Collect SystemType to know if it is PASS1 planar.
484 auto bus = sdbusplus::bus::new_default();
485 auto properties = bus.new_method_call(
486 INVENTORY_MANAGER_SERVICE,
487 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
488 "org.freedesktop.DBus.Properties", "Get");
489 properties.append("com.ibm.ipzvpd.VINI");
490 properties.append("HW");
491 auto result = bus.call(properties);
492
493 inventory::Value val;
494 result.read(val);
495 if (auto pVal = get_if<Binary>(&val))
496 {
497 auto hwVersion = *pVal;
498 if (hwVersion[1] < 2)
499 isPASS1 = true;
500 }
501 }
502
503 return (isThisPCIeDev && isPASS1);
504}
505
Alpana Kumari2f793042020-08-18 05:51:03 -0500506static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500507{
508 uint32_t offset = 0;
509 // check if offset present?
510 for (const auto& item : js["frus"][file])
511 {
512 if (item.find("offset") != item.end())
513 {
514 offset = item["offset"];
515 }
516 }
517
518 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600519 auto maxVPDSize = std::min(std::filesystem::file_size(file),
520 static_cast<uintmax_t>(65504));
521
Alpana Kumari58e22142020-05-05 00:22:12 -0500522 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600523 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500524 ifstream vpdFile;
525 vpdFile.open(file, ios::binary);
526
527 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600528 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500529 vpdVector.resize(vpdFile.gcount());
530
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600531 resetEEPROMPointer(js, file, vpdFile);
532
Alpana Kumari58e22142020-05-05 00:22:12 -0500533 return vpdVector;
534}
535
Alpana Kumari735dee92022-03-25 01:24:40 -0500536/** Performs any pre-action needed to get the FRU setup for collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500537 *
538 * @param[in] json - json object
539 * @param[in] file - eeprom file path
540 */
541static void preAction(const nlohmann::json& json, const string& file)
542{
Alpana Kumari735dee92022-03-25 01:24:40 -0500543 if ((json["frus"][file].at(0)).find("preAction") ==
Alpana Kumari2f793042020-08-18 05:51:03 -0500544 json["frus"][file].at(0).end())
545 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500546 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500547 }
548
Alpana Kumari735dee92022-03-25 01:24:40 -0500549 if (executePreAction(json, file))
Alpana Kumari2f793042020-08-18 05:51:03 -0500550 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500551 if (json["frus"][file].at(0).find("devAddress") !=
552 json["frus"][file].at(0).end())
Alpana Kumari2f793042020-08-18 05:51:03 -0500553 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500554 // Now bind the device
555 string bind = json["frus"][file].at(0).value("devAddress", "");
556 cout << "Binding device " << bind << endl;
557 string bindCmd = string("echo \"") + bind +
558 string("\" > /sys/bus/i2c/drivers/at24/bind");
559 cout << bindCmd << endl;
560 executeCmd(bindCmd);
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600561
Alpana Kumari40d1c192022-03-09 21:16:02 -0600562 // Check if device showed up (test for file)
563 if (!fs::exists(file))
564 {
565 cerr << "EEPROM " << file
566 << " does not exist. Take failure action" << endl;
567 // If not, then take failure postAction
568 executePostFailAction(json, file);
569 }
570 }
571 else
Alpana Kumari735dee92022-03-25 01:24:40 -0500572 {
Alpana Kumari40d1c192022-03-09 21:16:02 -0600573 // missing required informations
574 cerr
575 << "VPD inventory JSON missing basic informations of preAction "
576 "for this FRU : ["
577 << file << "]. Executing executePostFailAction." << endl;
578
579 // Take failure postAction
Alpana Kumari735dee92022-03-25 01:24:40 -0500580 executePostFailAction(json, file);
Alpana Kumari40d1c192022-03-09 21:16:02 -0600581 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500582 }
583 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500584}
585
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530586/**
Santosh Puranikf3e69682022-03-31 17:52:38 +0530587 * @brief Fills the Decorator.AssetTag property into the interfaces map
588 *
589 * This function should only be called in cases where we did not find a JSON
590 * symlink. A missing symlink in /var/lib will be considered as a factory reset
591 * and this function will be used to default the AssetTag property.
592 *
593 * @param interfaces A possibly pre-populated map of inetrfaces to properties.
594 * @param vpdMap A VPD map of the system VPD data.
595 */
596static void fillAssetTag(inventory::InterfaceMap& interfaces,
597 const Parsed& vpdMap)
598{
599 // Read the system serial number and MTM
600 // Default asset tag is Server-MTM-System Serial
601 inventory::Interface assetIntf{
602 "xyz.openbmc_project.Inventory.Decorator.AssetTag"};
603 inventory::PropertyMap assetTagProps;
604 std::string defaultAssetTag =
605 std::string{"Server-"} + getKwVal(vpdMap, "VSYS", "TM") +
606 std::string{"-"} + getKwVal(vpdMap, "VSYS", "SE");
607 assetTagProps.emplace("AssetTag", defaultAssetTag);
608 insertOrMerge(interfaces, assetIntf, std::move(assetTagProps));
609}
610
611/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530612 * @brief Set certain one time properties in the inventory
613 * Use this function to insert the Functional and Enabled properties into the
614 * inventory map. This function first checks if the object in question already
615 * has these properties hosted on D-Bus, if the property is already there, it is
616 * not modified, hence the name "one time". If the property is not already
617 * present, it will be added to the map with a suitable default value (true for
618 * Functional and false for Enabled)
619 *
620 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
621 * @param[inout] interfaces - Reference to a map of inventory interfaces to
622 * which the properties will be attached.
623 */
624static void setOneTimeProperties(const std::string& object,
625 inventory::InterfaceMap& interfaces)
626{
627 auto bus = sdbusplus::bus::new_default();
628 auto objectPath = INVENTORY_PATH + object;
629 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
630 objectPath.c_str(),
631 "org.freedesktop.DBus.Properties", "Get");
632 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
633 prop.append("Functional");
634 try
635 {
636 auto result = bus.call(prop);
637 }
638 catch (const sdbusplus::exception::SdBusError& e)
639 {
640 // Treat as property unavailable
641 inventory::PropertyMap prop;
642 prop.emplace("Functional", true);
643 interfaces.emplace(
644 "xyz.openbmc_project.State.Decorator.OperationalStatus",
645 move(prop));
646 }
647 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
648 objectPath.c_str(),
649 "org.freedesktop.DBus.Properties", "Get");
650 prop.append("xyz.openbmc_project.Object.Enable");
651 prop.append("Enabled");
652 try
653 {
654 auto result = bus.call(prop);
655 }
656 catch (const sdbusplus::exception::SdBusError& e)
657 {
658 // Treat as property unavailable
659 inventory::PropertyMap prop;
660 prop.emplace("Enabled", false);
661 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
662 }
663}
664
665/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530666 * @brief Prime the Inventory
667 * Prime the inventory by populating only the location code,
668 * type interface and the inventory object for the frus
669 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530670 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530671 * @param[in] jsObject - Reference to vpd inventory json object
672 * @param[in] vpdMap - Reference to the parsed vpd map
673 *
674 * @returns Map of items in extraInterface.
675 */
676template <typename T>
677inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
678 const T& vpdMap)
679{
680 inventory::ObjectMap objects;
681
682 for (auto& itemFRUS : jsObject["frus"].items())
683 {
684 for (auto& itemEEPROM : itemFRUS.value())
685 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600686 // Take pre actions if needed
687 if (itemEEPROM.find("preAction") != itemEEPROM.end())
688 {
689 preAction(jsObject, itemFRUS.key());
690 }
691
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530692 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530693 inventory::Object object(itemEEPROM.at("inventoryPath"));
694
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530695 if ((itemFRUS.key() != systemVpdFilePath) &&
696 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530697 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600698 inventory::PropertyMap presProp;
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500699
700 // Do not populate Present property for frus whose
701 // synthesized=true. synthesized=true says the fru is owned by
702 // some other component and not by vpd.
703 if (!itemEEPROM.value("synthesized", false))
704 {
705 presProp.emplace("Present", false);
706 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
707 presProp);
708 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530709 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530710 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
711 {
712 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
713 {
714 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000715 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530716 {
717 if constexpr (std::is_same<T, Parsed>::value)
718 {
719 for (auto& lC : eI.value().items())
720 {
721 auto propVal = expandLocationCode(
722 lC.value().get<string>(), vpdMap, true);
723
724 props.emplace(move(lC.key()),
725 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530726 interfaces.emplace(XYZ_LOCATION_CODE_INF,
727 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530728 interfaces.emplace(move(eI.key()),
729 move(props));
730 }
731 }
732 }
733 else if (eI.key().find("Inventory.Item.") !=
734 string::npos)
735 {
736 interfaces.emplace(move(eI.key()), move(props));
737 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530738 else if (eI.key() ==
739 "xyz.openbmc_project.Inventory.Item")
740 {
741 for (auto& val : eI.value().items())
742 {
743 if (val.key() == "PrettyName")
744 {
745 presProp.emplace(val.key(),
746 val.value().get<string>());
747 }
748 }
749 // Use insert_or_assign here as we may already have
750 // inserted the present property only earlier in
751 // this function under this same interface.
752 interfaces.insert_or_assign(eI.key(),
753 move(presProp));
754 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530755 }
756 }
757 objects.emplace(move(object), move(interfaces));
758 }
759 }
760 }
761 return objects;
762}
763
Alpana Kumari65b83602020-09-01 00:24:56 -0500764/**
765 * @brief This API executes command to set environment variable
766 * And then reboot the system
767 * @param[in] key -env key to set new value
768 * @param[in] value -value to set.
769 */
770void setEnvAndReboot(const string& key, const string& value)
771{
772 // set env and reboot and break.
773 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600774 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500775 // make dbus call to reboot
776 auto bus = sdbusplus::bus::new_default_system();
777 auto method = bus.new_method_call(
778 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
779 "org.freedesktop.systemd1.Manager", "Reboot");
780 bus.call_noreply(method);
781}
782
783/*
784 * @brief This API checks for env var fitconfig.
785 * If not initialised OR updated as per the current system type,
786 * update this env var and reboot the system.
787 *
788 * @param[in] systemType IM kwd in vpd tells about which system type it is.
789 * */
790void setDevTreeEnv(const string& systemType)
791{
Alpana Kumari37e72702021-11-18 11:18:04 -0600792 // Init with default dtb
793 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530794 static const deviceTreeMap deviceTreeSystemTypeMap = {
795 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
796 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
797 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
798 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
799 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
800 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500801
802 if (deviceTreeSystemTypeMap.find(systemType) !=
803 deviceTreeSystemTypeMap.end())
804 {
805 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
806 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600807 else
808 {
809 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600810 string err = "This System type not found/supported in dtb table " +
811 systemType +
812 ".Please check the HW and IM keywords in the system "
813 "VPD.Breaking...";
814
815 // map to hold additional data in case of logging pel
816 PelAdditionalData additionalData{};
817 additionalData.emplace("DESCRIPTION", err);
818 createPEL(additionalData, PelSeverity::WARNING,
819 errIntfForInvalidSystemType);
820 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600821 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500822
823 string readVarValue;
824 bool envVarFound = false;
825
826 vector<string> output = executeCmd("/sbin/fw_printenv");
827 for (const auto& entry : output)
828 {
829 size_t pos = entry.find("=");
830 string key = entry.substr(0, pos);
831 if (key != "fitconfig")
832 {
833 continue;
834 }
835
836 envVarFound = true;
837 if (pos + 1 < entry.size())
838 {
839 readVarValue = entry.substr(pos + 1);
840 if (readVarValue.find(newDeviceTree) != string::npos)
841 {
842 // fitconfig is Updated. No action needed
843 break;
844 }
845 }
846 // set env and reboot and break.
847 setEnvAndReboot(key, newDeviceTree);
848 exit(0);
849 }
850
851 // check If env var Not found
852 if (!envVarFound)
853 {
854 setEnvAndReboot("fitconfig", newDeviceTree);
855 }
856}
857
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530858/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500859 * @brief API to call VPD manager to write VPD to EEPROM.
860 * @param[in] Object path.
861 * @param[in] record to be updated.
862 * @param[in] keyword to be updated.
863 * @param[in] keyword data to be updated
864 */
865void updateHardware(const string& objectName, const string& recName,
866 const string& kwdName, const Binary& data)
867{
868 try
869 {
870 auto bus = sdbusplus::bus::new_default();
871 auto properties =
872 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
873 properties.append(
874 static_cast<sdbusplus::message::object_path>(objectName));
875 properties.append(recName);
876 properties.append(kwdName);
877 properties.append(data);
878 bus.call(properties);
879 }
Patrick Williams8be43342021-09-02 09:33:36 -0500880 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500881 {
882 std::string what =
883 "VPDManager WriteKeyword api failed for inventory path " +
884 objectName;
885 what += " record " + recName;
886 what += " keyword " + kwdName;
887 what += " with bus error = " + std::string(e.what());
888
889 // map to hold additional data in case of logging pel
890 PelAdditionalData additionalData{};
891 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
892 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500893 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500894 }
895}
896
897/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600898 * @brief An api to get list of blank system VPD properties.
899 * @param[in] vpdMap - IPZ vpd map.
900 * @param[in] objectPath - Object path for the FRU.
901 * @param[out] blankPropertyList - Properties which are blank in System VPD and
902 * needs to be updated as standby.
903 */
904void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
905 std::vector<RestoredEeproms>& blankPropertyList)
906{
907 for (const auto& systemRecKwdPair : svpdKwdMap)
908 {
909 auto it = vpdMap.find(systemRecKwdPair.first);
910
911 // check if record is found in map we got by parser
912 if (it != vpdMap.end())
913 {
914 const auto& kwdListForRecord = systemRecKwdPair.second;
915 for (const auto& keyword : kwdListForRecord)
916 {
917 DbusPropertyMap& kwdValMap = it->second;
918 auto iterator = kwdValMap.find(keyword);
919
920 if (iterator != kwdValMap.end())
921 {
922 string& kwdValue = iterator->second;
923
924 // check bus data
925 const string& recordName = systemRecKwdPair.first;
926 const string& busValue = readBusProperty(
927 objectPath, ipzVpdInf + recordName, keyword);
928
929 if (busValue.find_first_not_of(' ') != string::npos)
930 {
931 if (kwdValue.find_first_not_of(' ') == string::npos)
932 {
933 // implies data is blank on EEPROM but not on cache.
934 // So EEPROM vpd update is required.
935 Binary busData(busValue.begin(), busValue.end());
936
937 blankPropertyList.push_back(std::make_tuple(
938 objectPath, recordName, keyword, busData));
939 }
940 }
941 }
942 }
943 }
944 }
945}
946
947/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500948 * @brief API to check if we need to restore system VPD
949 * This functionality is only applicable for IPZ VPD data.
950 * @param[in] vpdMap - IPZ vpd map
951 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500952 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600953void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500954{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500955 for (const auto& systemRecKwdPair : svpdKwdMap)
956 {
957 auto it = vpdMap.find(systemRecKwdPair.first);
958
959 // check if record is found in map we got by parser
960 if (it != vpdMap.end())
961 {
962 const auto& kwdListForRecord = systemRecKwdPair.second;
963 for (const auto& keyword : kwdListForRecord)
964 {
965 DbusPropertyMap& kwdValMap = it->second;
966 auto iterator = kwdValMap.find(keyword);
967
968 if (iterator != kwdValMap.end())
969 {
970 string& kwdValue = iterator->second;
971
972 // check bus data
973 const string& recordName = systemRecKwdPair.first;
974 const string& busValue = readBusProperty(
975 objectPath, ipzVpdInf + recordName, keyword);
976
977 if (busValue.find_first_not_of(' ') != string::npos)
978 {
979 if (kwdValue.find_first_not_of(' ') != string::npos)
980 {
981 // both the data are present, check for mismatch
982 if (busValue != kwdValue)
983 {
984 string errMsg = "VPD data mismatch on cache "
985 "and hardware for record: ";
986 errMsg += (*it).first;
987 errMsg += " and keyword: ";
988 errMsg += keyword;
989
990 // data mismatch
991 PelAdditionalData additionalData;
992 additionalData.emplace("CALLOUT_INVENTORY_PATH",
993 objectPath);
994
995 additionalData.emplace("DESCRIPTION", errMsg);
996
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500997 createPEL(additionalData, PelSeverity::WARNING,
998 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500999 }
1000 }
1001 else
1002 {
1003 // implies hardware data is blank
1004 // update the map
1005 Binary busData(busValue.begin(), busValue.end());
1006
Sunny Srivastava90a63b92021-05-26 06:30:24 -05001007 // update the map as well, so that cache data is not
1008 // updated as blank while populating VPD map on Dbus
1009 // in populateDBus Api
1010 kwdValue = busValue;
1011 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001012 }
1013 else if (kwdValue.find_first_not_of(' ') == string::npos)
1014 {
1015 string errMsg = "VPD is blank on both cache and "
1016 "hardware for record: ";
1017 errMsg += (*it).first;
1018 errMsg += " and keyword: ";
1019 errMsg += keyword;
1020 errMsg += ". SSR need to update hardware VPD.";
1021
1022 // both the data are blanks, log PEL
1023 PelAdditionalData additionalData;
1024 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1025 objectPath);
1026
1027 additionalData.emplace("DESCRIPTION", errMsg);
1028
1029 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001030 createPEL(additionalData, PelSeverity::ERROR,
1031 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001032 continue;
1033 }
1034 }
1035 }
1036 }
1037 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001038}
1039
1040/**
alpana077ce68722021-07-25 13:23:59 -05001041 * @brief This checks for is this FRU a processor
1042 * And if yes, then checks for is this primary
1043 *
1044 * @param[in] js- vpd json to get the information about this FRU
1045 * @param[in] filePath- FRU vpd
1046 *
1047 * @return true/false
1048 */
1049bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
1050{
1051 bool isProcessor = false;
1052 bool isPrimary = false;
1053
1054 for (const auto& item : js["frus"][filePath])
1055 {
1056 if (item.find("extraInterfaces") != item.end())
1057 {
1058 for (const auto& eI : item["extraInterfaces"].items())
1059 {
1060 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
1061 {
1062 isProcessor = true;
1063 }
1064 }
1065 }
1066
1067 if (isProcessor)
1068 {
1069 string cpuType = item.value("cpuType", "");
1070 if (cpuType == "primary")
1071 {
1072 isPrimary = true;
1073 }
1074 }
1075 }
1076
1077 return (isProcessor && isPrimary);
1078}
1079
1080/**
1081 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1082 * driver
1083 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1084 */
1085void doEnableAllDimms(nlohmann::json& js)
1086{
1087 // iterate over each fru
1088 for (const auto& eachFru : js["frus"].items())
1089 {
1090 // skip the driver binding if eeprom already exists
1091 if (fs::exists(eachFru.key()))
1092 {
1093 continue;
1094 }
1095
1096 for (const auto& eachInventory : eachFru.value())
1097 {
1098 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1099 {
1100 for (const auto& eI : eachInventory["extraInterfaces"].items())
1101 {
1102 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1103 {
1104 string dimmVpd = eachFru.key();
1105 // fetch it from
1106 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1107
1108 regex matchPatern("([0-9]+-[0-9]{4})");
1109 smatch matchFound;
1110 if (regex_search(dimmVpd, matchFound, matchPatern))
1111 {
1112 vector<string> i2cReg;
1113 boost::split(i2cReg, matchFound.str(0),
1114 boost::is_any_of("-"));
1115
1116 // remove 0s from begining
1117 const regex pattern("^0+(?!$)");
1118 for (auto& i : i2cReg)
1119 {
1120 i = regex_replace(i, pattern, "");
1121 }
1122
1123 if (i2cReg.size() == 2)
1124 {
1125 // echo 24c32 0x50 >
1126 // /sys/bus/i2c/devices/i2c-16/new_device
1127 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1128 " > /sys/bus/i2c/devices/i2c-" +
1129 i2cReg[0] + "/new_device";
1130
1131 executeCmd(cmnd);
1132 }
1133 }
1134 }
1135 }
1136 }
1137 }
1138 }
1139}
1140
1141/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001142 * @brief Check if the given CPU is an IO only chip.
1143 * The CPU is termed as IO, whose all of the cores are bad and can never be
1144 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1145 * etc., The CPU whose every cores are bad, can be identified from the CP00
1146 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1147 * has 4 cores grouped together by sharing its cache memory.)
1148 * @param [in] pgKeyword - PG Keyword of CPU.
1149 * @return true if the given cpu is an IO, false otherwise.
1150 */
1151static bool isCPUIOGoodOnly(const string& pgKeyword)
1152{
1153 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1154 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1155 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1156 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1157 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1158 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1159 // IO.
1160 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1161 {
1162 return true;
1163 }
1164
1165 // The CPU is not an IO
1166 return false;
1167}
1168
1169/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301170 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301171 * This method invokes all the populateInterface functions
1172 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301173 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1174 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301175 * @param[in] js - Inventory json object
1176 * @param[in] filePath - Path of the vpd file
1177 * @param[in] preIntrStr - Interface string
1178 */
1179template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001180static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301181{
1182 inventory::InterfaceMap interfaces;
1183 inventory::ObjectMap objects;
1184 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001185 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301186
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301187 bool isSystemVpd = (filePath == systemVpdFilePath);
1188 if constexpr (is_same<T, Parsed>::value)
1189 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001190 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1191 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1192 ::toupper);
1193
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301194 if (isSystemVpd)
1195 {
1196 std::vector<std::string> interfaces = {motherBoardInterface};
1197 // call mapper to check for object path creation
1198 MapperResponse subTree =
1199 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1200 string mboardPath =
1201 js["frus"][filePath].at(0).value("inventoryPath", "");
1202
1203 // Attempt system VPD restore if we have a motherboard
1204 // object in the inventory.
1205 if ((subTree.size() != 0) &&
1206 (subTree.find(pimPath + mboardPath) != subTree.end()))
1207 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001208 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301209 }
1210 else
1211 {
1212 log<level::ERR>("No object path found");
1213 }
1214 }
alpana077ce68722021-07-25 13:23:59 -05001215 else
1216 {
1217 // check if it is processor vpd.
1218 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1219
1220 if (isPrimaryCpu)
1221 {
1222 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1223
1224 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1225
1226 if (chipVersion >= 2)
1227 {
1228 doEnableAllDimms(js);
1229 }
1230 }
1231 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301232 }
1233
Santosh Puranikf3e69682022-03-31 17:52:38 +05301234 auto processFactoryReset = false;
1235
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001236 if (isSystemVpd)
1237 {
1238 string systemJsonName{};
1239 if constexpr (is_same<T, Parsed>::value)
1240 {
1241 // pick the right system json
1242 systemJsonName = getSystemsJson(vpdMap);
1243 }
1244
1245 fs::path target = systemJsonName;
1246 fs::path link = INVENTORY_JSON_SYM_LINK;
1247
Santosh Puranikf3e69682022-03-31 17:52:38 +05301248 // If the symlink does not exist, we treat that as a factory reset
1249 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1250
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001251 // Create the directory for hosting the symlink
1252 fs::create_directories(VPD_FILES_PATH);
1253 // unlink the symlink previously created (if any)
1254 remove(INVENTORY_JSON_SYM_LINK);
1255 // create a new symlink based on the system
1256 fs::create_symlink(target, link);
1257
1258 // Reloading the json
1259 ifstream inventoryJson(link);
1260 js = json::parse(inventoryJson);
1261 inventoryJson.close();
1262 }
1263
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301264 for (const auto& item : js["frus"][filePath])
1265 {
1266 const auto& objectPath = item["inventoryPath"];
1267 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001268
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001269 vector<string> ccinList;
1270 if (item.find("ccin") != item.end())
1271 {
1272 for (const auto& cc : item["ccin"])
1273 {
1274 string ccin = cc;
1275 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1276 ccinList.push_back(ccin);
1277 }
1278 }
1279
1280 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1281 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1282 ccinList.end()))
1283 {
1284 continue;
1285 }
1286
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001287 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301288 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001289
1290 // Populate one time properties for the system VPD and its sub-frus
1291 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301292 // For the remaining FRUs, this will get handled as a part of
1293 // priming the inventory.
1294 setOneTimeProperties(objectPath, interfaces);
1295 }
1296
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301297 // Populate the VPD keywords and the common interfaces only if we
1298 // are asked to inherit that data from the VPD, else only add the
1299 // extraInterfaces.
1300 if (item.value("inherit", true))
1301 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001302 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301303 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301304 // Each record in the VPD becomes an interface and all
1305 // keyword within the record are properties under that
1306 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301307 for (const auto& record : vpdMap)
1308 {
1309 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001310 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301311 }
1312 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001313 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301314 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001315 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301316 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301317 if (js.find("commonInterfaces") != js.end())
1318 {
1319 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1320 isSystemVpd);
1321 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301322 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001323 else
1324 {
1325 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001326 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001327 {
1328 if (item.find("copyRecords") != item.end())
1329 {
1330 for (const auto& record : item["copyRecords"])
1331 {
1332 const string& recordName = record;
1333 if (vpdMap.find(recordName) != vpdMap.end())
1334 {
1335 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001336 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001337 interfaces);
1338 }
1339 }
1340 }
1341 }
1342 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301343 // Populate interfaces and properties that are common to every FRU
1344 // and additional interface that might be defined on a per-FRU
1345 // basis.
1346 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301347 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301348 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1349 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001350 if constexpr (is_same<T, Parsed>::value)
1351 {
1352 if (item["extraInterfaces"].find(
1353 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1354 item["extraInterfaces"].end())
1355 {
1356 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1357 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001358 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001359 }
1360 }
1361 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301362 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001363
1364 // embedded property(true or false) says whether the subfru is embedded
1365 // into the parent fru (or) not. VPD sets Present property only for
1366 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1367 // or may not be physically present. Those non embedded frus will always
1368 // have Present=false irrespective of its physical presence or absence.
1369 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1370 // Present to true for such sub frus.
1371 // Eg: ethernet port is embedded into bmc card. So set Present to true
1372 // for such sub frus. Also donot populate present property for embedded
1373 // subfru which is synthesized. Currently there is no subfru which are
1374 // both embedded and synthesized. But still the case is handled here.
1375 if ((item.value("embedded", true)) &&
1376 (!item.value("synthesized", false)))
1377 {
1378 inventory::PropertyMap presProp;
1379 presProp.emplace("Present", true);
1380 insertOrMerge(interfaces, invItemIntf, move(presProp));
1381 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001382
Santosh Puranikf3e69682022-03-31 17:52:38 +05301383 if constexpr (is_same<T, Parsed>::value)
1384 {
1385 // Restore asset tag, if needed
1386 if (processFactoryReset && objectPath == "/system")
1387 {
1388 fillAssetTag(interfaces, vpdMap);
1389 }
1390 }
1391
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301392 objects.emplace(move(object), move(interfaces));
1393 }
1394
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301395 if (isSystemVpd)
1396 {
1397 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1398 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001399
Alpana Kumarif05effd2021-04-07 07:32:53 -05001400 // set the U-boot environment variable for device-tree
1401 if constexpr (is_same<T, Parsed>::value)
1402 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301403 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001404 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301405 }
1406
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301407 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001408 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301409}
1410
1411int main(int argc, char** argv)
1412{
1413 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001414 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001415 Binary vpdVector{};
1416 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001417 // map to hold additional data in case of logging pel
1418 PelAdditionalData additionalData{};
1419
1420 // this is needed to hold base fru inventory path in case there is ECC or
1421 // vpd exception while parsing the file
1422 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301423
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001424 // severity for PEL
1425 PelSeverity pelSeverity = PelSeverity::WARNING;
1426
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301427 try
1428 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301429 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1430 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301431
1432 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001433 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301434
1435 CLI11_PARSE(app, argc, argv);
1436
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001437 // PEL severity should be ERROR in case of any system VPD failure
1438 if (file == systemVpdFilePath)
1439 {
1440 pelSeverity = PelSeverity::ERROR;
1441 }
1442
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301443 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1444
1445 // If the symlink exists, it means it has been setup for us, switch the
1446 // path
1447 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1448 {
1449 jsonToParse = INVENTORY_JSON_SYM_LINK;
1450 }
1451
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301452 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301453 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001454 if (!inventoryJson)
1455 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001456 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001457 }
1458
1459 try
1460 {
1461 js = json::parse(inventoryJson);
1462 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001463 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001464 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001465 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001466 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301467
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301468 // Do we have the mandatory "frus" section?
1469 if (js.find("frus") == js.end())
1470 {
1471 throw(VpdJsonException("FRUs section not found in JSON",
1472 jsonToParse));
1473 }
1474
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301475 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1476 if (file.find("/ahb:apb") != string::npos)
1477 {
1478 // Translate udev path to a generic /sys/bus/.. file path.
1479 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301480
1481 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301482 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301483 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001484 // We need manager service active to process restoring of
1485 // system VPD on hardware. So in case any system restore is
1486 // required, update hardware in the second trigger of parser
1487 // code for system vpd file path.
1488
1489 std::vector<std::string> interfaces{motherBoardInterface};
1490
1491 // call mapper to check for object path creation
1492 MapperResponse subTree =
1493 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1494 string mboardPath =
1495 js["frus"][file].at(0).value("inventoryPath", "");
1496
1497 // Attempt system VPD restore if we have a motherboard
1498 // object in the inventory.
1499 if ((subTree.size() != 0) &&
1500 (subTree.find(pimPath + mboardPath) != subTree.end()))
1501 {
1502 vpdVector = getVpdDataInVector(js, file);
1503 ParserInterface* parser =
1504 ParserFactory::getParser(vpdVector);
1505 variant<KeywordVpdMap, Store> parseResult;
1506 parseResult = parser->parse();
1507
1508 if (auto pVal = get_if<Store>(&parseResult))
1509 {
1510 // map to hold all the keywords whose value is blank and
1511 // needs to be updated at standby.
1512 vector<RestoredEeproms> blankSystemVpdProperties{};
1513 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1514 blankSystemVpdProperties);
1515
1516 // if system VPD restore is required, update the
1517 // EEPROM
1518 for (const auto& item : blankSystemVpdProperties)
1519 {
1520 updateHardware(get<0>(item), get<1>(item),
1521 get<2>(item), get<3>(item));
1522 }
1523 }
1524 else
1525 {
1526 std::cout << "Not a valid format to restore system VPD"
1527 << std::endl;
1528 }
1529 // release the parser object
1530 ParserFactory::freeParser(parser);
1531 }
1532 else
1533 {
1534 log<level::ERR>("No object path found");
1535 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301536 return 0;
1537 }
1538 }
1539
1540 if (file.empty())
1541 {
1542 cerr << "The EEPROM path <" << file << "> is not valid.";
1543 return 0;
1544 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301545 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301546 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301547 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301548 }
1549
Alpana Kumari2f793042020-08-18 05:51:03 -05001550 if (!fs::exists(file))
1551 {
1552 cout << "Device path: " << file
1553 << " does not exist. Spurious udev event? Exiting." << endl;
1554 return 0;
1555 }
1556
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001557 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301558 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001559 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301560 // 1) The eeprom we are trying to read is that of the system VPD and the
1561 // JSON symlink is already setup (the symlink's existence tells us we
1562 // are not coming out of a factory reset)
1563 // 2) The JSON tells us that the FRU EEPROM cannot be
1564 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001565 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301566 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301567 {
1568 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1569 getPowerState())
1570 {
1571 cout << "This VPD cannot be read when power is ON" << endl;
1572 return 0;
1573 }
1574 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001575
Santosh Puranike9c57532022-03-15 16:51:51 +05301576 // Check if this VPD should be recollected at all
1577 if (!needsRecollection(js, file))
1578 {
1579 cout << "Skip VPD recollection for: " << file << endl;
1580 return 0;
1581 }
1582
Alpana Kumari2f793042020-08-18 05:51:03 -05001583 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301584 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001585 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001586 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001587 variant<KeywordVpdMap, Store> parseResult;
1588 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001589
Alpana Kumari2f793042020-08-18 05:51:03 -05001590 if (auto pVal = get_if<Store>(&parseResult))
1591 {
1592 populateDbus(pVal->getVpdMap(), js, file);
1593 }
1594 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1595 {
1596 populateDbus(*pVal, js, file);
1597 }
1598
1599 // release the parser object
1600 ParserFactory::freeParser(parser);
1601 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001602 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001603 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001604 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001605 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001606 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301607 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001608 catch (const VpdJsonException& ex)
1609 {
1610 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1611 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001612 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001613
1614 cerr << ex.what() << "\n";
1615 rc = -1;
1616 }
1617 catch (const VpdEccException& ex)
1618 {
1619 additionalData.emplace("DESCRIPTION", "ECC check failed");
1620 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1621 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001622 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001623 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001624 cerr << ex.what() << "\n";
1625 rc = -1;
1626 }
1627 catch (const VpdDataException& ex)
1628 {
alpana075cb3b1f2021-12-16 11:19:36 -06001629 if (isThisPcieOnPass1planar(js, file))
1630 {
1631 cout << "Pcie_device [" << file
1632 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1633 rc = 0;
1634 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301635 else if (!(isPresent(js, file).value_or(true)))
1636 {
1637 cout << "FRU at: " << file
1638 << " is not detected present. Ignore parser error.\n";
1639 rc = 0;
1640 }
alpana075cb3b1f2021-12-16 11:19:36 -06001641 else
1642 {
1643 string errorMsg =
1644 "VPD file is either empty or invalid. Parser failed for [";
1645 errorMsg += file;
1646 errorMsg += "], with error = " + std::string(ex.what());
1647
1648 additionalData.emplace("DESCRIPTION", errorMsg);
1649 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1650 INVENTORY_PATH + baseFruInventoryPath);
1651 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1652
1653 rc = -1;
1654 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001655 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001656 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301657 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001658 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301659 cerr << e.what() << "\n";
1660 rc = -1;
1661 }
1662
1663 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001664}