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