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 string path{}; 25 26 auto object = 27 app.add_option("--object, -O", objectPath, "Enter the Object Path"); 28 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"); 31 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"); 35 auto pathOption = 36 app.add_option("--path, -P", path, 37 "Path - if hardware option is used, give either EEPROM " 38 "path/Object path; if not give the object path"); 39 40 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 50 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 60 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 }") 67 ->needs(pathOption) 68 ->needs(record) 69 ->needs(kw) 70 ->needs(valOption); 71 72 auto forceResetFlag = app.add_flag( 73 "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe " 74 "--forceReset/-f/-F }"); 75 auto Hardware = app.add_flag( 76 "--Hardware, -H", 77 "This is a supplementary flag to read/write directly from/to hardware. " 78 "Enter the hardware path while using the object option in " 79 "corresponding read/write flags. This --Hardware flag is to be given " 80 "along with readKeyword/writeKeyword."); 81 82 CLI11_PARSE(app, argc, argv); 83 84 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); 85 auto jsObject = json::parse(inventoryJson); 86 87 try 88 { 89 if (*Hardware) 90 { 91 if (!fs::exists(path)) // dbus object path 92 { 93 string p = getVpdFilePath(INVENTORY_JSON_SYM_LINK, path); 94 if (p.empty()) // object path not present in inventory json 95 { 96 string errorMsg = "Invalid object path : "; 97 errorMsg += path; 98 errorMsg += ". Unable to find the corresponding EEPROM " 99 "path for the given object path : "; 100 errorMsg += path; 101 errorMsg += " in the vpd inventory json : "; 102 errorMsg += INVENTORY_JSON_SYM_LINK; 103 throw runtime_error(errorMsg); 104 } 105 else 106 { 107 path = p; 108 } 109 } 110 } 111 if (*dumpObjFlag) 112 { 113 VpdTool vpdToolObj(move(objectPath)); 114 vpdToolObj.dumpObject(jsObject); 115 } 116 117 else if (*dumpInvFlag) 118 { 119 VpdTool vpdToolObj; 120 vpdToolObj.dumpInventory(jsObject); 121 } 122 123 else if (*readFlag) 124 { 125 VpdTool vpdToolObj(move(objectPath), move(recordName), 126 move(keyword)); 127 vpdToolObj.readKeyword(); 128 } 129 130 else if (*writeFlag && !*Hardware) 131 { 132 VpdTool vpdToolObj(move(path), move(recordName), move(keyword), 133 move(val)); 134 rc = vpdToolObj.updateKeyword(); 135 } 136 137 else if (*forceResetFlag) 138 { 139 VpdTool vpdToolObj; 140 vpdToolObj.forceReset(jsObject); 141 } 142 143 else if (*writeFlag && *Hardware) 144 { 145 VpdTool vpdToolObj(move(path), move(recordName), move(keyword), 146 move(val)); 147 rc = vpdToolObj.updateHardware(); 148 } 149 150 else 151 { 152 throw runtime_error("One of the valid options is required. Refer " 153 "--help for list of options."); 154 } 155 } 156 157 catch (exception& e) 158 { 159 cerr << e.what(); 160 } 161 162 return rc; 163 } 164