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"); |
| 23 | bool doFru = arguments.count("fru") && arguments.count("object"); |
| 24 | |
| 25 | if (!haveVpd) |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 26 | { |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame^] | 27 | std::cerr << "VPD file required (--vpd=<filename>)\n"; |
| 28 | return -1; |
| 29 | } |
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 | if (!doFru) |
| 32 | { |
| 33 | std::cerr << "No task to perform\n\n"; |
| 34 | std::cerr << " Update FRU: --fru <type> --object <path>\n"; |
| 35 | std::cerr << " --fru <t1>,<t2> --object <p1>,<p2>\n\n"; |
| 36 | return -1; |
| 37 | } |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 38 | |
Joel Stanley | 26c141b | 2020-04-17 13:09:22 +0930 | [diff] [blame^] | 39 | // Read binary VPD file |
| 40 | auto file = arguments.at("vpd")[0]; |
| 41 | std::ifstream vpdFile(file, std::ios::binary); |
| 42 | Binary vpd((std::istreambuf_iterator<char>(vpdFile)), |
| 43 | std::istreambuf_iterator<char>()); |
| 44 | |
| 45 | // Parse VPD |
| 46 | auto vpdStore = parse(std::move(vpd)); |
| 47 | |
| 48 | // Set FRU based on FRU type and object path |
| 49 | if (doFru) |
| 50 | { |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 51 | using argList = std::vector<std::string>; |
| 52 | argList frus = std::move(arguments.at("fru")); |
| 53 | argList objects = std::move(arguments.at("object")); |
| 54 | |
| 55 | if (frus.size() != objects.size()) |
| 56 | { |
| 57 | std::cerr << "Unequal number of FRU types and object paths " |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 58 | "specified\n"; |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 59 | rc = -1; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | // Write VPD to FRU inventory |
| 64 | for (std::size_t index = 0; index < frus.size(); ++index) |
| 65 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 66 | inventory::write(frus[index], vpdStore, objects[index]); |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Deepak Kodihalli | 51748cf | 2016-11-28 05:00:44 -0600 | [diff] [blame] | 70 | } |
| 71 | catch (std::exception& e) |
| 72 | { |
| 73 | std::cerr << e.what() << "\n"; |
| 74 | } |
| 75 | |
| 76 | return rc; |
| 77 | } |