1 #include "writefrudata.hpp"
2 
3 #include <ipmid/api.h>
4 #include <unistd.h>
5 
6 #include <cstdio>
7 #include <cstring>
8 #include <phosphor-logging/log.hpp>
9 #include <sdbusplus/bus.hpp>
10 
11 void register_netfn_storage_write_fru() __attribute__((constructor));
12 
13 sd_bus* ipmid_get_sd_bus_connection(void);
14 
15 using namespace phosphor::logging;
16 
17 ///-------------------------------------------------------
18 // Called by IPMI netfn router for write fru data command
19 //--------------------------------------------------------
20 ipmi_ret_t ipmiStorageWriteFruData(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
21                                    ipmi_request_t request,
22                                    ipmi_response_t response,
23                                    ipmi_data_len_t dataLen,
24                                    ipmi_context_t context)
25 {
26     FILE* fp = NULL;
27     char fruFilename[16] = {0};
28     uint8_t offset = 0;
29     uint16_t len = 0;
30     ipmi_ret_t rc = IPMI_CC_INVALID;
31     const char* mode = NULL;
32 
33     // From the payload, extract the header that has fruid and the offsets
34     auto reqptr = static_cast<write_fru_data_t*>(request);
35 
36     // Maintaining a temporary file to pump the data
37     std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
38 
39     offset = ((uint16_t)reqptr->offsetms) << 8 | reqptr->offsetls;
40 
41     // Length is the number of request bytes minus the header itself.
42     // The header contains an extra byte to indicate the start of
43     // the data (so didn't need to worry about word/byte boundaries)
44     // hence the -1...
45     len = ((uint16_t)*dataLen) - (sizeof(write_fru_data_t) - 1);
46 
47     // On error there is no response data for this command.
48     *dataLen = 0;
49 
50 #ifdef __IPMI__DEBUG__
51     log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fruFilename),
52                       entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
53 #endif
54 
55     if (access(fruFilename, F_OK) == -1)
56     {
57         mode = "wb";
58     }
59     else
60     {
61         mode = "rb+";
62     }
63 
64     if ((fp = std::fopen(fruFilename, mode)) != NULL)
65     {
66         if (std::fseek(fp, offset, SEEK_SET))
67         {
68             log<level::ERR>("Seek into fru file failed",
69                             entry("FILE=%s", fruFilename),
70                             entry("ERRNO=%s", std::strerror(errno)));
71             std::fclose(fp);
72             return rc;
73         }
74 
75         if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
76         {
77             log<level::ERR>("Write into fru file failed",
78                             entry("FILE=%s", fruFilename),
79                             entry("ERRNO=%s", std::strerror(errno)));
80             std::fclose(fp);
81             return rc;
82         }
83 
84         std::fclose(fp);
85     }
86     else
87     {
88         log<level::ERR>("Error trying to write to fru file",
89                         entry("FILE=%s", fruFilename));
90         return rc;
91     }
92 
93     // If we got here then set the resonse byte
94     // to the number of bytes written
95     std::memcpy(response, &len, 1);
96     *dataLen = 1;
97     rc = IPMI_CC_OK;
98 
99     // Get the reference to global sd_bus object
100     sd_bus* bus_type = ipmid_get_sd_bus_connection();
101 
102     // We received some bytes. It may be full or partial. Send a valid
103     // FRU file to the inventory controller on DBus for the correct number
104     sdbusplus::bus::bus bus{bus_type};
105     bool bmcOnlyFru = false;
106     validateFRUArea(reqptr->frunum, fruFilename, bus, bmcOnlyFru);
107 
108     return rc;
109 }
110 
111 //-------------------------------------------------------
112 // Registering WRITE FRU DATA command handler with daemon
113 //-------------------------------------------------------
114 void register_netfn_storage_write_fru()
115 {
116     std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
117                 IPMI_CMD_WRITE_FRU_DATA);
118 
119     ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL,
120                            ipmiStorageWriteFruData, SYSTEM_INTERFACE);
121 }
122