SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 3 | #include "ibm_vpd_utils.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 4 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 5 | #include "common_utility.hpp" |
Jinu Joy Thomas | e2b0383 | 2023-08-22 19:51:31 +0530 | [diff] [blame^] | 6 | #include "const.hpp" |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 7 | #include "defines.hpp" |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 8 | #include "vpd_exceptions.hpp" |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 9 | |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 10 | #include <boost/algorithm/string.hpp> |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 11 | #include <gpiod.hpp> |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 14 | #include <phosphor-logging/log.hpp> |
| 15 | #include <sdbusplus/server.hpp> |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
| 17 | |
| 18 | #include <filesystem> |
| 19 | #include <fstream> |
| 20 | #include <iomanip> |
| 21 | #include <regex> |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 22 | #include <sstream> |
| 23 | #include <vector> |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 24 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 25 | using json = nlohmann::json; |
| 26 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 27 | namespace openpower |
| 28 | { |
| 29 | namespace vpd |
| 30 | { |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 31 | using namespace openpower::vpd::constants; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 32 | using namespace inventory; |
| 33 | using namespace phosphor::logging; |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 34 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 35 | using namespace record; |
| 36 | using namespace openpower::vpd::exceptions; |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 37 | using namespace common::utility; |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 38 | using Severity = openpower::vpd::constants::PelSeverity; |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 39 | namespace fs = std::filesystem; |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 40 | |
| 41 | // mapping of severity enum to severity interface |
| 42 | static std::unordered_map<Severity, std::string> sevMap = { |
| 43 | {Severity::INFORMATIONAL, |
| 44 | "xyz.openbmc_project.Logging.Entry.Level.Informational"}, |
| 45 | {Severity::DEBUG, "xyz.openbmc_project.Logging.Entry.Level.Debug"}, |
| 46 | {Severity::NOTICE, "xyz.openbmc_project.Logging.Entry.Level.Notice"}, |
| 47 | {Severity::WARNING, "xyz.openbmc_project.Logging.Entry.Level.Warning"}, |
| 48 | {Severity::CRITICAL, "xyz.openbmc_project.Logging.Entry.Level.Critical"}, |
| 49 | {Severity::EMERGENCY, "xyz.openbmc_project.Logging.Entry.Level.Emergency"}, |
| 50 | {Severity::ERROR, "xyz.openbmc_project.Logging.Entry.Level.Error"}, |
| 51 | {Severity::ALERT, "xyz.openbmc_project.Logging.Entry.Level.Alert"}}; |
| 52 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 53 | namespace inventory |
| 54 | { |
| 55 | |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 56 | MapperResponse |
| 57 | getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth, |
| 58 | const std::vector<std::string>& interfaces) |
| 59 | { |
| 60 | auto bus = sdbusplus::bus::new_default(); |
| 61 | auto mapperCall = bus.new_method_call(mapperDestination, mapperObjectPath, |
| 62 | mapperInterface, "GetSubTree"); |
| 63 | mapperCall.append(root); |
| 64 | mapperCall.append(depth); |
| 65 | mapperCall.append(interfaces); |
| 66 | |
| 67 | MapperResponse result = {}; |
| 68 | |
| 69 | try |
| 70 | { |
| 71 | auto response = bus.call(mapperCall); |
| 72 | |
| 73 | response.read(result); |
| 74 | } |
Patrick Williams | 2eb0176 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 75 | catch (const sdbusplus::exception_t& e) |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 76 | { |
| 77 | log<level::ERR>("Error in mapper GetSubTree", |
| 78 | entry("ERROR=%s", e.what())); |
| 79 | } |
| 80 | |
| 81 | return result; |
| 82 | } |
| 83 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 84 | } // namespace inventory |
| 85 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 86 | LE2ByteData readUInt16LE(Binary::const_iterator iterator) |
| 87 | { |
| 88 | LE2ByteData lowByte = *iterator; |
| 89 | LE2ByteData highByte = *(iterator + 1); |
| 90 | lowByte |= (highByte << 8); |
| 91 | return lowByte; |
| 92 | } |
| 93 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 94 | /** @brief Encodes a keyword for D-Bus. |
| 95 | */ |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 96 | std::string encodeKeyword(const std::string& kw, const std::string& encoding) |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 97 | { |
| 98 | if (encoding == "MAC") |
| 99 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 100 | std::string res{}; |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 101 | size_t first = kw[0]; |
| 102 | res += toHex(first >> 4); |
| 103 | res += toHex(first & 0x0f); |
| 104 | for (size_t i = 1; i < kw.size(); ++i) |
| 105 | { |
| 106 | res += ":"; |
| 107 | res += toHex(kw[i] >> 4); |
| 108 | res += toHex(kw[i] & 0x0f); |
| 109 | } |
| 110 | return res; |
| 111 | } |
| 112 | else if (encoding == "DATE") |
| 113 | { |
| 114 | // Date, represent as |
| 115 | // <year>-<month>-<day> <hour>:<min> |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 116 | std::string res{}; |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 117 | static constexpr uint8_t skipPrefix = 3; |
| 118 | |
| 119 | auto strItr = kw.begin(); |
| 120 | advance(strItr, skipPrefix); |
| 121 | for_each(strItr, kw.end(), [&res](size_t c) { res += c; }); |
| 122 | |
| 123 | res.insert(BD_YEAR_END, 1, '-'); |
| 124 | res.insert(BD_MONTH_END, 1, '-'); |
| 125 | res.insert(BD_DAY_END, 1, ' '); |
| 126 | res.insert(BD_HOUR_END, 1, ':'); |
| 127 | |
| 128 | return res; |
| 129 | } |
| 130 | else // default to string encoding |
| 131 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 132 | return std::string(kw.begin(), kw.end()); |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 133 | } |
| 134 | } |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 135 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 136 | std::string readBusProperty(const std::string& obj, const std::string& inf, |
| 137 | const std::string& prop) |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 138 | { |
| 139 | std::string propVal{}; |
| 140 | std::string object = INVENTORY_PATH + obj; |
| 141 | auto bus = sdbusplus::bus::new_default(); |
| 142 | auto properties = bus.new_method_call( |
| 143 | "xyz.openbmc_project.Inventory.Manager", object.c_str(), |
| 144 | "org.freedesktop.DBus.Properties", "Get"); |
| 145 | properties.append(inf); |
| 146 | properties.append(prop); |
| 147 | auto result = bus.call(properties); |
| 148 | if (!result.is_method_error()) |
| 149 | { |
Sunny Srivastava | 28abd6e | 2021-07-28 02:58:28 -0500 | [diff] [blame] | 150 | inventory::Value val; |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 151 | result.read(val); |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 152 | if (auto pVal = std::get_if<Binary>(&val)) |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 153 | { |
| 154 | propVal.assign(reinterpret_cast<const char*>(pVal->data()), |
| 155 | pVal->size()); |
| 156 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 157 | else if (auto pVal = std::get_if<std::string>(&val)) |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 158 | { |
| 159 | propVal.assign(pVal->data(), pVal->size()); |
| 160 | } |
Sunny Srivastava | 28abd6e | 2021-07-28 02:58:28 -0500 | [diff] [blame] | 161 | else if (auto pVal = get_if<bool>(&val)) |
| 162 | { |
| 163 | if (*pVal == false) |
| 164 | { |
| 165 | propVal = "false"; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | propVal = "true"; |
| 170 | } |
| 171 | } |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 172 | } |
| 173 | return propVal; |
| 174 | } |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 175 | |
| 176 | void createPEL(const std::map<std::string, std::string>& additionalData, |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 177 | const Severity& sev, const std::string& errIntf, sd_bus* sdBus) |
| 178 | { |
| 179 | // This pointer will be NULL in case the call is made from ibm-read-vpd. In |
| 180 | // that case a sync call will do. |
| 181 | if (sdBus == nullptr) |
| 182 | { |
| 183 | createSyncPEL(additionalData, sev, errIntf); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | std::string errDescription{}; |
| 188 | auto pos = additionalData.find("DESCRIPTION"); |
| 189 | if (pos != additionalData.end()) |
| 190 | { |
| 191 | errDescription = pos->second; |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | errDescription = "Description field missing in additional data"; |
| 196 | } |
| 197 | |
| 198 | std::string pelSeverity = |
| 199 | "xyz.openbmc_project.Logging.Entry.Level.Error"; |
| 200 | auto itr = sevMap.find(sev); |
| 201 | if (itr != sevMap.end()) |
| 202 | { |
| 203 | pelSeverity = itr->second; |
| 204 | } |
| 205 | |
| 206 | // Implies this is a call from Manager. Hence we need to make an async |
| 207 | // call to avoid deadlock with Phosphor-logging. |
| 208 | auto rc = sd_bus_call_method_async( |
| 209 | sdBus, NULL, loggerService, loggerObjectPath, loggerCreateInterface, |
| 210 | "Create", NULL, NULL, "ssa{ss}", errIntf.c_str(), |
| 211 | pelSeverity.c_str(), 1, "DESCRIPTION", errDescription.c_str()); |
| 212 | |
| 213 | if (rc < 0) |
| 214 | { |
| 215 | log<level::ERR>("Error calling sd_bus_call_method_async", |
| 216 | entry("RC=%d", rc), entry("MSG=%s", strerror(-rc))); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void createSyncPEL(const std::map<std::string, std::string>& additionalData, |
| 222 | const Severity& sev, const std::string& errIntf) |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 223 | { |
| 224 | try |
| 225 | { |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 226 | std::string pelSeverity = |
| 227 | "xyz.openbmc_project.Logging.Entry.Level.Error"; |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 228 | auto bus = sdbusplus::bus::new_default(); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 229 | auto service = getService(bus, loggerObjectPath, loggerCreateInterface); |
| 230 | auto method = bus.new_method_call(service.c_str(), loggerObjectPath, |
| 231 | loggerCreateInterface, "Create"); |
| 232 | |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 233 | auto itr = sevMap.find(sev); |
| 234 | if (itr != sevMap.end()) |
| 235 | { |
| 236 | pelSeverity = itr->second; |
| 237 | } |
| 238 | |
| 239 | method.append(errIntf, pelSeverity, additionalData); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 240 | auto resp = bus.call(method); |
| 241 | } |
Patrick Williams | 2eb0176 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 242 | catch (const sdbusplus::exception_t& e) |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 243 | { |
Sunny Srivastava | 5ef6ccc | 2022-05-30 01:35:13 -0500 | [diff] [blame] | 244 | std::cerr << "Dbus call to phosphor-logging Create failed. Reason:" |
| 245 | << e.what(); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 246 | } |
| 247 | } |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 248 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 249 | inventory::VPDfilepath getVpdFilePath(const std::string& jsonFile, |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 250 | const std::string& ObjPath) |
| 251 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 252 | std::ifstream inventoryJson(jsonFile); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 253 | const auto& jsonObject = json::parse(inventoryJson); |
| 254 | inventory::VPDfilepath filePath{}; |
| 255 | |
| 256 | if (jsonObject.find("frus") == jsonObject.end()) |
| 257 | { |
| 258 | throw(VpdJsonException( |
| 259 | "Invalid JSON structure - frus{} object not found in ", jsonFile)); |
| 260 | } |
| 261 | |
| 262 | const nlohmann::json& groupFRUS = |
| 263 | jsonObject["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 264 | for (const auto& itemFRUS : groupFRUS.items()) |
| 265 | { |
| 266 | const std::vector<nlohmann::json>& groupEEPROM = |
| 267 | itemFRUS.value().get_ref<const nlohmann::json::array_t&>(); |
| 268 | for (const auto& itemEEPROM : groupEEPROM) |
| 269 | { |
| 270 | if (itemEEPROM["inventoryPath"] |
| 271 | .get_ref<const nlohmann::json::string_t&>() == ObjPath) |
| 272 | { |
| 273 | filePath = itemFRUS.key(); |
| 274 | return filePath; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return filePath; |
| 280 | } |
| 281 | |
| 282 | bool isPathInJson(const std::string& eepromPath) |
| 283 | { |
| 284 | bool present = false; |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 285 | std::ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 286 | |
| 287 | try |
| 288 | { |
| 289 | auto js = json::parse(inventoryJson); |
| 290 | if (js.find("frus") == js.end()) |
| 291 | { |
| 292 | throw(VpdJsonException( |
| 293 | "Invalid JSON structure - frus{} object not found in ", |
| 294 | INVENTORY_JSON_SYM_LINK)); |
| 295 | } |
| 296 | json fruJson = js["frus"]; |
| 297 | |
| 298 | if (fruJson.find(eepromPath) != fruJson.end()) |
| 299 | { |
| 300 | present = true; |
| 301 | } |
| 302 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 303 | catch (const json::parse_error& ex) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 304 | { |
| 305 | throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK)); |
| 306 | } |
| 307 | return present; |
| 308 | } |
| 309 | |
| 310 | bool isRecKwInDbusJson(const std::string& recordName, |
| 311 | const std::string& keyword) |
| 312 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 313 | std::ifstream propertyJson(DBUS_PROP_JSON); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 314 | json dbusProperty; |
| 315 | bool present = false; |
| 316 | |
| 317 | if (propertyJson.is_open()) |
| 318 | { |
| 319 | try |
| 320 | { |
| 321 | auto dbusPropertyJson = json::parse(propertyJson); |
| 322 | if (dbusPropertyJson.find("dbusProperties") == |
| 323 | dbusPropertyJson.end()) |
| 324 | { |
| 325 | throw(VpdJsonException("dbusProperties{} object not found in " |
| 326 | "DbusProperties json : ", |
| 327 | DBUS_PROP_JSON)); |
| 328 | } |
| 329 | |
| 330 | dbusProperty = dbusPropertyJson["dbusProperties"]; |
| 331 | if (dbusProperty.contains(recordName)) |
| 332 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 333 | const std::vector<std::string>& kwdsToPublish = |
| 334 | dbusProperty[recordName]; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 335 | if (find(kwdsToPublish.begin(), kwdsToPublish.end(), keyword) != |
| 336 | kwdsToPublish.end()) // present |
| 337 | { |
| 338 | present = true; |
| 339 | } |
| 340 | } |
| 341 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 342 | catch (const json::parse_error& ex) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 343 | { |
| 344 | throw(VpdJsonException("Json Parsing failed", DBUS_PROP_JSON)); |
| 345 | } |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | // If dbus properties json is not available, we assume the given |
| 350 | // record-keyword is part of dbus-properties json. So setting the bool |
| 351 | // variable to true. |
| 352 | present = true; |
| 353 | } |
| 354 | return present; |
| 355 | } |
| 356 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 357 | vpdType vpdTypeCheck(const Binary& vpdVector) |
| 358 | { |
| 359 | // Read first 3 Bytes to check the 11S bar code format |
| 360 | std::string is11SFormat = ""; |
| 361 | for (uint8_t i = 0; i < FORMAT_11S_LEN; i++) |
| 362 | { |
| 363 | is11SFormat += vpdVector[MEMORY_VPD_DATA_START + i]; |
| 364 | } |
| 365 | |
| 366 | if (vpdVector[IPZ_DATA_START] == KW_VAL_PAIR_START_TAG) |
| 367 | { |
| 368 | // IPZ VPD FORMAT |
| 369 | return vpdType::IPZ_VPD; |
| 370 | } |
| 371 | else if (vpdVector[KW_VPD_DATA_START] == KW_VPD_START_TAG) |
| 372 | { |
| 373 | // KEYWORD VPD FORMAT |
| 374 | return vpdType::KEYWORD_VPD; |
| 375 | } |
jinuthomas | 6555e7e | 2023-02-14 21:48:00 -0600 | [diff] [blame] | 376 | else if (((vpdVector[SPD_BYTE_3] & SPD_BYTE_BIT_0_3_MASK) == |
| 377 | SPD_MODULE_TYPE_DDIMM) && |
jinuthomas | 0abbb9c | 2023-05-05 01:37:07 -0500 | [diff] [blame] | 378 | (is11SFormat.compare(MEMORY_VPD_START_TAG) == 0)) |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 379 | { |
jinuthomas | 6555e7e | 2023-02-14 21:48:00 -0600 | [diff] [blame] | 380 | // DDIMM Memory VPD format |
| 381 | if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR5) |
| 382 | { |
| 383 | return vpdType::DDR5_DDIMM_MEMORY_VPD; |
| 384 | } |
| 385 | else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR4) |
| 386 | { |
| 387 | return vpdType::DDR4_DDIMM_MEMORY_VPD; |
| 388 | } |
| 389 | } |
| 390 | else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR5) |
| 391 | { |
| 392 | // ISDIMM Memory VPD format |
| 393 | return vpdType::DDR5_ISDIMM_MEMORY_VPD; |
| 394 | } |
| 395 | else if ((vpdVector[SPD_BYTE_2] & SPD_BYTE_MASK) == SPD_DRAM_TYPE_DDR4) |
| 396 | { |
| 397 | // ISDIMM Memory VPD format |
| 398 | return vpdType::DDR4_ISDIMM_MEMORY_VPD; |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // INVALID VPD FORMAT |
| 402 | return vpdType::INVALID_VPD_FORMAT; |
| 403 | } |
| 404 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 405 | const std::string getIM(const Parsed& vpdMap) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 406 | { |
| 407 | Binary imVal; |
| 408 | auto property = vpdMap.find("VSBP"); |
| 409 | if (property != vpdMap.end()) |
| 410 | { |
| 411 | auto kw = (property->second).find("IM"); |
| 412 | if (kw != (property->second).end()) |
| 413 | { |
| 414 | copy(kw->second.begin(), kw->second.end(), back_inserter(imVal)); |
| 415 | } |
| 416 | } |
| 417 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 418 | std::ostringstream oss; |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 419 | for (auto& i : imVal) |
| 420 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 421 | oss << std::setw(2) << std::setfill('0') << std::hex |
| 422 | << static_cast<int>(i); |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | return oss.str(); |
| 426 | } |
| 427 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 428 | const std::string getHW(const Parsed& vpdMap) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 429 | { |
| 430 | Binary hwVal; |
| 431 | auto prop = vpdMap.find("VINI"); |
| 432 | if (prop != vpdMap.end()) |
| 433 | { |
| 434 | auto kw = (prop->second).find("HW"); |
| 435 | if (kw != (prop->second).end()) |
| 436 | { |
| 437 | copy(kw->second.begin(), kw->second.end(), back_inserter(hwVal)); |
| 438 | } |
| 439 | } |
| 440 | |
Alpana Kumari | 88d2ae8 | 2021-11-10 03:23:31 -0600 | [diff] [blame] | 441 | // The planar pass only comes from the LSB of the HW keyword, |
| 442 | // where as the MSB is used for other purposes such as signifying clock |
| 443 | // termination. |
| 444 | hwVal[0] = 0x00; |
| 445 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 446 | std::ostringstream hwString; |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 447 | for (auto& i : hwVal) |
| 448 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 449 | hwString << std::setw(2) << std::setfill('0') << std::hex |
| 450 | << static_cast<int>(i); |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return hwString.str(); |
| 454 | } |
| 455 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 456 | std::string getSystemsJson(const Parsed& vpdMap) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 457 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 458 | std::string jsonPath = "/usr/share/vpd/"; |
| 459 | std::string jsonName{}; |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 460 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 461 | std::ifstream systemJson(SYSTEM_JSON); |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 462 | if (!systemJson) |
| 463 | { |
| 464 | throw((VpdJsonException("Failed to access Json path", SYSTEM_JSON))); |
| 465 | } |
| 466 | |
| 467 | try |
| 468 | { |
| 469 | auto js = json::parse(systemJson); |
| 470 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 471 | std::string hwKeyword = getHW(vpdMap); |
| 472 | const std::string imKeyword = getIM(vpdMap); |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 473 | |
Alpana Kumari | 1b02611 | 2022-03-02 23:41:38 -0600 | [diff] [blame] | 474 | transform(hwKeyword.begin(), hwKeyword.end(), hwKeyword.begin(), |
| 475 | ::toupper); |
| 476 | |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 477 | if (js.find("system") == js.end()) |
| 478 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 479 | throw std::runtime_error("Invalid systems Json"); |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | if (js["system"].find(imKeyword) == js["system"].end()) |
| 483 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 484 | throw std::runtime_error( |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 485 | "Invalid system. This system type is not present " |
| 486 | "in the systemsJson. IM: " + |
| 487 | imKeyword); |
| 488 | } |
| 489 | |
| 490 | if ((js["system"][imKeyword].find("constraint") != |
| 491 | js["system"][imKeyword].end()) && |
Alpana Kumari | 1b02611 | 2022-03-02 23:41:38 -0600 | [diff] [blame] | 492 | js["system"][imKeyword]["constraint"].find("HW") != |
| 493 | js["system"][imKeyword]["constraint"].end()) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 494 | { |
Alpana Kumari | 1b02611 | 2022-03-02 23:41:38 -0600 | [diff] [blame] | 495 | // collect hw versions from json, and check hwKeyword is part of it |
| 496 | // if hwKeyword is found there then load respective json |
| 497 | // otherwise load default one. |
| 498 | for (const auto& hwVersion : |
| 499 | js["system"][imKeyword]["constraint"]["HW"]) |
| 500 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 501 | std::string hw = hwVersion; |
Alpana Kumari | 1b02611 | 2022-03-02 23:41:38 -0600 | [diff] [blame] | 502 | transform(hw.begin(), hw.end(), hw.begin(), ::toupper); |
| 503 | |
| 504 | if (hw == hwKeyword) |
| 505 | { |
| 506 | jsonName = js["system"][imKeyword]["constraint"]["json"]; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | if (jsonName.empty() && js["system"][imKeyword].find("default") != |
| 512 | js["system"][imKeyword].end()) |
| 513 | { |
| 514 | jsonName = js["system"][imKeyword]["default"]; |
| 515 | } |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 516 | } |
| 517 | else if (js["system"][imKeyword].find("default") != |
| 518 | js["system"][imKeyword].end()) |
| 519 | { |
| 520 | jsonName = js["system"][imKeyword]["default"]; |
| 521 | } |
| 522 | else |
| 523 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 524 | throw std::runtime_error( |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 525 | "Bad System json. Neither constraint nor default found"); |
| 526 | } |
| 527 | |
| 528 | jsonPath += jsonName; |
| 529 | } |
| 530 | |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 531 | catch (const json::parse_error& ex) |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 532 | { |
| 533 | throw(VpdJsonException("Json Parsing failed", SYSTEM_JSON)); |
| 534 | } |
| 535 | return jsonPath; |
| 536 | } |
| 537 | |
jinuthomas | f457a3e | 2023-04-13 12:22:48 -0500 | [diff] [blame] | 538 | void udevToGenericPath(std::string& file, const std::string& driver) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 539 | { |
| 540 | // Sample udevEvent i2c path : |
| 541 | // "/sys/devices/platform/ahb/ahb:apb/ahb:apb:bus@1e78a000/1e78a480.i2c-bus/i2c-8/8-0051/8-00510/nvmem" |
| 542 | // find if the path contains the word i2c in it. |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 543 | if (file.find("i2c") != std::string::npos) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 544 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 545 | std::string i2cBusAddr{}; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 546 | |
| 547 | // Every udev i2c path should have the common pattern |
| 548 | // "i2c-bus_number/bus_number-vpd_address". Search for |
| 549 | // "bus_number-vpd_address". |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 550 | std::regex i2cPattern("((i2c)-[0-9]+\\/)([0-9]+-[0-9]{4})"); |
| 551 | std::smatch match; |
| 552 | if (std::regex_search(file, match, i2cPattern)) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 553 | { |
| 554 | i2cBusAddr = match.str(3); |
| 555 | } |
| 556 | else |
| 557 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 558 | std::cerr << "The given udev path < " << file |
| 559 | << " > doesn't match the required pattern. Skipping VPD " |
| 560 | "collection." |
| 561 | << std::endl; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 562 | exit(EXIT_SUCCESS); |
| 563 | } |
| 564 | // Forming the generic file path |
jinuthomas | f457a3e | 2023-04-13 12:22:48 -0500 | [diff] [blame] | 565 | file = i2cPathPrefix + driver + "/" + i2cBusAddr + "/eeprom"; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 566 | } |
| 567 | // Sample udevEvent spi path : |
| 568 | // "/sys/devices/platform/ahb/ahb:apb/1e79b000.fsi/fsi-master/fsi0/slave@00:00/00:00:00:04/spi_master/spi2/spi2.0/spi2.00/nvmem" |
| 569 | // find if the path contains the word spi in it. |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 570 | else if (file.find("spi") != std::string::npos) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 571 | { |
| 572 | // Every udev spi path will have common pattern "spi<Digit>/", which |
| 573 | // describes the spi bus number at which the fru is connected; Followed |
| 574 | // by a slash following the vpd address of the fru. Taking the above |
| 575 | // input as a common key, we try to search for the pattern "spi<Digit>/" |
| 576 | // using regular expression. |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 577 | std::regex spiPattern("((spi)[0-9]+)(\\/)"); |
| 578 | std::string spiBus{}; |
| 579 | std::smatch match; |
| 580 | if (std::regex_search(file, match, spiPattern)) |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 581 | { |
| 582 | spiBus = match.str(1); |
| 583 | } |
| 584 | else |
| 585 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 586 | std::cerr << "The given udev path < " << file |
| 587 | << " > doesn't match the required pattern. Skipping VPD " |
| 588 | "collection." |
| 589 | << std::endl; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 590 | exit(EXIT_SUCCESS); |
| 591 | } |
| 592 | // Forming the generic path |
jinuthomas | f457a3e | 2023-04-13 12:22:48 -0500 | [diff] [blame] | 593 | file = spiPathPrefix + driver + "/" + spiBus + ".0/eeprom"; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 594 | } |
| 595 | else |
| 596 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 597 | std::cerr << "\n The given EEPROM path < " << file |
| 598 | << " > is not valid. It's neither I2C nor " |
| 599 | "SPI path. Skipping VPD collection.." |
| 600 | << std::endl; |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 601 | exit(EXIT_SUCCESS); |
| 602 | } |
| 603 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 604 | std::string getBadVpdName(const std::string& file) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 605 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 606 | std::string badVpd = BAD_VPD_DIR; |
| 607 | if (file.find("i2c") != std::string::npos) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 608 | { |
| 609 | badVpd += "i2c-"; |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 610 | std::regex i2cPattern("(at24/)([0-9]+-[0-9]+)\\/"); |
| 611 | std::smatch match; |
| 612 | if (std::regex_search(file, match, i2cPattern)) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 613 | { |
| 614 | badVpd += match.str(2); |
| 615 | } |
| 616 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 617 | else if (file.find("spi") != std::string::npos) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 618 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 619 | std::regex spiPattern("((spi)[0-9]+)(.0)"); |
| 620 | std::smatch match; |
| 621 | if (std::regex_search(file, match, spiPattern)) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 622 | { |
| 623 | badVpd += match.str(1); |
| 624 | } |
| 625 | } |
| 626 | return badVpd; |
| 627 | } |
| 628 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 629 | void dumpBadVpd(const std::string& file, const Binary& vpdVector) |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 630 | { |
| 631 | fs::path badVpdDir = BAD_VPD_DIR; |
| 632 | fs::create_directory(badVpdDir); |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 633 | std::string badVpdPath = getBadVpdName(file); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 634 | if (fs::exists(badVpdPath)) |
| 635 | { |
| 636 | std::error_code ec; |
| 637 | fs::remove(badVpdPath, ec); |
| 638 | if (ec) // error code |
| 639 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 640 | std::string error = "Error removing the existing broken vpd in "; |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 641 | error += badVpdPath; |
| 642 | error += ". Error code : "; |
| 643 | error += ec.value(); |
| 644 | error += ". Error message : "; |
| 645 | error += ec.message(); |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 646 | throw std::runtime_error(error); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 647 | } |
| 648 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 649 | std::ofstream badVpdFileStream(badVpdPath, std::ofstream::binary); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 650 | if (!badVpdFileStream) |
| 651 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 652 | throw std::runtime_error( |
| 653 | "Failed to open bad vpd file path in /tmp/bad-vpd. " |
| 654 | "Unable to dump the broken/bad vpd file."); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 655 | } |
| 656 | badVpdFileStream.write(reinterpret_cast<const char*>(vpdVector.data()), |
| 657 | vpdVector.size()); |
| 658 | } |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 659 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 660 | const std::string getKwVal(const Parsed& vpdMap, const std::string& rec, |
| 661 | const std::string& kwd) |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 662 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 663 | std::string kwVal{}; |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 664 | |
| 665 | auto findRec = vpdMap.find(rec); |
| 666 | |
| 667 | // check if record is found in map we got by parser |
| 668 | if (findRec != vpdMap.end()) |
| 669 | { |
| 670 | auto findKwd = findRec->second.find(kwd); |
| 671 | |
| 672 | if (findKwd != findRec->second.end()) |
| 673 | { |
| 674 | kwVal = findKwd->second; |
| 675 | } |
Sunny Srivastava | 37992a6 | 2023-07-11 05:18:41 -0500 | [diff] [blame] | 676 | else |
| 677 | { |
| 678 | std::cout << "Keyword not found" << std::endl; |
| 679 | } |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | std::cerr << "Record not found" << std::endl; |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | return kwVal; |
| 687 | } |
| 688 | |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 689 | std::string hexString(const std::variant<Binary, std::string>& kw) |
Priyanga Ramasamy | c9ecf8e | 2021-10-08 02:28:52 -0500 | [diff] [blame] | 690 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 691 | std::string hexString; |
| 692 | std::visit( |
| 693 | [&hexString](auto&& kw) { |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 694 | std::stringstream ss; |
| 695 | std::string hexRep = "0x"; |
| 696 | ss << hexRep; |
| 697 | for (auto& kwVal : kw) |
| 698 | { |
| 699 | ss << std::setfill('0') << std::setw(2) << std::hex |
| 700 | << static_cast<int>(kwVal); |
| 701 | } |
| 702 | hexString = ss.str(); |
Patrick Williams | b7b352a | 2023-10-20 11:19:19 -0500 | [diff] [blame] | 703 | }, |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 704 | kw); |
| 705 | return hexString; |
Priyanga Ramasamy | c9ecf8e | 2021-10-08 02:28:52 -0500 | [diff] [blame] | 706 | } |
| 707 | |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 708 | std::string getPrintableValue(const std::variant<Binary, std::string>& kwVal) |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 709 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 710 | std::string kwString{}; |
| 711 | std::visit( |
| 712 | [&kwString](auto&& kwVal) { |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 713 | const auto it = |
| 714 | std::find_if(kwVal.begin(), kwVal.end(), |
| 715 | [](const auto& kw) { return !isprint(kw); }); |
| 716 | if (it != kwVal.end()) |
| 717 | { |
girik | 390fccb | 2023-05-02 07:42:47 -0500 | [diff] [blame] | 718 | kwString = hexString(kwVal); |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 719 | } |
| 720 | else |
| 721 | { |
| 722 | kwString = std::string(kwVal.begin(), kwVal.end()); |
| 723 | } |
Patrick Williams | b7b352a | 2023-10-20 11:19:19 -0500 | [diff] [blame] | 724 | }, |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 725 | kwVal); |
| 726 | return kwString; |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 729 | void executePostFailAction(const nlohmann::json& json, const std::string& file) |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 730 | { |
| 731 | if ((json["frus"][file].at(0)).find("postActionFail") == |
| 732 | json["frus"][file].at(0).end()) |
| 733 | { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | uint8_t pinValue = 0; |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 738 | std::string pinName; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 739 | |
| 740 | for (const auto& postAction : |
| 741 | (json["frus"][file].at(0))["postActionFail"].items()) |
| 742 | { |
| 743 | if (postAction.key() == "pin") |
| 744 | { |
| 745 | pinName = postAction.value(); |
| 746 | } |
| 747 | else if (postAction.key() == "value") |
| 748 | { |
| 749 | // Get the value to set |
| 750 | pinValue = postAction.value(); |
| 751 | } |
| 752 | } |
| 753 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 754 | std::cout << "Setting GPIO: " << pinName << " to " << (int)pinValue |
| 755 | << std::endl; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 756 | |
| 757 | try |
| 758 | { |
| 759 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 760 | |
| 761 | if (!outputLine) |
| 762 | { |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 763 | throw GpioException( |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 764 | "Couldn't find output line for the GPIO. Skipping " |
| 765 | "this GPIO action."); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 766 | } |
| 767 | outputLine.request( |
| 768 | {"Disable line", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 769 | pinValue); |
| 770 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 771 | catch (const std::exception& e) |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 772 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 773 | std::string i2cBusAddr; |
| 774 | std::string errMsg = e.what(); |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 775 | errMsg += "\nGPIO: " + pinName; |
| 776 | |
| 777 | if ((json["frus"][file].at(0)["postActionFail"].find( |
| 778 | "gpioI2CAddress")) != |
| 779 | json["frus"][file].at(0)["postActionFail"].end()) |
| 780 | { |
| 781 | i2cBusAddr = |
| 782 | json["frus"][file].at(0)["postActionFail"]["gpioI2CAddress"]; |
Sunny Srivastava | fdf9ff2 | 2022-06-15 11:15:54 -0500 | [diff] [blame] | 783 | errMsg += " i2cBusAddress: " + i2cBusAddr; |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 784 | } |
| 785 | |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 786 | throw GpioException(e.what()); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 787 | } |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 788 | |
| 789 | return; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 790 | } |
| 791 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 792 | std::optional<bool> isPresent(const nlohmann::json& json, |
| 793 | const std::string& file) |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 794 | |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 795 | { |
| 796 | if ((json["frus"][file].at(0)).find("presence") != |
| 797 | json["frus"][file].at(0).end()) |
| 798 | { |
| 799 | if (((json["frus"][file].at(0)["presence"]).find("pin") != |
| 800 | json["frus"][file].at(0)["presence"].end()) && |
| 801 | ((json["frus"][file].at(0)["presence"]).find("value") != |
| 802 | json["frus"][file].at(0)["presence"].end())) |
| 803 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 804 | std::string presPinName = |
| 805 | json["frus"][file].at(0)["presence"]["pin"]; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 806 | Byte presPinValue = json["frus"][file].at(0)["presence"]["value"]; |
| 807 | |
| 808 | try |
| 809 | { |
| 810 | gpiod::line presenceLine = gpiod::find_line(presPinName); |
| 811 | |
| 812 | if (!presenceLine) |
| 813 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 814 | std::cerr << "Couldn't find the presence line for - " |
| 815 | << presPinName << std::endl; |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 816 | |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 817 | throw GpioException( |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 818 | "Couldn't find the presence line for the " |
| 819 | "GPIO. Skipping this GPIO action."); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | presenceLine.request({"Read the presence line", |
| 823 | gpiod::line_request::DIRECTION_INPUT, 0}); |
| 824 | |
| 825 | Byte gpioData = presenceLine.get_value(); |
| 826 | |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 827 | return (gpioData == presPinValue); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 828 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 829 | catch (const std::exception& e) |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 830 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 831 | std::string i2cBusAddr; |
| 832 | std::string errMsg = e.what(); |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 833 | errMsg += " GPIO : " + presPinName; |
| 834 | |
| 835 | if ((json["frus"][file].at(0)["presence"]) |
| 836 | .find("gpioI2CAddress") != |
| 837 | json["frus"][file].at(0)["presence"].end()) |
| 838 | { |
| 839 | i2cBusAddr = |
| 840 | json["frus"][file].at(0)["presence"]["gpioI2CAddress"]; |
Sunny Srivastava | fdf9ff2 | 2022-06-15 11:15:54 -0500 | [diff] [blame] | 841 | errMsg += " i2cBusAddress: " + i2cBusAddr; |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 842 | } |
| 843 | |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 844 | // Take failure postAction |
| 845 | executePostFailAction(json, file); |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 846 | throw GpioException(errMsg); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 847 | } |
| 848 | } |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 849 | else |
| 850 | { |
| 851 | // missing required informations |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 852 | std::cerr |
| 853 | << "VPD inventory JSON missing basic informations of presence " |
| 854 | "for this FRU : [" |
| 855 | << file << "]. Executing executePostFailAction." << std::endl; |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 856 | |
| 857 | // Take failure postAction |
| 858 | executePostFailAction(json, file); |
| 859 | |
| 860 | return false; |
| 861 | } |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 862 | } |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 863 | return std::optional<bool>{}; |
| 864 | } |
| 865 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 866 | bool executePreAction(const nlohmann::json& json, const std::string& file) |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 867 | { |
| 868 | auto present = isPresent(json, file); |
| 869 | if (present && !present.value()) |
| 870 | { |
| 871 | executePostFailAction(json, file); |
| 872 | return false; |
| 873 | } |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 874 | |
| 875 | if ((json["frus"][file].at(0)).find("preAction") != |
| 876 | json["frus"][file].at(0).end()) |
| 877 | { |
| 878 | if (((json["frus"][file].at(0)["preAction"]).find("pin") != |
| 879 | json["frus"][file].at(0)["preAction"].end()) && |
| 880 | ((json["frus"][file].at(0)["preAction"]).find("value") != |
| 881 | json["frus"][file].at(0)["preAction"].end())) |
| 882 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 883 | std::string pinName = json["frus"][file].at(0)["preAction"]["pin"]; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 884 | // Get the value to set |
| 885 | Byte pinValue = json["frus"][file].at(0)["preAction"]["value"]; |
| 886 | |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 887 | std::cout << "Setting GPIO: " << pinName << " to " << (int)pinValue |
| 888 | << std::endl; |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 889 | try |
| 890 | { |
| 891 | gpiod::line outputLine = gpiod::find_line(pinName); |
| 892 | |
| 893 | if (!outputLine) |
| 894 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 895 | std::cerr << "Couldn't find the line for output pin - " |
| 896 | << pinName << std::endl; |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 897 | throw GpioException( |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 898 | "Couldn't find output line for the GPIO. " |
| 899 | "Skipping this GPIO action."); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 900 | } |
| 901 | outputLine.request({"FRU pre-action", |
| 902 | ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 903 | pinValue); |
| 904 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 905 | catch (const std::exception& e) |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 906 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 907 | std::string i2cBusAddr; |
| 908 | std::string errMsg = e.what(); |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 909 | errMsg += " GPIO : " + pinName; |
| 910 | |
| 911 | if ((json["frus"][file].at(0)["preAction"]) |
| 912 | .find("gpioI2CAddress") != |
| 913 | json["frus"][file].at(0)["preAction"].end()) |
| 914 | { |
| 915 | i2cBusAddr = |
| 916 | json["frus"][file].at(0)["preAction"]["gpioI2CAddress"]; |
Sunny Srivastava | fdf9ff2 | 2022-06-15 11:15:54 -0500 | [diff] [blame] | 917 | errMsg += " i2cBusAddress: " + i2cBusAddr; |
Alpana Kumari | 6bd095f | 2022-02-23 10:20:20 -0600 | [diff] [blame] | 918 | } |
| 919 | |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 920 | // Take failure postAction |
| 921 | executePostFailAction(json, file); |
Sunny Srivastava | a2ddc96 | 2022-06-29 08:53:16 -0500 | [diff] [blame] | 922 | throw GpioException(errMsg); |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 923 | } |
| 924 | } |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 925 | else |
| 926 | { |
| 927 | // missing required informations |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 928 | std::cerr |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 929 | << "VPD inventory JSON missing basic informations of preAction " |
| 930 | "for this FRU : [" |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 931 | << file << "]. Executing executePostFailAction." << std::endl; |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 932 | |
| 933 | // Take failure postAction |
| 934 | executePostFailAction(json, file); |
Alpana Kumari | 40d1c19 | 2022-03-09 21:16:02 -0600 | [diff] [blame] | 935 | return false; |
| 936 | } |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 937 | } |
| 938 | return true; |
| 939 | } |
| 940 | |
Priyanga Ramasamy | aa8a893 | 2022-01-27 09:12:41 -0600 | [diff] [blame] | 941 | void insertOrMerge(inventory::InterfaceMap& map, |
| 942 | const inventory::Interface& interface, |
| 943 | inventory::PropertyMap&& property) |
| 944 | { |
| 945 | if (map.find(interface) != map.end()) |
| 946 | { |
| 947 | auto& prop = map.at(interface); |
| 948 | prop.insert(property.begin(), property.end()); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | map.emplace(interface, property); |
| 953 | } |
| 954 | } |
Santosh Puranik | f2d3b53 | 2022-04-19 06:44:07 -0500 | [diff] [blame] | 955 | |
| 956 | BIOSAttrValueType readBIOSAttribute(const std::string& attrName) |
| 957 | { |
| 958 | std::tuple<std::string, BIOSAttrValueType, BIOSAttrValueType> attrVal; |
| 959 | auto bus = sdbusplus::bus::new_default(); |
| 960 | auto method = bus.new_method_call( |
| 961 | "xyz.openbmc_project.BIOSConfigManager", |
| 962 | "/xyz/openbmc_project/bios_config/manager", |
| 963 | "xyz.openbmc_project.BIOSConfig.Manager", "GetAttribute"); |
| 964 | method.append(attrName); |
| 965 | try |
| 966 | { |
| 967 | auto result = bus.call(method); |
| 968 | result.read(std::get<0>(attrVal), std::get<1>(attrVal), |
| 969 | std::get<2>(attrVal)); |
| 970 | } |
| 971 | catch (const sdbusplus::exception::SdBusError& e) |
| 972 | { |
| 973 | std::cerr << "Failed to read BIOS Attribute: " << attrName << std::endl; |
| 974 | std::cerr << e.what() << std::endl; |
| 975 | } |
| 976 | return std::get<1>(attrVal); |
| 977 | } |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 978 | |
| 979 | std::string getPowerState() |
| 980 | { |
| 981 | // TODO: How do we handle multiple chassis? |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 982 | std::string powerState{}; |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 983 | auto bus = sdbusplus::bus::new_default(); |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 984 | auto properties = bus.new_method_call("xyz.openbmc_project.State.Chassis", |
| 985 | "/xyz/openbmc_project/state/chassis0", |
| 986 | "org.freedesktop.DBus.Properties", |
| 987 | "Get"); |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 988 | properties.append("xyz.openbmc_project.State.Chassis"); |
| 989 | properties.append("CurrentPowerState"); |
| 990 | auto result = bus.call(properties); |
| 991 | if (!result.is_method_error()) |
| 992 | { |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 993 | std::variant<std::string> val; |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 994 | result.read(val); |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 995 | if (auto pVal = std::get_if<std::string>(&val)) |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 996 | { |
| 997 | powerState = *pVal; |
| 998 | } |
| 999 | } |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 1000 | std::cout << "Power state is: " << powerState << std::endl; |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 1001 | return powerState; |
| 1002 | } |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 1003 | |
| 1004 | Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file) |
| 1005 | { |
| 1006 | uint32_t offset = 0; |
| 1007 | // check if offset present? |
| 1008 | for (const auto& item : js["frus"][file]) |
| 1009 | { |
| 1010 | if (item.find("offset") != item.end()) |
| 1011 | { |
| 1012 | offset = item["offset"]; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // TODO: Figure out a better way to get max possible VPD size. |
| 1017 | auto maxVPDSize = std::min(std::filesystem::file_size(file), |
| 1018 | static_cast<uintmax_t>(65504)); |
| 1019 | |
| 1020 | Binary vpdVector; |
| 1021 | vpdVector.resize(maxVPDSize); |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 1022 | std::ifstream vpdFile; |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 1023 | vpdFile.exceptions(std::ifstream::badbit | std::ifstream::failbit); |
| 1024 | try |
| 1025 | { |
Jinu Joy Thomas | e2b0383 | 2023-08-22 19:51:31 +0530 | [diff] [blame^] | 1026 | vpdFile.open(file, std::ios::binary | std::ios::in); |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 1027 | vpdFile.seekg(offset, std::ios_base::cur); |
| 1028 | vpdFile.read(reinterpret_cast<char*>(&vpdVector[0]), maxVPDSize); |
| 1029 | vpdVector.resize(vpdFile.gcount()); |
| 1030 | } |
| 1031 | catch (const std::ifstream::failure& fail) |
| 1032 | { |
| 1033 | std::cerr << "Exception in file handling [" << file |
| 1034 | << "] error : " << fail.what(); |
| 1035 | std::cerr << "EEPROM file size =" << std::filesystem::file_size(file) |
| 1036 | << std::endl; |
| 1037 | std::cerr << "Stream file size = " << vpdFile.gcount() << std::endl; |
| 1038 | std::cerr << " Vector size" << vpdVector.size() << std::endl; |
| 1039 | throw; |
| 1040 | } |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 1041 | |
Jinu Joy Thomas | e2b0383 | 2023-08-22 19:51:31 +0530 | [diff] [blame^] | 1042 | // Make sure we reset the EEPROM pointer to a "safe" location if it was |
| 1043 | // a DDIMM SPD that we just read. |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 1044 | for (const auto& item : js["frus"][file]) |
| 1045 | { |
| 1046 | if (item.find("extraInterfaces") != item.end()) |
| 1047 | { |
| 1048 | if (item["extraInterfaces"].find( |
| 1049 | "xyz.openbmc_project.Inventory.Item.Dimm") != |
| 1050 | item["extraInterfaces"].end()) |
| 1051 | { |
Jinu Joy Thomas | e2b0383 | 2023-08-22 19:51:31 +0530 | [diff] [blame^] | 1052 | // check added here for DDIMM only workarround |
| 1053 | vpdType dimmType = vpdTypeCheck(vpdVector); |
| 1054 | if (dimmType == constants::DDR4_DDIMM_MEMORY_VPD || |
| 1055 | dimmType == constants::DDR5_DDIMM_MEMORY_VPD) |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 1056 | { |
Jinu Joy Thomas | e2b0383 | 2023-08-22 19:51:31 +0530 | [diff] [blame^] | 1057 | try |
| 1058 | { |
| 1059 | // moves the EEPROM pointer to 2048 'th byte. |
| 1060 | vpdFile.seekg(2047, std::ios::beg); |
| 1061 | // Read that byte and discard - to affirm the move |
| 1062 | // operation. |
| 1063 | char ch; |
| 1064 | vpdFile.read(&ch, sizeof(ch)); |
| 1065 | } |
| 1066 | catch (const std::ifstream::failure& fail) |
| 1067 | { |
| 1068 | std::cerr << "Exception in file handling [" << file |
| 1069 | << "] error : " << fail.what(); |
| 1070 | std::cerr << "Stream file size = " << vpdFile.gcount() |
| 1071 | << std::endl; |
| 1072 | throw; |
| 1073 | } |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 1074 | } |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return vpdVector; |
| 1081 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 1082 | |
| 1083 | std::string getDbusNameForThisKw(const std::string& keyword) |
| 1084 | { |
| 1085 | if (keyword[0] == constants::POUND_KW) |
| 1086 | { |
| 1087 | return (std::string(constants::POUND_KW_PREFIX) + keyword[1]); |
| 1088 | } |
| 1089 | else if (isdigit(keyword[0])) |
| 1090 | { |
| 1091 | return (std::string(constants::NUMERIC_KW_PREFIX) + keyword); |
| 1092 | } |
| 1093 | return keyword; |
| 1094 | } |
| 1095 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 1096 | } // namespace vpd |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 1097 | } // namespace openpower |