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