PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 3 | #include "common_utility.hpp" |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 4 | #include "defines.hpp" |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 5 | #include "ibm_vpd_utils.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 6 | #include "ipz_parser.hpp" |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 7 | #include "keyword_vpd_parser.hpp" |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 8 | #include "memory_vpd_parser.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 9 | #include "parser_factory.hpp" |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 10 | #include "vpd_exceptions.hpp" |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 11 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 12 | #include <assert.h> |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 15 | #include <CLI/CLI.hpp> |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 16 | #include <algorithm> |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 17 | #include <cstdarg> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 18 | #include <exception> |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 19 | #include <filesystem> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 20 | #include <fstream> |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 21 | #include <gpiod.hpp> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <iterator> |
| 24 | #include <nlohmann/json.hpp> |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 25 | #include <phosphor-logging/log.hpp> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | using namespace openpower::vpd; |
| 29 | using namespace CLI; |
| 30 | using namespace vpd::keyword::parser; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 31 | using namespace openpower::vpd::constants; |
| 32 | namespace fs = filesystem; |
| 33 | using json = nlohmann::json; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 34 | using namespace openpower::vpd::parser::factory; |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 35 | using namespace openpower::vpd::inventory; |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 36 | using namespace openpower::vpd::memory::parser; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 37 | using namespace openpower::vpd::parser::interface; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 38 | using namespace openpower::vpd::exceptions; |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 39 | using namespace phosphor::logging; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 40 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 41 | static const deviceTreeMap deviceTreeSystemTypeMap = { |
Andrew Geissler | 2fe709f | 2021-03-25 10:59:07 -0500 | [diff] [blame] | 42 | {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier.dtb"}, |
| 43 | {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u.dtb"}, |
| 44 | {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"}, |
| 45 | {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}}; |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 46 | |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 47 | /** |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 48 | * @brief Returns the power state for chassis0 |
| 49 | */ |
| 50 | static auto getPowerState() |
| 51 | { |
| 52 | // TODO: How do we handle multiple chassis? |
| 53 | string powerState{}; |
| 54 | auto bus = sdbusplus::bus::new_default(); |
| 55 | auto properties = |
| 56 | bus.new_method_call("xyz.openbmc_project.State.Chassis", |
| 57 | "/xyz/openbmc_project/state/chassis0", |
| 58 | "org.freedesktop.DBus.Properties", "Get"); |
| 59 | properties.append("xyz.openbmc_project.State.Chassis"); |
| 60 | properties.append("CurrentPowerState"); |
| 61 | auto result = bus.call(properties); |
| 62 | if (!result.is_method_error()) |
| 63 | { |
| 64 | variant<string> val; |
| 65 | result.read(val); |
| 66 | if (auto pVal = get_if<string>(&val)) |
| 67 | { |
| 68 | powerState = *pVal; |
| 69 | } |
| 70 | } |
| 71 | cout << "Power state is: " << powerState << endl; |
| 72 | return powerState; |
| 73 | } |
| 74 | |
| 75 | /** |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 76 | * @brief Expands location codes |
| 77 | */ |
| 78 | static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap, |
| 79 | bool isSystemVpd) |
| 80 | { |
| 81 | auto expanded{unexpanded}; |
| 82 | static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard"; |
| 83 | static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN"; |
| 84 | static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS"; |
| 85 | size_t idx = expanded.find("fcs"); |
| 86 | try |
| 87 | { |
| 88 | if (idx != string::npos) |
| 89 | { |
| 90 | string fc{}; |
| 91 | string se{}; |
| 92 | if (isSystemVpd) |
| 93 | { |
| 94 | const auto& fcData = vpdMap.at("VCEN").at("FC"); |
| 95 | const auto& seData = vpdMap.at("VCEN").at("SE"); |
| 96 | fc = string(fcData.data(), fcData.size()); |
| 97 | se = string(seData.data(), seData.size()); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC"); |
| 102 | se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE"); |
| 103 | } |
| 104 | |
Alpana Kumari | 81671f6 | 2021-02-10 02:21:59 -0600 | [diff] [blame] | 105 | // TODO: See if ND0 can be placed in the JSON |
| 106 | expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
| 110 | idx = expanded.find("mts"); |
| 111 | if (idx != string::npos) |
| 112 | { |
| 113 | string mt{}; |
| 114 | string se{}; |
| 115 | if (isSystemVpd) |
| 116 | { |
| 117 | const auto& mtData = vpdMap.at("VSYS").at("TM"); |
| 118 | const auto& seData = vpdMap.at("VSYS").at("SE"); |
| 119 | mt = string(mtData.data(), mtData.size()); |
| 120 | se = string(seData.data(), seData.size()); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM"); |
| 125 | se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE"); |
| 126 | } |
| 127 | |
| 128 | replace(mt.begin(), mt.end(), '-', '.'); |
| 129 | expanded.replace(idx, 3, mt + "." + se); |
| 130 | } |
| 131 | } |
| 132 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 133 | catch (exception& e) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 134 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 135 | cerr << "Failed to expand location code with exception: " << e.what() |
| 136 | << "\n"; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 137 | } |
| 138 | return expanded; |
| 139 | } |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 140 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 141 | /** |
| 142 | * @brief Populate FRU specific interfaces. |
| 143 | * |
| 144 | * This is a common method which handles both |
| 145 | * ipz and keyword specific interfaces thus, |
| 146 | * reducing the code redundancy. |
| 147 | * @param[in] map - Reference to the innermost keyword-value map. |
| 148 | * @param[in] preIntrStr - Reference to the interface string. |
| 149 | * @param[out] interfaces - Reference to interface map. |
| 150 | */ |
| 151 | template <typename T> |
| 152 | static void populateFruSpecificInterfaces(const T& map, |
| 153 | const string& preIntrStr, |
| 154 | inventory::InterfaceMap& interfaces) |
| 155 | { |
| 156 | inventory::PropertyMap prop; |
| 157 | |
| 158 | for (const auto& kwVal : map) |
| 159 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 160 | vector<uint8_t> vec(kwVal.second.begin(), kwVal.second.end()); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 161 | |
| 162 | auto kw = kwVal.first; |
| 163 | |
| 164 | if (kw[0] == '#') |
| 165 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 166 | kw = string("PD_") + kw[1]; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 167 | } |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 168 | else if (isdigit(kw[0])) |
| 169 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 170 | kw = string("N_") + kw; |
Alpana Kumari | 8ea3f6d | 2020-04-02 00:26:07 -0500 | [diff] [blame] | 171 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 172 | prop.emplace(move(kw), move(vec)); |
| 173 | } |
| 174 | |
| 175 | interfaces.emplace(preIntrStr, move(prop)); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @brief Populate Interfaces. |
| 180 | * |
| 181 | * This method populates common and extra interfaces to dbus. |
| 182 | * @param[in] js - json object |
| 183 | * @param[out] interfaces - Reference to interface map |
| 184 | * @param[in] vpdMap - Reference to the parsed vpd map. |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 185 | * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 186 | */ |
| 187 | template <typename T> |
| 188 | static void populateInterfaces(const nlohmann::json& js, |
| 189 | inventory::InterfaceMap& interfaces, |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 190 | const T& vpdMap, bool isSystemVpd) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 191 | { |
| 192 | for (const auto& ifs : js.items()) |
| 193 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 194 | string inf = ifs.key(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 195 | inventory::PropertyMap props; |
| 196 | |
| 197 | for (const auto& itr : ifs.value().items()) |
| 198 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 199 | const string& busProp = itr.key(); |
| 200 | |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 201 | if (itr.value().is_boolean()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 202 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 203 | props.emplace(busProp, itr.value().get<bool>()); |
| 204 | } |
| 205 | else if (itr.value().is_string()) |
| 206 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 207 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 208 | { |
| 209 | if (busProp == "LocationCode" && |
| 210 | inf == "com.ibm.ipzvpd.Location") |
| 211 | { |
| 212 | auto prop = expandLocationCode( |
| 213 | itr.value().get<string>(), vpdMap, isSystemVpd); |
| 214 | props.emplace(busProp, prop); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | props.emplace(busProp, itr.value().get<string>()); |
| 219 | } |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | props.emplace(busProp, itr.value().get<string>()); |
| 224 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 225 | } |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 226 | else if (itr.value().is_object()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 227 | { |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 228 | const string& rec = itr.value().value("recordName", ""); |
| 229 | const string& kw = itr.value().value("keywordName", ""); |
| 230 | const string& encoding = itr.value().value("encoding", ""); |
| 231 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 232 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 233 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 234 | if (!rec.empty() && !kw.empty() && vpdMap.count(rec) && |
| 235 | vpdMap.at(rec).count(kw)) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 236 | { |
| 237 | auto encoded = |
| 238 | encodeKeyword(vpdMap.at(rec).at(kw), encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 239 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 240 | } |
| 241 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 242 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 243 | { |
| 244 | if (!kw.empty() && vpdMap.count(kw)) |
| 245 | { |
| 246 | auto prop = |
| 247 | string(vpdMap.at(kw).begin(), vpdMap.at(kw).end()); |
| 248 | auto encoded = encodeKeyword(prop, encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 249 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 250 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | interfaces.emplace(inf, move(props)); |
| 255 | } |
| 256 | } |
| 257 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 258 | static Binary getVpdDataInVector(const nlohmann::json& js, const string& file) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 259 | { |
| 260 | uint32_t offset = 0; |
| 261 | // check if offset present? |
| 262 | for (const auto& item : js["frus"][file]) |
| 263 | { |
| 264 | if (item.find("offset") != item.end()) |
| 265 | { |
| 266 | offset = item["offset"]; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // TODO: Figure out a better way to get max possible VPD size. |
| 271 | Binary vpdVector; |
| 272 | vpdVector.resize(65504); |
| 273 | ifstream vpdFile; |
| 274 | vpdFile.open(file, ios::binary); |
| 275 | |
| 276 | vpdFile.seekg(offset, ios_base::cur); |
| 277 | vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504); |
| 278 | vpdVector.resize(vpdFile.gcount()); |
| 279 | |
| 280 | return vpdVector; |
| 281 | } |
| 282 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 283 | /** This API will be called at the end of VPD collection to perform any post |
| 284 | * actions. |
| 285 | * |
| 286 | * @param[in] json - json object |
| 287 | * @param[in] file - eeprom file path |
| 288 | */ |
| 289 | static void postFailAction(const nlohmann::json& json, const string& file) |
| 290 | { |
| 291 | if ((json["frus"][file].at(0)).find("postActionFail") == |
| 292 | json["frus"][file].at(0).end()) |
| 293 | { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | uint8_t pinValue = 0; |
| 298 | string pinName; |
| 299 | |
| 300 | for (const auto& postAction : |
| 301 | (json["frus"][file].at(0))["postActionFail"].items()) |
| 302 | { |
| 303 | if (postAction.key() == "pin") |
| 304 | { |
| 305 | pinName = postAction.value(); |
| 306 | } |
| 307 | else if (postAction.key() == "value") |
| 308 | { |
| 309 | // Get the value to set |
| 310 | pinValue = postAction.value(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 315 | |
| 316 | try |
| 317 | { |
| 318 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 319 | |
| 320 | if (!outputLine) |
| 321 | { |
| 322 | cout << "Couldn't find output line:" << pinName |
| 323 | << " on GPIO. Skipping...\n"; |
| 324 | |
| 325 | return; |
| 326 | } |
| 327 | outputLine.request( |
| 328 | {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 329 | pinValue); |
| 330 | } |
| 331 | catch (system_error&) |
| 332 | { |
| 333 | cerr << "Failed to set post-action GPIO" << endl; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** Performs any pre-action needed to get the FRU setup for collection. |
| 338 | * |
| 339 | * @param[in] json - json object |
| 340 | * @param[in] file - eeprom file path |
| 341 | */ |
| 342 | static void preAction(const nlohmann::json& json, const string& file) |
| 343 | { |
| 344 | if ((json["frus"][file].at(0)).find("preAction") == |
| 345 | json["frus"][file].at(0).end()) |
| 346 | { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | uint8_t pinValue = 0; |
| 351 | string pinName; |
| 352 | |
| 353 | for (const auto& postAction : |
| 354 | (json["frus"][file].at(0))["preAction"].items()) |
| 355 | { |
| 356 | if (postAction.key() == "pin") |
| 357 | { |
| 358 | pinName = postAction.value(); |
| 359 | } |
| 360 | else if (postAction.key() == "value") |
| 361 | { |
| 362 | // Get the value to set |
| 363 | pinValue = postAction.value(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 368 | try |
| 369 | { |
| 370 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 371 | |
| 372 | if (!outputLine) |
| 373 | { |
| 374 | cout << "Couldn't find output line:" << pinName |
| 375 | << " on GPIO. Skipping...\n"; |
| 376 | |
| 377 | return; |
| 378 | } |
| 379 | outputLine.request( |
| 380 | {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 381 | pinValue); |
| 382 | } |
| 383 | catch (system_error&) |
| 384 | { |
| 385 | cerr << "Failed to set pre-action GPIO" << endl; |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | // Now bind the device |
| 390 | string bind = json["frus"][file].at(0).value("bind", ""); |
| 391 | cout << "Binding device " << bind << endl; |
| 392 | string bindCmd = string("echo \"") + bind + |
| 393 | string("\" > /sys/bus/i2c/drivers/at24/bind"); |
| 394 | cout << bindCmd << endl; |
| 395 | executeCmd(bindCmd); |
| 396 | |
| 397 | // Check if device showed up (test for file) |
| 398 | if (!fs::exists(file)) |
| 399 | { |
| 400 | cout << "EEPROM " << file << " does not exist. Take failure action" |
| 401 | << endl; |
| 402 | // If not, then take failure postAction |
| 403 | postFailAction(json, file); |
| 404 | } |
| 405 | } |
| 406 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 407 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 408 | * @brief Prime the Inventory |
| 409 | * Prime the inventory by populating only the location code, |
| 410 | * type interface and the inventory object for the frus |
| 411 | * which are not system vpd fru. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 412 | * |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 413 | * @param[in] jsObject - Reference to vpd inventory json object |
| 414 | * @param[in] vpdMap - Reference to the parsed vpd map |
| 415 | * |
| 416 | * @returns Map of items in extraInterface. |
| 417 | */ |
| 418 | template <typename T> |
| 419 | inventory::ObjectMap primeInventory(const nlohmann::json& jsObject, |
| 420 | const T& vpdMap) |
| 421 | { |
| 422 | inventory::ObjectMap objects; |
| 423 | |
| 424 | for (auto& itemFRUS : jsObject["frus"].items()) |
| 425 | { |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 426 | // Take pre actions |
| 427 | preAction(jsObject, itemFRUS.key()); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 428 | for (auto& itemEEPROM : itemFRUS.value()) |
| 429 | { |
| 430 | inventory::InterfaceMap interfaces; |
| 431 | auto isSystemVpd = itemEEPROM.value("isSystemVpd", false); |
| 432 | inventory::Object object(itemEEPROM.at("inventoryPath")); |
| 433 | |
| 434 | if (!isSystemVpd && !itemEEPROM.value("noprime", false)) |
| 435 | { |
Alpana Kumari | cfd7a75 | 2021-02-07 23:23:01 -0600 | [diff] [blame] | 436 | inventory::PropertyMap presProp; |
| 437 | presProp.emplace("Present", false); |
| 438 | interfaces.emplace("xyz.openbmc_project.Inventory.Item", |
| 439 | move(presProp)); |
| 440 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 441 | if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end()) |
| 442 | { |
| 443 | for (const auto& eI : itemEEPROM["extraInterfaces"].items()) |
| 444 | { |
| 445 | inventory::PropertyMap props; |
| 446 | if (eI.key() == |
| 447 | openpower::vpd::constants::LOCATION_CODE_INF) |
| 448 | { |
| 449 | if constexpr (std::is_same<T, Parsed>::value) |
| 450 | { |
| 451 | for (auto& lC : eI.value().items()) |
| 452 | { |
| 453 | auto propVal = expandLocationCode( |
| 454 | lC.value().get<string>(), vpdMap, true); |
| 455 | |
| 456 | props.emplace(move(lC.key()), |
| 457 | move(propVal)); |
| 458 | interfaces.emplace(move(eI.key()), |
| 459 | move(props)); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | else if (eI.key().find("Inventory.Item.") != |
| 464 | string::npos) |
| 465 | { |
| 466 | interfaces.emplace(move(eI.key()), move(props)); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | objects.emplace(move(object), move(interfaces)); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | return objects; |
| 475 | } |
| 476 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 477 | /** |
| 478 | * @brief This API executes command to set environment variable |
| 479 | * And then reboot the system |
| 480 | * @param[in] key -env key to set new value |
| 481 | * @param[in] value -value to set. |
| 482 | */ |
| 483 | void setEnvAndReboot(const string& key, const string& value) |
| 484 | { |
| 485 | // set env and reboot and break. |
| 486 | executeCmd("/sbin/fw_setenv", key, value); |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 487 | log<level::INFO>("Rebooting BMC to pick up new device tree"); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 488 | // make dbus call to reboot |
| 489 | auto bus = sdbusplus::bus::new_default_system(); |
| 490 | auto method = bus.new_method_call( |
| 491 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 492 | "org.freedesktop.systemd1.Manager", "Reboot"); |
| 493 | bus.call_noreply(method); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * @brief This API checks for env var fitconfig. |
| 498 | * If not initialised OR updated as per the current system type, |
| 499 | * update this env var and reboot the system. |
| 500 | * |
| 501 | * @param[in] systemType IM kwd in vpd tells about which system type it is. |
| 502 | * */ |
| 503 | void setDevTreeEnv(const string& systemType) |
| 504 | { |
| 505 | string newDeviceTree; |
| 506 | |
| 507 | if (deviceTreeSystemTypeMap.find(systemType) != |
| 508 | deviceTreeSystemTypeMap.end()) |
| 509 | { |
| 510 | newDeviceTree = deviceTreeSystemTypeMap.at(systemType); |
| 511 | } |
| 512 | |
| 513 | string readVarValue; |
| 514 | bool envVarFound = false; |
| 515 | |
| 516 | vector<string> output = executeCmd("/sbin/fw_printenv"); |
| 517 | for (const auto& entry : output) |
| 518 | { |
| 519 | size_t pos = entry.find("="); |
| 520 | string key = entry.substr(0, pos); |
| 521 | if (key != "fitconfig") |
| 522 | { |
| 523 | continue; |
| 524 | } |
| 525 | |
| 526 | envVarFound = true; |
| 527 | if (pos + 1 < entry.size()) |
| 528 | { |
| 529 | readVarValue = entry.substr(pos + 1); |
| 530 | if (readVarValue.find(newDeviceTree) != string::npos) |
| 531 | { |
| 532 | // fitconfig is Updated. No action needed |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | // set env and reboot and break. |
| 537 | setEnvAndReboot(key, newDeviceTree); |
| 538 | exit(0); |
| 539 | } |
| 540 | |
| 541 | // check If env var Not found |
| 542 | if (!envVarFound) |
| 543 | { |
| 544 | setEnvAndReboot("fitconfig", newDeviceTree); |
| 545 | } |
| 546 | } |
| 547 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 548 | /** |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 549 | * @brief API to call VPD manager to write VPD to EEPROM. |
| 550 | * @param[in] Object path. |
| 551 | * @param[in] record to be updated. |
| 552 | * @param[in] keyword to be updated. |
| 553 | * @param[in] keyword data to be updated |
| 554 | */ |
| 555 | void updateHardware(const string& objectName, const string& recName, |
| 556 | const string& kwdName, const Binary& data) |
| 557 | { |
| 558 | try |
| 559 | { |
| 560 | auto bus = sdbusplus::bus::new_default(); |
| 561 | auto properties = |
| 562 | bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword"); |
| 563 | properties.append( |
| 564 | static_cast<sdbusplus::message::object_path>(objectName)); |
| 565 | properties.append(recName); |
| 566 | properties.append(kwdName); |
| 567 | properties.append(data); |
| 568 | bus.call(properties); |
| 569 | } |
| 570 | catch (const sdbusplus::exception::SdBusError& e) |
| 571 | { |
| 572 | std::string what = |
| 573 | "VPDManager WriteKeyword api failed for inventory path " + |
| 574 | objectName; |
| 575 | what += " record " + recName; |
| 576 | what += " keyword " + kwdName; |
| 577 | what += " with bus error = " + std::string(e.what()); |
| 578 | |
| 579 | // map to hold additional data in case of logging pel |
| 580 | PelAdditionalData additionalData{}; |
| 581 | additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName); |
| 582 | additionalData.emplace("DESCRIPTION", what); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 583 | createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * @brief API to check if we need to restore system VPD |
| 589 | * This functionality is only applicable for IPZ VPD data. |
| 590 | * @param[in] vpdMap - IPZ vpd map |
| 591 | * @param[in] objectPath - Object path for the FRU |
| 592 | * @return EEPROMs with records and keywords updated at standby |
| 593 | */ |
| 594 | std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap, |
| 595 | const string& objectPath) |
| 596 | { |
| 597 | // the list of keywords for VSYS record is as per the S0 system. Should be |
| 598 | // updated for another type of systems |
| 599 | static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{ |
| 600 | {"VSYS", {"BR", "TM", "SE", "SU", "RB"}}, |
| 601 | {"VCEN", {"FC", "SE"}}, |
| 602 | {"LXR0", {"LX"}}}; |
| 603 | |
| 604 | // vector to hold all the EEPROMs updated at standby |
| 605 | std::vector<RestoredEeproms> updatedEeproms = {}; |
| 606 | |
| 607 | for (const auto& systemRecKwdPair : svpdKwdMap) |
| 608 | { |
| 609 | auto it = vpdMap.find(systemRecKwdPair.first); |
| 610 | |
| 611 | // check if record is found in map we got by parser |
| 612 | if (it != vpdMap.end()) |
| 613 | { |
| 614 | const auto& kwdListForRecord = systemRecKwdPair.second; |
| 615 | for (const auto& keyword : kwdListForRecord) |
| 616 | { |
| 617 | DbusPropertyMap& kwdValMap = it->second; |
| 618 | auto iterator = kwdValMap.find(keyword); |
| 619 | |
| 620 | if (iterator != kwdValMap.end()) |
| 621 | { |
| 622 | string& kwdValue = iterator->second; |
| 623 | |
| 624 | // check bus data |
| 625 | const string& recordName = systemRecKwdPair.first; |
| 626 | const string& busValue = readBusProperty( |
| 627 | objectPath, ipzVpdInf + recordName, keyword); |
| 628 | |
| 629 | if (busValue.find_first_not_of(' ') != string::npos) |
| 630 | { |
| 631 | if (kwdValue.find_first_not_of(' ') != string::npos) |
| 632 | { |
| 633 | // both the data are present, check for mismatch |
| 634 | if (busValue != kwdValue) |
| 635 | { |
| 636 | string errMsg = "VPD data mismatch on cache " |
| 637 | "and hardware for record: "; |
| 638 | errMsg += (*it).first; |
| 639 | errMsg += " and keyword: "; |
| 640 | errMsg += keyword; |
| 641 | |
| 642 | // data mismatch |
| 643 | PelAdditionalData additionalData; |
| 644 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 645 | objectPath); |
| 646 | |
| 647 | additionalData.emplace("DESCRIPTION", errMsg); |
| 648 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 649 | createPEL(additionalData, PelSeverity::WARNING, |
| 650 | errIntfForInvalidVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | // implies hardware data is blank |
| 656 | // update the map |
| 657 | Binary busData(busValue.begin(), busValue.end()); |
| 658 | |
| 659 | updatedEeproms.push_back(std::make_tuple( |
| 660 | objectPath, recordName, keyword, busData)); |
| 661 | } |
| 662 | |
| 663 | // update the map as well, so that cache data is not |
| 664 | // updated as blank while populating VPD map on Dbus in |
| 665 | // populateDBus Api |
| 666 | kwdValue = busValue; |
| 667 | continue; |
| 668 | } |
| 669 | else if (kwdValue.find_first_not_of(' ') == string::npos) |
| 670 | { |
| 671 | string errMsg = "VPD is blank on both cache and " |
| 672 | "hardware for record: "; |
| 673 | errMsg += (*it).first; |
| 674 | errMsg += " and keyword: "; |
| 675 | errMsg += keyword; |
| 676 | errMsg += ". SSR need to update hardware VPD."; |
| 677 | |
| 678 | // both the data are blanks, log PEL |
| 679 | PelAdditionalData additionalData; |
| 680 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 681 | objectPath); |
| 682 | |
| 683 | additionalData.emplace("DESCRIPTION", errMsg); |
| 684 | |
| 685 | // log PEL TODO: Block IPL |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 686 | createPEL(additionalData, PelSeverity::ERROR, |
| 687 | errIntfForBlankSystemVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 688 | continue; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return updatedEeproms; |
| 696 | } |
| 697 | |
| 698 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 699 | * @brief Populate Dbus. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 700 | * This method invokes all the populateInterface functions |
| 701 | * and notifies PIM about dbus object. |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 702 | * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the |
| 703 | * input. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 704 | * @param[in] js - Inventory json object |
| 705 | * @param[in] filePath - Path of the vpd file |
| 706 | * @param[in] preIntrStr - Interface string |
| 707 | */ |
| 708 | template <typename T> |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 709 | static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 710 | { |
| 711 | inventory::InterfaceMap interfaces; |
| 712 | inventory::ObjectMap objects; |
| 713 | inventory::PropertyMap prop; |
| 714 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 715 | // map to hold all the keywords whose value has been changed at standby |
| 716 | vector<RestoredEeproms> updatedEeproms = {}; |
| 717 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 718 | bool isSystemVpd = false; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 719 | for (const auto& item : js["frus"][filePath]) |
| 720 | { |
| 721 | const auto& objectPath = item["inventoryPath"]; |
| 722 | sdbusplus::message::object_path object(objectPath); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 723 | isSystemVpd = item.value("isSystemVpd", false); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 724 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 725 | // Populate the VPD keywords and the common interfaces only if we |
| 726 | // are asked to inherit that data from the VPD, else only add the |
| 727 | // extraInterfaces. |
| 728 | if (item.value("inherit", true)) |
| 729 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 730 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 731 | { |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 732 | if (isSystemVpd) |
| 733 | { |
| 734 | std::vector<std::string> interfaces = { |
| 735 | motherBoardInterface}; |
| 736 | // call mapper to check for object path creation |
| 737 | MapperResponse subTree = |
| 738 | getObjectSubtreeForInterfaces(pimPath, 0, interfaces); |
| 739 | |
| 740 | // Skip system vpd restore if object path is not generated |
| 741 | // for motherboard, Implies first boot. |
| 742 | if (subTree.size() != 0) |
| 743 | { |
| 744 | assert( |
| 745 | (subTree.find(pimPath + std::string(objectPath)) != |
| 746 | subTree.end())); |
| 747 | |
| 748 | updatedEeproms = restoreSystemVPD(vpdMap, objectPath); |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | log<level::ERR>("No object path found"); |
| 753 | } |
| 754 | } |
| 755 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 756 | // Each record in the VPD becomes an interface and all |
| 757 | // keyword within the record are properties under that |
| 758 | // interface. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 759 | for (const auto& record : vpdMap) |
| 760 | { |
| 761 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 762 | record.second, ipzVpdInf + record.first, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 763 | } |
| 764 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 765 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 766 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 767 | populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 768 | } |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 769 | if (js.find("commonInterfaces") != js.end()) |
| 770 | { |
| 771 | populateInterfaces(js["commonInterfaces"], interfaces, vpdMap, |
| 772 | isSystemVpd); |
| 773 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 774 | } |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 775 | else |
| 776 | { |
| 777 | // Check if we have been asked to inherit specific record(s) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 778 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 779 | { |
| 780 | if (item.find("copyRecords") != item.end()) |
| 781 | { |
| 782 | for (const auto& record : item["copyRecords"]) |
| 783 | { |
| 784 | const string& recordName = record; |
| 785 | if (vpdMap.find(recordName) != vpdMap.end()) |
| 786 | { |
| 787 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 788 | vpdMap.at(recordName), ipzVpdInf + recordName, |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 789 | interfaces); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 795 | if (item.value("inheritEI", true)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 796 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 797 | // Populate interfaces and properties that are common to every FRU |
| 798 | // and additional interface that might be defined on a per-FRU |
| 799 | // basis. |
| 800 | if (item.find("extraInterfaces") != item.end()) |
| 801 | { |
| 802 | populateInterfaces(item["extraInterfaces"], interfaces, vpdMap, |
| 803 | isSystemVpd); |
| 804 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 805 | } |
| 806 | objects.emplace(move(object), move(interfaces)); |
| 807 | } |
| 808 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 809 | if (isSystemVpd) |
| 810 | { |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 811 | vector<uint8_t> imVal; |
| 812 | if constexpr (is_same<T, Parsed>::value) |
| 813 | { |
| 814 | auto property = vpdMap.find("VSBP"); |
| 815 | if (property != vpdMap.end()) |
| 816 | { |
| 817 | auto value = (property->second).find("IM"); |
| 818 | if (value != (property->second).end()) |
| 819 | { |
| 820 | copy(value->second.begin(), value->second.end(), |
| 821 | back_inserter(imVal)); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | fs::path target; |
| 827 | fs::path link = INVENTORY_JSON_SYM_LINK; |
| 828 | |
| 829 | ostringstream oss; |
| 830 | for (auto& i : imVal) |
| 831 | { |
| 832 | oss << setw(2) << setfill('0') << hex << static_cast<int>(i); |
| 833 | } |
| 834 | string imValStr = oss.str(); |
| 835 | |
Andrew Geissler | 7c9cddf | 2021-03-19 10:34:31 -0500 | [diff] [blame] | 836 | if ((imValStr == RAINIER_4U) || // 4U |
| 837 | (imValStr == RAINIER_1S4U)) |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 838 | { |
| 839 | target = INVENTORY_JSON_4U; |
| 840 | } |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 841 | else if (imValStr == RAINIER_2U) // 2U |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 842 | { |
| 843 | target = INVENTORY_JSON_2U; |
| 844 | } |
Santosh Puranik | 4641bff | 2020-11-30 20:26:44 +0530 | [diff] [blame] | 845 | else if (imValStr == EVEREST) |
| 846 | { |
| 847 | target = INVENTORY_JSON_EVEREST; |
| 848 | } |
PriyangaRamasamy | a504c3e | 2020-12-06 12:14:52 -0600 | [diff] [blame] | 849 | else |
| 850 | { |
| 851 | PelAdditionalData additionalData{}; |
| 852 | const string& baseFruInventoryPath = |
| 853 | js["frus"][filePath][0]["inventoryPath"].get_ref<string&>(); |
| 854 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 855 | INVENTORY_PATH + baseFruInventoryPath); |
| 856 | additionalData.emplace( |
| 857 | "DESCRIPTION", "System IM value is erroneous/not supported."); |
| 858 | additionalData.emplace("INVALID IM VALUE", imValStr); |
| 859 | createPEL(additionalData, PelSeverity::ERROR, errIntfForInvalidVPD); |
| 860 | throw runtime_error( |
| 861 | "Erroneous/Unsupported IM in System VPD. PEL logged."); |
| 862 | } |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 863 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 864 | // Create the directory for hosting the symlink |
| 865 | fs::create_directories(VPD_FILES_PATH); |
| 866 | // unlink the symlink previously created (if any) |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 867 | remove(INVENTORY_JSON_SYM_LINK); |
| 868 | // create a new symlink based on the system |
| 869 | fs::create_symlink(target, link); |
| 870 | |
| 871 | // Reloading the json |
| 872 | ifstream inventoryJson(link); |
| 873 | auto js = json::parse(inventoryJson); |
| 874 | inventoryJson.close(); |
| 875 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 876 | inventory::ObjectMap primeObject = primeInventory(js, vpdMap); |
| 877 | objects.insert(primeObject.begin(), primeObject.end()); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 878 | |
| 879 | // set the U-boot environment variable for device-tree |
| 880 | setDevTreeEnv(imValStr); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 881 | |
| 882 | // if system VPD has been restored at standby, update the EEPROM |
| 883 | for (const auto& item : updatedEeproms) |
| 884 | { |
| 885 | updateHardware(get<0>(item), get<1>(item), get<2>(item), |
| 886 | get<3>(item)); |
| 887 | } |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 888 | } |
| 889 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 890 | // Notify PIM |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 891 | common::utility::callPIM(move(objects)); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | int main(int argc, char** argv) |
| 895 | { |
| 896 | int rc = 0; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 897 | string file{}; |
| 898 | json js{}; |
| 899 | |
| 900 | // map to hold additional data in case of logging pel |
| 901 | PelAdditionalData additionalData{}; |
| 902 | |
| 903 | // this is needed to hold base fru inventory path in case there is ECC or |
| 904 | // vpd exception while parsing the file |
| 905 | std::string baseFruInventoryPath = {}; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 906 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 907 | // severity for PEL |
| 908 | PelSeverity pelSeverity = PelSeverity::WARNING; |
| 909 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 910 | try |
| 911 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 912 | App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store " |
| 913 | "in DBUS"}; |
| 914 | string file{}; |
| 915 | |
| 916 | app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)") |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 917 | ->required(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 918 | |
| 919 | CLI11_PARSE(app, argc, argv); |
| 920 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 921 | // PEL severity should be ERROR in case of any system VPD failure |
| 922 | if (file == systemVpdFilePath) |
| 923 | { |
| 924 | pelSeverity = PelSeverity::ERROR; |
| 925 | } |
| 926 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 927 | auto jsonToParse = INVENTORY_JSON_DEFAULT; |
| 928 | |
| 929 | // If the symlink exists, it means it has been setup for us, switch the |
| 930 | // path |
| 931 | if (fs::exists(INVENTORY_JSON_SYM_LINK)) |
| 932 | { |
| 933 | jsonToParse = INVENTORY_JSON_SYM_LINK; |
| 934 | } |
| 935 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 936 | // 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] | 937 | ifstream inventoryJson(jsonToParse); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 938 | if (!inventoryJson) |
| 939 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 940 | throw(VpdJsonException("Failed to access Json path", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | try |
| 944 | { |
| 945 | js = json::parse(inventoryJson); |
| 946 | } |
| 947 | catch (json::parse_error& ex) |
| 948 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 949 | throw(VpdJsonException("Json parsing failed", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 950 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 951 | |
| 952 | if ((js.find("frus") == js.end()) || |
| 953 | (js["frus"].find(file) == js["frus"].end())) |
| 954 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 955 | cout << "Device path not in JSON, ignoring" << endl; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 956 | return 0; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 957 | } |
| 958 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 959 | if (!fs::exists(file)) |
| 960 | { |
| 961 | cout << "Device path: " << file |
| 962 | << " does not exist. Spurious udev event? Exiting." << endl; |
| 963 | return 0; |
| 964 | } |
| 965 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 966 | baseFruInventoryPath = js["frus"][file][0]["inventoryPath"]; |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 967 | // Check if we can read the VPD file based on the power state |
| 968 | if (js["frus"][file].at(0).value("powerOffOnly", false)) |
| 969 | { |
| 970 | if ("xyz.openbmc_project.State.Chassis.PowerState.On" == |
| 971 | getPowerState()) |
| 972 | { |
| 973 | cout << "This VPD cannot be read when power is ON" << endl; |
| 974 | return 0; |
| 975 | } |
| 976 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 977 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 978 | try |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 979 | { |
Alpana Kumari | 80f1534 | 2020-06-09 07:41:02 -0500 | [diff] [blame] | 980 | // check if vpd file is empty |
| 981 | if (file.empty()) |
| 982 | { |
| 983 | throw(VpdDataException( |
| 984 | "VPD file is empty. Can't process with blank file.")); |
| 985 | } |
| 986 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 987 | Binary vpdVector = getVpdDataInVector(js, file); |
| 988 | ParserInterface* parser = ParserFactory::getParser(move(vpdVector)); |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 989 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 990 | variant<KeywordVpdMap, Store> parseResult; |
| 991 | parseResult = parser->parse(); |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame^] | 992 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 993 | if (auto pVal = get_if<Store>(&parseResult)) |
| 994 | { |
| 995 | populateDbus(pVal->getVpdMap(), js, file); |
| 996 | } |
| 997 | else if (auto pVal = get_if<KeywordVpdMap>(&parseResult)) |
| 998 | { |
| 999 | populateDbus(*pVal, js, file); |
| 1000 | } |
| 1001 | |
| 1002 | // release the parser object |
| 1003 | ParserFactory::freeParser(parser); |
| 1004 | } |
| 1005 | catch (exception& e) |
| 1006 | { |
| 1007 | postFailAction(js, file); |
PriyangaRamasamy | a504c3e | 2020-12-06 12:14:52 -0600 | [diff] [blame] | 1008 | throw; |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1009 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1010 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1011 | catch (const VpdJsonException& ex) |
| 1012 | { |
| 1013 | additionalData.emplace("JSON_PATH", ex.getJsonPath()); |
| 1014 | additionalData.emplace("DESCRIPTION", ex.what()); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1015 | createPEL(additionalData, pelSeverity, errIntfForJsonFailure); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1016 | |
| 1017 | cerr << ex.what() << "\n"; |
| 1018 | rc = -1; |
| 1019 | } |
| 1020 | catch (const VpdEccException& ex) |
| 1021 | { |
| 1022 | additionalData.emplace("DESCRIPTION", "ECC check failed"); |
| 1023 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1024 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1025 | createPEL(additionalData, pelSeverity, errIntfForEccCheckFail); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1026 | |
| 1027 | cerr << ex.what() << "\n"; |
| 1028 | rc = -1; |
| 1029 | } |
| 1030 | catch (const VpdDataException& ex) |
| 1031 | { |
| 1032 | additionalData.emplace("DESCRIPTION", "Invalid VPD data"); |
| 1033 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1034 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1035 | createPEL(additionalData, pelSeverity, errIntfForInvalidVPD); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1036 | |
| 1037 | cerr << ex.what() << "\n"; |
| 1038 | rc = -1; |
| 1039 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1040 | catch (exception& e) |
| 1041 | { |
| 1042 | cerr << e.what() << "\n"; |
| 1043 | rc = -1; |
| 1044 | } |
| 1045 | |
| 1046 | return rc; |
Alpana Kumari | 80f1534 | 2020-06-09 07:41:02 -0500 | [diff] [blame] | 1047 | } |