xref: /openbmc/ipmi-fru-parser/readeeprom.cpp (revision 05261aa5)
1155c34fbSMatthew Barth #include <iostream>
2155c34fbSMatthew Barth #include <memory>
3155c34fbSMatthew Barth #include "argument.hpp"
4155c34fbSMatthew Barth #include "writefrudata.hpp"
5155c34fbSMatthew Barth 
6155c34fbSMatthew Barth static void exit_with_error(const char* err, char** argv)
7155c34fbSMatthew Barth {
8155c34fbSMatthew Barth     ArgumentParser::usage(argv);
9155c34fbSMatthew Barth     std::cerr << std::endl;
10155c34fbSMatthew Barth     std::cerr << "ERROR: " << err << std::endl;
11155c34fbSMatthew Barth     exit(-1);
12155c34fbSMatthew Barth }
13155c34fbSMatthew Barth 
14155c34fbSMatthew Barth //--------------------------------------------------------------------------
15155c34fbSMatthew Barth // This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
16155c34fbSMatthew Barth //--------------------------------------------------------------------------
17155c34fbSMatthew Barth int main(int argc, char **argv)
18155c34fbSMatthew Barth {
19155c34fbSMatthew Barth     int rc = 0;
20155c34fbSMatthew Barth     uint8_t fruid = 0;
21155c34fbSMatthew Barth 
22155c34fbSMatthew Barth     // Handle to per process system bus
23155c34fbSMatthew Barth     sd_bus *bus_type = NULL;
24155c34fbSMatthew Barth 
25155c34fbSMatthew Barth     // Read the arguments.
26155c34fbSMatthew Barth     auto cli_options = std::make_unique<ArgumentParser>(argc, argv);
27155c34fbSMatthew Barth 
28155c34fbSMatthew Barth     // Parse out each argument.
29155c34fbSMatthew Barth     auto eeprom_file = (*cli_options)["eeprom"];
30155c34fbSMatthew Barth     if (eeprom_file == ArgumentParser::empty_string)
31155c34fbSMatthew Barth     {
32155c34fbSMatthew Barth         // User has not passed in the appropriate argument value
33155c34fbSMatthew Barth         exit_with_error("eeprom data not found.", argv);
34155c34fbSMatthew Barth     }
35155c34fbSMatthew Barth 
36155c34fbSMatthew Barth     auto fruid_str = (*cli_options)["fruid"];
37*05261aa5SDmitry Bazhenov     if (fruid_str == ArgumentParser::empty_string)
38155c34fbSMatthew Barth     {
39155c34fbSMatthew Barth         // User has not passed in the appropriate argument value
40155c34fbSMatthew Barth         exit_with_error("fruid data not found.", argv);
41155c34fbSMatthew Barth     }
42155c34fbSMatthew Barth 
43155c34fbSMatthew Barth     // Extract the fruid
44155c34fbSMatthew Barth     fruid = strtol(fruid_str.c_str(), NULL, 16);
45155c34fbSMatthew Barth     if(fruid == 0)
46155c34fbSMatthew Barth     {
47155c34fbSMatthew Barth         // User has not passed in the appropriate argument value
48155c34fbSMatthew Barth         exit_with_error("Invalid fruid.", argv);
49155c34fbSMatthew Barth     }
50155c34fbSMatthew Barth 
51155c34fbSMatthew Barth     // Finished getting options out, so release the parser.
52155c34fbSMatthew Barth     cli_options.release();
53155c34fbSMatthew Barth 
54155c34fbSMatthew Barth     // Get a handle to System Bus
55155c34fbSMatthew Barth     rc = sd_bus_open_system(&bus_type);
56155c34fbSMatthew Barth     if (rc < 0)
57155c34fbSMatthew Barth     {
58155c34fbSMatthew Barth         fprintf(stderr, "Failed to connect to system bus: %s\n",strerror(-rc));
59155c34fbSMatthew Barth     }
60155c34fbSMatthew Barth     else
61155c34fbSMatthew Barth     {
62155c34fbSMatthew Barth         // Now that we have the file that contains the eeprom data, go read it and
63155c34fbSMatthew Barth         // update the Inventory DB.
64155c34fbSMatthew Barth         bool bmc_fru = true;
65155c34fbSMatthew Barth         rc = ipmi_validate_fru_area(fruid, eeprom_file.c_str(), bus_type, bmc_fru);
66155c34fbSMatthew Barth     }
67155c34fbSMatthew Barth 
68155c34fbSMatthew Barth     // Cleanup
69155c34fbSMatthew Barth     sd_bus_unref(bus_type);
70155c34fbSMatthew Barth 
71155c34fbSMatthew Barth     return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
72155c34fbSMatthew Barth }
73