blob: b2659190a360cc5b4c1b6f1f5e175e2a1de575b9 [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
3#include "defines.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05304#include "keyword_vpd_parser.hpp"
Alpana Kumaria00936f2020-04-14 07:15:46 -05005#include "memory_vpd_parser.hpp"
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05306#include "parser.hpp"
7#include "utils.hpp"
8
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -05009#include <ctype.h>
10
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053011#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053012#include <algorithm>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053013#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053014#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053015#include <fstream>
16#include <iostream>
17#include <iterator>
18#include <nlohmann/json.hpp>
19
20using namespace std;
21using namespace openpower::vpd;
22using namespace CLI;
23using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053024using namespace openpower::vpd::constants;
25namespace fs = filesystem;
26using json = nlohmann::json;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050027using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050028using namespace openpower::vpd::memory::parser;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053029
Santosh Puranik88edeb62020-03-02 12:00:09 +053030/**
31 * @brief Expands location codes
32 */
33static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
34 bool isSystemVpd)
35{
36 auto expanded{unexpanded};
37 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
38 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
39 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
40 size_t idx = expanded.find("fcs");
41 try
42 {
43 if (idx != string::npos)
44 {
45 string fc{};
46 string se{};
47 if (isSystemVpd)
48 {
49 const auto& fcData = vpdMap.at("VCEN").at("FC");
50 const auto& seData = vpdMap.at("VCEN").at("SE");
51 fc = string(fcData.data(), fcData.size());
52 se = string(seData.data(), seData.size());
53 }
54 else
55 {
56 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
57 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
58 }
59
60 // TODO: See if ND1 can be placed in the JSON
61 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
62 }
63 else
64 {
65 idx = expanded.find("mts");
66 if (idx != string::npos)
67 {
68 string mt{};
69 string se{};
70 if (isSystemVpd)
71 {
72 const auto& mtData = vpdMap.at("VSYS").at("TM");
73 const auto& seData = vpdMap.at("VSYS").at("SE");
74 mt = string(mtData.data(), mtData.size());
75 se = string(seData.data(), seData.size());
76 }
77 else
78 {
79 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
80 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
81 }
82
83 replace(mt.begin(), mt.end(), '-', '.');
84 expanded.replace(idx, 3, mt + "." + se);
85 }
86 }
87 }
Alpana Kumari58e22142020-05-05 00:22:12 -050088 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +053089 {
Alpana Kumari58e22142020-05-05 00:22:12 -050090 cerr << "Failed to expand location code with exception: " << e.what()
91 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +053092 }
93 return expanded;
94}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053095/**
96 * @brief Populate FRU specific interfaces.
97 *
98 * This is a common method which handles both
99 * ipz and keyword specific interfaces thus,
100 * reducing the code redundancy.
101 * @param[in] map - Reference to the innermost keyword-value map.
102 * @param[in] preIntrStr - Reference to the interface string.
103 * @param[out] interfaces - Reference to interface map.
104 */
105template <typename T>
106static void populateFruSpecificInterfaces(const T& map,
107 const string& preIntrStr,
108 inventory::InterfaceMap& interfaces)
109{
110 inventory::PropertyMap prop;
111
112 for (const auto& kwVal : map)
113 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500114 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530115
116 auto kw = kwVal.first;
117
118 if (kw[0] == '#')
119 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500120 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530121 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500122 else if (isdigit(kw[0]))
123 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500124 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500125 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530126 prop.emplace(move(kw), move(vec));
127 }
128
129 interfaces.emplace(preIntrStr, move(prop));
130}
131
132/**
133 * @brief Populate Interfaces.
134 *
135 * This method populates common and extra interfaces to dbus.
136 * @param[in] js - json object
137 * @param[out] interfaces - Reference to interface map
138 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530139 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530140 */
141template <typename T>
142static void populateInterfaces(const nlohmann::json& js,
143 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530144 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530145{
146 for (const auto& ifs : js.items())
147 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530148 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530149 inventory::PropertyMap props;
150
151 for (const auto& itr : ifs.value().items())
152 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530153 const string& busProp = itr.key();
154
Alpana Kumari31970de2020-02-17 06:49:57 -0600155 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530156 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530157 props.emplace(busProp, itr.value().get<bool>());
158 }
159 else if (itr.value().is_string())
160 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500161 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530162 {
163 if (busProp == "LocationCode" &&
164 inf == "com.ibm.ipzvpd.Location")
165 {
166 auto prop = expandLocationCode(
167 itr.value().get<string>(), vpdMap, isSystemVpd);
168 props.emplace(busProp, prop);
169 }
170 else
171 {
172 props.emplace(busProp, itr.value().get<string>());
173 }
174 }
175 else
176 {
177 props.emplace(busProp, itr.value().get<string>());
178 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530179 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600180 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530181 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600182 const string& rec = itr.value().value("recordName", "");
183 const string& kw = itr.value().value("keywordName", "");
184 const string& encoding = itr.value().value("encoding", "");
185
Alpana Kumari58e22142020-05-05 00:22:12 -0500186 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530187 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530188 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
189 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600190 {
191 auto encoded =
192 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530193 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600194 }
195 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500196 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600197 {
198 if (!kw.empty() && vpdMap.count(kw))
199 {
200 auto prop =
201 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
202 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530203 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600204 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530205 }
206 }
207 }
208 interfaces.emplace(inf, move(props));
209 }
210}
211
Alpana Kumari58e22142020-05-05 00:22:12 -0500212Binary getVpdDataInVector(nlohmann::json& js, const string& file)
213{
214 uint32_t offset = 0;
215 // check if offset present?
216 for (const auto& item : js["frus"][file])
217 {
218 if (item.find("offset") != item.end())
219 {
220 offset = item["offset"];
221 }
222 }
223
224 // TODO: Figure out a better way to get max possible VPD size.
225 Binary vpdVector;
226 vpdVector.resize(65504);
227 ifstream vpdFile;
228 vpdFile.open(file, ios::binary);
229
230 vpdFile.seekg(offset, ios_base::cur);
231 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
232 vpdVector.resize(vpdFile.gcount());
233
234 return vpdVector;
235}
236
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530237/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530238 * @brief Prime the Inventory
239 * Prime the inventory by populating only the location code,
240 * type interface and the inventory object for the frus
241 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530242 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530243 * @param[in] jsObject - Reference to vpd inventory json object
244 * @param[in] vpdMap - Reference to the parsed vpd map
245 *
246 * @returns Map of items in extraInterface.
247 */
248template <typename T>
249inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
250 const T& vpdMap)
251{
252 inventory::ObjectMap objects;
253
254 for (auto& itemFRUS : jsObject["frus"].items())
255 {
256 for (auto& itemEEPROM : itemFRUS.value())
257 {
258 inventory::InterfaceMap interfaces;
259 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
260 inventory::Object object(itemEEPROM.at("inventoryPath"));
261
262 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
263 {
264 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
265 {
266 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
267 {
268 inventory::PropertyMap props;
269 if (eI.key() ==
270 openpower::vpd::constants::LOCATION_CODE_INF)
271 {
272 if constexpr (std::is_same<T, Parsed>::value)
273 {
274 for (auto& lC : eI.value().items())
275 {
276 auto propVal = expandLocationCode(
277 lC.value().get<string>(), vpdMap, true);
278
279 props.emplace(move(lC.key()),
280 move(propVal));
281 interfaces.emplace(move(eI.key()),
282 move(props));
283 }
284 }
285 }
286 else if (eI.key().find("Inventory.Item.") !=
287 string::npos)
288 {
289 interfaces.emplace(move(eI.key()), move(props));
290 }
291 }
292 }
293 objects.emplace(move(object), move(interfaces));
294 }
295 }
296 }
297 return objects;
298}
299
300/**
301 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530302 * This method invokes all the populateInterface functions
303 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530304 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
305 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530306 * @param[in] js - Inventory json object
307 * @param[in] filePath - Path of the vpd file
308 * @param[in] preIntrStr - Interface string
309 */
310template <typename T>
311static void populateDbus(const T& vpdMap, nlohmann::json& js,
312 const string& filePath, const string& preIntrStr)
313{
314 inventory::InterfaceMap interfaces;
315 inventory::ObjectMap objects;
316 inventory::PropertyMap prop;
317
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530318 bool isSystemVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530319 for (const auto& item : js["frus"][filePath])
320 {
321 const auto& objectPath = item["inventoryPath"];
322 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530323 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530324 // Populate the VPD keywords and the common interfaces only if we
325 // are asked to inherit that data from the VPD, else only add the
326 // extraInterfaces.
327 if (item.value("inherit", true))
328 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500329 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530330 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530331 // Each record in the VPD becomes an interface and all
332 // keyword within the record are properties under that
333 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530334 for (const auto& record : vpdMap)
335 {
336 populateFruSpecificInterfaces(
337 record.second, preIntrStr + record.first, interfaces);
338 }
339 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500340 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530341 {
342 populateFruSpecificInterfaces(vpdMap, preIntrStr, interfaces);
343 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530344 if (js.find("commonInterfaces") != js.end())
345 {
346 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
347 isSystemVpd);
348 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530349 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500350 else
351 {
352 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500353 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500354 {
355 if (item.find("copyRecords") != item.end())
356 {
357 for (const auto& record : item["copyRecords"])
358 {
359 const string& recordName = record;
360 if (vpdMap.find(recordName) != vpdMap.end())
361 {
362 populateFruSpecificInterfaces(
363 vpdMap.at(recordName), preIntrStr + recordName,
364 interfaces);
365 }
366 }
367 }
368 }
369 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530370
Alpana Kumari58e22142020-05-05 00:22:12 -0500371 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530372 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500373 // Populate interfaces and properties that are common to every FRU
374 // and additional interface that might be defined on a per-FRU
375 // basis.
376 if (item.find("extraInterfaces") != item.end())
377 {
378 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
379 isSystemVpd);
380 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530381 }
382 objects.emplace(move(object), move(interfaces));
383 }
384
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530385 if (isSystemVpd)
386 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530387 vector<uint8_t> imVal;
388 if constexpr (is_same<T, Parsed>::value)
389 {
390 auto property = vpdMap.find("VSBP");
391 if (property != vpdMap.end())
392 {
393 auto value = (property->second).find("IM");
394 if (value != (property->second).end())
395 {
396 copy(value->second.begin(), value->second.end(),
397 back_inserter(imVal));
398 }
399 }
400 }
401
402 fs::path target;
403 fs::path link = INVENTORY_JSON_SYM_LINK;
404
405 ostringstream oss;
406 for (auto& i : imVal)
407 {
408 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
409 }
410 string imValStr = oss.str();
411
412 if (imValStr == SYSTEM_4U) // 4U
413 {
414 target = INVENTORY_JSON_4U;
415 }
416
417 else if (imValStr == SYSTEM_2U) // 2U
418 {
419 target = INVENTORY_JSON_2U;
420 }
421
422 // unlink the symlink which is created at build time
423 remove(INVENTORY_JSON_SYM_LINK);
424 // create a new symlink based on the system
425 fs::create_symlink(target, link);
426
427 // Reloading the json
428 ifstream inventoryJson(link);
429 auto js = json::parse(inventoryJson);
430 inventoryJson.close();
431
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530432 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
433 objects.insert(primeObject.begin(), primeObject.end());
434 }
435
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530436 // Notify PIM
437 inventory::callPIM(move(objects));
438}
439
440int main(int argc, char** argv)
441{
442 int rc = 0;
443
444 try
445 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530446 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
447 "in DBUS"};
448 string file{};
449
450 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
451 ->required()
452 ->check(ExistingFile);
453
454 CLI11_PARSE(app, argc, argv);
455
456 // Make sure that the file path we get is for a supported EEPROM
457 ifstream inventoryJson(INVENTORY_JSON);
458 auto js = json::parse(inventoryJson);
459
460 if ((js.find("frus") == js.end()) ||
461 (js["frus"].find(file) == js["frus"].end()))
462 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500463 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530464 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530465 }
466
Alpana Kumari58e22142020-05-05 00:22:12 -0500467 Binary vpdVector(getVpdDataInVector(js, file));
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530468 vpdType type = vpdTypeCheck(vpdVector);
469
470 switch (type)
471 {
472 case IPZ_VPD:
473 {
474 // Invoking IPZ Vpd Parser
475 auto vpdStore = parse(move(vpdVector));
476 const Parsed& vpdMap = vpdStore.getVpdMap();
477 string preIntrStr = "com.ibm.ipzvpd.";
478 // Write it to the inventory
479 populateDbus(vpdMap, js, file, preIntrStr);
480 }
481 break;
482
483 case KEYWORD_VPD:
484 {
485 // Creating Keyword Vpd Parser Object
486 KeywordVpdParser parserObj(move(vpdVector));
487 // Invoking KW Vpd Parser
488 const auto& kwValMap = parserObj.parseKwVpd();
Alpana Kumaria00936f2020-04-14 07:15:46 -0500489 string preIntrStr = "com.ibm.ipzvpd.VINI";
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530490 populateDbus(kwValMap, js, file, preIntrStr);
491 }
492 break;
Alpana Kumaria00936f2020-04-14 07:15:46 -0500493
494 case MEMORY_VPD:
495 {
496 // Get an object to call API & get the key-value map
497 memoryVpdParser vpdParser(move(vpdVector));
498 const auto& memKwValMap = vpdParser.parseMemVpd();
499
500 string preIntrStr = "com.ibm.kwvpd.KWVPD";
501 // js(define dimm sys path in js), ObjPath(define in JS)
502 populateDbus(memKwValMap, js, file, preIntrStr);
503 }
504 break;
505
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530506 default:
Alpana Kumari58e22142020-05-05 00:22:12 -0500507 throw runtime_error("Invalid VPD format");
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530508 }
509 }
510 catch (exception& e)
511 {
512 cerr << e.what() << "\n";
513 rc = -1;
514 }
515
516 return rc;
517}