PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 3 | #include "editor_impl.hpp" |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 4 | #include "ibm_vpd_utils.hpp" |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 5 | #include "types.hpp" |
| 6 | |
| 7 | #include <nlohmann/json.hpp> |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 8 | |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 9 | #include <string> |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 10 | |
| 11 | using json = nlohmann::json; |
| 12 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 13 | // <S.no, Record, Keyword, D-Bus value, HW value, Data mismatch> |
| 14 | using SystemCriticalData = |
| 15 | std::vector<std::tuple<int, std::string, std::string, std::string, |
| 16 | std::string, std::string>>; |
| 17 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 18 | class VpdTool |
| 19 | { |
| 20 | private: |
| 21 | const std::string fruPath; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 22 | const std::string recordName; |
| 23 | const std::string keyword; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 24 | const std::string value; |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 25 | bool objFound = true; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 26 | SystemCriticalData recKwData; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 27 | |
Alpana Kumari | b6965f1 | 2020-06-01 00:32:21 -0500 | [diff] [blame] | 28 | // Store Type of FRU |
| 29 | std::string fruType; |
| 30 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 31 | /** |
| 32 | * @brief Debugger |
| 33 | * Displays the output in JSON. |
| 34 | * |
| 35 | * @param[in] output - json output to be displayed |
| 36 | */ |
| 37 | void debugger(json output); |
| 38 | |
| 39 | /** |
| 40 | * @brief make Dbus Call |
| 41 | * |
| 42 | * @param[in] objectName - dbus Object |
| 43 | * @param[in] interface - dbus Interface |
| 44 | * @param[in] kw - keyword under the interface |
| 45 | * |
| 46 | * @return dbus call response |
| 47 | */ |
| 48 | auto makeDBusCall(const std::string& objectName, |
| 49 | const std::string& interface, const std::string& kw); |
| 50 | |
| 51 | /** |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 52 | * @brief Get VINI properties |
| 53 | * Making a dbus call for properties [SN, PN, CC, FN, DR] |
| 54 | * under VINI interface. |
| 55 | * |
| 56 | * @param[in] invPath - Value of inventory Path |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 57 | * |
| 58 | * @return json output which gives the properties under invPath's VINI |
| 59 | * interface |
| 60 | */ |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 61 | json getVINIProperties(std::string invPath); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * @brief Get ExtraInterface Properties |
| 65 | * Making a dbus call for those properties under extraInterfaces. |
| 66 | * |
| 67 | * @param[in] invPath - Value of inventory path |
| 68 | * @param[in] extraInterface - One of the invPath's extraInterfaces whose |
| 69 | * value is not null |
| 70 | * @param[in] prop - All properties of the extraInterface. |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 71 | * @param[out] output - output json which has the properties under invPath's |
| 72 | * extra interface. |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 73 | */ |
Priyanga Ramasamy | e008432 | 2022-09-27 06:28:33 -0500 | [diff] [blame] | 74 | void getExtraInterfaceProperties(const std::string& invPath, |
| 75 | const std::string& extraInterface, |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 76 | const json& prop, json& output); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * @brief Interface Decider |
| 80 | * Decides whether to make the dbus call for |
| 81 | * getting properites from extraInterface or from |
| 82 | * VINI interface, depending on the value of |
| 83 | * extraInterfaces object in the inventory json. |
| 84 | * |
| 85 | * @param[in] itemEEPROM - holds the reference of one of the EEPROM objects. |
| 86 | * |
| 87 | * @return json output for one of the EEPROM objects. |
| 88 | */ |
| 89 | json interfaceDecider(json& itemEEPROM); |
| 90 | |
| 91 | /** |
| 92 | * @brief Parse Inventory JSON |
| 93 | * Parses the complete inventory json and depending upon |
| 94 | * the user option makes the dbuscall for the frus |
| 95 | * via interfaceDecider function. |
| 96 | * |
| 97 | * @param[in] jsObject - Inventory json object |
| 98 | * @param[in] flag - flag which tells about the user option(either |
| 99 | * dumpInventory or dumpObject) |
| 100 | * @param[in] fruPath - fruPath is empty for dumpInventory option and holds |
| 101 | * valid fruPath for dumpObject option. |
| 102 | * |
| 103 | * @return output json |
| 104 | */ |
| 105 | json parseInvJson(const json& jsObject, char flag, std::string fruPath); |
| 106 | |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 107 | /** |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 108 | * @brief eraseInventoryPath |
| 109 | * Remove the INVENTORY_PATH - "/xyz/openbmc_project/inventory" |
| 110 | * for code convenience. |
| 111 | * |
| 112 | * @param[out] fru - Reference to the fru path whose INVENTORY_PATH is |
| 113 | * stripped off. |
| 114 | */ |
| 115 | void eraseInventoryPath(std::string& fru); |
| 116 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 117 | /** |
| 118 | * @brief printReturnCode |
| 119 | * Prints the return code of the program in console output, whenever |
| 120 | * the program fails to exit successfully. |
| 121 | * |
| 122 | * @param[in] returnCode - return code of the program. |
| 123 | */ |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 124 | void printReturnCode(int returnCode); |
| 125 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 126 | /** |
| 127 | * @brief Convert hex/ascii values to Binary |
| 128 | * @param[in] - value in hex/ascii. |
| 129 | * @param[out] - value in binary. |
| 130 | */ |
| 131 | openpower::vpd::Binary toBinary(const std::string& value); |
| 132 | |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 133 | /** |
| 134 | * @brief Get the json which has Present property value of the given fru. |
| 135 | * @param[in] invPath - inventory path of the fru. |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 136 | * @return output json which has the Present property value. |
| 137 | */ |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 138 | json getPresentPropJson(const std::string& invPath); |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 139 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 140 | /** |
| 141 | * @brief Parse through the options to fix system VPD |
| 142 | * |
| 143 | * @param[in] json - Inventory JSON |
| 144 | */ |
| 145 | void parseSVPDOptions(const nlohmann::json& json); |
| 146 | |
| 147 | /** |
| 148 | * @brief List of user options that can be used to fix system VPD keywords. |
| 149 | */ |
| 150 | enum UserOption |
| 151 | { |
| 152 | EXIT = 0, |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 153 | BACKUP_DATA_FOR_ALL = 1, |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 154 | SYSTEM_BACKPLANE_DATA_FOR_ALL = 2, |
| 155 | MORE_OPTIONS = 3, |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 156 | BACKUP_DATA_FOR_CURRENT = 4, |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 157 | SYSTEM_BACKPLANE_DATA_FOR_CURRENT = 5, |
| 158 | NEW_VALUE_ON_BOTH = 6, |
| 159 | SKIP_CURRENT = 7 |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * @brief Print options to fix system VPD. |
| 164 | * @param[in] option - Option to use. |
| 165 | */ |
| 166 | void printFixSystemVPDOption(UserOption option); |
| 167 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 168 | /** |
| 169 | * @brief Get System VPD data stored in cache |
| 170 | * |
| 171 | * @param[in] svpdBusData - Map of system VPD record data. |
| 172 | */ |
| 173 | void getSystemDataFromCache( |
| 174 | openpower::vpd::inventory::IntfPropMap& svpdBusData); |
| 175 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 176 | /** |
| 177 | * @brief Get data from file and store in binary format |
| 178 | * |
| 179 | * @param[out] data - The resulting binary data |
| 180 | * |
| 181 | * @return If operation is success return true, else on failure return |
| 182 | * false. |
| 183 | */ |
| 184 | bool fileToVector(openpower::vpd::Binary& data); |
| 185 | |
| 186 | /** |
| 187 | * @brief Copy string data to file. |
| 188 | * |
| 189 | * @param[in] input - input string |
| 190 | * |
| 191 | * @return If operation is success return true, else on failure return |
| 192 | * false. |
| 193 | */ |
| 194 | bool copyStringToFile(const std::string& input); |
| 195 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 196 | public: |
| 197 | /** |
| 198 | * @brief Dump the complete inventory in JSON format |
| 199 | * |
| 200 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 201 | */ |
| 202 | void dumpInventory(const nlohmann::basic_json<>& jsObject); |
| 203 | |
| 204 | /** |
| 205 | * @brief Dump the given inventory object in JSON format |
| 206 | * |
| 207 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 208 | */ |
| 209 | void dumpObject(const nlohmann::basic_json<>& jsObject); |
| 210 | |
| 211 | /** |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 212 | * @brief Read keyword |
| 213 | * Read the given object path, record name and keyword |
| 214 | * from the inventory and display the value of the keyword |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 215 | * in JSON format. The read value will be piped to file if --file is given |
| 216 | * by the user. Else the value read will be displayed on console. |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 217 | */ |
| 218 | void readKeyword(); |
| 219 | |
| 220 | /** |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 221 | * @brief Update Keyword |
| 222 | * Update the given keyword with the given value. |
| 223 | * |
| 224 | * @return return code (Success(0)/Failure(-1)) |
| 225 | */ |
| 226 | int updateKeyword(); |
| 227 | |
| 228 | /** |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 229 | * @brief Force Reset |
| 230 | * Clearing the inventory cache data and restarting the |
| 231 | * phosphor inventory manager and also retriggering all the |
| 232 | * udev events. |
| 233 | * |
| 234 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 235 | */ |
| 236 | void forceReset(const nlohmann::basic_json<>& jsObject); |
| 237 | |
| 238 | /** |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 239 | * @brief Update Hardware |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 240 | * The given data is updated only on the given hardware path and not on dbus |
| 241 | * for the given record-keyword pair. The user can now update record-keyword |
| 242 | * value for any hardware path irrespective of whether its present or not in |
| 243 | * VPD JSON, by providing a valid offset. By default offset takes 0. |
| 244 | * |
| 245 | * @param[in] offset - VPD offset. |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 246 | * @return returncode (success/failure). |
| 247 | */ |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 248 | int updateHardware(const uint32_t offset); |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 249 | |
| 250 | /** |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 251 | * @brief Read Keyword from Hardware |
| 252 | * This api is to read a keyword directly from the hardware. The hardware |
| 253 | * path, record name and keyword name are received at the time of |
| 254 | * initialising the constructor. |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 255 | * The user can now read keyword from any hardware path irrespective of |
| 256 | * whether its present or not in VPD JSON, by providing a valid offset. By |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 257 | * default offset takes 0. The read value can be either saved in a |
| 258 | * file/displayed on console. |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 259 | * |
| 260 | * @param[in] startOffset - VPD offset. |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 261 | */ |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 262 | void readKwFromHw(const uint32_t& startOffset); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 263 | |
| 264 | /** |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 265 | * @brief Fix System VPD keyword. |
| 266 | * This API provides an interactive way to fix system VPD keywords that are |
| 267 | * part of restorable record-keyword pairs. The user can use this option to |
| 268 | * restore the restorable keywords in cache or in hardware or in both cache |
| 269 | * and hardware. |
| 270 | * @return returncode (success/failure). |
| 271 | */ |
| 272 | int fixSystemVPD(); |
| 273 | |
| 274 | /** |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 275 | * @brief Clean specific keywords in system backplane VPD |
| 276 | * |
| 277 | * @return return code (success/failure) |
| 278 | */ |
| 279 | int cleanSystemVPD(); |
| 280 | |
| 281 | /** |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 282 | * @brief Constructor |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 283 | * Constructor is called during the |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 284 | * object instantiation for dumpInventory option and |
| 285 | * forceReset option. |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 286 | */ |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 287 | VpdTool() {} |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * @brief Constructor |
| 291 | * Constructor is called during the |
| 292 | * object instantiation for dumpObject option. |
| 293 | */ |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 294 | VpdTool(const std::string&& fru) : fruPath(std::move(fru)) {} |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * @brief Constructor |
| 298 | * Constructor is called during the |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 299 | * object instantiation for updateKeyword option. |
| 300 | */ |
| 301 | |
| 302 | VpdTool(const std::string&& fru, const std::string&& recName, |
| 303 | const std::string&& kw, const std::string&& val) : |
| 304 | fruPath(std::move(fru)), |
| 305 | recordName(std::move(recName)), keyword(std::move(kw)), |
| 306 | value(std::move(val)) |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 307 | {} |
| 308 | }; |