blob: b54af12a305755b9acfe455ef4400b31c1bb29ce [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>
Andrew Geissler280197e2020-12-08 20:51:49 -060025#include <phosphor-logging/log.hpp>
alpana077ce68722021-07-25 13:23:59 -050026#include <regex>
Santosh Puranik253fbe92022-10-06 22:38:09 +053027#include <thread>
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
Santosh Puranik88edeb62020-03-02 12:00:09 +053043/**
Santosh Puranike9c57532022-03-15 16:51:51 +053044 * @brief Returns the BMC state
45 */
46static auto getBMCState()
47{
48 std::string bmcState;
49 try
50 {
51 auto bus = sdbusplus::bus::new_default();
52 auto properties = bus.new_method_call(
53 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
54 "org.freedesktop.DBus.Properties", "Get");
55 properties.append("xyz.openbmc_project.State.BMC");
56 properties.append("CurrentBMCState");
57 auto result = bus.call(properties);
58 std::variant<std::string> val;
59 result.read(val);
60 if (auto pVal = std::get_if<std::string>(&val))
61 {
62 bmcState = *pVal;
63 }
64 }
65 catch (const sdbusplus::exception::SdBusError& e)
66 {
67 // Ignore any error
68 std::cerr << "Failed to get BMC state: " << e.what() << "\n";
69 }
70 return bmcState;
71}
72
73/**
74 * @brief Check if the FRU is in the cache
75 *
76 * Checks if the FRU associated with the supplied D-Bus object path is already
77 * on D-Bus. This can be used to test if a VPD collection is required for this
78 * FRU. It uses the "xyz.openbmc_project.Inventory.Item, Present" property to
79 * determine the presence of a FRU in the cache.
80 *
81 * @param objectPath - The D-Bus object path without the PIM prefix.
82 * @return true if the object exists on D-Bus, false otherwise.
83 */
84static auto isFruInVpdCache(const std::string& objectPath)
85{
86 try
87 {
88 auto bus = sdbusplus::bus::new_default();
89 auto invPath = std::string{pimPath} + objectPath;
90 auto props = bus.new_method_call(
91 "xyz.openbmc_project.Inventory.Manager", invPath.c_str(),
92 "org.freedesktop.DBus.Properties", "Get");
93 props.append("xyz.openbmc_project.Inventory.Item");
94 props.append("Present");
95 auto result = bus.call(props);
96 std::variant<bool> present;
97 result.read(present);
98 if (auto pVal = std::get_if<bool>(&present))
99 {
100 return *pVal;
101 }
102 return false;
103 }
104 catch (const sdbusplus::exception::SdBusError& e)
105 {
106 std::cout << "FRU: " << objectPath << " not in D-Bus\n";
107 // Assume not present in case of an error
108 return false;
109 }
110}
111
112/**
113 * @brief Check if VPD recollection is needed for the given EEPROM
114 *
115 * Not all FRUs can be swapped at BMC ready state. This function does the
116 * following:
117 * -- Check if the FRU is marked as "pluggableAtStandby" OR
118 * "concurrentlyMaintainable". If so, return true.
119 * -- Check if we are at BMC NotReady state. If we are, then return true.
120 * -- Else check if the FRU is not present in the VPD cache (to cover for VPD
121 * force collection). If not found in the cache, return true.
122 * -- Else return false.
123 *
124 * @param js - JSON Object.
125 * @param filePath - The EEPROM file.
126 * @return true if collection should be attempted, false otherwise.
127 */
128static auto needsRecollection(const nlohmann::json& js, const string& filePath)
129{
130 if (js["frus"][filePath].at(0).value("pluggableAtStandby", false) ||
131 js["frus"][filePath].at(0).value("concurrentlyMaintainable", false))
132 {
133 return true;
134 }
135 if (getBMCState() == "xyz.openbmc_project.State.BMC.BMCState.NotReady")
136 {
137 return true;
138 }
139 if (!isFruInVpdCache(js["frus"][filePath].at(0).value("inventoryPath", "")))
140 {
141 return true;
142 }
143 return false;
144}
145
146/**
Santosh Puranik88edeb62020-03-02 12:00:09 +0530147 * @brief Expands location codes
148 */
149static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
150 bool isSystemVpd)
151{
152 auto expanded{unexpanded};
153 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
154 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
155 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
156 size_t idx = expanded.find("fcs");
157 try
158 {
159 if (idx != string::npos)
160 {
161 string fc{};
162 string se{};
163 if (isSystemVpd)
164 {
165 const auto& fcData = vpdMap.at("VCEN").at("FC");
166 const auto& seData = vpdMap.at("VCEN").at("SE");
167 fc = string(fcData.data(), fcData.size());
168 se = string(seData.data(), seData.size());
169 }
170 else
171 {
172 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
173 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
174 }
175
Alpana Kumari81671f62021-02-10 02:21:59 -0600176 // TODO: See if ND0 can be placed in the JSON
177 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530178 }
179 else
180 {
181 idx = expanded.find("mts");
182 if (idx != string::npos)
183 {
184 string mt{};
185 string se{};
186 if (isSystemVpd)
187 {
188 const auto& mtData = vpdMap.at("VSYS").at("TM");
189 const auto& seData = vpdMap.at("VSYS").at("SE");
190 mt = string(mtData.data(), mtData.size());
191 se = string(seData.data(), seData.size());
192 }
193 else
194 {
195 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
196 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
197 }
198
199 replace(mt.begin(), mt.end(), '-', '.');
200 expanded.replace(idx, 3, mt + "." + se);
201 }
202 }
203 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500204 catch (const exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530205 {
jinuthomasf457a3e2023-04-13 12:22:48 -0500206 std::cerr << "Failed to expand location code with exception: "
207 << e.what() << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530208 }
209 return expanded;
210}
Alpana Kumari2f793042020-08-18 05:51:03 -0500211
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530212/**
213 * @brief Populate FRU specific interfaces.
214 *
215 * This is a common method which handles both
216 * ipz and keyword specific interfaces thus,
217 * reducing the code redundancy.
218 * @param[in] map - Reference to the innermost keyword-value map.
219 * @param[in] preIntrStr - Reference to the interface string.
220 * @param[out] interfaces - Reference to interface map.
221 */
222template <typename T>
223static void populateFruSpecificInterfaces(const T& map,
224 const string& preIntrStr,
225 inventory::InterfaceMap& interfaces)
226{
227 inventory::PropertyMap prop;
228
229 for (const auto& kwVal : map)
230 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530231 auto kw = kwVal.first;
232
233 if (kw[0] == '#')
234 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500235 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530236 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500237 else if (isdigit(kw[0]))
238 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500239 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500240 }
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000241 if constexpr (is_same<T, KeywordVpdMap>::value)
242 {
jinuthomasd640f692023-03-28 04:13:23 -0500243 if (auto keywordValue = get_if<Binary>(&kwVal.second))
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000244 {
jinuthomasd640f692023-03-28 04:13:23 -0500245 Binary vec((*keywordValue).begin(), (*keywordValue).end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000246 prop.emplace(move(kw), move(vec));
247 }
jinuthomasd640f692023-03-28 04:13:23 -0500248 else if (auto keywordValue = get_if<std::string>(&kwVal.second))
249 {
250 Binary vec((*keywordValue).begin(), (*keywordValue).end());
251 prop.emplace(move(kw), move(vec));
252 }
253 else if (auto keywordValue = get_if<size_t>(&kwVal.second))
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000254 {
255 if (kw == "MemorySizeInKB")
256 {
257 inventory::PropertyMap memProp;
jinuthomasd640f692023-03-28 04:13:23 -0500258 memProp.emplace(move(kw), ((*keywordValue)));
259 interfaces.emplace(
260 "xyz.openbmc_project.Inventory.Item.Dimm",
261 move(memProp));
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000262 }
jinuthomasd640f692023-03-28 04:13:23 -0500263 else
264 {
jinuthomasf457a3e2023-04-13 12:22:48 -0500265 std::cerr << "Unknown Keyword[" << kw << "] found ";
jinuthomasd640f692023-03-28 04:13:23 -0500266 }
267 }
268 else
269 {
jinuthomasf457a3e2023-04-13 12:22:48 -0500270 std::cerr << "Unknown Variant found ";
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000271 }
272 }
273 else
274 {
275 Binary vec(kwVal.second.begin(), kwVal.second.end());
276 prop.emplace(move(kw), move(vec));
277 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530278 }
279
280 interfaces.emplace(preIntrStr, move(prop));
281}
282
283/**
284 * @brief Populate Interfaces.
285 *
286 * This method populates common and extra interfaces to dbus.
287 * @param[in] js - json object
288 * @param[out] interfaces - Reference to interface map
289 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530290 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530291 */
292template <typename T>
293static void populateInterfaces(const nlohmann::json& js,
294 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530295 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530296{
297 for (const auto& ifs : js.items())
298 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530299 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530300 inventory::PropertyMap props;
301
302 for (const auto& itr : ifs.value().items())
303 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530304 const string& busProp = itr.key();
305
Alpana Kumari31970de2020-02-17 06:49:57 -0600306 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530307 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530308 props.emplace(busProp, itr.value().get<bool>());
309 }
310 else if (itr.value().is_string())
311 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600312 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530313 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600314 std::string prop;
315 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530316 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000317 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600318 prop = expandLocationCode(itr.value().get<string>(),
319 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530320 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600321 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530322 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600323 // Send empty Parsed object to expandLocationCode api.
324 prop = expandLocationCode(itr.value().get<string>(),
325 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530326 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600327 props.emplace(busProp, prop);
328 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
329 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530330 }
331 else
332 {
333 props.emplace(busProp, itr.value().get<string>());
334 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530335 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530336 else if (itr.value().is_array())
337 {
338 try
339 {
340 props.emplace(busProp, itr.value().get<Binary>());
341 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500342 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530343 {
344 std::cerr << "Type exception: " << e.what() << "\n";
345 // Ignore any type errors
346 }
347 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600348 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530349 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600350 const string& rec = itr.value().value("recordName", "");
351 const string& kw = itr.value().value("keywordName", "");
352 const string& encoding = itr.value().value("encoding", "");
353
Alpana Kumari58e22142020-05-05 00:22:12 -0500354 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530355 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530356 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
357 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600358 {
359 auto encoded =
360 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530361 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600362 }
363 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500364 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600365 {
366 if (!kw.empty() && vpdMap.count(kw))
367 {
jinuthomasd640f692023-03-28 04:13:23 -0500368 if (auto kwValue = get_if<Binary>(&vpdMap.at(kw)))
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000369 {
370 auto prop =
371 string((*kwValue).begin(), (*kwValue).end());
372
373 auto encoded = encodeKeyword(prop, encoding);
374
375 props.emplace(busProp, encoded);
376 }
jinuthomasd640f692023-03-28 04:13:23 -0500377 else if (auto kwValue =
378 get_if<std::string>(&vpdMap.at(kw)))
379 {
380 auto prop =
381 string((*kwValue).begin(), (*kwValue).end());
382
383 auto encoded = encodeKeyword(prop, encoding);
384
385 props.emplace(busProp, encoded);
386 }
387 else if (auto uintValue =
388 get_if<size_t>(&vpdMap.at(kw)))
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000389 {
390 props.emplace(busProp, *uintValue);
391 }
jinuthomasd640f692023-03-28 04:13:23 -0500392 else
393 {
394 std::cerr << " Unknown Keyword [" << kw
395 << "] Encountered";
396 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600397 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530398 }
399 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500400 else if (itr.value().is_number())
401 {
402 // For now assume the value is a size_t. In the future it would
403 // be nice to come up with a way to get the type from the JSON.
404 props.emplace(busProp, itr.value().get<size_t>());
405 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530406 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600407 insertOrMerge(interfaces, inf, move(props));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530408 }
409}
410
alpana075cb3b1f2021-12-16 11:19:36 -0600411/**
412 * @brief This API checks if this FRU is pcie_devices. If yes then it further
413 * checks whether it is PASS1 planar.
414 */
415static bool isThisPcieOnPass1planar(const nlohmann::json& js,
416 const string& file)
417{
418 auto isThisPCIeDev = false;
419 auto isPASS1 = false;
420
421 // Check if it is a PCIE device
422 if (js["frus"].find(file) != js["frus"].end())
423 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530424 if ((js["frus"][file].at(0).find("extraInterfaces") !=
425 js["frus"][file].at(0).end()))
alpana075cb3b1f2021-12-16 11:19:36 -0600426 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530427 if (js["frus"][file].at(0)["extraInterfaces"].find(
alpana075cb3b1f2021-12-16 11:19:36 -0600428 "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
Santosh Puranikc03f3902022-04-14 10:58:26 +0530429 js["frus"][file].at(0)["extraInterfaces"].end())
alpana075cb3b1f2021-12-16 11:19:36 -0600430 {
431 isThisPCIeDev = true;
432 }
433 }
434 }
435
436 if (isThisPCIeDev)
437 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500438 // Collect HW version and SystemType to know if it is PASS1 planar.
alpana075cb3b1f2021-12-16 11:19:36 -0600439 auto bus = sdbusplus::bus::new_default();
Alpana Kumaria6181e22022-05-12 05:01:53 -0500440 auto property1 = bus.new_method_call(
alpana075cb3b1f2021-12-16 11:19:36 -0600441 INVENTORY_MANAGER_SERVICE,
442 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
443 "org.freedesktop.DBus.Properties", "Get");
Alpana Kumaria6181e22022-05-12 05:01:53 -0500444 property1.append("com.ibm.ipzvpd.VINI");
445 property1.append("HW");
446 auto result1 = bus.call(property1);
447 inventory::Value hwVal;
448 result1.read(hwVal);
alpana075cb3b1f2021-12-16 11:19:36 -0600449
Alpana Kumaria6181e22022-05-12 05:01:53 -0500450 // SystemType
451 auto property2 = bus.new_method_call(
452 INVENTORY_MANAGER_SERVICE,
453 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
454 "org.freedesktop.DBus.Properties", "Get");
455 property2.append("com.ibm.ipzvpd.VSBP");
456 property2.append("IM");
457 auto result2 = bus.call(property2);
458 inventory::Value imVal;
459 result2.read(imVal);
460
461 auto pVal1 = get_if<Binary>(&hwVal);
462 auto pVal2 = get_if<Binary>(&imVal);
463
464 if (pVal1 && pVal2)
alpana075cb3b1f2021-12-16 11:19:36 -0600465 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500466 auto hwVersion = *pVal1;
467 auto systemType = *pVal2;
468
469 // IM kw for Everest
470 Binary everestSystem{80, 00, 48, 00};
471
472 if (systemType == everestSystem)
473 {
474 if (hwVersion[1] < 21)
475 {
476 isPASS1 = true;
477 }
478 }
479 else if (hwVersion[1] < 2)
480 {
alpana075cb3b1f2021-12-16 11:19:36 -0600481 isPASS1 = true;
Alpana Kumaria6181e22022-05-12 05:01:53 -0500482 }
alpana075cb3b1f2021-12-16 11:19:36 -0600483 }
484 }
485
486 return (isThisPCIeDev && isPASS1);
487}
488
Alpana Kumari735dee92022-03-25 01:24:40 -0500489/** Performs any pre-action needed to get the FRU setup for collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500490 *
491 * @param[in] json - json object
492 * @param[in] file - eeprom file path
493 */
494static void preAction(const nlohmann::json& json, const string& file)
495{
Alpana Kumari735dee92022-03-25 01:24:40 -0500496 if ((json["frus"][file].at(0)).find("preAction") ==
Alpana Kumari2f793042020-08-18 05:51:03 -0500497 json["frus"][file].at(0).end())
498 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500499 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500500 }
501
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500502 try
Alpana Kumari2f793042020-08-18 05:51:03 -0500503 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500504 if (executePreAction(json, file))
Alpana Kumari2f793042020-08-18 05:51:03 -0500505 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500506 if (json["frus"][file].at(0).find("devAddress") !=
507 json["frus"][file].at(0).end())
Alpana Kumari40d1c192022-03-09 21:16:02 -0600508 {
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500509 // Now bind the device
510 string bind = json["frus"][file].at(0).value("devAddress", "");
jinuthomasf457a3e2023-04-13 12:22:48 -0500511 std::cout << "Binding device " << bind << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500512 string bindCmd = string("echo \"") + bind +
513 string("\" > /sys/bus/i2c/drivers/at24/bind");
jinuthomasf457a3e2023-04-13 12:22:48 -0500514 std::cout << bindCmd << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500515 executeCmd(bindCmd);
516
517 // Check if device showed up (test for file)
518 if (!fs::exists(file))
519 {
jinuthomasf457a3e2023-04-13 12:22:48 -0500520 std::cerr << "EEPROM " << file
521 << " does not exist. Take failure action"
522 << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500523 // If not, then take failure postAction
524 executePostFailAction(json, file);
525 }
526 }
527 else
528 {
529 // missing required informations
jinuthomasf457a3e2023-04-13 12:22:48 -0500530 std::cerr << "VPD inventory JSON missing basic informations of "
531 "preAction "
532 "for this FRU : ["
533 << file << "]. Executing executePostFailAction."
534 << std::endl;
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500535
536 // Take failure postAction
Alpana Kumari40d1c192022-03-09 21:16:02 -0600537 executePostFailAction(json, file);
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500538 return;
Alpana Kumari40d1c192022-03-09 21:16:02 -0600539 }
540 }
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530541 else
542 {
543 // If the FRU is not there, clear the VINI/CCIN data.
544 // Enity manager probes for this keyword to look for this
545 // FRU, now if the data is persistent on BMC and FRU is
546 // removed this can lead to ambiguity. Hence clearing this
547 // Keyword if FRU is absent.
548 const auto& invPath =
549 json["frus"][file].at(0).value("inventoryPath", "");
550
551 if (!invPath.empty())
552 {
553 inventory::ObjectMap pimObjMap{
554 {invPath, {{"com.ibm.ipzvpd.VINI", {{"CC", Binary{}}}}}}};
555
556 common::utility::callPIM(move(pimObjMap));
557 }
558 else
559 {
560 throw std::runtime_error("Path empty in Json");
561 }
562 }
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500563 }
564 catch (const GpioException& e)
565 {
566 PelAdditionalData additionalData{};
567 additionalData.emplace("DESCRIPTION", e.what());
568 createPEL(additionalData, PelSeverity::WARNING, errIntfForGpioError,
569 nullptr);
Alpana Kumari2f793042020-08-18 05:51:03 -0500570 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500571}
572
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530573/**
Santosh Puranikf3e69682022-03-31 17:52:38 +0530574 * @brief Fills the Decorator.AssetTag property into the interfaces map
575 *
576 * This function should only be called in cases where we did not find a JSON
577 * symlink. A missing symlink in /var/lib will be considered as a factory reset
578 * and this function will be used to default the AssetTag property.
579 *
580 * @param interfaces A possibly pre-populated map of inetrfaces to properties.
581 * @param vpdMap A VPD map of the system VPD data.
582 */
583static void fillAssetTag(inventory::InterfaceMap& interfaces,
584 const Parsed& vpdMap)
585{
586 // Read the system serial number and MTM
587 // Default asset tag is Server-MTM-System Serial
588 inventory::Interface assetIntf{
589 "xyz.openbmc_project.Inventory.Decorator.AssetTag"};
590 inventory::PropertyMap assetTagProps;
591 std::string defaultAssetTag =
592 std::string{"Server-"} + getKwVal(vpdMap, "VSYS", "TM") +
593 std::string{"-"} + getKwVal(vpdMap, "VSYS", "SE");
594 assetTagProps.emplace("AssetTag", defaultAssetTag);
595 insertOrMerge(interfaces, assetIntf, std::move(assetTagProps));
596}
597
598/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530599 * @brief Set certain one time properties in the inventory
600 * Use this function to insert the Functional and Enabled properties into the
601 * inventory map. This function first checks if the object in question already
602 * has these properties hosted on D-Bus, if the property is already there, it is
603 * not modified, hence the name "one time". If the property is not already
604 * present, it will be added to the map with a suitable default value (true for
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530605 * Functional and Enabled)
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530606 *
607 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
608 * @param[inout] interfaces - Reference to a map of inventory interfaces to
609 * which the properties will be attached.
610 */
611static void setOneTimeProperties(const std::string& object,
612 inventory::InterfaceMap& interfaces)
613{
614 auto bus = sdbusplus::bus::new_default();
615 auto objectPath = INVENTORY_PATH + object;
616 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
617 objectPath.c_str(),
618 "org.freedesktop.DBus.Properties", "Get");
619 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
620 prop.append("Functional");
621 try
622 {
623 auto result = bus.call(prop);
624 }
625 catch (const sdbusplus::exception::SdBusError& e)
626 {
627 // Treat as property unavailable
628 inventory::PropertyMap prop;
629 prop.emplace("Functional", true);
630 interfaces.emplace(
631 "xyz.openbmc_project.State.Decorator.OperationalStatus",
632 move(prop));
633 }
634 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
635 objectPath.c_str(),
636 "org.freedesktop.DBus.Properties", "Get");
637 prop.append("xyz.openbmc_project.Object.Enable");
638 prop.append("Enabled");
639 try
640 {
641 auto result = bus.call(prop);
642 }
643 catch (const sdbusplus::exception::SdBusError& e)
644 {
645 // Treat as property unavailable
646 inventory::PropertyMap prop;
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530647 prop.emplace("Enabled", true);
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530648 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
649 }
650}
651
652/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530653 * @brief Prime the Inventory
654 * Prime the inventory by populating only the location code,
655 * type interface and the inventory object for the frus
656 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530657 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530658 * @param[in] jsObject - Reference to vpd inventory json object
659 * @param[in] vpdMap - Reference to the parsed vpd map
660 *
661 * @returns Map of items in extraInterface.
662 */
663template <typename T>
664inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
665 const T& vpdMap)
666{
667 inventory::ObjectMap objects;
668
669 for (auto& itemFRUS : jsObject["frus"].items())
670 {
671 for (auto& itemEEPROM : itemFRUS.value())
672 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600673 // Take pre actions if needed
674 if (itemEEPROM.find("preAction") != itemEEPROM.end())
675 {
676 preAction(jsObject, itemFRUS.key());
677 }
678
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530679 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530680 inventory::Object object(itemEEPROM.at("inventoryPath"));
681
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530682 if ((itemFRUS.key() != systemVpdFilePath) &&
683 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530684 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600685 inventory::PropertyMap presProp;
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500686
687 // Do not populate Present property for frus whose
Priyanga Ramasamyaca61372023-01-24 08:02:28 -0600688 // synthesized=true. synthesized=true says the fru VPD is
689 // synthesized and owned by a separate component.
690 // In some cases, the FRU has its own VPD, but still a separate
691 // application handles the FRU's presence. So VPD parser skips
692 // populating Present property by checking the JSON flag,
693 // "handlePresence".
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500694 if (!itemEEPROM.value("synthesized", false))
695 {
Priyanga Ramasamyaca61372023-01-24 08:02:28 -0600696 if (itemEEPROM.value("handlePresence", true))
697 {
698 presProp.emplace("Present", false);
699 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
700 presProp);
701 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500702 }
Priyanga Ramasamyaca61372023-01-24 08:02:28 -0600703
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530704 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530705 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
706 {
707 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
708 {
709 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000710 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530711 {
712 if constexpr (std::is_same<T, Parsed>::value)
713 {
714 for (auto& lC : eI.value().items())
715 {
716 auto propVal = expandLocationCode(
717 lC.value().get<string>(), vpdMap, true);
718
719 props.emplace(move(lC.key()),
720 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530721 interfaces.emplace(XYZ_LOCATION_CODE_INF,
722 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530723 interfaces.emplace(move(eI.key()),
724 move(props));
725 }
726 }
727 }
728 else if (eI.key().find("Inventory.Item.") !=
729 string::npos)
730 {
731 interfaces.emplace(move(eI.key()), move(props));
732 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530733 else if (eI.key() ==
734 "xyz.openbmc_project.Inventory.Item")
735 {
736 for (auto& val : eI.value().items())
737 {
738 if (val.key() == "PrettyName")
739 {
740 presProp.emplace(val.key(),
741 val.value().get<string>());
742 }
743 }
744 // Use insert_or_assign here as we may already have
745 // inserted the present property only earlier in
746 // this function under this same interface.
747 interfaces.insert_or_assign(eI.key(),
748 move(presProp));
749 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530750 }
751 }
752 objects.emplace(move(object), move(interfaces));
753 }
754 }
755 }
756 return objects;
757}
758
Alpana Kumari65b83602020-09-01 00:24:56 -0500759/**
760 * @brief This API executes command to set environment variable
761 * And then reboot the system
762 * @param[in] key -env key to set new value
763 * @param[in] value -value to set.
764 */
765void setEnvAndReboot(const string& key, const string& value)
766{
767 // set env and reboot and break.
768 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600769 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500770 // make dbus call to reboot
771 auto bus = sdbusplus::bus::new_default_system();
772 auto method = bus.new_method_call(
773 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
774 "org.freedesktop.systemd1.Manager", "Reboot");
775 bus.call_noreply(method);
776}
777
778/*
779 * @brief This API checks for env var fitconfig.
780 * If not initialised OR updated as per the current system type,
781 * update this env var and reboot the system.
782 *
783 * @param[in] systemType IM kwd in vpd tells about which system type it is.
784 * */
785void setDevTreeEnv(const string& systemType)
786{
Alpana Kumari37e72702021-11-18 11:18:04 -0600787 // Init with default dtb
788 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530789 static const deviceTreeMap deviceTreeSystemTypeMap = {
790 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
791 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
792 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
793 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
794 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
Alpana Kumari1b026112022-03-02 23:41:38 -0600795 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"},
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530796 {EVEREST_V2, "conf-aspeed-bmc-ibm-everest.dtb"},
797 {BONNELL, "conf-aspeed-bmc-ibm-bonnell.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500798
799 if (deviceTreeSystemTypeMap.find(systemType) !=
800 deviceTreeSystemTypeMap.end())
801 {
802 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
803 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600804 else
805 {
806 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600807 string err = "This System type not found/supported in dtb table " +
808 systemType +
809 ".Please check the HW and IM keywords in the system "
810 "VPD.Breaking...";
811
812 // map to hold additional data in case of logging pel
813 PelAdditionalData additionalData{};
814 additionalData.emplace("DESCRIPTION", err);
815 createPEL(additionalData, PelSeverity::WARNING,
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500816 errIntfForInvalidSystemType, nullptr);
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600817 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600818 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500819
820 string readVarValue;
821 bool envVarFound = false;
822
823 vector<string> output = executeCmd("/sbin/fw_printenv");
824 for (const auto& entry : output)
825 {
826 size_t pos = entry.find("=");
827 string key = entry.substr(0, pos);
828 if (key != "fitconfig")
829 {
830 continue;
831 }
832
833 envVarFound = true;
834 if (pos + 1 < entry.size())
835 {
836 readVarValue = entry.substr(pos + 1);
837 if (readVarValue.find(newDeviceTree) != string::npos)
838 {
839 // fitconfig is Updated. No action needed
840 break;
841 }
842 }
843 // set env and reboot and break.
844 setEnvAndReboot(key, newDeviceTree);
845 exit(0);
846 }
847
848 // check If env var Not found
849 if (!envVarFound)
850 {
851 setEnvAndReboot("fitconfig", newDeviceTree);
852 }
853}
854
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530855/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500856 * @brief API to check if we need to restore system VPD
857 * This functionality is only applicable for IPZ VPD data.
858 * @param[in] vpdMap - IPZ vpd map
859 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500860 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600861void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500862{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500863 for (const auto& systemRecKwdPair : svpdKwdMap)
864 {
865 auto it = vpdMap.find(systemRecKwdPair.first);
866
867 // check if record is found in map we got by parser
868 if (it != vpdMap.end())
869 {
870 const auto& kwdListForRecord = systemRecKwdPair.second;
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600871 for (const auto& keywordInfo : kwdListForRecord)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500872 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600873 const auto keyword = get<0>(keywordInfo);
874
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500875 DbusPropertyMap& kwdValMap = it->second;
876 auto iterator = kwdValMap.find(keyword);
877
878 if (iterator != kwdValMap.end())
879 {
880 string& kwdValue = iterator->second;
881
882 // check bus data
883 const string& recordName = systemRecKwdPair.first;
884 const string& busValue = readBusProperty(
885 objectPath, ipzVpdInf + recordName, keyword);
886
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600887 const auto& defaultValue = get<1>(keywordInfo);
888 Binary busDataInBinary(busValue.begin(), busValue.end());
889 Binary kwdDataInBinary(kwdValue.begin(), kwdValue.end());
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500890
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600891 if (busDataInBinary != defaultValue)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500892 {
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600893 if (kwdDataInBinary != defaultValue)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500894 {
895 // both the data are present, check for mismatch
896 if (busValue != kwdValue)
897 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600898 string errMsg = "Mismatch found between backup "
899 "and primary VPD for record: ";
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500900 errMsg += (*it).first;
901 errMsg += " and keyword: ";
902 errMsg += keyword;
903
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530904 std::ostringstream busStream;
905 for (uint16_t byte : busValue)
906 {
907 busStream << std::setfill('0')
908 << std::setw(2) << std::hex
909 << "0x" << byte << " ";
910 }
911
912 std::ostringstream vpdStream;
913 for (uint16_t byte : kwdValue)
914 {
915 vpdStream << std::setfill('0')
916 << std::setw(2) << std::hex
917 << "0x" << byte << " ";
918 }
919
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500920 // data mismatch
921 PelAdditionalData additionalData;
922 additionalData.emplace("CALLOUT_INVENTORY_PATH",
Priyanga Ramasamyf6123682022-12-02 07:29:07 -0600923 INVENTORY_PATH +
924 objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500925
926 additionalData.emplace("DESCRIPTION", errMsg);
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530927 additionalData.emplace(
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600928 "Value read from Backup: ",
929 busStream.str());
930 additionalData.emplace(
931 "Value read from Primary: ",
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530932 vpdStream.str());
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500933
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500934 createPEL(additionalData, PelSeverity::WARNING,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600935 errIntfForVPDMismatch, nullptr);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500936 }
937 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500938
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600939 // If backup data is not default, then irrespective of
940 // primary data(default or other than backup), copy the
941 // backup data to vpd map as we don't need to change the
942 // backup data in either case in the process of
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530943 // restoring system vpd.
Santosh Puranikdedb5a62022-12-19 23:58:32 +0530944 kwdValue = busValue;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500945 }
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -0600946 else if (kwdDataInBinary == defaultValue &&
947 get<2>(keywordInfo)) // Check isPELRequired is true
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500948 {
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600949 string errMsg = "Found default value on both backup "
950 "and primary VPD for record: ";
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500951 errMsg += (*it).first;
952 errMsg += " and keyword: ";
953 errMsg += keyword;
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600954 errMsg += ". SSR need to update primary VPD.";
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500955
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600956 // mfg default on both backup and primary, log PEL
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500957 PelAdditionalData additionalData;
958 additionalData.emplace("CALLOUT_INVENTORY_PATH",
Priyanga Ramasamyf6123682022-12-02 07:29:07 -0600959 INVENTORY_PATH + objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500960
961 additionalData.emplace("DESCRIPTION", errMsg);
962
963 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500964 createPEL(additionalData, PelSeverity::ERROR,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600965 errIntfForVPDDefault, nullptr);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500966 continue;
967 }
968 }
969 }
970 }
971 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500972}
973
974/**
alpana077ce68722021-07-25 13:23:59 -0500975 * @brief This checks for is this FRU a processor
976 * And if yes, then checks for is this primary
977 *
978 * @param[in] js- vpd json to get the information about this FRU
979 * @param[in] filePath- FRU vpd
980 *
981 * @return true/false
982 */
983bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
984{
985 bool isProcessor = false;
986 bool isPrimary = false;
987
988 for (const auto& item : js["frus"][filePath])
989 {
990 if (item.find("extraInterfaces") != item.end())
991 {
992 for (const auto& eI : item["extraInterfaces"].items())
993 {
994 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
995 {
996 isProcessor = true;
997 }
998 }
999 }
1000
1001 if (isProcessor)
1002 {
1003 string cpuType = item.value("cpuType", "");
1004 if (cpuType == "primary")
1005 {
1006 isPrimary = true;
1007 }
1008 }
1009 }
1010
1011 return (isProcessor && isPrimary);
1012}
1013
1014/**
1015 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1016 * driver
1017 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1018 */
1019void doEnableAllDimms(nlohmann::json& js)
1020{
1021 // iterate over each fru
1022 for (const auto& eachFru : js["frus"].items())
1023 {
1024 // skip the driver binding if eeprom already exists
1025 if (fs::exists(eachFru.key()))
1026 {
1027 continue;
1028 }
1029
1030 for (const auto& eachInventory : eachFru.value())
1031 {
1032 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1033 {
1034 for (const auto& eI : eachInventory["extraInterfaces"].items())
1035 {
1036 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1037 {
1038 string dimmVpd = eachFru.key();
1039 // fetch it from
1040 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1041
1042 regex matchPatern("([0-9]+-[0-9]{4})");
1043 smatch matchFound;
1044 if (regex_search(dimmVpd, matchFound, matchPatern))
1045 {
1046 vector<string> i2cReg;
1047 boost::split(i2cReg, matchFound.str(0),
1048 boost::is_any_of("-"));
1049
1050 // remove 0s from begining
1051 const regex pattern("^0+(?!$)");
1052 for (auto& i : i2cReg)
1053 {
1054 i = regex_replace(i, pattern, "");
1055 }
1056
1057 if (i2cReg.size() == 2)
1058 {
1059 // echo 24c32 0x50 >
1060 // /sys/bus/i2c/devices/i2c-16/new_device
1061 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1062 " > /sys/bus/i2c/devices/i2c-" +
1063 i2cReg[0] + "/new_device";
1064
1065 executeCmd(cmnd);
1066 }
1067 }
1068 }
1069 }
1070 }
1071 }
1072 }
1073}
1074
1075/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001076 * @brief Check if the given CPU is an IO only chip.
1077 * The CPU is termed as IO, whose all of the cores are bad and can never be
1078 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1079 * etc., The CPU whose every cores are bad, can be identified from the CP00
1080 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1081 * has 4 cores grouped together by sharing its cache memory.)
1082 * @param [in] pgKeyword - PG Keyword of CPU.
1083 * @return true if the given cpu is an IO, false otherwise.
1084 */
1085static bool isCPUIOGoodOnly(const string& pgKeyword)
1086{
1087 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1088 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1089 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1090 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1091 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1092 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1093 // IO.
1094 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1095 {
1096 return true;
1097 }
1098
1099 // The CPU is not an IO
1100 return false;
1101}
1102
1103/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301104 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301105 * This method invokes all the populateInterface functions
1106 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301107 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1108 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301109 * @param[in] js - Inventory json object
1110 * @param[in] filePath - Path of the vpd file
1111 * @param[in] preIntrStr - Interface string
1112 */
1113template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001114static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301115{
1116 inventory::InterfaceMap interfaces;
1117 inventory::ObjectMap objects;
1118 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001119 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301120
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301121 bool isSystemVpd = (filePath == systemVpdFilePath);
1122 if constexpr (is_same<T, Parsed>::value)
1123 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001124 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1125 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1126 ::toupper);
1127
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301128 if (isSystemVpd)
1129 {
1130 std::vector<std::string> interfaces = {motherBoardInterface};
1131 // call mapper to check for object path creation
1132 MapperResponse subTree =
1133 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1134 string mboardPath =
1135 js["frus"][filePath].at(0).value("inventoryPath", "");
1136
1137 // Attempt system VPD restore if we have a motherboard
1138 // object in the inventory.
1139 if ((subTree.size() != 0) &&
1140 (subTree.find(pimPath + mboardPath) != subTree.end()))
1141 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001142 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301143 }
1144 else
1145 {
1146 log<level::ERR>("No object path found");
1147 }
1148 }
alpana077ce68722021-07-25 13:23:59 -05001149 else
1150 {
1151 // check if it is processor vpd.
1152 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1153
1154 if (isPrimaryCpu)
1155 {
1156 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1157
1158 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1159
1160 if (chipVersion >= 2)
1161 {
1162 doEnableAllDimms(js);
Santosh Puranik253fbe92022-10-06 22:38:09 +05301163 // Sleep for a few seconds to let the DIMM parses start
1164 using namespace std::chrono_literals;
1165 std::this_thread::sleep_for(5s);
alpana077ce68722021-07-25 13:23:59 -05001166 }
1167 }
1168 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301169 }
1170
Santosh Puranikf3e69682022-03-31 17:52:38 +05301171 auto processFactoryReset = false;
1172
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001173 if (isSystemVpd)
1174 {
1175 string systemJsonName{};
1176 if constexpr (is_same<T, Parsed>::value)
1177 {
1178 // pick the right system json
1179 systemJsonName = getSystemsJson(vpdMap);
1180 }
1181
1182 fs::path target = systemJsonName;
1183 fs::path link = INVENTORY_JSON_SYM_LINK;
1184
Santosh Puranikf3e69682022-03-31 17:52:38 +05301185 // If the symlink does not exist, we treat that as a factory reset
1186 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1187
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001188 // Create the directory for hosting the symlink
1189 fs::create_directories(VPD_FILES_PATH);
1190 // unlink the symlink previously created (if any)
1191 remove(INVENTORY_JSON_SYM_LINK);
1192 // create a new symlink based on the system
1193 fs::create_symlink(target, link);
1194
1195 // Reloading the json
1196 ifstream inventoryJson(link);
1197 js = json::parse(inventoryJson);
1198 inventoryJson.close();
1199 }
1200
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301201 for (const auto& item : js["frus"][filePath])
1202 {
1203 const auto& objectPath = item["inventoryPath"];
1204 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001205
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001206 vector<string> ccinList;
1207 if (item.find("ccin") != item.end())
1208 {
1209 for (const auto& cc : item["ccin"])
1210 {
1211 string ccin = cc;
1212 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1213 ccinList.push_back(ccin);
1214 }
1215 }
1216
1217 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1218 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1219 ccinList.end()))
1220 {
1221 continue;
1222 }
1223
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001224 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301225 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001226
1227 // Populate one time properties for the system VPD and its sub-frus
1228 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301229 // For the remaining FRUs, this will get handled as a part of
1230 // priming the inventory.
1231 setOneTimeProperties(objectPath, interfaces);
1232 }
1233
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301234 // Populate the VPD keywords and the common interfaces only if we
1235 // are asked to inherit that data from the VPD, else only add the
1236 // extraInterfaces.
1237 if (item.value("inherit", true))
1238 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001239 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301240 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301241 // Each record in the VPD becomes an interface and all
1242 // keyword within the record are properties under that
1243 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301244 for (const auto& record : vpdMap)
1245 {
1246 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001247 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301248 }
1249 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001250 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301251 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001252 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301253 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301254 if (js.find("commonInterfaces") != js.end())
1255 {
1256 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1257 isSystemVpd);
1258 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301259 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001260 else
1261 {
1262 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001263 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001264 {
1265 if (item.find("copyRecords") != item.end())
1266 {
1267 for (const auto& record : item["copyRecords"])
1268 {
1269 const string& recordName = record;
1270 if (vpdMap.find(recordName) != vpdMap.end())
1271 {
1272 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001273 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001274 interfaces);
1275 }
1276 }
1277 }
1278 }
1279 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301280 // Populate interfaces and properties that are common to every FRU
1281 // and additional interface that might be defined on a per-FRU
1282 // basis.
1283 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301284 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301285 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1286 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001287 if constexpr (is_same<T, Parsed>::value)
1288 {
1289 if (item["extraInterfaces"].find(
1290 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1291 item["extraInterfaces"].end())
1292 {
1293 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1294 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001295 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001296 }
1297 }
1298 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301299 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001300
1301 // embedded property(true or false) says whether the subfru is embedded
1302 // into the parent fru (or) not. VPD sets Present property only for
1303 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1304 // or may not be physically present. Those non embedded frus will always
1305 // have Present=false irrespective of its physical presence or absence.
1306 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1307 // Present to true for such sub frus.
1308 // Eg: ethernet port is embedded into bmc card. So set Present to true
1309 // for such sub frus. Also donot populate present property for embedded
1310 // subfru which is synthesized. Currently there is no subfru which are
1311 // both embedded and synthesized. But still the case is handled here.
1312 if ((item.value("embedded", true)) &&
1313 (!item.value("synthesized", false)))
1314 {
Priyanga Ramasamyaca61372023-01-24 08:02:28 -06001315 // Check if its required to handle presence for this FRU.
1316 if (item.value("handlePresence", true))
1317 {
1318 inventory::PropertyMap presProp;
1319 presProp.emplace("Present", true);
1320 insertOrMerge(interfaces, invItemIntf, move(presProp));
1321 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001322 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001323
Santosh Puranikf3e69682022-03-31 17:52:38 +05301324 if constexpr (is_same<T, Parsed>::value)
1325 {
1326 // Restore asset tag, if needed
1327 if (processFactoryReset && objectPath == "/system")
1328 {
1329 fillAssetTag(interfaces, vpdMap);
1330 }
1331 }
1332
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301333 objects.emplace(move(object), move(interfaces));
1334 }
1335
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301336 if (isSystemVpd)
1337 {
1338 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1339 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001340
Alpana Kumarif05effd2021-04-07 07:32:53 -05001341 // set the U-boot environment variable for device-tree
1342 if constexpr (is_same<T, Parsed>::value)
1343 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301344 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001345 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301346 }
1347
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301348 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001349 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301350}
1351
1352int main(int argc, char** argv)
1353{
1354 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001355 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001356 Binary vpdVector{};
1357 string file{};
jinuthomasf457a3e2023-04-13 12:22:48 -05001358 string driver{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001359 // map to hold additional data in case of logging pel
1360 PelAdditionalData additionalData{};
1361
1362 // this is needed to hold base fru inventory path in case there is ECC or
1363 // vpd exception while parsing the file
1364 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301365
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001366 // severity for PEL
1367 PelSeverity pelSeverity = PelSeverity::WARNING;
1368
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301369 try
1370 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001371 App app{"ibm-read-vpd - App to read IPZ/Jedec format VPD, parse it and "
1372 "store it in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301373
1374 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001375 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301376
jinuthomasf457a3e2023-04-13 12:22:48 -05001377 app.add_option("--driver", driver,
1378 "Driver used by kernel (at24,at25,ee1004)")
1379 ->required();
1380
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301381 CLI11_PARSE(app, argc, argv);
1382
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001383 // PEL severity should be ERROR in case of any system VPD failure
1384 if (file == systemVpdFilePath)
1385 {
1386 pelSeverity = PelSeverity::ERROR;
1387 }
1388
jinuthomasf457a3e2023-04-13 12:22:48 -05001389 // Check if input file is not empty.
1390 if ((file.empty()) || (driver.empty()))
1391 {
1392 std::cerr << "Encountered empty input parameter file [" << file
1393 << "] driver [" << driver << "]" << std::endl;
1394 return 0;
1395 }
1396
1397 // Check if currently supported driver or not
1398 if ((driver != at24driver) && (driver != at25driver) &&
1399 (driver != ee1004driver))
1400 {
1401 std::cerr << "The driver [" << driver << "] is not supported."
1402 << std::endl;
1403 return 0;
1404 }
1405
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301406 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1407
1408 // If the symlink exists, it means it has been setup for us, switch the
1409 // path
1410 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1411 {
1412 jsonToParse = INVENTORY_JSON_SYM_LINK;
1413 }
1414
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301415 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301416 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001417 if (!inventoryJson)
1418 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001419 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001420 }
1421
1422 try
1423 {
1424 js = json::parse(inventoryJson);
1425 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001426 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001427 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001428 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001429 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301430
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301431 // Do we have the mandatory "frus" section?
1432 if (js.find("frus") == js.end())
1433 {
1434 throw(VpdJsonException("FRUs section not found in JSON",
1435 jsonToParse));
1436 }
1437
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301438 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1439 if (file.find("/ahb:apb") != string::npos)
1440 {
1441 // Translate udev path to a generic /sys/bus/.. file path.
jinuthomasf457a3e2023-04-13 12:22:48 -05001442 udevToGenericPath(file, driver);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301443
1444 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301445 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301446 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001447 std::cout << "We have already collected system VPD, skiping."
1448 << std::endl;
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301449 return 0;
1450 }
1451 }
1452
1453 if (file.empty())
1454 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001455 std::cerr << "The EEPROM path <" << file << "> is not valid.";
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301456 return 0;
1457 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301458 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301459 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001460 std::cerr << "The EEPROM path [" << file
1461 << "] is not found in the json." << std::endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +05301462 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301463 }
1464
Alpana Kumari2f793042020-08-18 05:51:03 -05001465 if (!fs::exists(file))
1466 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001467 std::cout << "Device path: " << file
1468 << " does not exist. Spurious udev event? Exiting."
1469 << std::endl;
Alpana Kumari2f793042020-08-18 05:51:03 -05001470 return 0;
1471 }
1472
Santosh Puranikdedb5a62022-12-19 23:58:32 +05301473 // In case of system VPD it will already be filled, Don't have to
1474 // overwrite that.
1475 if (baseFruInventoryPath.empty())
1476 {
1477 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
1478 }
1479
Santosh Puranik85893752020-11-10 21:31:43 +05301480 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001481 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301482 // 1) The eeprom we are trying to read is that of the system VPD and the
1483 // JSON symlink is already setup (the symlink's existence tells us we
1484 // are not coming out of a factory reset)
1485 // 2) The JSON tells us that the FRU EEPROM cannot be
1486 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001487 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301488 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301489 {
1490 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1491 getPowerState())
1492 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001493 std::cout << "This VPD cannot be read when power is ON"
1494 << std::endl;
Santosh Puranik85893752020-11-10 21:31:43 +05301495 return 0;
1496 }
1497 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001498
Santosh Puranike9c57532022-03-15 16:51:51 +05301499 // Check if this VPD should be recollected at all
1500 if (!needsRecollection(js, file))
1501 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001502 std::cout << "Skip VPD recollection for: " << file << std::endl;
Santosh Puranike9c57532022-03-15 16:51:51 +05301503 return 0;
1504 }
1505
Alpana Kumari2f793042020-08-18 05:51:03 -05001506 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301507 {
girik18bb9852022-11-16 05:48:13 -06001508 uint32_t vpdStartOffset = 0;
1509 for (const auto& item : js["frus"][file])
1510 {
1511 if (item.find("offset") != item.end())
1512 {
1513 vpdStartOffset = item["offset"];
1514 }
1515 }
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001516 vpdVector = getVpdDataInVector(js, file);
Sunny Srivastavaf31a91b2022-06-09 08:11:29 -05001517 ParserInterface* parser = ParserFactory::getParser(
girik18bb9852022-11-16 05:48:13 -06001518 vpdVector, (pimPath + baseFruInventoryPath), file,
1519 vpdStartOffset);
Alpana Kumari2f793042020-08-18 05:51:03 -05001520 variant<KeywordVpdMap, Store> parseResult;
1521 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001522
Alpana Kumari2f793042020-08-18 05:51:03 -05001523 if (auto pVal = get_if<Store>(&parseResult))
1524 {
1525 populateDbus(pVal->getVpdMap(), js, file);
1526 }
1527 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1528 {
1529 populateDbus(*pVal, js, file);
1530 }
1531
1532 // release the parser object
1533 ParserFactory::freeParser(parser);
1534 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001535 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001536 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001537 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001538 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001539 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301540 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001541 catch (const VpdJsonException& ex)
1542 {
1543 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1544 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -05001545 createPEL(additionalData, pelSeverity, errIntfForJsonFailure, nullptr);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001546
jinuthomasf457a3e2023-04-13 12:22:48 -05001547 std::cerr << ex.what() << "\n";
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001548 rc = -1;
1549 }
1550 catch (const VpdEccException& ex)
1551 {
1552 additionalData.emplace("DESCRIPTION", "ECC check failed");
1553 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1554 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -05001555 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail, nullptr);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001556 dumpBadVpd(file, vpdVector);
jinuthomasf457a3e2023-04-13 12:22:48 -05001557 std::cerr << ex.what() << "\n";
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001558 rc = -1;
1559 }
1560 catch (const VpdDataException& ex)
1561 {
alpana075cb3b1f2021-12-16 11:19:36 -06001562 if (isThisPcieOnPass1planar(js, file))
1563 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001564 std::cout << "Pcie_device [" << file
1565 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
alpana075cb3b1f2021-12-16 11:19:36 -06001566 rc = 0;
1567 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301568 else if (!(isPresent(js, file).value_or(true)))
1569 {
jinuthomasf457a3e2023-04-13 12:22:48 -05001570 std::cout << "FRU at: " << file
1571 << " is not detected present. Ignore parser error.\n";
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301572 rc = 0;
1573 }
alpana075cb3b1f2021-12-16 11:19:36 -06001574 else
1575 {
1576 string errorMsg =
1577 "VPD file is either empty or invalid. Parser failed for [";
1578 errorMsg += file;
1579 errorMsg += "], with error = " + std::string(ex.what());
1580
1581 additionalData.emplace("DESCRIPTION", errorMsg);
1582 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1583 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -05001584 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD,
1585 nullptr);
alpana075cb3b1f2021-12-16 11:19:36 -06001586
1587 rc = -1;
1588 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001589 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001590 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301591 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001592 dumpBadVpd(file, vpdVector);
jinuthomasf457a3e2023-04-13 12:22:48 -05001593 std::cerr << e.what() << "\n";
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301594 rc = -1;
1595 }
1596
1597 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001598}