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> |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 14 | #include <cstdarg> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 15 | #include <exception> |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 16 | #include <filesystem> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <iostream> |
| 19 | #include <iterator> |
| 20 | #include <nlohmann/json.hpp> |
| 21 | |
| 22 | using namespace std; |
| 23 | using namespace openpower::vpd; |
| 24 | using namespace CLI; |
| 25 | using namespace vpd::keyword::parser; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 26 | using namespace openpower::vpd::constants; |
| 27 | namespace fs = filesystem; |
| 28 | using json = nlohmann::json; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 29 | using namespace openpower::vpd::parser::factory; |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 30 | using namespace openpower::vpd::inventory; |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 31 | using namespace openpower::vpd::memory::parser; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 32 | using namespace openpower::vpd::parser::interface; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 33 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 34 | static const deviceTreeMap deviceTreeSystemTypeMap = { |
| 35 | {RAINIER_2U, "conf@aspeed-bmc-ibm-rainier-2u.dtb"}, |
| 36 | {RAINIER_4U, "conf@aspeed-bmc-ibm-rainier-4u.dtb"}}; |
| 37 | |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 38 | /** |
| 39 | * @brief Expands location codes |
| 40 | */ |
| 41 | static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap, |
| 42 | bool isSystemVpd) |
| 43 | { |
| 44 | auto expanded{unexpanded}; |
| 45 | static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard"; |
| 46 | static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN"; |
| 47 | static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS"; |
| 48 | size_t idx = expanded.find("fcs"); |
| 49 | try |
| 50 | { |
| 51 | if (idx != string::npos) |
| 52 | { |
| 53 | string fc{}; |
| 54 | string se{}; |
| 55 | if (isSystemVpd) |
| 56 | { |
| 57 | const auto& fcData = vpdMap.at("VCEN").at("FC"); |
| 58 | const auto& seData = vpdMap.at("VCEN").at("SE"); |
| 59 | fc = string(fcData.data(), fcData.size()); |
| 60 | se = string(seData.data(), seData.size()); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC"); |
| 65 | se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE"); |
| 66 | } |
| 67 | |
| 68 | // TODO: See if ND1 can be placed in the JSON |
| 69 | expanded.replace(idx, 3, fc.substr(0, 4) + ".ND1." + se); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | idx = expanded.find("mts"); |
| 74 | if (idx != string::npos) |
| 75 | { |
| 76 | string mt{}; |
| 77 | string se{}; |
| 78 | if (isSystemVpd) |
| 79 | { |
| 80 | const auto& mtData = vpdMap.at("VSYS").at("TM"); |
| 81 | const auto& seData = vpdMap.at("VSYS").at("SE"); |
| 82 | mt = string(mtData.data(), mtData.size()); |
| 83 | se = string(seData.data(), seData.size()); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM"); |
| 88 | se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE"); |
| 89 | } |
| 90 | |
| 91 | replace(mt.begin(), mt.end(), '-', '.'); |
| 92 | expanded.replace(idx, 3, mt + "." + se); |
| 93 | } |
| 94 | } |
| 95 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 96 | catch (exception& e) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 97 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 98 | cerr << "Failed to expand location code with exception: " << e.what() |
| 99 | << "\n"; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 100 | } |
| 101 | return expanded; |
| 102 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 103 | /** |
| 104 | * @brief Populate FRU specific interfaces. |
| 105 | * |
| 106 | * This is a common method which handles both |
| 107 | * ipz and keyword specific interfaces thus, |
| 108 | * reducing the code redundancy. |
| 109 | * @param[in] map - Reference to the innermost keyword-value map. |
| 110 | * @param[in] preIntrStr - Reference to the interface string. |
| 111 | * @param[out] interfaces - Reference to interface map. |
| 112 | */ |
| 113 | template <typename T> |
| 114 | static void populateFruSpecificInterfaces(const T& map, |
| 115 | const string& preIntrStr, |
| 116 | inventory::InterfaceMap& interfaces) |
| 117 | { |
| 118 | inventory::PropertyMap prop; |
| 119 | |
| 120 | for (const auto& kwVal : map) |
| 121 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 122 | vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end()); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 123 | |
| 124 | auto kw = kwVal.first; |
| 125 | |
| 126 | if (kw[0] == '#') |
| 127 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 128 | kw = string("PD_") + kw[1]; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 129 | } |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 130 | else if (isdigit(kw[0])) |
| 131 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 132 | kw = string("N_") + kw; |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 133 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 134 | prop.emplace(move(kw), move(vec)); |
| 135 | } |
| 136 | |
| 137 | interfaces.emplace(preIntrStr, move(prop)); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @brief Populate Interfaces. |
| 142 | * |
| 143 | * This method populates common and extra interfaces to dbus. |
| 144 | * @param[in] js - json object |
| 145 | * @param[out] interfaces - Reference to interface map |
| 146 | * @param[in] vpdMap - Reference to the parsed vpd map. |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 147 | * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 148 | */ |
| 149 | template <typename T> |
| 150 | static void populateInterfaces(const nlohmann::json& js, |
| 151 | inventory::InterfaceMap& interfaces, |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 152 | const T& vpdMap, bool isSystemVpd) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 153 | { |
| 154 | for (const auto& ifs : js.items()) |
| 155 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 156 | string inf = ifs.key(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 157 | inventory::PropertyMap props; |
| 158 | |
| 159 | for (const auto& itr : ifs.value().items()) |
| 160 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 161 | const string& busProp = itr.key(); |
| 162 | |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 163 | if (itr.value().is_boolean()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 164 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 165 | props.emplace(busProp, itr.value().get<bool>()); |
| 166 | } |
| 167 | else if (itr.value().is_string()) |
| 168 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 169 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 170 | { |
| 171 | if (busProp == "LocationCode" && |
| 172 | inf == "com.ibm.ipzvpd.Location") |
| 173 | { |
| 174 | auto prop = expandLocationCode( |
| 175 | itr.value().get<string>(), vpdMap, isSystemVpd); |
| 176 | props.emplace(busProp, prop); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | props.emplace(busProp, itr.value().get<string>()); |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | props.emplace(busProp, itr.value().get<string>()); |
| 186 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 187 | } |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 188 | else if (itr.value().is_object()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 189 | { |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 190 | const string& rec = itr.value().value("recordName", ""); |
| 191 | const string& kw = itr.value().value("keywordName", ""); |
| 192 | const string& encoding = itr.value().value("encoding", ""); |
| 193 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 194 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 195 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 196 | if (!rec.empty() && !kw.empty() && vpdMap.count(rec) && |
| 197 | vpdMap.at(rec).count(kw)) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 198 | { |
| 199 | auto encoded = |
| 200 | encodeKeyword(vpdMap.at(rec).at(kw), encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 201 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 202 | } |
| 203 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 204 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 205 | { |
| 206 | if (!kw.empty() && vpdMap.count(kw)) |
| 207 | { |
| 208 | auto prop = |
| 209 | string(vpdMap.at(kw).begin(), vpdMap.at(kw).end()); |
| 210 | auto encoded = encodeKeyword(prop, encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 211 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 212 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | interfaces.emplace(inf, move(props)); |
| 217 | } |
| 218 | } |
| 219 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 220 | Binary getVpdDataInVector(nlohmann::json& js, const string& file) |
| 221 | { |
| 222 | uint32_t offset = 0; |
| 223 | // check if offset present? |
| 224 | for (const auto& item : js["frus"][file]) |
| 225 | { |
| 226 | if (item.find("offset") != item.end()) |
| 227 | { |
| 228 | offset = item["offset"]; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // TODO: Figure out a better way to get max possible VPD size. |
| 233 | Binary vpdVector; |
| 234 | vpdVector.resize(65504); |
| 235 | ifstream vpdFile; |
| 236 | vpdFile.open(file, ios::binary); |
| 237 | |
| 238 | vpdFile.seekg(offset, ios_base::cur); |
| 239 | vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504); |
| 240 | vpdVector.resize(vpdFile.gcount()); |
| 241 | |
| 242 | return vpdVector; |
| 243 | } |
| 244 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 245 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 246 | * @brief Prime the Inventory |
| 247 | * Prime the inventory by populating only the location code, |
| 248 | * type interface and the inventory object for the frus |
| 249 | * which are not system vpd fru. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 250 | * |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 251 | * @param[in] jsObject - Reference to vpd inventory json object |
| 252 | * @param[in] vpdMap - Reference to the parsed vpd map |
| 253 | * |
| 254 | * @returns Map of items in extraInterface. |
| 255 | */ |
| 256 | template <typename T> |
| 257 | inventory::ObjectMap primeInventory(const nlohmann::json& jsObject, |
| 258 | const T& vpdMap) |
| 259 | { |
| 260 | inventory::ObjectMap objects; |
| 261 | |
| 262 | for (auto& itemFRUS : jsObject["frus"].items()) |
| 263 | { |
| 264 | for (auto& itemEEPROM : itemFRUS.value()) |
| 265 | { |
| 266 | inventory::InterfaceMap interfaces; |
| 267 | auto isSystemVpd = itemEEPROM.value("isSystemVpd", false); |
| 268 | inventory::Object object(itemEEPROM.at("inventoryPath")); |
| 269 | |
| 270 | if (!isSystemVpd && !itemEEPROM.value("noprime", false)) |
| 271 | { |
| 272 | if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end()) |
| 273 | { |
| 274 | for (const auto& eI : itemEEPROM["extraInterfaces"].items()) |
| 275 | { |
| 276 | inventory::PropertyMap props; |
| 277 | if (eI.key() == |
| 278 | openpower::vpd::constants::LOCATION_CODE_INF) |
| 279 | { |
| 280 | if constexpr (std::is_same<T, Parsed>::value) |
| 281 | { |
| 282 | for (auto& lC : eI.value().items()) |
| 283 | { |
| 284 | auto propVal = expandLocationCode( |
| 285 | lC.value().get<string>(), vpdMap, true); |
| 286 | |
| 287 | props.emplace(move(lC.key()), |
| 288 | move(propVal)); |
| 289 | interfaces.emplace(move(eI.key()), |
| 290 | move(props)); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | else if (eI.key().find("Inventory.Item.") != |
| 295 | string::npos) |
| 296 | { |
| 297 | interfaces.emplace(move(eI.key()), move(props)); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | objects.emplace(move(object), move(interfaces)); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | return objects; |
| 306 | } |
| 307 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 308 | /* It does nothing. Just an empty function to return null |
| 309 | * at the end of variadic template args |
| 310 | */ |
| 311 | string getCommand() |
| 312 | { |
| 313 | return ""; |
| 314 | } |
| 315 | |
| 316 | /* This function to arrange all arguments to make command |
| 317 | */ |
| 318 | template <typename T, typename... Types> |
| 319 | string getCommand(T arg1, Types... args) |
| 320 | { |
| 321 | string cmd = " " + arg1 + getCommand(args...); |
| 322 | |
| 323 | return cmd; |
| 324 | } |
| 325 | |
| 326 | /* This API takes arguments and run that command |
| 327 | * returns output of that command |
| 328 | */ |
| 329 | template <typename T, typename... Types> |
| 330 | static vector<string> executeCmd(T& path, Types... args) |
| 331 | { |
| 332 | vector<string> stdOutput; |
| 333 | array<char, 128> buffer; |
| 334 | |
| 335 | string cmd = path + getCommand(args...); |
| 336 | |
| 337 | unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); |
| 338 | if (!pipe) |
| 339 | { |
| 340 | throw runtime_error("popen() failed!"); |
| 341 | } |
| 342 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 343 | { |
| 344 | stdOutput.emplace_back(buffer.data()); |
| 345 | } |
| 346 | |
| 347 | return stdOutput; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * @brief This API executes command to set environment variable |
| 352 | * And then reboot the system |
| 353 | * @param[in] key -env key to set new value |
| 354 | * @param[in] value -value to set. |
| 355 | */ |
| 356 | void setEnvAndReboot(const string& key, const string& value) |
| 357 | { |
| 358 | // set env and reboot and break. |
| 359 | executeCmd("/sbin/fw_setenv", key, value); |
| 360 | // make dbus call to reboot |
| 361 | auto bus = sdbusplus::bus::new_default_system(); |
| 362 | auto method = bus.new_method_call( |
| 363 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 364 | "org.freedesktop.systemd1.Manager", "Reboot"); |
| 365 | bus.call_noreply(method); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * @brief This API checks for env var fitconfig. |
| 370 | * If not initialised OR updated as per the current system type, |
| 371 | * update this env var and reboot the system. |
| 372 | * |
| 373 | * @param[in] systemType IM kwd in vpd tells about which system type it is. |
| 374 | * */ |
| 375 | void setDevTreeEnv(const string& systemType) |
| 376 | { |
| 377 | string newDeviceTree; |
| 378 | |
| 379 | if (deviceTreeSystemTypeMap.find(systemType) != |
| 380 | deviceTreeSystemTypeMap.end()) |
| 381 | { |
| 382 | newDeviceTree = deviceTreeSystemTypeMap.at(systemType); |
| 383 | } |
| 384 | |
| 385 | string readVarValue; |
| 386 | bool envVarFound = false; |
| 387 | |
| 388 | vector<string> output = executeCmd("/sbin/fw_printenv"); |
| 389 | for (const auto& entry : output) |
| 390 | { |
| 391 | size_t pos = entry.find("="); |
| 392 | string key = entry.substr(0, pos); |
| 393 | if (key != "fitconfig") |
| 394 | { |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | envVarFound = true; |
| 399 | if (pos + 1 < entry.size()) |
| 400 | { |
| 401 | readVarValue = entry.substr(pos + 1); |
| 402 | if (readVarValue.find(newDeviceTree) != string::npos) |
| 403 | { |
| 404 | // fitconfig is Updated. No action needed |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | // set env and reboot and break. |
| 409 | setEnvAndReboot(key, newDeviceTree); |
| 410 | exit(0); |
| 411 | } |
| 412 | |
| 413 | // check If env var Not found |
| 414 | if (!envVarFound) |
| 415 | { |
| 416 | setEnvAndReboot("fitconfig", newDeviceTree); |
| 417 | } |
| 418 | } |
| 419 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 420 | /** |
| 421 | * @brief Populate Dbus. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 422 | * This method invokes all the populateInterface functions |
| 423 | * and notifies PIM about dbus object. |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 424 | * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the |
| 425 | * input. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 426 | * @param[in] js - Inventory json object |
| 427 | * @param[in] filePath - Path of the vpd file |
| 428 | * @param[in] preIntrStr - Interface string |
| 429 | */ |
| 430 | template <typename T> |
| 431 | static void populateDbus(const T& vpdMap, nlohmann::json& js, |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 432 | const string& filePath) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 433 | { |
| 434 | inventory::InterfaceMap interfaces; |
| 435 | inventory::ObjectMap objects; |
| 436 | inventory::PropertyMap prop; |
| 437 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 438 | bool isSystemVpd = false; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 439 | for (const auto& item : js["frus"][filePath]) |
| 440 | { |
| 441 | const auto& objectPath = item["inventoryPath"]; |
| 442 | sdbusplus::message::object_path object(objectPath); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 443 | isSystemVpd = item.value("isSystemVpd", false); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 444 | // Populate the VPD keywords and the common interfaces only if we |
| 445 | // are asked to inherit that data from the VPD, else only add the |
| 446 | // extraInterfaces. |
| 447 | if (item.value("inherit", true)) |
| 448 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 449 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 450 | { |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 451 | // Each record in the VPD becomes an interface and all |
| 452 | // keyword within the record are properties under that |
| 453 | // interface. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 454 | for (const auto& record : vpdMap) |
| 455 | { |
| 456 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 457 | record.second, ipzVpdInf + record.first, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 458 | } |
| 459 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 460 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 461 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 462 | populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 463 | } |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 464 | if (js.find("commonInterfaces") != js.end()) |
| 465 | { |
| 466 | populateInterfaces(js["commonInterfaces"], interfaces, vpdMap, |
| 467 | isSystemVpd); |
| 468 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 469 | } |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 470 | else |
| 471 | { |
| 472 | // Check if we have been asked to inherit specific record(s) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 473 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 474 | { |
| 475 | if (item.find("copyRecords") != item.end()) |
| 476 | { |
| 477 | for (const auto& record : item["copyRecords"]) |
| 478 | { |
| 479 | const string& recordName = record; |
| 480 | if (vpdMap.find(recordName) != vpdMap.end()) |
| 481 | { |
| 482 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 483 | vpdMap.at(recordName), ipzVpdInf + recordName, |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 484 | interfaces); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 490 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 491 | if (item.value("inheritEI", true)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 492 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 493 | // Populate interfaces and properties that are common to every FRU |
| 494 | // and additional interface that might be defined on a per-FRU |
| 495 | // basis. |
| 496 | if (item.find("extraInterfaces") != item.end()) |
| 497 | { |
| 498 | populateInterfaces(item["extraInterfaces"], interfaces, vpdMap, |
| 499 | isSystemVpd); |
| 500 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 501 | } |
| 502 | objects.emplace(move(object), move(interfaces)); |
| 503 | } |
| 504 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 505 | if (isSystemVpd) |
| 506 | { |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 507 | vector<uint8_t> imVal; |
| 508 | if constexpr (is_same<T, Parsed>::value) |
| 509 | { |
| 510 | auto property = vpdMap.find("VSBP"); |
| 511 | if (property != vpdMap.end()) |
| 512 | { |
| 513 | auto value = (property->second).find("IM"); |
| 514 | if (value != (property->second).end()) |
| 515 | { |
| 516 | copy(value->second.begin(), value->second.end(), |
| 517 | back_inserter(imVal)); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | fs::path target; |
| 523 | fs::path link = INVENTORY_JSON_SYM_LINK; |
| 524 | |
| 525 | ostringstream oss; |
| 526 | for (auto& i : imVal) |
| 527 | { |
| 528 | oss << setw(2) << setfill('0') << hex << static_cast<int>(i); |
| 529 | } |
| 530 | string imValStr = oss.str(); |
| 531 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 532 | if (imValStr == RAINIER_4U) // 4U |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 533 | { |
| 534 | target = INVENTORY_JSON_4U; |
| 535 | } |
| 536 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 537 | else if (imValStr == RAINIER_2U) // 2U |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 538 | { |
| 539 | target = INVENTORY_JSON_2U; |
| 540 | } |
| 541 | |
| 542 | // unlink the symlink which is created at build time |
| 543 | remove(INVENTORY_JSON_SYM_LINK); |
| 544 | // create a new symlink based on the system |
| 545 | fs::create_symlink(target, link); |
| 546 | |
| 547 | // Reloading the json |
| 548 | ifstream inventoryJson(link); |
| 549 | auto js = json::parse(inventoryJson); |
| 550 | inventoryJson.close(); |
| 551 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 552 | inventory::ObjectMap primeObject = primeInventory(js, vpdMap); |
| 553 | objects.insert(primeObject.begin(), primeObject.end()); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 554 | |
| 555 | // set the U-boot environment variable for device-tree |
| 556 | setDevTreeEnv(imValStr); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 557 | } |
| 558 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 559 | // Notify PIM |
| 560 | inventory::callPIM(move(objects)); |
| 561 | } |
| 562 | |
| 563 | int main(int argc, char** argv) |
| 564 | { |
| 565 | int rc = 0; |
| 566 | |
| 567 | try |
| 568 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 569 | App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store " |
| 570 | "in DBUS"}; |
| 571 | string file{}; |
| 572 | |
| 573 | app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)") |
| 574 | ->required() |
| 575 | ->check(ExistingFile); |
| 576 | |
| 577 | CLI11_PARSE(app, argc, argv); |
| 578 | |
| 579 | // Make sure that the file path we get is for a supported EEPROM |
| 580 | ifstream inventoryJson(INVENTORY_JSON); |
| 581 | auto js = json::parse(inventoryJson); |
| 582 | |
| 583 | if ((js.find("frus") == js.end()) || |
| 584 | (js["frus"].find(file) == js["frus"].end())) |
| 585 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 586 | cout << "Device path not in JSON, ignoring" << endl; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 587 | return 0; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 588 | } |
| 589 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 590 | Binary vpdVector = getVpdDataInVector(js, file); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame^] | 591 | ParserInterface* parser = ParserFactory::getParser(move(vpdVector)); |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 592 | |
| 593 | variant<KeywordVpdMap, Store> parseResult; |
| 594 | parseResult = parser->parse(); |
| 595 | |
| 596 | if (auto pVal = get_if<Store>(&parseResult)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 597 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 598 | populateDbus(pVal->getVpdMap(), js, file); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 599 | } |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 600 | else if (auto pVal = get_if<KeywordVpdMap>(&parseResult)) |
| 601 | { |
| 602 | populateDbus(*pVal, js, file); |
| 603 | } |
| 604 | |
| 605 | // release the parser object |
| 606 | ParserFactory::freeParser(parser); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 607 | } |
| 608 | catch (exception& e) |
| 609 | { |
| 610 | cerr << e.what() << "\n"; |
| 611 | rc = -1; |
| 612 | } |
| 613 | |
| 614 | return rc; |
| 615 | } |