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