blob: 6bfafdf32a0ee945a4a7f4d7bba882a5bbc09d7f [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 Srivastavaa559c2d2022-05-02 11:56:45 -050047 svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN", "RG"}},
Sunny Srivastava3c244142022-01-11 08:47:04 -060048 {"VCEN", {"FC", "SE"}},
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -050049 {"LXR0", {"LX"}},
50 {"UTIL", {"D0"}}};
Sunny Srivastava3c244142022-01-11 08:47:04 -060051
Santosh Puranik88edeb62020-03-02 12:00:09 +053052/**
Santosh Puranike9c57532022-03-15 16:51:51 +053053 * @brief Returns the BMC state
54 */
55static auto getBMCState()
56{
57 std::string bmcState;
58 try
59 {
60 auto bus = sdbusplus::bus::new_default();
61 auto properties = bus.new_method_call(
62 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
63 "org.freedesktop.DBus.Properties", "Get");
64 properties.append("xyz.openbmc_project.State.BMC");
65 properties.append("CurrentBMCState");
66 auto result = bus.call(properties);
67 std::variant<std::string> val;
68 result.read(val);
69 if (auto pVal = std::get_if<std::string>(&val))
70 {
71 bmcState = *pVal;
72 }
73 }
74 catch (const sdbusplus::exception::SdBusError& e)
75 {
76 // Ignore any error
77 std::cerr << "Failed to get BMC state: " << e.what() << "\n";
78 }
79 return bmcState;
80}
81
82/**
83 * @brief Check if the FRU is in the cache
84 *
85 * Checks if the FRU associated with the supplied D-Bus object path is already
86 * on D-Bus. This can be used to test if a VPD collection is required for this
87 * FRU. It uses the "xyz.openbmc_project.Inventory.Item, Present" property to
88 * determine the presence of a FRU in the cache.
89 *
90 * @param objectPath - The D-Bus object path without the PIM prefix.
91 * @return true if the object exists on D-Bus, false otherwise.
92 */
93static auto isFruInVpdCache(const std::string& objectPath)
94{
95 try
96 {
97 auto bus = sdbusplus::bus::new_default();
98 auto invPath = std::string{pimPath} + objectPath;
99 auto props = bus.new_method_call(
100 "xyz.openbmc_project.Inventory.Manager", invPath.c_str(),
101 "org.freedesktop.DBus.Properties", "Get");
102 props.append("xyz.openbmc_project.Inventory.Item");
103 props.append("Present");
104 auto result = bus.call(props);
105 std::variant<bool> present;
106 result.read(present);
107 if (auto pVal = std::get_if<bool>(&present))
108 {
109 return *pVal;
110 }
111 return false;
112 }
113 catch (const sdbusplus::exception::SdBusError& e)
114 {
115 std::cout << "FRU: " << objectPath << " not in D-Bus\n";
116 // Assume not present in case of an error
117 return false;
118 }
119}
120
121/**
122 * @brief Check if VPD recollection is needed for the given EEPROM
123 *
124 * Not all FRUs can be swapped at BMC ready state. This function does the
125 * following:
126 * -- Check if the FRU is marked as "pluggableAtStandby" OR
127 * "concurrentlyMaintainable". If so, return true.
128 * -- Check if we are at BMC NotReady state. If we are, then return true.
129 * -- Else check if the FRU is not present in the VPD cache (to cover for VPD
130 * force collection). If not found in the cache, return true.
131 * -- Else return false.
132 *
133 * @param js - JSON Object.
134 * @param filePath - The EEPROM file.
135 * @return true if collection should be attempted, false otherwise.
136 */
137static auto needsRecollection(const nlohmann::json& js, const string& filePath)
138{
139 if (js["frus"][filePath].at(0).value("pluggableAtStandby", false) ||
140 js["frus"][filePath].at(0).value("concurrentlyMaintainable", false))
141 {
142 return true;
143 }
144 if (getBMCState() == "xyz.openbmc_project.State.BMC.BMCState.NotReady")
145 {
146 return true;
147 }
148 if (!isFruInVpdCache(js["frus"][filePath].at(0).value("inventoryPath", "")))
149 {
150 return true;
151 }
152 return false;
153}
154
155/**
Santosh Puranik88edeb62020-03-02 12:00:09 +0530156 * @brief Expands location codes
157 */
158static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
159 bool isSystemVpd)
160{
161 auto expanded{unexpanded};
162 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
163 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
164 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
165 size_t idx = expanded.find("fcs");
166 try
167 {
168 if (idx != string::npos)
169 {
170 string fc{};
171 string se{};
172 if (isSystemVpd)
173 {
174 const auto& fcData = vpdMap.at("VCEN").at("FC");
175 const auto& seData = vpdMap.at("VCEN").at("SE");
176 fc = string(fcData.data(), fcData.size());
177 se = string(seData.data(), seData.size());
178 }
179 else
180 {
181 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
182 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
183 }
184
Alpana Kumari81671f62021-02-10 02:21:59 -0600185 // TODO: See if ND0 can be placed in the JSON
186 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530187 }
188 else
189 {
190 idx = expanded.find("mts");
191 if (idx != string::npos)
192 {
193 string mt{};
194 string se{};
195 if (isSystemVpd)
196 {
197 const auto& mtData = vpdMap.at("VSYS").at("TM");
198 const auto& seData = vpdMap.at("VSYS").at("SE");
199 mt = string(mtData.data(), mtData.size());
200 se = string(seData.data(), seData.size());
201 }
202 else
203 {
204 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
205 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
206 }
207
208 replace(mt.begin(), mt.end(), '-', '.');
209 expanded.replace(idx, 3, mt + "." + se);
210 }
211 }
212 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500213 catch (const exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530214 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500215 cerr << "Failed to expand location code with exception: " << e.what()
216 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530217 }
218 return expanded;
219}
Alpana Kumari2f793042020-08-18 05:51:03 -0500220
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530221/**
222 * @brief Populate FRU specific interfaces.
223 *
224 * This is a common method which handles both
225 * ipz and keyword specific interfaces thus,
226 * reducing the code redundancy.
227 * @param[in] map - Reference to the innermost keyword-value map.
228 * @param[in] preIntrStr - Reference to the interface string.
229 * @param[out] interfaces - Reference to interface map.
230 */
231template <typename T>
232static void populateFruSpecificInterfaces(const T& map,
233 const string& preIntrStr,
234 inventory::InterfaceMap& interfaces)
235{
236 inventory::PropertyMap prop;
237
238 for (const auto& kwVal : map)
239 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530240 auto kw = kwVal.first;
241
242 if (kw[0] == '#')
243 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500244 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530245 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500246 else if (isdigit(kw[0]))
247 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500248 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500249 }
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000250 if constexpr (is_same<T, KeywordVpdMap>::value)
251 {
252 if (get_if<Binary>(&kwVal.second))
253 {
254 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
255 get_if<Binary>(&kwVal.second)->end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000256 prop.emplace(move(kw), move(vec));
257 }
258 else
259 {
260 if (kw == "MemorySizeInKB")
261 {
262 inventory::PropertyMap memProp;
263 auto memVal = get_if<size_t>(&kwVal.second);
264 if (memVal)
265 {
266 memProp.emplace(move(kw),
267 ((*memVal) * CONVERT_MB_TO_KB));
268 interfaces.emplace(
269 "xyz.openbmc_project.Inventory.Item.Dimm",
270 move(memProp));
271 }
272 else
273 {
274 cerr << "MemorySizeInKB value not found in vpd map\n";
275 }
276 }
277 }
278 }
279 else
280 {
281 Binary vec(kwVal.second.begin(), kwVal.second.end());
282 prop.emplace(move(kw), move(vec));
283 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530284 }
285
286 interfaces.emplace(preIntrStr, move(prop));
287}
288
289/**
290 * @brief Populate Interfaces.
291 *
292 * This method populates common and extra interfaces to dbus.
293 * @param[in] js - json object
294 * @param[out] interfaces - Reference to interface map
295 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530296 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530297 */
298template <typename T>
299static void populateInterfaces(const nlohmann::json& js,
300 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530301 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530302{
303 for (const auto& ifs : js.items())
304 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530305 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530306 inventory::PropertyMap props;
307
308 for (const auto& itr : ifs.value().items())
309 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530310 const string& busProp = itr.key();
311
Alpana Kumari31970de2020-02-17 06:49:57 -0600312 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530313 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530314 props.emplace(busProp, itr.value().get<bool>());
315 }
316 else if (itr.value().is_string())
317 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600318 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530319 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600320 std::string prop;
321 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530322 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000323 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600324 prop = expandLocationCode(itr.value().get<string>(),
325 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530326 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600327 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530328 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600329 // Send empty Parsed object to expandLocationCode api.
330 prop = expandLocationCode(itr.value().get<string>(),
331 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530332 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600333 props.emplace(busProp, prop);
334 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
335 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530336 }
337 else
338 {
339 props.emplace(busProp, itr.value().get<string>());
340 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530341 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530342 else if (itr.value().is_array())
343 {
344 try
345 {
346 props.emplace(busProp, itr.value().get<Binary>());
347 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500348 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530349 {
350 std::cerr << "Type exception: " << e.what() << "\n";
351 // Ignore any type errors
352 }
353 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600354 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530355 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600356 const string& rec = itr.value().value("recordName", "");
357 const string& kw = itr.value().value("keywordName", "");
358 const string& encoding = itr.value().value("encoding", "");
359
Alpana Kumari58e22142020-05-05 00:22:12 -0500360 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530361 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530362 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
363 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600364 {
365 auto encoded =
366 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530367 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600368 }
369 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500370 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600371 {
372 if (!kw.empty() && vpdMap.count(kw))
373 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000374 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
375 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
376
377 if (kwValue)
378 {
379 auto prop =
380 string((*kwValue).begin(), (*kwValue).end());
381
382 auto encoded = encodeKeyword(prop, encoding);
383
384 props.emplace(busProp, encoded);
385 }
386 else if (uintValue)
387 {
388 props.emplace(busProp, *uintValue);
389 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600390 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530391 }
392 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500393 else if (itr.value().is_number())
394 {
395 // For now assume the value is a size_t. In the future it would
396 // be nice to come up with a way to get the type from the JSON.
397 props.emplace(busProp, itr.value().get<size_t>());
398 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530399 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600400 insertOrMerge(interfaces, inf, move(props));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530401 }
402}
403
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600404/*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
405 * Currently do reset only for DIMM VPD.*/
406static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
407 ifstream& vpdFile)
408{
409 for (const auto& item : js["frus"][file])
410 {
411 if (item.find("extraInterfaces") != item.end())
412 {
413 if (item["extraInterfaces"].find(
414 "xyz.openbmc_project.Inventory.Item.Dimm") !=
415 item["extraInterfaces"].end())
416 {
417 // moves the EEPROM pointer to 2048 'th byte.
418 vpdFile.seekg(2047, std::ios::beg);
419 // Read that byte and discard - to affirm the move
420 // operation.
421 char ch;
422 vpdFile.read(&ch, sizeof(ch));
423 }
424 return;
425 }
426 }
427}
428
alpana075cb3b1f2021-12-16 11:19:36 -0600429/**
430 * @brief This API checks if this FRU is pcie_devices. If yes then it further
431 * checks whether it is PASS1 planar.
432 */
433static bool isThisPcieOnPass1planar(const nlohmann::json& js,
434 const string& file)
435{
436 auto isThisPCIeDev = false;
437 auto isPASS1 = false;
438
439 // Check if it is a PCIE device
440 if (js["frus"].find(file) != js["frus"].end())
441 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530442 if ((js["frus"][file].at(0).find("extraInterfaces") !=
443 js["frus"][file].at(0).end()))
alpana075cb3b1f2021-12-16 11:19:36 -0600444 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530445 if (js["frus"][file].at(0)["extraInterfaces"].find(
alpana075cb3b1f2021-12-16 11:19:36 -0600446 "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
Santosh Puranikc03f3902022-04-14 10:58:26 +0530447 js["frus"][file].at(0)["extraInterfaces"].end())
alpana075cb3b1f2021-12-16 11:19:36 -0600448 {
449 isThisPCIeDev = true;
450 }
451 }
452 }
453
454 if (isThisPCIeDev)
455 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500456 // Collect HW version and SystemType to know if it is PASS1 planar.
alpana075cb3b1f2021-12-16 11:19:36 -0600457 auto bus = sdbusplus::bus::new_default();
Alpana Kumaria6181e22022-05-12 05:01:53 -0500458 auto property1 = bus.new_method_call(
alpana075cb3b1f2021-12-16 11:19:36 -0600459 INVENTORY_MANAGER_SERVICE,
460 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
461 "org.freedesktop.DBus.Properties", "Get");
Alpana Kumaria6181e22022-05-12 05:01:53 -0500462 property1.append("com.ibm.ipzvpd.VINI");
463 property1.append("HW");
464 auto result1 = bus.call(property1);
465 inventory::Value hwVal;
466 result1.read(hwVal);
alpana075cb3b1f2021-12-16 11:19:36 -0600467
Alpana Kumaria6181e22022-05-12 05:01:53 -0500468 // SystemType
469 auto property2 = bus.new_method_call(
470 INVENTORY_MANAGER_SERVICE,
471 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
472 "org.freedesktop.DBus.Properties", "Get");
473 property2.append("com.ibm.ipzvpd.VSBP");
474 property2.append("IM");
475 auto result2 = bus.call(property2);
476 inventory::Value imVal;
477 result2.read(imVal);
478
479 auto pVal1 = get_if<Binary>(&hwVal);
480 auto pVal2 = get_if<Binary>(&imVal);
481
482 if (pVal1 && pVal2)
alpana075cb3b1f2021-12-16 11:19:36 -0600483 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500484 auto hwVersion = *pVal1;
485 auto systemType = *pVal2;
486
487 // IM kw for Everest
488 Binary everestSystem{80, 00, 48, 00};
489
490 if (systemType == everestSystem)
491 {
492 if (hwVersion[1] < 21)
493 {
494 isPASS1 = true;
495 }
496 }
497 else if (hwVersion[1] < 2)
498 {
alpana075cb3b1f2021-12-16 11:19:36 -0600499 isPASS1 = true;
Alpana Kumaria6181e22022-05-12 05:01:53 -0500500 }
alpana075cb3b1f2021-12-16 11:19:36 -0600501 }
502 }
503
504 return (isThisPCIeDev && isPASS1);
505}
506
Alpana Kumari2f793042020-08-18 05:51:03 -0500507static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500508{
509 uint32_t offset = 0;
510 // check if offset present?
511 for (const auto& item : js["frus"][file])
512 {
513 if (item.find("offset") != item.end())
514 {
515 offset = item["offset"];
516 }
517 }
518
519 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600520 auto maxVPDSize = std::min(std::filesystem::file_size(file),
521 static_cast<uintmax_t>(65504));
522
Alpana Kumari58e22142020-05-05 00:22:12 -0500523 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600524 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500525 ifstream vpdFile;
526 vpdFile.open(file, ios::binary);
527
528 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600529 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500530 vpdVector.resize(vpdFile.gcount());
531
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600532 resetEEPROMPointer(js, file, vpdFile);
533
Alpana Kumari58e22142020-05-05 00:22:12 -0500534 return vpdVector;
535}
536
Alpana Kumari735dee92022-03-25 01:24:40 -0500537/** Performs any pre-action needed to get the FRU setup for collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500538 *
539 * @param[in] json - json object
540 * @param[in] file - eeprom file path
541 */
542static void preAction(const nlohmann::json& json, const string& file)
543{
Alpana Kumari735dee92022-03-25 01:24:40 -0500544 if ((json["frus"][file].at(0)).find("preAction") ==
Alpana Kumari2f793042020-08-18 05:51:03 -0500545 json["frus"][file].at(0).end())
546 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500547 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500548 }
549
Alpana Kumari735dee92022-03-25 01:24:40 -0500550 if (executePreAction(json, file))
Alpana Kumari2f793042020-08-18 05:51:03 -0500551 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500552 if (json["frus"][file].at(0).find("devAddress") !=
553 json["frus"][file].at(0).end())
Alpana Kumari2f793042020-08-18 05:51:03 -0500554 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500555 // Now bind the device
556 string bind = json["frus"][file].at(0).value("devAddress", "");
557 cout << "Binding device " << bind << endl;
558 string bindCmd = string("echo \"") + bind +
559 string("\" > /sys/bus/i2c/drivers/at24/bind");
560 cout << bindCmd << endl;
561 executeCmd(bindCmd);
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600562
Alpana Kumari40d1c192022-03-09 21:16:02 -0600563 // Check if device showed up (test for file)
564 if (!fs::exists(file))
565 {
566 cerr << "EEPROM " << file
567 << " does not exist. Take failure action" << endl;
568 // If not, then take failure postAction
569 executePostFailAction(json, file);
570 }
571 }
572 else
Alpana Kumari735dee92022-03-25 01:24:40 -0500573 {
Alpana Kumari40d1c192022-03-09 21:16:02 -0600574 // missing required informations
575 cerr
576 << "VPD inventory JSON missing basic informations of preAction "
577 "for this FRU : ["
578 << file << "]. Executing executePostFailAction." << endl;
579
580 // Take failure postAction
Alpana Kumari735dee92022-03-25 01:24:40 -0500581 executePostFailAction(json, file);
Alpana Kumari40d1c192022-03-09 21:16:02 -0600582 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500583 }
584 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500585}
586
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530587/**
Santosh Puranikf3e69682022-03-31 17:52:38 +0530588 * @brief Fills the Decorator.AssetTag property into the interfaces map
589 *
590 * This function should only be called in cases where we did not find a JSON
591 * symlink. A missing symlink in /var/lib will be considered as a factory reset
592 * and this function will be used to default the AssetTag property.
593 *
594 * @param interfaces A possibly pre-populated map of inetrfaces to properties.
595 * @param vpdMap A VPD map of the system VPD data.
596 */
597static void fillAssetTag(inventory::InterfaceMap& interfaces,
598 const Parsed& vpdMap)
599{
600 // Read the system serial number and MTM
601 // Default asset tag is Server-MTM-System Serial
602 inventory::Interface assetIntf{
603 "xyz.openbmc_project.Inventory.Decorator.AssetTag"};
604 inventory::PropertyMap assetTagProps;
605 std::string defaultAssetTag =
606 std::string{"Server-"} + getKwVal(vpdMap, "VSYS", "TM") +
607 std::string{"-"} + getKwVal(vpdMap, "VSYS", "SE");
608 assetTagProps.emplace("AssetTag", defaultAssetTag);
609 insertOrMerge(interfaces, assetIntf, std::move(assetTagProps));
610}
611
612/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530613 * @brief Set certain one time properties in the inventory
614 * Use this function to insert the Functional and Enabled properties into the
615 * inventory map. This function first checks if the object in question already
616 * has these properties hosted on D-Bus, if the property is already there, it is
617 * not modified, hence the name "one time". If the property is not already
618 * present, it will be added to the map with a suitable default value (true for
619 * Functional and false for Enabled)
620 *
621 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
622 * @param[inout] interfaces - Reference to a map of inventory interfaces to
623 * which the properties will be attached.
624 */
625static void setOneTimeProperties(const std::string& object,
626 inventory::InterfaceMap& interfaces)
627{
628 auto bus = sdbusplus::bus::new_default();
629 auto objectPath = INVENTORY_PATH + object;
630 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
631 objectPath.c_str(),
632 "org.freedesktop.DBus.Properties", "Get");
633 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
634 prop.append("Functional");
635 try
636 {
637 auto result = bus.call(prop);
638 }
639 catch (const sdbusplus::exception::SdBusError& e)
640 {
641 // Treat as property unavailable
642 inventory::PropertyMap prop;
643 prop.emplace("Functional", true);
644 interfaces.emplace(
645 "xyz.openbmc_project.State.Decorator.OperationalStatus",
646 move(prop));
647 }
648 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
649 objectPath.c_str(),
650 "org.freedesktop.DBus.Properties", "Get");
651 prop.append("xyz.openbmc_project.Object.Enable");
652 prop.append("Enabled");
653 try
654 {
655 auto result = bus.call(prop);
656 }
657 catch (const sdbusplus::exception::SdBusError& e)
658 {
659 // Treat as property unavailable
660 inventory::PropertyMap prop;
661 prop.emplace("Enabled", false);
662 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
663 }
664}
665
666/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530667 * @brief Prime the Inventory
668 * Prime the inventory by populating only the location code,
669 * type interface and the inventory object for the frus
670 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530671 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530672 * @param[in] jsObject - Reference to vpd inventory json object
673 * @param[in] vpdMap - Reference to the parsed vpd map
674 *
675 * @returns Map of items in extraInterface.
676 */
677template <typename T>
678inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
679 const T& vpdMap)
680{
681 inventory::ObjectMap objects;
682
683 for (auto& itemFRUS : jsObject["frus"].items())
684 {
685 for (auto& itemEEPROM : itemFRUS.value())
686 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600687 // Take pre actions if needed
688 if (itemEEPROM.find("preAction") != itemEEPROM.end())
689 {
690 preAction(jsObject, itemFRUS.key());
691 }
692
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530693 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530694 inventory::Object object(itemEEPROM.at("inventoryPath"));
695
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530696 if ((itemFRUS.key() != systemVpdFilePath) &&
697 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530698 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600699 inventory::PropertyMap presProp;
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500700
701 // Do not populate Present property for frus whose
702 // synthesized=true. synthesized=true says the fru is owned by
703 // some other component and not by vpd.
704 if (!itemEEPROM.value("synthesized", false))
705 {
706 presProp.emplace("Present", false);
707 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
708 presProp);
709 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530710 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530711 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
712 {
713 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
714 {
715 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000716 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530717 {
718 if constexpr (std::is_same<T, Parsed>::value)
719 {
720 for (auto& lC : eI.value().items())
721 {
722 auto propVal = expandLocationCode(
723 lC.value().get<string>(), vpdMap, true);
724
725 props.emplace(move(lC.key()),
726 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530727 interfaces.emplace(XYZ_LOCATION_CODE_INF,
728 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530729 interfaces.emplace(move(eI.key()),
730 move(props));
731 }
732 }
733 }
734 else if (eI.key().find("Inventory.Item.") !=
735 string::npos)
736 {
737 interfaces.emplace(move(eI.key()), move(props));
738 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530739 else if (eI.key() ==
740 "xyz.openbmc_project.Inventory.Item")
741 {
742 for (auto& val : eI.value().items())
743 {
744 if (val.key() == "PrettyName")
745 {
746 presProp.emplace(val.key(),
747 val.value().get<string>());
748 }
749 }
750 // Use insert_or_assign here as we may already have
751 // inserted the present property only earlier in
752 // this function under this same interface.
753 interfaces.insert_or_assign(eI.key(),
754 move(presProp));
755 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530756 }
757 }
758 objects.emplace(move(object), move(interfaces));
759 }
760 }
761 }
762 return objects;
763}
764
Alpana Kumari65b83602020-09-01 00:24:56 -0500765/**
766 * @brief This API executes command to set environment variable
767 * And then reboot the system
768 * @param[in] key -env key to set new value
769 * @param[in] value -value to set.
770 */
771void setEnvAndReboot(const string& key, const string& value)
772{
773 // set env and reboot and break.
774 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600775 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500776 // make dbus call to reboot
777 auto bus = sdbusplus::bus::new_default_system();
778 auto method = bus.new_method_call(
779 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
780 "org.freedesktop.systemd1.Manager", "Reboot");
781 bus.call_noreply(method);
782}
783
784/*
785 * @brief This API checks for env var fitconfig.
786 * If not initialised OR updated as per the current system type,
787 * update this env var and reboot the system.
788 *
789 * @param[in] systemType IM kwd in vpd tells about which system type it is.
790 * */
791void setDevTreeEnv(const string& systemType)
792{
Alpana Kumari37e72702021-11-18 11:18:04 -0600793 // Init with default dtb
794 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530795 static const deviceTreeMap deviceTreeSystemTypeMap = {
796 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
797 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
798 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
799 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
800 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
Alpana Kumari1b026112022-03-02 23:41:38 -0600801 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"},
802 {EVEREST_V2, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500803
804 if (deviceTreeSystemTypeMap.find(systemType) !=
805 deviceTreeSystemTypeMap.end())
806 {
807 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
808 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600809 else
810 {
811 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600812 string err = "This System type not found/supported in dtb table " +
813 systemType +
814 ".Please check the HW and IM keywords in the system "
815 "VPD.Breaking...";
816
817 // map to hold additional data in case of logging pel
818 PelAdditionalData additionalData{};
819 additionalData.emplace("DESCRIPTION", err);
820 createPEL(additionalData, PelSeverity::WARNING,
821 errIntfForInvalidSystemType);
822 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600823 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500824
825 string readVarValue;
826 bool envVarFound = false;
827
828 vector<string> output = executeCmd("/sbin/fw_printenv");
829 for (const auto& entry : output)
830 {
831 size_t pos = entry.find("=");
832 string key = entry.substr(0, pos);
833 if (key != "fitconfig")
834 {
835 continue;
836 }
837
838 envVarFound = true;
839 if (pos + 1 < entry.size())
840 {
841 readVarValue = entry.substr(pos + 1);
842 if (readVarValue.find(newDeviceTree) != string::npos)
843 {
844 // fitconfig is Updated. No action needed
845 break;
846 }
847 }
848 // set env and reboot and break.
849 setEnvAndReboot(key, newDeviceTree);
850 exit(0);
851 }
852
853 // check If env var Not found
854 if (!envVarFound)
855 {
856 setEnvAndReboot("fitconfig", newDeviceTree);
857 }
858}
859
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530860/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500861 * @brief API to call VPD manager to write VPD to EEPROM.
862 * @param[in] Object path.
863 * @param[in] record to be updated.
864 * @param[in] keyword to be updated.
865 * @param[in] keyword data to be updated
866 */
867void updateHardware(const string& objectName, const string& recName,
868 const string& kwdName, const Binary& data)
869{
870 try
871 {
872 auto bus = sdbusplus::bus::new_default();
873 auto properties =
874 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
875 properties.append(
876 static_cast<sdbusplus::message::object_path>(objectName));
877 properties.append(recName);
878 properties.append(kwdName);
879 properties.append(data);
880 bus.call(properties);
881 }
Patrick Williams8be43342021-09-02 09:33:36 -0500882 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500883 {
884 std::string what =
885 "VPDManager WriteKeyword api failed for inventory path " +
886 objectName;
887 what += " record " + recName;
888 what += " keyword " + kwdName;
889 what += " with bus error = " + std::string(e.what());
890
891 // map to hold additional data in case of logging pel
892 PelAdditionalData additionalData{};
893 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
894 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500895 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500896 }
897}
898
899/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600900 * @brief An api to get list of blank system VPD properties.
901 * @param[in] vpdMap - IPZ vpd map.
902 * @param[in] objectPath - Object path for the FRU.
903 * @param[out] blankPropertyList - Properties which are blank in System VPD and
904 * needs to be updated as standby.
905 */
906void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
907 std::vector<RestoredEeproms>& blankPropertyList)
908{
909 for (const auto& systemRecKwdPair : svpdKwdMap)
910 {
911 auto it = vpdMap.find(systemRecKwdPair.first);
912
913 // check if record is found in map we got by parser
914 if (it != vpdMap.end())
915 {
916 const auto& kwdListForRecord = systemRecKwdPair.second;
917 for (const auto& keyword : kwdListForRecord)
918 {
919 DbusPropertyMap& kwdValMap = it->second;
920 auto iterator = kwdValMap.find(keyword);
921
922 if (iterator != kwdValMap.end())
923 {
924 string& kwdValue = iterator->second;
925
926 // check bus data
927 const string& recordName = systemRecKwdPair.first;
928 const string& busValue = readBusProperty(
929 objectPath, ipzVpdInf + recordName, keyword);
930
931 if (busValue.find_first_not_of(' ') != string::npos)
932 {
933 if (kwdValue.find_first_not_of(' ') == string::npos)
934 {
935 // implies data is blank on EEPROM but not on cache.
936 // So EEPROM vpd update is required.
937 Binary busData(busValue.begin(), busValue.end());
938
939 blankPropertyList.push_back(std::make_tuple(
940 objectPath, recordName, keyword, busData));
941 }
942 }
943 }
944 }
945 }
946 }
947}
948
949/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500950 * @brief API to check if we need to restore system VPD
951 * This functionality is only applicable for IPZ VPD data.
952 * @param[in] vpdMap - IPZ vpd map
953 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500954 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600955void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500956{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500957 for (const auto& systemRecKwdPair : svpdKwdMap)
958 {
959 auto it = vpdMap.find(systemRecKwdPair.first);
960
961 // check if record is found in map we got by parser
962 if (it != vpdMap.end())
963 {
964 const auto& kwdListForRecord = systemRecKwdPair.second;
965 for (const auto& keyword : kwdListForRecord)
966 {
967 DbusPropertyMap& kwdValMap = it->second;
968 auto iterator = kwdValMap.find(keyword);
969
970 if (iterator != kwdValMap.end())
971 {
972 string& kwdValue = iterator->second;
973
974 // check bus data
975 const string& recordName = systemRecKwdPair.first;
976 const string& busValue = readBusProperty(
977 objectPath, ipzVpdInf + recordName, keyword);
978
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500979 std::string defaultValue{' '};
980
981 // Explicit check for D0 is required as this keyword will
982 // never be blank and 0x00 should be treated as no value in
983 // this case.
984 if (recordName == "UTIL" && keyword == "D0")
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500985 {
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500986 // default value of kwd D0 is 0x00. This kwd will never
987 // be blank.
988 defaultValue = '\0';
989 }
990
991 if (busValue.find_first_not_of(defaultValue) !=
992 string::npos)
993 {
994 if (kwdValue.find_first_not_of(defaultValue) !=
995 string::npos)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500996 {
997 // both the data are present, check for mismatch
998 if (busValue != kwdValue)
999 {
1000 string errMsg = "VPD data mismatch on cache "
1001 "and hardware for record: ";
1002 errMsg += (*it).first;
1003 errMsg += " and keyword: ";
1004 errMsg += keyword;
1005
1006 // data mismatch
1007 PelAdditionalData additionalData;
1008 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1009 objectPath);
1010
1011 additionalData.emplace("DESCRIPTION", errMsg);
1012
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001013 createPEL(additionalData, PelSeverity::WARNING,
1014 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001015 }
1016 }
1017 else
1018 {
1019 // implies hardware data is blank
1020 // update the map
1021 Binary busData(busValue.begin(), busValue.end());
1022
Sunny Srivastava90a63b92021-05-26 06:30:24 -05001023 // update the map as well, so that cache data is not
1024 // updated as blank while populating VPD map on Dbus
1025 // in populateDBus Api
1026 kwdValue = busValue;
1027 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001028 }
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -05001029 else if (kwdValue.find_first_not_of(defaultValue) ==
1030 string::npos)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001031 {
1032 string errMsg = "VPD is blank on both cache and "
1033 "hardware for record: ";
1034 errMsg += (*it).first;
1035 errMsg += " and keyword: ";
1036 errMsg += keyword;
1037 errMsg += ". SSR need to update hardware VPD.";
1038
1039 // both the data are blanks, log PEL
1040 PelAdditionalData additionalData;
1041 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1042 objectPath);
1043
1044 additionalData.emplace("DESCRIPTION", errMsg);
1045
1046 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001047 createPEL(additionalData, PelSeverity::ERROR,
1048 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001049 continue;
1050 }
1051 }
1052 }
1053 }
1054 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001055}
1056
1057/**
alpana077ce68722021-07-25 13:23:59 -05001058 * @brief This checks for is this FRU a processor
1059 * And if yes, then checks for is this primary
1060 *
1061 * @param[in] js- vpd json to get the information about this FRU
1062 * @param[in] filePath- FRU vpd
1063 *
1064 * @return true/false
1065 */
1066bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
1067{
1068 bool isProcessor = false;
1069 bool isPrimary = false;
1070
1071 for (const auto& item : js["frus"][filePath])
1072 {
1073 if (item.find("extraInterfaces") != item.end())
1074 {
1075 for (const auto& eI : item["extraInterfaces"].items())
1076 {
1077 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
1078 {
1079 isProcessor = true;
1080 }
1081 }
1082 }
1083
1084 if (isProcessor)
1085 {
1086 string cpuType = item.value("cpuType", "");
1087 if (cpuType == "primary")
1088 {
1089 isPrimary = true;
1090 }
1091 }
1092 }
1093
1094 return (isProcessor && isPrimary);
1095}
1096
1097/**
1098 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1099 * driver
1100 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1101 */
1102void doEnableAllDimms(nlohmann::json& js)
1103{
1104 // iterate over each fru
1105 for (const auto& eachFru : js["frus"].items())
1106 {
1107 // skip the driver binding if eeprom already exists
1108 if (fs::exists(eachFru.key()))
1109 {
1110 continue;
1111 }
1112
1113 for (const auto& eachInventory : eachFru.value())
1114 {
1115 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1116 {
1117 for (const auto& eI : eachInventory["extraInterfaces"].items())
1118 {
1119 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1120 {
1121 string dimmVpd = eachFru.key();
1122 // fetch it from
1123 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1124
1125 regex matchPatern("([0-9]+-[0-9]{4})");
1126 smatch matchFound;
1127 if (regex_search(dimmVpd, matchFound, matchPatern))
1128 {
1129 vector<string> i2cReg;
1130 boost::split(i2cReg, matchFound.str(0),
1131 boost::is_any_of("-"));
1132
1133 // remove 0s from begining
1134 const regex pattern("^0+(?!$)");
1135 for (auto& i : i2cReg)
1136 {
1137 i = regex_replace(i, pattern, "");
1138 }
1139
1140 if (i2cReg.size() == 2)
1141 {
1142 // echo 24c32 0x50 >
1143 // /sys/bus/i2c/devices/i2c-16/new_device
1144 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1145 " > /sys/bus/i2c/devices/i2c-" +
1146 i2cReg[0] + "/new_device";
1147
1148 executeCmd(cmnd);
1149 }
1150 }
1151 }
1152 }
1153 }
1154 }
1155 }
1156}
1157
1158/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001159 * @brief Check if the given CPU is an IO only chip.
1160 * The CPU is termed as IO, whose all of the cores are bad and can never be
1161 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1162 * etc., The CPU whose every cores are bad, can be identified from the CP00
1163 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1164 * has 4 cores grouped together by sharing its cache memory.)
1165 * @param [in] pgKeyword - PG Keyword of CPU.
1166 * @return true if the given cpu is an IO, false otherwise.
1167 */
1168static bool isCPUIOGoodOnly(const string& pgKeyword)
1169{
1170 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1171 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1172 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1173 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1174 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1175 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1176 // IO.
1177 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1178 {
1179 return true;
1180 }
1181
1182 // The CPU is not an IO
1183 return false;
1184}
1185
1186/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301187 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301188 * This method invokes all the populateInterface functions
1189 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301190 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1191 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301192 * @param[in] js - Inventory json object
1193 * @param[in] filePath - Path of the vpd file
1194 * @param[in] preIntrStr - Interface string
1195 */
1196template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001197static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301198{
1199 inventory::InterfaceMap interfaces;
1200 inventory::ObjectMap objects;
1201 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001202 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301203
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301204 bool isSystemVpd = (filePath == systemVpdFilePath);
1205 if constexpr (is_same<T, Parsed>::value)
1206 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001207 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1208 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1209 ::toupper);
1210
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301211 if (isSystemVpd)
1212 {
1213 std::vector<std::string> interfaces = {motherBoardInterface};
1214 // call mapper to check for object path creation
1215 MapperResponse subTree =
1216 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1217 string mboardPath =
1218 js["frus"][filePath].at(0).value("inventoryPath", "");
1219
1220 // Attempt system VPD restore if we have a motherboard
1221 // object in the inventory.
1222 if ((subTree.size() != 0) &&
1223 (subTree.find(pimPath + mboardPath) != subTree.end()))
1224 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001225 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301226 }
1227 else
1228 {
1229 log<level::ERR>("No object path found");
1230 }
1231 }
alpana077ce68722021-07-25 13:23:59 -05001232 else
1233 {
1234 // check if it is processor vpd.
1235 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1236
1237 if (isPrimaryCpu)
1238 {
1239 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1240
1241 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1242
1243 if (chipVersion >= 2)
1244 {
1245 doEnableAllDimms(js);
1246 }
1247 }
1248 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301249 }
1250
Santosh Puranikf3e69682022-03-31 17:52:38 +05301251 auto processFactoryReset = false;
1252
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001253 if (isSystemVpd)
1254 {
1255 string systemJsonName{};
1256 if constexpr (is_same<T, Parsed>::value)
1257 {
1258 // pick the right system json
1259 systemJsonName = getSystemsJson(vpdMap);
1260 }
1261
1262 fs::path target = systemJsonName;
1263 fs::path link = INVENTORY_JSON_SYM_LINK;
1264
Santosh Puranikf3e69682022-03-31 17:52:38 +05301265 // If the symlink does not exist, we treat that as a factory reset
1266 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1267
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001268 // Create the directory for hosting the symlink
1269 fs::create_directories(VPD_FILES_PATH);
1270 // unlink the symlink previously created (if any)
1271 remove(INVENTORY_JSON_SYM_LINK);
1272 // create a new symlink based on the system
1273 fs::create_symlink(target, link);
1274
1275 // Reloading the json
1276 ifstream inventoryJson(link);
1277 js = json::parse(inventoryJson);
1278 inventoryJson.close();
1279 }
1280
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301281 for (const auto& item : js["frus"][filePath])
1282 {
1283 const auto& objectPath = item["inventoryPath"];
1284 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001285
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001286 vector<string> ccinList;
1287 if (item.find("ccin") != item.end())
1288 {
1289 for (const auto& cc : item["ccin"])
1290 {
1291 string ccin = cc;
1292 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1293 ccinList.push_back(ccin);
1294 }
1295 }
1296
1297 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1298 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1299 ccinList.end()))
1300 {
1301 continue;
1302 }
1303
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001304 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301305 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001306
1307 // Populate one time properties for the system VPD and its sub-frus
1308 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301309 // For the remaining FRUs, this will get handled as a part of
1310 // priming the inventory.
1311 setOneTimeProperties(objectPath, interfaces);
1312 }
1313
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301314 // Populate the VPD keywords and the common interfaces only if we
1315 // are asked to inherit that data from the VPD, else only add the
1316 // extraInterfaces.
1317 if (item.value("inherit", true))
1318 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001319 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301320 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301321 // Each record in the VPD becomes an interface and all
1322 // keyword within the record are properties under that
1323 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301324 for (const auto& record : vpdMap)
1325 {
1326 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001327 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301328 }
1329 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001330 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301331 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001332 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301333 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301334 if (js.find("commonInterfaces") != js.end())
1335 {
1336 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1337 isSystemVpd);
1338 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301339 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001340 else
1341 {
1342 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001343 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001344 {
1345 if (item.find("copyRecords") != item.end())
1346 {
1347 for (const auto& record : item["copyRecords"])
1348 {
1349 const string& recordName = record;
1350 if (vpdMap.find(recordName) != vpdMap.end())
1351 {
1352 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001353 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001354 interfaces);
1355 }
1356 }
1357 }
1358 }
1359 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301360 // Populate interfaces and properties that are common to every FRU
1361 // and additional interface that might be defined on a per-FRU
1362 // basis.
1363 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301364 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301365 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1366 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001367 if constexpr (is_same<T, Parsed>::value)
1368 {
1369 if (item["extraInterfaces"].find(
1370 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1371 item["extraInterfaces"].end())
1372 {
1373 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1374 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001375 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001376 }
1377 }
1378 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301379 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001380
1381 // embedded property(true or false) says whether the subfru is embedded
1382 // into the parent fru (or) not. VPD sets Present property only for
1383 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1384 // or may not be physically present. Those non embedded frus will always
1385 // have Present=false irrespective of its physical presence or absence.
1386 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1387 // Present to true for such sub frus.
1388 // Eg: ethernet port is embedded into bmc card. So set Present to true
1389 // for such sub frus. Also donot populate present property for embedded
1390 // subfru which is synthesized. Currently there is no subfru which are
1391 // both embedded and synthesized. But still the case is handled here.
1392 if ((item.value("embedded", true)) &&
1393 (!item.value("synthesized", false)))
1394 {
1395 inventory::PropertyMap presProp;
1396 presProp.emplace("Present", true);
1397 insertOrMerge(interfaces, invItemIntf, move(presProp));
1398 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001399
Santosh Puranikf3e69682022-03-31 17:52:38 +05301400 if constexpr (is_same<T, Parsed>::value)
1401 {
1402 // Restore asset tag, if needed
1403 if (processFactoryReset && objectPath == "/system")
1404 {
1405 fillAssetTag(interfaces, vpdMap);
1406 }
1407 }
1408
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301409 objects.emplace(move(object), move(interfaces));
1410 }
1411
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301412 if (isSystemVpd)
1413 {
1414 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1415 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001416
Alpana Kumarif05effd2021-04-07 07:32:53 -05001417 // set the U-boot environment variable for device-tree
1418 if constexpr (is_same<T, Parsed>::value)
1419 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301420 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001421 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301422 }
1423
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301424 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001425 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301426}
1427
1428int main(int argc, char** argv)
1429{
1430 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001431 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001432 Binary vpdVector{};
1433 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001434 // map to hold additional data in case of logging pel
1435 PelAdditionalData additionalData{};
1436
1437 // this is needed to hold base fru inventory path in case there is ECC or
1438 // vpd exception while parsing the file
1439 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301440
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001441 // severity for PEL
1442 PelSeverity pelSeverity = PelSeverity::WARNING;
1443
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301444 try
1445 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301446 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1447 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301448
1449 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001450 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301451
1452 CLI11_PARSE(app, argc, argv);
1453
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001454 // PEL severity should be ERROR in case of any system VPD failure
1455 if (file == systemVpdFilePath)
1456 {
1457 pelSeverity = PelSeverity::ERROR;
1458 }
1459
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301460 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1461
1462 // If the symlink exists, it means it has been setup for us, switch the
1463 // path
1464 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1465 {
1466 jsonToParse = INVENTORY_JSON_SYM_LINK;
1467 }
1468
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301469 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301470 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001471 if (!inventoryJson)
1472 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001473 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001474 }
1475
1476 try
1477 {
1478 js = json::parse(inventoryJson);
1479 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001480 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001481 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001482 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001483 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301484
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301485 // Do we have the mandatory "frus" section?
1486 if (js.find("frus") == js.end())
1487 {
1488 throw(VpdJsonException("FRUs section not found in JSON",
1489 jsonToParse));
1490 }
1491
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301492 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1493 if (file.find("/ahb:apb") != string::npos)
1494 {
1495 // Translate udev path to a generic /sys/bus/.. file path.
1496 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301497
1498 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301499 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301500 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001501 // We need manager service active to process restoring of
1502 // system VPD on hardware. So in case any system restore is
1503 // required, update hardware in the second trigger of parser
1504 // code for system vpd file path.
1505
1506 std::vector<std::string> interfaces{motherBoardInterface};
1507
1508 // call mapper to check for object path creation
1509 MapperResponse subTree =
1510 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1511 string mboardPath =
1512 js["frus"][file].at(0).value("inventoryPath", "");
1513
1514 // Attempt system VPD restore if we have a motherboard
1515 // object in the inventory.
1516 if ((subTree.size() != 0) &&
1517 (subTree.find(pimPath + mboardPath) != subTree.end()))
1518 {
1519 vpdVector = getVpdDataInVector(js, file);
1520 ParserInterface* parser =
1521 ParserFactory::getParser(vpdVector);
1522 variant<KeywordVpdMap, Store> parseResult;
1523 parseResult = parser->parse();
1524
1525 if (auto pVal = get_if<Store>(&parseResult))
1526 {
1527 // map to hold all the keywords whose value is blank and
1528 // needs to be updated at standby.
1529 vector<RestoredEeproms> blankSystemVpdProperties{};
1530 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1531 blankSystemVpdProperties);
1532
1533 // if system VPD restore is required, update the
1534 // EEPROM
1535 for (const auto& item : blankSystemVpdProperties)
1536 {
1537 updateHardware(get<0>(item), get<1>(item),
1538 get<2>(item), get<3>(item));
1539 }
1540 }
1541 else
1542 {
1543 std::cout << "Not a valid format to restore system VPD"
1544 << std::endl;
1545 }
1546 // release the parser object
1547 ParserFactory::freeParser(parser);
1548 }
1549 else
1550 {
1551 log<level::ERR>("No object path found");
1552 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301553 return 0;
1554 }
1555 }
1556
1557 if (file.empty())
1558 {
1559 cerr << "The EEPROM path <" << file << "> is not valid.";
1560 return 0;
1561 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301562 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301563 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301564 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301565 }
1566
Alpana Kumari2f793042020-08-18 05:51:03 -05001567 if (!fs::exists(file))
1568 {
1569 cout << "Device path: " << file
1570 << " does not exist. Spurious udev event? Exiting." << endl;
1571 return 0;
1572 }
1573
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001574 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301575 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001576 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301577 // 1) The eeprom we are trying to read is that of the system VPD and the
1578 // JSON symlink is already setup (the symlink's existence tells us we
1579 // are not coming out of a factory reset)
1580 // 2) The JSON tells us that the FRU EEPROM cannot be
1581 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001582 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301583 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301584 {
1585 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1586 getPowerState())
1587 {
1588 cout << "This VPD cannot be read when power is ON" << endl;
1589 return 0;
1590 }
1591 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001592
Santosh Puranike9c57532022-03-15 16:51:51 +05301593 // Check if this VPD should be recollected at all
1594 if (!needsRecollection(js, file))
1595 {
1596 cout << "Skip VPD recollection for: " << file << endl;
1597 return 0;
1598 }
1599
Alpana Kumari2f793042020-08-18 05:51:03 -05001600 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301601 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001602 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001603 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001604 variant<KeywordVpdMap, Store> parseResult;
1605 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001606
Alpana Kumari2f793042020-08-18 05:51:03 -05001607 if (auto pVal = get_if<Store>(&parseResult))
1608 {
1609 populateDbus(pVal->getVpdMap(), js, file);
1610 }
1611 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1612 {
1613 populateDbus(*pVal, js, file);
1614 }
1615
1616 // release the parser object
1617 ParserFactory::freeParser(parser);
1618 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001619 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001620 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001621 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001622 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001623 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301624 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001625 catch (const VpdJsonException& ex)
1626 {
1627 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1628 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001629 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001630
1631 cerr << ex.what() << "\n";
1632 rc = -1;
1633 }
1634 catch (const VpdEccException& ex)
1635 {
1636 additionalData.emplace("DESCRIPTION", "ECC check failed");
1637 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1638 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001639 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001640 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001641 cerr << ex.what() << "\n";
1642 rc = -1;
1643 }
1644 catch (const VpdDataException& ex)
1645 {
alpana075cb3b1f2021-12-16 11:19:36 -06001646 if (isThisPcieOnPass1planar(js, file))
1647 {
1648 cout << "Pcie_device [" << file
1649 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1650 rc = 0;
1651 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301652 else if (!(isPresent(js, file).value_or(true)))
1653 {
1654 cout << "FRU at: " << file
1655 << " is not detected present. Ignore parser error.\n";
1656 rc = 0;
1657 }
alpana075cb3b1f2021-12-16 11:19:36 -06001658 else
1659 {
1660 string errorMsg =
1661 "VPD file is either empty or invalid. Parser failed for [";
1662 errorMsg += file;
1663 errorMsg += "], with error = " + std::string(ex.what());
1664
1665 additionalData.emplace("DESCRIPTION", errorMsg);
1666 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1667 INVENTORY_PATH + baseFruInventoryPath);
1668 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1669
1670 rc = -1;
1671 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001672 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001673 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301674 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001675 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301676 cerr << e.what() << "\n";
1677 rc = -1;
1678 }
1679
1680 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001681}