blob: 11e6ef8d1cf8fd0aa807a56add720ccdb824692d [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 =
15 std::vector<std::tuple<int, std::string, std::string, std::string,
16 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
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
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
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 Ramasamy24942232023-01-05 04:54:59 -0600153 BACKUP_DATA_FOR_ALL = 1,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500154 SYSTEM_BACKPLANE_DATA_FOR_ALL = 2,
155 MORE_OPTIONS = 3,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600156 BACKUP_DATA_FOR_CURRENT = 4,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500157 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 Ramasamy6d5e7342022-10-14 07:17:25 -0500168 /**
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 Ramasamy5629fbc2023-03-01 08:17:19 -0600176 /**
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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530196 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 /**
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530212 * @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 Ramasamy5629fbc2023-03-01 08:17:19 -0600215 * 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.
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530217 */
218 void readKeyword();
219
220 /**
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530221 * @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 /**
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530229 * @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 /**
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530239 * @brief Update Hardware
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500240 * 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.
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530246 * @return returncode (success/failure).
247 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500248 int updateHardware(const uint32_t offset);
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530249
250 /**
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500251 * @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 Ramasamyc99a0b02022-06-08 14:53:39 -0500255 * 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 Ramasamy5629fbc2023-03-01 08:17:19 -0600257 * default offset takes 0. The read value can be either saved in a
258 * file/displayed on console.
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500259 *
260 * @param[in] startOffset - VPD offset.
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500261 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500262 void readKwFromHw(const uint32_t& startOffset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500263
264 /**
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500265 * @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 Ramasamy124ae6c2022-10-18 12:46:14 -0500275 * @brief Clean specific keywords in system backplane VPD
276 *
277 * @return return code (success/failure)
278 */
279 int cleanSystemVPD();
280
281 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530282 * @brief Constructor
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530283 * Constructor is called during the
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530284 * object instantiation for dumpInventory option and
285 * forceReset option.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530286 */
Patrick Williamsc78d8872023-05-10 07:50:56 -0500287 VpdTool() {}
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530288
289 /**
290 * @brief Constructor
291 * Constructor is called during the
292 * object instantiation for dumpObject option.
293 */
Patrick Williamsc78d8872023-05-10 07:50:56 -0500294 VpdTool(const std::string&& fru) : fruPath(std::move(fru)) {}
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530295
296 /**
297 * @brief Constructor
298 * Constructor is called during the
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530299 * 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 Williamsc78d8872023-05-10 07:50:56 -0500307 {}
308};