blob: 47a555f04e0ce802ac552448e36b31f2687027a2 [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "config.h"
2
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05303#include "editor_impl.hpp"
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -05004#include "ibm_vpd_utils.hpp"
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05305#include "types.hpp"
6
7#include <nlohmann/json.hpp>
Patrick Williamsc78d8872023-05-10 07:50:56 -05008
PriyangaRamasamy887a42a2020-09-03 00:33:57 +05309#include <string>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053010
11using json = nlohmann::json;
12
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050013// <S.no, Record, Keyword, D-Bus value, HW value, Data mismatch>
14using SystemCriticalData =
priyaram0eb8cac2023-09-20 09:16:46 +053015 std::vector<std::tuple<uint8_t, std::string, std::string, std::string,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050016 std::string, std::string>>;
17
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053018class VpdTool
19{
20 private:
21 const std::string fruPath;
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053022 const std::string recordName;
23 const std::string keyword;
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053024 const std::string value;
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050025 bool objFound = true;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050026 SystemCriticalData recKwData;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053027
Alpana Kumarib6965f12020-06-01 00:32:21 -050028 // Store Type of FRU
29 std::string fruType;
30
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053031 /**
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 /**
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053052 * @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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053057 *
58 * @return json output which gives the properties under invPath's VINI
59 * interface
60 */
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050061 json getVINIProperties(std::string invPath);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053062
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.
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050071 * @param[out] output - output json which has the properties under invPath's
72 * extra interface.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053073 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050074 void getExtraInterfaceProperties(const std::string& invPath,
75 const std::string& extraInterface,
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050076 const json& prop, json& output);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053077
78 /**
79 * @brief Interface Decider
80 * Decides whether to make the dbus call for
Manojkiran Edaaf921752024-06-17 15:10:21 +053081 * getting properties from extraInterface or from
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053082 * 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
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530107 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530108 * @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
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530117 /**
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 */
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600124 void printReturnCode(int returnCode);
125
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530126 /**
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 Ramasamy0086dd12021-09-28 10:31:50 -0500133 /**
134 * @brief Get the json which has Present property value of the given fru.
135 * @param[in] invPath - inventory path of the fru.
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500136 * @return output json which has the Present property value.
137 */
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500138 json getPresentPropJson(const std::string& invPath);
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500139
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500140 /**
141 * @brief Parse through the options to fix system VPD
142 *
143 * @param[in] json - Inventory JSON
priyaram0eb8cac2023-09-20 09:16:46 +0530144 * @param[in] backupEEPROMPath - Backup VPD path
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500145 */
priyaram0eb8cac2023-09-20 09:16:46 +0530146 void parseSVPDOptions(const nlohmann::json& json,
147 const std::string& backupEEPROMPath);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500148
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 Ramasamy24942232023-01-05 04:54:59 -0600155 BACKUP_DATA_FOR_ALL = 1,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500156 SYSTEM_BACKPLANE_DATA_FOR_ALL = 2,
157 MORE_OPTIONS = 3,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600158 BACKUP_DATA_FOR_CURRENT = 4,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500159 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 Ramasamy6d5e7342022-10-14 07:17:25 -0500170 /**
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 Ramasamy5629fbc2023-03-01 08:17:19 -0600178 /**
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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530198 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 /**
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530214 * @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 Ramasamy5629fbc2023-03-01 08:17:19 -0600217 * 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.
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530219 */
220 void readKeyword();
221
222 /**
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530223 * @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 /**
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530231 * @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 /**
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530241 * @brief Update Hardware
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500242 * 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.
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530248 * @return returncode (success/failure).
249 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500250 int updateHardware(const uint32_t offset);
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530251
252 /**
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500253 * @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 Ramasamyc99a0b02022-06-08 14:53:39 -0500257 * 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 Ramasamy5629fbc2023-03-01 08:17:19 -0600259 * default offset takes 0. The read value can be either saved in a
260 * file/displayed on console.
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500261 *
262 * @param[in] startOffset - VPD offset.
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500263 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500264 void readKwFromHw(const uint32_t& startOffset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500265
266 /**
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500267 * @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 Ramasamy124ae6c2022-10-18 12:46:14 -0500277 * @brief Clean specific keywords in system backplane VPD
278 *
279 * @return return code (success/failure)
280 */
281 int cleanSystemVPD();
282
283 /**
priyaram0eb8cac2023-09-20 09:16:46 +0530284 * @brief Fix system VPD and its backup VPD
Manojkiran Edaaf921752024-06-17 15:10:21 +0530285 * API is triggered if the backup of system VPD has to be taken in a
priyaram0eb8cac2023-09-20 09:16:46 +0530286 * 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 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530297 * @brief Constructor
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530298 * Constructor is called during the
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530299 * object instantiation for dumpInventory option and
300 * forceReset option.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530301 */
Patrick Williamsc78d8872023-05-10 07:50:56 -0500302 VpdTool() {}
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530303
304 /**
305 * @brief Constructor
306 * Constructor is called during the
307 * object instantiation for dumpObject option.
308 */
Patrick Williamsc78d8872023-05-10 07:50:56 -0500309 VpdTool(const std::string&& fru) : fruPath(std::move(fru)) {}
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530310
311 /**
312 * @brief Constructor
313 * Constructor is called during the
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530314 * 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 Williams08dc31c2024-08-16 15:21:06 -0400319 fruPath(std::move(fru)), recordName(std::move(recName)),
320 keyword(std::move(kw)), value(std::move(val))
Patrick Williamsc78d8872023-05-10 07:50:56 -0500321 {}
322};