blob: 83e2cd2819f99809968c675a8a2415826328520c [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/**
44 * @brief Expands location codes
45 */
46static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
47 bool isSystemVpd)
48{
49 auto expanded{unexpanded};
50 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
51 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
52 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
53 size_t idx = expanded.find("fcs");
54 try
55 {
56 if (idx != string::npos)
57 {
58 string fc{};
59 string se{};
60 if (isSystemVpd)
61 {
62 const auto& fcData = vpdMap.at("VCEN").at("FC");
63 const auto& seData = vpdMap.at("VCEN").at("SE");
64 fc = string(fcData.data(), fcData.size());
65 se = string(seData.data(), seData.size());
66 }
67 else
68 {
69 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
70 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
71 }
72
73 // TODO: See if ND1 can be placed in the JSON
74 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
75 }
76 else
77 {
78 idx = expanded.find("mts");
79 if (idx != string::npos)
80 {
81 string mt{};
82 string se{};
83 if (isSystemVpd)
84 {
85 const auto& mtData = vpdMap.at("VSYS").at("TM");
86 const auto& seData = vpdMap.at("VSYS").at("SE");
87 mt = string(mtData.data(), mtData.size());
88 se = string(seData.data(), seData.size());
89 }
90 else
91 {
92 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
93 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
94 }
95
96 replace(mt.begin(), mt.end(), '-', '.');
97 expanded.replace(idx, 3, mt + "." + se);
98 }
99 }
100 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500101 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530102 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500103 cerr << "Failed to expand location code with exception: " << e.what()
104 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +0530105 }
106 return expanded;
107}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530108/**
109 * @brief Populate FRU specific interfaces.
110 *
111 * This is a common method which handles both
112 * ipz and keyword specific interfaces thus,
113 * reducing the code redundancy.
114 * @param[in] map - Reference to the innermost keyword-value map.
115 * @param[in] preIntrStr - Reference to the interface string.
116 * @param[out] interfaces - Reference to interface map.
117 */
118template <typename T>
119static void populateFruSpecificInterfaces(const T& map,
120 const string& preIntrStr,
121 inventory::InterfaceMap& interfaces)
122{
123 inventory::PropertyMap prop;
124
125 for (const auto& kwVal : map)
126 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500127 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530128
129 auto kw = kwVal.first;
130
131 if (kw[0] == '#')
132 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500133 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530134 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500135 else if (isdigit(kw[0]))
136 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500137 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500138 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530139 prop.emplace(move(kw), move(vec));
140 }
141
142 interfaces.emplace(preIntrStr, move(prop));
143}
144
145/**
146 * @brief Populate Interfaces.
147 *
148 * This method populates common and extra interfaces to dbus.
149 * @param[in] js - json object
150 * @param[out] interfaces - Reference to interface map
151 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530152 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530153 */
154template <typename T>
155static void populateInterfaces(const nlohmann::json& js,
156 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530157 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530158{
159 for (const auto& ifs : js.items())
160 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530161 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530162 inventory::PropertyMap props;
163
164 for (const auto& itr : ifs.value().items())
165 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530166 const string& busProp = itr.key();
167
Alpana Kumari31970de2020-02-17 06:49:57 -0600168 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530169 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530170 props.emplace(busProp, itr.value().get<bool>());
171 }
172 else if (itr.value().is_string())
173 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500174 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530175 {
176 if (busProp == "LocationCode" &&
177 inf == "com.ibm.ipzvpd.Location")
178 {
179 auto prop = expandLocationCode(
180 itr.value().get<string>(), vpdMap, isSystemVpd);
181 props.emplace(busProp, prop);
182 }
183 else
184 {
185 props.emplace(busProp, itr.value().get<string>());
186 }
187 }
188 else
189 {
190 props.emplace(busProp, itr.value().get<string>());
191 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530192 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600193 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530194 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600195 const string& rec = itr.value().value("recordName", "");
196 const string& kw = itr.value().value("keywordName", "");
197 const string& encoding = itr.value().value("encoding", "");
198
Alpana Kumari58e22142020-05-05 00:22:12 -0500199 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530200 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530201 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
202 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600203 {
204 auto encoded =
205 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530206 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600207 }
208 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500209 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600210 {
211 if (!kw.empty() && vpdMap.count(kw))
212 {
213 auto prop =
214 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
215 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530216 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600217 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530218 }
219 }
220 }
221 interfaces.emplace(inf, move(props));
222 }
223}
224
Alpana Kumari58e22142020-05-05 00:22:12 -0500225Binary getVpdDataInVector(nlohmann::json& js, const string& file)
226{
227 uint32_t offset = 0;
228 // check if offset present?
229 for (const auto& item : js["frus"][file])
230 {
231 if (item.find("offset") != item.end())
232 {
233 offset = item["offset"];
234 }
235 }
236
237 // TODO: Figure out a better way to get max possible VPD size.
238 Binary vpdVector;
239 vpdVector.resize(65504);
240 ifstream vpdFile;
241 vpdFile.open(file, ios::binary);
242
243 vpdFile.seekg(offset, ios_base::cur);
244 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
245 vpdVector.resize(vpdFile.gcount());
246
247 return vpdVector;
248}
249
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530250/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530251 * @brief Prime the Inventory
252 * Prime the inventory by populating only the location code,
253 * type interface and the inventory object for the frus
254 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530255 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530256 * @param[in] jsObject - Reference to vpd inventory json object
257 * @param[in] vpdMap - Reference to the parsed vpd map
258 *
259 * @returns Map of items in extraInterface.
260 */
261template <typename T>
262inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
263 const T& vpdMap)
264{
265 inventory::ObjectMap objects;
266
267 for (auto& itemFRUS : jsObject["frus"].items())
268 {
269 for (auto& itemEEPROM : itemFRUS.value())
270 {
271 inventory::InterfaceMap interfaces;
272 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
273 inventory::Object object(itemEEPROM.at("inventoryPath"));
274
275 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
276 {
277 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
278 {
279 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
280 {
281 inventory::PropertyMap props;
282 if (eI.key() ==
283 openpower::vpd::constants::LOCATION_CODE_INF)
284 {
285 if constexpr (std::is_same<T, Parsed>::value)
286 {
287 for (auto& lC : eI.value().items())
288 {
289 auto propVal = expandLocationCode(
290 lC.value().get<string>(), vpdMap, true);
291
292 props.emplace(move(lC.key()),
293 move(propVal));
294 interfaces.emplace(move(eI.key()),
295 move(props));
296 }
297 }
298 }
299 else if (eI.key().find("Inventory.Item.") !=
300 string::npos)
301 {
302 interfaces.emplace(move(eI.key()), move(props));
303 }
304 }
305 }
306 objects.emplace(move(object), move(interfaces));
307 }
308 }
309 }
310 return objects;
311}
312
Alpana Kumari65b83602020-09-01 00:24:56 -0500313/* It does nothing. Just an empty function to return null
314 * at the end of variadic template args
315 */
316string getCommand()
317{
318 return "";
319}
320
321/* This function to arrange all arguments to make command
322 */
323template <typename T, typename... Types>
324string getCommand(T arg1, Types... args)
325{
326 string cmd = " " + arg1 + getCommand(args...);
327
328 return cmd;
329}
330
331/* This API takes arguments and run that command
332 * returns output of that command
333 */
334template <typename T, typename... Types>
335static vector<string> executeCmd(T& path, Types... args)
336{
337 vector<string> stdOutput;
338 array<char, 128> buffer;
339
340 string cmd = path + getCommand(args...);
341
342 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
343 if (!pipe)
344 {
345 throw runtime_error("popen() failed!");
346 }
347 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
348 {
349 stdOutput.emplace_back(buffer.data());
350 }
351
352 return stdOutput;
353}
354
355/**
356 * @brief This API executes command to set environment variable
357 * And then reboot the system
358 * @param[in] key -env key to set new value
359 * @param[in] value -value to set.
360 */
361void setEnvAndReboot(const string& key, const string& value)
362{
363 // set env and reboot and break.
364 executeCmd("/sbin/fw_setenv", key, value);
Andrew Geissler280197e2020-12-08 20:51:49 -0600365 log<level::INFO>("Rebooting BMC to pick up new device tree");
Alpana Kumari65b83602020-09-01 00:24:56 -0500366 // make dbus call to reboot
367 auto bus = sdbusplus::bus::new_default_system();
368 auto method = bus.new_method_call(
369 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
370 "org.freedesktop.systemd1.Manager", "Reboot");
371 bus.call_noreply(method);
372}
373
374/*
375 * @brief This API checks for env var fitconfig.
376 * If not initialised OR updated as per the current system type,
377 * update this env var and reboot the system.
378 *
379 * @param[in] systemType IM kwd in vpd tells about which system type it is.
380 * */
381void setDevTreeEnv(const string& systemType)
382{
383 string newDeviceTree;
384
385 if (deviceTreeSystemTypeMap.find(systemType) !=
386 deviceTreeSystemTypeMap.end())
387 {
388 newDeviceTree = deviceTreeSystemTypeMap.at(systemType);
389 }
390
391 string readVarValue;
392 bool envVarFound = false;
393
394 vector<string> output = executeCmd("/sbin/fw_printenv");
395 for (const auto& entry : output)
396 {
397 size_t pos = entry.find("=");
398 string key = entry.substr(0, pos);
399 if (key != "fitconfig")
400 {
401 continue;
402 }
403
404 envVarFound = true;
405 if (pos + 1 < entry.size())
406 {
407 readVarValue = entry.substr(pos + 1);
408 if (readVarValue.find(newDeviceTree) != string::npos)
409 {
410 // fitconfig is Updated. No action needed
411 break;
412 }
413 }
414 // set env and reboot and break.
415 setEnvAndReboot(key, newDeviceTree);
416 exit(0);
417 }
418
419 // check If env var Not found
420 if (!envVarFound)
421 {
422 setEnvAndReboot("fitconfig", newDeviceTree);
423 }
424}
425
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530426/**
427 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530428 * This method invokes all the populateInterface functions
429 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530430 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
431 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530432 * @param[in] js - Inventory json object
433 * @param[in] filePath - Path of the vpd file
434 * @param[in] preIntrStr - Interface string
435 */
436template <typename T>
437static void populateDbus(const T& vpdMap, nlohmann::json& js,
Alpana Kumari65b83602020-09-01 00:24:56 -0500438 const string& filePath)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530439{
440 inventory::InterfaceMap interfaces;
441 inventory::ObjectMap objects;
442 inventory::PropertyMap prop;
443
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500444 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530445 for (const auto& item : js["frus"][filePath])
446 {
447 const auto& objectPath = item["inventoryPath"];
448 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530449 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530450 // Populate the VPD keywords and the common interfaces only if we
451 // are asked to inherit that data from the VPD, else only add the
452 // extraInterfaces.
453 if (item.value("inherit", true))
454 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500455 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530456 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530457 // Each record in the VPD becomes an interface and all
458 // keyword within the record are properties under that
459 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530460 for (const auto& record : vpdMap)
461 {
462 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500463 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530464 }
465 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500466 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530467 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500468 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530469 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530470 if (js.find("commonInterfaces") != js.end())
471 {
472 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
473 isSystemVpd);
474 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530475 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500476 else
477 {
478 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500479 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500480 {
481 if (item.find("copyRecords") != item.end())
482 {
483 for (const auto& record : item["copyRecords"])
484 {
485 const string& recordName = record;
486 if (vpdMap.find(recordName) != vpdMap.end())
487 {
488 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500489 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500490 interfaces);
491 }
492 }
493 }
494 }
495 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530496
Alpana Kumari58e22142020-05-05 00:22:12 -0500497 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530498 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500499 // Populate interfaces and properties that are common to every FRU
500 // and additional interface that might be defined on a per-FRU
501 // basis.
502 if (item.find("extraInterfaces") != item.end())
503 {
504 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
505 isSystemVpd);
506 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530507 }
508 objects.emplace(move(object), move(interfaces));
509 }
510
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530511 if (isSystemVpd)
512 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530513 vector<uint8_t> imVal;
514 if constexpr (is_same<T, Parsed>::value)
515 {
516 auto property = vpdMap.find("VSBP");
517 if (property != vpdMap.end())
518 {
519 auto value = (property->second).find("IM");
520 if (value != (property->second).end())
521 {
522 copy(value->second.begin(), value->second.end(),
523 back_inserter(imVal));
524 }
525 }
526 }
527
528 fs::path target;
529 fs::path link = INVENTORY_JSON_SYM_LINK;
530
531 ostringstream oss;
532 for (auto& i : imVal)
533 {
534 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
535 }
536 string imValStr = oss.str();
537
Alpana Kumari65b83602020-09-01 00:24:56 -0500538 if (imValStr == RAINIER_4U) // 4U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530539 {
540 target = INVENTORY_JSON_4U;
541 }
Alpana Kumari65b83602020-09-01 00:24:56 -0500542 else if (imValStr == RAINIER_2U) // 2U
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530543 {
544 target = INVENTORY_JSON_2U;
545 }
Santosh Puranik4641bff2020-11-30 20:26:44 +0530546 else if (imValStr == EVEREST)
547 {
548 target = INVENTORY_JSON_EVEREST;
549 }
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530550
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530551 // Create the directory for hosting the symlink
552 fs::create_directories(VPD_FILES_PATH);
553 // unlink the symlink previously created (if any)
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530554 remove(INVENTORY_JSON_SYM_LINK);
555 // create a new symlink based on the system
556 fs::create_symlink(target, link);
557
558 // Reloading the json
559 ifstream inventoryJson(link);
560 auto js = json::parse(inventoryJson);
561 inventoryJson.close();
562
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530563 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
564 objects.insert(primeObject.begin(), primeObject.end());
Alpana Kumari65b83602020-09-01 00:24:56 -0500565
566 // set the U-boot environment variable for device-tree
567 setDevTreeEnv(imValStr);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530568 }
569
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530570 // Notify PIM
571 inventory::callPIM(move(objects));
572}
573
574int main(int argc, char** argv)
575{
576 int rc = 0;
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500577 string file{};
578 json js{};
579
580 // map to hold additional data in case of logging pel
581 PelAdditionalData additionalData{};
582
583 // this is needed to hold base fru inventory path in case there is ECC or
584 // vpd exception while parsing the file
585 std::string baseFruInventoryPath = {};
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530586
587 try
588 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530589 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
590 "in DBUS"};
591 string file{};
592
593 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
594 ->required()
595 ->check(ExistingFile);
596
597 CLI11_PARSE(app, argc, argv);
598
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530599 auto jsonToParse = INVENTORY_JSON_DEFAULT;
600
601 // If the symlink exists, it means it has been setup for us, switch the
602 // path
603 if (fs::exists(INVENTORY_JSON_SYM_LINK))
604 {
605 jsonToParse = INVENTORY_JSON_SYM_LINK;
606 }
607
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530608 // Make sure that the file path we get is for a supported EEPROM
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530609 ifstream inventoryJson(jsonToParse);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500610 if (!inventoryJson)
611 {
612 throw(
613 (VpdJsonException("Failed to access Json path", jsonToParse)));
614 }
615
616 try
617 {
618 js = json::parse(inventoryJson);
619 }
620 catch (json::parse_error& ex)
621 {
622 throw((VpdJsonException("Json parsing failed", jsonToParse)));
623 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530624
625 if ((js.find("frus") == js.end()) ||
626 (js["frus"].find(file) == js["frus"].end()))
627 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500628 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530629 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530630 }
631
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500632 baseFruInventoryPath = js["frus"][file][0]["inventoryPath"];
633
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500634 Binary vpdVector = getVpdDataInVector(js, file);
Alpana Kumari65b83602020-09-01 00:24:56 -0500635 ParserInterface* parser = ParserFactory::getParser(move(vpdVector));
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500636
637 variant<KeywordVpdMap, Store> parseResult;
638 parseResult = parser->parse();
639
640 if (auto pVal = get_if<Store>(&parseResult))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530641 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500642 populateDbus(pVal->getVpdMap(), js, file);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530643 }
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500644 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
645 {
646 populateDbus(*pVal, js, file);
647 }
648
649 // release the parser object
650 ParserFactory::freeParser(parser);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530651 }
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500652 catch (const VpdJsonException& ex)
653 {
654 additionalData.emplace("JSON_PATH", ex.getJsonPath());
655 additionalData.emplace("DESCRIPTION", ex.what());
656 createPEL(additionalData, errIntfForJsonFailure);
657
658 cerr << ex.what() << "\n";
659 rc = -1;
660 }
661 catch (const VpdEccException& ex)
662 {
663 additionalData.emplace("DESCRIPTION", "ECC check failed");
664 additionalData.emplace("CALLOUT_INVENTORY_PATH",
665 INVENTORY_PATH + baseFruInventoryPath);
666 createPEL(additionalData, errIntfForEccCheckFail);
667
668 cerr << ex.what() << "\n";
669 rc = -1;
670 }
671 catch (const VpdDataException& ex)
672 {
673 additionalData.emplace("DESCRIPTION", "Invalid VPD data");
674 additionalData.emplace("CALLOUT_INVENTORY_PATH",
675 INVENTORY_PATH + baseFruInventoryPath);
676 createPEL(additionalData, errIntfForInvalidVPD);
677
678 cerr << ex.what() << "\n";
679 rc = -1;
680 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530681 catch (exception& e)
682 {
683 cerr << e.what() << "\n";
684 rc = -1;
685 }
686
687 return rc;
688}