xref: /openbmc/openpower-vpd-parser/app.cpp (revision f31a91bc)
1 #include "args.hpp"
2 #include "defines.hpp"
3 #include "ipz_parser.hpp"
4 #include "write.hpp"
5 
6 #include <exception>
7 #include <fstream>
8 #include <iostream>
9 #include <iterator>
10 #include <string>
11 #include <variant>
12 #include <vector>
13 
14 int main(int argc, char** argv)
15 {
16     int rc = 0;
17 
18     try
19     {
20         using namespace openpower::vpd;
21         using namespace openpower::vpd::ipz::parser;
22 
23         args::Args arguments = args::parse(argc, argv);
24 
25         bool haveVpd = arguments.count("vpd");
26         bool doDump = arguments.count("dump");
27         bool doFru = arguments.count("fru") && arguments.count("object");
28 
29         if (!haveVpd)
30         {
31             std::cerr << "VPD file required (--vpd=<filename>)\n";
32             return -1;
33         }
34 
35         if (!doDump && !doFru)
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";
40             std::cerr << "  Dump VPD: --dump\n\n";
41             return -1;
42         }
43 
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
51         IpzVpdParser ipzParser(std::move(vpd), std::string{});
52         auto vpdStore = std::move(std::get<Store>(ipzParser.parse()));
53 
54         if (doDump)
55         {
56             vpdStore.dump();
57         }
58 
59         // Set FRU based on FRU type and object path
60         if (doFru)
61         {
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 "
69                              "specified\n";
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                 {
77                     inventory::write(frus[index], vpdStore, objects[index]);
78                 }
79             }
80         }
81     }
82     catch (const std::exception& e)
83     {
84         std::cerr << e.what() << "\n";
85     }
86 
87     return rc;
88 }
89