blob: ecbe110d3208f40a1154a933e88df1b64d04a57f [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);
561 }
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600562
Alpana Kumari735dee92022-03-25 01:24:40 -0500563 // Check if device showed up (test for file)
564 if (!fs::exists(file))
565 {
566 cout << "EEPROM " << file << " does not exist. Take failure action"
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600567 << endl;
Alpana Kumari735dee92022-03-25 01:24:40 -0500568 // If not, then take failure postAction
569 executePostFailAction(json, file);
Alpana Kumari2f793042020-08-18 05:51:03 -0500570 }
571 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500572}
573
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530574/**
Santosh Puranikf3e69682022-03-31 17:52:38 +0530575 * @brief Fills the Decorator.AssetTag property into the interfaces map
576 *
577 * This function should only be called in cases where we did not find a JSON
578 * symlink. A missing symlink in /var/lib will be considered as a factory reset
579 * and this function will be used to default the AssetTag property.
580 *
581 * @param interfaces A possibly pre-populated map of inetrfaces to properties.
582 * @param vpdMap A VPD map of the system VPD data.
583 */
584static void fillAssetTag(inventory::InterfaceMap& interfaces,
585 const Parsed& vpdMap)
586{
587 // Read the system serial number and MTM
588 // Default asset tag is Server-MTM-System Serial
589 inventory::Interface assetIntf{
590 "xyz.openbmc_project.Inventory.Decorator.AssetTag"};
591 inventory::PropertyMap assetTagProps;
592 std::string defaultAssetTag =
593 std::string{"Server-"} + getKwVal(vpdMap, "VSYS", "TM") +
594 std::string{"-"} + getKwVal(vpdMap, "VSYS", "SE");
595 assetTagProps.emplace("AssetTag", defaultAssetTag);
596 insertOrMerge(interfaces, assetIntf, std::move(assetTagProps));
597}
598
599/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530600 * @brief Set certain one time properties in the inventory
601 * Use this function to insert the Functional and Enabled properties into the
602 * inventory map. This function first checks if the object in question already
603 * has these properties hosted on D-Bus, if the property is already there, it is
604 * not modified, hence the name "one time". If the property is not already
605 * present, it will be added to the map with a suitable default value (true for
606 * Functional and false for Enabled)
607 *
608 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
609 * @param[inout] interfaces - Reference to a map of inventory interfaces to
610 * which the properties will be attached.
611 */
612static void setOneTimeProperties(const std::string& object,
613 inventory::InterfaceMap& interfaces)
614{
615 auto bus = sdbusplus::bus::new_default();
616 auto objectPath = INVENTORY_PATH + object;
617 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
618 objectPath.c_str(),
619 "org.freedesktop.DBus.Properties", "Get");
620 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
621 prop.append("Functional");
622 try
623 {
624 auto result = bus.call(prop);
625 }
626 catch (const sdbusplus::exception::SdBusError& e)
627 {
628 // Treat as property unavailable
629 inventory::PropertyMap prop;
630 prop.emplace("Functional", true);
631 interfaces.emplace(
632 "xyz.openbmc_project.State.Decorator.OperationalStatus",
633 move(prop));
634 }
635 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
636 objectPath.c_str(),
637 "org.freedesktop.DBus.Properties", "Get");
638 prop.append("xyz.openbmc_project.Object.Enable");
639 prop.append("Enabled");
640 try
641 {
642 auto result = bus.call(prop);
643 }
644 catch (const sdbusplus::exception::SdBusError& e)
645 {
646 // Treat as property unavailable
647 inventory::PropertyMap prop;
648 prop.emplace("Enabled", false);
649 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
650 }
651}
652
653/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530654 * @brief Prime the Inventory
655 * Prime the inventory by populating only the location code,
656 * type interface and the inventory object for the frus
657 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530658 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530659 * @param[in] jsObject - Reference to vpd inventory json object
660 * @param[in] vpdMap - Reference to the parsed vpd map
661 *
662 * @returns Map of items in extraInterface.
663 */
664template <typename T>
665inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
666 const T& vpdMap)
667{
668 inventory::ObjectMap objects;
669
670 for (auto& itemFRUS : jsObject["frus"].items())
671 {
672 for (auto& itemEEPROM : itemFRUS.value())
673 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600674 // Take pre actions if needed
675 if (itemEEPROM.find("preAction") != itemEEPROM.end())
676 {
677 preAction(jsObject, itemFRUS.key());
678 }
679
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530680 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530681 inventory::Object object(itemEEPROM.at("inventoryPath"));
682
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530683 if ((itemFRUS.key() != systemVpdFilePath) &&
684 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530685 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600686 inventory::PropertyMap presProp;
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500687
688 // Do not populate Present property for frus whose
689 // synthesized=true. synthesized=true says the fru is owned by
690 // some other component and not by vpd.
691 if (!itemEEPROM.value("synthesized", false))
692 {
693 presProp.emplace("Present", false);
694 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
695 presProp);
696 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530697 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530698 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
699 {
700 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
701 {
702 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000703 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530704 {
705 if constexpr (std::is_same<T, Parsed>::value)
706 {
707 for (auto& lC : eI.value().items())
708 {
709 auto propVal = expandLocationCode(
710 lC.value().get<string>(), vpdMap, true);
711
712 props.emplace(move(lC.key()),
713 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530714 interfaces.emplace(XYZ_LOCATION_CODE_INF,
715 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530716 interfaces.emplace(move(eI.key()),
717 move(props));
718 }
719 }
720 }
721 else if (eI.key().find("Inventory.Item.") !=
722 string::npos)
723 {
724 interfaces.emplace(move(eI.key()), move(props));
725 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530726 else if (eI.key() ==
727 "xyz.openbmc_project.Inventory.Item")
728 {
729 for (auto& val : eI.value().items())
730 {
731 if (val.key() == "PrettyName")
732 {
733 presProp.emplace(val.key(),
734 val.value().get<string>());
735 }
736 }
737 // Use insert_or_assign here as we may already have
738 // inserted the present property only earlier in
739 // this function under this same interface.
740 interfaces.insert_or_assign(eI.key(),
741 move(presProp));
742 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530743 }
744 }
745 objects.emplace(move(object), move(interfaces));
746 }
747 }
748 }
749 return objects;
750}
751
Alpana Kumari65b83602020-09-01 00:24:56 -0500752/**
753 * @brief This API executes command to set environment variable
754 * And then reboot the system
755 * @param[in] key -env key to set new value
756 * @param[in] value -value to set.
757 */
758void setEnvAndReboot(const string& key, const string& value)
759{
760 // set env and reboot and break.
761 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600762 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500763 // make dbus call to reboot
764 auto bus = sdbusplus::bus::new_default_system();
765 auto method = bus.new_method_call(
766 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
767 "org.freedesktop.systemd1.Manager", "Reboot");
768 bus.call_noreply(method);
769}
770
771/*
772 * @brief This API checks for env var fitconfig.
773 * If not initialised OR updated as per the current system type,
774 * update this env var and reboot the system.
775 *
776 * @param[in] systemType IM kwd in vpd tells about which system type it is.
777 * */
778void setDevTreeEnv(const string& systemType)
779{
Alpana Kumari37e72702021-11-18 11:18:04 -0600780 // Init with default dtb
781 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530782 static const deviceTreeMap deviceTreeSystemTypeMap = {
783 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
784 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
785 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
786 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
787 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
788 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500789
790 if (deviceTreeSystemTypeMap.find(systemType) !=
791 deviceTreeSystemTypeMap.end())
792 {
793 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
794 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600795 else
796 {
797 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600798 string err = "This System type not found/supported in dtb table " +
799 systemType +
800 ".Please check the HW and IM keywords in the system "
801 "VPD.Breaking...";
802
803 // map to hold additional data in case of logging pel
804 PelAdditionalData additionalData{};
805 additionalData.emplace("DESCRIPTION", err);
806 createPEL(additionalData, PelSeverity::WARNING,
807 errIntfForInvalidSystemType);
808 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600809 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500810
811 string readVarValue;
812 bool envVarFound = false;
813
814 vector<string> output = executeCmd("/sbin/fw_printenv");
815 for (const auto& entry : output)
816 {
817 size_t pos = entry.find("=");
818 string key = entry.substr(0, pos);
819 if (key != "fitconfig")
820 {
821 continue;
822 }
823
824 envVarFound = true;
825 if (pos + 1 < entry.size())
826 {
827 readVarValue = entry.substr(pos + 1);
828 if (readVarValue.find(newDeviceTree) != string::npos)
829 {
830 // fitconfig is Updated. No action needed
831 break;
832 }
833 }
834 // set env and reboot and break.
835 setEnvAndReboot(key, newDeviceTree);
836 exit(0);
837 }
838
839 // check If env var Not found
840 if (!envVarFound)
841 {
842 setEnvAndReboot("fitconfig", newDeviceTree);
843 }
844}
845
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530846/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500847 * @brief API to call VPD manager to write VPD to EEPROM.
848 * @param[in] Object path.
849 * @param[in] record to be updated.
850 * @param[in] keyword to be updated.
851 * @param[in] keyword data to be updated
852 */
853void updateHardware(const string& objectName, const string& recName,
854 const string& kwdName, const Binary& data)
855{
856 try
857 {
858 auto bus = sdbusplus::bus::new_default();
859 auto properties =
860 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
861 properties.append(
862 static_cast<sdbusplus::message::object_path>(objectName));
863 properties.append(recName);
864 properties.append(kwdName);
865 properties.append(data);
866 bus.call(properties);
867 }
Patrick Williams8be43342021-09-02 09:33:36 -0500868 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500869 {
870 std::string what =
871 "VPDManager WriteKeyword api failed for inventory path " +
872 objectName;
873 what += " record " + recName;
874 what += " keyword " + kwdName;
875 what += " with bus error = " + std::string(e.what());
876
877 // map to hold additional data in case of logging pel
878 PelAdditionalData additionalData{};
879 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
880 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500881 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500882 }
883}
884
885/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600886 * @brief An api to get list of blank system VPD properties.
887 * @param[in] vpdMap - IPZ vpd map.
888 * @param[in] objectPath - Object path for the FRU.
889 * @param[out] blankPropertyList - Properties which are blank in System VPD and
890 * needs to be updated as standby.
891 */
892void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
893 std::vector<RestoredEeproms>& blankPropertyList)
894{
895 for (const auto& systemRecKwdPair : svpdKwdMap)
896 {
897 auto it = vpdMap.find(systemRecKwdPair.first);
898
899 // check if record is found in map we got by parser
900 if (it != vpdMap.end())
901 {
902 const auto& kwdListForRecord = systemRecKwdPair.second;
903 for (const auto& keyword : kwdListForRecord)
904 {
905 DbusPropertyMap& kwdValMap = it->second;
906 auto iterator = kwdValMap.find(keyword);
907
908 if (iterator != kwdValMap.end())
909 {
910 string& kwdValue = iterator->second;
911
912 // check bus data
913 const string& recordName = systemRecKwdPair.first;
914 const string& busValue = readBusProperty(
915 objectPath, ipzVpdInf + recordName, keyword);
916
917 if (busValue.find_first_not_of(' ') != string::npos)
918 {
919 if (kwdValue.find_first_not_of(' ') == string::npos)
920 {
921 // implies data is blank on EEPROM but not on cache.
922 // So EEPROM vpd update is required.
923 Binary busData(busValue.begin(), busValue.end());
924
925 blankPropertyList.push_back(std::make_tuple(
926 objectPath, recordName, keyword, busData));
927 }
928 }
929 }
930 }
931 }
932 }
933}
934
935/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500936 * @brief API to check if we need to restore system VPD
937 * This functionality is only applicable for IPZ VPD data.
938 * @param[in] vpdMap - IPZ vpd map
939 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500940 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600941void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500942{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500943 for (const auto& systemRecKwdPair : svpdKwdMap)
944 {
945 auto it = vpdMap.find(systemRecKwdPair.first);
946
947 // check if record is found in map we got by parser
948 if (it != vpdMap.end())
949 {
950 const auto& kwdListForRecord = systemRecKwdPair.second;
951 for (const auto& keyword : kwdListForRecord)
952 {
953 DbusPropertyMap& kwdValMap = it->second;
954 auto iterator = kwdValMap.find(keyword);
955
956 if (iterator != kwdValMap.end())
957 {
958 string& kwdValue = iterator->second;
959
960 // check bus data
961 const string& recordName = systemRecKwdPair.first;
962 const string& busValue = readBusProperty(
963 objectPath, ipzVpdInf + recordName, keyword);
964
965 if (busValue.find_first_not_of(' ') != string::npos)
966 {
967 if (kwdValue.find_first_not_of(' ') != string::npos)
968 {
969 // both the data are present, check for mismatch
970 if (busValue != kwdValue)
971 {
972 string errMsg = "VPD data mismatch on cache "
973 "and hardware for record: ";
974 errMsg += (*it).first;
975 errMsg += " and keyword: ";
976 errMsg += keyword;
977
978 // data mismatch
979 PelAdditionalData additionalData;
980 additionalData.emplace("CALLOUT_INVENTORY_PATH",
981 objectPath);
982
983 additionalData.emplace("DESCRIPTION", errMsg);
984
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500985 createPEL(additionalData, PelSeverity::WARNING,
986 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500987 }
988 }
989 else
990 {
991 // implies hardware data is blank
992 // update the map
993 Binary busData(busValue.begin(), busValue.end());
994
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500995 // update the map as well, so that cache data is not
996 // updated as blank while populating VPD map on Dbus
997 // in populateDBus Api
998 kwdValue = busValue;
999 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001000 }
1001 else if (kwdValue.find_first_not_of(' ') == string::npos)
1002 {
1003 string errMsg = "VPD is blank on both cache and "
1004 "hardware for record: ";
1005 errMsg += (*it).first;
1006 errMsg += " and keyword: ";
1007 errMsg += keyword;
1008 errMsg += ". SSR need to update hardware VPD.";
1009
1010 // both the data are blanks, log PEL
1011 PelAdditionalData additionalData;
1012 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1013 objectPath);
1014
1015 additionalData.emplace("DESCRIPTION", errMsg);
1016
1017 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001018 createPEL(additionalData, PelSeverity::ERROR,
1019 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001020 continue;
1021 }
1022 }
1023 }
1024 }
1025 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001026}
1027
1028/**
alpana077ce68722021-07-25 13:23:59 -05001029 * @brief This checks for is this FRU a processor
1030 * And if yes, then checks for is this primary
1031 *
1032 * @param[in] js- vpd json to get the information about this FRU
1033 * @param[in] filePath- FRU vpd
1034 *
1035 * @return true/false
1036 */
1037bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
1038{
1039 bool isProcessor = false;
1040 bool isPrimary = false;
1041
1042 for (const auto& item : js["frus"][filePath])
1043 {
1044 if (item.find("extraInterfaces") != item.end())
1045 {
1046 for (const auto& eI : item["extraInterfaces"].items())
1047 {
1048 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
1049 {
1050 isProcessor = true;
1051 }
1052 }
1053 }
1054
1055 if (isProcessor)
1056 {
1057 string cpuType = item.value("cpuType", "");
1058 if (cpuType == "primary")
1059 {
1060 isPrimary = true;
1061 }
1062 }
1063 }
1064
1065 return (isProcessor && isPrimary);
1066}
1067
1068/**
1069 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1070 * driver
1071 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1072 */
1073void doEnableAllDimms(nlohmann::json& js)
1074{
1075 // iterate over each fru
1076 for (const auto& eachFru : js["frus"].items())
1077 {
1078 // skip the driver binding if eeprom already exists
1079 if (fs::exists(eachFru.key()))
1080 {
1081 continue;
1082 }
1083
1084 for (const auto& eachInventory : eachFru.value())
1085 {
1086 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1087 {
1088 for (const auto& eI : eachInventory["extraInterfaces"].items())
1089 {
1090 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1091 {
1092 string dimmVpd = eachFru.key();
1093 // fetch it from
1094 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1095
1096 regex matchPatern("([0-9]+-[0-9]{4})");
1097 smatch matchFound;
1098 if (regex_search(dimmVpd, matchFound, matchPatern))
1099 {
1100 vector<string> i2cReg;
1101 boost::split(i2cReg, matchFound.str(0),
1102 boost::is_any_of("-"));
1103
1104 // remove 0s from begining
1105 const regex pattern("^0+(?!$)");
1106 for (auto& i : i2cReg)
1107 {
1108 i = regex_replace(i, pattern, "");
1109 }
1110
1111 if (i2cReg.size() == 2)
1112 {
1113 // echo 24c32 0x50 >
1114 // /sys/bus/i2c/devices/i2c-16/new_device
1115 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1116 " > /sys/bus/i2c/devices/i2c-" +
1117 i2cReg[0] + "/new_device";
1118
1119 executeCmd(cmnd);
1120 }
1121 }
1122 }
1123 }
1124 }
1125 }
1126 }
1127}
1128
1129/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001130 * @brief Check if the given CPU is an IO only chip.
1131 * The CPU is termed as IO, whose all of the cores are bad and can never be
1132 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1133 * etc., The CPU whose every cores are bad, can be identified from the CP00
1134 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1135 * has 4 cores grouped together by sharing its cache memory.)
1136 * @param [in] pgKeyword - PG Keyword of CPU.
1137 * @return true if the given cpu is an IO, false otherwise.
1138 */
1139static bool isCPUIOGoodOnly(const string& pgKeyword)
1140{
1141 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1142 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1143 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1144 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1145 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1146 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1147 // IO.
1148 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1149 {
1150 return true;
1151 }
1152
1153 // The CPU is not an IO
1154 return false;
1155}
1156
1157/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301158 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301159 * This method invokes all the populateInterface functions
1160 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301161 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1162 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301163 * @param[in] js - Inventory json object
1164 * @param[in] filePath - Path of the vpd file
1165 * @param[in] preIntrStr - Interface string
1166 */
1167template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001168static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301169{
1170 inventory::InterfaceMap interfaces;
1171 inventory::ObjectMap objects;
1172 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001173 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301174
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301175 bool isSystemVpd = (filePath == systemVpdFilePath);
1176 if constexpr (is_same<T, Parsed>::value)
1177 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001178 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1179 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1180 ::toupper);
1181
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301182 if (isSystemVpd)
1183 {
1184 std::vector<std::string> interfaces = {motherBoardInterface};
1185 // call mapper to check for object path creation
1186 MapperResponse subTree =
1187 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1188 string mboardPath =
1189 js["frus"][filePath].at(0).value("inventoryPath", "");
1190
1191 // Attempt system VPD restore if we have a motherboard
1192 // object in the inventory.
1193 if ((subTree.size() != 0) &&
1194 (subTree.find(pimPath + mboardPath) != subTree.end()))
1195 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001196 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301197 }
1198 else
1199 {
1200 log<level::ERR>("No object path found");
1201 }
1202 }
alpana077ce68722021-07-25 13:23:59 -05001203 else
1204 {
1205 // check if it is processor vpd.
1206 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1207
1208 if (isPrimaryCpu)
1209 {
1210 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1211
1212 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1213
1214 if (chipVersion >= 2)
1215 {
1216 doEnableAllDimms(js);
1217 }
1218 }
1219 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301220 }
1221
Santosh Puranikf3e69682022-03-31 17:52:38 +05301222 auto processFactoryReset = false;
1223
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001224 if (isSystemVpd)
1225 {
1226 string systemJsonName{};
1227 if constexpr (is_same<T, Parsed>::value)
1228 {
1229 // pick the right system json
1230 systemJsonName = getSystemsJson(vpdMap);
1231 }
1232
1233 fs::path target = systemJsonName;
1234 fs::path link = INVENTORY_JSON_SYM_LINK;
1235
Santosh Puranikf3e69682022-03-31 17:52:38 +05301236 // If the symlink does not exist, we treat that as a factory reset
1237 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1238
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001239 // Create the directory for hosting the symlink
1240 fs::create_directories(VPD_FILES_PATH);
1241 // unlink the symlink previously created (if any)
1242 remove(INVENTORY_JSON_SYM_LINK);
1243 // create a new symlink based on the system
1244 fs::create_symlink(target, link);
1245
1246 // Reloading the json
1247 ifstream inventoryJson(link);
1248 js = json::parse(inventoryJson);
1249 inventoryJson.close();
1250 }
1251
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301252 for (const auto& item : js["frus"][filePath])
1253 {
1254 const auto& objectPath = item["inventoryPath"];
1255 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001256
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001257 vector<string> ccinList;
1258 if (item.find("ccin") != item.end())
1259 {
1260 for (const auto& cc : item["ccin"])
1261 {
1262 string ccin = cc;
1263 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1264 ccinList.push_back(ccin);
1265 }
1266 }
1267
1268 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1269 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1270 ccinList.end()))
1271 {
1272 continue;
1273 }
1274
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001275 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301276 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001277
1278 // Populate one time properties for the system VPD and its sub-frus
1279 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301280 // For the remaining FRUs, this will get handled as a part of
1281 // priming the inventory.
1282 setOneTimeProperties(objectPath, interfaces);
1283 }
1284
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301285 // Populate the VPD keywords and the common interfaces only if we
1286 // are asked to inherit that data from the VPD, else only add the
1287 // extraInterfaces.
1288 if (item.value("inherit", true))
1289 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001290 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301291 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301292 // Each record in the VPD becomes an interface and all
1293 // keyword within the record are properties under that
1294 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301295 for (const auto& record : vpdMap)
1296 {
1297 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001298 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301299 }
1300 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001301 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301302 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001303 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301304 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301305 if (js.find("commonInterfaces") != js.end())
1306 {
1307 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1308 isSystemVpd);
1309 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301310 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001311 else
1312 {
1313 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001314 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001315 {
1316 if (item.find("copyRecords") != item.end())
1317 {
1318 for (const auto& record : item["copyRecords"])
1319 {
1320 const string& recordName = record;
1321 if (vpdMap.find(recordName) != vpdMap.end())
1322 {
1323 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001324 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001325 interfaces);
1326 }
1327 }
1328 }
1329 }
1330 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301331 // Populate interfaces and properties that are common to every FRU
1332 // and additional interface that might be defined on a per-FRU
1333 // basis.
1334 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301335 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301336 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1337 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001338 if constexpr (is_same<T, Parsed>::value)
1339 {
1340 if (item["extraInterfaces"].find(
1341 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1342 item["extraInterfaces"].end())
1343 {
1344 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1345 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001346 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001347 }
1348 }
1349 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301350 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001351
1352 // embedded property(true or false) says whether the subfru is embedded
1353 // into the parent fru (or) not. VPD sets Present property only for
1354 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1355 // or may not be physically present. Those non embedded frus will always
1356 // have Present=false irrespective of its physical presence or absence.
1357 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1358 // Present to true for such sub frus.
1359 // Eg: ethernet port is embedded into bmc card. So set Present to true
1360 // for such sub frus. Also donot populate present property for embedded
1361 // subfru which is synthesized. Currently there is no subfru which are
1362 // both embedded and synthesized. But still the case is handled here.
1363 if ((item.value("embedded", true)) &&
1364 (!item.value("synthesized", false)))
1365 {
1366 inventory::PropertyMap presProp;
1367 presProp.emplace("Present", true);
1368 insertOrMerge(interfaces, invItemIntf, move(presProp));
1369 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001370
Santosh Puranikf3e69682022-03-31 17:52:38 +05301371 if constexpr (is_same<T, Parsed>::value)
1372 {
1373 // Restore asset tag, if needed
1374 if (processFactoryReset && objectPath == "/system")
1375 {
1376 fillAssetTag(interfaces, vpdMap);
1377 }
1378 }
1379
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301380 objects.emplace(move(object), move(interfaces));
1381 }
1382
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301383 if (isSystemVpd)
1384 {
1385 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1386 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001387
Alpana Kumarif05effd2021-04-07 07:32:53 -05001388 // set the U-boot environment variable for device-tree
1389 if constexpr (is_same<T, Parsed>::value)
1390 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301391 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001392 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301393 }
1394
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301395 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001396 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301397}
1398
1399int main(int argc, char** argv)
1400{
1401 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001402 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001403 Binary vpdVector{};
1404 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001405 // map to hold additional data in case of logging pel
1406 PelAdditionalData additionalData{};
1407
1408 // this is needed to hold base fru inventory path in case there is ECC or
1409 // vpd exception while parsing the file
1410 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301411
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001412 // severity for PEL
1413 PelSeverity pelSeverity = PelSeverity::WARNING;
1414
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301415 try
1416 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301417 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1418 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301419
1420 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001421 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301422
1423 CLI11_PARSE(app, argc, argv);
1424
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001425 // PEL severity should be ERROR in case of any system VPD failure
1426 if (file == systemVpdFilePath)
1427 {
1428 pelSeverity = PelSeverity::ERROR;
1429 }
1430
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301431 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1432
1433 // If the symlink exists, it means it has been setup for us, switch the
1434 // path
1435 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1436 {
1437 jsonToParse = INVENTORY_JSON_SYM_LINK;
1438 }
1439
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301440 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301441 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001442 if (!inventoryJson)
1443 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001444 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001445 }
1446
1447 try
1448 {
1449 js = json::parse(inventoryJson);
1450 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001451 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001452 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001453 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001454 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301455
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301456 // Do we have the mandatory "frus" section?
1457 if (js.find("frus") == js.end())
1458 {
1459 throw(VpdJsonException("FRUs section not found in JSON",
1460 jsonToParse));
1461 }
1462
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301463 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1464 if (file.find("/ahb:apb") != string::npos)
1465 {
1466 // Translate udev path to a generic /sys/bus/.. file path.
1467 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301468
1469 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301470 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301471 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001472 // We need manager service active to process restoring of
1473 // system VPD on hardware. So in case any system restore is
1474 // required, update hardware in the second trigger of parser
1475 // code for system vpd file path.
1476
1477 std::vector<std::string> interfaces{motherBoardInterface};
1478
1479 // call mapper to check for object path creation
1480 MapperResponse subTree =
1481 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1482 string mboardPath =
1483 js["frus"][file].at(0).value("inventoryPath", "");
1484
1485 // Attempt system VPD restore if we have a motherboard
1486 // object in the inventory.
1487 if ((subTree.size() != 0) &&
1488 (subTree.find(pimPath + mboardPath) != subTree.end()))
1489 {
1490 vpdVector = getVpdDataInVector(js, file);
1491 ParserInterface* parser =
1492 ParserFactory::getParser(vpdVector);
1493 variant<KeywordVpdMap, Store> parseResult;
1494 parseResult = parser->parse();
1495
1496 if (auto pVal = get_if<Store>(&parseResult))
1497 {
1498 // map to hold all the keywords whose value is blank and
1499 // needs to be updated at standby.
1500 vector<RestoredEeproms> blankSystemVpdProperties{};
1501 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1502 blankSystemVpdProperties);
1503
1504 // if system VPD restore is required, update the
1505 // EEPROM
1506 for (const auto& item : blankSystemVpdProperties)
1507 {
1508 updateHardware(get<0>(item), get<1>(item),
1509 get<2>(item), get<3>(item));
1510 }
1511 }
1512 else
1513 {
1514 std::cout << "Not a valid format to restore system VPD"
1515 << std::endl;
1516 }
1517 // release the parser object
1518 ParserFactory::freeParser(parser);
1519 }
1520 else
1521 {
1522 log<level::ERR>("No object path found");
1523 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301524 return 0;
1525 }
1526 }
1527
1528 if (file.empty())
1529 {
1530 cerr << "The EEPROM path <" << file << "> is not valid.";
1531 return 0;
1532 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301533 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301534 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301535 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301536 }
1537
Alpana Kumari2f793042020-08-18 05:51:03 -05001538 if (!fs::exists(file))
1539 {
1540 cout << "Device path: " << file
1541 << " does not exist. Spurious udev event? Exiting." << endl;
1542 return 0;
1543 }
1544
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001545 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301546 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001547 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301548 // 1) The eeprom we are trying to read is that of the system VPD and the
1549 // JSON symlink is already setup (the symlink's existence tells us we
1550 // are not coming out of a factory reset)
1551 // 2) The JSON tells us that the FRU EEPROM cannot be
1552 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001553 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301554 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301555 {
1556 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1557 getPowerState())
1558 {
1559 cout << "This VPD cannot be read when power is ON" << endl;
1560 return 0;
1561 }
1562 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001563
Santosh Puranike9c57532022-03-15 16:51:51 +05301564 // Check if this VPD should be recollected at all
1565 if (!needsRecollection(js, file))
1566 {
1567 cout << "Skip VPD recollection for: " << file << endl;
1568 return 0;
1569 }
1570
Alpana Kumari2f793042020-08-18 05:51:03 -05001571 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301572 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001573 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001574 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001575 variant<KeywordVpdMap, Store> parseResult;
1576 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001577
Alpana Kumari2f793042020-08-18 05:51:03 -05001578 if (auto pVal = get_if<Store>(&parseResult))
1579 {
1580 populateDbus(pVal->getVpdMap(), js, file);
1581 }
1582 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1583 {
1584 populateDbus(*pVal, js, file);
1585 }
1586
1587 // release the parser object
1588 ParserFactory::freeParser(parser);
1589 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001590 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001591 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001592 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001593 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001594 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301595 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001596 catch (const VpdJsonException& ex)
1597 {
1598 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1599 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001600 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001601
1602 cerr << ex.what() << "\n";
1603 rc = -1;
1604 }
1605 catch (const VpdEccException& ex)
1606 {
1607 additionalData.emplace("DESCRIPTION", "ECC check failed");
1608 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1609 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001610 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001611 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001612 cerr << ex.what() << "\n";
1613 rc = -1;
1614 }
1615 catch (const VpdDataException& ex)
1616 {
alpana075cb3b1f2021-12-16 11:19:36 -06001617 if (isThisPcieOnPass1planar(js, file))
1618 {
1619 cout << "Pcie_device [" << file
1620 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1621 rc = 0;
1622 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301623 else if (!(isPresent(js, file).value_or(true)))
1624 {
1625 cout << "FRU at: " << file
1626 << " is not detected present. Ignore parser error.\n";
1627 rc = 0;
1628 }
alpana075cb3b1f2021-12-16 11:19:36 -06001629 else
1630 {
1631 string errorMsg =
1632 "VPD file is either empty or invalid. Parser failed for [";
1633 errorMsg += file;
1634 errorMsg += "], with error = " + std::string(ex.what());
1635
1636 additionalData.emplace("DESCRIPTION", errorMsg);
1637 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1638 INVENTORY_PATH + baseFruInventoryPath);
1639 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1640
1641 rc = -1;
1642 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001643 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001644 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301645 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001646 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301647 cerr << e.what() << "\n";
1648 rc = -1;
1649 }
1650
1651 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001652}