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> |
Patrick Venture | 07f635c | 2018-10-19 09:19:39 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
| 10 | using namespace phosphor::logging; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 11 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 12 | static void exit_with_error(const char* err, char** argv) |
| 13 | { |
| 14 | ArgumentParser::usage(argv); |
| 15 | std::cerr << std::endl; |
| 16 | std::cerr << "ERROR: " << err << std::endl; |
| 17 | exit(-1); |
| 18 | } |
| 19 | |
| 20 | //-------------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 21 | // This gets called by udev monitor soon after seeing hog plugs for EEPROMS. |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 22 | //-------------------------------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 23 | int main(int argc, char** argv) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 24 | { |
| 25 | int rc = 0; |
| 26 | uint8_t fruid = 0; |
| 27 | |
| 28 | // Handle to per process system bus |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 29 | sd_bus* bus_type = NULL; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 30 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 31 | // Read the arguments. |
| 32 | auto cli_options = std::make_unique<ArgumentParser>(argc, argv); |
| 33 | |
| 34 | // Parse out each argument. |
| 35 | auto eeprom_file = (*cli_options)["eeprom"]; |
| 36 | if (eeprom_file == ArgumentParser::empty_string) |
| 37 | { |
| 38 | // User has not passed in the appropriate argument value |
| 39 | exit_with_error("eeprom data not found.", argv); |
| 40 | } |
| 41 | |
| 42 | auto fruid_str = (*cli_options)["fruid"]; |
Dmitry Bazhenov | 05261aa | 2017-08-02 13:09:48 +0500 | [diff] [blame] | 43 | if (fruid_str == ArgumentParser::empty_string) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 44 | { |
| 45 | // User has not passed in the appropriate argument value |
| 46 | exit_with_error("fruid data not found.", argv); |
| 47 | } |
| 48 | |
| 49 | // Extract the fruid |
Patrick Venture | 52f1f18 | 2018-10-17 13:05:03 -0700 | [diff] [blame] | 50 | fruid = std::strtol(fruid_str.c_str(), NULL, 16); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 51 | if (fruid == 0) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 52 | { |
| 53 | // User has not passed in the appropriate argument value |
| 54 | exit_with_error("Invalid fruid.", argv); |
| 55 | } |
| 56 | |
| 57 | // Finished getting options out, so release the parser. |
| 58 | cli_options.release(); |
| 59 | |
| 60 | // Get a handle to System Bus |
| 61 | rc = sd_bus_open_system(&bus_type); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 62 | if (rc < 0) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 63 | { |
Patrick Venture | 07f635c | 2018-10-19 09:19:39 -0700 | [diff] [blame] | 64 | log<level::ERR>("Failed to connect to system bus", |
| 65 | entry("ERRNO=%s", std::strerror(-rc))); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 69 | // Now that we have the file that contains the eeprom data, go read it |
| 70 | // and update the Inventory DB. |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 71 | bool bmc_fru = true; |
Patrick Venture | 98072dc | 2018-10-20 09:31:35 -0700 | [diff] [blame] | 72 | rc = validateFRUArea(fruid, eeprom_file.c_str(), bus_type, bmc_fru); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Cleanup |
| 76 | sd_bus_unref(bus_type); |
| 77 | |
| 78 | return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 79 | } |