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