PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "defines.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 4 | #include "ipz_parser.hpp" |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 5 | #include "keyword_vpd_parser.hpp" |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 6 | #include "memory_vpd_parser.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 7 | #include "parser_factory.hpp" |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 8 | #include "utils.hpp" |
| 9 | |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 10 | #include <ctype.h> |
| 11 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 12 | #include <CLI/CLI.hpp> |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 13 | #include <algorithm> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 14 | #include <exception> |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 15 | #include <filesystem> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 16 | #include <fstream> |
| 17 | #include <iostream> |
| 18 | #include <iterator> |
| 19 | #include <nlohmann/json.hpp> |
| 20 | |
| 21 | using namespace std; |
| 22 | using namespace openpower::vpd; |
| 23 | using namespace CLI; |
| 24 | using namespace vpd::keyword::parser; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 25 | using namespace openpower::vpd::constants; |
| 26 | namespace fs = filesystem; |
| 27 | using json = nlohmann::json; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 28 | using namespace openpower::vpd::parser::factory; |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 29 | using namespace openpower::vpd::inventory; |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 30 | using namespace openpower::vpd::memory::parser; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 31 | using namespace openpower::vpd::parser::interface; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 32 | |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 33 | /** |
| 34 | * @brief Expands location codes |
| 35 | */ |
| 36 | static 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 Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 91 | catch (exception& e) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 92 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 93 | cerr << "Failed to expand location code with exception: " << e.what() |
| 94 | << "\n"; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 95 | } |
| 96 | return expanded; |
| 97 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 98 | /** |
| 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 | */ |
| 108 | template <typename T> |
| 109 | static 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 Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 117 | vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end()); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 118 | |
| 119 | auto kw = kwVal.first; |
| 120 | |
| 121 | if (kw[0] == '#') |
| 122 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 123 | kw = string("PD_") + kw[1]; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 124 | } |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 125 | else if (isdigit(kw[0])) |
| 126 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 127 | kw = string("N_") + kw; |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 128 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 129 | 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 Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 142 | * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 143 | */ |
| 144 | template <typename T> |
| 145 | static void populateInterfaces(const nlohmann::json& js, |
| 146 | inventory::InterfaceMap& interfaces, |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 147 | const T& vpdMap, bool isSystemVpd) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 148 | { |
| 149 | for (const auto& ifs : js.items()) |
| 150 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 151 | string inf = ifs.key(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 152 | inventory::PropertyMap props; |
| 153 | |
| 154 | for (const auto& itr : ifs.value().items()) |
| 155 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 156 | const string& busProp = itr.key(); |
| 157 | |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 158 | if (itr.value().is_boolean()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 159 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 160 | props.emplace(busProp, itr.value().get<bool>()); |
| 161 | } |
| 162 | else if (itr.value().is_string()) |
| 163 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 164 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 165 | { |
| 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 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 182 | } |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 183 | else if (itr.value().is_object()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 184 | { |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 185 | 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 Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 189 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 190 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 191 | if (!rec.empty() && !kw.empty() && vpdMap.count(rec) && |
| 192 | vpdMap.at(rec).count(kw)) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 193 | { |
| 194 | auto encoded = |
| 195 | encodeKeyword(vpdMap.at(rec).at(kw), encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 196 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 197 | } |
| 198 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 199 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 200 | { |
| 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 Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 206 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 207 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | interfaces.emplace(inf, move(props)); |
| 212 | } |
| 213 | } |
| 214 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 215 | Binary 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 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 240 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 241 | * @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. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 245 | * |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 246 | * @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 | */ |
| 251 | template <typename T> |
| 252 | inventory::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. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 305 | * This method invokes all the populateInterface functions |
| 306 | * and notifies PIM about dbus object. |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 307 | * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the |
| 308 | * input. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 309 | * @param[in] js - Inventory json object |
| 310 | * @param[in] filePath - Path of the vpd file |
| 311 | * @param[in] preIntrStr - Interface string |
| 312 | */ |
| 313 | template <typename T> |
| 314 | static void populateDbus(const T& vpdMap, nlohmann::json& js, |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 315 | const string& filePath) //, const string &preIntrStr) { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 316 | { |
| 317 | inventory::InterfaceMap interfaces; |
| 318 | inventory::ObjectMap objects; |
| 319 | inventory::PropertyMap prop; |
| 320 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 321 | bool isSystemVpd = false; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 322 | for (const auto& item : js["frus"][filePath]) |
| 323 | { |
| 324 | const auto& objectPath = item["inventoryPath"]; |
| 325 | sdbusplus::message::object_path object(objectPath); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 326 | isSystemVpd = item.value("isSystemVpd", false); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 327 | // 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 Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 332 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 333 | { |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 334 | // Each record in the VPD becomes an interface and all |
| 335 | // keyword within the record are properties under that |
| 336 | // interface. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 337 | for (const auto& record : vpdMap) |
| 338 | { |
| 339 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 340 | record.second, ipzVpdInf + record.first, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 341 | } |
| 342 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 343 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 344 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 345 | populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 346 | } |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 347 | if (js.find("commonInterfaces") != js.end()) |
| 348 | { |
| 349 | populateInterfaces(js["commonInterfaces"], interfaces, vpdMap, |
| 350 | isSystemVpd); |
| 351 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 352 | } |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 353 | else |
| 354 | { |
| 355 | // Check if we have been asked to inherit specific record(s) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 356 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 357 | { |
| 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( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 366 | vpdMap.at(recordName), ipzVpdInf + recordName, |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 367 | interfaces); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 373 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 374 | if (item.value("inheritEI", true)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 375 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 376 | // 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 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 384 | } |
| 385 | objects.emplace(move(object), move(interfaces)); |
| 386 | } |
| 387 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 388 | if (isSystemVpd) |
| 389 | { |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 390 | 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 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 435 | inventory::ObjectMap primeObject = primeInventory(js, vpdMap); |
| 436 | objects.insert(primeObject.begin(), primeObject.end()); |
| 437 | } |
| 438 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 439 | // Notify PIM |
| 440 | inventory::callPIM(move(objects)); |
| 441 | } |
| 442 | |
| 443 | int main(int argc, char** argv) |
| 444 | { |
| 445 | int rc = 0; |
| 446 | |
| 447 | try |
| 448 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 449 | 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 Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 466 | cout << "Device path not in JSON, ignoring" << endl; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 467 | return 0; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 468 | } |
| 469 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 470 | Binary vpdVector = getVpdDataInVector(js, file); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 471 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 472 | 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)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 479 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 480 | populateDbus(pVal->getVpdMap(), js, file); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 481 | } |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame^] | 482 | 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); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 489 | } |
| 490 | catch (exception& e) |
| 491 | { |
| 492 | cerr << e.what() << "\n"; |
| 493 | rc = -1; |
| 494 | } |
| 495 | |
| 496 | return rc; |
| 497 | } |