xref: /openbmc/openpower-vpd-parser/vpd_tool.cpp (revision 8e15b93ada4ab399db36dec5f525ff93bacb5353)
1 #include "vpd_tool_impl.hpp"
2 
3 #include <CLI/CLI.hpp>
4 #include <filesystem>
5 #include <fstream>
6 #include <iostream>
7 
8 using namespace CLI;
9 using namespace std;
10 namespace fs = std::filesystem;
11 using namespace openpower::vpd;
12 using json = nlohmann::json;
13 
14 int main(int argc, char** argv)
15 {
16     int rc = 0;
17     App app{"VPD Command line tool to dump the inventory and to read and "
18             "update the keywords"};
19 
20     string objectPath{};
21     string recordName{};
22     string keyword{};
23     string val{};
24 
25     auto object =
26         app.add_option("--object, -O", objectPath, "Enter the Object Path");
27     auto record =
28         app.add_option("--record, -R", recordName, "Enter the Record Name");
29     auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword");
30     auto valOption = app.add_option(
31         "--value, -V", val,
32         "Enter the value. The value to be updated should be either in ascii or "
33         "in hex. ascii eg: 01234; hex eg: 0x30313233");
34     auto dumpObjFlag =
35         app.add_flag("--dumpObject, -o",
36                      "Dump the given object from the inventory. { "
37                      "vpd-tool-exe --dumpObject/-o --object/-O object-name }")
38             ->needs(object);
39 
40     auto dumpInvFlag = app.add_flag(
41         "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe "
42                                "--dumpInventory/-i }");
43 
44     auto readFlag =
45         app.add_flag("--readKeyword, -r",
46                      "Read the data of the given keyword. { "
47                      "vpd-tool-exe --readKeyword/-r --object/-O "
48                      "\"object-name\" --record/-R \"record-name\" --keyword/-K "
49                      "\"keyword-name\" }")
50             ->needs(object)
51             ->needs(record)
52             ->needs(kw);
53 
54     auto writeFlag =
55         app.add_flag(
56                "--writeKeyword, -w, --updateKeyword, -u",
57                "Update the value. { vpd-tool-exe "
58                "--writeKeyword/-w/--updateKeyword/-u "
59                "--object/-O object-name --record/-R record-name --keyword/-K "
60                "keyword-name --value/-V value-to-be-updated }")
61             ->needs(object)
62             ->needs(record)
63             ->needs(kw)
64             ->needs(valOption);
65 
66     auto forceResetFlag = app.add_flag(
67         "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe "
68                                 "--forceReset/-f/-F }");
69     auto Hardware = app.add_flag(
70         "--Hardware, -H",
71         "This is a supplementary flag to write directly to hardware. When the "
72         "-H flag is given, User should provide valid hardware/eeprom path (and "
73         "not dbus object path) in the -O/--object path.");
74 
75     CLI11_PARSE(app, argc, argv);
76 
77     ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
78     auto jsObject = json::parse(inventoryJson);
79 
80     try
81     {
82         if (*Hardware)
83         {
84             if (!fs::exists(objectPath)) // if dbus object path is given or
85                                          // invalid eeprom path is given
86             {
87                 string errorMsg = "Invalid EEPROM path : ";
88                 errorMsg += objectPath;
89                 errorMsg +=
90                     ". The given EEPROM path doesn't exist. Provide valid "
91                     "EEPROM path when -H flag is used. Refer help option. ";
92                 throw runtime_error(errorMsg);
93             }
94         }
95         if (*dumpObjFlag)
96         {
97             VpdTool vpdToolObj(move(objectPath));
98             vpdToolObj.dumpObject(jsObject);
99         }
100 
101         else if (*dumpInvFlag)
102         {
103             VpdTool vpdToolObj;
104             vpdToolObj.dumpInventory(jsObject);
105         }
106 
107         else if (*readFlag)
108         {
109             VpdTool vpdToolObj(move(objectPath), move(recordName),
110                                move(keyword));
111             vpdToolObj.readKeyword();
112         }
113 
114         else if (*writeFlag && !*Hardware)
115         {
116             VpdTool vpdToolObj(move(objectPath), move(recordName),
117                                move(keyword), move(val));
118             rc = vpdToolObj.updateKeyword();
119         }
120 
121         else if (*forceResetFlag)
122         {
123             VpdTool vpdToolObj;
124             vpdToolObj.forceReset(jsObject);
125         }
126 
127         else if (*writeFlag && *Hardware)
128         {
129             VpdTool vpdToolObj(move(objectPath), move(recordName),
130                                move(keyword), move(val));
131             rc = vpdToolObj.updateHardware();
132         }
133 
134         else
135         {
136             throw runtime_error("One of the valid options is required. Refer "
137                                 "--help for list of options.");
138         }
139     }
140 
141     catch (const exception& e)
142     {
143         cerr << e.what();
144     }
145 
146     return rc;
147 }
148