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