blob: 04bcbba2e6c887337f9a078f8e96ed2bdcd4bae7 [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>
22
23using namespace std;
24using namespace openpower::vpd;
25using namespace CLI;
26using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053027using namespace openpower::vpd::constants;
28namespace fs = filesystem;
29using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050030using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050031using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050032using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050033using namespace openpower::vpd::parser::interface;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050034using namespace openpower::vpd::exceptions;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053035
Alpana Kumari65b83602020-09-01 00:24:56 -050036static const deviceTreeMap deviceTreeSystemTypeMap = {
37 {RAINIER_2U, "conf@aspeed-bmc-ibm-rainier-2u.dtb"},
38 {RAINIER_4U, "conf@aspeed-bmc-ibm-rainier-4u.dtb"}};
39
Santosh Puranik88edeb62020-03-02 12:00:09 +053040/**
41 * @brief Expands location codes
42 */
43static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
44 bool isSystemVpd)
45{
46 auto expanded{unexpanded};
47 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
48 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
49 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
50 size_t idx = expanded.find("fcs");
51 try
52 {
53 if (idx != string::npos)
54 {
55 string fc{};
56 string se{};
57 if (isSystemVpd)
58 {
59 const auto& fcData = vpdMap.at("VCEN").at("FC");
60 const auto& seData = vpdMap.at("VCEN").at("SE");
61 fc = string(fcData.data(), fcData.size());
62 se = string(seData.data(), seData.size());
63 }
64 else
65 {
66 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
67 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
68 }
69
70 // TODO: See if ND1 can be placed in the JSON
71 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
72 }
73 else
74 {
75 idx = expanded.find("mts");
76 if (idx != string::npos)
77 {
78 string mt{};
79 string se{};
80 if (isSystemVpd)
81 {
82 const auto& mtData = vpdMap.at("VSYS").at("TM");
83 const auto& seData = vpdMap.at("VSYS").at("SE");
84 mt = string(mtData.data(), mtData.size());
85 se = string(seData.data(), seData.size());
86 }
87 else
88 {
89 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
90 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
91 }
92
93 replace(mt.begin(), mt.end(), '-', '.');
94 expanded.replace(idx, 3, mt + "." + se);
95 }
96 }
97 }
Alpana Kumari58e22142020-05-05 00:22:12 -050098 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +053099 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500100 cerr << "Failed to expand location code with exception: " << e.what()
101 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530102 }
103 return expanded;
104}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530105/**
106 * @brief Populate FRU specific interfaces.
107 *
108 * This is a common method which handles both
109 * ipz and keyword specific interfaces thus,
110 * reducing the code redundancy.
111 * @param[in] map - Reference to the innermost keyword-value map.
112 * @param[in] preIntrStr - Reference to the interface string.
113 * @param[out] interfaces - Reference to interface map.
114 */
115template <typename T>
116static void populateFruSpecificInterfaces(const T& map,
117 const string& preIntrStr,
118 inventory::InterfaceMap& interfaces)
119{
120 inventory::PropertyMap prop;
121
122 for (const auto& kwVal : map)
123 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500124 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530125
126 auto kw = kwVal.first;
127
128 if (kw[0] == '#')
129 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500130 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530131 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500132 else if (isdigit(kw[0]))
133 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500134 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500135 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530136 prop.emplace(move(kw), move(vec));
137 }
138
139 interfaces.emplace(preIntrStr, move(prop));
140}
141
142/**
143 * @brief Populate Interfaces.
144 *
145 * This method populates common and extra interfaces to dbus.
146 * @param[in] js - json object
147 * @param[out] interfaces - Reference to interface map
148 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530149 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530150 */
151template <typename T>
152static void populateInterfaces(const nlohmann::json& js,
153 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530154 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530155{
156 for (const auto& ifs : js.items())
157 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530158 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530159 inventory::PropertyMap props;
160
161 for (const auto& itr : ifs.value().items())
162 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530163 const string& busProp = itr.key();
164
Alpana Kumari31970de2020-02-17 06:49:57 -0600165 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530166 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530167 props.emplace(busProp, itr.value().get<bool>());
168 }
169 else if (itr.value().is_string())
170 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500171 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530172 {
173 if (busProp == "LocationCode" &&
174 inf == "com.ibm.ipzvpd.Location")
175 {
176 auto prop = expandLocationCode(
177 itr.value().get<string>(), vpdMap, isSystemVpd);
178 props.emplace(busProp, prop);
179 }
180 else
181 {
182 props.emplace(busProp, itr.value().get<string>());
183 }
184 }
185 else
186 {
187 props.emplace(busProp, itr.value().get<string>());
188 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530189 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600190 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530191 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600192 const string& rec = itr.value().value("recordName", "");
193 const string& kw = itr.value().value("keywordName", "");
194 const string& encoding = itr.value().value("encoding", "");
195
Alpana Kumari58e22142020-05-05 00:22:12 -0500196 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530197 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530198 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
199 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600200 {
201 auto encoded =
202 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530203 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600204 }
205 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500206 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600207 {
208 if (!kw.empty() && vpdMap.count(kw))
209 {
210 auto prop =
211 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
212 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530213 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600214 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530215 }
216 }
217 }
218 interfaces.emplace(inf, move(props));
219 }
220}
221
Alpana Kumari58e22142020-05-05 00:22:12 -0500222Binary getVpdDataInVector(nlohmann::json& js, const string& file)
223{
224 uint32_t offset = 0;
225 // check if offset present?
226 for (const auto& item : js["frus"][file])
227 {
228 if (item.find("offset") != item.end())
229 {
230 offset = item["offset"];
231 }
232 }
233
234 // TODO: Figure out a better way to get max possible VPD size.
235 Binary vpdVector;
236 vpdVector.resize(65504);
237 ifstream vpdFile;
238 vpdFile.open(file, ios::binary);
239
240 vpdFile.seekg(offset, ios_base::cur);
241 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
242 vpdVector.resize(vpdFile.gcount());
243
244 return vpdVector;
245}
246
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530247/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530248 * @brief Prime the Inventory
249 * Prime the inventory by populating only the location code,
250 * type interface and the inventory object for the frus
251 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530252 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530253 * @param[in] jsObject - Reference to vpd inventory json object
254 * @param[in] vpdMap - Reference to the parsed vpd map
255 *
256 * @returns Map of items in extraInterface.
257 */
258template <typename T>
259inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
260 const T& vpdMap)
261{
262 inventory::ObjectMap objects;
263
264 for (auto& itemFRUS : jsObject["frus"].items())
265 {
266 for (auto& itemEEPROM : itemFRUS.value())
267 {
268 inventory::InterfaceMap interfaces;
269 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
270 inventory::Object object(itemEEPROM.at("inventoryPath"));
271
272 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
273 {
274 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
275 {
276 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
277 {
278 inventory::PropertyMap props;
279 if (eI.key() ==
280 openpower::vpd::constants::LOCATION_CODE_INF)
281 {
282 if constexpr (std::is_same<T, Parsed>::value)
283 {
284 for (auto& lC : eI.value().items())
285 {
286 auto propVal = expandLocationCode(
287 lC.value().get<string>(), vpdMap, true);
288
289 props.emplace(move(lC.key()),
290 move(propVal));
291 interfaces.emplace(move(eI.key()),
292 move(props));
293 }
294 }
295 }
296 else if (eI.key().find("Inventory.Item.") !=
297 string::npos)
298 {
299 interfaces.emplace(move(eI.key()), move(props));
300 }
301 }
302 }
303 objects.emplace(move(object), move(interfaces));
304 }
305 }
306 }
307 return objects;
308}
309
Alpana Kumari65b83602020-09-01 00:24:56 -0500310/* It does nothing. Just an empty function to return null
311 * at the end of variadic template args
312 */
313string getCommand()
314{
315 return "";
316}
317
318/* This function to arrange all arguments to make command
319 */
320template <typename T, typename... Types>
321string getCommand(T arg1, Types... args)
322{
323 string cmd = " " + arg1 + getCommand(args...);
324
325 return cmd;
326}
327
328/* This API takes arguments and run that command
329 * returns output of that command
330 */
331template <typename T, typename... Types>
332static vector<string> executeCmd(T& path, Types... args)
333{
334 vector<string> stdOutput;
335 array<char, 128> buffer;
336
337 string cmd = path + getCommand(args...);
338
339 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
340 if (!pipe)
341 {
342 throw runtime_error("popen() failed!");
343 }
344 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
345 {
346 stdOutput.emplace_back(buffer.data());
347 }
348
349 return stdOutput;
350}
351
352/**
353 * @brief This API executes command to set environment variable
354 * And then reboot the system
355 * @param[in] key -env key to set new value
356 * @param[in] value -value to set.
357 */
358void setEnvAndReboot(const string& key, const string& value)
359{
360 // set env and reboot and break.
361 executeCmd("/sbin/fw_setenv", key, value);
362 // make dbus call to reboot
363 auto bus = sdbusplus::bus::new_default_system();
364 auto method = bus.new_method_call(
365 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
366 "org.freedesktop.systemd1.Manager", "Reboot");
367 bus.call_noreply(method);
368}
369
370/*
371 * @brief This API checks for env var fitconfig.
372 * If not initialised OR updated as per the current system type,
373 * update this env var and reboot the system.
374 *
375 * @param[in] systemType IM kwd in vpd tells about which system type it is.
376 * */
377void setDevTreeEnv(const string& systemType)
378{
379 string newDeviceTree;
380
381 if (deviceTreeSystemTypeMap.find(systemType) !=
382 deviceTreeSystemTypeMap.end())
383 {
384 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
385 }
386
387 string readVarValue;
388 bool envVarFound = false;
389
390 vector<string> output = executeCmd("/sbin/fw_printenv");
391 for (const auto& entry : output)
392 {
393 size_t pos = entry.find("=");
394 string key = entry.substr(0, pos);
395 if (key != "fitconfig")
396 {
397 continue;
398 }
399
400 envVarFound = true;
401 if (pos + 1 < entry.size())
402 {
403 readVarValue = entry.substr(pos + 1);
404 if (readVarValue.find(newDeviceTree) != string::npos)
405 {
406 // fitconfig is Updated. No action needed
407 break;
408 }
409 }
410 // set env and reboot and break.
411 setEnvAndReboot(key, newDeviceTree);
412 exit(0);
413 }
414
415 // check If env var Not found
416 if (!envVarFound)
417 {
418 setEnvAndReboot("fitconfig", newDeviceTree);
419 }
420}
421
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530422/**
423 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530424 * This method invokes all the populateInterface functions
425 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530426 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
427 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530428 * @param[in] js - Inventory json object
429 * @param[in] filePath - Path of the vpd file
430 * @param[in] preIntrStr - Interface string
431 */
432template <typename T>
433static void populateDbus(const T& vpdMap, nlohmann::json& js,
Alpana Kumari65b83602020-09-01 00:24:56 -0500434 const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530435{
436 inventory::InterfaceMap interfaces;
437 inventory::ObjectMap objects;
438 inventory::PropertyMap prop;
439
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500440 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530441 for (const auto& item : js["frus"][filePath])
442 {
443 const auto& objectPath = item["inventoryPath"];
444 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530445 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530446 // Populate the VPD keywords and the common interfaces only if we
447 // are asked to inherit that data from the VPD, else only add the
448 // extraInterfaces.
449 if (item.value("inherit", true))
450 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500451 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530452 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530453 // Each record in the VPD becomes an interface and all
454 // keyword within the record are properties under that
455 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530456 for (const auto& record : vpdMap)
457 {
458 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500459 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530460 }
461 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500462 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530463 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500464 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530465 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530466 if (js.find("commonInterfaces") != js.end())
467 {
468 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
469 isSystemVpd);
470 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530471 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500472 else
473 {
474 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500475 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500476 {
477 if (item.find("copyRecords") != item.end())
478 {
479 for (const auto& record : item["copyRecords"])
480 {
481 const string& recordName = record;
482 if (vpdMap.find(recordName) != vpdMap.end())
483 {
484 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500485 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500486 interfaces);
487 }
488 }
489 }
490 }
491 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530492
Alpana Kumari58e22142020-05-05 00:22:12 -0500493 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530494 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500495 // Populate interfaces and properties that are common to every FRU
496 // and additional interface that might be defined on a per-FRU
497 // basis.
498 if (item.find("extraInterfaces") != item.end())
499 {
500 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
501 isSystemVpd);
502 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530503 }
504 objects.emplace(move(object), move(interfaces));
505 }
506
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530507 if (isSystemVpd)
508 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530509 vector<uint8_t> imVal;
510 if constexpr (is_same<T, Parsed>::value)
511 {
512 auto property = vpdMap.find("VSBP");
513 if (property != vpdMap.end())
514 {
515 auto value = (property->second).find("IM");
516 if (value != (property->second).end())
517 {
518 copy(value->second.begin(), value->second.end(),
519 back_inserter(imVal));
520 }
521 }
522 }
523
524 fs::path target;
525 fs::path link = INVENTORY_JSON_SYM_LINK;
526
527 ostringstream oss;
528 for (auto& i : imVal)
529 {
530 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
531 }
532 string imValStr = oss.str();
533
Alpana Kumari65b83602020-09-01 00:24:56 -0500534 if (imValStr == RAINIER_4U) // 4U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530535 {
536 target = INVENTORY_JSON_4U;
537 }
538
Alpana Kumari65b83602020-09-01 00:24:56 -0500539 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530540 {
541 target = INVENTORY_JSON_2U;
542 }
543
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530544 // Create the directory for hosting the symlink
545 fs::create_directories(VPD_FILES_PATH);
546 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530547 remove(INVENTORY_JSON_SYM_LINK);
548 // create a new symlink based on the system
549 fs::create_symlink(target, link);
550
551 // Reloading the json
552 ifstream inventoryJson(link);
553 auto js = json::parse(inventoryJson);
554 inventoryJson.close();
555
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530556 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
557 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500558
559 // set the U-boot environment variable for device-tree
560 setDevTreeEnv(imValStr);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530561 }
562
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530563 // Notify PIM
564 inventory::callPIM(move(objects));
565}
566
567int main(int argc, char** argv)
568{
569 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500570 string file{};
571 json js{};
572
573 // map to hold additional data in case of logging pel
574 PelAdditionalData additionalData{};
575
576 // this is needed to hold base fru inventory path in case there is ECC or
577 // vpd exception while parsing the file
578 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530579
580 try
581 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530582 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
583 "in DBUS"};
584 string file{};
585
586 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
587 ->required()
588 ->check(ExistingFile);
589
590 CLI11_PARSE(app, argc, argv);
591
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530592 auto jsonToParse = INVENTORY_JSON_DEFAULT;
593
594 // If the symlink exists, it means it has been setup for us, switch the
595 // path
596 if (fs::exists(INVENTORY_JSON_SYM_LINK))
597 {
598 jsonToParse = INVENTORY_JSON_SYM_LINK;
599 }
600
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530601 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530602 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500603 if (!inventoryJson)
604 {
605 throw(
606 (VpdJsonException("Failed to access Json path", jsonToParse)));
607 }
608
609 try
610 {
611 js = json::parse(inventoryJson);
612 }
613 catch (json::parse_error& ex)
614 {
615 throw((VpdJsonException("Json parsing failed", jsonToParse)));
616 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530617
618 if ((js.find("frus") == js.end()) ||
619 (js["frus"].find(file) == js["frus"].end()))
620 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500621 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530622 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530623 }
624
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500625 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
626
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500627 Binary vpdVector = getVpdDataInVector(js, file);
Alpana Kumari65b83602020-09-01 00:24:56 -0500628 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500629
630 variant<KeywordVpdMap, Store> parseResult;
631 parseResult = parser->parse();
632
633 if (auto pVal = get_if<Store>(&parseResult))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530634 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500635 populateDbus(pVal->getVpdMap(), js, file);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530636 }
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500637 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
638 {
639 populateDbus(*pVal, js, file);
640 }
641
642 // release the parser object
643 ParserFactory::freeParser(parser);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530644 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500645 catch (const VpdJsonException& ex)
646 {
647 additionalData.emplace("JSON_PATH", ex.getJsonPath());
648 additionalData.emplace("DESCRIPTION", ex.what());
649 createPEL(additionalData, errIntfForJsonFailure);
650
651 cerr << ex.what() << "\n";
652 rc = -1;
653 }
654 catch (const VpdEccException& ex)
655 {
656 additionalData.emplace("DESCRIPTION", "ECC check failed");
657 additionalData.emplace("CALLOUT_INVENTORY_PATH",
658 INVENTORY_PATH + baseFruInventoryPath);
659 createPEL(additionalData, errIntfForEccCheckFail);
660
661 cerr << ex.what() << "\n";
662 rc = -1;
663 }
664 catch (const VpdDataException& ex)
665 {
666 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
667 additionalData.emplace("CALLOUT_INVENTORY_PATH",
668 INVENTORY_PATH + baseFruInventoryPath);
669 createPEL(additionalData, errIntfForInvalidVPD);
670
671 cerr << ex.what() << "\n";
672 rc = -1;
673 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530674 catch (exception& e)
675 {
676 cerr << e.what() << "\n";
677 rc = -1;
678 }
679
680 return rc;
681}