Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 3 | #include "const.hpp" |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 4 | #include "store.hpp" |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 5 | #include "types.hpp" |
| 6 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 7 | #include <iostream> |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 9 | #include <optional> |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 12 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 13 | namespace openpower |
| 14 | { |
| 15 | namespace vpd |
| 16 | { |
SunnySrivastava1984 | 945a02d | 2020-05-06 01:55:41 -0500 | [diff] [blame] | 17 | |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 18 | // Map to hold record, kwd pair which can be re-stored at standby. |
| 19 | // The list of keywords for VSYS record is as per the S0 system. Should |
| 20 | // be updated for another type of systems |
| 21 | static const std::unordered_map<std::string, std::vector<std::string>> |
| 22 | svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN", "RG"}}, |
| 23 | {"VCEN", {"FC", "SE"}}, |
| 24 | {"LXR0", {"LX"}}, |
| 25 | {"UTIL", {"D0"}}}; |
| 26 | |
Santosh Puranik | bd011b2 | 2020-01-23 04:05:25 -0600 | [diff] [blame] | 27 | /** @brief Return the hex representation of the incoming byte |
| 28 | * |
| 29 | * @param [in] c - The input byte |
| 30 | * @returns The hex representation of the byte as a character. |
| 31 | */ |
| 32 | constexpr auto toHex(size_t c) |
| 33 | { |
| 34 | constexpr auto map = "0123456789abcdef"; |
| 35 | return map[c]; |
| 36 | } |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 37 | |
| 38 | namespace inventory |
| 39 | { |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 40 | /** @brief API to obtain a dictionary of path -> services |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 41 | * where path is in subtree and services is of the type |
| 42 | * returned by the GetObject method. |
| 43 | * |
| 44 | * @param [in] root - Root path for object subtree |
| 45 | * @param [in] depth - Maximum subtree depth required |
| 46 | * @param [in] interfaces - Array to interfaces for which |
| 47 | * result is required. |
| 48 | * @return A dictionary of Path -> services |
| 49 | */ |
| 50 | MapperResponse |
| 51 | getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth, |
| 52 | const std::vector<std::string>& interfaces); |
| 53 | |
Deepak Kodihalli | 7679449 | 2017-02-16 23:48:18 -0600 | [diff] [blame] | 54 | } // namespace inventory |
| 55 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 56 | /**@brief This API reads 2 Bytes of data and swap the read data |
| 57 | * @param[in] iterator- Pointer pointing to the data to be read |
| 58 | * @return returns 2 Byte data read at the given pointer |
| 59 | */ |
| 60 | openpower::vpd::constants::LE2ByteData |
| 61 | readUInt16LE(Binary::const_iterator iterator); |
| 62 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 63 | /** @brief Encodes a keyword for D-Bus. |
| 64 | * @param[in] kw - kwd data in string format |
| 65 | * @param[in] encoding - required for kwd data |
| 66 | */ |
| 67 | string encodeKeyword(const string& kw, const string& encoding); |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 68 | |
| 69 | /** @brief Reads a property from the inventory manager given object path, |
| 70 | * intreface and property. |
| 71 | * @param[in] obj - object path |
| 72 | * @param[in] inf - interface |
| 73 | * @param[in] prop - property whose value is fetched |
| 74 | * @return [out] - value of the property |
| 75 | */ |
| 76 | string readBusProperty(const string& obj, const string& inf, |
| 77 | const string& prop); |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 78 | |
Priyanga Ramasamy | f1449cd | 2022-07-13 04:28:30 -0500 | [diff] [blame] | 79 | /** @brief A templated function to read D-Bus properties. |
| 80 | * |
| 81 | * @param[in] service - Service path |
| 82 | * @param[in] object - object path |
| 83 | * @param[in] inf - interface |
| 84 | * @param[in] prop - property whose value is fetched |
| 85 | * @return The property value of its own type. |
| 86 | */ |
| 87 | template <typename T> |
| 88 | T readDBusProperty(const string& service, const string& object, |
| 89 | const string& inf, const string& prop) |
| 90 | { |
| 91 | T retVal{}; |
| 92 | try |
| 93 | { |
| 94 | auto bus = sdbusplus::bus::new_default(); |
| 95 | auto properties = |
| 96 | bus.new_method_call(service.c_str(), object.c_str(), |
| 97 | "org.freedesktop.DBus.Properties", "Get"); |
| 98 | properties.append(inf); |
| 99 | properties.append(prop); |
| 100 | auto result = bus.call(properties); |
| 101 | result.read(retVal); |
| 102 | } |
| 103 | catch (const sdbusplus::exception::SdBusError& e) |
| 104 | { |
| 105 | std::cerr << e.what(); |
| 106 | } |
| 107 | return retVal; |
| 108 | } |
| 109 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame^] | 110 | /** @brief A templated method to get all D-Bus properties |
| 111 | * |
| 112 | * @param[in] service - Service path |
| 113 | * @param[in] object - Object path |
| 114 | * @param[in] inf - Interface |
| 115 | * |
| 116 | * @return All properties under the given interface. |
| 117 | */ |
| 118 | template <typename T> |
| 119 | T getAllDBusProperty(const std::string& service, const std::string& object, |
| 120 | const std::string& inf) |
| 121 | { |
| 122 | T retVal{}; |
| 123 | try |
| 124 | { |
| 125 | auto bus = sdbusplus::bus::new_default(); |
| 126 | auto allProperties = |
| 127 | bus.new_method_call(service.c_str(), object.c_str(), |
| 128 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 129 | allProperties.append(inf); |
| 130 | |
| 131 | auto result = bus.call(allProperties); |
| 132 | result.read(retVal); |
| 133 | } |
| 134 | catch (const sdbusplus::exception::SdBusError& e) |
| 135 | { |
| 136 | std::cerr << e.what(); |
| 137 | } |
| 138 | return retVal; |
| 139 | } |
| 140 | |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 141 | /** |
| 142 | * @brief API to create PEL entry |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 143 | * @param[in] additionalData - Map holding the additional data |
| 144 | * @param[in] sev - Severity |
| 145 | * @param[in] errIntf - error interface |
SunnySrivastava1984 | a20be8e | 2020-08-26 02:00:50 -0500 | [diff] [blame] | 146 | */ |
| 147 | void createPEL(const std::map<std::string, std::string>& additionalData, |
Sunny Srivastava | 0746eee | 2021-03-22 13:36:54 -0500 | [diff] [blame] | 148 | const constants::PelSeverity& sev, const std::string& errIntf); |
SunnySrivastava1984 | 9094d4f | 2020-08-05 09:32:29 -0500 | [diff] [blame] | 149 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 150 | /** |
| 151 | * @brief getVpdFilePath |
| 152 | * Get vpd file path corresponding to the given object path. |
| 153 | * @param[in] - json file path |
| 154 | * @param[in] - Object path |
| 155 | * @return - Vpd file path |
| 156 | */ |
| 157 | inventory::VPDfilepath getVpdFilePath(const string& jsonFile, |
| 158 | const std::string& ObjPath); |
| 159 | |
| 160 | /** |
| 161 | * @brief isPathInJson |
| 162 | * API which checks for the presence of the given eeprom path in the given json. |
| 163 | * @param[in] - eepromPath |
| 164 | * @return - true if the eeprom is present in the json; false otherwise |
| 165 | */ |
| 166 | bool isPathInJson(const std::string& eepromPath); |
| 167 | |
| 168 | /** |
| 169 | * @brief isRecKwInDbusJson |
| 170 | * API which checks whether the given keyword under the given record is to be |
| 171 | * published on dbus or not. Checks against the keywords present in |
| 172 | * dbus_property.json. |
| 173 | * @param[in] - record name |
| 174 | * @param[in] - keyword name |
| 175 | * @return - true if the record-keyword pair is present in dbus_property.json; |
| 176 | * false otherwise. |
| 177 | */ |
| 178 | bool isRecKwInDbusJson(const std::string& record, const std::string& keyword); |
| 179 | |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 180 | /** |
| 181 | * @brief Check the type of VPD. |
| 182 | * |
| 183 | * Checks the type of vpd based on the start tag. |
| 184 | * @param[in] vector - Vpd data in vector format |
| 185 | * |
| 186 | * @return enum of type vpdType |
| 187 | */ |
| 188 | constants::vpdType vpdTypeCheck(const Binary& vector); |
| 189 | |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 190 | /* |
| 191 | * @brief This method does nothing. Just an empty function to return null |
| 192 | * at the end of variadic template args |
| 193 | */ |
| 194 | inline string getCommand() |
| 195 | { |
| 196 | return ""; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @brief This function to arrange all arguments to make commandy |
| 201 | * @param[in] arguments to create the command |
| 202 | * @return cmd - command string |
| 203 | */ |
| 204 | template <typename T, typename... Types> |
| 205 | inline string getCommand(T arg1, Types... args) |
| 206 | { |
| 207 | string cmd = " " + arg1 + getCommand(args...); |
| 208 | |
| 209 | return cmd; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @brief This API takes arguments, creates a shell command line and executes |
| 214 | * them. |
| 215 | * @param[in] arguments for command |
| 216 | * @returns output of that command |
| 217 | */ |
| 218 | template <typename T, typename... Types> |
| 219 | inline vector<string> executeCmd(T&& path, Types... args) |
| 220 | { |
| 221 | vector<string> stdOutput; |
| 222 | array<char, 128> buffer; |
| 223 | |
| 224 | string cmd = path + getCommand(args...); |
| 225 | |
| 226 | unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); |
| 227 | if (!pipe) |
| 228 | { |
| 229 | throw runtime_error("popen() failed!"); |
| 230 | } |
| 231 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 232 | { |
| 233 | stdOutput.emplace_back(buffer.data()); |
| 234 | } |
| 235 | |
| 236 | return stdOutput; |
| 237 | } |
| 238 | |
Alpana Kumari | f05effd | 2021-04-07 07:32:53 -0500 | [diff] [blame] | 239 | /** @brief This API checks for IM and HW keywords, and based |
| 240 | * on these values decides which system json to be used. |
| 241 | * @param[in] vpdMap - parsed vpd |
| 242 | * @returns System json path |
| 243 | */ |
| 244 | string getSystemsJson(const Parsed& vpdMap); |
| 245 | |
| 246 | /** @brief Reads HW Keyword from the vpd |
| 247 | * @param[in] vpdMap - parsed vpd |
| 248 | * @returns value of HW Keyword |
| 249 | */ |
| 250 | const string getHW(const Parsed& vpdMap); |
| 251 | |
| 252 | /** @brief Reads IM Keyword from the vpd |
| 253 | * @param[in] vpdMap - parsed vpd |
| 254 | * @returns value of IM Keyword |
| 255 | */ |
| 256 | const string getIM(const Parsed& vpdMap); |
| 257 | |
PriyangaRamasamy | 647868e | 2020-09-08 17:03:19 +0530 | [diff] [blame] | 258 | /** @brief Translate udev event generated path to a generic /sys/bus eeprom path |
| 259 | * @param[io] file - path generated from udev event. |
| 260 | */ |
| 261 | void udevToGenericPath(string& file); |
PriyangaRamasamy | c2fe40f | 2021-03-02 06:27:33 -0600 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * @brief API to generate a vpd name in some pattern. |
| 265 | * This vpd-name denotes name of the bad vpd file. |
| 266 | * For i2c eeproms - the pattern of the vpd-name will be |
| 267 | * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the |
| 268 | * vpd-name will be spi-<spi-number>. |
| 269 | * |
| 270 | * @param[in] file - file path of the vpd |
| 271 | * @return the vpd-name. |
| 272 | */ |
| 273 | string getBadVpdName(const string& file); |
| 274 | |
| 275 | /** |
| 276 | * @brief API which dumps the broken/bad vpd in a directory |
| 277 | * When the vpd is bad, this api places the bad vpd file inside |
| 278 | * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user |
| 279 | * initiated BMC dump. |
| 280 | * |
| 281 | * @param[in] file - bad vpd file path |
| 282 | * @param[in] vpdVector - bad vpd vector |
| 283 | */ |
| 284 | void dumpBadVpd(const std::string& file, const Binary& vpdVector); |
alpana07 | 7ce6872 | 2021-07-25 13:23:59 -0500 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * @brief This function fetches the value for given keyword in the given record |
| 288 | * from vpd data and returns this value. |
| 289 | * |
| 290 | * @param[in] vpdMap - vpd to find out the data |
| 291 | * @param[in] rec - Record under which desired keyword exists |
| 292 | * @param[in] kwd - keyword to read the data from |
| 293 | * |
| 294 | * @returns keyword value if record/keyword combination found |
| 295 | * empty string if record or keyword is not found. |
| 296 | */ |
| 297 | const string getKwVal(const Parsed& vpdMap, const string& rec, |
| 298 | const string& kwd); |
| 299 | |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 300 | /** @brief This creates a complete command using all it's input parameters, |
| 301 | * to bind or unbind the driver. |
| 302 | * @param[in] devNameAddr - device address on that bus |
| 303 | * @param[in] busType - i2c, spi |
| 304 | * @param[in] driverType - type of driver like at24 |
| 305 | * @param[in] bindOrUnbind - either bind or unbind |
| 306 | * @returns Command to bind or unbind the driver. |
| 307 | */ |
| 308 | inline string createBindUnbindDriverCmnd(const string& devNameAddr, |
| 309 | const string& busType, |
| 310 | const string& driverType, |
| 311 | const string& bindOrUnbind) |
| 312 | { |
| 313 | return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" + |
| 314 | driverType + "/" + bindOrUnbind); |
| 315 | } |
| 316 | |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 317 | /** |
| 318 | * @brief Get Printable Value |
| 319 | * |
| 320 | * Checks if the vector value has non printable characters. |
| 321 | * Returns hex value if non printable char is found else |
| 322 | * returns ascii value. |
| 323 | * |
| 324 | * @param[in] vector - Reference of the Binary vector |
| 325 | * @return printable value - either in hex or in ascii. |
| 326 | */ |
Priyanga Ramasamy | c9ecf8e | 2021-10-08 02:28:52 -0500 | [diff] [blame] | 327 | string getPrintableValue(const Binary& vec); |
| 328 | |
| 329 | /** |
| 330 | * @brief Convert byte array to hex string. |
| 331 | * @param[in] vec - byte array |
| 332 | * @return hexadecimal string of bytes. |
| 333 | */ |
| 334 | string byteArrayToHexString(const Binary& vec); |
Priyanga Ramasamy | 0243493 | 2021-10-07 16:26:05 -0500 | [diff] [blame] | 335 | |
Priyanga Ramasamy | aa8a893 | 2022-01-27 09:12:41 -0600 | [diff] [blame] | 336 | /** |
Santosh Puranik | 53b38ed | 2022-04-10 23:15:22 +0530 | [diff] [blame] | 337 | * @brief Return presence of the FRU. |
| 338 | * |
| 339 | * This API returns the presence information of the FRU corresponding to the |
| 340 | * given EEPROM. If the JSON contains no information about presence detect, this |
| 341 | * will return an empty optional. Else it will get the presence GPIO information |
| 342 | * from the JSON and return the appropriate present status. |
| 343 | * In case of GPIO find/read errors, it will return false. |
| 344 | * |
| 345 | * @param[in] json - The VPD JSON |
| 346 | * @param[in] file - EEPROM file path |
| 347 | * @return Empty optional if there is no presence info. Else returns presence |
| 348 | * based on the GPIO read. |
| 349 | */ |
| 350 | std::optional<bool> isPresent(const nlohmann::json& json, const string& file); |
| 351 | |
| 352 | /** |
| 353 | * @brief Performs any pre-action needed to get the FRU setup for |
| 354 | * collection. |
Alpana Kumari | 735dee9 | 2022-03-25 01:24:40 -0500 | [diff] [blame] | 355 | * |
| 356 | * @param[in] json - json object |
| 357 | * @param[in] file - eeprom file path |
| 358 | * @return - success or failure |
| 359 | */ |
| 360 | bool executePreAction(const nlohmann::json& json, const string& file); |
| 361 | |
| 362 | /** |
| 363 | * @brief This API will be called at the end of VPD collection to perform any |
| 364 | * post actions. |
| 365 | * |
| 366 | * @param[in] json - json object |
| 367 | * @param[in] file - eeprom file path |
| 368 | */ |
| 369 | void executePostFailAction(const nlohmann::json& json, const string& file); |
| 370 | |
| 371 | /** |
Priyanga Ramasamy | aa8a893 | 2022-01-27 09:12:41 -0600 | [diff] [blame] | 372 | * @brief Helper function to insert or merge in map. |
| 373 | * |
| 374 | * This method checks in the given inventory::InterfaceMap if the given |
| 375 | * interface key is existing or not. If the interface key already exists, given |
| 376 | * property map is inserted into it. If the key does'nt exist then given |
| 377 | * interface and property map pair is newly created. If the property present in |
| 378 | * propertymap already exist in the InterfaceMap, then the new property value is |
| 379 | * ignored. |
| 380 | * |
| 381 | * @param[in,out] map - map object of type inventory::InterfaceMap only. |
| 382 | * @param[in] interface - Interface name. |
| 383 | * @param[in] property - new property map that needs to be emplaced. |
| 384 | */ |
| 385 | void insertOrMerge(inventory::InterfaceMap& map, |
| 386 | const inventory::Interface& interface, |
| 387 | inventory::PropertyMap&& property); |
| 388 | |
Santosh Puranik | f2d3b53 | 2022-04-19 06:44:07 -0500 | [diff] [blame] | 389 | /** |
| 390 | * @brief Utility API to set a D-Bus property |
| 391 | * |
| 392 | * This calls org.freedesktop.DBus.Properties;Set method with the supplied |
| 393 | * arguments |
| 394 | * |
| 395 | * @tparam T Template type of the D-Bus property |
| 396 | * @param service[in] - The D-Bus service name. |
| 397 | * @param object[in] - The D-Bus object on which the property is to be set. |
| 398 | * @param interface[in] - The D-Bus interface to which the property belongs. |
| 399 | * @param propertyName[in] - The name of the property to set. |
| 400 | * @param propertyValue[in] - The value of the property. |
| 401 | */ |
| 402 | template <typename T> |
| 403 | void setBusProperty(const std::string& service, const std::string& object, |
| 404 | const std::string& interface, |
| 405 | const std::string& propertyName, |
| 406 | const std::variant<T>& propertyValue) |
| 407 | { |
| 408 | try |
| 409 | { |
| 410 | auto bus = sdbusplus::bus::new_default(); |
| 411 | auto method = |
| 412 | bus.new_method_call(service.c_str(), object.c_str(), |
| 413 | "org.freedesktop.DBus.Properties", "Set"); |
| 414 | method.append(interface); |
| 415 | method.append(propertyName); |
| 416 | method.append(propertyValue); |
| 417 | |
| 418 | bus.call(method); |
| 419 | } |
| 420 | catch (const sdbusplus::exception::SdBusError& e) |
| 421 | { |
| 422 | std::cerr << e.what() << std::endl; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * @brief Reads BIOS Attribute by name. |
| 428 | * |
| 429 | * @param attrName[in] - The BIOS attribute name. |
| 430 | * @return std::variant<int64_t, std::string> - The BIOS attribute value. |
| 431 | */ |
| 432 | std::variant<int64_t, std::string> |
| 433 | readBIOSAttribute(const std::string& attrName); |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * @brief Returns the power state for chassis0 |
| 437 | * @return The chassis power state. |
| 438 | */ |
| 439 | std::string getPowerState(); |
Santosh Puranik | 6b2b537 | 2022-06-02 20:49:02 +0530 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * @brief Reads VPD from the supplied EEPROM |
| 443 | * |
| 444 | * This function reads the given VPD EEPROM file and returns its contents as a |
| 445 | * byte array. It handles any offsets into the file that need to be taken care |
| 446 | * of by looking up the VPD JSON for a possible offset key. |
| 447 | * |
| 448 | * @param js[in] - The VPD JSON Object |
| 449 | * @param file[in] - The path to the EEPROM to read |
| 450 | * @return A byte array containing the raw VPD. |
| 451 | */ |
| 452 | Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file); |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 453 | } // namespace vpd |
Santosh Puranik | f2d3b53 | 2022-04-19 06:44:07 -0500 | [diff] [blame] | 454 | } // namespace openpower |