blob: 948decd027b80c9e97ca37f8c672c48c44a92536 [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");
Joel Stanleyee72ab52020-04-17 13:13:19 +093023 bool doDump = arguments.count("dump");
Joel Stanley26c141b2020-04-17 13:09:22 +093024 bool doFru = arguments.count("fru") && arguments.count("object");
25
26 if (!haveVpd)
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060027 {
Joel Stanley26c141b2020-04-17 13:09:22 +093028 std::cerr << "VPD file required (--vpd=<filename>)\n";
29 return -1;
30 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060031
Joel Stanleyee72ab52020-04-17 13:13:19 +093032 if (!doDump && !doFru)
Joel Stanley26c141b2020-04-17 13:09:22 +093033 {
34 std::cerr << "No task to perform\n\n";
35 std::cerr << " Update FRU: --fru <type> --object <path>\n";
36 std::cerr << " --fru <t1>,<t2> --object <p1>,<p2>\n\n";
Joel Stanleyee72ab52020-04-17 13:13:19 +093037 std::cerr << " Dump VPD: --dump\n\n";
Joel Stanley26c141b2020-04-17 13:09:22 +093038 return -1;
39 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060040
Joel Stanley26c141b2020-04-17 13:09:22 +093041 // Read binary VPD file
42 auto file = arguments.at("vpd")[0];
43 std::ifstream vpdFile(file, std::ios::binary);
44 Binary vpd((std::istreambuf_iterator<char>(vpdFile)),
45 std::istreambuf_iterator<char>());
46
47 // Parse VPD
48 auto vpdStore = parse(std::move(vpd));
49
Joel Stanleyee72ab52020-04-17 13:13:19 +093050 if (doDump)
51 {
52 vpdStore.dump();
53 }
54
Joel Stanley26c141b2020-04-17 13:09:22 +093055 // Set FRU based on FRU type and object path
56 if (doFru)
57 {
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060058 using argList = std::vector<std::string>;
59 argList frus = std::move(arguments.at("fru"));
60 argList objects = std::move(arguments.at("object"));
61
62 if (frus.size() != objects.size())
63 {
64 std::cerr << "Unequal number of FRU types and object paths "
Patrick Venturec83c4dc2018-11-01 16:29:18 -070065 "specified\n";
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060066 rc = -1;
67 }
68 else
69 {
70 // Write VPD to FRU inventory
71 for (std::size_t index = 0; index < frus.size(); ++index)
72 {
Patrick Venturec83c4dc2018-11-01 16:29:18 -070073 inventory::write(frus[index], vpdStore, objects[index]);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060074 }
75 }
76 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060077 }
78 catch (std::exception& e)
79 {
80 std::cerr << e.what() << "\n";
81 }
82
83 return rc;
84}