blob: 9e763316de5fa8bc4da84cca1f1663dbae203b47 [file] [log] [blame]
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05301#include "utils.hpp"
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05302#include "vpd_tool_impl.hpp"
3
4#include <CLI/CLI.hpp>
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05305#include <filesystem>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05306#include <fstream>
7#include <iostream>
8
9using namespace CLI;
10using namespace std;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053011namespace fs = std::filesystem;
12using namespace openpower::vpd;
13using json = nlohmann::json;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053014
15int main(int argc, char** argv)
16{
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053017 int rc = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053018 App app{"VPD Command line tool to dump the inventory and to read and "
19 "update the keywords"};
20
21 string objectPath{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053022 string recordName{};
23 string keyword{};
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053024 string val{};
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053025 string path{};
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053026
27 auto object =
28 app.add_option("--object, -O", objectPath, "Enter the Object Path");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053029 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");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053032 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");
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053036 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");
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053040
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
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053051 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
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053061 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 }")
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053068 ->needs(pathOption)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053069 ->needs(record)
70 ->needs(kw)
71 ->needs(valOption);
72
PriyangaRamasamy0407b172020-03-31 13:57:18 +053073 auto forceResetFlag = app.add_flag(
74 "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe "
75 "--forceReset/-f/-F }");
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053076 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.");
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 {
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 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530112 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
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530124 else if (*readFlag)
125 {
126 VpdTool vpdToolObj(move(objectPath), move(recordName),
127 move(keyword));
128 vpdToolObj.readKeyword();
129 }
130
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530131 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530132 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530133 VpdTool vpdToolObj(move(path), move(recordName), move(keyword),
134 move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530135 rc = vpdToolObj.updateKeyword();
136 }
137
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530138 else if (*forceResetFlag)
139 {
140 VpdTool vpdToolObj;
141 vpdToolObj.forceReset(jsObject);
142 }
143
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530144 else if (*writeFlag && *Hardware)
145 {
146 VpdTool vpdToolObj(move(path), move(recordName), move(keyword),
147 move(val));
148 rc = vpdToolObj.updateHardware();
149 }
150
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530151 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
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530163 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530164}