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 | |
Oskar Senft | 8f51109 | 2018-12-04 13:53:16 -0500 | [diff] [blame] | 20 | static uint8_t parse_fruid_or_exit(const char* fruid_str, char** argv) |
| 21 | { |
| 22 | const uint8_t MAX_FRU_ID = 0xfe; |
| 23 | unsigned long fruid; |
| 24 | char* endptr = NULL; |
| 25 | |
| 26 | // The FRUID string must not be empty. |
| 27 | if (fruid_str == nullptr || *fruid_str == '\0') |
| 28 | { |
| 29 | exit_with_error("Empty fruid.", argv); |
| 30 | } |
| 31 | |
| 32 | errno = 0; |
| 33 | fruid = std::strtoul(fruid_str, &endptr, 16); |
| 34 | |
| 35 | // Handle error cases |
| 36 | if (errno == ERANGE) |
| 37 | { |
| 38 | exit_with_error("fruid is out of range.", argv); |
| 39 | } |
| 40 | if (errno != 0) |
| 41 | { |
| 42 | exit_with_error("Could not parse fruid.", argv); |
| 43 | } |
| 44 | if (*endptr != '\0') |
| 45 | { |
| 46 | // The string was not fully parsed, e.g. contains invalid characters |
| 47 | exit_with_error("Invalid fruid.", argv); |
| 48 | } |
| 49 | if (fruid > MAX_FRU_ID) |
| 50 | { |
| 51 | // The string was parsed, but the set FRUID is too large. |
| 52 | exit_with_error("fruid is out of range.", argv); |
| 53 | } |
| 54 | |
| 55 | return fruid; |
| 56 | } |
| 57 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 58 | //-------------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 59 | // This gets called by udev monitor soon after seeing hog plugs for EEPROMS. |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 60 | //-------------------------------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 61 | int main(int argc, char** argv) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 62 | { |
| 63 | int rc = 0; |
Oskar Senft | 8f51109 | 2018-12-04 13:53:16 -0500 | [diff] [blame] | 64 | uint8_t fruid; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 65 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 66 | // Read the arguments. |
| 67 | auto cli_options = std::make_unique<ArgumentParser>(argc, argv); |
| 68 | |
| 69 | // Parse out each argument. |
| 70 | auto eeprom_file = (*cli_options)["eeprom"]; |
| 71 | if (eeprom_file == ArgumentParser::empty_string) |
| 72 | { |
| 73 | // User has not passed in the appropriate argument value |
| 74 | exit_with_error("eeprom data not found.", argv); |
| 75 | } |
| 76 | |
| 77 | auto fruid_str = (*cli_options)["fruid"]; |
Dmitry Bazhenov | 05261aa | 2017-08-02 13:09:48 +0500 | [diff] [blame] | 78 | if (fruid_str == ArgumentParser::empty_string) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 79 | { |
| 80 | // User has not passed in the appropriate argument value |
| 81 | exit_with_error("fruid data not found.", argv); |
| 82 | } |
| 83 | |
| 84 | // Extract the fruid |
Oskar Senft | 8f51109 | 2018-12-04 13:53:16 -0500 | [diff] [blame] | 85 | fruid = parse_fruid_or_exit(fruid_str.c_str(), argv); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 86 | |
| 87 | // Finished getting options out, so release the parser. |
| 88 | cli_options.release(); |
| 89 | |
Patrick Venture | a8093a2 | 2018-10-21 09:07:11 -0700 | [diff] [blame] | 90 | // Now that we have the file that contains the eeprom data, go read it |
| 91 | // and update the Inventory DB. |
| 92 | auto bus = sdbusplus::bus::new_default(); |
| 93 | bool bmc_fru = true; |
| 94 | rc = validateFRUArea(fruid, eeprom_file.c_str(), bus, bmc_fru); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 95 | |
| 96 | return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 97 | } |