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