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{}; |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 24 | uint32_t offset = 0; |
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"); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 35 | app.add_option("--seek, -s", offset, |
| 36 | "User can provide VPD offset using this option. Default " |
| 37 | "offset value is 0. Using --offset is optional and is valid " |
| 38 | "only while using --Hardware/-H option."); |
| 39 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 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 | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 67 | ->needs(object) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 68 | ->needs(record) |
| 69 | ->needs(kw) |
| 70 | ->needs(valOption); |
| 71 | |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 72 | auto forceResetFlag = |
| 73 | app.add_flag("--forceReset, -f, -F", |
| 74 | "Force Collect for Hardware. CAUTION: Developer Only " |
| 75 | "Option. { vpd-tool-exe --forceReset/-f/-F }"); |
| 76 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 77 | auto Hardware = app.add_flag( |
| 78 | "--Hardware, -H", |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 79 | "This is a supplementary flag to read/write directly from/to hardware. " |
| 80 | "User should provide valid hardware/eeprom path (and not dbus object " |
| 81 | "path) in the -O/--object path. CAUTION: Developer Only Option"); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 82 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 83 | auto fixSystemVPDFlag = app.add_flag( |
| 84 | "--fixSystemVPD", "Use this option to interactively fix critical " |
| 85 | "system VPD keywords {vpd-tool-exe --fixSystemVPD}"); |
| 86 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 87 | CLI11_PARSE(app, argc, argv); |
| 88 | |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 89 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 90 | auto jsObject = json::parse(inventoryJson); |
| 91 | |
| 92 | try |
| 93 | { |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 94 | if (*Hardware) |
| 95 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 96 | if (!fs::exists(objectPath)) // if dbus object path is given or |
| 97 | // invalid eeprom path is given |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 98 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 99 | string errorMsg = "Invalid EEPROM path : "; |
| 100 | errorMsg += objectPath; |
| 101 | errorMsg += |
| 102 | ". The given EEPROM path doesn't exist. Provide valid " |
| 103 | "EEPROM path when -H flag is used. Refer help option. "; |
| 104 | throw runtime_error(errorMsg); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 105 | } |
| 106 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 107 | if (*dumpObjFlag) |
| 108 | { |
| 109 | VpdTool vpdToolObj(move(objectPath)); |
| 110 | vpdToolObj.dumpObject(jsObject); |
| 111 | } |
| 112 | |
| 113 | else if (*dumpInvFlag) |
| 114 | { |
| 115 | VpdTool vpdToolObj; |
| 116 | vpdToolObj.dumpInventory(jsObject); |
| 117 | } |
| 118 | |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 119 | else if (*readFlag && !*Hardware) |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 120 | { |
| 121 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 122 | move(keyword)); |
| 123 | vpdToolObj.readKeyword(); |
| 124 | } |
| 125 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 126 | else if (*writeFlag && !*Hardware) |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 127 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 128 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 129 | move(keyword), move(val)); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 130 | rc = vpdToolObj.updateKeyword(); |
| 131 | } |
| 132 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 133 | else if (*forceResetFlag) |
| 134 | { |
Priyanga Ramasamy | 335873f | 2022-05-18 01:31:54 -0500 | [diff] [blame] | 135 | // Force reset the BMC only if the CEC is powered OFF. |
| 136 | if (getPowerState() == |
| 137 | "xyz.openbmc_project.State.Chassis.PowerState.Off") |
| 138 | { |
| 139 | VpdTool vpdToolObj; |
| 140 | vpdToolObj.forceReset(jsObject); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | std::cerr << "The chassis power state is not Off. Force reset " |
| 145 | "operation is not allowed."; |
| 146 | return -1; |
| 147 | } |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 148 | } |
| 149 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 150 | else if (*writeFlag && *Hardware) |
| 151 | { |
PriyangaRamasamy | 83ea53f | 2021-05-13 05:55:21 -0500 | [diff] [blame] | 152 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 153 | move(keyword), move(val)); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 154 | rc = vpdToolObj.updateHardware(offset); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 155 | } |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 156 | else if (*readFlag && *Hardware) |
| 157 | { |
| 158 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 159 | move(keyword)); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 160 | vpdToolObj.readKwFromHw(offset); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 161 | } |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 162 | else if (*fixSystemVPDFlag) |
| 163 | { |
| 164 | VpdTool vpdToolObj; |
| 165 | rc = vpdToolObj.fixSystemVPD(); |
| 166 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 167 | else |
| 168 | { |
| 169 | throw runtime_error("One of the valid options is required. Refer " |
| 170 | "--help for list of options."); |
| 171 | } |
| 172 | } |
| 173 | |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 174 | catch (const exception& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 175 | { |
| 176 | cerr << e.what(); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 177 | |
| 178 | if (*Hardware) |
| 179 | { |
| 180 | std::cerr << "\nDid you provide a valid offset? By default VPD " |
| 181 | "offset is taken as 0. To input offset, use --offset. " |
| 182 | "Refer vpd-tool help."; |
| 183 | } |
Santosh Puranik | 6c7a84e | 2022-03-09 13:42:18 +0530 | [diff] [blame] | 184 | rc = -1; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 185 | } |
| 186 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 187 | return rc; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 188 | } |