blob: 91e5bd97f2e0f8191402e58d63f96f125a97b2e6 [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05003#include "common_utility.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05304#include "defines.hpp"
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05005#include "ibm_vpd_utils.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05006#include "ipz_parser.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05307#include "keyword_vpd_parser.hpp"
Alpana Kumaria00936f2020-04-14 07:15:46 -05008#include "memory_vpd_parser.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05009#include "parser_factory.hpp"
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050010#include "vpd_exceptions.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053011
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012#include <assert.h>
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -050013#include <ctype.h>
14
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053015#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053016#include <algorithm>
alpana077ce68722021-07-25 13:23:59 -050017#include <boost/algorithm/string.hpp>
Alpana Kumari65b83602020-09-01 00:24:56 -050018#include <cstdarg>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053019#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053020#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053021#include <fstream>
Alpana Kumari2f793042020-08-18 05:51:03 -050022#include <gpiod.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053023#include <iostream>
24#include <iterator>
25#include <nlohmann/json.hpp>
Andrew Geissler280197e2020-12-08 20:51:49 -060026#include <phosphor-logging/log.hpp>
alpana077ce68722021-07-25 13:23:59 -050027#include <regex>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053028
29using namespace std;
30using namespace openpower::vpd;
31using namespace CLI;
32using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053033using namespace openpower::vpd::constants;
34namespace fs = filesystem;
35using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050036using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050037using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050038using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050039using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050040using namespace openpower::vpd::exceptions;
Andrew Geissler280197e2020-12-08 20:51:49 -060041using namespace phosphor::logging;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053042
Sunny Srivastava3c244142022-01-11 08:47:04 -060043// Map to hold record, kwd pair which can be re-stored at standby.
44// The list of keywords for VSYS record is as per the S0 system. Should
45// be updated for another type of systems
46static const std::unordered_map<std::string, std::vector<std::string>>
Sunny Srivastava01e6c632022-02-28 03:38:46 -060047 svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN"}},
Sunny Srivastava3c244142022-01-11 08:47:04 -060048 {"VCEN", {"FC", "SE"}},
49 {"LXR0", {"LX"}}};
50
Santosh Puranik88edeb62020-03-02 12:00:09 +053051/**
Santosh Puranik85893752020-11-10 21:31:43 +053052 * @brief Returns the power state for chassis0
53 */
54static auto getPowerState()
55{
56 // TODO: How do we handle multiple chassis?
57 string powerState{};
58 auto bus = sdbusplus::bus::new_default();
59 auto properties =
60 bus.new_method_call("xyz.openbmc_project.State.Chassis",
61 "/xyz/openbmc_project/state/chassis0",
62 "org.freedesktop.DBus.Properties", "Get");
63 properties.append("xyz.openbmc_project.State.Chassis");
64 properties.append("CurrentPowerState");
65 auto result = bus.call(properties);
66 if (!result.is_method_error())
67 {
68 variant<string> val;
69 result.read(val);
70 if (auto pVal = get_if<string>(&val))
71 {
72 powerState = *pVal;
73 }
74 }
75 cout << "Power state is: " << powerState << endl;
76 return powerState;
77}
78
79/**
Santosh Puranik88edeb62020-03-02 12:00:09 +053080 * @brief Expands location codes
81 */
82static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
83 bool isSystemVpd)
84{
85 auto expanded{unexpanded};
86 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
87 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
88 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
89 size_t idx = expanded.find("fcs");
90 try
91 {
92 if (idx != string::npos)
93 {
94 string fc{};
95 string se{};
96 if (isSystemVpd)
97 {
98 const auto& fcData = vpdMap.at("VCEN").at("FC");
99 const auto& seData = vpdMap.at("VCEN").at("SE");
100 fc = string(fcData.data(), fcData.size());
101 se = string(seData.data(), seData.size());
102 }
103 else
104 {
105 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
106 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
107 }
108
Alpana Kumari81671f62021-02-10 02:21:59 -0600109 // TODO: See if ND0 can be placed in the JSON
110 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530111 }
112 else
113 {
114 idx = expanded.find("mts");
115 if (idx != string::npos)
116 {
117 string mt{};
118 string se{};
119 if (isSystemVpd)
120 {
121 const auto& mtData = vpdMap.at("VSYS").at("TM");
122 const auto& seData = vpdMap.at("VSYS").at("SE");
123 mt = string(mtData.data(), mtData.size());
124 se = string(seData.data(), seData.size());
125 }
126 else
127 {
128 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
129 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
130 }
131
132 replace(mt.begin(), mt.end(), '-', '.');
133 expanded.replace(idx, 3, mt + "." + se);
134 }
135 }
136 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500137 catch (const exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530138 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500139 cerr << "Failed to expand location code with exception: " << e.what()
140 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530141 }
142 return expanded;
143}
Alpana Kumari2f793042020-08-18 05:51:03 -0500144
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530145/**
146 * @brief Populate FRU specific interfaces.
147 *
148 * This is a common method which handles both
149 * ipz and keyword specific interfaces thus,
150 * reducing the code redundancy.
151 * @param[in] map - Reference to the innermost keyword-value map.
152 * @param[in] preIntrStr - Reference to the interface string.
153 * @param[out] interfaces - Reference to interface map.
154 */
155template <typename T>
156static void populateFruSpecificInterfaces(const T& map,
157 const string& preIntrStr,
158 inventory::InterfaceMap& interfaces)
159{
160 inventory::PropertyMap prop;
161
162 for (const auto& kwVal : map)
163 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530164 auto kw = kwVal.first;
165
166 if (kw[0] == '#')
167 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500168 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530169 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500170 else if (isdigit(kw[0]))
171 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500172 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500173 }
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000174 if constexpr (is_same<T, KeywordVpdMap>::value)
175 {
176 if (get_if<Binary>(&kwVal.second))
177 {
178 Binary vec(get_if<Binary>(&kwVal.second)->begin(),
179 get_if<Binary>(&kwVal.second)->end());
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000180 prop.emplace(move(kw), move(vec));
181 }
182 else
183 {
184 if (kw == "MemorySizeInKB")
185 {
186 inventory::PropertyMap memProp;
187 auto memVal = get_if<size_t>(&kwVal.second);
188 if (memVal)
189 {
190 memProp.emplace(move(kw),
191 ((*memVal) * CONVERT_MB_TO_KB));
192 interfaces.emplace(
193 "xyz.openbmc_project.Inventory.Item.Dimm",
194 move(memProp));
195 }
196 else
197 {
198 cerr << "MemorySizeInKB value not found in vpd map\n";
199 }
200 }
201 }
202 }
203 else
204 {
205 Binary vec(kwVal.second.begin(), kwVal.second.end());
206 prop.emplace(move(kw), move(vec));
207 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530208 }
209
210 interfaces.emplace(preIntrStr, move(prop));
211}
212
213/**
214 * @brief Populate Interfaces.
215 *
216 * This method populates common and extra interfaces to dbus.
217 * @param[in] js - json object
218 * @param[out] interfaces - Reference to interface map
219 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530220 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530221 */
222template <typename T>
223static void populateInterfaces(const nlohmann::json& js,
224 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530225 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530226{
227 for (const auto& ifs : js.items())
228 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530229 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530230 inventory::PropertyMap props;
231
232 for (const auto& itr : ifs.value().items())
233 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530234 const string& busProp = itr.key();
235
Alpana Kumari31970de2020-02-17 06:49:57 -0600236 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530237 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530238 props.emplace(busProp, itr.value().get<bool>());
239 }
240 else if (itr.value().is_string())
241 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600242 if (busProp == "LocationCode" && inf == IBM_LOCATION_CODE_INF)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530243 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600244 std::string prop;
245 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530246 {
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000247 // TODO deprecate the com.ibm interface later
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600248 prop = expandLocationCode(itr.value().get<string>(),
249 vpdMap, isSystemVpd);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530250 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600251 else if constexpr (is_same<T, KeywordVpdMap>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530252 {
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600253 // Send empty Parsed object to expandLocationCode api.
254 prop = expandLocationCode(itr.value().get<string>(),
255 Parsed{}, false);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530256 }
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -0600257 props.emplace(busProp, prop);
258 interfaces.emplace(XYZ_LOCATION_CODE_INF, props);
259 interfaces.emplace(IBM_LOCATION_CODE_INF, props);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530260 }
261 else
262 {
263 props.emplace(busProp, itr.value().get<string>());
264 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530265 }
Santosh Puraniked609af2021-06-21 11:30:07 +0530266 else if (itr.value().is_array())
267 {
268 try
269 {
270 props.emplace(busProp, itr.value().get<Binary>());
271 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500272 catch (const nlohmann::detail::type_error& e)
Santosh Puraniked609af2021-06-21 11:30:07 +0530273 {
274 std::cerr << "Type exception: " << e.what() << "\n";
275 // Ignore any type errors
276 }
277 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600278 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530279 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600280 const string& rec = itr.value().value("recordName", "");
281 const string& kw = itr.value().value("keywordName", "");
282 const string& encoding = itr.value().value("encoding", "");
283
Alpana Kumari58e22142020-05-05 00:22:12 -0500284 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530285 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530286 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
287 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600288 {
289 auto encoded =
290 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530291 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600292 }
293 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500294 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600295 {
296 if (!kw.empty() && vpdMap.count(kw))
297 {
Alpana Kumari3ab26a72021-04-05 19:09:19 +0000298 auto kwValue = get_if<Binary>(&vpdMap.at(kw));
299 auto uintValue = get_if<size_t>(&vpdMap.at(kw));
300
301 if (kwValue)
302 {
303 auto prop =
304 string((*kwValue).begin(), (*kwValue).end());
305
306 auto encoded = encodeKeyword(prop, encoding);
307
308 props.emplace(busProp, encoded);
309 }
310 else if (uintValue)
311 {
312 props.emplace(busProp, *uintValue);
313 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600314 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530315 }
316 }
Matt Spinlerb1e64bb2021-09-08 09:57:48 -0500317 else if (itr.value().is_number())
318 {
319 // For now assume the value is a size_t. In the future it would
320 // be nice to come up with a way to get the type from the JSON.
321 props.emplace(busProp, itr.value().get<size_t>());
322 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530323 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600324 insertOrMerge(interfaces, inf, move(props));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530325 }
326}
327
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600328/*API to reset EEPROM pointer to a safe position to avoid VPD corruption.
329 * Currently do reset only for DIMM VPD.*/
330static void resetEEPROMPointer(const nlohmann::json& js, const string& file,
331 ifstream& vpdFile)
332{
333 for (const auto& item : js["frus"][file])
334 {
335 if (item.find("extraInterfaces") != item.end())
336 {
337 if (item["extraInterfaces"].find(
338 "xyz.openbmc_project.Inventory.Item.Dimm") !=
339 item["extraInterfaces"].end())
340 {
341 // moves the EEPROM pointer to 2048 'th byte.
342 vpdFile.seekg(2047, std::ios::beg);
343 // Read that byte and discard - to affirm the move
344 // operation.
345 char ch;
346 vpdFile.read(&ch, sizeof(ch));
347 }
348 return;
349 }
350 }
351}
352
alpana075cb3b1f2021-12-16 11:19:36 -0600353/**
354 * @brief This API checks if this FRU is pcie_devices. If yes then it further
355 * checks whether it is PASS1 planar.
356 */
357static bool isThisPcieOnPass1planar(const nlohmann::json& js,
358 const string& file)
359{
360 auto isThisPCIeDev = false;
361 auto isPASS1 = false;
362
363 // Check if it is a PCIE device
364 if (js["frus"].find(file) != js["frus"].end())
365 {
366 if ((js["frus"][file].find("extraInterfaces") !=
367 js["frus"][file].end()))
368 {
369 if (js["frus"][file]["extraInterfaces"].find(
370 "xyz.openbmc_project.Inventory.Item.PCIeDevice") !=
371 js["frus"][file]["extraInterfaces"].end())
372 {
373 isThisPCIeDev = true;
374 }
375 }
376 }
377
378 if (isThisPCIeDev)
379 {
380 // Collect SystemType to know if it is PASS1 planar.
381 auto bus = sdbusplus::bus::new_default();
382 auto properties = bus.new_method_call(
383 INVENTORY_MANAGER_SERVICE,
384 "/xyz/openbmc_project/inventory/system/chassis/motherboard",
385 "org.freedesktop.DBus.Properties", "Get");
386 properties.append("com.ibm.ipzvpd.VINI");
387 properties.append("HW");
388 auto result = bus.call(properties);
389
390 inventory::Value val;
391 result.read(val);
392 if (auto pVal = get_if<Binary>(&val))
393 {
394 auto hwVersion = *pVal;
395 if (hwVersion[1] < 2)
396 isPASS1 = true;
397 }
398 }
399
400 return (isThisPCIeDev && isPASS1);
401}
402
Alpana Kumari2f793042020-08-18 05:51:03 -0500403static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500404{
405 uint32_t offset = 0;
406 // check if offset present?
407 for (const auto& item : js["frus"][file])
408 {
409 if (item.find("offset") != item.end())
410 {
411 offset = item["offset"];
412 }
413 }
414
415 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600416 auto maxVPDSize = std::min(std::filesystem::file_size(file),
417 static_cast<uintmax_t>(65504));
418
Alpana Kumari58e22142020-05-05 00:22:12 -0500419 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600420 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500421 ifstream vpdFile;
422 vpdFile.open(file, ios::binary);
423
424 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600425 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500426 vpdVector.resize(vpdFile.gcount());
427
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600428 resetEEPROMPointer(js, file, vpdFile);
429
Alpana Kumari58e22142020-05-05 00:22:12 -0500430 return vpdVector;
431}
432
Alpana Kumari735dee92022-03-25 01:24:40 -0500433/** Performs any pre-action needed to get the FRU setup for collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500434 *
435 * @param[in] json - json object
436 * @param[in] file - eeprom file path
437 */
438static void preAction(const nlohmann::json& json, const string& file)
439{
Alpana Kumari735dee92022-03-25 01:24:40 -0500440 if ((json["frus"][file].at(0)).find("preAction") ==
Alpana Kumari2f793042020-08-18 05:51:03 -0500441 json["frus"][file].at(0).end())
442 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500443 return;
Alpana Kumari2f793042020-08-18 05:51:03 -0500444 }
445
Alpana Kumari735dee92022-03-25 01:24:40 -0500446 if (executePreAction(json, file))
Alpana Kumari2f793042020-08-18 05:51:03 -0500447 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500448 if (json["frus"][file].at(0).find("devAddress") !=
449 json["frus"][file].at(0).end())
Alpana Kumari2f793042020-08-18 05:51:03 -0500450 {
Alpana Kumari735dee92022-03-25 01:24:40 -0500451 // Now bind the device
452 string bind = json["frus"][file].at(0).value("devAddress", "");
453 cout << "Binding device " << bind << endl;
454 string bindCmd = string("echo \"") + bind +
455 string("\" > /sys/bus/i2c/drivers/at24/bind");
456 cout << bindCmd << endl;
457 executeCmd(bindCmd);
458 }
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600459
Alpana Kumari735dee92022-03-25 01:24:40 -0500460 // Check if device showed up (test for file)
461 if (!fs::exists(file))
462 {
463 cout << "EEPROM " << file << " does not exist. Take failure action"
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600464 << endl;
Alpana Kumari735dee92022-03-25 01:24:40 -0500465 // If not, then take failure postAction
466 executePostFailAction(json, file);
Alpana Kumari2f793042020-08-18 05:51:03 -0500467 }
468 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500469}
470
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530471/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530472 * @brief Set certain one time properties in the inventory
473 * Use this function to insert the Functional and Enabled properties into the
474 * inventory map. This function first checks if the object in question already
475 * has these properties hosted on D-Bus, if the property is already there, it is
476 * not modified, hence the name "one time". If the property is not already
477 * present, it will be added to the map with a suitable default value (true for
478 * Functional and false for Enabled)
479 *
480 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
481 * @param[inout] interfaces - Reference to a map of inventory interfaces to
482 * which the properties will be attached.
483 */
484static void setOneTimeProperties(const std::string& object,
485 inventory::InterfaceMap& interfaces)
486{
487 auto bus = sdbusplus::bus::new_default();
488 auto objectPath = INVENTORY_PATH + object;
489 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
490 objectPath.c_str(),
491 "org.freedesktop.DBus.Properties", "Get");
492 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
493 prop.append("Functional");
494 try
495 {
496 auto result = bus.call(prop);
497 }
498 catch (const sdbusplus::exception::SdBusError& e)
499 {
500 // Treat as property unavailable
501 inventory::PropertyMap prop;
502 prop.emplace("Functional", true);
503 interfaces.emplace(
504 "xyz.openbmc_project.State.Decorator.OperationalStatus",
505 move(prop));
506 }
507 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
508 objectPath.c_str(),
509 "org.freedesktop.DBus.Properties", "Get");
510 prop.append("xyz.openbmc_project.Object.Enable");
511 prop.append("Enabled");
512 try
513 {
514 auto result = bus.call(prop);
515 }
516 catch (const sdbusplus::exception::SdBusError& e)
517 {
518 // Treat as property unavailable
519 inventory::PropertyMap prop;
520 prop.emplace("Enabled", false);
521 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
522 }
523}
524
525/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530526 * @brief Prime the Inventory
527 * Prime the inventory by populating only the location code,
528 * type interface and the inventory object for the frus
529 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530530 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530531 * @param[in] jsObject - Reference to vpd inventory json object
532 * @param[in] vpdMap - Reference to the parsed vpd map
533 *
534 * @returns Map of items in extraInterface.
535 */
536template <typename T>
537inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
538 const T& vpdMap)
539{
540 inventory::ObjectMap objects;
541
542 for (auto& itemFRUS : jsObject["frus"].items())
543 {
544 for (auto& itemEEPROM : itemFRUS.value())
545 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600546 // Take pre actions if needed
547 if (itemEEPROM.find("preAction") != itemEEPROM.end())
548 {
549 preAction(jsObject, itemFRUS.key());
550 }
551
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530552 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530553 inventory::Object object(itemEEPROM.at("inventoryPath"));
554
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530555 if ((itemFRUS.key() != systemVpdFilePath) &&
556 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530557 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600558 inventory::PropertyMap presProp;
559 presProp.emplace("Present", false);
560 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530561 presProp);
562 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530563 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
564 {
565 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
566 {
567 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000568 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530569 {
570 if constexpr (std::is_same<T, Parsed>::value)
571 {
572 for (auto& lC : eI.value().items())
573 {
574 auto propVal = expandLocationCode(
575 lC.value().get<string>(), vpdMap, true);
576
577 props.emplace(move(lC.key()),
578 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530579 interfaces.emplace(XYZ_LOCATION_CODE_INF,
580 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530581 interfaces.emplace(move(eI.key()),
582 move(props));
583 }
584 }
585 }
586 else if (eI.key().find("Inventory.Item.") !=
587 string::npos)
588 {
589 interfaces.emplace(move(eI.key()), move(props));
590 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530591 else if (eI.key() ==
592 "xyz.openbmc_project.Inventory.Item")
593 {
594 for (auto& val : eI.value().items())
595 {
596 if (val.key() == "PrettyName")
597 {
598 presProp.emplace(val.key(),
599 val.value().get<string>());
600 }
601 }
602 // Use insert_or_assign here as we may already have
603 // inserted the present property only earlier in
604 // this function under this same interface.
605 interfaces.insert_or_assign(eI.key(),
606 move(presProp));
607 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530608 }
609 }
610 objects.emplace(move(object), move(interfaces));
611 }
612 }
613 }
614 return objects;
615}
616
Alpana Kumari65b83602020-09-01 00:24:56 -0500617/**
618 * @brief This API executes command to set environment variable
619 * And then reboot the system
620 * @param[in] key -env key to set new value
621 * @param[in] value -value to set.
622 */
623void setEnvAndReboot(const string& key, const string& value)
624{
625 // set env and reboot and break.
626 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600627 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500628 // make dbus call to reboot
629 auto bus = sdbusplus::bus::new_default_system();
630 auto method = bus.new_method_call(
631 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
632 "org.freedesktop.systemd1.Manager", "Reboot");
633 bus.call_noreply(method);
634}
635
636/*
637 * @brief This API checks for env var fitconfig.
638 * If not initialised OR updated as per the current system type,
639 * update this env var and reboot the system.
640 *
641 * @param[in] systemType IM kwd in vpd tells about which system type it is.
642 * */
643void setDevTreeEnv(const string& systemType)
644{
Alpana Kumari37e72702021-11-18 11:18:04 -0600645 // Init with default dtb
646 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530647 static const deviceTreeMap deviceTreeSystemTypeMap = {
648 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
649 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
650 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
651 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
652 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
653 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500654
655 if (deviceTreeSystemTypeMap.find(systemType) !=
656 deviceTreeSystemTypeMap.end())
657 {
658 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
659 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600660 else
661 {
662 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600663 string err = "This System type not found/supported in dtb table " +
664 systemType +
665 ".Please check the HW and IM keywords in the system "
666 "VPD.Breaking...";
667
668 // map to hold additional data in case of logging pel
669 PelAdditionalData additionalData{};
670 additionalData.emplace("DESCRIPTION", err);
671 createPEL(additionalData, PelSeverity::WARNING,
672 errIntfForInvalidSystemType);
673 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600674 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500675
676 string readVarValue;
677 bool envVarFound = false;
678
679 vector<string> output = executeCmd("/sbin/fw_printenv");
680 for (const auto& entry : output)
681 {
682 size_t pos = entry.find("=");
683 string key = entry.substr(0, pos);
684 if (key != "fitconfig")
685 {
686 continue;
687 }
688
689 envVarFound = true;
690 if (pos + 1 < entry.size())
691 {
692 readVarValue = entry.substr(pos + 1);
693 if (readVarValue.find(newDeviceTree) != string::npos)
694 {
695 // fitconfig is Updated. No action needed
696 break;
697 }
698 }
699 // set env and reboot and break.
700 setEnvAndReboot(key, newDeviceTree);
701 exit(0);
702 }
703
704 // check If env var Not found
705 if (!envVarFound)
706 {
707 setEnvAndReboot("fitconfig", newDeviceTree);
708 }
709}
710
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530711/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500712 * @brief API to call VPD manager to write VPD to EEPROM.
713 * @param[in] Object path.
714 * @param[in] record to be updated.
715 * @param[in] keyword to be updated.
716 * @param[in] keyword data to be updated
717 */
718void updateHardware(const string& objectName, const string& recName,
719 const string& kwdName, const Binary& data)
720{
721 try
722 {
723 auto bus = sdbusplus::bus::new_default();
724 auto properties =
725 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
726 properties.append(
727 static_cast<sdbusplus::message::object_path>(objectName));
728 properties.append(recName);
729 properties.append(kwdName);
730 properties.append(data);
731 bus.call(properties);
732 }
Patrick Williams8be43342021-09-02 09:33:36 -0500733 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500734 {
735 std::string what =
736 "VPDManager WriteKeyword api failed for inventory path " +
737 objectName;
738 what += " record " + recName;
739 what += " keyword " + kwdName;
740 what += " with bus error = " + std::string(e.what());
741
742 // map to hold additional data in case of logging pel
743 PelAdditionalData additionalData{};
744 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
745 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500746 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500747 }
748}
749
750/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600751 * @brief An api to get list of blank system VPD properties.
752 * @param[in] vpdMap - IPZ vpd map.
753 * @param[in] objectPath - Object path for the FRU.
754 * @param[out] blankPropertyList - Properties which are blank in System VPD and
755 * needs to be updated as standby.
756 */
757void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
758 std::vector<RestoredEeproms>& blankPropertyList)
759{
760 for (const auto& systemRecKwdPair : svpdKwdMap)
761 {
762 auto it = vpdMap.find(systemRecKwdPair.first);
763
764 // check if record is found in map we got by parser
765 if (it != vpdMap.end())
766 {
767 const auto& kwdListForRecord = systemRecKwdPair.second;
768 for (const auto& keyword : kwdListForRecord)
769 {
770 DbusPropertyMap& kwdValMap = it->second;
771 auto iterator = kwdValMap.find(keyword);
772
773 if (iterator != kwdValMap.end())
774 {
775 string& kwdValue = iterator->second;
776
777 // check bus data
778 const string& recordName = systemRecKwdPair.first;
779 const string& busValue = readBusProperty(
780 objectPath, ipzVpdInf + recordName, keyword);
781
782 if (busValue.find_first_not_of(' ') != string::npos)
783 {
784 if (kwdValue.find_first_not_of(' ') == string::npos)
785 {
786 // implies data is blank on EEPROM but not on cache.
787 // So EEPROM vpd update is required.
788 Binary busData(busValue.begin(), busValue.end());
789
790 blankPropertyList.push_back(std::make_tuple(
791 objectPath, recordName, keyword, busData));
792 }
793 }
794 }
795 }
796 }
797 }
798}
799
800/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500801 * @brief API to check if we need to restore system VPD
802 * This functionality is only applicable for IPZ VPD data.
803 * @param[in] vpdMap - IPZ vpd map
804 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500805 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600806void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500807{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500808 for (const auto& systemRecKwdPair : svpdKwdMap)
809 {
810 auto it = vpdMap.find(systemRecKwdPair.first);
811
812 // check if record is found in map we got by parser
813 if (it != vpdMap.end())
814 {
815 const auto& kwdListForRecord = systemRecKwdPair.second;
816 for (const auto& keyword : kwdListForRecord)
817 {
818 DbusPropertyMap& kwdValMap = it->second;
819 auto iterator = kwdValMap.find(keyword);
820
821 if (iterator != kwdValMap.end())
822 {
823 string& kwdValue = iterator->second;
824
825 // check bus data
826 const string& recordName = systemRecKwdPair.first;
827 const string& busValue = readBusProperty(
828 objectPath, ipzVpdInf + recordName, keyword);
829
830 if (busValue.find_first_not_of(' ') != string::npos)
831 {
832 if (kwdValue.find_first_not_of(' ') != string::npos)
833 {
834 // both the data are present, check for mismatch
835 if (busValue != kwdValue)
836 {
837 string errMsg = "VPD data mismatch on cache "
838 "and hardware for record: ";
839 errMsg += (*it).first;
840 errMsg += " and keyword: ";
841 errMsg += keyword;
842
843 // data mismatch
844 PelAdditionalData additionalData;
845 additionalData.emplace("CALLOUT_INVENTORY_PATH",
846 objectPath);
847
848 additionalData.emplace("DESCRIPTION", errMsg);
849
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500850 createPEL(additionalData, PelSeverity::WARNING,
851 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500852 }
853 }
854 else
855 {
856 // implies hardware data is blank
857 // update the map
858 Binary busData(busValue.begin(), busValue.end());
859
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500860 // update the map as well, so that cache data is not
861 // updated as blank while populating VPD map on Dbus
862 // in populateDBus Api
863 kwdValue = busValue;
864 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500865 }
866 else if (kwdValue.find_first_not_of(' ') == string::npos)
867 {
868 string errMsg = "VPD is blank on both cache and "
869 "hardware for record: ";
870 errMsg += (*it).first;
871 errMsg += " and keyword: ";
872 errMsg += keyword;
873 errMsg += ". SSR need to update hardware VPD.";
874
875 // both the data are blanks, log PEL
876 PelAdditionalData additionalData;
877 additionalData.emplace("CALLOUT_INVENTORY_PATH",
878 objectPath);
879
880 additionalData.emplace("DESCRIPTION", errMsg);
881
882 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500883 createPEL(additionalData, PelSeverity::ERROR,
884 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500885 continue;
886 }
887 }
888 }
889 }
890 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500891}
892
893/**
alpana077ce68722021-07-25 13:23:59 -0500894 * @brief This checks for is this FRU a processor
895 * And if yes, then checks for is this primary
896 *
897 * @param[in] js- vpd json to get the information about this FRU
898 * @param[in] filePath- FRU vpd
899 *
900 * @return true/false
901 */
902bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
903{
904 bool isProcessor = false;
905 bool isPrimary = false;
906
907 for (const auto& item : js["frus"][filePath])
908 {
909 if (item.find("extraInterfaces") != item.end())
910 {
911 for (const auto& eI : item["extraInterfaces"].items())
912 {
913 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
914 {
915 isProcessor = true;
916 }
917 }
918 }
919
920 if (isProcessor)
921 {
922 string cpuType = item.value("cpuType", "");
923 if (cpuType == "primary")
924 {
925 isPrimary = true;
926 }
927 }
928 }
929
930 return (isProcessor && isPrimary);
931}
932
933/**
934 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
935 * driver
936 * @param[in] js- vpd json to iterate through and take action if it is DIMM
937 */
938void doEnableAllDimms(nlohmann::json& js)
939{
940 // iterate over each fru
941 for (const auto& eachFru : js["frus"].items())
942 {
943 // skip the driver binding if eeprom already exists
944 if (fs::exists(eachFru.key()))
945 {
946 continue;
947 }
948
949 for (const auto& eachInventory : eachFru.value())
950 {
951 if (eachInventory.find("extraInterfaces") != eachInventory.end())
952 {
953 for (const auto& eI : eachInventory["extraInterfaces"].items())
954 {
955 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
956 {
957 string dimmVpd = eachFru.key();
958 // fetch it from
959 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
960
961 regex matchPatern("([0-9]+-[0-9]{4})");
962 smatch matchFound;
963 if (regex_search(dimmVpd, matchFound, matchPatern))
964 {
965 vector<string> i2cReg;
966 boost::split(i2cReg, matchFound.str(0),
967 boost::is_any_of("-"));
968
969 // remove 0s from begining
970 const regex pattern("^0+(?!$)");
971 for (auto& i : i2cReg)
972 {
973 i = regex_replace(i, pattern, "");
974 }
975
976 if (i2cReg.size() == 2)
977 {
978 // echo 24c32 0x50 >
979 // /sys/bus/i2c/devices/i2c-16/new_device
980 string cmnd = "echo 24c32 0x" + i2cReg[1] +
981 " > /sys/bus/i2c/devices/i2c-" +
982 i2cReg[0] + "/new_device";
983
984 executeCmd(cmnd);
985 }
986 }
987 }
988 }
989 }
990 }
991 }
992}
993
994/**
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -0600995 * @brief Check if the given CPU is an IO only chip.
996 * The CPU is termed as IO, whose all of the cores are bad and can never be
997 * used. Those CPU chips can be used for IO purpose like connecting PCIe devices
998 * etc., The CPU whose every cores are bad, can be identified from the CP00
999 * record's PG keyword, only if all of the 8 EQs' value equals 0xE7F9FF. (1EQ
1000 * has 4 cores grouped together by sharing its cache memory.)
1001 * @param [in] pgKeyword - PG Keyword of CPU.
1002 * @return true if the given cpu is an IO, false otherwise.
1003 */
1004static bool isCPUIOGoodOnly(const string& pgKeyword)
1005{
1006 const unsigned char io[] = {0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9,
1007 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7,
1008 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF};
1009 // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0).
1010 // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs'
1011 // value equals 0xE7F9FF, then the cpu has no good cores and its treated as
1012 // IO.
1013 if (memcmp(io, pgKeyword.data() + 97, 24) == 0)
1014 {
1015 return true;
1016 }
1017
1018 // The CPU is not an IO
1019 return false;
1020}
1021
1022/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301023 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301024 * This method invokes all the populateInterface functions
1025 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301026 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1027 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301028 * @param[in] js - Inventory json object
1029 * @param[in] filePath - Path of the vpd file
1030 * @param[in] preIntrStr - Interface string
1031 */
1032template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001033static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301034{
1035 inventory::InterfaceMap interfaces;
1036 inventory::ObjectMap objects;
1037 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001038 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301039
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301040 bool isSystemVpd = (filePath == systemVpdFilePath);
1041 if constexpr (is_same<T, Parsed>::value)
1042 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001043 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1044 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1045 ::toupper);
1046
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301047 if (isSystemVpd)
1048 {
1049 std::vector<std::string> interfaces = {motherBoardInterface};
1050 // call mapper to check for object path creation
1051 MapperResponse subTree =
1052 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1053 string mboardPath =
1054 js["frus"][filePath].at(0).value("inventoryPath", "");
1055
1056 // Attempt system VPD restore if we have a motherboard
1057 // object in the inventory.
1058 if ((subTree.size() != 0) &&
1059 (subTree.find(pimPath + mboardPath) != subTree.end()))
1060 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001061 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301062 }
1063 else
1064 {
1065 log<level::ERR>("No object path found");
1066 }
1067 }
alpana077ce68722021-07-25 13:23:59 -05001068 else
1069 {
1070 // check if it is processor vpd.
1071 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1072
1073 if (isPrimaryCpu)
1074 {
1075 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1076
1077 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1078
1079 if (chipVersion >= 2)
1080 {
1081 doEnableAllDimms(js);
1082 }
1083 }
1084 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301085 }
1086
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001087 if (isSystemVpd)
1088 {
1089 string systemJsonName{};
1090 if constexpr (is_same<T, Parsed>::value)
1091 {
1092 // pick the right system json
1093 systemJsonName = getSystemsJson(vpdMap);
1094 }
1095
1096 fs::path target = systemJsonName;
1097 fs::path link = INVENTORY_JSON_SYM_LINK;
1098
1099 // Create the directory for hosting the symlink
1100 fs::create_directories(VPD_FILES_PATH);
1101 // unlink the symlink previously created (if any)
1102 remove(INVENTORY_JSON_SYM_LINK);
1103 // create a new symlink based on the system
1104 fs::create_symlink(target, link);
1105
1106 // Reloading the json
1107 ifstream inventoryJson(link);
1108 js = json::parse(inventoryJson);
1109 inventoryJson.close();
1110 }
1111
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301112 for (const auto& item : js["frus"][filePath])
1113 {
1114 const auto& objectPath = item["inventoryPath"];
1115 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001116
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001117 vector<string> ccinList;
1118 if (item.find("ccin") != item.end())
1119 {
1120 for (const auto& cc : item["ccin"])
1121 {
1122 string ccin = cc;
1123 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1124 ccinList.push_back(ccin);
1125 }
1126 }
1127
1128 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1129 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1130 ccinList.end()))
1131 {
1132 continue;
1133 }
1134
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001135 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301136 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001137
1138 // Populate one time properties for the system VPD and its sub-frus
1139 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301140 // For the remaining FRUs, this will get handled as a part of
1141 // priming the inventory.
1142 setOneTimeProperties(objectPath, interfaces);
1143 }
1144
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301145 // Populate the VPD keywords and the common interfaces only if we
1146 // are asked to inherit that data from the VPD, else only add the
1147 // extraInterfaces.
1148 if (item.value("inherit", true))
1149 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001150 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301151 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301152 // Each record in the VPD becomes an interface and all
1153 // keyword within the record are properties under that
1154 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301155 for (const auto& record : vpdMap)
1156 {
1157 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001158 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301159 }
1160 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001161 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301162 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001163 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301164 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301165 if (js.find("commonInterfaces") != js.end())
1166 {
1167 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1168 isSystemVpd);
1169 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301170 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001171 else
1172 {
1173 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001174 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001175 {
1176 if (item.find("copyRecords") != item.end())
1177 {
1178 for (const auto& record : item["copyRecords"])
1179 {
1180 const string& recordName = record;
1181 if (vpdMap.find(recordName) != vpdMap.end())
1182 {
1183 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001184 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001185 interfaces);
1186 }
1187 }
1188 }
1189 }
1190 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301191 // Populate interfaces and properties that are common to every FRU
1192 // and additional interface that might be defined on a per-FRU
1193 // basis.
1194 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301195 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301196 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1197 isSystemVpd);
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001198 if constexpr (is_same<T, Parsed>::value)
1199 {
1200 if (item["extraInterfaces"].find(
1201 "xyz.openbmc_project.Inventory.Item.Cpu") !=
1202 item["extraInterfaces"].end())
1203 {
1204 if (isCPUIOGoodOnly(getKwVal(vpdMap, "CP00", "PG")))
1205 {
Priyanga Ramasamy2c607a92022-04-08 00:30:17 -05001206 interfaces[invItemIntf]["PrettyName"] = "IO Module";
Priyanga Ramasamy6abdeb62022-01-09 23:15:11 -06001207 }
1208 }
1209 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301210 }
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -06001211 inventory::PropertyMap presProp;
1212 presProp.emplace("Present", true);
1213 insertOrMerge(interfaces, invItemIntf, move(presProp));
1214
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301215 objects.emplace(move(object), move(interfaces));
1216 }
1217
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301218 if (isSystemVpd)
1219 {
1220 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1221 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001222
Alpana Kumarif05effd2021-04-07 07:32:53 -05001223 // set the U-boot environment variable for device-tree
1224 if constexpr (is_same<T, Parsed>::value)
1225 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301226 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001227 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301228 }
1229
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301230 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001231 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301232}
1233
1234int main(int argc, char** argv)
1235{
1236 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001237 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001238 Binary vpdVector{};
1239 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001240 // map to hold additional data in case of logging pel
1241 PelAdditionalData additionalData{};
1242
1243 // this is needed to hold base fru inventory path in case there is ECC or
1244 // vpd exception while parsing the file
1245 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301246
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001247 // severity for PEL
1248 PelSeverity pelSeverity = PelSeverity::WARNING;
1249
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301250 try
1251 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301252 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1253 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301254
1255 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001256 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301257
1258 CLI11_PARSE(app, argc, argv);
1259
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001260 // PEL severity should be ERROR in case of any system VPD failure
1261 if (file == systemVpdFilePath)
1262 {
1263 pelSeverity = PelSeverity::ERROR;
1264 }
1265
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301266 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1267
1268 // If the symlink exists, it means it has been setup for us, switch the
1269 // path
1270 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1271 {
1272 jsonToParse = INVENTORY_JSON_SYM_LINK;
1273 }
1274
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301275 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301276 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001277 if (!inventoryJson)
1278 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001279 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001280 }
1281
1282 try
1283 {
1284 js = json::parse(inventoryJson);
1285 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001286 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001287 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001288 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001289 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301290
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301291 // Do we have the mandatory "frus" section?
1292 if (js.find("frus") == js.end())
1293 {
1294 throw(VpdJsonException("FRUs section not found in JSON",
1295 jsonToParse));
1296 }
1297
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301298 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1299 if (file.find("/ahb:apb") != string::npos)
1300 {
1301 // Translate udev path to a generic /sys/bus/.. file path.
1302 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301303
1304 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301305 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301306 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001307 // We need manager service active to process restoring of
1308 // system VPD on hardware. So in case any system restore is
1309 // required, update hardware in the second trigger of parser
1310 // code for system vpd file path.
1311
1312 std::vector<std::string> interfaces{motherBoardInterface};
1313
1314 // call mapper to check for object path creation
1315 MapperResponse subTree =
1316 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1317 string mboardPath =
1318 js["frus"][file].at(0).value("inventoryPath", "");
1319
1320 // Attempt system VPD restore if we have a motherboard
1321 // object in the inventory.
1322 if ((subTree.size() != 0) &&
1323 (subTree.find(pimPath + mboardPath) != subTree.end()))
1324 {
1325 vpdVector = getVpdDataInVector(js, file);
1326 ParserInterface* parser =
1327 ParserFactory::getParser(vpdVector);
1328 variant<KeywordVpdMap, Store> parseResult;
1329 parseResult = parser->parse();
1330
1331 if (auto pVal = get_if<Store>(&parseResult))
1332 {
1333 // map to hold all the keywords whose value is blank and
1334 // needs to be updated at standby.
1335 vector<RestoredEeproms> blankSystemVpdProperties{};
1336 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1337 blankSystemVpdProperties);
1338
1339 // if system VPD restore is required, update the
1340 // EEPROM
1341 for (const auto& item : blankSystemVpdProperties)
1342 {
1343 updateHardware(get<0>(item), get<1>(item),
1344 get<2>(item), get<3>(item));
1345 }
1346 }
1347 else
1348 {
1349 std::cout << "Not a valid format to restore system VPD"
1350 << std::endl;
1351 }
1352 // release the parser object
1353 ParserFactory::freeParser(parser);
1354 }
1355 else
1356 {
1357 log<level::ERR>("No object path found");
1358 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301359 return 0;
1360 }
1361 }
1362
1363 if (file.empty())
1364 {
1365 cerr << "The EEPROM path <" << file << "> is not valid.";
1366 return 0;
1367 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301368 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301369 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301370 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301371 }
1372
Alpana Kumari2f793042020-08-18 05:51:03 -05001373 if (!fs::exists(file))
1374 {
1375 cout << "Device path: " << file
1376 << " does not exist. Spurious udev event? Exiting." << endl;
1377 return 0;
1378 }
1379
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001380 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301381 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001382 // We skip reading VPD when the power is ON in two scenarios:
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301383 // 1) The eeprom we are trying to read is that of the system VPD and the
1384 // JSON symlink is already setup (the symlink's existence tells us we
1385 // are not coming out of a factory reset)
1386 // 2) The JSON tells us that the FRU EEPROM cannot be
1387 // read when we are powered ON.
Santosh Puranik27a5e952021-10-07 22:08:01 -05001388 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
Santosh Puranik31d50fa2022-04-04 12:04:37 +05301389 (file == systemVpdFilePath && fs::exists(INVENTORY_JSON_SYM_LINK)))
Santosh Puranik85893752020-11-10 21:31:43 +05301390 {
1391 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1392 getPowerState())
1393 {
1394 cout << "This VPD cannot be read when power is ON" << endl;
1395 return 0;
1396 }
1397 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001398
Alpana Kumari2f793042020-08-18 05:51:03 -05001399 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301400 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001401 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001402 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001403 variant<KeywordVpdMap, Store> parseResult;
1404 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001405
Alpana Kumari2f793042020-08-18 05:51:03 -05001406 if (auto pVal = get_if<Store>(&parseResult))
1407 {
1408 populateDbus(pVal->getVpdMap(), js, file);
1409 }
1410 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1411 {
1412 populateDbus(*pVal, js, file);
1413 }
1414
1415 // release the parser object
1416 ParserFactory::freeParser(parser);
1417 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001418 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001419 {
Alpana Kumari735dee92022-03-25 01:24:40 -05001420 executePostFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001421 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001422 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301423 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001424 catch (const VpdJsonException& ex)
1425 {
1426 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1427 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001428 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001429
1430 cerr << ex.what() << "\n";
1431 rc = -1;
1432 }
1433 catch (const VpdEccException& ex)
1434 {
1435 additionalData.emplace("DESCRIPTION", "ECC check failed");
1436 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1437 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001438 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001439 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001440 cerr << ex.what() << "\n";
1441 rc = -1;
1442 }
1443 catch (const VpdDataException& ex)
1444 {
alpana075cb3b1f2021-12-16 11:19:36 -06001445 if (isThisPcieOnPass1planar(js, file))
1446 {
1447 cout << "Pcie_device [" << file
1448 << "]'s VPD is not valid on PASS1 planar.Ignoring.\n";
1449 rc = 0;
1450 }
1451 else
1452 {
1453 string errorMsg =
1454 "VPD file is either empty or invalid. Parser failed for [";
1455 errorMsg += file;
1456 errorMsg += "], with error = " + std::string(ex.what());
1457
1458 additionalData.emplace("DESCRIPTION", errorMsg);
1459 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1460 INVENTORY_PATH + baseFruInventoryPath);
1461 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
1462
1463 rc = -1;
1464 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001465 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001466 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301467 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001468 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301469 cerr << e.what() << "\n";
1470 rc = -1;
1471 }
1472
1473 return rc;
Alpana Kumari735dee92022-03-25 01:24:40 -05001474}