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