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 = |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 15 | std::vector<std::tuple<uint8_t, std::string, std::string, std::string, |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 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 |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 81 | * getting properties from extraInterface or from |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 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 |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 144 | * @param[in] backupEEPROMPath - Backup VPD path |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 145 | */ |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 146 | void parseSVPDOptions(const nlohmann::json& json, |
| 147 | const std::string& backupEEPROMPath); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @brief List of user options that can be used to fix system VPD keywords. |
| 151 | */ |
| 152 | enum UserOption |
| 153 | { |
| 154 | EXIT = 0, |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 155 | BACKUP_DATA_FOR_ALL = 1, |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 156 | SYSTEM_BACKPLANE_DATA_FOR_ALL = 2, |
| 157 | MORE_OPTIONS = 3, |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 158 | BACKUP_DATA_FOR_CURRENT = 4, |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 159 | SYSTEM_BACKPLANE_DATA_FOR_CURRENT = 5, |
| 160 | NEW_VALUE_ON_BOTH = 6, |
| 161 | SKIP_CURRENT = 7 |
| 162 | }; |
| 163 | |
| 164 | /** |
| 165 | * @brief Print options to fix system VPD. |
| 166 | * @param[in] option - Option to use. |
| 167 | */ |
| 168 | void printFixSystemVPDOption(UserOption option); |
| 169 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 170 | /** |
| 171 | * @brief Get System VPD data stored in cache |
| 172 | * |
| 173 | * @param[in] svpdBusData - Map of system VPD record data. |
| 174 | */ |
| 175 | void getSystemDataFromCache( |
| 176 | openpower::vpd::inventory::IntfPropMap& svpdBusData); |
| 177 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 178 | /** |
| 179 | * @brief Get data from file and store in binary format |
| 180 | * |
| 181 | * @param[out] data - The resulting binary data |
| 182 | * |
| 183 | * @return If operation is success return true, else on failure return |
| 184 | * false. |
| 185 | */ |
| 186 | bool fileToVector(openpower::vpd::Binary& data); |
| 187 | |
| 188 | /** |
| 189 | * @brief Copy string data to file. |
| 190 | * |
| 191 | * @param[in] input - input string |
| 192 | * |
| 193 | * @return If operation is success return true, else on failure return |
| 194 | * false. |
| 195 | */ |
| 196 | bool copyStringToFile(const std::string& input); |
| 197 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 198 | public: |
| 199 | /** |
| 200 | * @brief Dump the complete inventory in JSON format |
| 201 | * |
| 202 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 203 | */ |
| 204 | void dumpInventory(const nlohmann::basic_json<>& jsObject); |
| 205 | |
| 206 | /** |
| 207 | * @brief Dump the given inventory object in JSON format |
| 208 | * |
| 209 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 210 | */ |
| 211 | void dumpObject(const nlohmann::basic_json<>& jsObject); |
| 212 | |
| 213 | /** |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 214 | * @brief Read keyword |
| 215 | * Read the given object path, record name and keyword |
| 216 | * from the inventory and display the value of the keyword |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 217 | * in JSON format. The read value will be piped to file if --file is given |
| 218 | * by the user. Else the value read will be displayed on console. |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 219 | */ |
| 220 | void readKeyword(); |
| 221 | |
| 222 | /** |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 223 | * @brief Update Keyword |
| 224 | * Update the given keyword with the given value. |
| 225 | * |
| 226 | * @return return code (Success(0)/Failure(-1)) |
| 227 | */ |
| 228 | int updateKeyword(); |
| 229 | |
| 230 | /** |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 231 | * @brief Force Reset |
| 232 | * Clearing the inventory cache data and restarting the |
| 233 | * phosphor inventory manager and also retriggering all the |
| 234 | * udev events. |
| 235 | * |
| 236 | * @param[in] jsObject - Inventory JSON specified in configure file. |
| 237 | */ |
| 238 | void forceReset(const nlohmann::basic_json<>& jsObject); |
| 239 | |
| 240 | /** |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 241 | * @brief Update Hardware |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 242 | * The given data is updated only on the given hardware path and not on dbus |
| 243 | * for the given record-keyword pair. The user can now update record-keyword |
| 244 | * value for any hardware path irrespective of whether its present or not in |
| 245 | * VPD JSON, by providing a valid offset. By default offset takes 0. |
| 246 | * |
| 247 | * @param[in] offset - VPD offset. |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 248 | * @return returncode (success/failure). |
| 249 | */ |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 250 | int updateHardware(const uint32_t offset); |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 251 | |
| 252 | /** |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 253 | * @brief Read Keyword from Hardware |
| 254 | * This api is to read a keyword directly from the hardware. The hardware |
| 255 | * path, record name and keyword name are received at the time of |
| 256 | * initialising the constructor. |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 257 | * The user can now read keyword from any hardware path irrespective of |
| 258 | * 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] | 259 | * default offset takes 0. The read value can be either saved in a |
| 260 | * file/displayed on console. |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 261 | * |
| 262 | * @param[in] startOffset - VPD offset. |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 263 | */ |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 264 | void readKwFromHw(const uint32_t& startOffset); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 265 | |
| 266 | /** |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 267 | * @brief Fix System VPD keyword. |
| 268 | * This API provides an interactive way to fix system VPD keywords that are |
| 269 | * part of restorable record-keyword pairs. The user can use this option to |
| 270 | * restore the restorable keywords in cache or in hardware or in both cache |
| 271 | * and hardware. |
| 272 | * @return returncode (success/failure). |
| 273 | */ |
| 274 | int fixSystemVPD(); |
| 275 | |
| 276 | /** |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 277 | * @brief Clean specific keywords in system backplane VPD |
| 278 | * |
| 279 | * @return return code (success/failure) |
| 280 | */ |
| 281 | int cleanSystemVPD(); |
| 282 | |
| 283 | /** |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 284 | * @brief Fix system VPD and its backup VPD |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 285 | * API is triggered if the backup of system VPD has to be taken in a |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 286 | * hardware VPD. User can use the --fixSystemVPD option to restore the |
| 287 | * keywords in backup VPD and/or system VPD. |
| 288 | * |
| 289 | * @param[in] backupEepromPath - Backup VPD path |
| 290 | * @param[in] backupInvPath - Backup inventory path |
| 291 | * @return returncode |
| 292 | */ |
| 293 | int fixSystemBackupVPD(const std::string& backupEepromPath, |
| 294 | const std::string& backupInvPath); |
| 295 | |
| 296 | /** |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 297 | * @brief Constructor |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 298 | * Constructor is called during the |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 299 | * object instantiation for dumpInventory option and |
| 300 | * forceReset option. |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 301 | */ |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 302 | VpdTool() {} |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * @brief Constructor |
| 306 | * Constructor is called during the |
| 307 | * object instantiation for dumpObject option. |
| 308 | */ |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 309 | VpdTool(const std::string&& fru) : fruPath(std::move(fru)) {} |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * @brief Constructor |
| 313 | * Constructor is called during the |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 314 | * object instantiation for updateKeyword option. |
| 315 | */ |
| 316 | |
| 317 | VpdTool(const std::string&& fru, const std::string&& recName, |
| 318 | const std::string&& kw, const std::string&& val) : |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 319 | fruPath(std::move(fru)), recordName(std::move(recName)), |
| 320 | keyword(std::move(kw)), value(std::move(val)) |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 321 | {} |
| 322 | }; |