summaryrefslogtreecommitdiffstats
path: root/readeeprom.cpp
blob: f5fa1484dc6fbba42a2079782cc54a2ea6134094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "argument.hpp"
#include "writefrudata.hpp"

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <phosphor-logging/log.hpp>

using namespace phosphor::logging;

static void exit_with_error(const char* err, char** argv)
{
    ArgumentParser::usage(argv);
    std::cerr << std::endl;
    std::cerr << "ERROR: " << err << std::endl;
    exit(-1);
}

static uint8_t parse_fruid_or_exit(const char* fruid_str, char** argv)
{
    const uint8_t MAX_FRU_ID = 0xfe;
    unsigned long fruid;
    char* endptr = NULL;

    // The FRUID string must not be empty.
    if (fruid_str == nullptr || *fruid_str == '\0')
    {
        exit_with_error("Empty fruid.", argv);
    }

    errno = 0;
    fruid = std::strtoul(fruid_str, &endptr, 16);

    // Handle error cases
    if (errno == ERANGE)
    {
        exit_with_error("fruid is out of range.", argv);
    }
    if (errno != 0)
    {
        exit_with_error("Could not parse fruid.", argv);
    }
    if (*endptr != '\0')
    {
        // The string was not fully parsed, e.g. contains invalid characters
        exit_with_error("Invalid fruid.", argv);
    }
    if (fruid > MAX_FRU_ID)
    {
        // The string was parsed, but the set FRUID is too large.
        exit_with_error("fruid is out of range.", argv);
    }

    return fruid;
}

//--------------------------------------------------------------------------
// This gets called by udev monitor soon after seeing hog plugs for EEPROMS.
//--------------------------------------------------------------------------
int main(int argc, char** argv)
{
    int rc = 0;
    uint8_t fruid;

    // Read the arguments.
    auto cli_options = std::make_unique<ArgumentParser>(argc, argv);

    // Parse out each argument.
    auto eeprom_file = (*cli_options)["eeprom"];
    if (eeprom_file == ArgumentParser::empty_string)
    {
        // User has not passed in the appropriate argument value
        exit_with_error("eeprom data not found.", argv);
    }

    auto fruid_str = (*cli_options)["fruid"];
    if (fruid_str == ArgumentParser::empty_string)
    {
        // User has not passed in the appropriate argument value
        exit_with_error("fruid data not found.", argv);
    }

    // Extract the fruid
    fruid = parse_fruid_or_exit(fruid_str.c_str(), argv);

    // Finished getting options out, so release the parser.
    cli_options.release();

    // Now that we have the file that contains the eeprom data, go read it
    // and update the Inventory DB.
    auto bus = sdbusplus::bus::new_default();
    bool bmc_fru = true;
    rc = validateFRUArea(fruid, eeprom_file.c_str(), bus, bmc_fru);

    return (rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}
OpenPOWER on IntegriCloud