xref: /openbmc/openpower-vpd-parser/app.cpp (revision 18bb985c)
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 
main(int argc,char ** argv)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         uint32_t vpdStartOffset = 0;
52         IpzVpdParser ipzParser(std::move(vpd), std::string{}, file,
53                                vpdStartOffset);
54         auto vpdStore = std::move(std::get<Store>(ipzParser.parse()));
55 
56         if (doDump)
57         {
58             vpdStore.dump();
59         }
60 
61         // Set FRU based on FRU type and object path
62         if (doFru)
63         {
64             using argList = std::vector<std::string>;
65             argList frus = std::move(arguments.at("fru"));
66             argList objects = std::move(arguments.at("object"));
67 
68             if (frus.size() != objects.size())
69             {
70                 std::cerr << "Unequal number of FRU types and object paths "
71                              "specified\n";
72                 rc = -1;
73             }
74             else
75             {
76                 // Write VPD to FRU inventory
77                 for (std::size_t index = 0; index < frus.size(); ++index)
78                 {
79                     inventory::write(frus[index], vpdStore, objects[index]);
80                 }
81             }
82         }
83     }
84     catch (const std::exception& e)
85     {
86         std::cerr << e.what() << "\n";
87     }
88 
89     return rc;
90 }
91