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