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