blob: d53a5939a5edc922193e0e3ec96c62feb54715dc [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 {
Anupama B Ra36d6432024-09-19 09:14:04 +0530110 if (*object && objectPath.empty())
PriyangaRamasamy44fca012024-08-26 02:35:05 -0500111 {
112 throw runtime_error("Given path is empty.");
113 }
114
Anupama B Ra36d6432024-09-19 09:14:04 +0530115 if (*record && (recordName.size() != 4))
116 {
117 throw runtime_error("Record " + recordName + " not supported.");
118 }
119
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600120 if ((*kw) && (keyword.size() != 2))
121 {
122 throw runtime_error("Keyword " + keyword + " not supported.");
123 }
124
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530125 if (*Hardware)
126 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500127 if (!fs::exists(objectPath)) // if dbus object path is given or
128 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530129 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500130 string errorMsg = "Invalid EEPROM path : ";
131 errorMsg += objectPath;
132 errorMsg +=
133 ". The given EEPROM path doesn't exist. Provide valid "
134 "EEPROM path when -H flag is used. Refer help option. ";
135 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530136 }
137 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600138
139 if (*writeFlag)
140 {
PriyangaRamasamy44fca012024-08-26 02:35:05 -0500141 if (isReadOnlyEEPROM(objectPath, jsObject))
142 {
143 throw runtime_error("Read only EEPROM. Update not allowed.");
144 }
145
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600146 if ((!*fileOption) && (!*valOption))
147 {
148 throw runtime_error("Please provide the data that needs to be "
149 "updated. Use --value/--file to "
150 "input data. Refer --help.");
151 }
152
153 if ((*fileOption) && (!fs::exists(val)))
154 {
155 throw runtime_error("Please provide a valid file with absolute "
156 "path in --file.");
157 }
158 }
159
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530160 if (*dumpObjFlag)
161 {
162 VpdTool vpdToolObj(move(objectPath));
163 vpdToolObj.dumpObject(jsObject);
164 }
165
166 else if (*dumpInvFlag)
167 {
168 VpdTool vpdToolObj;
169 vpdToolObj.dumpInventory(jsObject);
170 }
171
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500172 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530173 {
174 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600175 move(keyword), move(val));
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530176 vpdToolObj.readKeyword();
177 }
178
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530179 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530180 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500181 VpdTool vpdToolObj(move(objectPath), move(recordName),
182 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530183 rc = vpdToolObj.updateKeyword();
184 }
185
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530186 else if (*forceResetFlag)
187 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500188 // Force reset the BMC only if the CEC is powered OFF.
189 if (getPowerState() ==
190 "xyz.openbmc_project.State.Chassis.PowerState.Off")
191 {
192 VpdTool vpdToolObj;
193 vpdToolObj.forceReset(jsObject);
194 }
195 else
196 {
197 std::cerr << "The chassis power state is not Off. Force reset "
198 "operation is not allowed.";
199 return -1;
200 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530201 }
202
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530203 else if (*writeFlag && *Hardware)
204 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500205 VpdTool vpdToolObj(move(objectPath), move(recordName),
206 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500207 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530208 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500209 else if (*readFlag && *Hardware)
210 {
211 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600212 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500213 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500214 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500215 else if (*fixSystemVPDFlag)
216 {
priyaram0eb8cac2023-09-20 09:16:46 +0530217 std::string backupEepromPath;
218 std::string backupInvPath;
219 findBackupVPDPaths(backupEepromPath, backupInvPath, jsObject);
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500220 VpdTool vpdToolObj;
priyaram0eb8cac2023-09-20 09:16:46 +0530221
222 if (backupEepromPath.empty())
223 {
224 rc = vpdToolObj.fixSystemVPD();
225 }
226 else
227 {
228 rc = vpdToolObj.fixSystemBackupVPD(backupEepromPath,
229 backupInvPath);
230 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500231 }
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -0500232 else if (*mfgClean)
233 {
234 if (!*confirm)
235 {
236 std::string confirmation{};
237 std::cout << "\nThis option resets some of the system VPD "
238 "keywords to their default values. Do you really "
239 "wish to proceed further?[yes/no]: ";
240 std::cin >> confirmation;
241
242 if (confirmation != "yes")
243 {
244 return 0;
245 }
246 }
247 VpdTool vpdToolObj;
248 rc = vpdToolObj.cleanSystemVPD();
249 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530250 else
251 {
252 throw runtime_error("One of the valid options is required. Refer "
253 "--help for list of options.");
254 }
255 }
256
Patrick Williams8e15b932021-10-06 13:04:22 -0500257 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530258 {
259 cerr << e.what();
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530260 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530261 }
262
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530263 return rc;
Patrick Williamsc78d8872023-05-10 07:50:56 -0500264}