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