blob: 25b77f1a4a8e684e4bc4a816fdb3bf8d93fc5517 [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"
9
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -050010#include <ctype.h>
11
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053012#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053013#include <algorithm>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053014#include <exception>
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053015#include <filesystem>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053016#include <fstream>
17#include <iostream>
18#include <iterator>
19#include <nlohmann/json.hpp>
20
21using namespace std;
22using namespace openpower::vpd;
23using namespace CLI;
24using namespace vpd::keyword::parser;
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +053025using namespace openpower::vpd::constants;
26namespace fs = filesystem;
27using json = nlohmann::json;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050028using namespace openpower::vpd::parser::factory;
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050029using namespace openpower::vpd::inventory;
Alpana Kumaria00936f2020-04-14 07:15:46 -050030using namespace openpower::vpd::memory::parser;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050031using namespace openpower::vpd::parser::interface;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053032
Santosh Puranik88edeb62020-03-02 12:00:09 +053033/**
34 * @brief Expands location codes
35 */
36static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
37 bool isSystemVpd)
38{
39 auto expanded{unexpanded};
40 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
41 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
42 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
43 size_t idx = expanded.find("fcs");
44 try
45 {
46 if (idx != string::npos)
47 {
48 string fc{};
49 string se{};
50 if (isSystemVpd)
51 {
52 const auto& fcData = vpdMap.at("VCEN").at("FC");
53 const auto& seData = vpdMap.at("VCEN").at("SE");
54 fc = string(fcData.data(), fcData.size());
55 se = string(seData.data(), seData.size());
56 }
57 else
58 {
59 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
60 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
61 }
62
63 // TODO: See if ND1 can be placed in the JSON
64 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
65 }
66 else
67 {
68 idx = expanded.find("mts");
69 if (idx != string::npos)
70 {
71 string mt{};
72 string se{};
73 if (isSystemVpd)
74 {
75 const auto& mtData = vpdMap.at("VSYS").at("TM");
76 const auto& seData = vpdMap.at("VSYS").at("SE");
77 mt = string(mtData.data(), mtData.size());
78 se = string(seData.data(), seData.size());
79 }
80 else
81 {
82 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
83 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
84 }
85
86 replace(mt.begin(), mt.end(), '-', '.');
87 expanded.replace(idx, 3, mt + "." + se);
88 }
89 }
90 }
Alpana Kumari58e22142020-05-05 00:22:12 -050091 catch (exception& e)
Santosh Puranik88edeb62020-03-02 12:00:09 +053092 {
Alpana Kumari58e22142020-05-05 00:22:12 -050093 cerr << "Failed to expand location code with exception: " << e.what()
94 << "\n";
Santosh Puranik88edeb62020-03-02 12:00:09 +053095 }
96 return expanded;
97}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053098/**
99 * @brief Populate FRU specific interfaces.
100 *
101 * This is a common method which handles both
102 * ipz and keyword specific interfaces thus,
103 * reducing the code redundancy.
104 * @param[in] map - Reference to the innermost keyword-value map.
105 * @param[in] preIntrStr - Reference to the interface string.
106 * @param[out] interfaces - Reference to interface map.
107 */
108template <typename T>
109static void populateFruSpecificInterfaces(const T& map,
110 const string& preIntrStr,
111 inventory::InterfaceMap& interfaces)
112{
113 inventory::PropertyMap prop;
114
115 for (const auto& kwVal : map)
116 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500117 vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530118
119 auto kw = kwVal.first;
120
121 if (kw[0] == '#')
122 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500123 kw = string("PD_") + kw[1];
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530124 }
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500125 else if (isdigit(kw[0]))
126 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500127 kw = string("N_") + kw;
Alpana Kumari8ea3f6d2020-04-02 00:26:07 -0500128 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530129 prop.emplace(move(kw), move(vec));
130 }
131
132 interfaces.emplace(preIntrStr, move(prop));
133}
134
135/**
136 * @brief Populate Interfaces.
137 *
138 * This method populates common and extra interfaces to dbus.
139 * @param[in] js - json object
140 * @param[out] interfaces - Reference to interface map
141 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530142 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530143 */
144template <typename T>
145static void populateInterfaces(const nlohmann::json& js,
146 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530147 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530148{
149 for (const auto& ifs : js.items())
150 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530151 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530152 inventory::PropertyMap props;
153
154 for (const auto& itr : ifs.value().items())
155 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530156 const string& busProp = itr.key();
157
Alpana Kumari31970de2020-02-17 06:49:57 -0600158 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530159 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530160 props.emplace(busProp, itr.value().get<bool>());
161 }
162 else if (itr.value().is_string())
163 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500164 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik88edeb62020-03-02 12:00:09 +0530165 {
166 if (busProp == "LocationCode" &&
167 inf == "com.ibm.ipzvpd.Location")
168 {
169 auto prop = expandLocationCode(
170 itr.value().get<string>(), vpdMap, isSystemVpd);
171 props.emplace(busProp, prop);
172 }
173 else
174 {
175 props.emplace(busProp, itr.value().get<string>());
176 }
177 }
178 else
179 {
180 props.emplace(busProp, itr.value().get<string>());
181 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530182 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600183 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530184 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600185 const string& rec = itr.value().value("recordName", "");
186 const string& kw = itr.value().value("keywordName", "");
187 const string& encoding = itr.value().value("encoding", "");
188
Alpana Kumari58e22142020-05-05 00:22:12 -0500189 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530190 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530191 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
192 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600193 {
194 auto encoded =
195 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530196 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600197 }
198 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500199 else if constexpr (is_same<T, KeywordVpdMap>::value)
Alpana Kumari31970de2020-02-17 06:49:57 -0600200 {
201 if (!kw.empty() && vpdMap.count(kw))
202 {
203 auto prop =
204 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
205 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530206 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600207 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530208 }
209 }
210 }
211 interfaces.emplace(inf, move(props));
212 }
213}
214
Alpana Kumari58e22142020-05-05 00:22:12 -0500215Binary getVpdDataInVector(nlohmann::json& js, const string& file)
216{
217 uint32_t offset = 0;
218 // check if offset present?
219 for (const auto& item : js["frus"][file])
220 {
221 if (item.find("offset") != item.end())
222 {
223 offset = item["offset"];
224 }
225 }
226
227 // TODO: Figure out a better way to get max possible VPD size.
228 Binary vpdVector;
229 vpdVector.resize(65504);
230 ifstream vpdFile;
231 vpdFile.open(file, ios::binary);
232
233 vpdFile.seekg(offset, ios_base::cur);
234 vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504);
235 vpdVector.resize(vpdFile.gcount());
236
237 return vpdVector;
238}
239
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530240/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530241 * @brief Prime the Inventory
242 * Prime the inventory by populating only the location code,
243 * type interface and the inventory object for the frus
244 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530245 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530246 * @param[in] jsObject - Reference to vpd inventory json object
247 * @param[in] vpdMap - Reference to the parsed vpd map
248 *
249 * @returns Map of items in extraInterface.
250 */
251template <typename T>
252inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
253 const T& vpdMap)
254{
255 inventory::ObjectMap objects;
256
257 for (auto& itemFRUS : jsObject["frus"].items())
258 {
259 for (auto& itemEEPROM : itemFRUS.value())
260 {
261 inventory::InterfaceMap interfaces;
262 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
263 inventory::Object object(itemEEPROM.at("inventoryPath"));
264
265 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
266 {
267 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
268 {
269 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
270 {
271 inventory::PropertyMap props;
272 if (eI.key() ==
273 openpower::vpd::constants::LOCATION_CODE_INF)
274 {
275 if constexpr (std::is_same<T, Parsed>::value)
276 {
277 for (auto& lC : eI.value().items())
278 {
279 auto propVal = expandLocationCode(
280 lC.value().get<string>(), vpdMap, true);
281
282 props.emplace(move(lC.key()),
283 move(propVal));
284 interfaces.emplace(move(eI.key()),
285 move(props));
286 }
287 }
288 }
289 else if (eI.key().find("Inventory.Item.") !=
290 string::npos)
291 {
292 interfaces.emplace(move(eI.key()), move(props));
293 }
294 }
295 }
296 objects.emplace(move(object), move(interfaces));
297 }
298 }
299 }
300 return objects;
301}
302
303/**
304 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530305 * This method invokes all the populateInterface functions
306 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530307 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
308 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530309 * @param[in] js - Inventory json object
310 * @param[in] filePath - Path of the vpd file
311 * @param[in] preIntrStr - Interface string
312 */
313template <typename T>
314static void populateDbus(const T& vpdMap, nlohmann::json& js,
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500315 const string& filePath) //, const string &preIntrStr) {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530316{
317 inventory::InterfaceMap interfaces;
318 inventory::ObjectMap objects;
319 inventory::PropertyMap prop;
320
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500321 bool isSystemVpd = false;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530322 for (const auto& item : js["frus"][filePath])
323 {
324 const auto& objectPath = item["inventoryPath"];
325 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530326 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530327 // Populate the VPD keywords and the common interfaces only if we
328 // are asked to inherit that data from the VPD, else only add the
329 // extraInterfaces.
330 if (item.value("inherit", true))
331 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500332 if constexpr (is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530333 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530334 // Each record in the VPD becomes an interface and all
335 // keyword within the record are properties under that
336 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530337 for (const auto& record : vpdMap)
338 {
339 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500340 record.second, ipzVpdInf + record.first, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530341 }
342 }
Alpana Kumari58e22142020-05-05 00:22:12 -0500343 else if constexpr (is_same<T, KeywordVpdMap>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530344 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500345 populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530346 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530347 if (js.find("commonInterfaces") != js.end())
348 {
349 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
350 isSystemVpd);
351 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530352 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500353 else
354 {
355 // Check if we have been asked to inherit specific record(s)
Alpana Kumari58e22142020-05-05 00:22:12 -0500356 if constexpr (is_same<T, Parsed>::value)
Santosh Puranik0859eb62020-03-16 02:56:29 -0500357 {
358 if (item.find("copyRecords") != item.end())
359 {
360 for (const auto& record : item["copyRecords"])
361 {
362 const string& recordName = record;
363 if (vpdMap.find(recordName) != vpdMap.end())
364 {
365 populateFruSpecificInterfaces(
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500366 vpdMap.at(recordName), ipzVpdInf + recordName,
Santosh Puranik0859eb62020-03-16 02:56:29 -0500367 interfaces);
368 }
369 }
370 }
371 }
372 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530373
Alpana Kumari58e22142020-05-05 00:22:12 -0500374 if (item.value("inheritEI", true))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530375 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500376 // Populate interfaces and properties that are common to every FRU
377 // and additional interface that might be defined on a per-FRU
378 // basis.
379 if (item.find("extraInterfaces") != item.end())
380 {
381 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
382 isSystemVpd);
383 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530384 }
385 objects.emplace(move(object), move(interfaces));
386 }
387
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530388 if (isSystemVpd)
389 {
PriyangaRamasamy83a1d5d2020-04-30 19:15:43 +0530390 vector<uint8_t> imVal;
391 if constexpr (is_same<T, Parsed>::value)
392 {
393 auto property = vpdMap.find("VSBP");
394 if (property != vpdMap.end())
395 {
396 auto value = (property->second).find("IM");
397 if (value != (property->second).end())
398 {
399 copy(value->second.begin(), value->second.end(),
400 back_inserter(imVal));
401 }
402 }
403 }
404
405 fs::path target;
406 fs::path link = INVENTORY_JSON_SYM_LINK;
407
408 ostringstream oss;
409 for (auto& i : imVal)
410 {
411 oss << setw(2) << setfill('0') << hex << static_cast<int>(i);
412 }
413 string imValStr = oss.str();
414
415 if (imValStr == SYSTEM_4U) // 4U
416 {
417 target = INVENTORY_JSON_4U;
418 }
419
420 else if (imValStr == SYSTEM_2U) // 2U
421 {
422 target = INVENTORY_JSON_2U;
423 }
424
425 // unlink the symlink which is created at build time
426 remove(INVENTORY_JSON_SYM_LINK);
427 // create a new symlink based on the system
428 fs::create_symlink(target, link);
429
430 // Reloading the json
431 ifstream inventoryJson(link);
432 auto js = json::parse(inventoryJson);
433 inventoryJson.close();
434
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530435 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
436 objects.insert(primeObject.begin(), primeObject.end());
437 }
438
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530439 // Notify PIM
440 inventory::callPIM(move(objects));
441}
442
443int main(int argc, char** argv)
444{
445 int rc = 0;
446
447 try
448 {
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530449 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
450 "in DBUS"};
451 string file{};
452
453 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
454 ->required()
455 ->check(ExistingFile);
456
457 CLI11_PARSE(app, argc, argv);
458
459 // Make sure that the file path we get is for a supported EEPROM
460 ifstream inventoryJson(INVENTORY_JSON);
461 auto js = json::parse(inventoryJson);
462
463 if ((js.find("frus") == js.end()) ||
464 (js["frus"].find(file) == js["frus"].end()))
465 {
Alpana Kumari58e22142020-05-05 00:22:12 -0500466 cout << "Device path not in JSON, ignoring" << endl;
Santosh Puranik88edeb62020-03-02 12:00:09 +0530467 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530468 }
469
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500470 Binary vpdVector = getVpdDataInVector(js, file);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530471
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500472 ParserInterface* parser =
473 ParserFactory::getParser(std::move(vpdVector));
474
475 variant<KeywordVpdMap, Store> parseResult;
476 parseResult = parser->parse();
477
478 if (auto pVal = get_if<Store>(&parseResult))
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530479 {
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500480 populateDbus(pVal->getVpdMap(), js, file);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530481 }
SunnySrivastava1984e12b1812020-05-26 02:23:11 -0500482 else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
483 {
484 populateDbus(*pVal, js, file);
485 }
486
487 // release the parser object
488 ParserFactory::freeParser(parser);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530489 }
490 catch (exception& e)
491 {
492 cerr << e.what() << "\n";
493 rc = -1;
494 }
495
496 return rc;
497}