blob: 96a8cc1c9f16ec787abbaf4771d3db82caf68b50 [file] [log] [blame]
Matthew Barth155c34f2016-10-18 14:33:17 -05001#include "argument.hpp"
2#include "writefrudata.hpp"
vishwa13555bd2015-11-10 12:10:38 -06003
Patrick Venture52f1f182018-10-17 13:05:03 -07004#include <cstdlib>
5#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -07006#include <iostream>
7#include <memory>
Patrick Venture07f635c2018-10-19 09:19:39 -07008#include <phosphor-logging/log.hpp>
9
10using namespace phosphor::logging;
Patrick Venturec9508db2018-10-16 17:18:43 -070011
vishwa13555bd2015-11-10 12:10:38 -060012static 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//--------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060021// This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
vishwa13555bd2015-11-10 12:10:38 -060022//--------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070023int main(int argc, char** argv)
vishwa13555bd2015-11-10 12:10:38 -060024{
25 int rc = 0;
26 uint8_t fruid = 0;
27
28 // Handle to per process system bus
Patrick Venturec9508db2018-10-16 17:18:43 -070029 sd_bus* bus_type = NULL;
vishwac93d6d42015-12-16 11:55:16 -060030
vishwa13555bd2015-11-10 12:10:38 -060031 // 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 Bazhenov05261aa2017-08-02 13:09:48 +050043 if (fruid_str == ArgumentParser::empty_string)
vishwa13555bd2015-11-10 12:10:38 -060044 {
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 Venture52f1f182018-10-17 13:05:03 -070050 fruid = std::strtol(fruid_str.c_str(), NULL, 16);
Patrick Venturec9508db2018-10-16 17:18:43 -070051 if (fruid == 0)
vishwa13555bd2015-11-10 12:10:38 -060052 {
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);
vishwac93d6d42015-12-16 11:55:16 -060062 if (rc < 0)
vishwa13555bd2015-11-10 12:10:38 -060063 {
Patrick Venture07f635c2018-10-19 09:19:39 -070064 log<level::ERR>("Failed to connect to system bus",
65 entry("ERRNO=%s", std::strerror(-rc)));
vishwa13555bd2015-11-10 12:10:38 -060066 }
67 else
68 {
Patrick Venturec9508db2018-10-16 17:18:43 -070069 // Now that we have the file that contains the eeprom data, go read it
70 // and update the Inventory DB.
vishwac93d6d42015-12-16 11:55:16 -060071 bool bmc_fru = true;
Patrick Venturec9508db2018-10-16 17:18:43 -070072 rc = ipmi_validate_fru_area(fruid, eeprom_file.c_str(), bus_type,
73 bmc_fru);
vishwa13555bd2015-11-10 12:10:38 -060074 }
75
76 // Cleanup
77 sd_bus_unref(bus_type);
78
79 return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
80}