blob: 2f0e666e70760785ecc7656b5b820631412bfa44 [file] [log] [blame]
Matthew Barth155c34f2016-10-18 14:33:17 -05001#include "argument.hpp"
2#include "writefrudata.hpp"
vishwa13555bd2015-11-10 12:10:38 -06003
Patrick Venture52f1f182018-10-17 13:05:03 -07004#include <cstdlib>
5#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -07006#include <iostream>
7#include <memory>
8
vishwa13555bd2015-11-10 12:10:38 -06009static void exit_with_error(const char* err, char** argv)
10{
11 ArgumentParser::usage(argv);
12 std::cerr << std::endl;
13 std::cerr << "ERROR: " << err << std::endl;
14 exit(-1);
15}
16
17//--------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060018// This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
vishwa13555bd2015-11-10 12:10:38 -060019//--------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070020int main(int argc, char** argv)
vishwa13555bd2015-11-10 12:10:38 -060021{
22 int rc = 0;
23 uint8_t fruid = 0;
24
25 // Handle to per process system bus
Patrick Venturec9508db2018-10-16 17:18:43 -070026 sd_bus* bus_type = NULL;
vishwac93d6d42015-12-16 11:55:16 -060027
vishwa13555bd2015-11-10 12:10:38 -060028 // Read the arguments.
29 auto cli_options = std::make_unique<ArgumentParser>(argc, argv);
30
31 // Parse out each argument.
32 auto eeprom_file = (*cli_options)["eeprom"];
33 if (eeprom_file == ArgumentParser::empty_string)
34 {
35 // User has not passed in the appropriate argument value
36 exit_with_error("eeprom data not found.", argv);
37 }
38
39 auto fruid_str = (*cli_options)["fruid"];
Dmitry Bazhenov05261aa2017-08-02 13:09:48 +050040 if (fruid_str == ArgumentParser::empty_string)
vishwa13555bd2015-11-10 12:10:38 -060041 {
42 // User has not passed in the appropriate argument value
43 exit_with_error("fruid data not found.", argv);
44 }
45
46 // Extract the fruid
Patrick Venture52f1f182018-10-17 13:05:03 -070047 fruid = std::strtol(fruid_str.c_str(), NULL, 16);
Patrick Venturec9508db2018-10-16 17:18:43 -070048 if (fruid == 0)
vishwa13555bd2015-11-10 12:10:38 -060049 {
50 // User has not passed in the appropriate argument value
51 exit_with_error("Invalid fruid.", argv);
52 }
53
54 // Finished getting options out, so release the parser.
55 cli_options.release();
56
57 // Get a handle to System Bus
58 rc = sd_bus_open_system(&bus_type);
vishwac93d6d42015-12-16 11:55:16 -060059 if (rc < 0)
vishwa13555bd2015-11-10 12:10:38 -060060 {
Patrick Venture52f1f182018-10-17 13:05:03 -070061 std::fprintf(stderr, "Failed to connect to system bus: %s\n",
62 std::strerror(-rc));
vishwa13555bd2015-11-10 12:10:38 -060063 }
64 else
65 {
Patrick Venturec9508db2018-10-16 17:18:43 -070066 // Now that we have the file that contains the eeprom data, go read it
67 // and update the Inventory DB.
vishwac93d6d42015-12-16 11:55:16 -060068 bool bmc_fru = true;
Patrick Venturec9508db2018-10-16 17:18:43 -070069 rc = ipmi_validate_fru_area(fruid, eeprom_file.c_str(), bus_type,
70 bmc_fru);
vishwa13555bd2015-11-10 12:10:38 -060071 }
72
73 // Cleanup
74 sd_bus_unref(bus_type);
75
76 return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
77}