blob: 740d5cd5549abc3be5c95a8dd724289fe82d8a43 [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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053083 CLI11_PARSE(app, argc, argv);
84
Santosh Puranik0246a4d2020-11-04 16:57:39 +053085 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053086 auto jsObject = json::parse(inventoryJson);
87
88 try
89 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053090 if (*Hardware)
91 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050092 if (!fs::exists(objectPath)) // if dbus object path is given or
93 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053094 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050095 string errorMsg = "Invalid EEPROM path : ";
96 errorMsg += objectPath;
97 errorMsg +=
98 ". The given EEPROM path doesn't exist. Provide valid "
99 "EEPROM path when -H flag is used. Refer help option. ";
100 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530101 }
102 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530103 if (*dumpObjFlag)
104 {
105 VpdTool vpdToolObj(move(objectPath));
106 vpdToolObj.dumpObject(jsObject);
107 }
108
109 else if (*dumpInvFlag)
110 {
111 VpdTool vpdToolObj;
112 vpdToolObj.dumpInventory(jsObject);
113 }
114
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500115 else if (*readFlag && !*Hardware)
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530116 {
117 VpdTool vpdToolObj(move(objectPath), move(recordName),
118 move(keyword));
119 vpdToolObj.readKeyword();
120 }
121
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530122 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530123 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500124 VpdTool vpdToolObj(move(objectPath), move(recordName),
125 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530126 rc = vpdToolObj.updateKeyword();
127 }
128
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530129 else if (*forceResetFlag)
130 {
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500131 // Force reset the BMC only if the CEC is powered OFF.
132 if (getPowerState() ==
133 "xyz.openbmc_project.State.Chassis.PowerState.Off")
134 {
135 VpdTool vpdToolObj;
136 vpdToolObj.forceReset(jsObject);
137 }
138 else
139 {
140 std::cerr << "The chassis power state is not Off. Force reset "
141 "operation is not allowed.";
142 return -1;
143 }
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530144 }
145
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530146 else if (*writeFlag && *Hardware)
147 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500148 VpdTool vpdToolObj(move(objectPath), move(recordName),
149 move(keyword), move(val));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500150 rc = vpdToolObj.updateHardware(offset);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530151 }
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500152 else if (*readFlag && *Hardware)
153 {
154 VpdTool vpdToolObj(move(objectPath), move(recordName),
155 move(keyword));
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500156 vpdToolObj.readKwFromHw(offset);
Priyanga Ramasamy38031312021-10-07 16:39:13 -0500157 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530158 else
159 {
160 throw runtime_error("One of the valid options is required. Refer "
161 "--help for list of options.");
162 }
163 }
164
Patrick Williams8e15b932021-10-06 13:04:22 -0500165 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530166 {
167 cerr << e.what();
Priyanga Ramasamyc99a0b02022-06-08 14:53:39 -0500168
169 if (*Hardware)
170 {
171 std::cerr << "\nDid you provide a valid offset? By default VPD "
172 "offset is taken as 0. To input offset, use --offset. "
173 "Refer vpd-tool help.";
174 }
Santosh Puranik6c7a84e2022-03-09 13:42:18 +0530175 rc = -1;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530176 }
177
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530178 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530179}