blob: 624118852f250edc07351bda565b9a1218248130 [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
3#include "defines.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05004#include "ipz_parser.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05305#include "keyword_vpd_parser.hpp"
Alpana Kumaria00936f2020-04-14 07:15:46 -05006#include "memory_vpd_parser.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05007#include "parser_factory.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05308#include "utils.hpp"
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -05009#include "vpd_exceptions.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053010
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -050011#include <ctype.h>
12
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053013#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053014#include <algorithm>
Alpana Kumari65b83602020-09-01 00:24:56 -050015#include <cstdarg>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053016#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053017#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053018#include <fstream>
19#include <iostream>
20#include <iterator>
21#include <nlohmann/json.hpp>
Andrew Geissler280197e2020-12-08 20:51:49 -060022#include <phosphor-logging/log.hpp>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053023
24using namespace std;
25using namespace openpower::vpd;
26using namespace CLI;
27using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053028using namespace openpower::vpd::constants;
29namespace fs = filesystem;
30using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050031using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050032using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050033using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050034using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050035using namespace openpower::vpd::exceptions;
Andrew Geissler280197e2020-12-08 20:51:49 -060036using namespace phosphor::logging;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053037
Alpana Kumari65b83602020-09-01 00:24:56 -050038static const deviceTreeMap deviceTreeSystemTypeMap = {
Santosh Puranik4641bff2020-11-30 20:26:44 +053039 {RAINIER_2U, "conf@aspeed-bmc-ibm-rainier.dtb"},
40 {RAINIER_4U, "conf@aspeed-bmc-ibm-rainier-4u.dtb"},
41 {EVEREST, "conf@aspeed-bmc-ibm-everest.dtb"}};
Alpana Kumari65b83602020-09-01 00:24:56 -050042
Santosh Puranik88edeb62020-03-02 12:00:09 +053043/**
Santosh Puranik85893752020-11-10 21:31:43 +053044 * @brief Returns the power state for chassis0
45 */
46static auto getPowerState()
47{
48 // TODO: How do we handle multiple chassis?
49 string powerState{};
50 auto bus = sdbusplus::bus::new_default();
51 auto properties =
52 bus.new_method_call("xyz.openbmc_project.State.Chassis",
53 "/xyz/openbmc_project/state/chassis0",
54 "org.freedesktop.DBus.Properties", "Get");
55 properties.append("xyz.openbmc_project.State.Chassis");
56 properties.append("CurrentPowerState");
57 auto result = bus.call(properties);
58 if (!result.is_method_error())
59 {
60 variant<string> val;
61 result.read(val);
62 if (auto pVal = get_if<string>(&val))
63 {
64 powerState = *pVal;
65 }
66 }
67 cout << "Power state is: " << powerState << endl;
68 return powerState;
69}
70
71/**
Santosh Puranik88edeb62020-03-02 12:00:09 +053072 * @brief Expands location codes
73 */
74static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
75 bool isSystemVpd)
76{
77 auto expanded{unexpanded};
78 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
79 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
80 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
81 size_t idx = expanded.find("fcs");
82 try
83 {
84 if (idx != string::npos)
85 {
86 string fc{};
87 string se{};
88 if (isSystemVpd)
89 {
90 const auto& fcData = vpdMap.at("VCEN").at("FC");
91 const auto& seData = vpdMap.at("VCEN").at("SE");
92 fc = string(fcData.data(), fcData.size());
93 se = string(seData.data(), seData.size());
94 }
95 else
96 {
97 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
98 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
99 }
100
101 // TODO: See if ND1 can be placed in the JSON
102 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
103 }
104 else
105 {
106 idx = expanded.find("mts");
107 if (idx != string::npos)
108 {
109 string mt{};
110 string se{};
111 if (isSystemVpd)
112 {
113 const auto& mtData = vpdMap.at("VSYS").at("TM");
114 const auto& seData = vpdMap.at("VSYS").at("SE");
115 mt = string(mtData.data(), mtData.size());
116 se = string(seData.data(), seData.size());
117 }
118 else
119 {
120 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
121 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
122 }
123
124 replace(mt.begin(), mt.end(), '-', '.');
125 expanded.replace(idx, 3, mt + "." + se);
126 }
127 }
128 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500129 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530130 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500131 cerr << "Failed to expand location code with exception: " << e.what()
132 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530133 }
134 return expanded;
135}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530136/**
137 * @brief Populate FRU specific interfaces.
138 *
139 * This is a common method which handles both
140 * ipz and keyword specific interfaces thus,
141 * reducing the code redundancy.
142 * @param[in] map - Reference to the innermost keyword-value map.
143 * @param[in] preIntrStr - Reference to the interface string.
144 * @param[out] interfaces - Reference to interface map.
145 */
146template <typename T>
147static void populateFruSpecificInterfaces(const T& map,
148 const string& preIntrStr,
149 inventory::InterfaceMap& interfaces)
150{
151 inventory::PropertyMap prop;
152
153 for (const auto& kwVal : map)
154 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500155 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530156
157 auto kw = kwVal.first;
158
159 if (kw[0] == '#')
160 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500161 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530162 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500163 else if (isdigit(kw[0]))
164 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500165 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500166 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530167 prop.emplace(move(kw), move(vec));
168 }
169
170 interfaces.emplace(preIntrStr, move(prop));
171}
172
173/**
174 * @brief Populate Interfaces.
175 *
176 * This method populates common and extra interfaces to dbus.
177 * @param[in] js - json object
178 * @param[out] interfaces - Reference to interface map
179 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530180 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530181 */
182template <typename T>
183static void populateInterfaces(const nlohmann::json& js,
184 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530185 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530186{
187 for (const auto& ifs : js.items())
188 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530189 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530190 inventory::PropertyMap props;
191
192 for (const auto& itr : ifs.value().items())
193 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530194 const string& busProp = itr.key();
195
Alpana Kumari31970de2020-02-17 06:49:57 -0600196 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530197 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530198 props.emplace(busProp, itr.value().get<bool>());
199 }
200 else if (itr.value().is_string())
201 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500202 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530203 {
204 if (busProp == "LocationCode" &&
205 inf == "com.ibm.ipzvpd.Location")
206 {
207 auto prop = expandLocationCode(
208 itr.value().get<string>(), vpdMap, isSystemVpd);
209 props.emplace(busProp, prop);
210 }
211 else
212 {
213 props.emplace(busProp, itr.value().get<string>());
214 }
215 }
216 else
217 {
218 props.emplace(busProp, itr.value().get<string>());
219 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530220 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600221 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530222 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600223 const string& rec = itr.value().value("recordName", "");
224 const string& kw = itr.value().value("keywordName", "");
225 const string& encoding = itr.value().value("encoding", "");
226
Alpana Kumari58e22142020-05-05 00:22:12 -0500227 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530228 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530229 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
230 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600231 {
232 auto encoded =
233 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530234 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600235 }
236 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500237 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600238 {
239 if (!kw.empty() && vpdMap.count(kw))
240 {
241 auto prop =
242 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
243 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530244 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600245 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530246 }
247 }
248 }
249 interfaces.emplace(inf, move(props));
250 }
251}
252
Alpana Kumari58e22142020-05-05 00:22:12 -0500253Binary getVpdDataInVector(nlohmann::json& js, const string& file)
254{
255 uint32_t offset = 0;
256 // check if offset present?
257 for (const auto& item : js["frus"][file])
258 {
259 if (item.find("offset") != item.end())
260 {
261 offset = item["offset"];
262 }
263 }
264
265 // TODO: Figure out a better way to get max possible VPD size.
266 Binary vpdVector;
267 vpdVector.resize(65504);
268 ifstream vpdFile;
269 vpdFile.open(file, ios::binary);
270
271 vpdFile.seekg(offset, ios_base::cur);
272 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
273 vpdVector.resize(vpdFile.gcount());
274
275 return vpdVector;
276}
277
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530278/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530279 * @brief Prime the Inventory
280 * Prime the inventory by populating only the location code,
281 * type interface and the inventory object for the frus
282 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530283 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530284 * @param[in] jsObject - Reference to vpd inventory json object
285 * @param[in] vpdMap - Reference to the parsed vpd map
286 *
287 * @returns Map of items in extraInterface.
288 */
289template <typename T>
290inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
291 const T& vpdMap)
292{
293 inventory::ObjectMap objects;
294
295 for (auto& itemFRUS : jsObject["frus"].items())
296 {
297 for (auto& itemEEPROM : itemFRUS.value())
298 {
299 inventory::InterfaceMap interfaces;
300 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
301 inventory::Object object(itemEEPROM.at("inventoryPath"));
302
303 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
304 {
305 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
306 {
307 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
308 {
309 inventory::PropertyMap props;
310 if (eI.key() ==
311 openpower::vpd::constants::LOCATION_CODE_INF)
312 {
313 if constexpr (std::is_same<T, Parsed>::value)
314 {
315 for (auto& lC : eI.value().items())
316 {
317 auto propVal = expandLocationCode(
318 lC.value().get<string>(), vpdMap, true);
319
320 props.emplace(move(lC.key()),
321 move(propVal));
322 interfaces.emplace(move(eI.key()),
323 move(props));
324 }
325 }
326 }
327 else if (eI.key().find("Inventory.Item.") !=
328 string::npos)
329 {
330 interfaces.emplace(move(eI.key()), move(props));
331 }
332 }
333 }
334 objects.emplace(move(object), move(interfaces));
335 }
336 }
337 }
338 return objects;
339}
340
Alpana Kumari65b83602020-09-01 00:24:56 -0500341/* It does nothing. Just an empty function to return null
342 * at the end of variadic template args
343 */
344string getCommand()
345{
346 return "";
347}
348
349/* This function to arrange all arguments to make command
350 */
351template <typename T, typename... Types>
352string getCommand(T arg1, Types... args)
353{
354 string cmd = " " + arg1 + getCommand(args...);
355
356 return cmd;
357}
358
359/* This API takes arguments and run that command
360 * returns output of that command
361 */
362template <typename T, typename... Types>
363static vector<string> executeCmd(T& path, Types... args)
364{
365 vector<string> stdOutput;
366 array<char, 128> buffer;
367
368 string cmd = path + getCommand(args...);
369
370 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
371 if (!pipe)
372 {
373 throw runtime_error("popen() failed!");
374 }
375 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
376 {
377 stdOutput.emplace_back(buffer.data());
378 }
379
380 return stdOutput;
381}
382
383/**
384 * @brief This API executes command to set environment variable
385 * And then reboot the system
386 * @param[in] key -env key to set new value
387 * @param[in] value -value to set.
388 */
389void setEnvAndReboot(const string& key, const string& value)
390{
391 // set env and reboot and break.
392 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600393 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500394 // make dbus call to reboot
395 auto bus = sdbusplus::bus::new_default_system();
396 auto method = bus.new_method_call(
397 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
398 "org.freedesktop.systemd1.Manager", "Reboot");
399 bus.call_noreply(method);
400}
401
402/*
403 * @brief This API checks for env var fitconfig.
404 * If not initialised OR updated as per the current system type,
405 * update this env var and reboot the system.
406 *
407 * @param[in] systemType IM kwd in vpd tells about which system type it is.
408 * */
409void setDevTreeEnv(const string& systemType)
410{
411 string newDeviceTree;
412
413 if (deviceTreeSystemTypeMap.find(systemType) !=
414 deviceTreeSystemTypeMap.end())
415 {
416 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
417 }
418
419 string readVarValue;
420 bool envVarFound = false;
421
422 vector<string> output = executeCmd("/sbin/fw_printenv");
423 for (const auto& entry : output)
424 {
425 size_t pos = entry.find("=");
426 string key = entry.substr(0, pos);
427 if (key != "fitconfig")
428 {
429 continue;
430 }
431
432 envVarFound = true;
433 if (pos + 1 < entry.size())
434 {
435 readVarValue = entry.substr(pos + 1);
436 if (readVarValue.find(newDeviceTree) != string::npos)
437 {
438 // fitconfig is Updated. No action needed
439 break;
440 }
441 }
442 // set env and reboot and break.
443 setEnvAndReboot(key, newDeviceTree);
444 exit(0);
445 }
446
447 // check If env var Not found
448 if (!envVarFound)
449 {
450 setEnvAndReboot("fitconfig", newDeviceTree);
451 }
452}
453
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530454/**
455 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530456 * This method invokes all the populateInterface functions
457 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530458 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
459 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530460 * @param[in] js - Inventory json object
461 * @param[in] filePath - Path of the vpd file
462 * @param[in] preIntrStr - Interface string
463 */
464template <typename T>
465static void populateDbus(const T& vpdMap, nlohmann::json& js,
Alpana Kumari65b83602020-09-01 00:24:56 -0500466 const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530467{
468 inventory::InterfaceMap interfaces;
469 inventory::ObjectMap objects;
470 inventory::PropertyMap prop;
471
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500472 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530473 for (const auto& item : js["frus"][filePath])
474 {
475 const auto& objectPath = item["inventoryPath"];
476 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530477 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530478 // Populate the VPD keywords and the common interfaces only if we
479 // are asked to inherit that data from the VPD, else only add the
480 // extraInterfaces.
481 if (item.value("inherit", true))
482 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500483 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530484 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530485 // Each record in the VPD becomes an interface and all
486 // keyword within the record are properties under that
487 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530488 for (const auto& record : vpdMap)
489 {
490 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500491 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530492 }
493 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500494 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530495 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500496 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530497 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530498 if (js.find("commonInterfaces") != js.end())
499 {
500 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
501 isSystemVpd);
502 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530503 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500504 else
505 {
506 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500507 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500508 {
509 if (item.find("copyRecords") != item.end())
510 {
511 for (const auto& record : item["copyRecords"])
512 {
513 const string& recordName = record;
514 if (vpdMap.find(recordName) != vpdMap.end())
515 {
516 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500517 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500518 interfaces);
519 }
520 }
521 }
522 }
523 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530524
Alpana Kumari58e22142020-05-05 00:22:12 -0500525 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530526 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500527 // Populate interfaces and properties that are common to every FRU
528 // and additional interface that might be defined on a per-FRU
529 // basis.
530 if (item.find("extraInterfaces") != item.end())
531 {
532 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
533 isSystemVpd);
534 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530535 }
536 objects.emplace(move(object), move(interfaces));
537 }
538
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530539 if (isSystemVpd)
540 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530541 vector<uint8_t> imVal;
542 if constexpr (is_same<T, Parsed>::value)
543 {
544 auto property = vpdMap.find("VSBP");
545 if (property != vpdMap.end())
546 {
547 auto value = (property->second).find("IM");
548 if (value != (property->second).end())
549 {
550 copy(value->second.begin(), value->second.end(),
551 back_inserter(imVal));
552 }
553 }
554 }
555
556 fs::path target;
557 fs::path link = INVENTORY_JSON_SYM_LINK;
558
559 ostringstream oss;
560 for (auto& i : imVal)
561 {
562 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
563 }
564 string imValStr = oss.str();
565
Alpana Kumari65b83602020-09-01 00:24:56 -0500566 if (imValStr == RAINIER_4U) // 4U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530567 {
568 target = INVENTORY_JSON_4U;
569 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500570 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530571 {
572 target = INVENTORY_JSON_2U;
573 }
Santosh Puranik4641bff2020-11-30 20:26:44 +0530574 else if (imValStr == EVEREST)
575 {
576 target = INVENTORY_JSON_EVEREST;
577 }
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530578
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530579 // Create the directory for hosting the symlink
580 fs::create_directories(VPD_FILES_PATH);
581 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530582 remove(INVENTORY_JSON_SYM_LINK);
583 // create a new symlink based on the system
584 fs::create_symlink(target, link);
585
586 // Reloading the json
587 ifstream inventoryJson(link);
588 auto js = json::parse(inventoryJson);
589 inventoryJson.close();
590
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530591 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
592 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500593
594 // set the U-boot environment variable for device-tree
595 setDevTreeEnv(imValStr);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530596 }
597
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530598 // Notify PIM
599 inventory::callPIM(move(objects));
600}
601
602int main(int argc, char** argv)
603{
604 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500605 string file{};
606 json js{};
607
608 // map to hold additional data in case of logging pel
609 PelAdditionalData additionalData{};
610
611 // this is needed to hold base fru inventory path in case there is ECC or
612 // vpd exception while parsing the file
613 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530614
615 try
616 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530617 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
618 "in DBUS"};
619 string file{};
620
621 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
622 ->required()
623 ->check(ExistingFile);
624
625 CLI11_PARSE(app, argc, argv);
626
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530627 auto jsonToParse = INVENTORY_JSON_DEFAULT;
628
629 // If the symlink exists, it means it has been setup for us, switch the
630 // path
631 if (fs::exists(INVENTORY_JSON_SYM_LINK))
632 {
633 jsonToParse = INVENTORY_JSON_SYM_LINK;
634 }
635
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530636 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530637 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500638 if (!inventoryJson)
639 {
640 throw(
641 (VpdJsonException("Failed to access Json path", jsonToParse)));
642 }
643
644 try
645 {
646 js = json::parse(inventoryJson);
647 }
648 catch (json::parse_error& ex)
649 {
650 throw((VpdJsonException("Json parsing failed", jsonToParse)));
651 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530652
653 if ((js.find("frus") == js.end()) ||
654 (js["frus"].find(file) == js["frus"].end()))
655 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500656 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530657 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530658 }
659
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500660 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
Santosh Puranik85893752020-11-10 21:31:43 +0530661 // Check if we can read the VPD file based on the power state
662 if (js["frus"][file].at(0).value("powerOffOnly", false))
663 {
664 if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
665 getPowerState())
666 {
667 cout << "This VPD cannot be read when power is ON" << endl;
668 return 0;
669 }
670 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500671
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500672 Binary vpdVector = getVpdDataInVector(js, file);
Alpana Kumari65b83602020-09-01 00:24:56 -0500673 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500674
675 variant<KeywordVpdMap, Store> parseResult;
676 parseResult = parser->parse();
677
678 if (auto pVal = get_if<Store>(&parseResult))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530679 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500680 populateDbus(pVal->getVpdMap(), js, file);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530681 }
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500682 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
683 {
684 populateDbus(*pVal, js, file);
685 }
686
687 // release the parser object
688 ParserFactory::freeParser(parser);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530689 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500690 catch (const VpdJsonException& ex)
691 {
692 additionalData.emplace("JSON_PATH", ex.getJsonPath());
693 additionalData.emplace("DESCRIPTION", ex.what());
694 createPEL(additionalData, errIntfForJsonFailure);
695
696 cerr << ex.what() << "\n";
697 rc = -1;
698 }
699 catch (const VpdEccException& ex)
700 {
701 additionalData.emplace("DESCRIPTION", "ECC check failed");
702 additionalData.emplace("CALLOUT_INVENTORY_PATH",
703 INVENTORY_PATH + baseFruInventoryPath);
704 createPEL(additionalData, errIntfForEccCheckFail);
705
706 cerr << ex.what() << "\n";
707 rc = -1;
708 }
709 catch (const VpdDataException& ex)
710 {
711 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
712 additionalData.emplace("CALLOUT_INVENTORY_PATH",
713 INVENTORY_PATH + baseFruInventoryPath);
714 createPEL(additionalData, errIntfForInvalidVPD);
715
716 cerr << ex.what() << "\n";
717 rc = -1;
718 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530719 catch (exception& e)
720 {
721 cerr << e.what() << "\n";
722 rc = -1;
723 }
724
725 return rc;
726}