Matthew Barth | 155c34f | 2016-10-18 14:33:17 -0500 | [diff] [blame] | 1 | #include "argument.hpp" |
| 2 | #include "writefrudata.hpp" |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 3 | |
Patrick Venture | 52f1f18 | 2018-10-17 13:05:03 -0700 | [diff] [blame] | 4 | #include <cstdlib> |
| 5 | #include <cstring> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 6 | #include <iostream> |
| 7 | #include <memory> |
| 8 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 9 | static 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 | //-------------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 18 | // This gets called by udev monitor soon after seeing hog plugs for EEPROMS. |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 19 | //-------------------------------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 20 | int main(int argc, char** argv) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 21 | { |
| 22 | int rc = 0; |
| 23 | uint8_t fruid = 0; |
| 24 | |
| 25 | // Handle to per process system bus |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 26 | sd_bus* bus_type = NULL; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 27 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 28 | // 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 Bazhenov | 05261aa | 2017-08-02 13:09:48 +0500 | [diff] [blame] | 40 | if (fruid_str == ArgumentParser::empty_string) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 41 | { |
| 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 Venture | 52f1f18 | 2018-10-17 13:05:03 -0700 | [diff] [blame] | 47 | fruid = std::strtol(fruid_str.c_str(), NULL, 16); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 48 | if (fruid == 0) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 49 | { |
| 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); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 59 | if (rc < 0) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 60 | { |
Patrick Venture | 52f1f18 | 2018-10-17 13:05:03 -0700 | [diff] [blame] | 61 | std::fprintf(stderr, "Failed to connect to system bus: %s\n", |
| 62 | std::strerror(-rc)); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 63 | } |
| 64 | else |
| 65 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 66 | // Now that we have the file that contains the eeprom data, go read it |
| 67 | // and update the Inventory DB. |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 68 | bool bmc_fru = true; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 69 | rc = ipmi_validate_fru_area(fruid, eeprom_file.c_str(), bus_type, |
| 70 | bmc_fru); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Cleanup |
| 74 | sd_bus_unref(bus_type); |
| 75 | |
| 76 | return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 77 | } |