blob: 43241bcf204e64e1595d0cadbe912bb71ae19d14 [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
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 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500206 cerr << "Failed to expand location code with exception: " << e.what()
207 << "\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 {
243 if (get_if<Binary>(&kwVal.second))
244 {
245 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
246 get_if<Binary>(&kwVal.second)->end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000247 prop.emplace(move(kw), move(vec));
248 }
249 else
250 {
251 if (kw == "MemorySizeInKB")
252 {
253 inventory::PropertyMap memProp;
254 auto memVal = get_if<size_t>(&kwVal.second);
255 if (memVal)
256 {
257 memProp.emplace(move(kw),
258 ((*memVal) * CONVERT_MB_TO_KB));
259 interfaces.emplace(
260 "xyz.openbmc_project.Inventory.Item.Dimm",
261 move(memProp));
262 }
263 else
264 {
265 cerr << "MemorySizeInKB value not found in vpd map\n";
266 }
267 }
268 }
269 }
270 else
271 {
272 Binary vec(kwVal.second.begin(), kwVal.second.end());
273 prop.emplace(move(kw), move(vec));
274 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530275 }
276
277 interfaces.emplace(preIntrStr, move(prop));
278}
279
280/**
281 * @brief Populate Interfaces.
282 *
283 * This method populates common and extra interfaces to dbus.
284 * @param[in] js - json object
285 * @param[out] interfaces - Reference to interface map
286 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530287 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530288 */
289template <typename T>
290static void populateInterfaces(const nlohmann::json& js,
291 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530292 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530293{
294 for (const auto& ifs : js.items())
295 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530296 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530297 inventory::PropertyMap props;
298
299 for (const auto& itr : ifs.value().items())
300 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530301 const string& busProp = itr.key();
302
Alpana Kumari31970de2020-02-17 06:49:57 -0600303 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530304 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530305 props.emplace(busProp, itr.value().get<bool>());
306 }
307 else if (itr.value().is_string())
308 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600309 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530310 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600311 std::string prop;
312 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530313 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000314 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600315 prop = expandLocationCode(itr.value().get<string>(),
316 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530317 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600318 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530319 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600320 // Send empty Parsed object to expandLocationCode api.
321 prop = expandLocationCode(itr.value().get<string>(),
322 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530323 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600324 props.emplace(busProp, prop);
325 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
326 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530327 }
328 else
329 {
330 props.emplace(busProp, itr.value().get<string>());
331 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530332 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530333 else if (itr.value().is_array())
334 {
335 try
336 {
337 props.emplace(busProp, itr.value().get<Binary>());
338 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500339 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530340 {
341 std::cerr << "Type exception: " << e.what() << "\n";
342 // Ignore any type errors
343 }
344 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600345 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530346 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600347 const string& rec = itr.value().value("recordName", "");
348 const string& kw = itr.value().value("keywordName", "");
349 const string& encoding = itr.value().value("encoding", "");
350
Alpana Kumari58e22142020-05-05 00:22:12 -0500351 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530352 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530353 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
354 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600355 {
356 auto encoded =
357 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530358 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600359 }
360 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500361 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600362 {
363 if (!kw.empty() && vpdMap.count(kw))
364 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000365 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
366 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
367
368 if (kwValue)
369 {
370 auto prop =
371 string((*kwValue).begin(), (*kwValue).end());
372
373 auto encoded = encodeKeyword(prop, encoding);
374
375 props.emplace(busProp, encoded);
376 }
377 else if (uintValue)
378 {
379 props.emplace(busProp, *uintValue);
380 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600381 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530382 }
383 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500384 else if (itr.value().is_number())
385 {
386 // For now assume the value is a size_t. In the future it would
387 // be nice to come up with a way to get the type from the JSON.
388 props.emplace(busProp, itr.value().get<size_t>());
389 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530390 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600391 insertOrMerge(interfaces, inf, move(props));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530392 }
393}
394
alpana075cb3b1f2021-12-16 11:19:36 -0600395/**
396 * @brief This API checks if this FRU is pcie_devices. If yes then it further
397 * checks whether it is PASS1 planar.
398 */
399static bool isThisPcieOnPass1planar(const nlohmann::json& js,
400 const string& file)
401{
402 auto isThisPCIeDev = false;
403 auto isPASS1 = false;
404
405 // Check if it is a PCIE device
406 if (js["frus"].find(file) != js["frus"].end())
407 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530408 if ((js["frus"][file].at(0).find("extraInterfaces") !=
409 js["frus"][file].at(0).end()))
alpana075cb3b1f2021-12-16 11:19:36 -0600410 {
Santosh Puranikc03f3902022-04-14 10:58:26 +0530411 if (js["frus"][file].at(0)["extraInterfaces"].find(
alpana075cb3b1f2021-12-16 11:19:36 -0600412 "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
Santosh Puranikc03f3902022-04-14 10:58:26 +0530413 js["frus"][file].at(0)["extraInterfaces"].end())
alpana075cb3b1f2021-12-16 11:19:36 -0600414 {
415 isThisPCIeDev = true;
416 }
417 }
418 }
419
420 if (isThisPCIeDev)
421 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500422 // Collect HW version and SystemType to know if it is PASS1 planar.
alpana075cb3b1f2021-12-16 11:19:36 -0600423 auto bus = sdbusplus::bus::new_default();
Alpana Kumaria6181e22022-05-12 05:01:53 -0500424 auto property1 = bus.new_method_call(
alpana075cb3b1f2021-12-16 11:19:36 -0600425 INVENTORY_MANAGER_SERVICE,
426 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
427 "org.freedesktop.DBus.Properties", "Get");
Alpana Kumaria6181e22022-05-12 05:01:53 -0500428 property1.append("com.ibm.ipzvpd.VINI");
429 property1.append("HW");
430 auto result1 = bus.call(property1);
431 inventory::Value hwVal;
432 result1.read(hwVal);
alpana075cb3b1f2021-12-16 11:19:36 -0600433
Alpana Kumaria6181e22022-05-12 05:01:53 -0500434 // SystemType
435 auto property2 = bus.new_method_call(
436 INVENTORY_MANAGER_SERVICE,
437 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
438 "org.freedesktop.DBus.Properties", "Get");
439 property2.append("com.ibm.ipzvpd.VSBP");
440 property2.append("IM");
441 auto result2 = bus.call(property2);
442 inventory::Value imVal;
443 result2.read(imVal);
444
445 auto pVal1 = get_if<Binary>(&hwVal);
446 auto pVal2 = get_if<Binary>(&imVal);
447
448 if (pVal1 && pVal2)
alpana075cb3b1f2021-12-16 11:19:36 -0600449 {
Alpana Kumaria6181e22022-05-12 05:01:53 -0500450 auto hwVersion = *pVal1;
451 auto systemType = *pVal2;
452
453 // IM kw for Everest
454 Binary everestSystem{80, 00, 48, 00};
455
456 if (systemType == everestSystem)
457 {
458 if (hwVersion[1] < 21)
459 {
460 isPASS1 = true;
461 }
462 }
463 else if (hwVersion[1] < 2)
464 {
alpana075cb3b1f2021-12-16 11:19:36 -0600465 isPASS1 = true;
Alpana Kumaria6181e22022-05-12 05:01:53 -0500466 }
alpana075cb3b1f2021-12-16 11:19:36 -0600467 }
468 }
469
470 return (isThisPCIeDev && isPASS1);
471}
472
Alpana Kumari735dee92022-03-25 01:24:40 -0500473/** Performs any pre-action needed to get the FRU setup for collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500474 *
475 * @param[in] json - json object
476 * @param[in] file - eeprom file path
477 */
478static void preAction(const nlohmann::json& json, const string& file)
479{
Alpana Kumari735dee92022-03-25 01:24:40 -0500480 if ((json["frus"][file].at(0)).find("preAction") ==
Alpana Kumari2f793042020-08-18 05:51:03 -0500481 json["frus"][file].at(0).end())
482 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500483 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500484 }
485
Alpana Kumari735dee92022-03-25 01:24:40 -0500486 if (executePreAction(json, file))
Alpana Kumari2f793042020-08-18 05:51:03 -0500487 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500488 if (json["frus"][file].at(0).find("devAddress") !=
489 json["frus"][file].at(0).end())
Alpana Kumari2f793042020-08-18 05:51:03 -0500490 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500491 // Now bind the device
492 string bind = json["frus"][file].at(0).value("devAddress", "");
493 cout << "Binding device " << bind << endl;
494 string bindCmd = string("echo \"") + bind +
495 string("\" > /sys/bus/i2c/drivers/at24/bind");
496 cout << bindCmd << endl;
497 executeCmd(bindCmd);
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600498
Alpana Kumari40d1c192022-03-09 21:16:02 -0600499 // Check if device showed up (test for file)
500 if (!fs::exists(file))
501 {
502 cerr << "EEPROM " << file
503 << " does not exist. Take failure action" << endl;
504 // If not, then take failure postAction
505 executePostFailAction(json, file);
506 }
507 }
508 else
Alpana Kumari735dee92022-03-25 01:24:40 -0500509 {
Alpana Kumari40d1c192022-03-09 21:16:02 -0600510 // missing required informations
511 cerr
512 << "VPD inventory JSON missing basic informations of preAction "
513 "for this FRU : ["
514 << file << "]. Executing executePostFailAction." << endl;
515
516 // Take failure postAction
Alpana Kumari735dee92022-03-25 01:24:40 -0500517 executePostFailAction(json, file);
Alpana Kumari40d1c192022-03-09 21:16:02 -0600518 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500519 }
520 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500521}
522
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530523/**
Santosh Puranikf3e69682022-03-31 17:52:38 +0530524 * @brief Fills the Decorator.AssetTag property into the interfaces map
525 *
526 * This function should only be called in cases where we did not find a JSON
527 * symlink. A missing symlink in /var/lib will be considered as a factory reset
528 * and this function will be used to default the AssetTag property.
529 *
530 * @param interfaces A possibly pre-populated map of inetrfaces to properties.
531 * @param vpdMap A VPD map of the system VPD data.
532 */
533static void fillAssetTag(inventory::InterfaceMap& interfaces,
534 const Parsed& vpdMap)
535{
536 // Read the system serial number and MTM
537 // Default asset tag is Server-MTM-System Serial
538 inventory::Interface assetIntf{
539 "xyz.openbmc_project.Inventory.Decorator.AssetTag"};
540 inventory::PropertyMap assetTagProps;
541 std::string defaultAssetTag =
542 std::string{"Server-"} + getKwVal(vpdMap, "VSYS", "TM") +
543 std::string{"-"} + getKwVal(vpdMap, "VSYS", "SE");
544 assetTagProps.emplace("AssetTag", defaultAssetTag);
545 insertOrMerge(interfaces, assetIntf, std::move(assetTagProps));
546}
547
548/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530549 * @brief Set certain one time properties in the inventory
550 * Use this function to insert the Functional and Enabled properties into the
551 * inventory map. This function first checks if the object in question already
552 * has these properties hosted on D-Bus, if the property is already there, it is
553 * not modified, hence the name "one time". If the property is not already
554 * present, it will be added to the map with a suitable default value (true for
555 * Functional and false for Enabled)
556 *
557 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
558 * @param[inout] interfaces - Reference to a map of inventory interfaces to
559 * which the properties will be attached.
560 */
561static void setOneTimeProperties(const std::string& object,
562 inventory::InterfaceMap& interfaces)
563{
564 auto bus = sdbusplus::bus::new_default();
565 auto objectPath = INVENTORY_PATH + object;
566 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
567 objectPath.c_str(),
568 "org.freedesktop.DBus.Properties", "Get");
569 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
570 prop.append("Functional");
571 try
572 {
573 auto result = bus.call(prop);
574 }
575 catch (const sdbusplus::exception::SdBusError& e)
576 {
577 // Treat as property unavailable
578 inventory::PropertyMap prop;
579 prop.emplace("Functional", true);
580 interfaces.emplace(
581 "xyz.openbmc_project.State.Decorator.OperationalStatus",
582 move(prop));
583 }
584 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
585 objectPath.c_str(),
586 "org.freedesktop.DBus.Properties", "Get");
587 prop.append("xyz.openbmc_project.Object.Enable");
588 prop.append("Enabled");
589 try
590 {
591 auto result = bus.call(prop);
592 }
593 catch (const sdbusplus::exception::SdBusError& e)
594 {
595 // Treat as property unavailable
596 inventory::PropertyMap prop;
597 prop.emplace("Enabled", false);
598 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
599 }
600}
601
602/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530603 * @brief Prime the Inventory
604 * Prime the inventory by populating only the location code,
605 * type interface and the inventory object for the frus
606 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530607 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530608 * @param[in] jsObject - Reference to vpd inventory json object
609 * @param[in] vpdMap - Reference to the parsed vpd map
610 *
611 * @returns Map of items in extraInterface.
612 */
613template <typename T>
614inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
615 const T& vpdMap)
616{
617 inventory::ObjectMap objects;
618
619 for (auto& itemFRUS : jsObject["frus"].items())
620 {
621 for (auto& itemEEPROM : itemFRUS.value())
622 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600623 // Take pre actions if needed
624 if (itemEEPROM.find("preAction") != itemEEPROM.end())
625 {
626 preAction(jsObject, itemFRUS.key());
627 }
628
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530629 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530630 inventory::Object object(itemEEPROM.at("inventoryPath"));
631
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530632 if ((itemFRUS.key() != systemVpdFilePath) &&
633 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530634 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600635 inventory::PropertyMap presProp;
Priyanga Ramasamye358acb2022-03-21 14:21:50 -0500636
637 // Do not populate Present property for frus whose
638 // synthesized=true. synthesized=true says the fru is owned by
639 // some other component and not by vpd.
640 if (!itemEEPROM.value("synthesized", false))
641 {
642 presProp.emplace("Present", false);
643 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
644 presProp);
645 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530646 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530647 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
648 {
649 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
650 {
651 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000652 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530653 {
654 if constexpr (std::is_same<T, Parsed>::value)
655 {
656 for (auto& lC : eI.value().items())
657 {
658 auto propVal = expandLocationCode(
659 lC.value().get<string>(), vpdMap, true);
660
661 props.emplace(move(lC.key()),
662 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530663 interfaces.emplace(XYZ_LOCATION_CODE_INF,
664 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530665 interfaces.emplace(move(eI.key()),
666 move(props));
667 }
668 }
669 }
670 else if (eI.key().find("Inventory.Item.") !=
671 string::npos)
672 {
673 interfaces.emplace(move(eI.key()), move(props));
674 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530675 else if (eI.key() ==
676 "xyz.openbmc_project.Inventory.Item")
677 {
678 for (auto& val : eI.value().items())
679 {
680 if (val.key() == "PrettyName")
681 {
682 presProp.emplace(val.key(),
683 val.value().get<string>());
684 }
685 }
686 // Use insert_or_assign here as we may already have
687 // inserted the present property only earlier in
688 // this function under this same interface.
689 interfaces.insert_or_assign(eI.key(),
690 move(presProp));
691 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530692 }
693 }
694 objects.emplace(move(object), move(interfaces));
695 }
696 }
697 }
698 return objects;
699}
700
Alpana Kumari65b83602020-09-01 00:24:56 -0500701/**
702 * @brief This API executes command to set environment variable
703 * And then reboot the system
704 * @param[in] key -env key to set new value
705 * @param[in] value -value to set.
706 */
707void setEnvAndReboot(const string& key, const string& value)
708{
709 // set env and reboot and break.
710 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600711 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500712 // make dbus call to reboot
713 auto bus = sdbusplus::bus::new_default_system();
714 auto method = bus.new_method_call(
715 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
716 "org.freedesktop.systemd1.Manager", "Reboot");
717 bus.call_noreply(method);
718}
719
720/*
721 * @brief This API checks for env var fitconfig.
722 * If not initialised OR updated as per the current system type,
723 * update this env var and reboot the system.
724 *
725 * @param[in] systemType IM kwd in vpd tells about which system type it is.
726 * */
727void setDevTreeEnv(const string& systemType)
728{
Alpana Kumari37e72702021-11-18 11:18:04 -0600729 // Init with default dtb
730 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530731 static const deviceTreeMap deviceTreeSystemTypeMap = {
732 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
733 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
734 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
735 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
736 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
Alpana Kumari1b026112022-03-02 23:41:38 -0600737 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"},
738 {EVEREST_V2, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500739
740 if (deviceTreeSystemTypeMap.find(systemType) !=
741 deviceTreeSystemTypeMap.end())
742 {
743 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
744 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600745 else
746 {
747 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600748 string err = "This System type not found/supported in dtb table " +
749 systemType +
750 ".Please check the HW and IM keywords in the system "
751 "VPD.Breaking...";
752
753 // map to hold additional data in case of logging pel
754 PelAdditionalData additionalData{};
755 additionalData.emplace("DESCRIPTION", err);
756 createPEL(additionalData, PelSeverity::WARNING,
757 errIntfForInvalidSystemType);
758 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600759 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500760
761 string readVarValue;
762 bool envVarFound = false;
763
764 vector<string> output = executeCmd("/sbin/fw_printenv");
765 for (const auto& entry : output)
766 {
767 size_t pos = entry.find("=");
768 string key = entry.substr(0, pos);
769 if (key != "fitconfig")
770 {
771 continue;
772 }
773
774 envVarFound = true;
775 if (pos + 1 < entry.size())
776 {
777 readVarValue = entry.substr(pos + 1);
778 if (readVarValue.find(newDeviceTree) != string::npos)
779 {
780 // fitconfig is Updated. No action needed
781 break;
782 }
783 }
784 // set env and reboot and break.
785 setEnvAndReboot(key, newDeviceTree);
786 exit(0);
787 }
788
789 // check If env var Not found
790 if (!envVarFound)
791 {
792 setEnvAndReboot("fitconfig", newDeviceTree);
793 }
794}
795
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530796/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500797 * @brief API to check if we need to restore system VPD
798 * This functionality is only applicable for IPZ VPD data.
799 * @param[in] vpdMap - IPZ vpd map
800 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500801 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600802void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500803{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500804 for (const auto& systemRecKwdPair : svpdKwdMap)
805 {
806 auto it = vpdMap.find(systemRecKwdPair.first);
807
808 // check if record is found in map we got by parser
809 if (it != vpdMap.end())
810 {
811 const auto& kwdListForRecord = systemRecKwdPair.second;
812 for (const auto& keyword : kwdListForRecord)
813 {
814 DbusPropertyMap& kwdValMap = it->second;
815 auto iterator = kwdValMap.find(keyword);
816
817 if (iterator != kwdValMap.end())
818 {
819 string& kwdValue = iterator->second;
820
821 // check bus data
822 const string& recordName = systemRecKwdPair.first;
823 const string& busValue = readBusProperty(
824 objectPath, ipzVpdInf + recordName, keyword);
825
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500826 std::string defaultValue{' '};
827
828 // Explicit check for D0 is required as this keyword will
829 // never be blank and 0x00 should be treated as no value in
830 // this case.
831 if (recordName == "UTIL" && keyword == "D0")
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500832 {
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500833 // default value of kwd D0 is 0x00. This kwd will never
834 // be blank.
835 defaultValue = '\0';
836 }
837
838 if (busValue.find_first_not_of(defaultValue) !=
839 string::npos)
840 {
841 if (kwdValue.find_first_not_of(defaultValue) !=
842 string::npos)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500843 {
844 // both the data are present, check for mismatch
845 if (busValue != kwdValue)
846 {
847 string errMsg = "VPD data mismatch on cache "
848 "and hardware for record: ";
849 errMsg += (*it).first;
850 errMsg += " and keyword: ";
851 errMsg += keyword;
852
853 // data mismatch
854 PelAdditionalData additionalData;
855 additionalData.emplace("CALLOUT_INVENTORY_PATH",
856 objectPath);
857
858 additionalData.emplace("DESCRIPTION", errMsg);
859
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500860 createPEL(additionalData, PelSeverity::WARNING,
861 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500862 }
863 }
864 else
865 {
866 // implies hardware data is blank
867 // update the map
868 Binary busData(busValue.begin(), busValue.end());
869
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500870 // update the map as well, so that cache data is not
871 // updated as blank while populating VPD map on Dbus
872 // in populateDBus Api
873 kwdValue = busValue;
874 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500875 }
Sunny Srivastavaa559c2d2022-05-02 11:56:45 -0500876 else if (kwdValue.find_first_not_of(defaultValue) ==
877 string::npos)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500878 {
879 string errMsg = "VPD is blank on both cache and "
880 "hardware for record: ";
881 errMsg += (*it).first;
882 errMsg += " and keyword: ";
883 errMsg += keyword;
884 errMsg += ". SSR need to update hardware VPD.";
885
886 // both the data are blanks, log PEL
887 PelAdditionalData additionalData;
888 additionalData.emplace("CALLOUT_INVENTORY_PATH",
889 objectPath);
890
891 additionalData.emplace("DESCRIPTION", errMsg);
892
893 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500894 createPEL(additionalData, PelSeverity::ERROR,
895 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500896 continue;
897 }
898 }
899 }
900 }
901 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500902}
903
904/**
alpana077ce68722021-07-25 13:23:59 -0500905 * @brief This checks for is this FRU a processor
906 * And if yes, then checks for is this primary
907 *
908 * @param[in] js- vpd json to get the information about this FRU
909 * @param[in] filePath- FRU vpd
910 *
911 * @return true/false
912 */
913bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
914{
915 bool isProcessor = false;
916 bool isPrimary = false;
917
918 for (const auto& item : js["frus"][filePath])
919 {
920 if (item.find("extraInterfaces") != item.end())
921 {
922 for (const auto& eI : item["extraInterfaces"].items())
923 {
924 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
925 {
926 isProcessor = true;
927 }
928 }
929 }
930
931 if (isProcessor)
932 {
933 string cpuType = item.value("cpuType", "");
934 if (cpuType == "primary")
935 {
936 isPrimary = true;
937 }
938 }
939 }
940
941 return (isProcessor && isPrimary);
942}
943
944/**
945 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
946 * driver
947 * @param[in] js- vpd json to iterate through and take action if it is DIMM
948 */
949void doEnableAllDimms(nlohmann::json& js)
950{
951 // iterate over each fru
952 for (const auto& eachFru : js["frus"].items())
953 {
954 // skip the driver binding if eeprom already exists
955 if (fs::exists(eachFru.key()))
956 {
957 continue;
958 }
959
960 for (const auto& eachInventory : eachFru.value())
961 {
962 if (eachInventory.find("extraInterfaces") != eachInventory.end())
963 {
964 for (const auto& eI : eachInventory["extraInterfaces"].items())
965 {
966 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
967 {
968 string dimmVpd = eachFru.key();
969 // fetch it from
970 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
971
972 regex matchPatern("([0-9]+-[0-9]{4})");
973 smatch matchFound;
974 if (regex_search(dimmVpd, matchFound, matchPatern))
975 {
976 vector<string> i2cReg;
977 boost::split(i2cReg, matchFound.str(0),
978 boost::is_any_of("-"));
979
980 // remove 0s from begining
981 const regex pattern("^0+(?!$)");
982 for (auto& i : i2cReg)
983 {
984 i = regex_replace(i, pattern, "");
985 }
986
987 if (i2cReg.size() == 2)
988 {
989 // echo 24c32 0x50 >
990 // /sys/bus/i2c/devices/i2c-16/new_device
991 string cmnd = "echo 24c32 0x" + i2cReg[1] +
992 " > /sys/bus/i2c/devices/i2c-" +
993 i2cReg[0] + "/new_device";
994
995 executeCmd(cmnd);
996 }
997 }
998 }
999 }
1000 }
1001 }
1002 }
1003}
1004
1005/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001006 * @brief Check if the given CPU is an IO only chip.
1007 * The CPU is termed as IO, whose all of the cores are bad and can never be
1008 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
1009 * etc., The CPU whose every cores are bad, can be identified from the CP00
1010 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1011 * has 4 cores grouped together by sharing its cache memory.)
1012 * @param [in] pgKeyword - PG Keyword of CPU.
1013 * @return true if the given cpu is an IO, false otherwise.
1014 */
1015static bool isCPUIOGoodOnly(const string& pgKeyword)
1016{
1017 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1018 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1019 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1020 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1021 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1022 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1023 // IO.
1024 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1025 {
1026 return true;
1027 }
1028
1029 // The CPU is not an IO
1030 return false;
1031}
1032
1033/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301034 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301035 * This method invokes all the populateInterface functions
1036 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301037 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1038 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301039 * @param[in] js - Inventory json object
1040 * @param[in] filePath - Path of the vpd file
1041 * @param[in] preIntrStr - Interface string
1042 */
1043template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001044static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301045{
1046 inventory::InterfaceMap interfaces;
1047 inventory::ObjectMap objects;
1048 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001049 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301050
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301051 bool isSystemVpd = (filePath == systemVpdFilePath);
1052 if constexpr (is_same<T, Parsed>::value)
1053 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001054 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1055 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1056 ::toupper);
1057
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301058 if (isSystemVpd)
1059 {
1060 std::vector<std::string> interfaces = {motherBoardInterface};
1061 // call mapper to check for object path creation
1062 MapperResponse subTree =
1063 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1064 string mboardPath =
1065 js["frus"][filePath].at(0).value("inventoryPath", "");
1066
1067 // Attempt system VPD restore if we have a motherboard
1068 // object in the inventory.
1069 if ((subTree.size() != 0) &&
1070 (subTree.find(pimPath + mboardPath) != subTree.end()))
1071 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001072 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301073 }
1074 else
1075 {
1076 log<level::ERR>("No object path found");
1077 }
1078 }
alpana077ce68722021-07-25 13:23:59 -05001079 else
1080 {
1081 // check if it is processor vpd.
1082 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1083
1084 if (isPrimaryCpu)
1085 {
1086 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1087
1088 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1089
1090 if (chipVersion >= 2)
1091 {
1092 doEnableAllDimms(js);
1093 }
1094 }
1095 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301096 }
1097
Santosh Puranikf3e69682022-03-31 17:52:38 +05301098 auto processFactoryReset = false;
1099
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001100 if (isSystemVpd)
1101 {
1102 string systemJsonName{};
1103 if constexpr (is_same<T, Parsed>::value)
1104 {
1105 // pick the right system json
1106 systemJsonName = getSystemsJson(vpdMap);
1107 }
1108
1109 fs::path target = systemJsonName;
1110 fs::path link = INVENTORY_JSON_SYM_LINK;
1111
Santosh Puranikf3e69682022-03-31 17:52:38 +05301112 // If the symlink does not exist, we treat that as a factory reset
1113 processFactoryReset = !fs::exists(INVENTORY_JSON_SYM_LINK);
1114
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001115 // Create the directory for hosting the symlink
1116 fs::create_directories(VPD_FILES_PATH);
1117 // unlink the symlink previously created (if any)
1118 remove(INVENTORY_JSON_SYM_LINK);
1119 // create a new symlink based on the system
1120 fs::create_symlink(target, link);
1121
1122 // Reloading the json
1123 ifstream inventoryJson(link);
1124 js = json::parse(inventoryJson);
1125 inventoryJson.close();
1126 }
1127
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301128 for (const auto& item : js["frus"][filePath])
1129 {
1130 const auto& objectPath = item["inventoryPath"];
1131 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001132
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001133 vector<string> ccinList;
1134 if (item.find("ccin") != item.end())
1135 {
1136 for (const auto& cc : item["ccin"])
1137 {
1138 string ccin = cc;
1139 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1140 ccinList.push_back(ccin);
1141 }
1142 }
1143
1144 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1145 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1146 ccinList.end()))
1147 {
1148 continue;
1149 }
1150
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001151 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301152 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001153
1154 // Populate one time properties for the system VPD and its sub-frus
1155 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301156 // For the remaining FRUs, this will get handled as a part of
1157 // priming the inventory.
1158 setOneTimeProperties(objectPath, interfaces);
1159 }
1160
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301161 // Populate the VPD keywords and the common interfaces only if we
1162 // are asked to inherit that data from the VPD, else only add the
1163 // extraInterfaces.
1164 if (item.value("inherit", true))
1165 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001166 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301167 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301168 // Each record in the VPD becomes an interface and all
1169 // keyword within the record are properties under that
1170 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301171 for (const auto& record : vpdMap)
1172 {
1173 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001174 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301175 }
1176 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001177 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301178 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001179 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301180 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301181 if (js.find("commonInterfaces") != js.end())
1182 {
1183 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1184 isSystemVpd);
1185 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301186 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001187 else
1188 {
1189 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001190 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001191 {
1192 if (item.find("copyRecords") != item.end())
1193 {
1194 for (const auto& record : item["copyRecords"])
1195 {
1196 const string& recordName = record;
1197 if (vpdMap.find(recordName) != vpdMap.end())
1198 {
1199 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001200 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001201 interfaces);
1202 }
1203 }
1204 }
1205 }
1206 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301207 // Populate interfaces and properties that are common to every FRU
1208 // and additional interface that might be defined on a per-FRU
1209 // basis.
1210 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301211 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301212 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1213 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001214 if constexpr (is_same<T, Parsed>::value)
1215 {
1216 if (item["extraInterfaces"].find(
1217 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1218 item["extraInterfaces"].end())
1219 {
1220 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1221 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001222 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001223 }
1224 }
1225 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301226 }
Priyanga Ramasamye358acb2022-03-21 14:21:50 -05001227
1228 // embedded property(true or false) says whether the subfru is embedded
1229 // into the parent fru (or) not. VPD sets Present property only for
1230 // embedded frus. If the subfru is not an embedded FRU, the subfru may
1231 // or may not be physically present. Those non embedded frus will always
1232 // have Present=false irrespective of its physical presence or absence.
1233 // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set
1234 // Present to true for such sub frus.
1235 // Eg: ethernet port is embedded into bmc card. So set Present to true
1236 // for such sub frus. Also donot populate present property for embedded
1237 // subfru which is synthesized. Currently there is no subfru which are
1238 // both embedded and synthesized. But still the case is handled here.
1239 if ((item.value("embedded", true)) &&
1240 (!item.value("synthesized", false)))
1241 {
1242 inventory::PropertyMap presProp;
1243 presProp.emplace("Present", true);
1244 insertOrMerge(interfaces, invItemIntf, move(presProp));
1245 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001246
Santosh Puranikf3e69682022-03-31 17:52:38 +05301247 if constexpr (is_same<T, Parsed>::value)
1248 {
1249 // Restore asset tag, if needed
1250 if (processFactoryReset && objectPath == "/system")
1251 {
1252 fillAssetTag(interfaces, vpdMap);
1253 }
1254 }
1255
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301256 objects.emplace(move(object), move(interfaces));
1257 }
1258
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301259 if (isSystemVpd)
1260 {
1261 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1262 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001263
Alpana Kumarif05effd2021-04-07 07:32:53 -05001264 // set the U-boot environment variable for device-tree
1265 if constexpr (is_same<T, Parsed>::value)
1266 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301267 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001268 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301269 }
1270
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301271 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001272 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301273}
1274
1275int main(int argc, char** argv)
1276{
1277 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001278 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001279 Binary vpdVector{};
1280 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001281 // map to hold additional data in case of logging pel
1282 PelAdditionalData additionalData{};
1283
1284 // this is needed to hold base fru inventory path in case there is ECC or
1285 // vpd exception while parsing the file
1286 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301287
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001288 // severity for PEL
1289 PelSeverity pelSeverity = PelSeverity::WARNING;
1290
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301291 try
1292 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301293 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1294 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301295
1296 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001297 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301298
1299 CLI11_PARSE(app, argc, argv);
1300
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001301 // PEL severity should be ERROR in case of any system VPD failure
1302 if (file == systemVpdFilePath)
1303 {
1304 pelSeverity = PelSeverity::ERROR;
1305 }
1306
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301307 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1308
1309 // If the symlink exists, it means it has been setup for us, switch the
1310 // path
1311 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1312 {
1313 jsonToParse = INVENTORY_JSON_SYM_LINK;
1314 }
1315
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301316 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301317 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001318 if (!inventoryJson)
1319 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001320 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001321 }
1322
1323 try
1324 {
1325 js = json::parse(inventoryJson);
1326 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001327 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001328 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001329 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001330 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301331
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301332 // Do we have the mandatory "frus" section?
1333 if (js.find("frus") == js.end())
1334 {
1335 throw(VpdJsonException("FRUs section not found in JSON",
1336 jsonToParse));
1337 }
1338
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301339 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1340 if (file.find("/ahb:apb") != string::npos)
1341 {
1342 // Translate udev path to a generic /sys/bus/.. file path.
1343 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301344
1345 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301346 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301347 {
Santosh Puranik6b2b5372022-06-02 20:49:02 +05301348 // We have already collected system VPD, skip.
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301349 return 0;
1350 }
1351 }
1352
1353 if (file.empty())
1354 {
1355 cerr << "The EEPROM path <" << file << "> is not valid.";
1356 return 0;
1357 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301358 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301359 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301360 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301361 }
1362
Alpana Kumari2f793042020-08-18 05:51:03 -05001363 if (!fs::exists(file))
1364 {
1365 cout << "Device path: " << file
1366 << " does not exist. Spurious udev event? Exiting." << endl;
1367 return 0;
1368 }
1369
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001370 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301371 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001372 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301373 // 1) The eeprom we are trying to read is that of the system VPD and the
1374 // JSON symlink is already setup (the symlink's existence tells us we
1375 // are not coming out of a factory reset)
1376 // 2) The JSON tells us that the FRU EEPROM cannot be
1377 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001378 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301379 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301380 {
1381 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1382 getPowerState())
1383 {
1384 cout << "This VPD cannot be read when power is ON" << endl;
1385 return 0;
1386 }
1387 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001388
Santosh Puranike9c57532022-03-15 16:51:51 +05301389 // Check if this VPD should be recollected at all
1390 if (!needsRecollection(js, file))
1391 {
1392 cout << "Skip VPD recollection for: " << file << endl;
1393 return 0;
1394 }
1395
Alpana Kumari2f793042020-08-18 05:51:03 -05001396 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301397 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001398 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001399 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001400 variant<KeywordVpdMap, Store> parseResult;
1401 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001402
Alpana Kumari2f793042020-08-18 05:51:03 -05001403 if (auto pVal = get_if<Store>(&parseResult))
1404 {
1405 populateDbus(pVal->getVpdMap(), js, file);
1406 }
1407 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1408 {
1409 populateDbus(*pVal, js, file);
1410 }
1411
1412 // release the parser object
1413 ParserFactory::freeParser(parser);
1414 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001415 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001416 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001417 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001418 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001419 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301420 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001421 catch (const VpdJsonException& ex)
1422 {
1423 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1424 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001425 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001426
1427 cerr << ex.what() << "\n";
1428 rc = -1;
1429 }
1430 catch (const VpdEccException& ex)
1431 {
1432 additionalData.emplace("DESCRIPTION", "ECC check failed");
1433 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1434 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001435 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001436 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001437 cerr << ex.what() << "\n";
1438 rc = -1;
1439 }
1440 catch (const VpdDataException& ex)
1441 {
alpana075cb3b1f2021-12-16 11:19:36 -06001442 if (isThisPcieOnPass1planar(js, file))
1443 {
1444 cout << "Pcie_device [" << file
1445 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1446 rc = 0;
1447 }
Santosh Puranik53b38ed2022-04-10 23:15:22 +05301448 else if (!(isPresent(js, file).value_or(true)))
1449 {
1450 cout << "FRU at: " << file
1451 << " is not detected present. Ignore parser error.\n";
1452 rc = 0;
1453 }
alpana075cb3b1f2021-12-16 11:19:36 -06001454 else
1455 {
1456 string errorMsg =
1457 "VPD file is either empty or invalid. Parser failed for [";
1458 errorMsg += file;
1459 errorMsg += "], with error = " + std::string(ex.what());
1460
1461 additionalData.emplace("DESCRIPTION", errorMsg);
1462 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1463 INVENTORY_PATH + baseFruInventoryPath);
1464 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1465
1466 rc = -1;
1467 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001468 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001469 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301470 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001471 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301472 cerr << e.what() << "\n";
1473 rc = -1;
1474 }
1475
1476 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001477}