blob: a0699bb95a81c8faf3a71247163b97423cd1ccc0 [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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053087 CLI11_PARSE(app, argc, argv);
88
Santosh Puranik0246a4d2020-11-04 16:57:39 +053089 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053090 auto jsObject = json::parse(inventoryJson);
91
92 try
93 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053094 if (*Hardware)
95 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050096 if (!fs::exists(objectPath)) // if dbus object path is given or
97 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053098 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050099 string errorMsg = "Invalid EEPROM path : ";
100 errorMsg += objectPath;
101 errorMsg +=
102 ". The given EEPROM path doesn't exist. Provide valid "
103 "EEPROM path when -H flag is used. Refer help option. ";
104 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530105 }
106 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530107 if (*dumpObjFlag)
108 {
109 VpdTool vpdToolObj(move(objectPath));
110 vpdToolObj.dumpObject(jsObject);
111 }
112
113 else if (*dumpInvFlag)
114 {
115 VpdTool vpdToolObj;
116 vpdToolObj.dumpInventory(jsObject);
117 }
118
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500119 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530120 {
121 VpdTool vpdToolObj(move(objectPath), move(recordName),
122 move(keyword));
123 vpdToolObj.readKeyword();
124 }
125
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530126 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530127 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500128 VpdTool vpdToolObj(move(objectPath), move(recordName),
129 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530130 rc = vpdToolObj.updateKeyword();
131 }
132
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530133 else if (*forceResetFlag)
134 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500135 // Force reset the BMC only if the CEC is powered OFF.
136 if (getPowerState() ==
137 "xyz.openbmc_project.State.Chassis.PowerState.Off")
138 {
139 VpdTool vpdToolObj;
140 vpdToolObj.forceReset(jsObject);
141 }
142 else
143 {
144 std::cerr << "The chassis power state is not Off. Force reset "
145 "operation is not allowed.";
146 return -1;
147 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530148 }
149
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530150 else if (*writeFlag && *Hardware)
151 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500152 VpdTool vpdToolObj(move(objectPath), move(recordName),
153 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500154 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530155 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500156 else if (*readFlag && *Hardware)
157 {
158 VpdTool vpdToolObj(move(objectPath), move(recordName),
159 move(keyword));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500160 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500161 }
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500162 else if (*fixSystemVPDFlag)
163 {
164 VpdTool vpdToolObj;
165 rc = vpdToolObj.fixSystemVPD();
166 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530167 else
168 {
169 throw runtime_error("One of the valid options is required. Refer "
170 "--help for list of options.");
171 }
172 }
173
Patrick Williams8e15b932021-10-06 13:04:22 -0500174 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530175 {
176 cerr << e.what();
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500177
178 if (*Hardware)
179 {
180 std::cerr << "\nDid you provide a valid offset? By default VPD "
181 "offset is taken as 0. To input offset, use --offset. "
182 "Refer vpd-tool help.";
183 }
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530184 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530185 }
186
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530187 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530188}