blob: b02cc43caeb2f58c2e331d352af4486040d21f7d [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"},
Alpana Kumari1b026112022-03-02 23:41:38 -0600800 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"},
801 {EVEREST_V2, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500802
803 if (deviceTreeSystemTypeMap.find(systemType) !=
804 deviceTreeSystemTypeMap.end())
805 {
806 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
807 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600808 else
809 {
810 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600811 string err = "This System type not found/supported in dtb table " +
812 systemType +
813 ".Please check the HW and IM keywords in the system "
814 "VPD.Breaking...";
815
816 // map to hold additional data in case of logging pel
817 PelAdditionalData additionalData{};
818 additionalData.emplace("DESCRIPTION", err);
819 createPEL(additionalData, PelSeverity::WARNING,
820 errIntfForInvalidSystemType);
821 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600822 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500823
824 string readVarValue;
825 bool envVarFound = false;
826
827 vector<string> output = executeCmd("/sbin/fw_printenv");
828 for (const auto& entry : output)
829 {
830 size_t pos = entry.find("=");
831 string key = entry.substr(0, pos);
832 if (key != "fitconfig")
833 {
834 continue;
835 }
836
837 envVarFound = true;
838 if (pos + 1 < entry.size())
839 {
840 readVarValue = entry.substr(pos + 1);
841 if (readVarValue.find(newDeviceTree) != string::npos)
842 {
843 // fitconfig is Updated. No action needed
844 break;
845 }
846 }
847 // set env and reboot and break.
848 setEnvAndReboot(key, newDeviceTree);
849 exit(0);
850 }
851
852 // check If env var Not found
853 if (!envVarFound)
854 {
855 setEnvAndReboot("fitconfig", newDeviceTree);
856 }
857}
858
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530859/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500860 * @brief API to call VPD manager to write VPD to EEPROM.
861 * @param[in] Object path.
862 * @param[in] record to be updated.
863 * @param[in] keyword to be updated.
864 * @param[in] keyword data to be updated
865 */
866void updateHardware(const string& objectName, const string& recName,
867 const string& kwdName, const Binary& data)
868{
869 try
870 {
871 auto bus = sdbusplus::bus::new_default();
872 auto properties =
873 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
874 properties.append(
875 static_cast<sdbusplus::message::object_path>(objectName));
876 properties.append(recName);
877 properties.append(kwdName);
878 properties.append(data);
879 bus.call(properties);
880 }
Patrick Williams8be43342021-09-02 09:33:36 -0500881 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500882 {
883 std::string what =
884 "VPDManager WriteKeyword api failed for inventory path " +
885 objectName;
886 what += " record " + recName;
887 what += " keyword " + kwdName;
888 what += " with bus error = " + std::string(e.what());
889
890 // map to hold additional data in case of logging pel
891 PelAdditionalData additionalData{};
892 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
893 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500894 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500895 }
896}
897
898/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600899 * @brief An api to get list of blank system VPD properties.
900 * @param[in] vpdMap - IPZ vpd map.
901 * @param[in] objectPath - Object path for the FRU.
902 * @param[out] blankPropertyList - Properties which are blank in System VPD and
903 * needs to be updated as standby.
904 */
905void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
906 std::vector<RestoredEeproms>& blankPropertyList)
907{
908 for (const auto& systemRecKwdPair : svpdKwdMap)
909 {
910 auto it = vpdMap.find(systemRecKwdPair.first);
911
912 // check if record is found in map we got by parser
913 if (it != vpdMap.end())
914 {
915 const auto& kwdListForRecord = systemRecKwdPair.second;
916 for (const auto& keyword : kwdListForRecord)
917 {
918 DbusPropertyMap& kwdValMap = it->second;
919 auto iterator = kwdValMap.find(keyword);
920
921 if (iterator != kwdValMap.end())
922 {
923 string& kwdValue = iterator->second;
924
925 // check bus data
926 const string& recordName = systemRecKwdPair.first;
927 const string& busValue = readBusProperty(
928 objectPath, ipzVpdInf + recordName, keyword);
929
930 if (busValue.find_first_not_of(' ') != string::npos)
931 {
932 if (kwdValue.find_first_not_of(' ') == string::npos)
933 {
934 // implies data is blank on EEPROM but not on cache.
935 // So EEPROM vpd update is required.
936 Binary busData(busValue.begin(), busValue.end());
937
938 blankPropertyList.push_back(std::make_tuple(
939 objectPath, recordName, keyword, busData));
940 }
941 }
942 }
943 }
944 }
945 }
946}
947
948/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500949 * @brief API to check if we need to restore system VPD
950 * This functionality is only applicable for IPZ VPD data.
951 * @param[in] vpdMap - IPZ vpd map
952 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500953 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600954void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500955{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500956 for (const auto& systemRecKwdPair : svpdKwdMap)
957 {
958 auto it = vpdMap.find(systemRecKwdPair.first);
959
960 // check if record is found in map we got by parser
961 if (it != vpdMap.end())
962 {
963 const auto& kwdListForRecord = systemRecKwdPair.second;
964 for (const auto& keyword : kwdListForRecord)
965 {
966 DbusPropertyMap& kwdValMap = it->second;
967 auto iterator = kwdValMap.find(keyword);
968
969 if (iterator != kwdValMap.end())
970 {
971 string& kwdValue = iterator->second;
972
973 // check bus data
974 const string& recordName = systemRecKwdPair.first;
975 const string& busValue = readBusProperty(
976 objectPath, ipzVpdInf + recordName, keyword);
977
978 if (busValue.find_first_not_of(' ') != string::npos)
979 {
980 if (kwdValue.find_first_not_of(' ') != string::npos)
981 {
982 // both the data are present, check for mismatch
983 if (busValue != kwdValue)
984 {
985 string errMsg = "VPD data mismatch on cache "
986 "and hardware for record: ";
987 errMsg += (*it).first;
988 errMsg += " and keyword: ";
989 errMsg += keyword;
990
991 // data mismatch
992 PelAdditionalData additionalData;
993 additionalData.emplace("CALLOUT_INVENTORY_PATH",
994 objectPath);
995
996 additionalData.emplace("DESCRIPTION", errMsg);
997
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500998 createPEL(additionalData, PelSeverity::WARNING,
999 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001000 }
1001 }
1002 else
1003 {
1004 // implies hardware data is blank
1005 // update the map
1006 Binary busData(busValue.begin(), busValue.end());
1007
Sunny Srivastava90a63b92021-05-26 06:30:24 -05001008 // update the map as well, so that cache data is not
1009 // updated as blank while populating VPD map on Dbus
1010 // in populateDBus Api
1011 kwdValue = busValue;
1012 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001013 }
1014 else if (kwdValue.find_first_not_of(' ') == string::npos)
1015 {
1016 string errMsg = "VPD is blank on both cache and "
1017 "hardware for record: ";
1018 errMsg += (*it).first;
1019 errMsg += " and keyword: ";
1020 errMsg += keyword;
1021 errMsg += ". SSR need to update hardware VPD.";
1022
1023 // both the data are blanks, log PEL
1024 PelAdditionalData additionalData;
1025 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1026 objectPath);
1027
1028 additionalData.emplace("DESCRIPTION", errMsg);
1029
1030 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001031 createPEL(additionalData, PelSeverity::ERROR,
1032 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001033 continue;
1034 }
1035 }
1036 }
1037 }
1038 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001039}
1040
1041/**
alpana077ce68722021-07-25 13:23:59 -05001042 * @brief This checks for is this FRU a processor
1043 * And if yes, then checks for is this primary
1044 *
1045 * @param[in] js- vpd json to get the information about this FRU
1046 * @param[in] filePath- FRU vpd
1047 *
1048 * @return true/false
1049 */
1050bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
1051{
1052 bool isProcessor = false;
1053 bool isPrimary = false;
1054
1055 for (const auto& item : js["frus"][filePath])
1056 {
1057 if (item.find("extraInterfaces") != item.end())
1058 {
1059 for (const auto& eI : item["extraInterfaces"].items())
1060 {
1061 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
1062 {
1063 isProcessor = true;
1064 }
1065 }
1066 }
1067
1068 if (isProcessor)
1069 {
1070 string cpuType = item.value("cpuType", "");
1071 if (cpuType == "primary")
1072 {
1073 isPrimary = true;
1074 }
1075 }
1076 }
1077
1078 return (isProcessor && isPrimary);
1079}
1080
1081/**
1082 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1083 * driver
1084 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1085 */
1086void doEnableAllDimms(nlohmann::json& js)
1087{
1088 // iterate over each fru
1089 for (const auto& eachFru : js["frus"].items())
1090 {
1091 // skip the driver binding if eeprom already exists
1092 if (fs::exists(eachFru.key()))
1093 {
1094 continue;
1095 }
1096
1097 for (const auto& eachInventory : eachFru.value())
1098 {
1099 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1100 {
1101 for (const auto& eI : eachInventory["extraInterfaces"].items())
1102 {
1103 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1104 {
1105 string dimmVpd = eachFru.key();
1106 // fetch it from
1107 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1108
1109 regex matchPatern("([0-9]+-[0-9]{4})");
1110 smatch matchFound;
1111 if (regex_search(dimmVpd, matchFound, matchPatern))
1112 {
1113 vector<string> i2cReg;
1114 boost::split(i2cReg, matchFound.str(0),
1115 boost::is_any_of("-"));
1116
1117 // remove 0s from begining
1118 const regex pattern("^0+(?!$)");
1119 for (auto& i : i2cReg)
1120 {
1121 i = regex_replace(i, pattern, "");
1122 }
1123
1124 if (i2cReg.size() == 2)
1125 {
1126 // echo 24c32 0x50 >
1127 // /sys/bus/i2c/devices/i2c-16/new_device
1128 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1129 " > /sys/bus/i2c/devices/i2c-" +
1130 i2cReg[0] + "/new_device";
1131
1132 executeCmd(cmnd);
1133 }
1134 }
1135 }
1136 }
1137 }
1138 }
1139 }
1140}
1141
1142/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001143 * @brief Check if the given CPU is an IO only chip.
1144 * The CPU is termed as IO, whose all of the cores are bad and can never be
1145 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1146 * etc., The CPU whose every cores are bad, can be identified from the CP00
1147 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1148 * has 4 cores grouped together by sharing its cache memory.)
1149 * @param [in] pgKeyword - PG Keyword of CPU.
1150 * @return true if the given cpu is an IO, false otherwise.
1151 */
1152static bool isCPUIOGoodOnly(const string& pgKeyword)
1153{
1154 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1155 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1156 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1157 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1158 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1159 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1160 // IO.
1161 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1162 {
1163 return true;
1164 }
1165
1166 // The CPU is not an IO
1167 return false;
1168}
1169
1170/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301171 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301172 * This method invokes all the populateInterface functions
1173 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301174 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1175 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301176 * @param[in] js - Inventory json object
1177 * @param[in] filePath - Path of the vpd file
1178 * @param[in] preIntrStr - Interface string
1179 */
1180template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001181static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301182{
1183 inventory::InterfaceMap interfaces;
1184 inventory::ObjectMap objects;
1185 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001186 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301187
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301188 bool isSystemVpd = (filePath == systemVpdFilePath);
1189 if constexpr (is_same<T, Parsed>::value)
1190 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001191 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1192 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1193 ::toupper);
1194
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301195 if (isSystemVpd)
1196 {
1197 std::vector<std::string> interfaces = {motherBoardInterface};
1198 // call mapper to check for object path creation
1199 MapperResponse subTree =
1200 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1201 string mboardPath =
1202 js["frus"][filePath].at(0).value("inventoryPath", "");
1203
1204 // Attempt system VPD restore if we have a motherboard
1205 // object in the inventory.
1206 if ((subTree.size() != 0) &&
1207 (subTree.find(pimPath + mboardPath) != subTree.end()))
1208 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001209 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301210 }
1211 else
1212 {
1213 log<level::ERR>("No object path found");
1214 }
1215 }
alpana077ce68722021-07-25 13:23:59 -05001216 else
1217 {
1218 // check if it is processor vpd.
1219 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1220
1221 if (isPrimaryCpu)
1222 {
1223 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1224
1225 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1226
1227 if (chipVersion >= 2)
1228 {
1229 doEnableAllDimms(js);
1230 }
1231 }
1232 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301233 }
1234
Santosh Puranikf3e69682022-03-31 17:52:38 +05301235 auto processFactoryReset = false;
1236
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001237 if (isSystemVpd)
1238 {
1239 string systemJsonName{};
1240 if constexpr (is_same<T, Parsed>::value)
1241 {
1242 // pick the right system json
1243 systemJsonName = getSystemsJson(vpdMap);
1244 }
1245
1246 fs::path target = systemJsonName;
1247 fs::path link = INVENTORY_JSON_SYM_LINK;
1248
Santosh Puranikf3e69682022-03-31 17:52:38 +05301249 // If the symlink does not exist, we treat that as a factory reset
1250 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1251
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001252 // Create the directory for hosting the symlink
1253 fs::create_directories(VPD_FILES_PATH);
1254 // unlink the symlink previously created (if any)
1255 remove(INVENTORY_JSON_SYM_LINK);
1256 // create a new symlink based on the system
1257 fs::create_symlink(target, link);
1258
1259 // Reloading the json
1260 ifstream inventoryJson(link);
1261 js = json::parse(inventoryJson);
1262 inventoryJson.close();
1263 }
1264
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301265 for (const auto& item : js["frus"][filePath])
1266 {
1267 const auto& objectPath = item["inventoryPath"];
1268 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001269
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001270 vector<string> ccinList;
1271 if (item.find("ccin") != item.end())
1272 {
1273 for (const auto& cc : item["ccin"])
1274 {
1275 string ccin = cc;
1276 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1277 ccinList.push_back(ccin);
1278 }
1279 }
1280
1281 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1282 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1283 ccinList.end()))
1284 {
1285 continue;
1286 }
1287
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001288 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301289 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001290
1291 // Populate one time properties for the system VPD and its sub-frus
1292 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301293 // For the remaining FRUs, this will get handled as a part of
1294 // priming the inventory.
1295 setOneTimeProperties(objectPath, interfaces);
1296 }
1297
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301298 // Populate the VPD keywords and the common interfaces only if we
1299 // are asked to inherit that data from the VPD, else only add the
1300 // extraInterfaces.
1301 if (item.value("inherit", true))
1302 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001303 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301304 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301305 // Each record in the VPD becomes an interface and all
1306 // keyword within the record are properties under that
1307 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301308 for (const auto& record : vpdMap)
1309 {
1310 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001311 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301312 }
1313 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001314 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301315 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001316 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301317 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301318 if (js.find("commonInterfaces") != js.end())
1319 {
1320 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1321 isSystemVpd);
1322 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301323 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001324 else
1325 {
1326 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001327 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001328 {
1329 if (item.find("copyRecords") != item.end())
1330 {
1331 for (const auto& record : item["copyRecords"])
1332 {
1333 const string& recordName = record;
1334 if (vpdMap.find(recordName) != vpdMap.end())
1335 {
1336 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001337 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001338 interfaces);
1339 }
1340 }
1341 }
1342 }
1343 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301344 // Populate interfaces and properties that are common to every FRU
1345 // and additional interface that might be defined on a per-FRU
1346 // basis.
1347 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301348 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301349 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1350 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001351 if constexpr (is_same<T, Parsed>::value)
1352 {
1353 if (item["extraInterfaces"].find(
1354 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1355 item["extraInterfaces"].end())
1356 {
1357 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1358 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001359 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001360 }
1361 }
1362 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301363 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001364
1365 // embedded property(true or false) says whether the subfru is embedded
1366 // into the parent fru (or) not. VPD sets Present property only for
1367 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1368 // or may not be physically present. Those non embedded frus will always
1369 // have Present=false irrespective of its physical presence or absence.
1370 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1371 // Present to true for such sub frus.
1372 // Eg: ethernet port is embedded into bmc card. So set Present to true
1373 // for such sub frus. Also donot populate present property for embedded
1374 // subfru which is synthesized. Currently there is no subfru which are
1375 // both embedded and synthesized. But still the case is handled here.
1376 if ((item.value("embedded", true)) &&
1377 (!item.value("synthesized", false)))
1378 {
1379 inventory::PropertyMap presProp;
1380 presProp.emplace("Present", true);
1381 insertOrMerge(interfaces, invItemIntf, move(presProp));
1382 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001383
Santosh Puranikf3e69682022-03-31 17:52:38 +05301384 if constexpr (is_same<T, Parsed>::value)
1385 {
1386 // Restore asset tag, if needed
1387 if (processFactoryReset && objectPath == "/system")
1388 {
1389 fillAssetTag(interfaces, vpdMap);
1390 }
1391 }
1392
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301393 objects.emplace(move(object), move(interfaces));
1394 }
1395
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301396 if (isSystemVpd)
1397 {
1398 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1399 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001400
Alpana Kumarif05effd2021-04-07 07:32:53 -05001401 // set the U-boot environment variable for device-tree
1402 if constexpr (is_same<T, Parsed>::value)
1403 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301404 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001405 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301406 }
1407
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301408 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001409 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301410}
1411
1412int main(int argc, char** argv)
1413{
1414 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001415 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001416 Binary vpdVector{};
1417 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001418 // map to hold additional data in case of logging pel
1419 PelAdditionalData additionalData{};
1420
1421 // this is needed to hold base fru inventory path in case there is ECC or
1422 // vpd exception while parsing the file
1423 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301424
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001425 // severity for PEL
1426 PelSeverity pelSeverity = PelSeverity::WARNING;
1427
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301428 try
1429 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301430 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1431 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301432
1433 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001434 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301435
1436 CLI11_PARSE(app, argc, argv);
1437
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001438 // PEL severity should be ERROR in case of any system VPD failure
1439 if (file == systemVpdFilePath)
1440 {
1441 pelSeverity = PelSeverity::ERROR;
1442 }
1443
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301444 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1445
1446 // If the symlink exists, it means it has been setup for us, switch the
1447 // path
1448 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1449 {
1450 jsonToParse = INVENTORY_JSON_SYM_LINK;
1451 }
1452
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301453 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301454 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001455 if (!inventoryJson)
1456 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001457 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001458 }
1459
1460 try
1461 {
1462 js = json::parse(inventoryJson);
1463 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001464 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001465 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001466 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001467 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301468
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301469 // Do we have the mandatory "frus" section?
1470 if (js.find("frus") == js.end())
1471 {
1472 throw(VpdJsonException("FRUs section not found in JSON",
1473 jsonToParse));
1474 }
1475
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301476 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1477 if (file.find("/ahb:apb") != string::npos)
1478 {
1479 // Translate udev path to a generic /sys/bus/.. file path.
1480 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301481
1482 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301483 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301484 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001485 // We need manager service active to process restoring of
1486 // system VPD on hardware. So in case any system restore is
1487 // required, update hardware in the second trigger of parser
1488 // code for system vpd file path.
1489
1490 std::vector<std::string> interfaces{motherBoardInterface};
1491
1492 // call mapper to check for object path creation
1493 MapperResponse subTree =
1494 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1495 string mboardPath =
1496 js["frus"][file].at(0).value("inventoryPath", "");
1497
1498 // Attempt system VPD restore if we have a motherboard
1499 // object in the inventory.
1500 if ((subTree.size() != 0) &&
1501 (subTree.find(pimPath + mboardPath) != subTree.end()))
1502 {
1503 vpdVector = getVpdDataInVector(js, file);
1504 ParserInterface* parser =
1505 ParserFactory::getParser(vpdVector);
1506 variant<KeywordVpdMap, Store> parseResult;
1507 parseResult = parser->parse();
1508
1509 if (auto pVal = get_if<Store>(&parseResult))
1510 {
1511 // map to hold all the keywords whose value is blank and
1512 // needs to be updated at standby.
1513 vector<RestoredEeproms> blankSystemVpdProperties{};
1514 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1515 blankSystemVpdProperties);
1516
1517 // if system VPD restore is required, update the
1518 // EEPROM
1519 for (const auto& item : blankSystemVpdProperties)
1520 {
1521 updateHardware(get<0>(item), get<1>(item),
1522 get<2>(item), get<3>(item));
1523 }
1524 }
1525 else
1526 {
1527 std::cout << "Not a valid format to restore system VPD"
1528 << std::endl;
1529 }
1530 // release the parser object
1531 ParserFactory::freeParser(parser);
1532 }
1533 else
1534 {
1535 log<level::ERR>("No object path found");
1536 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301537 return 0;
1538 }
1539 }
1540
1541 if (file.empty())
1542 {
1543 cerr << "The EEPROM path <" << file << "> is not valid.";
1544 return 0;
1545 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301546 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301547 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301548 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301549 }
1550
Alpana Kumari2f793042020-08-18 05:51:03 -05001551 if (!fs::exists(file))
1552 {
1553 cout << "Device path: " << file
1554 << " does not exist. Spurious udev event? Exiting." << endl;
1555 return 0;
1556 }
1557
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001558 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301559 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001560 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301561 // 1) The eeprom we are trying to read is that of the system VPD and the
1562 // JSON symlink is already setup (the symlink's existence tells us we
1563 // are not coming out of a factory reset)
1564 // 2) The JSON tells us that the FRU EEPROM cannot be
1565 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001566 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301567 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301568 {
1569 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1570 getPowerState())
1571 {
1572 cout << "This VPD cannot be read when power is ON" << endl;
1573 return 0;
1574 }
1575 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001576
Santosh Puranike9c57532022-03-15 16:51:51 +05301577 // Check if this VPD should be recollected at all
1578 if (!needsRecollection(js, file))
1579 {
1580 cout << "Skip VPD recollection for: " << file << endl;
1581 return 0;
1582 }
1583
Alpana Kumari2f793042020-08-18 05:51:03 -05001584 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301585 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001586 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001587 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001588 variant<KeywordVpdMap, Store> parseResult;
1589 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001590
Alpana Kumari2f793042020-08-18 05:51:03 -05001591 if (auto pVal = get_if<Store>(&parseResult))
1592 {
1593 populateDbus(pVal->getVpdMap(), js, file);
1594 }
1595 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1596 {
1597 populateDbus(*pVal, js, file);
1598 }
1599
1600 // release the parser object
1601 ParserFactory::freeParser(parser);
1602 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001603 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001604 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001605 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001606 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001607 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301608 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001609 catch (const VpdJsonException& ex)
1610 {
1611 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1612 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001613 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001614
1615 cerr << ex.what() << "\n";
1616 rc = -1;
1617 }
1618 catch (const VpdEccException& ex)
1619 {
1620 additionalData.emplace("DESCRIPTION", "ECC check failed");
1621 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1622 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001623 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001624 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001625 cerr << ex.what() << "\n";
1626 rc = -1;
1627 }
1628 catch (const VpdDataException& ex)
1629 {
alpana075cb3b1f2021-12-16 11:19:36 -06001630 if (isThisPcieOnPass1planar(js, file))
1631 {
1632 cout << "Pcie_device [" << file
1633 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1634 rc = 0;
1635 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301636 else if (!(isPresent(js, file).value_or(true)))
1637 {
1638 cout << "FRU at: " << file
1639 << " is not detected present. Ignore parser error.\n";
1640 rc = 0;
1641 }
alpana075cb3b1f2021-12-16 11:19:36 -06001642 else
1643 {
1644 string errorMsg =
1645 "VPD file is either empty or invalid. Parser failed for [";
1646 errorMsg += file;
1647 errorMsg += "], with error = " + std::string(ex.what());
1648
1649 additionalData.emplace("DESCRIPTION", errorMsg);
1650 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1651 INVENTORY_PATH + baseFruInventoryPath);
1652 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1653
1654 rc = -1;
1655 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001656 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001657 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301658 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001659 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301660 cerr << e.what() << "\n";
1661 rc = -1;
1662 }
1663
1664 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001665}