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