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 | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 24 | |
| 25 | auto object = |
| 26 | app.add_option("--object, -O", objectPath, "Enter the Object Path"); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 27 | auto record = |
| 28 | app.add_option("--record, -R", recordName, "Enter the Record Name"); |
| 29 | auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword"); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 30 | auto valOption = app.add_option( |
| 31 | "--value, -V", val, |
| 32 | "Enter the value. The value to be updated should be either in ascii or " |
| 33 | "in hex. ascii eg: 01234; hex eg: 0x30313233"); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 34 | auto dumpObjFlag = |
| 35 | app.add_flag("--dumpObject, -o", |
| 36 | "Dump the given object from the inventory. { " |
| 37 | "vpd-tool-exe --dumpObject/-o --object/-O object-name }") |
| 38 | ->needs(object); |
| 39 | |
| 40 | auto dumpInvFlag = app.add_flag( |
| 41 | "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe " |
| 42 | "--dumpInventory/-i }"); |
| 43 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 44 | auto readFlag = |
| 45 | app.add_flag("--readKeyword, -r", |
| 46 | "Read the data of the given keyword. { " |
| 47 | "vpd-tool-exe --readKeyword/-r --object/-O " |
| 48 | "\"object-name\" --record/-R \"record-name\" --keyword/-K " |
| 49 | "\"keyword-name\" }") |
| 50 | ->needs(object) |
| 51 | ->needs(record) |
| 52 | ->needs(kw); |
| 53 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 54 | auto writeFlag = |
| 55 | app.add_flag( |
| 56 | "--writeKeyword, -w, --updateKeyword, -u", |
| 57 | "Update the value. { vpd-tool-exe " |
| 58 | "--writeKeyword/-w/--updateKeyword/-u " |
| 59 | "--object/-O object-name --record/-R record-name --keyword/-K " |
| 60 | "keyword-name --value/-V value-to-be-updated }") |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 61 | ->needs(object) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 62 | ->needs(record) |
| 63 | ->needs(kw) |
| 64 | ->needs(valOption); |
| 65 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 66 | auto forceResetFlag = app.add_flag( |
| 67 | "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe " |
| 68 | "--forceReset/-f/-F }"); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 69 | auto Hardware = app.add_flag( |
| 70 | "--Hardware, -H", |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 71 | "This is a supplementary flag to write directly to hardware. When the " |
| 72 | "-H flag is given, User should provide valid hardware/eeprom path (and " |
| 73 | "not dbus object path) in the -O/--object path."); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 74 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 75 | CLI11_PARSE(app, argc, argv); |
| 76 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 77 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 78 | auto jsObject = json::parse(inventoryJson); |
| 79 | |
| 80 | try |
| 81 | { |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 82 | if (*Hardware) |
| 83 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 84 | if (!fs::exists(objectPath)) // if dbus object path is given or |
| 85 | // invalid eeprom path is given |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 86 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 87 | string errorMsg = "Invalid EEPROM path : "; |
| 88 | errorMsg += objectPath; |
| 89 | errorMsg += |
| 90 | ". The given EEPROM path doesn't exist. Provide valid " |
| 91 | "EEPROM path when -H flag is used. Refer help option. "; |
| 92 | throw runtime_error(errorMsg); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 93 | } |
| 94 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 95 | if (*dumpObjFlag) |
| 96 | { |
| 97 | VpdTool vpdToolObj(move(objectPath)); |
| 98 | vpdToolObj.dumpObject(jsObject); |
| 99 | } |
| 100 | |
| 101 | else if (*dumpInvFlag) |
| 102 | { |
| 103 | VpdTool vpdToolObj; |
| 104 | vpdToolObj.dumpInventory(jsObject); |
| 105 | } |
| 106 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 107 | else if (*readFlag) |
| 108 | { |
| 109 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 110 | move(keyword)); |
| 111 | vpdToolObj.readKeyword(); |
| 112 | } |
| 113 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 114 | else if (*writeFlag && !*Hardware) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 115 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 116 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 117 | move(keyword), move(val)); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 118 | rc = vpdToolObj.updateKeyword(); |
| 119 | } |
| 120 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 121 | else if (*forceResetFlag) |
| 122 | { |
| 123 | VpdTool vpdToolObj; |
| 124 | vpdToolObj.forceReset(jsObject); |
| 125 | } |
| 126 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 127 | else if (*writeFlag && *Hardware) |
| 128 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 129 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 130 | move(keyword), move(val)); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 131 | rc = vpdToolObj.updateHardware(); |
| 132 | } |
| 133 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 134 | else |
| 135 | { |
| 136 | throw runtime_error("One of the valid options is required. Refer " |
| 137 | "--help for list of options."); |
| 138 | } |
| 139 | } |
| 140 | |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 141 | catch (const exception& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 142 | { |
| 143 | cerr << e.what(); |
| 144 | } |
| 145 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 146 | return rc; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 147 | } |