blob: f5fa1484dc6fbba42a2079782cc54a2ea6134094 [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
Oskar Senft8f511092018-12-04 13:53:16 -050020static 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
vishwa13555bd2015-11-10 12:10:38 -060058//--------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060059// This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
vishwa13555bd2015-11-10 12:10:38 -060060//--------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070061int main(int argc, char** argv)
vishwa13555bd2015-11-10 12:10:38 -060062{
63 int rc = 0;
Oskar Senft8f511092018-12-04 13:53:16 -050064 uint8_t fruid;
vishwa13555bd2015-11-10 12:10:38 -060065
vishwa13555bd2015-11-10 12:10:38 -060066 // 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 Bazhenov05261aa2017-08-02 13:09:48 +050078 if (fruid_str == ArgumentParser::empty_string)
vishwa13555bd2015-11-10 12:10:38 -060079 {
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 Senft8f511092018-12-04 13:53:16 -050085 fruid = parse_fruid_or_exit(fruid_str.c_str(), argv);
vishwa13555bd2015-11-10 12:10:38 -060086
87 // Finished getting options out, so release the parser.
88 cli_options.release();
89
Patrick Venturea8093a22018-10-21 09:07:11 -070090 // 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);
vishwa13555bd2015-11-10 12:10:38 -060095
96 return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
97}