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