blob: ccbfe656bb41e3c69c649ffb28fd136eb9473f77 [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"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05003#include "ipz_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>
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050011#include <variant>
Patrick Venturec83c4dc2018-11-01 16:29:18 -070012#include <vector>
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060013
14int main(int argc, char** argv)
15{
16 int rc = 0;
17
18 try
19 {
20 using namespace openpower::vpd;
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050021 using namespace openpower::vpd::ipz::parser;
22
Patrick Venturec83c4dc2018-11-01 16:29:18 -070023 args::Args arguments = args::parse(argc, argv);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060024
Joel Stanley26c141b2020-04-17 13:09:22 +093025 bool haveVpd = arguments.count("vpd");
Joel Stanleyee72ab52020-04-17 13:13:19 +093026 bool doDump = arguments.count("dump");
Joel Stanley26c141b2020-04-17 13:09:22 +093027 bool doFru = arguments.count("fru") && arguments.count("object");
28
29 if (!haveVpd)
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060030 {
Joel Stanley26c141b2020-04-17 13:09:22 +093031 std::cerr << "VPD file required (--vpd=<filename>)\n";
32 return -1;
33 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060034
Joel Stanleyee72ab52020-04-17 13:13:19 +093035 if (!doDump && !doFru)
Joel Stanley26c141b2020-04-17 13:09:22 +093036 {
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 Stanleyee72ab52020-04-17 13:13:19 +093040 std::cerr << " Dump VPD: --dump\n\n";
Joel Stanley26c141b2020-04-17 13:09:22 +093041 return -1;
42 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060043
Joel Stanley26c141b2020-04-17 13:09:22 +093044 // 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
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050051 IpzVpdParser ipzParser(std::move(vpd));
52 auto vpdStore = std::move(std::get<Store>(ipzParser.parse()));
Joel Stanley26c141b2020-04-17 13:09:22 +093053
Joel Stanleyee72ab52020-04-17 13:13:19 +093054 if (doDump)
55 {
56 vpdStore.dump();
57 }
58
Joel Stanley26c141b2020-04-17 13:09:22 +093059 // Set FRU based on FRU type and object path
60 if (doFru)
61 {
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060062 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 Venturec83c4dc2018-11-01 16:29:18 -070069 "specified\n";
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060070 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 Venturec83c4dc2018-11-01 16:29:18 -070077 inventory::write(frus[index], vpdStore, objects[index]);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060078 }
79 }
80 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060081 }
Patrick Williams8e15b932021-10-06 13:04:22 -050082 catch (const std::exception& e)
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060083 {
84 std::cerr << e.what() << "\n";
85 }
86
87 return rc;
88}