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