blob: 4c3aba439a5fbc3377b244549275a1b7a094e42c [file] [log] [blame]
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +05301#include "config.h"
2
3#include "defines.hpp"
4#include "ibm_vpd_type_check.hpp"
5#include "keyword_vpd_parser.hpp"
6#include "parser.hpp"
7#include "utils.hpp"
8
9#include <CLI/CLI.hpp>
Santosh Puranik88edeb62020-03-02 12:00:09 +053010#include <algorithm>
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053011#include <exception>
12#include <fstream>
13#include <iostream>
14#include <iterator>
15#include <nlohmann/json.hpp>
16
17using namespace std;
18using namespace openpower::vpd;
19using namespace CLI;
20using namespace vpd::keyword::parser;
21using namespace vpdFormat;
22
Santosh Puranik88edeb62020-03-02 12:00:09 +053023/**
24 * @brief Expands location codes
25 */
26static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap,
27 bool isSystemVpd)
28{
29 auto expanded{unexpanded};
30 static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard";
31 static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN";
32 static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS";
33 size_t idx = expanded.find("fcs");
34 try
35 {
36 if (idx != string::npos)
37 {
38 string fc{};
39 string se{};
40 if (isSystemVpd)
41 {
42 const auto& fcData = vpdMap.at("VCEN").at("FC");
43 const auto& seData = vpdMap.at("VCEN").at("SE");
44 fc = string(fcData.data(), fcData.size());
45 se = string(seData.data(), seData.size());
46 }
47 else
48 {
49 fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC");
50 se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE");
51 }
52
53 // TODO: See if ND1 can be placed in the JSON
54 expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se);
55 }
56 else
57 {
58 idx = expanded.find("mts");
59 if (idx != string::npos)
60 {
61 string mt{};
62 string se{};
63 if (isSystemVpd)
64 {
65 const auto& mtData = vpdMap.at("VSYS").at("TM");
66 const auto& seData = vpdMap.at("VSYS").at("SE");
67 mt = string(mtData.data(), mtData.size());
68 se = string(seData.data(), seData.size());
69 }
70 else
71 {
72 mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM");
73 se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE");
74 }
75
76 replace(mt.begin(), mt.end(), '-', '.');
77 expanded.replace(idx, 3, mt + "." + se);
78 }
79 }
80 }
81 catch (std::exception& e)
82 {
83 std::cerr << "Failed to expand location code with exception: "
84 << e.what() << "\n";
85 }
86 return expanded;
87}
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +053088/**
89 * @brief Populate FRU specific interfaces.
90 *
91 * This is a common method which handles both
92 * ipz and keyword specific interfaces thus,
93 * reducing the code redundancy.
94 * @param[in] map - Reference to the innermost keyword-value map.
95 * @param[in] preIntrStr - Reference to the interface string.
96 * @param[out] interfaces - Reference to interface map.
97 */
98template <typename T>
99static void populateFruSpecificInterfaces(const T& map,
100 const string& preIntrStr,
101 inventory::InterfaceMap& interfaces)
102{
103 inventory::PropertyMap prop;
104
105 for (const auto& kwVal : map)
106 {
107 std::vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end());
108
109 auto kw = kwVal.first;
110
111 if (kw[0] == '#')
112 {
113 kw = std::string("PD_") + kw[1];
114 }
115 prop.emplace(move(kw), move(vec));
116 }
117
118 interfaces.emplace(preIntrStr, move(prop));
119}
120
121/**
122 * @brief Populate Interfaces.
123 *
124 * This method populates common and extra interfaces to dbus.
125 * @param[in] js - json object
126 * @param[out] interfaces - Reference to interface map
127 * @param[in] vpdMap - Reference to the parsed vpd map.
Santosh Puranik88edeb62020-03-02 12:00:09 +0530128 * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530129 */
130template <typename T>
131static void populateInterfaces(const nlohmann::json& js,
132 inventory::InterfaceMap& interfaces,
Santosh Puranik88edeb62020-03-02 12:00:09 +0530133 const T& vpdMap, bool isSystemVpd)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530134{
135 for (const auto& ifs : js.items())
136 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530137 string inf = ifs.key();
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530138 inventory::PropertyMap props;
139
140 for (const auto& itr : ifs.value().items())
141 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530142 const string& busProp = itr.key();
143
Alpana Kumari31970de2020-02-17 06:49:57 -0600144 if (itr.value().is_boolean())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530145 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530146 props.emplace(busProp, itr.value().get<bool>());
147 }
148 else if (itr.value().is_string())
149 {
150 if constexpr (std::is_same<T, Parsed>::value)
151 {
152 if (busProp == "LocationCode" &&
153 inf == "com.ibm.ipzvpd.Location")
154 {
155 auto prop = expandLocationCode(
156 itr.value().get<string>(), vpdMap, isSystemVpd);
157 props.emplace(busProp, prop);
158 }
159 else
160 {
161 props.emplace(busProp, itr.value().get<string>());
162 }
163 }
164 else
165 {
166 props.emplace(busProp, itr.value().get<string>());
167 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530168 }
Alpana Kumari31970de2020-02-17 06:49:57 -0600169 else if (itr.value().is_object())
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530170 {
Alpana Kumari31970de2020-02-17 06:49:57 -0600171 const string& rec = itr.value().value("recordName", "");
172 const string& kw = itr.value().value("keywordName", "");
173 const string& encoding = itr.value().value("encoding", "");
174
175 if constexpr (std::is_same<T, Parsed>::value)
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530176 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530177 if (!rec.empty() && !kw.empty() && vpdMap.count(rec) &&
178 vpdMap.at(rec).count(kw))
Alpana Kumari31970de2020-02-17 06:49:57 -0600179 {
180 auto encoded =
181 encodeKeyword(vpdMap.at(rec).at(kw), encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530182 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600183 }
184 }
185 else if constexpr (std::is_same<T, KeywordVpdMap>::value)
186 {
187 if (!kw.empty() && vpdMap.count(kw))
188 {
189 auto prop =
190 string(vpdMap.at(kw).begin(), vpdMap.at(kw).end());
191 auto encoded = encodeKeyword(prop, encoding);
Santosh Puranik88edeb62020-03-02 12:00:09 +0530192 props.emplace(busProp, encoded);
Alpana Kumari31970de2020-02-17 06:49:57 -0600193 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530194 }
195 }
196 }
197 interfaces.emplace(inf, move(props));
198 }
199}
200
201/**
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530202 * @brief Prime the Inventory
203 * Prime the inventory by populating only the location code,
204 * type interface and the inventory object for the frus
205 * which are not system vpd fru.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530206 *
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530207 * @param[in] jsObject - Reference to vpd inventory json object
208 * @param[in] vpdMap - Reference to the parsed vpd map
209 *
210 * @returns Map of items in extraInterface.
211 */
212template <typename T>
213inventory::ObjectMap primeInventory(const nlohmann::json& jsObject,
214 const T& vpdMap)
215{
216 inventory::ObjectMap objects;
217
218 for (auto& itemFRUS : jsObject["frus"].items())
219 {
220 for (auto& itemEEPROM : itemFRUS.value())
221 {
222 inventory::InterfaceMap interfaces;
223 auto isSystemVpd = itemEEPROM.value("isSystemVpd", false);
224 inventory::Object object(itemEEPROM.at("inventoryPath"));
225
226 if (!isSystemVpd && !itemEEPROM.value("noprime", false))
227 {
228 if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end())
229 {
230 for (const auto& eI : itemEEPROM["extraInterfaces"].items())
231 {
232 inventory::PropertyMap props;
233 if (eI.key() ==
234 openpower::vpd::constants::LOCATION_CODE_INF)
235 {
236 if constexpr (std::is_same<T, Parsed>::value)
237 {
238 for (auto& lC : eI.value().items())
239 {
240 auto propVal = expandLocationCode(
241 lC.value().get<string>(), vpdMap, true);
242
243 props.emplace(move(lC.key()),
244 move(propVal));
245 interfaces.emplace(move(eI.key()),
246 move(props));
247 }
248 }
249 }
250 else if (eI.key().find("Inventory.Item.") !=
251 string::npos)
252 {
253 interfaces.emplace(move(eI.key()), move(props));
254 }
255 }
256 }
257 objects.emplace(move(object), move(interfaces));
258 }
259 }
260 }
261 return objects;
262}
263
264/**
265 * @brief Populate Dbus.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530266 * This method invokes all the populateInterface functions
267 * and notifies PIM about dbus object.
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530268 * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the
269 * input.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530270 * @param[in] js - Inventory json object
271 * @param[in] filePath - Path of the vpd file
272 * @param[in] preIntrStr - Interface string
273 */
274template <typename T>
275static void populateDbus(const T& vpdMap, nlohmann::json& js,
276 const string& filePath, const string& preIntrStr)
277{
278 inventory::InterfaceMap interfaces;
279 inventory::ObjectMap objects;
280 inventory::PropertyMap prop;
281
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530282 bool isSystemVpd;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530283 for (const auto& item : js["frus"][filePath])
284 {
285 const auto& objectPath = item["inventoryPath"];
286 sdbusplus::message::object_path object(objectPath);
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530287 isSystemVpd = item.value("isSystemVpd", false);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530288 // Populate the VPD keywords and the common interfaces only if we
289 // are asked to inherit that data from the VPD, else only add the
290 // extraInterfaces.
291 if (item.value("inherit", true))
292 {
293 if constexpr (std::is_same<T, Parsed>::value)
294 {
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530295 // Each record in the VPD becomes an interface and all
296 // keyword within the record are properties under that
297 // interface.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530298 for (const auto& record : vpdMap)
299 {
300 populateFruSpecificInterfaces(
301 record.second, preIntrStr + record.first, interfaces);
302 }
303 }
304 else if constexpr (std::is_same<T, KeywordVpdMap>::value)
305 {
306 populateFruSpecificInterfaces(vpdMap, preIntrStr, interfaces);
307 }
Santosh Puranik88edeb62020-03-02 12:00:09 +0530308 if (js.find("commonInterfaces") != js.end())
309 {
310 populateInterfaces(js["commonInterfaces"], interfaces, vpdMap,
311 isSystemVpd);
312 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530313 }
Santosh Puranik0859eb62020-03-16 02:56:29 -0500314 else
315 {
316 // Check if we have been asked to inherit specific record(s)
317 if constexpr (std::is_same<T, Parsed>::value)
318 {
319 if (item.find("copyRecords") != item.end())
320 {
321 for (const auto& record : item["copyRecords"])
322 {
323 const string& recordName = record;
324 if (vpdMap.find(recordName) != vpdMap.end())
325 {
326 populateFruSpecificInterfaces(
327 vpdMap.at(recordName), preIntrStr + recordName,
328 interfaces);
329 }
330 }
331 }
332 }
333 }
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530334
335 // Populate interfaces and properties that are common to every FRU
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530336 // and additional interface that might be defined on a per-FRU
337 // basis.
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530338 if (item.find("extraInterfaces") != item.end())
339 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530340 populateInterfaces(item["extraInterfaces"], interfaces, vpdMap,
341 isSystemVpd);
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530342 }
343 objects.emplace(move(object), move(interfaces));
344 }
345
PriyangaRamasamy8e140a12020-04-13 19:24:03 +0530346 if (isSystemVpd)
347 {
348 inventory::ObjectMap primeObject = primeInventory(js, vpdMap);
349 objects.insert(primeObject.begin(), primeObject.end());
350 }
351
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530352 // Notify PIM
353 inventory::callPIM(move(objects));
354}
355
356int main(int argc, char** argv)
357{
358 int rc = 0;
359
360 try
361 {
362 using json = nlohmann::json;
363
364 App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store "
365 "in DBUS"};
366 string file{};
367
368 app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)")
369 ->required()
370 ->check(ExistingFile);
371
372 CLI11_PARSE(app, argc, argv);
373
374 // Make sure that the file path we get is for a supported EEPROM
375 ifstream inventoryJson(INVENTORY_JSON);
376 auto js = json::parse(inventoryJson);
377
378 if ((js.find("frus") == js.end()) ||
379 (js["frus"].find(file) == js["frus"].end()))
380 {
Santosh Puranik88edeb62020-03-02 12:00:09 +0530381 cout << "Device path not in JSON, ignoring" << std::endl;
382 return 0;
PriyangaRamasamyabb87ed2019-11-19 17:25:35 +0530383 }
384
385 // Open the file in binary mode
386 ifstream vpdFile(file, ios::binary);
387 // Read the content of the binary file into a vector
388 Binary vpdVector((istreambuf_iterator<char>(vpdFile)),
389 istreambuf_iterator<char>());
390
391 vpdType type = vpdTypeCheck(vpdVector);
392
393 switch (type)
394 {
395 case IPZ_VPD:
396 {
397 // Invoking IPZ Vpd Parser
398 auto vpdStore = parse(move(vpdVector));
399 const Parsed& vpdMap = vpdStore.getVpdMap();
400 string preIntrStr = "com.ibm.ipzvpd.";
401 // Write it to the inventory
402 populateDbus(vpdMap, js, file, preIntrStr);
403 }
404 break;
405
406 case KEYWORD_VPD:
407 {
408 // Creating Keyword Vpd Parser Object
409 KeywordVpdParser parserObj(move(vpdVector));
410 // Invoking KW Vpd Parser
411 const auto& kwValMap = parserObj.parseKwVpd();
412 string preIntrStr = "com.ibm.kwvpd.KWVPD";
413 populateDbus(kwValMap, js, file, preIntrStr);
414 }
415 break;
416 default:
417 throw std::runtime_error("Invalid VPD format");
418 }
419 }
420 catch (exception& e)
421 {
422 cerr << e.what() << "\n";
423 rc = -1;
424 }
425
426 return rc;
427}