blob: b006a300c3ad9af73f1e75470ca06e4adf715035 [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 }
324 interfaces.emplace(inf, move(props));
325 }
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
Alpana Kumari2f793042020-08-18 05:51:03 -0500353static Binary getVpdDataInVector(const nlohmann::json& js, const string& file)
Alpana Kumari58e22142020-05-05 00:22:12 -0500354{
355 uint32_t offset = 0;
356 // check if offset present?
357 for (const auto& item : js["frus"][file])
358 {
359 if (item.find("offset") != item.end())
360 {
361 offset = item["offset"];
362 }
363 }
364
365 // TODO: Figure out a better way to get max possible VPD size.
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600366 auto maxVPDSize = std::min(std::filesystem::file_size(file),
367 static_cast<uintmax_t>(65504));
368
Alpana Kumari58e22142020-05-05 00:22:12 -0500369 Binary vpdVector;
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600370 vpdVector.resize(maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500371 ifstream vpdFile;
372 vpdFile.open(file, ios::binary);
373
374 vpdFile.seekg(offset, ios_base::cur);
Priyanga Ramasamy3c2a2b92021-12-22 00:09:38 -0600375 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize);
Alpana Kumari58e22142020-05-05 00:22:12 -0500376 vpdVector.resize(vpdFile.gcount());
377
Priyanga Ramasamy37233782021-12-09 03:14:02 -0600378 resetEEPROMPointer(js, file, vpdFile);
379
Alpana Kumari58e22142020-05-05 00:22:12 -0500380 return vpdVector;
381}
382
Alpana Kumari2f793042020-08-18 05:51:03 -0500383/** This API will be called at the end of VPD collection to perform any post
384 * actions.
385 *
386 * @param[in] json - json object
387 * @param[in] file - eeprom file path
388 */
389static void postFailAction(const nlohmann::json& json, const string& file)
390{
391 if ((json["frus"][file].at(0)).find("postActionFail") ==
392 json["frus"][file].at(0).end())
393 {
394 return;
395 }
396
397 uint8_t pinValue = 0;
398 string pinName;
399
400 for (const auto& postAction :
401 (json["frus"][file].at(0))["postActionFail"].items())
402 {
403 if (postAction.key() == "pin")
404 {
405 pinName = postAction.value();
406 }
407 else if (postAction.key() == "value")
408 {
409 // Get the value to set
410 pinValue = postAction.value();
411 }
412 }
413
414 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl;
415
416 try
417 {
418 gpiod::line outputLine = gpiod::find_line(pinName);
419
420 if (!outputLine)
421 {
422 cout << "Couldn't find output line:" << pinName
423 << " on GPIO. Skipping...\n";
424
425 return;
426 }
427 outputLine.request(
428 {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0},
429 pinValue);
430 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500431 catch (const system_error&)
Alpana Kumari2f793042020-08-18 05:51:03 -0500432 {
433 cerr << "Failed to set post-action GPIO" << endl;
434 }
435}
436
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600437/**
438 * @brief This sets the appropriate value to presence GPIO based on device
439 * attached or not on expander GPIO, which enables that FRU's VPD collection.
Alpana Kumari2f793042020-08-18 05:51:03 -0500440 *
441 * @param[in] json - json object
442 * @param[in] file - eeprom file path
443 */
444static void preAction(const nlohmann::json& json, const string& file)
445{
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600446 if ((json["frus"][file].at(0)).find("presence") !=
Alpana Kumari2f793042020-08-18 05:51:03 -0500447 json["frus"][file].at(0).end())
448 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600449 if (((json["frus"][file].at(0)["presence"]).find("pin") !=
450 json["frus"][file].at(0)["presence"].end()) &&
451 ((json["frus"][file].at(0)["presence"]).find("value") !=
452 json["frus"][file].at(0)["presence"].end()))
453 {
Sunny Srivastava2e147892022-02-22 00:48:26 -0600454 string presPinName = json["frus"][file].at(0)["presence"]["pin"];
455 Byte presPinValue = json["frus"][file].at(0)["presence"]["value"];
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600456
457 try
458 {
459 gpiod::line presenceLine = gpiod::find_line(presPinName);
460
461 if (!presenceLine)
462 {
463 cerr << "couldn't find presence line:" << presPinName
464 << "\n";
465 return;
466 }
467
468 presenceLine.request({"Read the presence line",
469 gpiod::line_request::DIRECTION_INPUT, 0});
470
471 Byte gpioData = presenceLine.get_value();
472
473 if (gpioData != presPinValue)
474 {
475 return;
476 }
477 }
478 catch (system_error&)
479 {
480 cerr << "Failed to get the presence GPIO for - " << presPinName
481 << endl;
482 return;
483 }
484 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500485 }
486
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600487 if ((json["frus"][file].at(0)).find("preAction") !=
488 json["frus"][file].at(0).end())
Alpana Kumari2f793042020-08-18 05:51:03 -0500489 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600490 if (((json["frus"][file].at(0)["preAction"]).find("pin") !=
491 json["frus"][file].at(0)["preAction"].end()) &&
492 ((json["frus"][file].at(0)["preAction"]).find("value") !=
493 json["frus"][file].at(0)["preAction"].end()))
Alpana Kumari2f793042020-08-18 05:51:03 -0500494 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600495 string pinName = json["frus"][file].at(0)["preAction"]["pin"];
Alpana Kumari2f793042020-08-18 05:51:03 -0500496 // Get the value to set
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600497 Byte pinValue = json["frus"][file].at(0)["preAction"]["value"];
498
499 cout << "Setting GPIO: " << pinName << " to " << (int)pinValue
500 << endl;
501 try
502 {
503 gpiod::line outputLine = gpiod::find_line(pinName);
504
505 if (!outputLine)
506 {
507 cout << "Couldn't find output line:" << pinName
508 << " on GPIO. Skipping...\n";
509
510 return;
511 }
512 outputLine.request({"FRU pre-action",
513 ::gpiod::line_request::DIRECTION_OUTPUT, 0},
514 pinValue);
515 }
516 catch (system_error&)
517 {
518 cerr << "Failed to set pre-action for GPIO - " << pinName
519 << endl;
520 return;
521 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500522 }
523 }
524
Alpana Kumari2f793042020-08-18 05:51:03 -0500525 // Now bind the device
Alpana Kumari64b9f5f2022-02-19 13:25:58 -0600526 if (json["frus"][file].at(0).find("devAddress") !=
527 json["frus"][file].at(0).end())
528 {
529 string bind = json["frus"][file].at(0).value("devAddress", "");
530 cout << "Binding device " << bind << endl;
531 string bindCmd = string("echo \"") + bind +
532 string("\" > /sys/bus/i2c/drivers/at24/bind");
533 cout << bindCmd << endl;
534 executeCmd(bindCmd);
535 }
Alpana Kumari2f793042020-08-18 05:51:03 -0500536
537 // Check if device showed up (test for file)
538 if (!fs::exists(file))
539 {
540 cout << "EEPROM " << file << " does not exist. Take failure action"
541 << endl;
542 // If not, then take failure postAction
543 postFailAction(json, file);
544 }
545}
546
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530547/**
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530548 * @brief Set certain one time properties in the inventory
549 * Use this function to insert the Functional and Enabled properties into the
550 * inventory map. This function first checks if the object in question already
551 * has these properties hosted on D-Bus, if the property is already there, it is
552 * not modified, hence the name "one time". If the property is not already
553 * present, it will be added to the map with a suitable default value (true for
554 * Functional and false for Enabled)
555 *
556 * @param[in] object - The inventory D-Bus obejct without the inventory prefix.
557 * @param[inout] interfaces - Reference to a map of inventory interfaces to
558 * which the properties will be attached.
559 */
560static void setOneTimeProperties(const std::string& object,
561 inventory::InterfaceMap& interfaces)
562{
563 auto bus = sdbusplus::bus::new_default();
564 auto objectPath = INVENTORY_PATH + object;
565 auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
566 objectPath.c_str(),
567 "org.freedesktop.DBus.Properties", "Get");
568 prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus");
569 prop.append("Functional");
570 try
571 {
572 auto result = bus.call(prop);
573 }
574 catch (const sdbusplus::exception::SdBusError& e)
575 {
576 // Treat as property unavailable
577 inventory::PropertyMap prop;
578 prop.emplace("Functional", true);
579 interfaces.emplace(
580 "xyz.openbmc_project.State.Decorator.OperationalStatus",
581 move(prop));
582 }
583 prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager",
584 objectPath.c_str(),
585 "org.freedesktop.DBus.Properties", "Get");
586 prop.append("xyz.openbmc_project.Object.Enable");
587 prop.append("Enabled");
588 try
589 {
590 auto result = bus.call(prop);
591 }
592 catch (const sdbusplus::exception::SdBusError& e)
593 {
594 // Treat as property unavailable
595 inventory::PropertyMap prop;
596 prop.emplace("Enabled", false);
597 interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop));
598 }
599}
600
601/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530602 * @brief Prime the Inventory
603 * Prime the inventory by populating only the location code,
604 * type interface and the inventory object for the frus
605 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530606 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530607 * @param[in] jsObject - Reference to vpd inventory json object
608 * @param[in] vpdMap - Reference to the parsed vpd map
609 *
610 * @returns Map of items in extraInterface.
611 */
612template <typename T>
613inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
614 const T& vpdMap)
615{
616 inventory::ObjectMap objects;
617
618 for (auto& itemFRUS : jsObject["frus"].items())
619 {
620 for (auto& itemEEPROM : itemFRUS.value())
621 {
Alpana Kumari2e6c6f72020-12-03 00:10:03 -0600622 // Take pre actions if needed
623 if (itemEEPROM.find("preAction") != itemEEPROM.end())
624 {
625 preAction(jsObject, itemFRUS.key());
626 }
627
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530628 inventory::InterfaceMap interfaces;
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530629 inventory::Object object(itemEEPROM.at("inventoryPath"));
630
Santosh Puranik50f60bf2021-05-26 17:55:06 +0530631 if ((itemFRUS.key() != systemVpdFilePath) &&
632 !itemEEPROM.value("noprime", false))
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530633 {
Alpana Kumaricfd7a752021-02-07 23:23:01 -0600634 inventory::PropertyMap presProp;
635 presProp.emplace("Present", false);
636 interfaces.emplace("xyz.openbmc_project.Inventory.Item",
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530637 presProp);
638 setOneTimeProperties(object, interfaces);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530639 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
640 {
641 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
642 {
643 inventory::PropertyMap props;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000644 if (eI.key() == IBM_LOCATION_CODE_INF)
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530645 {
646 if constexpr (std::is_same<T, Parsed>::value)
647 {
648 for (auto& lC : eI.value().items())
649 {
650 auto propVal = expandLocationCode(
651 lC.value().get<string>(), vpdMap, true);
652
653 props.emplace(move(lC.key()),
654 move(propVal));
Santosh Puranikb0f37492021-06-21 09:42:47 +0530655 interfaces.emplace(XYZ_LOCATION_CODE_INF,
656 props);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530657 interfaces.emplace(move(eI.key()),
658 move(props));
659 }
660 }
661 }
662 else if (eI.key().find("Inventory.Item.") !=
663 string::npos)
664 {
665 interfaces.emplace(move(eI.key()), move(props));
666 }
Santosh Puranikd3a379a2021-08-23 19:12:59 +0530667 else if (eI.key() ==
668 "xyz.openbmc_project.Inventory.Item")
669 {
670 for (auto& val : eI.value().items())
671 {
672 if (val.key() == "PrettyName")
673 {
674 presProp.emplace(val.key(),
675 val.value().get<string>());
676 }
677 }
678 // Use insert_or_assign here as we may already have
679 // inserted the present property only earlier in
680 // this function under this same interface.
681 interfaces.insert_or_assign(eI.key(),
682 move(presProp));
683 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530684 }
685 }
686 objects.emplace(move(object), move(interfaces));
687 }
688 }
689 }
690 return objects;
691}
692
Alpana Kumari65b83602020-09-01 00:24:56 -0500693/**
694 * @brief This API executes command to set environment variable
695 * And then reboot the system
696 * @param[in] key -env key to set new value
697 * @param[in] value -value to set.
698 */
699void setEnvAndReboot(const string& key, const string& value)
700{
701 // set env and reboot and break.
702 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600703 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500704 // make dbus call to reboot
705 auto bus = sdbusplus::bus::new_default_system();
706 auto method = bus.new_method_call(
707 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
708 "org.freedesktop.systemd1.Manager", "Reboot");
709 bus.call_noreply(method);
710}
711
712/*
713 * @brief This API checks for env var fitconfig.
714 * If not initialised OR updated as per the current system type,
715 * update this env var and reboot the system.
716 *
717 * @param[in] systemType IM kwd in vpd tells about which system type it is.
718 * */
719void setDevTreeEnv(const string& systemType)
720{
Alpana Kumari37e72702021-11-18 11:18:04 -0600721 // Init with default dtb
722 string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb";
Santosh Puranike5f177a2022-01-24 20:14:46 +0530723 static const deviceTreeMap deviceTreeSystemTypeMap = {
724 {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"},
725 {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"},
726 {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"},
727 {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"},
728 {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"},
729 {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -0500730
731 if (deviceTreeSystemTypeMap.find(systemType) !=
732 deviceTreeSystemTypeMap.end())
733 {
734 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
735 }
Alpana Kumari37e72702021-11-18 11:18:04 -0600736 else
737 {
738 // System type not supported
Alpana Kumariab1e22c2021-11-24 11:03:38 -0600739 string err = "This System type not found/supported in dtb table " +
740 systemType +
741 ".Please check the HW and IM keywords in the system "
742 "VPD.Breaking...";
743
744 // map to hold additional data in case of logging pel
745 PelAdditionalData additionalData{};
746 additionalData.emplace("DESCRIPTION", err);
747 createPEL(additionalData, PelSeverity::WARNING,
748 errIntfForInvalidSystemType);
749 exit(-1);
Alpana Kumari37e72702021-11-18 11:18:04 -0600750 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500751
752 string readVarValue;
753 bool envVarFound = false;
754
755 vector<string> output = executeCmd("/sbin/fw_printenv");
756 for (const auto& entry : output)
757 {
758 size_t pos = entry.find("=");
759 string key = entry.substr(0, pos);
760 if (key != "fitconfig")
761 {
762 continue;
763 }
764
765 envVarFound = true;
766 if (pos + 1 < entry.size())
767 {
768 readVarValue = entry.substr(pos + 1);
769 if (readVarValue.find(newDeviceTree) != string::npos)
770 {
771 // fitconfig is Updated. No action needed
772 break;
773 }
774 }
775 // set env and reboot and break.
776 setEnvAndReboot(key, newDeviceTree);
777 exit(0);
778 }
779
780 // check If env var Not found
781 if (!envVarFound)
782 {
783 setEnvAndReboot("fitconfig", newDeviceTree);
784 }
785}
786
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530787/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500788 * @brief API to call VPD manager to write VPD to EEPROM.
789 * @param[in] Object path.
790 * @param[in] record to be updated.
791 * @param[in] keyword to be updated.
792 * @param[in] keyword data to be updated
793 */
794void updateHardware(const string& objectName, const string& recName,
795 const string& kwdName, const Binary& data)
796{
797 try
798 {
799 auto bus = sdbusplus::bus::new_default();
800 auto properties =
801 bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword");
802 properties.append(
803 static_cast<sdbusplus::message::object_path>(objectName));
804 properties.append(recName);
805 properties.append(kwdName);
806 properties.append(data);
807 bus.call(properties);
808 }
Patrick Williams8be43342021-09-02 09:33:36 -0500809 catch (const sdbusplus::exception::exception& e)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500810 {
811 std::string what =
812 "VPDManager WriteKeyword api failed for inventory path " +
813 objectName;
814 what += " record " + recName;
815 what += " keyword " + kwdName;
816 what += " with bus error = " + std::string(e.what());
817
818 // map to hold additional data in case of logging pel
819 PelAdditionalData additionalData{};
820 additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName);
821 additionalData.emplace("DESCRIPTION", what);
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500822 createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500823 }
824}
825
826/**
Sunny Srivastava3c244142022-01-11 08:47:04 -0600827 * @brief An api to get list of blank system VPD properties.
828 * @param[in] vpdMap - IPZ vpd map.
829 * @param[in] objectPath - Object path for the FRU.
830 * @param[out] blankPropertyList - Properties which are blank in System VPD and
831 * needs to be updated as standby.
832 */
833void getListOfBlankSystemVpd(Parsed& vpdMap, const string& objectPath,
834 std::vector<RestoredEeproms>& blankPropertyList)
835{
836 for (const auto& systemRecKwdPair : svpdKwdMap)
837 {
838 auto it = vpdMap.find(systemRecKwdPair.first);
839
840 // check if record is found in map we got by parser
841 if (it != vpdMap.end())
842 {
843 const auto& kwdListForRecord = systemRecKwdPair.second;
844 for (const auto& keyword : kwdListForRecord)
845 {
846 DbusPropertyMap& kwdValMap = it->second;
847 auto iterator = kwdValMap.find(keyword);
848
849 if (iterator != kwdValMap.end())
850 {
851 string& kwdValue = iterator->second;
852
853 // check bus data
854 const string& recordName = systemRecKwdPair.first;
855 const string& busValue = readBusProperty(
856 objectPath, ipzVpdInf + recordName, keyword);
857
858 if (busValue.find_first_not_of(' ') != string::npos)
859 {
860 if (kwdValue.find_first_not_of(' ') == string::npos)
861 {
862 // implies data is blank on EEPROM but not on cache.
863 // So EEPROM vpd update is required.
864 Binary busData(busValue.begin(), busValue.end());
865
866 blankPropertyList.push_back(std::make_tuple(
867 objectPath, recordName, keyword, busData));
868 }
869 }
870 }
871 }
872 }
873 }
874}
875
876/**
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500877 * @brief API to check if we need to restore system VPD
878 * This functionality is only applicable for IPZ VPD data.
879 * @param[in] vpdMap - IPZ vpd map
880 * @param[in] objectPath - Object path for the FRU
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500881 */
Sunny Srivastava3c244142022-01-11 08:47:04 -0600882void restoreSystemVPD(Parsed& vpdMap, const string& objectPath)
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500883{
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500884 for (const auto& systemRecKwdPair : svpdKwdMap)
885 {
886 auto it = vpdMap.find(systemRecKwdPair.first);
887
888 // check if record is found in map we got by parser
889 if (it != vpdMap.end())
890 {
891 const auto& kwdListForRecord = systemRecKwdPair.second;
892 for (const auto& keyword : kwdListForRecord)
893 {
894 DbusPropertyMap& kwdValMap = it->second;
895 auto iterator = kwdValMap.find(keyword);
896
897 if (iterator != kwdValMap.end())
898 {
899 string& kwdValue = iterator->second;
900
901 // check bus data
902 const string& recordName = systemRecKwdPair.first;
903 const string& busValue = readBusProperty(
904 objectPath, ipzVpdInf + recordName, keyword);
905
906 if (busValue.find_first_not_of(' ') != string::npos)
907 {
908 if (kwdValue.find_first_not_of(' ') != string::npos)
909 {
910 // both the data are present, check for mismatch
911 if (busValue != kwdValue)
912 {
913 string errMsg = "VPD data mismatch on cache "
914 "and hardware for record: ";
915 errMsg += (*it).first;
916 errMsg += " and keyword: ";
917 errMsg += keyword;
918
919 // data mismatch
920 PelAdditionalData additionalData;
921 additionalData.emplace("CALLOUT_INVENTORY_PATH",
922 objectPath);
923
924 additionalData.emplace("DESCRIPTION", errMsg);
925
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500926 createPEL(additionalData, PelSeverity::WARNING,
927 errIntfForInvalidVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500928 }
929 }
930 else
931 {
932 // implies hardware data is blank
933 // update the map
934 Binary busData(busValue.begin(), busValue.end());
935
Sunny Srivastava90a63b92021-05-26 06:30:24 -0500936 // update the map as well, so that cache data is not
937 // updated as blank while populating VPD map on Dbus
938 // in populateDBus Api
939 kwdValue = busValue;
940 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500941 }
942 else if (kwdValue.find_first_not_of(' ') == string::npos)
943 {
944 string errMsg = "VPD is blank on both cache and "
945 "hardware for record: ";
946 errMsg += (*it).first;
947 errMsg += " and keyword: ";
948 errMsg += keyword;
949 errMsg += ". SSR need to update hardware VPD.";
950
951 // both the data are blanks, log PEL
952 PelAdditionalData additionalData;
953 additionalData.emplace("CALLOUT_INVENTORY_PATH",
954 objectPath);
955
956 additionalData.emplace("DESCRIPTION", errMsg);
957
958 // log PEL TODO: Block IPL
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500959 createPEL(additionalData, PelSeverity::ERROR,
960 errIntfForBlankSystemVPD);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500961 continue;
962 }
963 }
964 }
965 }
966 }
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500967}
968
969/**
alpana077ce68722021-07-25 13:23:59 -0500970 * @brief This checks for is this FRU a processor
971 * And if yes, then checks for is this primary
972 *
973 * @param[in] js- vpd json to get the information about this FRU
974 * @param[in] filePath- FRU vpd
975 *
976 * @return true/false
977 */
978bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath)
979{
980 bool isProcessor = false;
981 bool isPrimary = false;
982
983 for (const auto& item : js["frus"][filePath])
984 {
985 if (item.find("extraInterfaces") != item.end())
986 {
987 for (const auto& eI : item["extraInterfaces"].items())
988 {
989 if (eI.key().find("Inventory.Item.Cpu") != string::npos)
990 {
991 isProcessor = true;
992 }
993 }
994 }
995
996 if (isProcessor)
997 {
998 string cpuType = item.value("cpuType", "");
999 if (cpuType == "primary")
1000 {
1001 isPrimary = true;
1002 }
1003 }
1004 }
1005
1006 return (isProcessor && isPrimary);
1007}
1008
1009/**
1010 * @brief This finds DIMM vpd in vpd json and enables them by binding the device
1011 * driver
1012 * @param[in] js- vpd json to iterate through and take action if it is DIMM
1013 */
1014void doEnableAllDimms(nlohmann::json& js)
1015{
1016 // iterate over each fru
1017 for (const auto& eachFru : js["frus"].items())
1018 {
1019 // skip the driver binding if eeprom already exists
1020 if (fs::exists(eachFru.key()))
1021 {
1022 continue;
1023 }
1024
1025 for (const auto& eachInventory : eachFru.value())
1026 {
1027 if (eachInventory.find("extraInterfaces") != eachInventory.end())
1028 {
1029 for (const auto& eI : eachInventory["extraInterfaces"].items())
1030 {
1031 if (eI.key().find("Inventory.Item.Dimm") != string::npos)
1032 {
1033 string dimmVpd = eachFru.key();
1034 // fetch it from
1035 // "/sys/bus/i2c/drivers/at24/414-0050/eeprom"
1036
1037 regex matchPatern("([0-9]+-[0-9]{4})");
1038 smatch matchFound;
1039 if (regex_search(dimmVpd, matchFound, matchPatern))
1040 {
1041 vector<string> i2cReg;
1042 boost::split(i2cReg, matchFound.str(0),
1043 boost::is_any_of("-"));
1044
1045 // remove 0s from begining
1046 const regex pattern("^0+(?!$)");
1047 for (auto& i : i2cReg)
1048 {
1049 i = regex_replace(i, pattern, "");
1050 }
1051
1052 if (i2cReg.size() == 2)
1053 {
1054 // echo 24c32 0x50 >
1055 // /sys/bus/i2c/devices/i2c-16/new_device
1056 string cmnd = "echo 24c32 0x" + i2cReg[1] +
1057 " > /sys/bus/i2c/devices/i2c-" +
1058 i2cReg[0] + "/new_device";
1059
1060 executeCmd(cmnd);
1061 }
1062 }
1063 }
1064 }
1065 }
1066 }
1067 }
1068}
1069
1070/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301071 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301072 * This method invokes all the populateInterface functions
1073 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301074 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
1075 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301076 * @param[in] js - Inventory json object
1077 * @param[in] filePath - Path of the vpd file
1078 * @param[in] preIntrStr - Interface string
1079 */
1080template <typename T>
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001081static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301082{
1083 inventory::InterfaceMap interfaces;
1084 inventory::ObjectMap objects;
1085 inventory::PropertyMap prop;
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001086 string ccinFromVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301087
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301088 bool isSystemVpd = (filePath == systemVpdFilePath);
1089 if constexpr (is_same<T, Parsed>::value)
1090 {
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001091 ccinFromVpd = getKwVal(vpdMap, "VINI", "CC");
1092 transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(),
1093 ::toupper);
1094
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301095 if (isSystemVpd)
1096 {
1097 std::vector<std::string> interfaces = {motherBoardInterface};
1098 // call mapper to check for object path creation
1099 MapperResponse subTree =
1100 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1101 string mboardPath =
1102 js["frus"][filePath].at(0).value("inventoryPath", "");
1103
1104 // Attempt system VPD restore if we have a motherboard
1105 // object in the inventory.
1106 if ((subTree.size() != 0) &&
1107 (subTree.find(pimPath + mboardPath) != subTree.end()))
1108 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001109 restoreSystemVPD(vpdMap, mboardPath);
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301110 }
1111 else
1112 {
1113 log<level::ERR>("No object path found");
1114 }
1115 }
alpana077ce68722021-07-25 13:23:59 -05001116 else
1117 {
1118 // check if it is processor vpd.
1119 auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath);
1120
1121 if (isPrimaryCpu)
1122 {
1123 auto ddVersion = getKwVal(vpdMap, "CRP0", "DD");
1124
1125 auto chipVersion = atoi(ddVersion.substr(1, 2).c_str());
1126
1127 if (chipVersion >= 2)
1128 {
1129 doEnableAllDimms(js);
1130 }
1131 }
1132 }
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301133 }
1134
Priyanga Ramasamy32c687f2022-01-04 23:14:03 -06001135 if (isSystemVpd)
1136 {
1137 string systemJsonName{};
1138 if constexpr (is_same<T, Parsed>::value)
1139 {
1140 // pick the right system json
1141 systemJsonName = getSystemsJson(vpdMap);
1142 }
1143
1144 fs::path target = systemJsonName;
1145 fs::path link = INVENTORY_JSON_SYM_LINK;
1146
1147 // Create the directory for hosting the symlink
1148 fs::create_directories(VPD_FILES_PATH);
1149 // unlink the symlink previously created (if any)
1150 remove(INVENTORY_JSON_SYM_LINK);
1151 // create a new symlink based on the system
1152 fs::create_symlink(target, link);
1153
1154 // Reloading the json
1155 ifstream inventoryJson(link);
1156 js = json::parse(inventoryJson);
1157 inventoryJson.close();
1158 }
1159
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301160 for (const auto& item : js["frus"][filePath])
1161 {
1162 const auto& objectPath = item["inventoryPath"];
1163 sdbusplus::message::object_path object(objectPath);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -05001164
Shantappa Teekappanavar6aa54502021-12-09 12:59:56 -06001165 vector<string> ccinList;
1166 if (item.find("ccin") != item.end())
1167 {
1168 for (const auto& cc : item["ccin"])
1169 {
1170 string ccin = cc;
1171 transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper);
1172 ccinList.push_back(ccin);
1173 }
1174 }
1175
1176 if (!ccinFromVpd.empty() && !ccinList.empty() &&
1177 (find(ccinList.begin(), ccinList.end(), ccinFromVpd) ==
1178 ccinList.end()))
1179 {
1180 continue;
1181 }
1182
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001183 if ((isSystemVpd) || (item.value("noprime", false)))
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301184 {
Priyanga Ramasamye3fed702022-01-11 01:05:32 -06001185
1186 // Populate one time properties for the system VPD and its sub-frus
1187 // and for other non-primeable frus.
Santosh Puranikd3a379a2021-08-23 19:12:59 +05301188 // For the remaining FRUs, this will get handled as a part of
1189 // priming the inventory.
1190 setOneTimeProperties(objectPath, interfaces);
1191 }
1192
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301193 // Populate the VPD keywords and the common interfaces only if we
1194 // are asked to inherit that data from the VPD, else only add the
1195 // extraInterfaces.
1196 if (item.value("inherit", true))
1197 {
Alpana Kumari58e22142020-05-05 00:22:12 -05001198 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301199 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301200 // Each record in the VPD becomes an interface and all
1201 // keyword within the record are properties under that
1202 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301203 for (const auto& record : vpdMap)
1204 {
1205 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001206 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301207 }
1208 }
Alpana Kumari58e22142020-05-05 00:22:12 -05001209 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301210 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001211 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301212 }
Santosh Puranik88edeb62020-03-02 12:00:09 +05301213 if (js.find("commonInterfaces") != js.end())
1214 {
1215 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
1216 isSystemVpd);
1217 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301218 }
Santosh Puranik0859eb62020-03-16 02:56:29 -05001219 else
1220 {
1221 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -05001222 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -05001223 {
1224 if (item.find("copyRecords") != item.end())
1225 {
1226 for (const auto& record : item["copyRecords"])
1227 {
1228 const string& recordName = record;
1229 if (vpdMap.find(recordName) != vpdMap.end())
1230 {
1231 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05001232 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -05001233 interfaces);
1234 }
1235 }
1236 }
1237 }
1238 }
Santosh Puranik32c46502022-02-10 08:55:07 +05301239 // Populate interfaces and properties that are common to every FRU
1240 // and additional interface that might be defined on a per-FRU
1241 // basis.
1242 if (item.find("extraInterfaces") != item.end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301243 {
Santosh Puranik32c46502022-02-10 08:55:07 +05301244 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
1245 isSystemVpd);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301246 }
1247 objects.emplace(move(object), move(interfaces));
1248 }
1249
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301250 if (isSystemVpd)
1251 {
1252 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
1253 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -05001254
Alpana Kumarif05effd2021-04-07 07:32:53 -05001255 // set the U-boot environment variable for device-tree
1256 if constexpr (is_same<T, Parsed>::value)
1257 {
Santosh Puranike5f177a2022-01-24 20:14:46 +05301258 setDevTreeEnv(fs::path(getSystemsJson(vpdMap)).filename());
Alpana Kumarif05effd2021-04-07 07:32:53 -05001259 }
PriyangaRamasamy8e140a12020-04-13 19:24:03 +05301260 }
1261
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301262 // Notify PIM
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05001263 common::utility::callPIM(move(objects));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301264}
1265
1266int main(int argc, char** argv)
1267{
1268 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001269 json js{};
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001270 Binary vpdVector{};
1271 string file{};
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001272 // map to hold additional data in case of logging pel
1273 PelAdditionalData additionalData{};
1274
1275 // this is needed to hold base fru inventory path in case there is ECC or
1276 // vpd exception while parsing the file
1277 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301278
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001279 // severity for PEL
1280 PelSeverity pelSeverity = PelSeverity::WARNING;
1281
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301282 try
1283 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301284 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
1285 "in DBUS"};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301286
1287 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
Alpana Kumari2f793042020-08-18 05:51:03 -05001288 ->required();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301289
1290 CLI11_PARSE(app, argc, argv);
1291
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001292 // PEL severity should be ERROR in case of any system VPD failure
1293 if (file == systemVpdFilePath)
1294 {
1295 pelSeverity = PelSeverity::ERROR;
1296 }
1297
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301298 auto jsonToParse = INVENTORY_JSON_DEFAULT;
1299
1300 // If the symlink exists, it means it has been setup for us, switch the
1301 // path
1302 if (fs::exists(INVENTORY_JSON_SYM_LINK))
1303 {
1304 jsonToParse = INVENTORY_JSON_SYM_LINK;
1305 }
1306
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301307 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +05301308 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001309 if (!inventoryJson)
1310 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001311 throw(VpdJsonException("Failed to access Json path", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001312 }
1313
1314 try
1315 {
1316 js = json::parse(inventoryJson);
1317 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001318 catch (const json::parse_error& ex)
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001319 {
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001320 throw(VpdJsonException("Json parsing failed", jsonToParse));
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001321 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301322
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301323 // Do we have the mandatory "frus" section?
1324 if (js.find("frus") == js.end())
1325 {
1326 throw(VpdJsonException("FRUs section not found in JSON",
1327 jsonToParse));
1328 }
1329
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301330 // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@)
1331 if (file.find("/ahb:apb") != string::npos)
1332 {
1333 // Translate udev path to a generic /sys/bus/.. file path.
1334 udevToGenericPath(file);
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301335
1336 if ((js["frus"].find(file) != js["frus"].end()) &&
Santosh Puranik50f60bf2021-05-26 17:55:06 +05301337 (file == systemVpdFilePath))
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301338 {
Sunny Srivastava3c244142022-01-11 08:47:04 -06001339 // We need manager service active to process restoring of
1340 // system VPD on hardware. So in case any system restore is
1341 // required, update hardware in the second trigger of parser
1342 // code for system vpd file path.
1343
1344 std::vector<std::string> interfaces{motherBoardInterface};
1345
1346 // call mapper to check for object path creation
1347 MapperResponse subTree =
1348 getObjectSubtreeForInterfaces(pimPath, 0, interfaces);
1349 string mboardPath =
1350 js["frus"][file].at(0).value("inventoryPath", "");
1351
1352 // Attempt system VPD restore if we have a motherboard
1353 // object in the inventory.
1354 if ((subTree.size() != 0) &&
1355 (subTree.find(pimPath + mboardPath) != subTree.end()))
1356 {
1357 vpdVector = getVpdDataInVector(js, file);
1358 ParserInterface* parser =
1359 ParserFactory::getParser(vpdVector);
1360 variant<KeywordVpdMap, Store> parseResult;
1361 parseResult = parser->parse();
1362
1363 if (auto pVal = get_if<Store>(&parseResult))
1364 {
1365 // map to hold all the keywords whose value is blank and
1366 // needs to be updated at standby.
1367 vector<RestoredEeproms> blankSystemVpdProperties{};
1368 getListOfBlankSystemVpd(pVal->getVpdMap(), mboardPath,
1369 blankSystemVpdProperties);
1370
1371 // if system VPD restore is required, update the
1372 // EEPROM
1373 for (const auto& item : blankSystemVpdProperties)
1374 {
1375 updateHardware(get<0>(item), get<1>(item),
1376 get<2>(item), get<3>(item));
1377 }
1378 }
1379 else
1380 {
1381 std::cout << "Not a valid format to restore system VPD"
1382 << std::endl;
1383 }
1384 // release the parser object
1385 ParserFactory::freeParser(parser);
1386 }
1387 else
1388 {
1389 log<level::ERR>("No object path found");
1390 }
PriyangaRamasamy647868e2020-09-08 17:03:19 +05301391 return 0;
1392 }
1393 }
1394
1395 if (file.empty())
1396 {
1397 cerr << "The EEPROM path <" << file << "> is not valid.";
1398 return 0;
1399 }
Santosh Puranik12e24ff2021-05-11 19:33:50 +05301400 if (js["frus"].find(file) == js["frus"].end())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301401 {
Santosh Puranik88edeb62020-03-02 12:00:09 +05301402 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301403 }
1404
Alpana Kumari2f793042020-08-18 05:51:03 -05001405 if (!fs::exists(file))
1406 {
1407 cout << "Device path: " << file
1408 << " does not exist. Spurious udev event? Exiting." << endl;
1409 return 0;
1410 }
1411
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001412 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +05301413 // Check if we can read the VPD file based on the power state
Santosh Puranik27a5e952021-10-07 22:08:01 -05001414 // We skip reading VPD when the power is ON in two scenarios:
1415 // 1) The eeprom we are trying to read is that of the system VPD
1416 // 2) The JSON tells us that the FRU EEPROM cannot be read when
1417 // we are powered ON.
1418 if (js["frus"][file].at(0).value("powerOffOnly", false) ||
1419 (file == systemVpdFilePath))
Santosh Puranik85893752020-11-10 21:31:43 +05301420 {
1421 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
1422 getPowerState())
1423 {
1424 cout << "This VPD cannot be read when power is ON" << endl;
1425 return 0;
1426 }
1427 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001428
Alpana Kumari2f793042020-08-18 05:51:03 -05001429 try
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301430 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001431 vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamy33c61c22021-04-06 11:15:57 -05001432 ParserInterface* parser = ParserFactory::getParser(vpdVector);
Alpana Kumari2f793042020-08-18 05:51:03 -05001433 variant<KeywordVpdMap, Store> parseResult;
1434 parseResult = parser->parse();
SunnySrivastava19849a195542020-09-07 06:04:50 -05001435
Alpana Kumari2f793042020-08-18 05:51:03 -05001436 if (auto pVal = get_if<Store>(&parseResult))
1437 {
1438 populateDbus(pVal->getVpdMap(), js, file);
1439 }
1440 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
1441 {
1442 populateDbus(*pVal, js, file);
1443 }
1444
1445 // release the parser object
1446 ParserFactory::freeParser(parser);
1447 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001448 catch (const exception& e)
Alpana Kumari2f793042020-08-18 05:51:03 -05001449 {
1450 postFailAction(js, file);
PriyangaRamasamya504c3e2020-12-06 12:14:52 -06001451 throw;
Alpana Kumari2f793042020-08-18 05:51:03 -05001452 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301453 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001454 catch (const VpdJsonException& ex)
1455 {
1456 additionalData.emplace("JSON_PATH", ex.getJsonPath());
1457 additionalData.emplace("DESCRIPTION", ex.what());
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001458 createPEL(additionalData, pelSeverity, errIntfForJsonFailure);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001459
1460 cerr << ex.what() << "\n";
1461 rc = -1;
1462 }
1463 catch (const VpdEccException& ex)
1464 {
1465 additionalData.emplace("DESCRIPTION", "ECC check failed");
1466 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1467 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001468 createPEL(additionalData, pelSeverity, errIntfForEccCheckFail);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001469 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001470 cerr << ex.what() << "\n";
1471 rc = -1;
1472 }
1473 catch (const VpdDataException& ex)
1474 {
1475 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
1476 additionalData.emplace("CALLOUT_INVENTORY_PATH",
1477 INVENTORY_PATH + baseFruInventoryPath);
Sunny Srivastava0746eee2021-03-22 13:36:54 -05001478 createPEL(additionalData, pelSeverity, errIntfForInvalidVPD);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001479 dumpBadVpd(file, vpdVector);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05001480 cerr << ex.what() << "\n";
1481 rc = -1;
1482 }
Patrick Williams8e15b932021-10-06 13:04:22 -05001483 catch (const exception& e)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301484 {
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -06001485 dumpBadVpd(file, vpdVector);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301486 cerr << e.what() << "\n";
1487 rc = -1;
1488 }
1489
1490 return rc;
Priyanga Ramasamy0d61c582022-01-21 04:38:22 -06001491}