PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 1 | #include "utils.hpp" |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 2 | #include "vpd_tool_impl.hpp" |
| 3 | |
| 4 | #include <CLI/CLI.hpp> |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 5 | #include <filesystem> |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 6 | #include <fstream> |
| 7 | #include <iostream> |
| 8 | |
| 9 | using namespace CLI; |
| 10 | using namespace std; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 11 | namespace fs = std::filesystem; |
| 12 | using namespace openpower::vpd; |
| 13 | using json = nlohmann::json; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 14 | |
| 15 | int main(int argc, char** argv) |
| 16 | { |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 17 | int rc = 0; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 18 | App app{"VPD Command line tool to dump the inventory and to read and " |
| 19 | "update the keywords"}; |
| 20 | |
| 21 | string objectPath{}; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 22 | string recordName{}; |
| 23 | string keyword{}; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 24 | string val{}; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 25 | string path{}; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 26 | |
| 27 | auto object = |
| 28 | app.add_option("--object, -O", objectPath, "Enter the Object Path"); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 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"); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 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"); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 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"); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 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 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 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 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 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 }") |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 68 | ->needs(pathOption) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 69 | ->needs(record) |
| 70 | ->needs(kw) |
| 71 | ->needs(valOption); |
| 72 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 73 | auto forceResetFlag = app.add_flag( |
| 74 | "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe " |
| 75 | "--forceReset/-f/-F }"); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 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."); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 82 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 83 | CLI11_PARSE(app, argc, argv); |
| 84 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 85 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 86 | auto jsObject = json::parse(inventoryJson); |
| 87 | |
| 88 | try |
| 89 | { |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 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 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 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 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 124 | else if (*readFlag) |
| 125 | { |
| 126 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 127 | move(keyword)); |
| 128 | vpdToolObj.readKeyword(); |
| 129 | } |
| 130 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 131 | else if (*writeFlag && !*Hardware) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 132 | { |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 133 | VpdTool vpdToolObj(move(path), move(recordName), move(keyword), |
| 134 | move(val)); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 135 | rc = vpdToolObj.updateKeyword(); |
| 136 | } |
| 137 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 138 | else if (*forceResetFlag) |
| 139 | { |
| 140 | VpdTool vpdToolObj; |
| 141 | vpdToolObj.forceReset(jsObject); |
| 142 | } |
| 143 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame^] | 144 | else if (*writeFlag && *Hardware) |
| 145 | { |
| 146 | VpdTool vpdToolObj(move(path), move(recordName), move(keyword), |
| 147 | move(val)); |
| 148 | rc = vpdToolObj.updateHardware(); |
| 149 | } |
| 150 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 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 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 163 | return rc; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 164 | } |