blob: e38936d3193d7ce5be545b00ad5c26f7cc1867d3 [file] [log] [blame]
Deepak Kodihalli51748cf2016-11-28 05:00:44 -06001#include "args.hpp"
Patrick Venturec83c4dc2018-11-01 16:29:18 -07002#include "defines.hpp"
Deepak Kodihalli51748cf2016-11-28 05:00:44 -06003#include "parser.hpp"
Patrick Venturec83c4dc2018-11-01 16:29:18 -07004#include "write.hpp"
5
6#include <exception>
7#include <fstream>
8#include <iostream>
9#include <iterator>
10#include <string>
11#include <vector>
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060012
13int main(int argc, char** argv)
14{
15 int rc = 0;
16
17 try
18 {
19 using namespace openpower::vpd;
Patrick Venturec83c4dc2018-11-01 16:29:18 -070020 args::Args arguments = args::parse(argc, argv);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060021
Joel Stanley26c141b2020-04-17 13:09:22 +093022 bool haveVpd = arguments.count("vpd");
23 bool doFru = arguments.count("fru") && arguments.count("object");
24
25 if (!haveVpd)
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060026 {
Joel Stanley26c141b2020-04-17 13:09:22 +093027 std::cerr << "VPD file required (--vpd=<filename>)\n";
28 return -1;
29 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060030
Joel Stanley26c141b2020-04-17 13:09:22 +093031 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 Kodihalli51748cf2016-11-28 05:00:44 -060038
Joel Stanley26c141b2020-04-17 13:09:22 +093039 // 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 Kodihalli51748cf2016-11-28 05:00:44 -060051 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 Venturec83c4dc2018-11-01 16:29:18 -070058 "specified\n";
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060059 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 Venturec83c4dc2018-11-01 16:29:18 -070066 inventory::write(frus[index], vpdStore, objects[index]);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060067 }
68 }
69 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060070 }
71 catch (std::exception& e)
72 {
73 std::cerr << e.what() << "\n";
74 }
75
76 return rc;
77}