blob: bc5e37e392489fc03db6e19a11946d41f86d5f45 [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "vpd_tool_impl.hpp"
2
3#include <CLI/CLI.hpp>
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05304#include <filesystem>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05305#include <fstream>
6#include <iostream>
7
8using namespace CLI;
9using namespace std;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053010namespace fs = std::filesystem;
11using namespace openpower::vpd;
12using json = nlohmann::json;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053013
14int main(int argc, char** argv)
15{
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053016 int rc = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053017 App app{"VPD Command line tool to dump the inventory and to read and "
18 "update the keywords"};
19
20 string objectPath{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053021 string recordName{};
22 string keyword{};
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053023 string val{};
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050024 uint32_t offset = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053025
26 auto object =
27 app.add_option("--object, -O", objectPath, "Enter the Object Path");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053028 auto record =
29 app.add_option("--record, -R", recordName, "Enter the Record Name");
30 auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053031 auto valOption = app.add_option(
32 "--value, -V", val,
33 "Enter the value. The value to be updated should be either in ascii or "
34 "in hex. ascii eg: 01234; hex eg: 0x30313233");
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050035 app.add_option("--seek, -s", offset,
36 "User can provide VPD offset using this option. Default "
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060037 "offset value is 0. Using --seek is optional and is valid "
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050038 "only while using --Hardware/-H option.");
39
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053040 auto dumpObjFlag =
41 app.add_flag("--dumpObject, -o",
42 "Dump the given object from the inventory. { "
43 "vpd-tool-exe --dumpObject/-o --object/-O object-name }")
44 ->needs(object);
45
46 auto dumpInvFlag = app.add_flag(
47 "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe "
48 "--dumpInventory/-i }");
49
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053050 auto readFlag =
51 app.add_flag("--readKeyword, -r",
52 "Read the data of the given keyword. { "
53 "vpd-tool-exe --readKeyword/-r --object/-O "
54 "\"object-name\" --record/-R \"record-name\" --keyword/-K "
55 "\"keyword-name\" }")
56 ->needs(object)
57 ->needs(record)
58 ->needs(kw);
59
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053060 auto writeFlag =
61 app.add_flag(
62 "--writeKeyword, -w, --updateKeyword, -u",
63 "Update the value. { vpd-tool-exe "
64 "--writeKeyword/-w/--updateKeyword/-u "
65 "--object/-O object-name --record/-R record-name --keyword/-K "
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060066 "keyword-name --value/-V (or) --file }. Value can be given "
67 "directly via console using --value or via file using --file")
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050068 ->needs(object)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053069 ->needs(record)
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -060070 ->needs(kw);
71
72 auto fileOption = app.add_option(
73 "--file", val,
74 "Enter the file name with its absolute path. This option can be used "
75 "in read and write operations. When used in read, the read value will "
76 "be saved to this file and when used in write, the value to be written "
77 "will be taken from this file.");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053078
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050079 auto forceResetFlag =
80 app.add_flag("--forceReset, -f, -F",
81 "Force Collect for Hardware. CAUTION: Developer Only "
82 "Option. { vpd-tool-exe --forceReset/-f/-F }");
83
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053084 auto Hardware = app.add_flag(
85 "--Hardware, -H",
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050086 "This is a supplementary flag to read/write directly from/to hardware. "
87 "User should provide valid hardware/eeprom path (and not dbus object "
88 "path) in the -O/--object path. CAUTION: Developer Only Option");
PriyangaRamasamy0407b172020-03-31 13:57:18 +053089
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050090 auto fixSystemVPDFlag = app.add_flag(
91 "--fixSystemVPD", "Use this option to interactively fix critical "
92 "system VPD keywords {vpd-tool-exe --fixSystemVPD}");
93
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -050094 auto mfgClean =
95 app.add_flag("--mfgClean", "Flag to clean and reset specific keywords "
96 "on system VPD to its default value.");
97
98 auto confirm =
99 app.add_flag("--yes", "Using this flag with --mfgClean option, assumes "
100 "yes to proceed without confirmation.");
101
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530102 CLI11_PARSE(app, argc, argv);
103
Santosh Puranik0246a4d2020-11-04 16:57:39 +0530104 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530105 auto jsObject = json::parse(inventoryJson);
106
107 try
108 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600109 if ((*kw) && (keyword.size() != 2))
110 {
111 throw runtime_error("Keyword " + keyword + " not supported.");
112 }
113
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530114 if (*Hardware)
115 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500116 if (!fs::exists(objectPath)) // if dbus object path is given or
117 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530118 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500119 string errorMsg = "Invalid EEPROM path : ";
120 errorMsg += objectPath;
121 errorMsg +=
122 ". The given EEPROM path doesn't exist. Provide valid "
123 "EEPROM path when -H flag is used. Refer help option. ";
124 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530125 }
126 }
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600127
128 if (*writeFlag)
129 {
130 if ((!*fileOption) && (!*valOption))
131 {
132 throw runtime_error("Please provide the data that needs to be "
133 "updated. Use --value/--file to "
134 "input data. Refer --help.");
135 }
136
137 if ((*fileOption) && (!fs::exists(val)))
138 {
139 throw runtime_error("Please provide a valid file with absolute "
140 "path in --file.");
141 }
142 }
143
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530144 if (*dumpObjFlag)
145 {
146 VpdTool vpdToolObj(move(objectPath));
147 vpdToolObj.dumpObject(jsObject);
148 }
149
150 else if (*dumpInvFlag)
151 {
152 VpdTool vpdToolObj;
153 vpdToolObj.dumpInventory(jsObject);
154 }
155
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500156 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530157 {
158 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600159 move(keyword), move(val));
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530160 vpdToolObj.readKeyword();
161 }
162
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530163 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530164 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500165 VpdTool vpdToolObj(move(objectPath), move(recordName),
166 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530167 rc = vpdToolObj.updateKeyword();
168 }
169
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530170 else if (*forceResetFlag)
171 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500172 // Force reset the BMC only if the CEC is powered OFF.
173 if (getPowerState() ==
174 "xyz.openbmc_project.State.Chassis.PowerState.Off")
175 {
176 VpdTool vpdToolObj;
177 vpdToolObj.forceReset(jsObject);
178 }
179 else
180 {
181 std::cerr << "The chassis power state is not Off. Force reset "
182 "operation is not allowed.";
183 return -1;
184 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530185 }
186
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530187 else if (*writeFlag && *Hardware)
188 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500189 VpdTool vpdToolObj(move(objectPath), move(recordName),
190 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500191 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530192 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500193 else if (*readFlag && *Hardware)
194 {
195 VpdTool vpdToolObj(move(objectPath), move(recordName),
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600196 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500197 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500198 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500199 else if (*fixSystemVPDFlag)
200 {
201 VpdTool vpdToolObj;
202 rc = vpdToolObj.fixSystemVPD();
203 }
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -0500204 else if (*mfgClean)
205 {
206 if (!*confirm)
207 {
208 std::string confirmation{};
209 std::cout << "\nThis option resets some of the system VPD "
210 "keywords to their default values. Do you really "
211 "wish to proceed further?[yes/no]: ";
212 std::cin >> confirmation;
213
214 if (confirmation != "yes")
215 {
216 return 0;
217 }
218 }
219 VpdTool vpdToolObj;
220 rc = vpdToolObj.cleanSystemVPD();
221 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530222 else
223 {
224 throw runtime_error("One of the valid options is required. Refer "
225 "--help for list of options.");
226 }
227 }
228
Patrick Williams8e15b932021-10-06 13:04:22 -0500229 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530230 {
231 cerr << e.what();
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500232
233 if (*Hardware)
234 {
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600235 std::cerr << "Did you provide a valid offset? By default VPD "
236 "offset is taken as 0. To input offset, use --seek. "
237 "Refer vpd-tool help."
238 << std::endl;
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500239 }
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530240 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530241 }
242
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530243 return rc;
Priyanga Ramasamy5629fbc2023-03-01 08:17:19 -0600244}