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 //--------------------------------------------------------
ipmiStorageWriteFruData(ipmi_netfn_t,ipmi_cmd_t,ipmi_request_t request,ipmi_response_t response,ipmi_data_len_t dataLen,ipmi_context_t)21 ipmi_ret_t ipmiStorageWriteFruData(
22 ipmi_netfn_t /*netfn*/, ipmi_cmd_t /*cmd*/, ipmi_request_t request,
23 ipmi_response_t response, ipmi_data_len_t dataLen,
24 ipmi_context_t /*context*/)
25 {
26 FILE* fp = nullptr;
27 char fruFilename[16] = {0};
28 size_t offset = 0;
29 size_t len = 0;
30 ipmi_ret_t rc = IPMI_CC_INVALID;
31 const char* mode = nullptr;
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 = ((size_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 = ((size_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)) != nullptr)
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 response 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_t 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 //-------------------------------------------------------
register_netfn_storage_write_fru()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, nullptr,
120 ipmiStorageWriteFruData, SYSTEM_INTERFACE);
121 }
122