blob: 63290debd3041df0436cc3c225d7cb42984a7f17 [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
girik18bb9852022-11-16 05:48:13 -060051 uint32_t vpdStartOffset = 0;
52 IpzVpdParser ipzParser(std::move(vpd), std::string{}, file,
53 vpdStartOffset);
SunnySrivastava1984e12b1812020-05-26 02:23:11 -050054 auto vpdStore = std::move(std::get<Store>(ipzParser.parse()));
Joel Stanley26c141b2020-04-17 13:09:22 +093055
Joel Stanleyee72ab52020-04-17 13:13:19 +093056 if (doDump)
57 {
58 vpdStore.dump();
59 }
60
Joel Stanley26c141b2020-04-17 13:09:22 +093061 // Set FRU based on FRU type and object path
62 if (doFru)
63 {
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060064 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 "
Patrick Venturec83c4dc2018-11-01 16:29:18 -070071 "specified\n";
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060072 rc = -1;
73 }
74 else
75 {
76 // Write VPD to FRU inventory
77 for (std::size_t index = 0; index < frus.size(); ++index)
78 {
Patrick Venturec83c4dc2018-11-01 16:29:18 -070079 inventory::write(frus[index], vpdStore, objects[index]);
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060080 }
81 }
82 }
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060083 }
Patrick Williams8e15b932021-10-06 13:04:22 -050084 catch (const std::exception& e)
Deepak Kodihalli51748cf2016-11-28 05:00:44 -060085 {
86 std::cerr << e.what() << "\n";
87 }
88
89 return rc;
90}