blob: 16c3a347fbe6623ee6d3c7f6c9d06f450d5d1438 [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>
PriyangaRamasamy887a42a2020-09-03 00:33:57 +05308#include <string>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05309
10using json = nlohmann::json;
11
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050012// <S.no, Record, Keyword, D-Bus value, HW value, Data mismatch>
13using SystemCriticalData =
14 std::vector<std::tuple<int, std::string, std::string, std::string,
15 std::string, std::string>>;
16
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053017class VpdTool
18{
19 private:
20 const std::string fruPath;
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053021 const std::string recordName;
22 const std::string keyword;
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053023 const std::string value;
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050024 bool objFound = true;
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050025 SystemCriticalData recKwData;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053026
Alpana Kumarib6965f12020-06-01 00:32:21 -050027 // Store Type of FRU
28 std::string fruType;
29
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053030 /**
31 * @brief Debugger
32 * Displays the output in JSON.
33 *
34 * @param[in] output - json output to be displayed
35 */
36 void debugger(json output);
37
38 /**
39 * @brief make Dbus Call
40 *
41 * @param[in] objectName - dbus Object
42 * @param[in] interface - dbus Interface
43 * @param[in] kw - keyword under the interface
44 *
45 * @return dbus call response
46 */
47 auto makeDBusCall(const std::string& objectName,
48 const std::string& interface, const std::string& kw);
49
50 /**
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053051 * @brief Get VINI properties
52 * Making a dbus call for properties [SN, PN, CC, FN, DR]
53 * under VINI interface.
54 *
55 * @param[in] invPath - Value of inventory Path
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053056 *
57 * @return json output which gives the properties under invPath's VINI
58 * interface
59 */
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050060 json getVINIProperties(std::string invPath);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053061
62 /**
63 * @brief Get ExtraInterface Properties
64 * Making a dbus call for those properties under extraInterfaces.
65 *
66 * @param[in] invPath - Value of inventory path
67 * @param[in] extraInterface - One of the invPath's extraInterfaces whose
68 * value is not null
69 * @param[in] prop - All properties of the extraInterface.
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050070 * @param[out] output - output json which has the properties under invPath's
71 * extra interface.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053072 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050073 void getExtraInterfaceProperties(const std::string& invPath,
74 const std::string& extraInterface,
PriyangaRamasamy8cc5b152021-06-03 13:05:17 -050075 const json& prop, json& output);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053076
77 /**
78 * @brief Interface Decider
79 * Decides whether to make the dbus call for
80 * getting properites from extraInterface or from
81 * VINI interface, depending on the value of
82 * extraInterfaces object in the inventory json.
83 *
84 * @param[in] itemEEPROM - holds the reference of one of the EEPROM objects.
85 *
86 * @return json output for one of the EEPROM objects.
87 */
88 json interfaceDecider(json& itemEEPROM);
89
90 /**
91 * @brief Parse Inventory JSON
92 * Parses the complete inventory json and depending upon
93 * the user option makes the dbuscall for the frus
94 * via interfaceDecider function.
95 *
96 * @param[in] jsObject - Inventory json object
97 * @param[in] flag - flag which tells about the user option(either
98 * dumpInventory or dumpObject)
99 * @param[in] fruPath - fruPath is empty for dumpInventory option and holds
100 * valid fruPath for dumpObject option.
101 *
102 * @return output json
103 */
104 json parseInvJson(const json& jsObject, char flag, std::string fruPath);
105
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530106 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530107 * @brief eraseInventoryPath
108 * Remove the INVENTORY_PATH - "/xyz/openbmc_project/inventory"
109 * for code convenience.
110 *
111 * @param[out] fru - Reference to the fru path whose INVENTORY_PATH is
112 * stripped off.
113 */
114 void eraseInventoryPath(std::string& fru);
115
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530116 /**
117 * @brief printReturnCode
118 * Prints the return code of the program in console output, whenever
119 * the program fails to exit successfully.
120 *
121 * @param[in] returnCode - return code of the program.
122 */
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600123 void printReturnCode(int returnCode);
124
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530125 /**
126 * @brief Convert hex/ascii values to Binary
127 * @param[in] - value in hex/ascii.
128 * @param[out] - value in binary.
129 */
130 openpower::vpd::Binary toBinary(const std::string& value);
131
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500132 /**
133 * @brief Get the json which has Present property value of the given fru.
134 * @param[in] invPath - inventory path of the fru.
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500135 * @return output json which has the Present property value.
136 */
Priyanga Ramasamyd90aadb2023-03-28 12:25:14 -0500137 json getPresentPropJson(const std::string& invPath);
Priyanga Ramasamy0086dd12021-09-28 10:31:50 -0500138
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500139 /**
140 * @brief Parse through the options to fix system VPD
141 *
142 * @param[in] json - Inventory JSON
143 */
144 void parseSVPDOptions(const nlohmann::json& json);
145
146 /**
147 * @brief List of user options that can be used to fix system VPD keywords.
148 */
149 enum UserOption
150 {
151 EXIT = 0,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600152 BACKUP_DATA_FOR_ALL = 1,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500153 SYSTEM_BACKPLANE_DATA_FOR_ALL = 2,
154 MORE_OPTIONS = 3,
Priyanga Ramasamy24942232023-01-05 04:54:59 -0600155 BACKUP_DATA_FOR_CURRENT = 4,
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500156 SYSTEM_BACKPLANE_DATA_FOR_CURRENT = 5,
157 NEW_VALUE_ON_BOTH = 6,
158 SKIP_CURRENT = 7
159 };
160
161 /**
162 * @brief Print options to fix system VPD.
163 * @param[in] option - Option to use.
164 */
165 void printFixSystemVPDOption(UserOption option);
166
Priyanga Ramasamy6d5e7342022-10-14 07:17:25 -0500167 /**
168 * @brief Get System VPD data stored in cache
169 *
170 * @param[in] svpdBusData - Map of system VPD record data.
171 */
172 void getSystemDataFromCache(
173 openpower::vpd::inventory::IntfPropMap& svpdBusData);
174
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530175 public:
176 /**
177 * @brief Dump the complete inventory in JSON format
178 *
179 * @param[in] jsObject - Inventory JSON specified in configure file.
180 */
181 void dumpInventory(const nlohmann::basic_json<>& jsObject);
182
183 /**
184 * @brief Dump the given inventory object in JSON format
185 *
186 * @param[in] jsObject - Inventory JSON specified in configure file.
187 */
188 void dumpObject(const nlohmann::basic_json<>& jsObject);
189
190 /**
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530191 * @brief Read keyword
192 * Read the given object path, record name and keyword
193 * from the inventory and display the value of the keyword
194 * in JSON format.
195 */
196 void readKeyword();
197
198 /**
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530199 * @brief Update Keyword
200 * Update the given keyword with the given value.
201 *
202 * @return return code (Success(0)/Failure(-1))
203 */
204 int updateKeyword();
205
206 /**
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530207 * @brief Force Reset
208 * Clearing the inventory cache data and restarting the
209 * phosphor inventory manager and also retriggering all the
210 * udev events.
211 *
212 * @param[in] jsObject - Inventory JSON specified in configure file.
213 */
214 void forceReset(const nlohmann::basic_json<>& jsObject);
215
216 /**
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530217 * @brief Update Hardware
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500218 * The given data is updated only on the given hardware path and not on dbus
219 * for the given record-keyword pair. The user can now update record-keyword
220 * value for any hardware path irrespective of whether its present or not in
221 * VPD JSON, by providing a valid offset. By default offset takes 0.
222 *
223 * @param[in] offset - VPD offset.
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530224 * @return returncode (success/failure).
225 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500226 int updateHardware(const uint32_t offset);
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530227
228 /**
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500229 * @brief Read Keyword from Hardware
230 * This api is to read a keyword directly from the hardware. The hardware
231 * path, record name and keyword name are received at the time of
232 * initialising the constructor.
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500233 * The user can now read keyword from any hardware path irrespective of
234 * whether its present or not in VPD JSON, by providing a valid offset. By
235 * default offset takes 0.
236 *
237 * @param[in] startOffset - VPD offset.
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500238 */
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500239 void readKwFromHw(const uint32_t& startOffset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500240
241 /**
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500242 * @brief Fix System VPD keyword.
243 * This API provides an interactive way to fix system VPD keywords that are
244 * part of restorable record-keyword pairs. The user can use this option to
245 * restore the restorable keywords in cache or in hardware or in both cache
246 * and hardware.
247 * @return returncode (success/failure).
248 */
249 int fixSystemVPD();
250
251 /**
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -0500252 * @brief Clean specific keywords in system backplane VPD
253 *
254 * @return return code (success/failure)
255 */
256 int cleanSystemVPD();
257
258 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530259 * @brief Constructor
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530260 * Constructor is called during the
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530261 * object instantiation for dumpInventory option and
262 * forceReset option.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530263 */
264 VpdTool()
265 {
266 }
267
268 /**
269 * @brief Constructor
270 * Constructor is called during the
271 * object instantiation for dumpObject option.
272 */
273 VpdTool(const std::string&& fru) : fruPath(std::move(fru))
274 {
275 }
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530276
277 /**
278 * @brief Constructor
279 * Constructor is called during the
280 * object instantiation for readKeyword option.
281 */
282 VpdTool(const std::string&& fru, const std::string&& recName,
283 const std::string&& kw) :
284 fruPath(std::move(fru)),
285 recordName(std::move(recName)), keyword(std::move(kw))
286 {
287 }
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530288
289 /**
290 * @brief Constructor
291 * Constructor is called during the
292 * object instantiation for updateKeyword option.
293 */
294
295 VpdTool(const std::string&& fru, const std::string&& recName,
296 const std::string&& kw, const std::string&& val) :
297 fruPath(std::move(fru)),
298 recordName(std::move(recName)), keyword(std::move(kw)),
299 value(std::move(val))
300 {
301 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530302};