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 | /* It does nothing. Just an empty function to return null |
| 284 | * at the end of variadic template args |
| 285 | */ |
| 286 | static string getCommand() |
| 287 | { |
| 288 | return ""; |
| 289 | } |
| 290 | |
| 291 | /* This function to arrange all arguments to make command |
| 292 | */ |
| 293 | template <typename T, typename... Types> |
| 294 | static string getCommand(T arg1, Types... args) |
| 295 | { |
| 296 | string cmd = " " + arg1 + getCommand(args...); |
| 297 | |
| 298 | return cmd; |
| 299 | } |
| 300 | |
| 301 | /* This API takes arguments and run that command |
| 302 | * returns output of that command |
| 303 | */ |
| 304 | template <typename T, typename... Types> |
| 305 | static vector<string> executeCmd(T&& path, Types... args) |
| 306 | { |
| 307 | vector<string> stdOutput; |
| 308 | array<char, 128> buffer; |
| 309 | |
| 310 | string cmd = path + getCommand(args...); |
| 311 | |
| 312 | unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); |
| 313 | if (!pipe) |
| 314 | { |
| 315 | throw runtime_error("popen() failed!"); |
| 316 | } |
| 317 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 318 | { |
| 319 | stdOutput.emplace_back(buffer.data()); |
| 320 | } |
| 321 | |
| 322 | return stdOutput; |
| 323 | } |
| 324 | |
| 325 | /** This API will be called at the end of VPD collection to perform any post |
| 326 | * actions. |
| 327 | * |
| 328 | * @param[in] json - json object |
| 329 | * @param[in] file - eeprom file path |
| 330 | */ |
| 331 | static void postFailAction(const nlohmann::json& json, const string& file) |
| 332 | { |
| 333 | if ((json["frus"][file].at(0)).find("postActionFail") == |
| 334 | json["frus"][file].at(0).end()) |
| 335 | { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | uint8_t pinValue = 0; |
| 340 | string pinName; |
| 341 | |
| 342 | for (const auto& postAction : |
| 343 | (json["frus"][file].at(0))["postActionFail"].items()) |
| 344 | { |
| 345 | if (postAction.key() == "pin") |
| 346 | { |
| 347 | pinName = postAction.value(); |
| 348 | } |
| 349 | else if (postAction.key() == "value") |
| 350 | { |
| 351 | // Get the value to set |
| 352 | pinValue = postAction.value(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 357 | |
| 358 | try |
| 359 | { |
| 360 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 361 | |
| 362 | if (!outputLine) |
| 363 | { |
| 364 | cout << "Couldn't find output line:" << pinName |
| 365 | << " on GPIO. Skipping...\n"; |
| 366 | |
| 367 | return; |
| 368 | } |
| 369 | outputLine.request( |
| 370 | {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 371 | pinValue); |
| 372 | } |
| 373 | catch (system_error&) |
| 374 | { |
| 375 | cerr << "Failed to set post-action GPIO" << endl; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** Performs any pre-action needed to get the FRU setup for collection. |
| 380 | * |
| 381 | * @param[in] json - json object |
| 382 | * @param[in] file - eeprom file path |
| 383 | */ |
| 384 | static void preAction(const nlohmann::json& json, const string& file) |
| 385 | { |
| 386 | if ((json["frus"][file].at(0)).find("preAction") == |
| 387 | json["frus"][file].at(0).end()) |
| 388 | { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | uint8_t pinValue = 0; |
| 393 | string pinName; |
| 394 | |
| 395 | for (const auto& postAction : |
| 396 | (json["frus"][file].at(0))["preAction"].items()) |
| 397 | { |
| 398 | if (postAction.key() == "pin") |
| 399 | { |
| 400 | pinName = postAction.value(); |
| 401 | } |
| 402 | else if (postAction.key() == "value") |
| 403 | { |
| 404 | // Get the value to set |
| 405 | pinValue = postAction.value(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 410 | try |
| 411 | { |
| 412 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 413 | |
| 414 | if (!outputLine) |
| 415 | { |
| 416 | cout << "Couldn't find output line:" << pinName |
| 417 | << " on GPIO. Skipping...\n"; |
| 418 | |
| 419 | return; |
| 420 | } |
| 421 | outputLine.request( |
| 422 | {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 423 | pinValue); |
| 424 | } |
| 425 | catch (system_error&) |
| 426 | { |
| 427 | cerr << "Failed to set pre-action GPIO" << endl; |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | // Now bind the device |
| 432 | string bind = json["frus"][file].at(0).value("bind", ""); |
| 433 | cout << "Binding device " << bind << endl; |
| 434 | string bindCmd = string("echo \"") + bind + |
| 435 | string("\" > /sys/bus/i2c/drivers/at24/bind"); |
| 436 | cout << bindCmd << endl; |
| 437 | executeCmd(bindCmd); |
| 438 | |
| 439 | // Check if device showed up (test for file) |
| 440 | if (!fs::exists(file)) |
| 441 | { |
| 442 | cout << "EEPROM " << file << " does not exist. Take failure action" |
| 443 | << endl; |
| 444 | // If not, then take failure postAction |
| 445 | postFailAction(json, file); |
| 446 | } |
| 447 | } |
| 448 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 449 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 450 | * @brief Prime the Inventory |
| 451 | * Prime the inventory by populating only the location code, |
| 452 | * type interface and the inventory object for the frus |
| 453 | * which are not system vpd fru. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 454 | * |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 455 | * @param[in] jsObject - Reference to vpd inventory json object |
| 456 | * @param[in] vpdMap - Reference to the parsed vpd map |
| 457 | * |
| 458 | * @returns Map of items in extraInterface. |
| 459 | */ |
| 460 | template <typename T> |
| 461 | inventory::ObjectMap primeInventory(const nlohmann::json& jsObject, |
| 462 | const T& vpdMap) |
| 463 | { |
| 464 | inventory::ObjectMap objects; |
| 465 | |
| 466 | for (auto& itemFRUS : jsObject["frus"].items()) |
| 467 | { |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 468 | // Take pre actions |
| 469 | preAction(jsObject, itemFRUS.key()); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 470 | for (auto& itemEEPROM : itemFRUS.value()) |
| 471 | { |
| 472 | inventory::InterfaceMap interfaces; |
| 473 | auto isSystemVpd = itemEEPROM.value("isSystemVpd", false); |
| 474 | inventory::Object object(itemEEPROM.at("inventoryPath")); |
| 475 | |
| 476 | if (!isSystemVpd && !itemEEPROM.value("noprime", false)) |
| 477 | { |
Alpana Kumari | cfd7a75 | 2021-02-07 23:23:01 -0600 | [diff] [blame] | 478 | inventory::PropertyMap presProp; |
| 479 | presProp.emplace("Present", false); |
| 480 | interfaces.emplace("xyz.openbmc_project.Inventory.Item", |
| 481 | move(presProp)); |
| 482 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 483 | if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end()) |
| 484 | { |
| 485 | for (const auto& eI : itemEEPROM["extraInterfaces"].items()) |
| 486 | { |
| 487 | inventory::PropertyMap props; |
| 488 | if (eI.key() == |
| 489 | openpower::vpd::constants::LOCATION_CODE_INF) |
| 490 | { |
| 491 | if constexpr (std::is_same<T, Parsed>::value) |
| 492 | { |
| 493 | for (auto& lC : eI.value().items()) |
| 494 | { |
| 495 | auto propVal = expandLocationCode( |
| 496 | lC.value().get<string>(), vpdMap, true); |
| 497 | |
| 498 | props.emplace(move(lC.key()), |
| 499 | move(propVal)); |
| 500 | interfaces.emplace(move(eI.key()), |
| 501 | move(props)); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | else if (eI.key().find("Inventory.Item.") != |
| 506 | string::npos) |
| 507 | { |
| 508 | interfaces.emplace(move(eI.key()), move(props)); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | objects.emplace(move(object), move(interfaces)); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | return objects; |
| 517 | } |
| 518 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 519 | /** |
| 520 | * @brief This API executes command to set environment variable |
| 521 | * And then reboot the system |
| 522 | * @param[in] key -env key to set new value |
| 523 | * @param[in] value -value to set. |
| 524 | */ |
| 525 | void setEnvAndReboot(const string& key, const string& value) |
| 526 | { |
| 527 | // set env and reboot and break. |
| 528 | executeCmd("/sbin/fw_setenv", key, value); |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 529 | log<level::INFO>("Rebooting BMC to pick up new device tree"); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 530 | // make dbus call to reboot |
| 531 | auto bus = sdbusplus::bus::new_default_system(); |
| 532 | auto method = bus.new_method_call( |
| 533 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 534 | "org.freedesktop.systemd1.Manager", "Reboot"); |
| 535 | bus.call_noreply(method); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * @brief This API checks for env var fitconfig. |
| 540 | * If not initialised OR updated as per the current system type, |
| 541 | * update this env var and reboot the system. |
| 542 | * |
| 543 | * @param[in] systemType IM kwd in vpd tells about which system type it is. |
| 544 | * */ |
| 545 | void setDevTreeEnv(const string& systemType) |
| 546 | { |
| 547 | string newDeviceTree; |
| 548 | |
| 549 | if (deviceTreeSystemTypeMap.find(systemType) != |
| 550 | deviceTreeSystemTypeMap.end()) |
| 551 | { |
| 552 | newDeviceTree = deviceTreeSystemTypeMap.at(systemType); |
| 553 | } |
| 554 | |
| 555 | string readVarValue; |
| 556 | bool envVarFound = false; |
| 557 | |
| 558 | vector<string> output = executeCmd("/sbin/fw_printenv"); |
| 559 | for (const auto& entry : output) |
| 560 | { |
| 561 | size_t pos = entry.find("="); |
| 562 | string key = entry.substr(0, pos); |
| 563 | if (key != "fitconfig") |
| 564 | { |
| 565 | continue; |
| 566 | } |
| 567 | |
| 568 | envVarFound = true; |
| 569 | if (pos + 1 < entry.size()) |
| 570 | { |
| 571 | readVarValue = entry.substr(pos + 1); |
| 572 | if (readVarValue.find(newDeviceTree) != string::npos) |
| 573 | { |
| 574 | // fitconfig is Updated. No action needed |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | // set env and reboot and break. |
| 579 | setEnvAndReboot(key, newDeviceTree); |
| 580 | exit(0); |
| 581 | } |
| 582 | |
| 583 | // check If env var Not found |
| 584 | if (!envVarFound) |
| 585 | { |
| 586 | setEnvAndReboot("fitconfig", newDeviceTree); |
| 587 | } |
| 588 | } |
| 589 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 590 | /** |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 591 | * @brief API to call VPD manager to write VPD to EEPROM. |
| 592 | * @param[in] Object path. |
| 593 | * @param[in] record to be updated. |
| 594 | * @param[in] keyword to be updated. |
| 595 | * @param[in] keyword data to be updated |
| 596 | */ |
| 597 | void updateHardware(const string& objectName, const string& recName, |
| 598 | const string& kwdName, const Binary& data) |
| 599 | { |
| 600 | try |
| 601 | { |
| 602 | auto bus = sdbusplus::bus::new_default(); |
| 603 | auto properties = |
| 604 | bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword"); |
| 605 | properties.append( |
| 606 | static_cast<sdbusplus::message::object_path>(objectName)); |
| 607 | properties.append(recName); |
| 608 | properties.append(kwdName); |
| 609 | properties.append(data); |
| 610 | bus.call(properties); |
| 611 | } |
| 612 | catch (const sdbusplus::exception::SdBusError& e) |
| 613 | { |
| 614 | std::string what = |
| 615 | "VPDManager WriteKeyword api failed for inventory path " + |
| 616 | objectName; |
| 617 | what += " record " + recName; |
| 618 | what += " keyword " + kwdName; |
| 619 | what += " with bus error = " + std::string(e.what()); |
| 620 | |
| 621 | // map to hold additional data in case of logging pel |
| 622 | PelAdditionalData additionalData{}; |
| 623 | additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName); |
| 624 | additionalData.emplace("DESCRIPTION", what); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 625 | createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * @brief API to check if we need to restore system VPD |
| 631 | * This functionality is only applicable for IPZ VPD data. |
| 632 | * @param[in] vpdMap - IPZ vpd map |
| 633 | * @param[in] objectPath - Object path for the FRU |
| 634 | * @return EEPROMs with records and keywords updated at standby |
| 635 | */ |
| 636 | std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap, |
| 637 | const string& objectPath) |
| 638 | { |
| 639 | // the list of keywords for VSYS record is as per the S0 system. Should be |
| 640 | // updated for another type of systems |
| 641 | static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{ |
| 642 | {"VSYS", {"BR", "TM", "SE", "SU", "RB"}}, |
| 643 | {"VCEN", {"FC", "SE"}}, |
| 644 | {"LXR0", {"LX"}}}; |
| 645 | |
| 646 | // vector to hold all the EEPROMs updated at standby |
| 647 | std::vector<RestoredEeproms> updatedEeproms = {}; |
| 648 | |
| 649 | for (const auto& systemRecKwdPair : svpdKwdMap) |
| 650 | { |
| 651 | auto it = vpdMap.find(systemRecKwdPair.first); |
| 652 | |
| 653 | // check if record is found in map we got by parser |
| 654 | if (it != vpdMap.end()) |
| 655 | { |
| 656 | const auto& kwdListForRecord = systemRecKwdPair.second; |
| 657 | for (const auto& keyword : kwdListForRecord) |
| 658 | { |
| 659 | DbusPropertyMap& kwdValMap = it->second; |
| 660 | auto iterator = kwdValMap.find(keyword); |
| 661 | |
| 662 | if (iterator != kwdValMap.end()) |
| 663 | { |
| 664 | string& kwdValue = iterator->second; |
| 665 | |
| 666 | // check bus data |
| 667 | const string& recordName = systemRecKwdPair.first; |
| 668 | const string& busValue = readBusProperty( |
| 669 | objectPath, ipzVpdInf + recordName, keyword); |
| 670 | |
| 671 | if (busValue.find_first_not_of(' ') != string::npos) |
| 672 | { |
| 673 | if (kwdValue.find_first_not_of(' ') != string::npos) |
| 674 | { |
| 675 | // both the data are present, check for mismatch |
| 676 | if (busValue != kwdValue) |
| 677 | { |
| 678 | string errMsg = "VPD data mismatch on cache " |
| 679 | "and hardware for record: "; |
| 680 | errMsg += (*it).first; |
| 681 | errMsg += " and keyword: "; |
| 682 | errMsg += keyword; |
| 683 | |
| 684 | // data mismatch |
| 685 | PelAdditionalData additionalData; |
| 686 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 687 | objectPath); |
| 688 | |
| 689 | additionalData.emplace("DESCRIPTION", errMsg); |
| 690 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 691 | createPEL(additionalData, PelSeverity::WARNING, |
| 692 | errIntfForInvalidVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | else |
| 696 | { |
| 697 | // implies hardware data is blank |
| 698 | // update the map |
| 699 | Binary busData(busValue.begin(), busValue.end()); |
| 700 | |
| 701 | updatedEeproms.push_back(std::make_tuple( |
| 702 | objectPath, recordName, keyword, busData)); |
| 703 | } |
| 704 | |
| 705 | // update the map as well, so that cache data is not |
| 706 | // updated as blank while populating VPD map on Dbus in |
| 707 | // populateDBus Api |
| 708 | kwdValue = busValue; |
| 709 | continue; |
| 710 | } |
| 711 | else if (kwdValue.find_first_not_of(' ') == string::npos) |
| 712 | { |
| 713 | string errMsg = "VPD is blank on both cache and " |
| 714 | "hardware for record: "; |
| 715 | errMsg += (*it).first; |
| 716 | errMsg += " and keyword: "; |
| 717 | errMsg += keyword; |
| 718 | errMsg += ". SSR need to update hardware VPD."; |
| 719 | |
| 720 | // both the data are blanks, log PEL |
| 721 | PelAdditionalData additionalData; |
| 722 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 723 | objectPath); |
| 724 | |
| 725 | additionalData.emplace("DESCRIPTION", errMsg); |
| 726 | |
| 727 | // log PEL TODO: Block IPL |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 728 | createPEL(additionalData, PelSeverity::ERROR, |
| 729 | errIntfForBlankSystemVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 730 | continue; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return updatedEeproms; |
| 738 | } |
| 739 | |
| 740 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 741 | * @brief Populate Dbus. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 742 | * This method invokes all the populateInterface functions |
| 743 | * and notifies PIM about dbus object. |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 744 | * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the |
| 745 | * input. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 746 | * @param[in] js - Inventory json object |
| 747 | * @param[in] filePath - Path of the vpd file |
| 748 | * @param[in] preIntrStr - Interface string |
| 749 | */ |
| 750 | template <typename T> |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 751 | static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 752 | { |
| 753 | inventory::InterfaceMap interfaces; |
| 754 | inventory::ObjectMap objects; |
| 755 | inventory::PropertyMap prop; |
| 756 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 757 | // map to hold all the keywords whose value has been changed at standby |
| 758 | vector<RestoredEeproms> updatedEeproms = {}; |
| 759 | |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 760 | bool isSystemVpd = false; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 761 | for (const auto& item : js["frus"][filePath]) |
| 762 | { |
| 763 | const auto& objectPath = item["inventoryPath"]; |
| 764 | sdbusplus::message::object_path object(objectPath); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 765 | isSystemVpd = item.value("isSystemVpd", false); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 766 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 767 | // Populate the VPD keywords and the common interfaces only if we |
| 768 | // are asked to inherit that data from the VPD, else only add the |
| 769 | // extraInterfaces. |
| 770 | if (item.value("inherit", true)) |
| 771 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 772 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 773 | { |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 774 | if (isSystemVpd) |
| 775 | { |
| 776 | std::vector<std::string> interfaces = { |
| 777 | motherBoardInterface}; |
| 778 | // call mapper to check for object path creation |
| 779 | MapperResponse subTree = |
| 780 | getObjectSubtreeForInterfaces(pimPath, 0, interfaces); |
| 781 | |
| 782 | // Skip system vpd restore if object path is not generated |
| 783 | // for motherboard, Implies first boot. |
| 784 | if (subTree.size() != 0) |
| 785 | { |
| 786 | assert( |
| 787 | (subTree.find(pimPath + std::string(objectPath)) != |
| 788 | subTree.end())); |
| 789 | |
| 790 | updatedEeproms = restoreSystemVPD(vpdMap, objectPath); |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | log<level::ERR>("No object path found"); |
| 795 | } |
| 796 | } |
| 797 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 798 | // Each record in the VPD becomes an interface and all |
| 799 | // keyword within the record are properties under that |
| 800 | // interface. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 801 | for (const auto& record : vpdMap) |
| 802 | { |
| 803 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 804 | record.second, ipzVpdInf + record.first, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 805 | } |
| 806 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 807 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 808 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 809 | populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 810 | } |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 811 | if (js.find("commonInterfaces") != js.end()) |
| 812 | { |
| 813 | populateInterfaces(js["commonInterfaces"], interfaces, vpdMap, |
| 814 | isSystemVpd); |
| 815 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 816 | } |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 817 | else |
| 818 | { |
| 819 | // Check if we have been asked to inherit specific record(s) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 820 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 821 | { |
| 822 | if (item.find("copyRecords") != item.end()) |
| 823 | { |
| 824 | for (const auto& record : item["copyRecords"]) |
| 825 | { |
| 826 | const string& recordName = record; |
| 827 | if (vpdMap.find(recordName) != vpdMap.end()) |
| 828 | { |
| 829 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 830 | vpdMap.at(recordName), ipzVpdInf + recordName, |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 831 | interfaces); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 837 | if (item.value("inheritEI", true)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 838 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 839 | // Populate interfaces and properties that are common to every FRU |
| 840 | // and additional interface that might be defined on a per-FRU |
| 841 | // basis. |
| 842 | if (item.find("extraInterfaces") != item.end()) |
| 843 | { |
| 844 | populateInterfaces(item["extraInterfaces"], interfaces, vpdMap, |
| 845 | isSystemVpd); |
| 846 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 847 | } |
| 848 | objects.emplace(move(object), move(interfaces)); |
| 849 | } |
| 850 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 851 | if (isSystemVpd) |
| 852 | { |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 853 | vector<uint8_t> imVal; |
| 854 | if constexpr (is_same<T, Parsed>::value) |
| 855 | { |
| 856 | auto property = vpdMap.find("VSBP"); |
| 857 | if (property != vpdMap.end()) |
| 858 | { |
| 859 | auto value = (property->second).find("IM"); |
| 860 | if (value != (property->second).end()) |
| 861 | { |
| 862 | copy(value->second.begin(), value->second.end(), |
| 863 | back_inserter(imVal)); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | fs::path target; |
| 869 | fs::path link = INVENTORY_JSON_SYM_LINK; |
| 870 | |
| 871 | ostringstream oss; |
| 872 | for (auto& i : imVal) |
| 873 | { |
| 874 | oss << setw(2) << setfill('0') << hex << static_cast<int>(i); |
| 875 | } |
| 876 | string imValStr = oss.str(); |
| 877 | |
Andrew Geissler | 7c9cddf | 2021-03-19 10:34:31 -0500 | [diff] [blame] | 878 | if ((imValStr == RAINIER_4U) || // 4U |
| 879 | (imValStr == RAINIER_1S4U)) |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 880 | { |
| 881 | target = INVENTORY_JSON_4U; |
| 882 | } |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 883 | else if (imValStr == RAINIER_2U) // 2U |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 884 | { |
| 885 | target = INVENTORY_JSON_2U; |
| 886 | } |
Santosh Puranik | 4641bff | 2020-11-30 20:26:44 +0530 | [diff] [blame] | 887 | else if (imValStr == EVEREST) |
| 888 | { |
| 889 | target = INVENTORY_JSON_EVEREST; |
| 890 | } |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 891 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 892 | // Create the directory for hosting the symlink |
| 893 | fs::create_directories(VPD_FILES_PATH); |
| 894 | // unlink the symlink previously created (if any) |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 895 | remove(INVENTORY_JSON_SYM_LINK); |
| 896 | // create a new symlink based on the system |
| 897 | fs::create_symlink(target, link); |
| 898 | |
| 899 | // Reloading the json |
| 900 | ifstream inventoryJson(link); |
| 901 | auto js = json::parse(inventoryJson); |
| 902 | inventoryJson.close(); |
| 903 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 904 | inventory::ObjectMap primeObject = primeInventory(js, vpdMap); |
| 905 | objects.insert(primeObject.begin(), primeObject.end()); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 906 | |
| 907 | // set the U-boot environment variable for device-tree |
| 908 | setDevTreeEnv(imValStr); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 909 | |
| 910 | // if system VPD has been restored at standby, update the EEPROM |
| 911 | for (const auto& item : updatedEeproms) |
| 912 | { |
| 913 | updateHardware(get<0>(item), get<1>(item), get<2>(item), |
| 914 | get<3>(item)); |
| 915 | } |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 916 | } |
| 917 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 918 | // Notify PIM |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 919 | common::utility::callPIM(move(objects)); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | int main(int argc, char** argv) |
| 923 | { |
| 924 | int rc = 0; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 925 | string file{}; |
| 926 | json js{}; |
| 927 | |
| 928 | // map to hold additional data in case of logging pel |
| 929 | PelAdditionalData additionalData{}; |
| 930 | |
| 931 | // this is needed to hold base fru inventory path in case there is ECC or |
| 932 | // vpd exception while parsing the file |
| 933 | std::string baseFruInventoryPath = {}; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 934 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 935 | // severity for PEL |
| 936 | PelSeverity pelSeverity = PelSeverity::WARNING; |
| 937 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 938 | try |
| 939 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 940 | App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store " |
| 941 | "in DBUS"}; |
| 942 | string file{}; |
| 943 | |
| 944 | app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)") |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 945 | ->required(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 946 | |
| 947 | CLI11_PARSE(app, argc, argv); |
| 948 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 949 | // PEL severity should be ERROR in case of any system VPD failure |
| 950 | if (file == systemVpdFilePath) |
| 951 | { |
| 952 | pelSeverity = PelSeverity::ERROR; |
| 953 | } |
| 954 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 955 | auto jsonToParse = INVENTORY_JSON_DEFAULT; |
| 956 | |
| 957 | // If the symlink exists, it means it has been setup for us, switch the |
| 958 | // path |
| 959 | if (fs::exists(INVENTORY_JSON_SYM_LINK)) |
| 960 | { |
| 961 | jsonToParse = INVENTORY_JSON_SYM_LINK; |
| 962 | } |
| 963 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 964 | // 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] | 965 | ifstream inventoryJson(jsonToParse); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 966 | if (!inventoryJson) |
| 967 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 968 | throw(VpdJsonException("Failed to access Json path", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | try |
| 972 | { |
| 973 | js = json::parse(inventoryJson); |
| 974 | } |
| 975 | catch (json::parse_error& ex) |
| 976 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 977 | throw(VpdJsonException("Json parsing failed", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 978 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 979 | |
| 980 | if ((js.find("frus") == js.end()) || |
| 981 | (js["frus"].find(file) == js["frus"].end())) |
| 982 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 983 | cout << "Device path not in JSON, ignoring" << endl; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 984 | return 0; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 985 | } |
| 986 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 987 | if (!fs::exists(file)) |
| 988 | { |
| 989 | cout << "Device path: " << file |
| 990 | << " does not exist. Spurious udev event? Exiting." << endl; |
| 991 | return 0; |
| 992 | } |
| 993 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 994 | baseFruInventoryPath = js["frus"][file][0]["inventoryPath"]; |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 995 | // Check if we can read the VPD file based on the power state |
| 996 | if (js["frus"][file].at(0).value("powerOffOnly", false)) |
| 997 | { |
| 998 | if ("xyz.openbmc_project.State.Chassis.PowerState.On" == |
| 999 | getPowerState()) |
| 1000 | { |
| 1001 | cout << "This VPD cannot be read when power is ON" << endl; |
| 1002 | return 0; |
| 1003 | } |
| 1004 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1005 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1006 | try |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1007 | { |
Alpana Kumari | 80f1534 | 2020-06-09 07:41:02 -0500 | [diff] [blame] | 1008 | // check if vpd file is empty |
| 1009 | if (file.empty()) |
| 1010 | { |
| 1011 | throw(VpdDataException( |
| 1012 | "VPD file is empty. Can't process with blank file.")); |
| 1013 | } |
| 1014 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1015 | Binary vpdVector = getVpdDataInVector(js, file); |
| 1016 | ParserInterface* parser = ParserFactory::getParser(move(vpdVector)); |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 1017 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1018 | variant<KeywordVpdMap, Store> parseResult; |
| 1019 | parseResult = parser->parse(); |
| 1020 | if (auto pVal = get_if<Store>(&parseResult)) |
| 1021 | { |
| 1022 | populateDbus(pVal->getVpdMap(), js, file); |
| 1023 | } |
| 1024 | else if (auto pVal = get_if<KeywordVpdMap>(&parseResult)) |
| 1025 | { |
| 1026 | populateDbus(*pVal, js, file); |
| 1027 | } |
| 1028 | |
| 1029 | // release the parser object |
| 1030 | ParserFactory::freeParser(parser); |
| 1031 | } |
| 1032 | catch (exception& e) |
| 1033 | { |
| 1034 | postFailAction(js, file); |
| 1035 | throw e; |
| 1036 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1037 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1038 | catch (const VpdJsonException& ex) |
| 1039 | { |
| 1040 | additionalData.emplace("JSON_PATH", ex.getJsonPath()); |
| 1041 | additionalData.emplace("DESCRIPTION", ex.what()); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 1042 | createPEL(additionalData, pelSeverity, errIntfForJsonFailure); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1043 | |
| 1044 | cerr << ex.what() << "\n"; |
| 1045 | rc = -1; |
| 1046 | } |
| 1047 | catch (const VpdEccException& ex) |
| 1048 | { |
| 1049 | additionalData.emplace("DESCRIPTION", "ECC check failed"); |
| 1050 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1051 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 1052 | createPEL(additionalData, pelSeverity, errIntfForEccCheckFail); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1053 | |
| 1054 | cerr << ex.what() << "\n"; |
| 1055 | rc = -1; |
| 1056 | } |
| 1057 | catch (const VpdDataException& ex) |
| 1058 | { |
| 1059 | additionalData.emplace("DESCRIPTION", "Invalid VPD data"); |
| 1060 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1061 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame^] | 1062 | createPEL(additionalData, pelSeverity, errIntfForInvalidVPD); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1063 | |
| 1064 | cerr << ex.what() << "\n"; |
| 1065 | rc = -1; |
| 1066 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1067 | catch (exception& e) |
| 1068 | { |
| 1069 | cerr << e.what() << "\n"; |
| 1070 | rc = -1; |
| 1071 | } |
| 1072 | |
| 1073 | return rc; |
Alpana Kumari | 80f1534 | 2020-06-09 07:41:02 -0500 | [diff] [blame] | 1074 | } |