blob: d23fe7fcf3d447c34c740898b21dab91dab7fa50 [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "vpd_tool_impl.hpp"
2
3#include <CLI/CLI.hpp>
Patrick Williamsc78d8872023-05-10 07:50:56 -05004
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05305#include <filesystem>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05306#include <fstream>
7#include <iostream>
8
9using namespace CLI;
10using namespace std;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053011namespace fs = std::filesystem;
12using namespace openpower::vpd;
13using json = nlohmann::json;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053014
15int main(int argc, char** argv)
16{
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053017 int rc = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053018 App app{"VPD Command line tool to dump the inventory and to read and "
19 "update the keywords"};
20
21 string objectPath{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053022 string recordName{};
23 string keyword{};
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053024 string val{};
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050025 uint32_t offset = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053026
Patrick Williams08dc31c2024-08-16 15:21:06 -040027 auto object =
28 app.add_option("--object, -O", objectPath, "Enter the Object Path");
29 auto record =
30 app.add_option("--record, -R", recordName, "Enter the Record Name");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053031 auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053032 auto valOption = app.add_option(
33 "--value, -V", val,
34 "Enter the value. The value to be updated should be either in ascii or "
35 "in hex. ascii eg: 01234; hex eg: 0x30313233");
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050036 app.add_option("--seek, -s", offset,
37 "User can provide VPD offset using this option. Default "
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060038 "offset value is 0. Using --seek is optional and is valid "
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050039 "only while using --Hardware/-H option.");
40
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053041 auto dumpObjFlag =
42 app.add_flag("--dumpObject, -o",
43 "Dump the given object from the inventory. { "
44 "vpd-tool-exe --dumpObject/-o --object/-O object-name }")
45 ->needs(object);
46
47 auto dumpInvFlag = app.add_flag(
48 "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe "
49 "--dumpInventory/-i }");
50
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053051 auto readFlag =
52 app.add_flag("--readKeyword, -r",
53 "Read the data of the given keyword. { "
54 "vpd-tool-exe --readKeyword/-r --object/-O "
55 "\"object-name\" --record/-R \"record-name\" --keyword/-K "
56 "\"keyword-name\" }")
57 ->needs(object)
58 ->needs(record)
59 ->needs(kw);
60
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053061 auto writeFlag =
62 app.add_flag(
63 "--writeKeyword, -w, --updateKeyword, -u",
64 "Update the value. { vpd-tool-exe "
65 "--writeKeyword/-w/--updateKeyword/-u "
66 "--object/-O object-name --record/-R record-name --keyword/-K "
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060067 "keyword-name --value/-V (or) --file }. Value can be given "
68 "directly via console using --value or via file using --file")
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050069 ->needs(object)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053070 ->needs(record)
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060071 ->needs(kw);
72
73 auto fileOption = app.add_option(
74 "--file", val,
75 "Enter the file name with its absolute path. This option can be used "
76 "in read and write operations. When used in read, the read value will "
77 "be saved to this file and when used in write, the value to be written "
78 "will be taken from this file.");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053079
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050080 auto forceResetFlag =
81 app.add_flag("--forceReset, -f, -F",
82 "Force Collect for Hardware. CAUTION: Developer Only "
83 "Option. { vpd-tool-exe --forceReset/-f/-F }");
84
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053085 auto Hardware = app.add_flag(
86 "--Hardware, -H",
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050087 "This is a supplementary flag to read/write directly from/to hardware. "
88 "User should provide valid hardware/eeprom path (and not dbus object "
89 "path) in the -O/--object path. CAUTION: Developer Only Option");
PriyangaRamasamy0407b172020-03-31 13:57:18 +053090
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050091 auto fixSystemVPDFlag = app.add_flag(
92 "--fixSystemVPD", "Use this option to interactively fix critical "
93 "system VPD keywords {vpd-tool-exe --fixSystemVPD}");
94
Patrick Williams08dc31c2024-08-16 15:21:06 -040095 auto mfgClean =
96 app.add_flag("--mfgClean", "Flag to clean and reset specific keywords "
97 "on system VPD to its default value.");
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -050098
99 auto confirm =
100 app.add_flag("--yes", "Using this flag with --mfgClean option, assumes "
101 "yes to proceed without confirmation.");
102
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530103 CLI11_PARSE(app, argc, argv);
104
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530105 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530106 auto jsObject = json::parse(inventoryJson);
107
108 try
109 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600110 if ((*kw) && (keyword.size() != 2))
111 {
112 throw runtime_error("Keyword " + keyword + " not supported.");
113 }
114
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530115 if (*Hardware)
116 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500117 if (!fs::exists(objectPath)) // if dbus object path is given or
118 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530119 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500120 string errorMsg = "Invalid EEPROM path : ";
121 errorMsg += objectPath;
122 errorMsg +=
123 ". The given EEPROM path doesn't exist. Provide valid "
124 "EEPROM path when -H flag is used. Refer help option. ";
125 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530126 }
127 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600128
129 if (*writeFlag)
130 {
131 if ((!*fileOption) && (!*valOption))
132 {
133 throw runtime_error("Please provide the data that needs to be "
134 "updated. Use --value/--file to "
135 "input data. Refer --help.");
136 }
137
138 if ((*fileOption) && (!fs::exists(val)))
139 {
140 throw runtime_error("Please provide a valid file with absolute "
141 "path in --file.");
142 }
143 }
144
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530145 if (*dumpObjFlag)
146 {
147 VpdTool vpdToolObj(move(objectPath));
148 vpdToolObj.dumpObject(jsObject);
149 }
150
151 else if (*dumpInvFlag)
152 {
153 VpdTool vpdToolObj;
154 vpdToolObj.dumpInventory(jsObject);
155 }
156
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500157 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530158 {
159 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600160 move(keyword), move(val));
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530161 vpdToolObj.readKeyword();
162 }
163
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530164 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530165 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500166 VpdTool vpdToolObj(move(objectPath), move(recordName),
167 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530168 rc = vpdToolObj.updateKeyword();
169 }
170
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530171 else if (*forceResetFlag)
172 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500173 // Force reset the BMC only if the CEC is powered OFF.
174 if (getPowerState() ==
175 "xyz.openbmc_project.State.Chassis.PowerState.Off")
176 {
177 VpdTool vpdToolObj;
178 vpdToolObj.forceReset(jsObject);
179 }
180 else
181 {
182 std::cerr << "The chassis power state is not Off. Force reset "
183 "operation is not allowed.";
184 return -1;
185 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530186 }
187
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530188 else if (*writeFlag && *Hardware)
189 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500190 VpdTool vpdToolObj(move(objectPath), move(recordName),
191 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500192 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530193 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500194 else if (*readFlag && *Hardware)
195 {
196 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600197 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500198 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500199 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500200 else if (*fixSystemVPDFlag)
201 {
priyaram0eb8cac2023-09-20 09:16:46 +0530202 std::string backupEepromPath;
203 std::string backupInvPath;
204 findBackupVPDPaths(backupEepromPath, backupInvPath, jsObject);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500205 VpdTool vpdToolObj;
priyaram0eb8cac2023-09-20 09:16:46 +0530206
207 if (backupEepromPath.empty())
208 {
209 rc = vpdToolObj.fixSystemVPD();
210 }
211 else
212 {
213 rc = vpdToolObj.fixSystemBackupVPD(backupEepromPath,
214 backupInvPath);
215 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500216 }
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -0500217 else if (*mfgClean)
218 {
219 if (!*confirm)
220 {
221 std::string confirmation{};
222 std::cout << "\nThis option resets some of the system VPD "
223 "keywords to their default values. Do you really "
224 "wish to proceed further?[yes/no]: ";
225 std::cin >> confirmation;
226
227 if (confirmation != "yes")
228 {
229 return 0;
230 }
231 }
232 VpdTool vpdToolObj;
233 rc = vpdToolObj.cleanSystemVPD();
234 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530235 else
236 {
237 throw runtime_error("One of the valid options is required. Refer "
238 "--help for list of options.");
239 }
240 }
241
Patrick Williams8e15b932021-10-06 13:04:22 -0500242 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530243 {
244 cerr << e.what();
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530245 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530246 }
247
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530248 return rc;
Patrick Williamsc78d8872023-05-10 07:50:56 -0500249}