blob: 67a52fc6d6112d63a1bb8a113faa696cebee6bbb [file] [log] [blame]
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001#pragma once
2
3#include "tool_utils.hpp"
4
5#include <nlohmann/json.hpp>
6
7#include <optional>
8#include <string>
9
10namespace 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 */
23class 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 *
Souvik Roy549d0902025-01-22 02:37:37 -060041 * @throw json::exception, std::out_of_range, std::bad_alloc
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050042 */
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 */
Patrick Williams43fedab2025-02-03 14:28:05 -050080 nlohmann::json getFruTypeProperty(
81 const std::string& i_objectPath) const noexcept;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -050082
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
Souvik Roy7f749a62025-02-10 22:23:41 -0600171 /**
172 * @brief API to get VPD value of keyword in BIOS Config Manager.
173 *
174 * For a given record and keyword, this API gets the associated BIOS
175 * attribute current value from BIOS Config Manager, by reading the
176 * attribute value from BIOS Config Manager, converts the BIOS attribute
177 * value to VPD format, and returns it.
178 *
179 * @param[in] i_recordName - Record name.
180 * @param[in] i_keywordName - Keyword name.
181 *
182 * @return On success returns the resultant keyword value in binary
183 * format, else returns empty value.
184 *
185 * @throw std::terminate, std::bad_alloc
186 */
187 types::BinaryVector getVpdValueInBiosConfigManager(
188 [[maybe_unused]] const std::string& i_recordName,
189 [[maybe_unused]] const std::string& i_keywordName) const;
190
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500191 public:
192 /**
193 * @brief Read keyword value.
194 *
195 * API to read VPD keyword's value from the given input path.
196 * If the provided i_onHardware option is true, read keyword's value from
197 * the hardware. Otherwise read keyword's value from DBus.
198 *
199 * @param[in] i_vpdPath - DBus object path or EEPROM path.
200 * @param[in] i_recordName - Record name.
201 * @param[in] i_keywordName - Keyword name.
202 * @param[in] i_onHardware - True if i_vpdPath is EEPROM path, false
203 * otherwise.
204 * @param[in] i_fileToSave - File path to save keyword's value, if not given
205 * result will redirect to a console.
206 *
207 * @return On success return 0, otherwise return -1.
208 */
209 int readKeyword(const std::string& i_vpdPath,
210 const std::string& i_recordName,
211 const std::string& i_keywordName, const bool i_onHardware,
212 const std::string& i_fileToSave = {});
213
214 /**
215 * @brief Dump the given inventory object in JSON format to console.
216 *
217 * For a given object path of a FRU, this API dumps the following properties
218 * of the FRU in JSON format to console:
219 * - Pretty Name, Location Code, Sub Model
220 * - SN, PN, CC, FN, DR keywords under VINI record.
221 * If the FRU's "Present" property is not true, the above properties are not
222 * dumped to console.
223 *
224 * @param[in] i_fruPath - DBus object path.
225 *
226 * @return On success returns 0, otherwise returns -1.
227 */
228 int dumpObject(std::string i_fruPath) const noexcept;
229
230 /**
231 * @brief API to fix system VPD keywords.
232 *
233 * The API to fix the system VPD keywords. Mainly used when there
234 * is a mismatch between the primary and backup(secondary) VPD. User can
235 * choose option to update all primary keywords value with corresponding
236 * backup keywords value or can choose primary keyword value to sync
237 * secondary VPD. Otherwise, user can also interactively choose different
238 * action for individual keyword.
239 *
240 * @return On success returns 0, otherwise returns -1.
241 */
242 int fixSystemVpd() const noexcept;
243
244 /**
245 * @brief Write keyword's value.
246 *
247 * API to update VPD keyword's value to the given input path.
248 * If i_onHardware value in true, i_vpdPath is considered has hardware path
249 * otherwise it will be considered as DBus object path.
250 *
251 * For provided DBus object path both primary path or secondary path will
252 * get updated, also redundant EEPROM(if any) path with new keyword's value.
253 *
254 * In case of hardware path, only given hardware path gets updated with new
255 * keyword’s value, any backup or redundant EEPROM (if exists) paths won't
256 * get updated.
257 *
258 * @param[in] i_vpdPath - DBus object path or EEPROM path.
259 * @param[in] i_recordName - Record name.
260 * @param[in] i_keywordName - Keyword name.
261 * @param[in] i_keywordValue - Keyword value.
262 * @param[in] i_onHardware - True if i_vpdPath is EEPROM path, false
263 * otherwise.
264 *
265 * @return On success returns 0, otherwise returns -1.
266 */
267 int writeKeyword(std::string i_vpdPath, const std::string& i_recordName,
268 const std::string& i_keywordName,
269 const std::string& i_keywordValue,
270 const bool i_onHardware) noexcept;
271
272 /**
273 * @brief Reset specific keywords on System VPD to default value.
274 *
275 * This API resets specific System VPD keywords to default value. The
276 * keyword values are reset on:
277 * 1. Primary EEPROM path.
278 * 2. Secondary EEPROM path.
279 * 3. D-Bus cache.
280 * 4. Backup path.
281 *
Souvik Roy7f749a62025-02-10 22:23:41 -0600282 * @param[in] i_syncBiosAttributesRequired - Flag which specifies whether
283 * BIOS attribute related keywords need to be synced from BIOS Config
284 * Manager instead of being reset to default value.
Souvik Roy63089422025-01-23 02:48:21 -0600285 *
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500286 * @return On success returns 0, otherwise returns -1.
287 */
Souvik Roy7f749a62025-02-10 22:23:41 -0600288 int cleanSystemVpd(
289 bool i_syncBiosAttributesRequired = false) const noexcept;
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500290
291 /**
292 * @brief Dump all the inventory objects in JSON or table format to console.
293 *
294 * This API dumps specific properties of all the inventory objects to
295 * console in JSON or table format to console. The inventory object paths
296 * are extracted from PIM. For each object, the following properties are
297 * dumped to console:
298 * - Present property, Pretty Name, Location Code, Sub Model
299 * - SN, PN, CC, FN, DR keywords under VINI record.
300 * If the "Present" property of a FRU is false, the FRU is not dumped to
301 * console.
302 * FRUs whose object path end in "unit([0-9][0-9]?)" are also not dumped to
303 * console.
304 *
305 * @param[in] i_dumpTable - Flag which specifies if the inventory should be
306 * dumped in table format or not.
307 *
308 * @return On success returns 0, otherwise returns -1.
309 */
310 int dumpInventory(bool i_dumpTable = false) const noexcept;
Anupama B R6be2c012025-01-23 02:59:27 -0600311
312 /**
313 * @brief Resets the VPD on DBus for all the Frus.
314 *
315 * API clears the inventory persisted data and restarts the phosphor
316 * inventory manager(PIM) DBus service and the VPD manager service. VPD
317 * manager service collects the VPD for all the FRU's listed on the system
318 * config JSON and calls PIM to publish VPD on DBus.
319 *
320 * @return On success returns 0, otherwise returns -1.
321 */
322 int resetVpdOnDbus();
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500323};
324} // namespace vpd