Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 1 | #include "args.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 2 | #include "defines.hpp" |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 3 | #include "parser.hpp" |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 4 | #include "write.hpp" |
| 5 | |
| 6 | #include <exception> |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | #include <iterator> |
| 10 | #include <string> |
| 11 | #include <vector> |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 12 | |
| 13 | int main(int argc, char** argv) |
| 14 | { |
| 15 | int rc = 0; |
| 16 | |
| 17 | try |
| 18 | { |
| 19 | using namespace openpower::vpd; |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 20 | args::Args arguments = args::parse(argc, argv); |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 21 | |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 22 | bool haveVpd = arguments.count("vpd"); |
Joel Stanley | ee72ab5 | 2020-04-17 13:13:19 +0930 | [diff] [blame^] | 23 | bool doDump = arguments.count("dump"); |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 24 | bool doFru = arguments.count("fru") && arguments.count("object"); |
| 25 | |
| 26 | if (!haveVpd) |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 27 | { |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 28 | std::cerr << "VPD file required (--vpd=<filename>)\n"; |
| 29 | return -1; |
| 30 | } |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 31 | |
Joel Stanley | ee72ab5 | 2020-04-17 13:13:19 +0930 | [diff] [blame^] | 32 | if (!doDump && !doFru) |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 33 | { |
| 34 | std::cerr << "No task to perform\n\n"; |
| 35 | std::cerr << " Update FRU: --fru <type> --object <path>\n"; |
| 36 | std::cerr << " --fru <t1>,<t2> --object <p1>,<p2>\n\n"; |
Joel Stanley | ee72ab5 | 2020-04-17 13:13:19 +0930 | [diff] [blame^] | 37 | std::cerr << " Dump VPD: --dump\n\n"; |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 38 | return -1; |
| 39 | } |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 40 | |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 41 | // Read binary VPD file |
| 42 | auto file = arguments.at("vpd")[0]; |
| 43 | std::ifstream vpdFile(file, std::ios::binary); |
| 44 | Binary vpd((std::istreambuf_iterator<char>(vpdFile)), |
| 45 | std::istreambuf_iterator<char>()); |
| 46 | |
| 47 | // Parse VPD |
| 48 | auto vpdStore = parse(std::move(vpd)); |
| 49 | |
Joel Stanley | ee72ab5 | 2020-04-17 13:13:19 +0930 | [diff] [blame^] | 50 | if (doDump) |
| 51 | { |
| 52 | vpdStore.dump(); |
| 53 | } |
| 54 | |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame] | 55 | // Set FRU based on FRU type and object path |
| 56 | if (doFru) |
| 57 | { |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 58 | using argList = std::vector<std::string>; |
| 59 | argList frus = std::move(arguments.at("fru")); |
| 60 | argList objects = std::move(arguments.at("object")); |
| 61 | |
| 62 | if (frus.size() != objects.size()) |
| 63 | { |
| 64 | std::cerr << "Unequal number of FRU types and object paths " |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 65 | "specified\n"; |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 66 | rc = -1; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Write VPD to FRU inventory |
| 71 | for (std::size_t index = 0; index < frus.size(); ++index) |
| 72 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 73 | inventory::write(frus[index], vpdStore, objects[index]); |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 77 | } |
| 78 | catch (std::exception& e) |
| 79 | { |
| 80 | std::cerr << e.what() << "\n"; |
| 81 | } |
| 82 | |
| 83 | return rc; |
| 84 | } |