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