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