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> |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 17 | #include <boost/algorithm/string.hpp> |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 18 | #include <cstdarg> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 19 | #include <exception> |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 20 | #include <filesystem> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 21 | #include <fstream> |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 22 | #include <gpiod.hpp> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <iterator> |
| 25 | #include <nlohmann/json.hpp> |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 27 | #include <regex> |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 28 | |
| 29 | using namespace std; |
| 30 | using namespace openpower::vpd; |
| 31 | using namespace CLI; |
| 32 | using namespace vpd::keyword::parser; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 33 | using namespace openpower::vpd::constants; |
| 34 | namespace fs = filesystem; |
| 35 | using json = nlohmann::json; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 36 | using namespace openpower::vpd::parser::factory; |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 37 | using namespace openpower::vpd::inventory; |
Alpana Kumari | a00936f | 2020-04-14 07:15:46 -0500 | [diff] [blame] | 38 | using namespace openpower::vpd::memory::parser; |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 39 | using namespace openpower::vpd::parser::interface; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 40 | using namespace openpower::vpd::exceptions; |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 41 | using namespace phosphor::logging; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 42 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 43 | static const deviceTreeMap deviceTreeSystemTypeMap = { |
Joel Stanley | 6fb0ef9 | 2021-05-26 13:14:32 +0930 | [diff] [blame] | 44 | {RAINIER_2U, "conf-aspeed-bmc-ibm-rainier-p1.dtb"}, |
| 45 | {RAINIER_2U_V2, "conf-aspeed-bmc-ibm-rainier.dtb"}, |
| 46 | {RAINIER_4U, "conf-aspeed-bmc-ibm-rainier-4u-p1.dtb"}, |
| 47 | {RAINIER_4U_V2, "conf-aspeed-bmc-ibm-rainier-4u.dtb"}, |
Andrew Geissler | 2fe709f | 2021-03-25 10:59:07 -0500 | [diff] [blame] | 48 | {RAINIER_1S4U, "conf-aspeed-bmc-ibm-rainier-1s4u.dtb"}, |
| 49 | {EVEREST, "conf-aspeed-bmc-ibm-everest.dtb"}}; |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 50 | |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 51 | /** |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 52 | * @brief Returns the power state for chassis0 |
| 53 | */ |
| 54 | static auto getPowerState() |
| 55 | { |
| 56 | // TODO: How do we handle multiple chassis? |
| 57 | string powerState{}; |
| 58 | auto bus = sdbusplus::bus::new_default(); |
| 59 | auto properties = |
| 60 | bus.new_method_call("xyz.openbmc_project.State.Chassis", |
| 61 | "/xyz/openbmc_project/state/chassis0", |
| 62 | "org.freedesktop.DBus.Properties", "Get"); |
| 63 | properties.append("xyz.openbmc_project.State.Chassis"); |
| 64 | properties.append("CurrentPowerState"); |
| 65 | auto result = bus.call(properties); |
| 66 | if (!result.is_method_error()) |
| 67 | { |
| 68 | variant<string> val; |
| 69 | result.read(val); |
| 70 | if (auto pVal = get_if<string>(&val)) |
| 71 | { |
| 72 | powerState = *pVal; |
| 73 | } |
| 74 | } |
| 75 | cout << "Power state is: " << powerState << endl; |
| 76 | return powerState; |
| 77 | } |
| 78 | |
| 79 | /** |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 80 | * @brief Expands location codes |
| 81 | */ |
| 82 | static auto expandLocationCode(const string& unexpanded, const Parsed& vpdMap, |
| 83 | bool isSystemVpd) |
| 84 | { |
| 85 | auto expanded{unexpanded}; |
| 86 | static constexpr auto SYSTEM_OBJECT = "/system/chassis/motherboard"; |
| 87 | static constexpr auto VCEN_IF = "com.ibm.ipzvpd.VCEN"; |
| 88 | static constexpr auto VSYS_IF = "com.ibm.ipzvpd.VSYS"; |
| 89 | size_t idx = expanded.find("fcs"); |
| 90 | try |
| 91 | { |
| 92 | if (idx != string::npos) |
| 93 | { |
| 94 | string fc{}; |
| 95 | string se{}; |
| 96 | if (isSystemVpd) |
| 97 | { |
| 98 | const auto& fcData = vpdMap.at("VCEN").at("FC"); |
| 99 | const auto& seData = vpdMap.at("VCEN").at("SE"); |
| 100 | fc = string(fcData.data(), fcData.size()); |
| 101 | se = string(seData.data(), seData.size()); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | fc = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "FC"); |
| 106 | se = readBusProperty(SYSTEM_OBJECT, VCEN_IF, "SE"); |
| 107 | } |
| 108 | |
Alpana Kumari | 81671f6 | 2021-02-10 02:21:59 -0600 | [diff] [blame] | 109 | // TODO: See if ND0 can be placed in the JSON |
| 110 | expanded.replace(idx, 3, fc.substr(0, 4) + ".ND0." + se); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | idx = expanded.find("mts"); |
| 115 | if (idx != string::npos) |
| 116 | { |
| 117 | string mt{}; |
| 118 | string se{}; |
| 119 | if (isSystemVpd) |
| 120 | { |
| 121 | const auto& mtData = vpdMap.at("VSYS").at("TM"); |
| 122 | const auto& seData = vpdMap.at("VSYS").at("SE"); |
| 123 | mt = string(mtData.data(), mtData.size()); |
| 124 | se = string(seData.data(), seData.size()); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | mt = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "TM"); |
| 129 | se = readBusProperty(SYSTEM_OBJECT, VSYS_IF, "SE"); |
| 130 | } |
| 131 | |
| 132 | replace(mt.begin(), mt.end(), '-', '.'); |
| 133 | expanded.replace(idx, 3, mt + "." + se); |
| 134 | } |
| 135 | } |
| 136 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 137 | catch (const exception& e) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 138 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 139 | cerr << "Failed to expand location code with exception: " << e.what() |
| 140 | << "\n"; |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 141 | } |
| 142 | return expanded; |
| 143 | } |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 144 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 145 | /** |
| 146 | * @brief Populate FRU specific interfaces. |
| 147 | * |
| 148 | * This is a common method which handles both |
| 149 | * ipz and keyword specific interfaces thus, |
| 150 | * reducing the code redundancy. |
| 151 | * @param[in] map - Reference to the innermost keyword-value map. |
| 152 | * @param[in] preIntrStr - Reference to the interface string. |
| 153 | * @param[out] interfaces - Reference to interface map. |
| 154 | */ |
| 155 | template <typename T> |
| 156 | static void populateFruSpecificInterfaces(const T& map, |
| 157 | const string& preIntrStr, |
| 158 | inventory::InterfaceMap& interfaces) |
| 159 | { |
| 160 | inventory::PropertyMap prop; |
| 161 | |
| 162 | for (const auto& kwVal : map) |
| 163 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 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 | } |
Alpana Kumari | 3ab26a7 | 2021-04-05 19:09:19 +0000 | [diff] [blame] | 174 | if constexpr (is_same<T, KeywordVpdMap>::value) |
| 175 | { |
| 176 | if (get_if<Binary>(&kwVal.second)) |
| 177 | { |
| 178 | Binary vec(get_if<Binary>(&kwVal.second)->begin(), |
| 179 | get_if<Binary>(&kwVal.second)->end()); |
Alpana Kumari | 3ab26a7 | 2021-04-05 19:09:19 +0000 | [diff] [blame] | 180 | prop.emplace(move(kw), move(vec)); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | if (kw == "MemorySizeInKB") |
| 185 | { |
| 186 | inventory::PropertyMap memProp; |
| 187 | auto memVal = get_if<size_t>(&kwVal.second); |
| 188 | if (memVal) |
| 189 | { |
| 190 | memProp.emplace(move(kw), |
| 191 | ((*memVal) * CONVERT_MB_TO_KB)); |
| 192 | interfaces.emplace( |
| 193 | "xyz.openbmc_project.Inventory.Item.Dimm", |
| 194 | move(memProp)); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | cerr << "MemorySizeInKB value not found in vpd map\n"; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | Binary vec(kwVal.second.begin(), kwVal.second.end()); |
| 206 | prop.emplace(move(kw), move(vec)); |
| 207 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | interfaces.emplace(preIntrStr, move(prop)); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @brief Populate Interfaces. |
| 215 | * |
| 216 | * This method populates common and extra interfaces to dbus. |
| 217 | * @param[in] js - json object |
| 218 | * @param[out] interfaces - Reference to interface map |
| 219 | * @param[in] vpdMap - Reference to the parsed vpd map. |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 220 | * @param[in] isSystemVpd - Denotes whether we are collecting the system VPD. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 221 | */ |
| 222 | template <typename T> |
| 223 | static void populateInterfaces(const nlohmann::json& js, |
| 224 | inventory::InterfaceMap& interfaces, |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 225 | const T& vpdMap, bool isSystemVpd) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 226 | { |
| 227 | for (const auto& ifs : js.items()) |
| 228 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 229 | string inf = ifs.key(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 230 | inventory::PropertyMap props; |
| 231 | |
| 232 | for (const auto& itr : ifs.value().items()) |
| 233 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 234 | const string& busProp = itr.key(); |
| 235 | |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 236 | if (itr.value().is_boolean()) |
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 | props.emplace(busProp, itr.value().get<bool>()); |
| 239 | } |
| 240 | else if (itr.value().is_string()) |
| 241 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 242 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 243 | { |
| 244 | if (busProp == "LocationCode" && |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 245 | inf == IBM_LOCATION_CODE_INF) |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 246 | { |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 247 | // TODO deprecate the com.ibm interface later |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 248 | auto prop = expandLocationCode( |
| 249 | itr.value().get<string>(), vpdMap, isSystemVpd); |
| 250 | props.emplace(busProp, prop); |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 251 | interfaces.emplace(XYZ_LOCATION_CODE_INF, props); |
Priyanga Ramasamy | 69f7602 | 2022-01-05 07:10:36 +0000 | [diff] [blame^] | 252 | interfaces.emplace(IBM_LOCATION_CODE_INF, props); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | { |
| 256 | props.emplace(busProp, itr.value().get<string>()); |
| 257 | } |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | props.emplace(busProp, itr.value().get<string>()); |
| 262 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 263 | } |
Santosh Puranik | ed609af | 2021-06-21 11:30:07 +0530 | [diff] [blame] | 264 | else if (itr.value().is_array()) |
| 265 | { |
| 266 | try |
| 267 | { |
| 268 | props.emplace(busProp, itr.value().get<Binary>()); |
| 269 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 270 | catch (const nlohmann::detail::type_error& e) |
Santosh Puranik | ed609af | 2021-06-21 11:30:07 +0530 | [diff] [blame] | 271 | { |
| 272 | std::cerr << "Type exception: " << e.what() << "\n"; |
| 273 | // Ignore any type errors |
| 274 | } |
| 275 | } |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 276 | else if (itr.value().is_object()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 277 | { |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 278 | const string& rec = itr.value().value("recordName", ""); |
| 279 | const string& kw = itr.value().value("keywordName", ""); |
| 280 | const string& encoding = itr.value().value("encoding", ""); |
| 281 | |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 282 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 283 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 284 | if (!rec.empty() && !kw.empty() && vpdMap.count(rec) && |
| 285 | vpdMap.at(rec).count(kw)) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 286 | { |
| 287 | auto encoded = |
| 288 | encodeKeyword(vpdMap.at(rec).at(kw), encoding); |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 289 | props.emplace(busProp, encoded); |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 290 | } |
| 291 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 292 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 293 | { |
| 294 | if (!kw.empty() && vpdMap.count(kw)) |
| 295 | { |
Alpana Kumari | 3ab26a7 | 2021-04-05 19:09:19 +0000 | [diff] [blame] | 296 | auto kwValue = get_if<Binary>(&vpdMap.at(kw)); |
| 297 | auto uintValue = get_if<size_t>(&vpdMap.at(kw)); |
| 298 | |
| 299 | if (kwValue) |
| 300 | { |
| 301 | auto prop = |
| 302 | string((*kwValue).begin(), (*kwValue).end()); |
| 303 | |
| 304 | auto encoded = encodeKeyword(prop, encoding); |
| 305 | |
| 306 | props.emplace(busProp, encoded); |
| 307 | } |
| 308 | else if (uintValue) |
| 309 | { |
| 310 | props.emplace(busProp, *uintValue); |
| 311 | } |
Alpana Kumari | 31970de | 2020-02-17 06:49:57 -0600 | [diff] [blame] | 312 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 313 | } |
| 314 | } |
Matt Spinler | b1e64bb | 2021-09-08 09:57:48 -0500 | [diff] [blame] | 315 | else if (itr.value().is_number()) |
| 316 | { |
| 317 | // For now assume the value is a size_t. In the future it would |
| 318 | // be nice to come up with a way to get the type from the JSON. |
| 319 | props.emplace(busProp, itr.value().get<size_t>()); |
| 320 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 321 | } |
| 322 | interfaces.emplace(inf, move(props)); |
| 323 | } |
| 324 | } |
| 325 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 326 | static Binary getVpdDataInVector(const nlohmann::json& js, const string& file) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 327 | { |
| 328 | uint32_t offset = 0; |
| 329 | // check if offset present? |
| 330 | for (const auto& item : js["frus"][file]) |
| 331 | { |
| 332 | if (item.find("offset") != item.end()) |
| 333 | { |
| 334 | offset = item["offset"]; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // TODO: Figure out a better way to get max possible VPD size. |
| 339 | Binary vpdVector; |
| 340 | vpdVector.resize(65504); |
| 341 | ifstream vpdFile; |
| 342 | vpdFile.open(file, ios::binary); |
| 343 | |
| 344 | vpdFile.seekg(offset, ios_base::cur); |
| 345 | vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), 65504); |
| 346 | vpdVector.resize(vpdFile.gcount()); |
| 347 | |
| 348 | return vpdVector; |
| 349 | } |
| 350 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 351 | /** This API will be called at the end of VPD collection to perform any post |
| 352 | * actions. |
| 353 | * |
| 354 | * @param[in] json - json object |
| 355 | * @param[in] file - eeprom file path |
| 356 | */ |
| 357 | static void postFailAction(const nlohmann::json& json, const string& file) |
| 358 | { |
| 359 | if ((json["frus"][file].at(0)).find("postActionFail") == |
| 360 | json["frus"][file].at(0).end()) |
| 361 | { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | uint8_t pinValue = 0; |
| 366 | string pinName; |
| 367 | |
| 368 | for (const auto& postAction : |
| 369 | (json["frus"][file].at(0))["postActionFail"].items()) |
| 370 | { |
| 371 | if (postAction.key() == "pin") |
| 372 | { |
| 373 | pinName = postAction.value(); |
| 374 | } |
| 375 | else if (postAction.key() == "value") |
| 376 | { |
| 377 | // Get the value to set |
| 378 | pinValue = postAction.value(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 383 | |
| 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 | {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 397 | pinValue); |
| 398 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 399 | catch (const system_error&) |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 400 | { |
| 401 | cerr << "Failed to set post-action GPIO" << endl; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** Performs any pre-action needed to get the FRU setup for collection. |
| 406 | * |
| 407 | * @param[in] json - json object |
| 408 | * @param[in] file - eeprom file path |
| 409 | */ |
| 410 | static void preAction(const nlohmann::json& json, const string& file) |
| 411 | { |
| 412 | if ((json["frus"][file].at(0)).find("preAction") == |
| 413 | json["frus"][file].at(0).end()) |
| 414 | { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | uint8_t pinValue = 0; |
| 419 | string pinName; |
| 420 | |
| 421 | for (const auto& postAction : |
| 422 | (json["frus"][file].at(0))["preAction"].items()) |
| 423 | { |
| 424 | if (postAction.key() == "pin") |
| 425 | { |
| 426 | pinName = postAction.value(); |
| 427 | } |
| 428 | else if (postAction.key() == "value") |
| 429 | { |
| 430 | // Get the value to set |
| 431 | pinValue = postAction.value(); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | cout << "Setting GPIO: " << pinName << " to " << (int)pinValue << endl; |
| 436 | try |
| 437 | { |
| 438 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 439 | |
| 440 | if (!outputLine) |
| 441 | { |
| 442 | cout << "Couldn't find output line:" << pinName |
| 443 | << " on GPIO. Skipping...\n"; |
| 444 | |
| 445 | return; |
| 446 | } |
| 447 | outputLine.request( |
| 448 | {"FRU pre-action", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 449 | pinValue); |
| 450 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 451 | catch (const system_error&) |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 452 | { |
| 453 | cerr << "Failed to set pre-action GPIO" << endl; |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | // Now bind the device |
Alpana Kumari | 4c3bf5b | 2021-09-16 07:24:58 -0500 | [diff] [blame] | 458 | string bind = json["frus"][file].at(0).value("devAddress", ""); |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 459 | cout << "Binding device " << bind << endl; |
| 460 | string bindCmd = string("echo \"") + bind + |
| 461 | string("\" > /sys/bus/i2c/drivers/at24/bind"); |
| 462 | cout << bindCmd << endl; |
| 463 | executeCmd(bindCmd); |
| 464 | |
| 465 | // Check if device showed up (test for file) |
| 466 | if (!fs::exists(file)) |
| 467 | { |
| 468 | cout << "EEPROM " << file << " does not exist. Take failure action" |
| 469 | << endl; |
| 470 | // If not, then take failure postAction |
| 471 | postFailAction(json, file); |
| 472 | } |
| 473 | } |
| 474 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 475 | /** |
Santosh Puranik | d3a379a | 2021-08-23 19:12:59 +0530 | [diff] [blame] | 476 | * @brief Set certain one time properties in the inventory |
| 477 | * Use this function to insert the Functional and Enabled properties into the |
| 478 | * inventory map. This function first checks if the object in question already |
| 479 | * has these properties hosted on D-Bus, if the property is already there, it is |
| 480 | * not modified, hence the name "one time". If the property is not already |
| 481 | * present, it will be added to the map with a suitable default value (true for |
| 482 | * Functional and false for Enabled) |
| 483 | * |
| 484 | * @param[in] object - The inventory D-Bus obejct without the inventory prefix. |
| 485 | * @param[inout] interfaces - Reference to a map of inventory interfaces to |
| 486 | * which the properties will be attached. |
| 487 | */ |
| 488 | static void setOneTimeProperties(const std::string& object, |
| 489 | inventory::InterfaceMap& interfaces) |
| 490 | { |
| 491 | auto bus = sdbusplus::bus::new_default(); |
| 492 | auto objectPath = INVENTORY_PATH + object; |
| 493 | auto prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager", |
| 494 | objectPath.c_str(), |
| 495 | "org.freedesktop.DBus.Properties", "Get"); |
| 496 | prop.append("xyz.openbmc_project.State.Decorator.OperationalStatus"); |
| 497 | prop.append("Functional"); |
| 498 | try |
| 499 | { |
| 500 | auto result = bus.call(prop); |
| 501 | } |
| 502 | catch (const sdbusplus::exception::SdBusError& e) |
| 503 | { |
| 504 | // Treat as property unavailable |
| 505 | inventory::PropertyMap prop; |
| 506 | prop.emplace("Functional", true); |
| 507 | interfaces.emplace( |
| 508 | "xyz.openbmc_project.State.Decorator.OperationalStatus", |
| 509 | move(prop)); |
| 510 | } |
| 511 | prop = bus.new_method_call("xyz.openbmc_project.Inventory.Manager", |
| 512 | objectPath.c_str(), |
| 513 | "org.freedesktop.DBus.Properties", "Get"); |
| 514 | prop.append("xyz.openbmc_project.Object.Enable"); |
| 515 | prop.append("Enabled"); |
| 516 | try |
| 517 | { |
| 518 | auto result = bus.call(prop); |
| 519 | } |
| 520 | catch (const sdbusplus::exception::SdBusError& e) |
| 521 | { |
| 522 | // Treat as property unavailable |
| 523 | inventory::PropertyMap prop; |
| 524 | prop.emplace("Enabled", false); |
| 525 | interfaces.emplace("xyz.openbmc_project.Object.Enable", move(prop)); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 530 | * @brief Prime the Inventory |
| 531 | * Prime the inventory by populating only the location code, |
| 532 | * type interface and the inventory object for the frus |
| 533 | * which are not system vpd fru. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 534 | * |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 535 | * @param[in] jsObject - Reference to vpd inventory json object |
| 536 | * @param[in] vpdMap - Reference to the parsed vpd map |
| 537 | * |
| 538 | * @returns Map of items in extraInterface. |
| 539 | */ |
| 540 | template <typename T> |
| 541 | inventory::ObjectMap primeInventory(const nlohmann::json& jsObject, |
| 542 | const T& vpdMap) |
| 543 | { |
| 544 | inventory::ObjectMap objects; |
| 545 | |
| 546 | for (auto& itemFRUS : jsObject["frus"].items()) |
| 547 | { |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 548 | // Take pre actions |
| 549 | preAction(jsObject, itemFRUS.key()); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 550 | for (auto& itemEEPROM : itemFRUS.value()) |
| 551 | { |
| 552 | inventory::InterfaceMap interfaces; |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 553 | inventory::Object object(itemEEPROM.at("inventoryPath")); |
| 554 | |
Santosh Puranik | 50f60bf | 2021-05-26 17:55:06 +0530 | [diff] [blame] | 555 | if ((itemFRUS.key() != systemVpdFilePath) && |
| 556 | !itemEEPROM.value("noprime", false)) |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 557 | { |
Alpana Kumari | cfd7a75 | 2021-02-07 23:23:01 -0600 | [diff] [blame] | 558 | inventory::PropertyMap presProp; |
| 559 | presProp.emplace("Present", false); |
| 560 | interfaces.emplace("xyz.openbmc_project.Inventory.Item", |
Santosh Puranik | d3a379a | 2021-08-23 19:12:59 +0530 | [diff] [blame] | 561 | presProp); |
| 562 | setOneTimeProperties(object, interfaces); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 563 | if (itemEEPROM.find("extraInterfaces") != itemEEPROM.end()) |
| 564 | { |
| 565 | for (const auto& eI : itemEEPROM["extraInterfaces"].items()) |
| 566 | { |
| 567 | inventory::PropertyMap props; |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 568 | if (eI.key() == IBM_LOCATION_CODE_INF) |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 569 | { |
| 570 | if constexpr (std::is_same<T, Parsed>::value) |
| 571 | { |
| 572 | for (auto& lC : eI.value().items()) |
| 573 | { |
| 574 | auto propVal = expandLocationCode( |
| 575 | lC.value().get<string>(), vpdMap, true); |
| 576 | |
| 577 | props.emplace(move(lC.key()), |
| 578 | move(propVal)); |
Santosh Puranik | b0f3749 | 2021-06-21 09:42:47 +0530 | [diff] [blame] | 579 | interfaces.emplace(XYZ_LOCATION_CODE_INF, |
| 580 | props); |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 581 | interfaces.emplace(move(eI.key()), |
| 582 | move(props)); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | else if (eI.key().find("Inventory.Item.") != |
| 587 | string::npos) |
| 588 | { |
| 589 | interfaces.emplace(move(eI.key()), move(props)); |
| 590 | } |
Santosh Puranik | d3a379a | 2021-08-23 19:12:59 +0530 | [diff] [blame] | 591 | else if (eI.key() == |
| 592 | "xyz.openbmc_project.Inventory.Item") |
| 593 | { |
| 594 | for (auto& val : eI.value().items()) |
| 595 | { |
| 596 | if (val.key() == "PrettyName") |
| 597 | { |
| 598 | presProp.emplace(val.key(), |
| 599 | val.value().get<string>()); |
| 600 | } |
| 601 | } |
| 602 | // Use insert_or_assign here as we may already have |
| 603 | // inserted the present property only earlier in |
| 604 | // this function under this same interface. |
| 605 | interfaces.insert_or_assign(eI.key(), |
| 606 | move(presProp)); |
| 607 | } |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | objects.emplace(move(object), move(interfaces)); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | return objects; |
| 615 | } |
| 616 | |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 617 | /** |
| 618 | * @brief This API executes command to set environment variable |
| 619 | * And then reboot the system |
| 620 | * @param[in] key -env key to set new value |
| 621 | * @param[in] value -value to set. |
| 622 | */ |
| 623 | void setEnvAndReboot(const string& key, const string& value) |
| 624 | { |
| 625 | // set env and reboot and break. |
| 626 | executeCmd("/sbin/fw_setenv", key, value); |
Andrew Geissler | 280197e | 2020-12-08 20:51:49 -0600 | [diff] [blame] | 627 | log<level::INFO>("Rebooting BMC to pick up new device tree"); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 628 | // make dbus call to reboot |
| 629 | auto bus = sdbusplus::bus::new_default_system(); |
| 630 | auto method = bus.new_method_call( |
| 631 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 632 | "org.freedesktop.systemd1.Manager", "Reboot"); |
| 633 | bus.call_noreply(method); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * @brief This API checks for env var fitconfig. |
| 638 | * If not initialised OR updated as per the current system type, |
| 639 | * update this env var and reboot the system. |
| 640 | * |
| 641 | * @param[in] systemType IM kwd in vpd tells about which system type it is. |
| 642 | * */ |
| 643 | void setDevTreeEnv(const string& systemType) |
| 644 | { |
Alpana Kumari | 37e7270 | 2021-11-18 11:18:04 -0600 | [diff] [blame] | 645 | // Init with default dtb |
| 646 | string newDeviceTree = "conf-aspeed-bmc-ibm-rainier-p1.dtb"; |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 647 | |
| 648 | if (deviceTreeSystemTypeMap.find(systemType) != |
| 649 | deviceTreeSystemTypeMap.end()) |
| 650 | { |
| 651 | newDeviceTree = deviceTreeSystemTypeMap.at(systemType); |
| 652 | } |
Alpana Kumari | 37e7270 | 2021-11-18 11:18:04 -0600 | [diff] [blame] | 653 | else |
| 654 | { |
| 655 | // System type not supported |
| 656 | cerr << "This System type not found/supported in dtb table " |
| 657 | << systemType << ". so system will be booted with default dtb.\n"; |
| 658 | } |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 659 | |
| 660 | string readVarValue; |
| 661 | bool envVarFound = false; |
| 662 | |
| 663 | vector<string> output = executeCmd("/sbin/fw_printenv"); |
| 664 | for (const auto& entry : output) |
| 665 | { |
| 666 | size_t pos = entry.find("="); |
| 667 | string key = entry.substr(0, pos); |
| 668 | if (key != "fitconfig") |
| 669 | { |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | envVarFound = true; |
| 674 | if (pos + 1 < entry.size()) |
| 675 | { |
| 676 | readVarValue = entry.substr(pos + 1); |
| 677 | if (readVarValue.find(newDeviceTree) != string::npos) |
| 678 | { |
| 679 | // fitconfig is Updated. No action needed |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | // set env and reboot and break. |
| 684 | setEnvAndReboot(key, newDeviceTree); |
| 685 | exit(0); |
| 686 | } |
| 687 | |
| 688 | // check If env var Not found |
| 689 | if (!envVarFound) |
| 690 | { |
| 691 | setEnvAndReboot("fitconfig", newDeviceTree); |
| 692 | } |
| 693 | } |
| 694 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 695 | /** |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 696 | * @brief API to call VPD manager to write VPD to EEPROM. |
| 697 | * @param[in] Object path. |
| 698 | * @param[in] record to be updated. |
| 699 | * @param[in] keyword to be updated. |
| 700 | * @param[in] keyword data to be updated |
| 701 | */ |
| 702 | void updateHardware(const string& objectName, const string& recName, |
| 703 | const string& kwdName, const Binary& data) |
| 704 | { |
| 705 | try |
| 706 | { |
| 707 | auto bus = sdbusplus::bus::new_default(); |
| 708 | auto properties = |
| 709 | bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword"); |
| 710 | properties.append( |
| 711 | static_cast<sdbusplus::message::object_path>(objectName)); |
| 712 | properties.append(recName); |
| 713 | properties.append(kwdName); |
| 714 | properties.append(data); |
| 715 | bus.call(properties); |
| 716 | } |
Patrick Williams | 8be4334 | 2021-09-02 09:33:36 -0500 | [diff] [blame] | 717 | catch (const sdbusplus::exception::exception& e) |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 718 | { |
| 719 | std::string what = |
| 720 | "VPDManager WriteKeyword api failed for inventory path " + |
| 721 | objectName; |
| 722 | what += " record " + recName; |
| 723 | what += " keyword " + kwdName; |
| 724 | what += " with bus error = " + std::string(e.what()); |
| 725 | |
| 726 | // map to hold additional data in case of logging pel |
| 727 | PelAdditionalData additionalData{}; |
| 728 | additionalData.emplace("CALLOUT_INVENTORY_PATH", objectName); |
| 729 | additionalData.emplace("DESCRIPTION", what); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 730 | createPEL(additionalData, PelSeverity::WARNING, errIntfForBusFailure); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @brief API to check if we need to restore system VPD |
| 736 | * This functionality is only applicable for IPZ VPD data. |
| 737 | * @param[in] vpdMap - IPZ vpd map |
| 738 | * @param[in] objectPath - Object path for the FRU |
| 739 | * @return EEPROMs with records and keywords updated at standby |
| 740 | */ |
| 741 | std::vector<RestoredEeproms> restoreSystemVPD(Parsed& vpdMap, |
| 742 | const string& objectPath) |
| 743 | { |
| 744 | // the list of keywords for VSYS record is as per the S0 system. Should be |
| 745 | // updated for another type of systems |
| 746 | static std::unordered_map<std::string, std::vector<std::string>> svpdKwdMap{ |
| 747 | {"VSYS", {"BR", "TM", "SE", "SU", "RB"}}, |
| 748 | {"VCEN", {"FC", "SE"}}, |
| 749 | {"LXR0", {"LX"}}}; |
| 750 | |
| 751 | // vector to hold all the EEPROMs updated at standby |
| 752 | std::vector<RestoredEeproms> updatedEeproms = {}; |
| 753 | |
| 754 | for (const auto& systemRecKwdPair : svpdKwdMap) |
| 755 | { |
| 756 | auto it = vpdMap.find(systemRecKwdPair.first); |
| 757 | |
| 758 | // check if record is found in map we got by parser |
| 759 | if (it != vpdMap.end()) |
| 760 | { |
| 761 | const auto& kwdListForRecord = systemRecKwdPair.second; |
| 762 | for (const auto& keyword : kwdListForRecord) |
| 763 | { |
| 764 | DbusPropertyMap& kwdValMap = it->second; |
| 765 | auto iterator = kwdValMap.find(keyword); |
| 766 | |
| 767 | if (iterator != kwdValMap.end()) |
| 768 | { |
| 769 | string& kwdValue = iterator->second; |
| 770 | |
| 771 | // check bus data |
| 772 | const string& recordName = systemRecKwdPair.first; |
| 773 | const string& busValue = readBusProperty( |
| 774 | objectPath, ipzVpdInf + recordName, keyword); |
| 775 | |
| 776 | if (busValue.find_first_not_of(' ') != string::npos) |
| 777 | { |
| 778 | if (kwdValue.find_first_not_of(' ') != string::npos) |
| 779 | { |
| 780 | // both the data are present, check for mismatch |
| 781 | if (busValue != kwdValue) |
| 782 | { |
| 783 | string errMsg = "VPD data mismatch on cache " |
| 784 | "and hardware for record: "; |
| 785 | errMsg += (*it).first; |
| 786 | errMsg += " and keyword: "; |
| 787 | errMsg += keyword; |
| 788 | |
| 789 | // data mismatch |
| 790 | PelAdditionalData additionalData; |
| 791 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 792 | objectPath); |
| 793 | |
| 794 | additionalData.emplace("DESCRIPTION", errMsg); |
| 795 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 796 | createPEL(additionalData, PelSeverity::WARNING, |
| 797 | errIntfForInvalidVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | else |
| 801 | { |
| 802 | // implies hardware data is blank |
| 803 | // update the map |
| 804 | Binary busData(busValue.begin(), busValue.end()); |
| 805 | |
| 806 | updatedEeproms.push_back(std::make_tuple( |
| 807 | objectPath, recordName, keyword, busData)); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 808 | |
Sunny Srivastava | 90a63b9 | 2021-05-26 06:30:24 -0500 | [diff] [blame] | 809 | // update the map as well, so that cache data is not |
| 810 | // updated as blank while populating VPD map on Dbus |
| 811 | // in populateDBus Api |
| 812 | kwdValue = busValue; |
| 813 | } |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 814 | } |
| 815 | else if (kwdValue.find_first_not_of(' ') == string::npos) |
| 816 | { |
| 817 | string errMsg = "VPD is blank on both cache and " |
| 818 | "hardware for record: "; |
| 819 | errMsg += (*it).first; |
| 820 | errMsg += " and keyword: "; |
| 821 | errMsg += keyword; |
| 822 | errMsg += ". SSR need to update hardware VPD."; |
| 823 | |
| 824 | // both the data are blanks, log PEL |
| 825 | PelAdditionalData additionalData; |
| 826 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 827 | objectPath); |
| 828 | |
| 829 | additionalData.emplace("DESCRIPTION", errMsg); |
| 830 | |
| 831 | // log PEL TODO: Block IPL |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 832 | createPEL(additionalData, PelSeverity::ERROR, |
| 833 | errIntfForBlankSystemVPD); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 834 | continue; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return updatedEeproms; |
| 842 | } |
| 843 | |
| 844 | /** |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 845 | * @brief This checks for is this FRU a processor |
| 846 | * And if yes, then checks for is this primary |
| 847 | * |
| 848 | * @param[in] js- vpd json to get the information about this FRU |
| 849 | * @param[in] filePath- FRU vpd |
| 850 | * |
| 851 | * @return true/false |
| 852 | */ |
| 853 | bool isThisPrimaryProcessor(nlohmann::json& js, const string& filePath) |
| 854 | { |
| 855 | bool isProcessor = false; |
| 856 | bool isPrimary = false; |
| 857 | |
| 858 | for (const auto& item : js["frus"][filePath]) |
| 859 | { |
| 860 | if (item.find("extraInterfaces") != item.end()) |
| 861 | { |
| 862 | for (const auto& eI : item["extraInterfaces"].items()) |
| 863 | { |
| 864 | if (eI.key().find("Inventory.Item.Cpu") != string::npos) |
| 865 | { |
| 866 | isProcessor = true; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (isProcessor) |
| 872 | { |
| 873 | string cpuType = item.value("cpuType", ""); |
| 874 | if (cpuType == "primary") |
| 875 | { |
| 876 | isPrimary = true; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | return (isProcessor && isPrimary); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * @brief This finds DIMM vpd in vpd json and enables them by binding the device |
| 886 | * driver |
| 887 | * @param[in] js- vpd json to iterate through and take action if it is DIMM |
| 888 | */ |
| 889 | void doEnableAllDimms(nlohmann::json& js) |
| 890 | { |
| 891 | // iterate over each fru |
| 892 | for (const auto& eachFru : js["frus"].items()) |
| 893 | { |
| 894 | // skip the driver binding if eeprom already exists |
| 895 | if (fs::exists(eachFru.key())) |
| 896 | { |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | for (const auto& eachInventory : eachFru.value()) |
| 901 | { |
| 902 | if (eachInventory.find("extraInterfaces") != eachInventory.end()) |
| 903 | { |
| 904 | for (const auto& eI : eachInventory["extraInterfaces"].items()) |
| 905 | { |
| 906 | if (eI.key().find("Inventory.Item.Dimm") != string::npos) |
| 907 | { |
| 908 | string dimmVpd = eachFru.key(); |
| 909 | // fetch it from |
| 910 | // "/sys/bus/i2c/drivers/at24/414-0050/eeprom" |
| 911 | |
| 912 | regex matchPatern("([0-9]+-[0-9]{4})"); |
| 913 | smatch matchFound; |
| 914 | if (regex_search(dimmVpd, matchFound, matchPatern)) |
| 915 | { |
| 916 | vector<string> i2cReg; |
| 917 | boost::split(i2cReg, matchFound.str(0), |
| 918 | boost::is_any_of("-")); |
| 919 | |
| 920 | // remove 0s from begining |
| 921 | const regex pattern("^0+(?!$)"); |
| 922 | for (auto& i : i2cReg) |
| 923 | { |
| 924 | i = regex_replace(i, pattern, ""); |
| 925 | } |
| 926 | |
| 927 | if (i2cReg.size() == 2) |
| 928 | { |
| 929 | // echo 24c32 0x50 > |
| 930 | // /sys/bus/i2c/devices/i2c-16/new_device |
| 931 | string cmnd = "echo 24c32 0x" + i2cReg[1] + |
| 932 | " > /sys/bus/i2c/devices/i2c-" + |
| 933 | i2cReg[0] + "/new_device"; |
| 934 | |
| 935 | executeCmd(cmnd); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 946 | * @brief Populate Dbus. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 947 | * This method invokes all the populateInterface functions |
| 948 | * and notifies PIM about dbus object. |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 949 | * @param[in] vpdMap - Either IPZ vpd map or Keyword vpd map based on the |
| 950 | * input. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 951 | * @param[in] js - Inventory json object |
| 952 | * @param[in] filePath - Path of the vpd file |
| 953 | * @param[in] preIntrStr - Interface string |
| 954 | */ |
| 955 | template <typename T> |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 956 | static void populateDbus(T& vpdMap, nlohmann::json& js, const string& filePath) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 957 | { |
| 958 | inventory::InterfaceMap interfaces; |
| 959 | inventory::ObjectMap objects; |
| 960 | inventory::PropertyMap prop; |
Shantappa Teekappanavar | 6aa5450 | 2021-12-09 12:59:56 -0600 | [diff] [blame] | 961 | string ccinFromVpd; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 962 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 963 | // map to hold all the keywords whose value has been changed at standby |
| 964 | vector<RestoredEeproms> updatedEeproms = {}; |
| 965 | |
Santosh Puranik | 50f60bf | 2021-05-26 17:55:06 +0530 | [diff] [blame] | 966 | bool isSystemVpd = (filePath == systemVpdFilePath); |
| 967 | if constexpr (is_same<T, Parsed>::value) |
| 968 | { |
Shantappa Teekappanavar | 6aa5450 | 2021-12-09 12:59:56 -0600 | [diff] [blame] | 969 | ccinFromVpd = getKwVal(vpdMap, "VINI", "CC"); |
| 970 | transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(), |
| 971 | ::toupper); |
| 972 | |
Santosh Puranik | 50f60bf | 2021-05-26 17:55:06 +0530 | [diff] [blame] | 973 | if (isSystemVpd) |
| 974 | { |
| 975 | std::vector<std::string> interfaces = {motherBoardInterface}; |
| 976 | // call mapper to check for object path creation |
| 977 | MapperResponse subTree = |
| 978 | getObjectSubtreeForInterfaces(pimPath, 0, interfaces); |
| 979 | string mboardPath = |
| 980 | js["frus"][filePath].at(0).value("inventoryPath", ""); |
| 981 | |
| 982 | // Attempt system VPD restore if we have a motherboard |
| 983 | // object in the inventory. |
| 984 | if ((subTree.size() != 0) && |
| 985 | (subTree.find(pimPath + mboardPath) != subTree.end())) |
| 986 | { |
| 987 | updatedEeproms = restoreSystemVPD(vpdMap, mboardPath); |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | log<level::ERR>("No object path found"); |
| 992 | } |
| 993 | } |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 994 | else |
| 995 | { |
| 996 | // check if it is processor vpd. |
| 997 | auto isPrimaryCpu = isThisPrimaryProcessor(js, filePath); |
| 998 | |
| 999 | if (isPrimaryCpu) |
| 1000 | { |
| 1001 | auto ddVersion = getKwVal(vpdMap, "CRP0", "DD"); |
| 1002 | |
| 1003 | auto chipVersion = atoi(ddVersion.substr(1, 2).c_str()); |
| 1004 | |
| 1005 | if (chipVersion >= 2) |
| 1006 | { |
| 1007 | doEnableAllDimms(js); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
Santosh Puranik | 50f60bf | 2021-05-26 17:55:06 +0530 | [diff] [blame] | 1011 | } |
| 1012 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1013 | for (const auto& item : js["frus"][filePath]) |
| 1014 | { |
| 1015 | const auto& objectPath = item["inventoryPath"]; |
| 1016 | sdbusplus::message::object_path object(objectPath); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 1017 | |
Shantappa Teekappanavar | 6aa5450 | 2021-12-09 12:59:56 -0600 | [diff] [blame] | 1018 | vector<string> ccinList; |
| 1019 | if (item.find("ccin") != item.end()) |
| 1020 | { |
| 1021 | for (const auto& cc : item["ccin"]) |
| 1022 | { |
| 1023 | string ccin = cc; |
| 1024 | transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper); |
| 1025 | ccinList.push_back(ccin); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (!ccinFromVpd.empty() && !ccinList.empty() && |
| 1030 | (find(ccinList.begin(), ccinList.end(), ccinFromVpd) == |
| 1031 | ccinList.end())) |
| 1032 | { |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
Santosh Puranik | d3a379a | 2021-08-23 19:12:59 +0530 | [diff] [blame] | 1036 | if (isSystemVpd) |
| 1037 | { |
| 1038 | // Populate one time properties for the system VPD and its sub-frus. |
| 1039 | // For the remaining FRUs, this will get handled as a part of |
| 1040 | // priming the inventory. |
| 1041 | setOneTimeProperties(objectPath, interfaces); |
| 1042 | } |
| 1043 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1044 | // Populate the VPD keywords and the common interfaces only if we |
| 1045 | // are asked to inherit that data from the VPD, else only add the |
| 1046 | // extraInterfaces. |
| 1047 | if (item.value("inherit", true)) |
| 1048 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 1049 | if constexpr (is_same<T, Parsed>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1050 | { |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 1051 | // Each record in the VPD becomes an interface and all |
| 1052 | // keyword within the record are properties under that |
| 1053 | // interface. |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1054 | for (const auto& record : vpdMap) |
| 1055 | { |
| 1056 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 1057 | record.second, ipzVpdInf + record.first, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1058 | } |
| 1059 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 1060 | else if constexpr (is_same<T, KeywordVpdMap>::value) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1061 | { |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 1062 | populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1063 | } |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 1064 | if (js.find("commonInterfaces") != js.end()) |
| 1065 | { |
| 1066 | populateInterfaces(js["commonInterfaces"], interfaces, vpdMap, |
| 1067 | isSystemVpd); |
| 1068 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1069 | } |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 1070 | else |
| 1071 | { |
| 1072 | // Check if we have been asked to inherit specific record(s) |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 1073 | if constexpr (is_same<T, Parsed>::value) |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 1074 | { |
| 1075 | if (item.find("copyRecords") != item.end()) |
| 1076 | { |
| 1077 | for (const auto& record : item["copyRecords"]) |
| 1078 | { |
| 1079 | const string& recordName = record; |
| 1080 | if (vpdMap.find(recordName) != vpdMap.end()) |
| 1081 | { |
| 1082 | populateFruSpecificInterfaces( |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 1083 | vpdMap.at(recordName), ipzVpdInf + recordName, |
Santosh Puranik | 0859eb6 | 2020-03-16 02:56:29 -0500 | [diff] [blame] | 1084 | interfaces); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 1090 | if (item.value("inheritEI", true)) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1091 | { |
Alpana Kumari | 58e2214 | 2020-05-05 00:22:12 -0500 | [diff] [blame] | 1092 | // Populate interfaces and properties that are common to every FRU |
| 1093 | // and additional interface that might be defined on a per-FRU |
| 1094 | // basis. |
| 1095 | if (item.find("extraInterfaces") != item.end()) |
| 1096 | { |
| 1097 | populateInterfaces(item["extraInterfaces"], interfaces, vpdMap, |
| 1098 | isSystemVpd); |
| 1099 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1100 | } |
| 1101 | objects.emplace(move(object), move(interfaces)); |
| 1102 | } |
| 1103 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 1104 | if (isSystemVpd) |
| 1105 | { |
Santosh Puranik | d278df1 | 2021-05-10 15:51:42 +0530 | [diff] [blame] | 1106 | string systemJsonName{}; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 1107 | if constexpr (is_same<T, Parsed>::value) |
| 1108 | { |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 1109 | // pick the right system json |
Santosh Puranik | d278df1 | 2021-05-10 15:51:42 +0530 | [diff] [blame] | 1110 | systemJsonName = getSystemsJson(vpdMap); |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 1111 | } |
| 1112 | |
Santosh Puranik | d278df1 | 2021-05-10 15:51:42 +0530 | [diff] [blame] | 1113 | fs::path target = systemJsonName; |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 1114 | fs::path link = INVENTORY_JSON_SYM_LINK; |
| 1115 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 1116 | // Create the directory for hosting the symlink |
| 1117 | fs::create_directories(VPD_FILES_PATH); |
| 1118 | // unlink the symlink previously created (if any) |
PriyangaRamasamy | 83a1d5d | 2020-04-30 19:15:43 +0530 | [diff] [blame] | 1119 | remove(INVENTORY_JSON_SYM_LINK); |
| 1120 | // create a new symlink based on the system |
| 1121 | fs::create_symlink(target, link); |
| 1122 | |
| 1123 | // Reloading the json |
| 1124 | ifstream inventoryJson(link); |
| 1125 | auto js = json::parse(inventoryJson); |
| 1126 | inventoryJson.close(); |
| 1127 | |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 1128 | inventory::ObjectMap primeObject = primeInventory(js, vpdMap); |
| 1129 | objects.insert(primeObject.begin(), primeObject.end()); |
Alpana Kumari | 65b8360 | 2020-09-01 00:24:56 -0500 | [diff] [blame] | 1130 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 1131 | // if system VPD has been restored at standby, update the EEPROM |
| 1132 | for (const auto& item : updatedEeproms) |
| 1133 | { |
| 1134 | updateHardware(get<0>(item), get<1>(item), get<2>(item), |
| 1135 | get<3>(item)); |
| 1136 | } |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 1137 | |
| 1138 | // set the U-boot environment variable for device-tree |
| 1139 | if constexpr (is_same<T, Parsed>::value) |
| 1140 | { |
| 1141 | const string imKeyword = getIM(vpdMap); |
| 1142 | const string hwKeyword = getHW(vpdMap); |
| 1143 | string systemType = imKeyword; |
| 1144 | |
| 1145 | // check If system has constraint then append HW version to it. |
| 1146 | ifstream sysJson(SYSTEM_JSON); |
| 1147 | if (!sysJson) |
| 1148 | { |
| 1149 | throw((VpdJsonException("Failed to access Json path", |
| 1150 | SYSTEM_JSON))); |
| 1151 | } |
| 1152 | |
| 1153 | try |
| 1154 | { |
| 1155 | auto systemJson = json::parse(sysJson); |
Santosh Puranik | d278df1 | 2021-05-10 15:51:42 +0530 | [diff] [blame] | 1156 | if (systemJson["system"].find(imKeyword) != |
| 1157 | systemJson["system"].end()) |
| 1158 | { |
| 1159 | if (systemJson["system"][imKeyword].find("constraint") != |
| 1160 | systemJson["system"][imKeyword].end()) |
| 1161 | { |
| 1162 | systemType += "_" + hwKeyword; |
| 1163 | } |
| 1164 | } |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 1165 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 1166 | catch (const json::parse_error& ex) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 1167 | { |
| 1168 | throw((VpdJsonException("System Json parsing failed", |
| 1169 | SYSTEM_JSON))); |
| 1170 | } |
| 1171 | |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 1172 | setDevTreeEnv(systemType); |
| 1173 | } |
PriyangaRamasamy | 8e140a1 | 2020-04-13 19:24:03 +0530 | [diff] [blame] | 1174 | } |
| 1175 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1176 | // Notify PIM |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 1177 | common::utility::callPIM(move(objects)); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | int main(int argc, char** argv) |
| 1181 | { |
| 1182 | int rc = 0; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1183 | json js{}; |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 1184 | Binary vpdVector{}; |
| 1185 | string file{}; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1186 | // map to hold additional data in case of logging pel |
| 1187 | PelAdditionalData additionalData{}; |
| 1188 | |
| 1189 | // this is needed to hold base fru inventory path in case there is ECC or |
| 1190 | // vpd exception while parsing the file |
| 1191 | std::string baseFruInventoryPath = {}; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1192 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1193 | // severity for PEL |
| 1194 | PelSeverity pelSeverity = PelSeverity::WARNING; |
| 1195 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1196 | try |
| 1197 | { |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1198 | App app{"ibm-read-vpd - App to read IPZ format VPD, parse it and store " |
| 1199 | "in DBUS"}; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1200 | |
| 1201 | app.add_option("-f, --file", file, "File containing VPD (IPZ/KEYWORD)") |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1202 | ->required(); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1203 | |
| 1204 | CLI11_PARSE(app, argc, argv); |
| 1205 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1206 | // PEL severity should be ERROR in case of any system VPD failure |
| 1207 | if (file == systemVpdFilePath) |
| 1208 | { |
| 1209 | pelSeverity = PelSeverity::ERROR; |
| 1210 | } |
| 1211 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 1212 | auto jsonToParse = INVENTORY_JSON_DEFAULT; |
| 1213 | |
| 1214 | // If the symlink exists, it means it has been setup for us, switch the |
| 1215 | // path |
| 1216 | if (fs::exists(INVENTORY_JSON_SYM_LINK)) |
| 1217 | { |
| 1218 | jsonToParse = INVENTORY_JSON_SYM_LINK; |
| 1219 | } |
| 1220 | |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1221 | // 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] | 1222 | ifstream inventoryJson(jsonToParse); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1223 | if (!inventoryJson) |
| 1224 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1225 | throw(VpdJsonException("Failed to access Json path", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | try |
| 1229 | { |
| 1230 | js = json::parse(inventoryJson); |
| 1231 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 1232 | catch (const json::parse_error& ex) |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1233 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1234 | throw(VpdJsonException("Json parsing failed", jsonToParse)); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1235 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1236 | |
Santosh Puranik | 12e24ff | 2021-05-11 19:33:50 +0530 | [diff] [blame] | 1237 | // Do we have the mandatory "frus" section? |
| 1238 | if (js.find("frus") == js.end()) |
| 1239 | { |
| 1240 | throw(VpdJsonException("FRUs section not found in JSON", |
| 1241 | jsonToParse)); |
| 1242 | } |
| 1243 | |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 1244 | // Check if it's a udev path - patterned as(/ahb/ahb:apb/ahb:apb:bus@) |
| 1245 | if (file.find("/ahb:apb") != string::npos) |
| 1246 | { |
| 1247 | // Translate udev path to a generic /sys/bus/.. file path. |
| 1248 | udevToGenericPath(file); |
Santosh Puranik | 12e24ff | 2021-05-11 19:33:50 +0530 | [diff] [blame] | 1249 | |
| 1250 | if ((js["frus"].find(file) != js["frus"].end()) && |
Santosh Puranik | 50f60bf | 2021-05-26 17:55:06 +0530 | [diff] [blame] | 1251 | (file == systemVpdFilePath)) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 1252 | { |
| 1253 | return 0; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | if (file.empty()) |
| 1258 | { |
| 1259 | cerr << "The EEPROM path <" << file << "> is not valid."; |
| 1260 | return 0; |
| 1261 | } |
Santosh Puranik | 12e24ff | 2021-05-11 19:33:50 +0530 | [diff] [blame] | 1262 | if (js["frus"].find(file) == js["frus"].end()) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1263 | { |
Santosh Puranik | 88edeb6 | 2020-03-02 12:00:09 +0530 | [diff] [blame] | 1264 | return 0; |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1265 | } |
| 1266 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1267 | if (!fs::exists(file)) |
| 1268 | { |
| 1269 | cout << "Device path: " << file |
| 1270 | << " does not exist. Spurious udev event? Exiting." << endl; |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1274 | baseFruInventoryPath = js["frus"][file][0]["inventoryPath"]; |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 1275 | // Check if we can read the VPD file based on the power state |
Santosh Puranik | 27a5e95 | 2021-10-07 22:08:01 -0500 | [diff] [blame] | 1276 | // We skip reading VPD when the power is ON in two scenarios: |
| 1277 | // 1) The eeprom we are trying to read is that of the system VPD |
| 1278 | // 2) The JSON tells us that the FRU EEPROM cannot be read when |
| 1279 | // we are powered ON. |
| 1280 | if (js["frus"][file].at(0).value("powerOffOnly", false) || |
| 1281 | (file == systemVpdFilePath)) |
Santosh Puranik | 8589375 | 2020-11-10 21:31:43 +0530 | [diff] [blame] | 1282 | { |
| 1283 | if ("xyz.openbmc_project.State.Chassis.PowerState.On" == |
| 1284 | getPowerState()) |
| 1285 | { |
| 1286 | cout << "This VPD cannot be read when power is ON" << endl; |
| 1287 | return 0; |
| 1288 | } |
| 1289 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1290 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1291 | try |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1292 | { |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 1293 | vpdVector = getVpdDataInVector(js, file); |
PriyangaRamasamy | 33c61c2 | 2021-04-06 11:15:57 -0500 | [diff] [blame] | 1294 | ParserInterface* parser = ParserFactory::getParser(vpdVector); |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1295 | variant<KeywordVpdMap, Store> parseResult; |
| 1296 | parseResult = parser->parse(); |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 1297 | |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1298 | if (auto pVal = get_if<Store>(&parseResult)) |
| 1299 | { |
| 1300 | populateDbus(pVal->getVpdMap(), js, file); |
| 1301 | } |
| 1302 | else if (auto pVal = get_if<KeywordVpdMap>(&parseResult)) |
| 1303 | { |
| 1304 | populateDbus(*pVal, js, file); |
| 1305 | } |
| 1306 | |
| 1307 | // release the parser object |
| 1308 | ParserFactory::freeParser(parser); |
| 1309 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 1310 | catch (const exception& e) |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1311 | { |
| 1312 | postFailAction(js, file); |
PriyangaRamasamy | a504c3e | 2020-12-06 12:14:52 -0600 | [diff] [blame] | 1313 | throw; |
Alpana Kumari | 2f79304 | 2020-08-18 05:51:03 -0500 | [diff] [blame] | 1314 | } |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1315 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1316 | catch (const VpdJsonException& ex) |
| 1317 | { |
| 1318 | additionalData.emplace("JSON_PATH", ex.getJsonPath()); |
| 1319 | additionalData.emplace("DESCRIPTION", ex.what()); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1320 | createPEL(additionalData, pelSeverity, errIntfForJsonFailure); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1321 | |
| 1322 | cerr << ex.what() << "\n"; |
| 1323 | rc = -1; |
| 1324 | } |
| 1325 | catch (const VpdEccException& ex) |
| 1326 | { |
| 1327 | additionalData.emplace("DESCRIPTION", "ECC check failed"); |
| 1328 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1329 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1330 | createPEL(additionalData, pelSeverity, errIntfForEccCheckFail); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 1331 | dumpBadVpd(file, vpdVector); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1332 | cerr << ex.what() << "\n"; |
| 1333 | rc = -1; |
| 1334 | } |
| 1335 | catch (const VpdDataException& ex) |
| 1336 | { |
| 1337 | additionalData.emplace("DESCRIPTION", "Invalid VPD data"); |
| 1338 | additionalData.emplace("CALLOUT_INVENTORY_PATH", |
| 1339 | INVENTORY_PATH + baseFruInventoryPath); |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 1340 | createPEL(additionalData, pelSeverity, errIntfForInvalidVPD); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 1341 | dumpBadVpd(file, vpdVector); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 1342 | cerr << ex.what() << "\n"; |
| 1343 | rc = -1; |
| 1344 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 1345 | catch (const exception& e) |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1346 | { |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 1347 | dumpBadVpd(file, vpdVector); |
PriyangaRamasamy | abb87ed | 2019-11-19 17:25:35 +0530 | [diff] [blame] | 1348 | cerr << e.what() << "\n"; |
| 1349 | rc = -1; |
| 1350 | } |
| 1351 | |
| 1352 | return rc; |
Alpana Kumari | 37e7270 | 2021-11-18 11:18:04 -0600 | [diff] [blame] | 1353 | } |