xref: /openbmc/openpower-vpd-parser/vpd-tool/include/vpd_tool.hpp (revision fa5e4d325ef9cea3c841fe89d202c340f92bd8c6)
1*fa5e4d32SSunny Srivastava #pragma once
2*fa5e4d32SSunny Srivastava 
3*fa5e4d32SSunny Srivastava #include "tool_utils.hpp"
4*fa5e4d32SSunny Srivastava 
5*fa5e4d32SSunny Srivastava #include <nlohmann/json.hpp>
6*fa5e4d32SSunny Srivastava 
7*fa5e4d32SSunny Srivastava #include <optional>
8*fa5e4d32SSunny Srivastava #include <string>
9*fa5e4d32SSunny Srivastava 
10*fa5e4d32SSunny Srivastava namespace vpd
11*fa5e4d32SSunny Srivastava {
12*fa5e4d32SSunny Srivastava /**
13*fa5e4d32SSunny Srivastava  * @brief Class to support operations on VPD.
14*fa5e4d32SSunny Srivastava  *
15*fa5e4d32SSunny Srivastava  * The class provides API's to,
16*fa5e4d32SSunny Srivastava  * Read keyword value from DBus/hardware.
17*fa5e4d32SSunny Srivastava  * Update keyword value to DBus/hardware.
18*fa5e4d32SSunny Srivastava  * Dump DBus object's critical information.
19*fa5e4d32SSunny Srivastava  * Fix system VPD if any mismatch between DBus and hardware data.
20*fa5e4d32SSunny Srivastava  * Reset specific system VPD keywords to its default value.
21*fa5e4d32SSunny Srivastava  * Force VPD collection for hardware.
22*fa5e4d32SSunny Srivastava  */
23*fa5e4d32SSunny Srivastava class VpdTool
24*fa5e4d32SSunny Srivastava {
25*fa5e4d32SSunny Srivastava   private:
26*fa5e4d32SSunny Srivastava     /**
27*fa5e4d32SSunny Srivastava      * @brief Get specific properties of a FRU in JSON format.
28*fa5e4d32SSunny Srivastava      *
29*fa5e4d32SSunny Srivastava      * For a given object path of a FRU, this API returns the following
30*fa5e4d32SSunny Srivastava      * properties of the FRU in JSON format:
31*fa5e4d32SSunny Srivastava      * - Pretty Name, Location Code, Sub Model
32*fa5e4d32SSunny Srivastava      * - SN, PN, CC, FN, DR keywords under VINI record.
33*fa5e4d32SSunny Srivastava      *
34*fa5e4d32SSunny Srivastava      * @param[in] i_objectPath - DBus object path
35*fa5e4d32SSunny Srivastava      *
36*fa5e4d32SSunny Srivastava      * @return On success, returns the properties of the FRU in JSON format,
37*fa5e4d32SSunny Srivastava      * otherwise returns an empty JSON.
38*fa5e4d32SSunny Srivastava      * If FRU's "Present" property is false, this API returns an empty JSON.
39*fa5e4d32SSunny Srivastava      * Note: The caller of this API should handle empty JSON.
40*fa5e4d32SSunny Srivastava      *
41*fa5e4d32SSunny Srivastava      * @throw json::exception
42*fa5e4d32SSunny Srivastava      */
43*fa5e4d32SSunny Srivastava     nlohmann::json getFruProperties(const std::string& i_objectPath) const;
44*fa5e4d32SSunny Srivastava 
45*fa5e4d32SSunny Srivastava     /**
46*fa5e4d32SSunny Srivastava      * @brief Get any inventory property in JSON.
47*fa5e4d32SSunny Srivastava      *
48*fa5e4d32SSunny Srivastava      * API to get any property of a FRU in JSON format. Given an object path,
49*fa5e4d32SSunny Srivastava      * interface and property name, this API does a D-Bus read property on PIM
50*fa5e4d32SSunny Srivastava      * to get the value of that property and returns it in JSON format. This API
51*fa5e4d32SSunny Srivastava      * returns empty JSON in case of failure. The caller of the API must check
52*fa5e4d32SSunny Srivastava      * for empty JSON.
53*fa5e4d32SSunny Srivastava      *
54*fa5e4d32SSunny Srivastava      * @param[in] i_objectPath - DBus object path
55*fa5e4d32SSunny Srivastava      * @param[in] i_interface - Interface name
56*fa5e4d32SSunny Srivastava      * @param[in] i_propertyName - Property name
57*fa5e4d32SSunny Srivastava      *
58*fa5e4d32SSunny Srivastava      * @return On success, returns the property and its value in JSON format,
59*fa5e4d32SSunny Srivastava      * otherwise return empty JSON.
60*fa5e4d32SSunny Srivastava      * {"SN" : "ABCD"}
61*fa5e4d32SSunny Srivastava      */
62*fa5e4d32SSunny Srivastava     template <typename PropertyType>
63*fa5e4d32SSunny Srivastava     nlohmann::json getInventoryPropertyJson(
64*fa5e4d32SSunny Srivastava         const std::string& i_objectPath, const std::string& i_interface,
65*fa5e4d32SSunny Srivastava         const std::string& i_propertyName) const noexcept;
66*fa5e4d32SSunny Srivastava 
67*fa5e4d32SSunny Srivastava     /**
68*fa5e4d32SSunny Srivastava      * @brief Get the "type" property for a FRU.
69*fa5e4d32SSunny Srivastava      *
70*fa5e4d32SSunny Srivastava      * Given a FRU path, and parsed System Config JSON, this API returns the
71*fa5e4d32SSunny Srivastava      * "type" property for the FRU in JSON format. This API gets
72*fa5e4d32SSunny Srivastava      * these properties from Phosphor Inventory Manager.
73*fa5e4d32SSunny Srivastava      *
74*fa5e4d32SSunny Srivastava      * @param[in] i_objectPath - DBus object path.
75*fa5e4d32SSunny Srivastava      *
76*fa5e4d32SSunny Srivastava      * @return On success, returns the "type" property in JSON
77*fa5e4d32SSunny Srivastava      * format, otherwise returns empty JSON. The caller of this API should
78*fa5e4d32SSunny Srivastava      * handle empty JSON.
79*fa5e4d32SSunny Srivastava      */
80*fa5e4d32SSunny Srivastava     nlohmann::json
81*fa5e4d32SSunny Srivastava         getFruTypeProperty(const std::string& i_objectPath) const noexcept;
82*fa5e4d32SSunny Srivastava 
83*fa5e4d32SSunny Srivastava     /**
84*fa5e4d32SSunny Srivastava      * @brief Check if a FRU is present in the system.
85*fa5e4d32SSunny Srivastava      *
86*fa5e4d32SSunny Srivastava      * Given a FRU's object path, this API checks if the FRU is present in the
87*fa5e4d32SSunny Srivastava      * system by reading the "Present" property of the FRU.
88*fa5e4d32SSunny Srivastava      *
89*fa5e4d32SSunny Srivastava      * @param[in] i_objectPath - DBus object path.
90*fa5e4d32SSunny Srivastava      *
91*fa5e4d32SSunny Srivastava      * @return true if FRU's "Present" property is true, false otherwise.
92*fa5e4d32SSunny Srivastava      */
93*fa5e4d32SSunny Srivastava     bool isFruPresent(const std::string& i_objectPath) const noexcept;
94*fa5e4d32SSunny Srivastava 
95*fa5e4d32SSunny Srivastava     /**
96*fa5e4d32SSunny Srivastava      * @brief An API to get backup-restore config JSON of the system.
97*fa5e4d32SSunny Srivastava      *
98*fa5e4d32SSunny Srivastava      * API gets this file by prasing system config JSON file and reading
99*fa5e4d32SSunny Srivastava      * backupRestoreConfigPath tag.
100*fa5e4d32SSunny Srivastava      *
101*fa5e4d32SSunny Srivastava      * @return On success returns valid JSON object, otherwise returns empty
102*fa5e4d32SSunny Srivastava      * JSON object.
103*fa5e4d32SSunny Srivastava      *
104*fa5e4d32SSunny Srivastava      * Note: The caller of this API should verify, is received JSON object is
105*fa5e4d32SSunny Srivastava      * empty or not.
106*fa5e4d32SSunny Srivastava      */
107*fa5e4d32SSunny Srivastava     nlohmann::json getBackupRestoreCfgJsonObj() const noexcept;
108*fa5e4d32SSunny Srivastava 
109*fa5e4d32SSunny Srivastava     /**
110*fa5e4d32SSunny Srivastava      * @brief Prints the user options for fix system VPD command.
111*fa5e4d32SSunny Srivastava      *
112*fa5e4d32SSunny Srivastava      * @param[in] i_option - Option to use.
113*fa5e4d32SSunny Srivastava      */
114*fa5e4d32SSunny Srivastava     void printFixSystemVpdOption(
115*fa5e4d32SSunny Srivastava         const types::UserOption& i_option) const noexcept;
116*fa5e4d32SSunny Srivastava 
117*fa5e4d32SSunny Srivastava     /**
118*fa5e4d32SSunny Srivastava      * @brief API to update source and destination keyword's value.
119*fa5e4d32SSunny Srivastava      *
120*fa5e4d32SSunny Srivastava      * API fetches source and destination keyword's value,
121*fa5e4d32SSunny Srivastava      * for each keyword entries found in the input JSON object and updates the
122*fa5e4d32SSunny Srivastava      * JSON object. If the path(source / destination) in JSON object is
123*fa5e4d32SSunny Srivastava      * inventory object path, API sends the request to Inventory.Manager DBus
124*fa5e4d32SSunny Srivastava      * service. Otherwise if its a hardware path, API sends the request to
125*fa5e4d32SSunny Srivastava      * vpd-manager DBus service to get the keyword's value.
126*fa5e4d32SSunny Srivastava      *
127*fa5e4d32SSunny Srivastava      * @param[in,out] io_parsedJsonObj - Parsed JSON object.
128*fa5e4d32SSunny Srivastava      *
129*fa5e4d32SSunny Srivastava      * @return true on success, false in case of any error.
130*fa5e4d32SSunny Srivastava      */
131*fa5e4d32SSunny Srivastava     bool fetchKeywordInfo(nlohmann::json& io_parsedJsonObj) const noexcept;
132*fa5e4d32SSunny Srivastava 
133*fa5e4d32SSunny Srivastava     /**
134*fa5e4d32SSunny Srivastava      * @brief API to print system VPD keyword's information.
135*fa5e4d32SSunny Srivastava      *
136*fa5e4d32SSunny Srivastava      * The API prints source and destination keyword's information in the table
137*fa5e4d32SSunny Srivastava      * format, found in the JSON object.
138*fa5e4d32SSunny Srivastava      *
139*fa5e4d32SSunny Srivastava      * @param[in] i_parsedJsonObj - Parsed JSON object.
140*fa5e4d32SSunny Srivastava      */
141*fa5e4d32SSunny Srivastava     void printSystemVpd(const nlohmann::json& i_parsedJsonObj) const noexcept;
142*fa5e4d32SSunny Srivastava 
143*fa5e4d32SSunny Srivastava     /**
144*fa5e4d32SSunny Srivastava      * @brief API to update keyword's value.
145*fa5e4d32SSunny Srivastava      *
146*fa5e4d32SSunny Srivastava      * API iterates the given JSON object for all record-keyword pairs, if there
147*fa5e4d32SSunny Srivastava      * is any mismatch between source and destination keyword's value, API calls
148*fa5e4d32SSunny Srivastava      * the utils::writeKeyword API to update keyword's value.
149*fa5e4d32SSunny Srivastava      *
150*fa5e4d32SSunny Srivastava      * Note: writeKeyword API, internally updates primary, backup, redundant
151*fa5e4d32SSunny Srivastava      * EEPROM paths(if exists) with the given keyword's value.
152*fa5e4d32SSunny Srivastava      *
153*fa5e4d32SSunny Srivastava      * @param i_parsedJsonObj - Parsed JSON object.
154*fa5e4d32SSunny Srivastava      * @param i_useBackupData - Specifies whether to use source or destination
155*fa5e4d32SSunny Srivastava      * keyword's value to update the keyword's value.
156*fa5e4d32SSunny Srivastava      *
157*fa5e4d32SSunny Srivastava      * @return On success return 0, otherwise return -1.
158*fa5e4d32SSunny Srivastava      */
159*fa5e4d32SSunny Srivastava     int updateAllKeywords(const nlohmann::json& i_parsedJsonObj,
160*fa5e4d32SSunny Srivastava                           bool i_useBackupData) const noexcept;
161*fa5e4d32SSunny Srivastava 
162*fa5e4d32SSunny Srivastava     /**
163*fa5e4d32SSunny Srivastava      * @brief API to handle more option for fix system VPD command.
164*fa5e4d32SSunny Srivastava      *
165*fa5e4d32SSunny Srivastava      * @param i_parsedJsonObj - Parsed JSON object.
166*fa5e4d32SSunny Srivastava      *
167*fa5e4d32SSunny Srivastava      * @return On success return 0, otherwise return -1.
168*fa5e4d32SSunny Srivastava      */
169*fa5e4d32SSunny Srivastava     int handleMoreOption(const nlohmann::json& i_parsedJsonObj) const noexcept;
170*fa5e4d32SSunny Srivastava 
171*fa5e4d32SSunny Srivastava   public:
172*fa5e4d32SSunny Srivastava     /**
173*fa5e4d32SSunny Srivastava      * @brief Read keyword value.
174*fa5e4d32SSunny Srivastava      *
175*fa5e4d32SSunny Srivastava      * API to read VPD keyword's value from the given input path.
176*fa5e4d32SSunny Srivastava      * If the provided i_onHardware option is true, read keyword's value from
177*fa5e4d32SSunny Srivastava      * the hardware. Otherwise read keyword's value from DBus.
178*fa5e4d32SSunny Srivastava      *
179*fa5e4d32SSunny Srivastava      * @param[in] i_vpdPath - DBus object path or EEPROM path.
180*fa5e4d32SSunny Srivastava      * @param[in] i_recordName - Record name.
181*fa5e4d32SSunny Srivastava      * @param[in] i_keywordName - Keyword name.
182*fa5e4d32SSunny Srivastava      * @param[in] i_onHardware - True if i_vpdPath is EEPROM path, false
183*fa5e4d32SSunny Srivastava      * otherwise.
184*fa5e4d32SSunny Srivastava      * @param[in] i_fileToSave - File path to save keyword's value, if not given
185*fa5e4d32SSunny Srivastava      * result will redirect to a console.
186*fa5e4d32SSunny Srivastava      *
187*fa5e4d32SSunny Srivastava      * @return On success return 0, otherwise return -1.
188*fa5e4d32SSunny Srivastava      */
189*fa5e4d32SSunny Srivastava     int readKeyword(const std::string& i_vpdPath,
190*fa5e4d32SSunny Srivastava                     const std::string& i_recordName,
191*fa5e4d32SSunny Srivastava                     const std::string& i_keywordName, const bool i_onHardware,
192*fa5e4d32SSunny Srivastava                     const std::string& i_fileToSave = {});
193*fa5e4d32SSunny Srivastava 
194*fa5e4d32SSunny Srivastava     /**
195*fa5e4d32SSunny Srivastava      * @brief Dump the given inventory object in JSON format to console.
196*fa5e4d32SSunny Srivastava      *
197*fa5e4d32SSunny Srivastava      * For a given object path of a FRU, this API dumps the following properties
198*fa5e4d32SSunny Srivastava      * of the FRU in JSON format to console:
199*fa5e4d32SSunny Srivastava      * - Pretty Name, Location Code, Sub Model
200*fa5e4d32SSunny Srivastava      * - SN, PN, CC, FN, DR keywords under VINI record.
201*fa5e4d32SSunny Srivastava      * If the FRU's "Present" property is not true, the above properties are not
202*fa5e4d32SSunny Srivastava      * dumped to console.
203*fa5e4d32SSunny Srivastava      *
204*fa5e4d32SSunny Srivastava      * @param[in] i_fruPath - DBus object path.
205*fa5e4d32SSunny Srivastava      *
206*fa5e4d32SSunny Srivastava      * @return On success returns 0, otherwise returns -1.
207*fa5e4d32SSunny Srivastava      */
208*fa5e4d32SSunny Srivastava     int dumpObject(std::string i_fruPath) const noexcept;
209*fa5e4d32SSunny Srivastava 
210*fa5e4d32SSunny Srivastava     /**
211*fa5e4d32SSunny Srivastava      * @brief API to fix system VPD keywords.
212*fa5e4d32SSunny Srivastava      *
213*fa5e4d32SSunny Srivastava      * The API to fix the system VPD keywords. Mainly used when there
214*fa5e4d32SSunny Srivastava      * is a mismatch between the primary and backup(secondary) VPD. User can
215*fa5e4d32SSunny Srivastava      * choose option to update all primary keywords value with corresponding
216*fa5e4d32SSunny Srivastava      * backup keywords value or can choose primary keyword value to sync
217*fa5e4d32SSunny Srivastava      * secondary VPD. Otherwise, user can also interactively choose different
218*fa5e4d32SSunny Srivastava      * action for individual keyword.
219*fa5e4d32SSunny Srivastava      *
220*fa5e4d32SSunny Srivastava      * @return On success returns 0, otherwise returns -1.
221*fa5e4d32SSunny Srivastava      */
222*fa5e4d32SSunny Srivastava     int fixSystemVpd() const noexcept;
223*fa5e4d32SSunny Srivastava 
224*fa5e4d32SSunny Srivastava     /**
225*fa5e4d32SSunny Srivastava      * @brief Write keyword's value.
226*fa5e4d32SSunny Srivastava      *
227*fa5e4d32SSunny Srivastava      * API to update VPD keyword's value to the given input path.
228*fa5e4d32SSunny Srivastava      * If i_onHardware value in true, i_vpdPath is considered has hardware path
229*fa5e4d32SSunny Srivastava      * otherwise it will be considered as DBus object path.
230*fa5e4d32SSunny Srivastava      *
231*fa5e4d32SSunny Srivastava      * For provided DBus object path both primary path or secondary path will
232*fa5e4d32SSunny Srivastava      * get updated, also redundant EEPROM(if any) path with new keyword's value.
233*fa5e4d32SSunny Srivastava      *
234*fa5e4d32SSunny Srivastava      * In case of hardware path, only given hardware path gets updated with new
235*fa5e4d32SSunny Srivastava      * keyword’s value, any backup or redundant EEPROM (if exists) paths won't
236*fa5e4d32SSunny Srivastava      * get updated.
237*fa5e4d32SSunny Srivastava      *
238*fa5e4d32SSunny Srivastava      * @param[in] i_vpdPath - DBus object path or EEPROM path.
239*fa5e4d32SSunny Srivastava      * @param[in] i_recordName - Record name.
240*fa5e4d32SSunny Srivastava      * @param[in] i_keywordName - Keyword name.
241*fa5e4d32SSunny Srivastava      * @param[in] i_keywordValue - Keyword value.
242*fa5e4d32SSunny Srivastava      * @param[in] i_onHardware - True if i_vpdPath is EEPROM path, false
243*fa5e4d32SSunny Srivastava      * otherwise.
244*fa5e4d32SSunny Srivastava      *
245*fa5e4d32SSunny Srivastava      * @return On success returns 0, otherwise returns -1.
246*fa5e4d32SSunny Srivastava      */
247*fa5e4d32SSunny Srivastava     int writeKeyword(std::string i_vpdPath, const std::string& i_recordName,
248*fa5e4d32SSunny Srivastava                      const std::string& i_keywordName,
249*fa5e4d32SSunny Srivastava                      const std::string& i_keywordValue,
250*fa5e4d32SSunny Srivastava                      const bool i_onHardware) noexcept;
251*fa5e4d32SSunny Srivastava 
252*fa5e4d32SSunny Srivastava     /**
253*fa5e4d32SSunny Srivastava      * @brief Reset specific keywords on System VPD to default value.
254*fa5e4d32SSunny Srivastava      *
255*fa5e4d32SSunny Srivastava      * This API resets specific System VPD keywords to default value. The
256*fa5e4d32SSunny Srivastava      * keyword values are reset on:
257*fa5e4d32SSunny Srivastava      * 1. Primary EEPROM path.
258*fa5e4d32SSunny Srivastava      * 2. Secondary EEPROM path.
259*fa5e4d32SSunny Srivastava      * 3. D-Bus cache.
260*fa5e4d32SSunny Srivastava      * 4. Backup path.
261*fa5e4d32SSunny Srivastava      *
262*fa5e4d32SSunny Srivastava      * @return On success returns 0, otherwise returns -1.
263*fa5e4d32SSunny Srivastava      */
264*fa5e4d32SSunny Srivastava     int cleanSystemVpd() const noexcept;
265*fa5e4d32SSunny Srivastava 
266*fa5e4d32SSunny Srivastava     /**
267*fa5e4d32SSunny Srivastava      * @brief Dump all the inventory objects in JSON or table format to console.
268*fa5e4d32SSunny Srivastava      *
269*fa5e4d32SSunny Srivastava      * This API dumps specific properties of all the inventory objects to
270*fa5e4d32SSunny Srivastava      * console in JSON or table format to console. The inventory object paths
271*fa5e4d32SSunny Srivastava      * are extracted from PIM. For each object, the following properties are
272*fa5e4d32SSunny Srivastava      * dumped to console:
273*fa5e4d32SSunny Srivastava      * - Present property, Pretty Name, Location Code, Sub Model
274*fa5e4d32SSunny Srivastava      * - SN, PN, CC, FN, DR keywords under VINI record.
275*fa5e4d32SSunny Srivastava      * If the "Present" property of a FRU is false, the FRU is not dumped to
276*fa5e4d32SSunny Srivastava      * console.
277*fa5e4d32SSunny Srivastava      * FRUs whose object path end in "unit([0-9][0-9]?)" are also not dumped to
278*fa5e4d32SSunny Srivastava      * console.
279*fa5e4d32SSunny Srivastava      *
280*fa5e4d32SSunny Srivastava      * @param[in] i_dumpTable - Flag which specifies if the inventory should be
281*fa5e4d32SSunny Srivastava      * dumped in table format or not.
282*fa5e4d32SSunny Srivastava      *
283*fa5e4d32SSunny Srivastava      * @return On success returns 0, otherwise returns -1.
284*fa5e4d32SSunny Srivastava      */
285*fa5e4d32SSunny Srivastava     int dumpInventory(bool i_dumpTable = false) const noexcept;
286*fa5e4d32SSunny Srivastava };
287*fa5e4d32SSunny Srivastava } // namespace vpd
288