blob: 61518bd9c15b16027f5582521db082cb882d2b39 [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 "
37 "offset value is 0. Using --offset is optional and is valid "
38 "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 "
66 "keyword-name --value/-V value-to-be-updated }")
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050067 ->needs(object)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053068 ->needs(record)
69 ->needs(kw)
70 ->needs(valOption);
71
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050072 auto forceResetFlag =
73 app.add_flag("--forceReset, -f, -F",
74 "Force Collect for Hardware. CAUTION: Developer Only "
75 "Option. { vpd-tool-exe --forceReset/-f/-F }");
76
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053077 auto Hardware = app.add_flag(
78 "--Hardware, -H",
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -050079 "This is a supplementary flag to read/write directly from/to hardware. "
80 "User should provide valid hardware/eeprom path (and not dbus object "
81 "path) in the -O/--object path. CAUTION: Developer Only Option");
PriyangaRamasamy0407b172020-03-31 13:57:18 +053082
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -050083 auto fixSystemVPDFlag = app.add_flag(
84 "--fixSystemVPD", "Use this option to interactively fix critical "
85 "system VPD keywords {vpd-tool-exe --fixSystemVPD}");
86
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -050087 auto mfgClean =
88 app.add_flag("--mfgClean", "Flag to clean and reset specific keywords "
89 "on system VPD to its default value.");
90
91 auto confirm =
92 app.add_flag("--yes", "Using this flag with --mfgClean option, assumes "
93 "yes to proceed without confirmation.");
94
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053095 CLI11_PARSE(app, argc, argv);
96
Santosh Puranik0246a4d2020-11-04 16:57:39 +053097 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053098 auto jsObject = json::parse(inventoryJson);
99
100 try
101 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530102 if (*Hardware)
103 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500104 if (!fs::exists(objectPath)) // if dbus object path is given or
105 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530106 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500107 string errorMsg = "Invalid EEPROM path : ";
108 errorMsg += objectPath;
109 errorMsg +=
110 ". The given EEPROM path doesn't exist. Provide valid "
111 "EEPROM path when -H flag is used. Refer help option. ";
112 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530113 }
114 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530115 if (*dumpObjFlag)
116 {
117 VpdTool vpdToolObj(move(objectPath));
118 vpdToolObj.dumpObject(jsObject);
119 }
120
121 else if (*dumpInvFlag)
122 {
123 VpdTool vpdToolObj;
124 vpdToolObj.dumpInventory(jsObject);
125 }
126
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500127 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530128 {
129 VpdTool vpdToolObj(move(objectPath), move(recordName),
130 move(keyword));
131 vpdToolObj.readKeyword();
132 }
133
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530134 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530135 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500136 VpdTool vpdToolObj(move(objectPath), move(recordName),
137 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530138 rc = vpdToolObj.updateKeyword();
139 }
140
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530141 else if (*forceResetFlag)
142 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500143 // Force reset the BMC only if the CEC is powered OFF.
144 if (getPowerState() ==
145 "xyz.openbmc_project.State.Chassis.PowerState.Off")
146 {
147 VpdTool vpdToolObj;
148 vpdToolObj.forceReset(jsObject);
149 }
150 else
151 {
152 std::cerr << "The chassis power state is not Off. Force reset "
153 "operation is not allowed.";
154 return -1;
155 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530156 }
157
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530158 else if (*writeFlag && *Hardware)
159 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500160 VpdTool vpdToolObj(move(objectPath), move(recordName),
161 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500162 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530163 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500164 else if (*readFlag && *Hardware)
165 {
166 VpdTool vpdToolObj(move(objectPath), move(recordName),
167 move(keyword));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500168 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500169 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500170 else if (*fixSystemVPDFlag)
171 {
172 VpdTool vpdToolObj;
173 rc = vpdToolObj.fixSystemVPD();
174 }
Priyanga Ramasamy124ae6c2022-10-18 12:46:14 -0500175 else if (*mfgClean)
176 {
177 if (!*confirm)
178 {
179 std::string confirmation{};
180 std::cout << "\nThis option resets some of the system VPD "
181 "keywords to their default values. Do you really "
182 "wish to proceed further?[yes/no]: ";
183 std::cin >> confirmation;
184
185 if (confirmation != "yes")
186 {
187 return 0;
188 }
189 }
190 VpdTool vpdToolObj;
191 rc = vpdToolObj.cleanSystemVPD();
192 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530193 else
194 {
195 throw runtime_error("One of the valid options is required. Refer "
196 "--help for list of options.");
197 }
198 }
199
Patrick Williams8e15b932021-10-06 13:04:22 -0500200 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530201 {
202 cerr << e.what();
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500203
204 if (*Hardware)
205 {
206 std::cerr << "\nDid you provide a valid offset? By default VPD "
207 "offset is taken as 0. To input offset, use --offset. "
208 "Refer vpd-tool help.";
209 }
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530210 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530211 }
212
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530213 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530214}