xref: /openbmc/ipmi-fru-parser/strgfnhandler.cpp (revision 883aa420700574d037fdb10ced4c9682c7c7e7ba)
1c9508db8SPatrick Venture #include "writefrudata.hpp"
2c9508db8SPatrick Venture 
38ab5784aSWilliam A. Kennington III #include <ipmid/api.h>
4155c34fbSMatthew Barth #include <unistd.h>
5155c34fbSMatthew Barth 
66cd5135fSPatrick Venture #include <phosphor-logging/log.hpp>
7a8093a25SPatrick Venture #include <sdbusplus/bus.hpp>
86cd5135fSPatrick Venture 
9cfa96afaSPatrick Williams #include <cstdio>
10cfa96afaSPatrick Williams #include <cstring>
11cfa96afaSPatrick Williams 
12155c34fbSMatthew Barth void register_netfn_storage_write_fru() __attribute__((constructor));
13155c34fbSMatthew Barth 
14155c34fbSMatthew Barth sd_bus* ipmid_get_sd_bus_connection(void);
15155c34fbSMatthew Barth 
166cd5135fSPatrick Venture using namespace phosphor::logging;
176cd5135fSPatrick Venture 
18155c34fbSMatthew Barth ///-------------------------------------------------------
19155c34fbSMatthew Barth // Called by IPMI netfn router for write fru data command
20155c34fbSMatthew Barth //--------------------------------------------------------
ipmiStorageWriteFruData(ipmi_netfn_t,ipmi_cmd_t,ipmi_request_t request,ipmi_response_t response,ipmi_data_len_t dataLen,ipmi_context_t)21dedaef57SPatrick Williams ipmi_ret_t ipmiStorageWriteFruData(
22dedaef57SPatrick Williams     ipmi_netfn_t /*netfn*/, ipmi_cmd_t /*cmd*/, ipmi_request_t request,
23dedaef57SPatrick Williams     ipmi_response_t response, ipmi_data_len_t dataLen,
24dedaef57SPatrick Williams     ipmi_context_t /*context*/)
25155c34fbSMatthew Barth {
26*883aa420SJayanth Othayoth     FILE* fp = nullptr;
27b25fb9fdSPatrick Venture     char fruFilename[16] = {0};
286d3b8054SJean-Marie Verdun     size_t offset = 0;
296d3b8054SJean-Marie Verdun     size_t len = 0;
30155c34fbSMatthew Barth     ipmi_ret_t rc = IPMI_CC_INVALID;
31*883aa420SJayanth Othayoth     const char* mode = nullptr;
32155c34fbSMatthew Barth 
33155c34fbSMatthew Barth     // From the payload, extract the header that has fruid and the offsets
34b65eef6dSPatrick Venture     auto reqptr = static_cast<write_fru_data_t*>(request);
35155c34fbSMatthew Barth 
36155c34fbSMatthew Barth     // Maintaining a temporary file to pump the data
37b25fb9fdSPatrick Venture     std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
38155c34fbSMatthew Barth 
396d3b8054SJean-Marie Verdun     offset = ((size_t)reqptr->offsetms) << 8 | reqptr->offsetls;
40155c34fbSMatthew Barth 
41155c34fbSMatthew Barth     // Length is the number of request bytes minus the header itself.
42155c34fbSMatthew Barth     // The header contains an extra byte to indicate the start of
43155c34fbSMatthew Barth     // the data (so didn't need to worry about word/byte boundaries)
44155c34fbSMatthew Barth     // hence the -1...
456d3b8054SJean-Marie Verdun     len = ((size_t)*dataLen) - (sizeof(write_fru_data_t) - 1);
46155c34fbSMatthew Barth 
47155c34fbSMatthew Barth     // On error there is no response data for this command.
48b25fb9fdSPatrick Venture     *dataLen = 0;
49155c34fbSMatthew Barth 
50155c34fbSMatthew Barth #ifdef __IPMI__DEBUG__
51b25fb9fdSPatrick Venture     log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fruFilename),
526cd5135fSPatrick Venture                       entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
53155c34fbSMatthew Barth #endif
54155c34fbSMatthew Barth 
55b25fb9fdSPatrick Venture     if (access(fruFilename, F_OK) == -1)
56c9508db8SPatrick Venture     {
57155c34fbSMatthew Barth         mode = "wb";
58c9508db8SPatrick Venture     }
59c9508db8SPatrick Venture     else
60c9508db8SPatrick Venture     {
61155c34fbSMatthew Barth         mode = "rb+";
62155c34fbSMatthew Barth     }
63155c34fbSMatthew Barth 
64*883aa420SJayanth Othayoth     if ((fp = std::fopen(fruFilename, mode)) != nullptr)
65155c34fbSMatthew Barth     {
666cd5135fSPatrick Venture         if (std::fseek(fp, offset, SEEK_SET))
67155c34fbSMatthew Barth         {
686cd5135fSPatrick Venture             log<level::ERR>("Seek into fru file failed",
69b25fb9fdSPatrick Venture                             entry("FILE=%s", fruFilename),
706cd5135fSPatrick Venture                             entry("ERRNO=%s", std::strerror(errno)));
716cd5135fSPatrick Venture             std::fclose(fp);
72155c34fbSMatthew Barth             return rc;
73155c34fbSMatthew Barth         }
74155c34fbSMatthew Barth 
756cd5135fSPatrick Venture         if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
76155c34fbSMatthew Barth         {
776cd5135fSPatrick Venture             log<level::ERR>("Write into fru file failed",
78b25fb9fdSPatrick Venture                             entry("FILE=%s", fruFilename),
796cd5135fSPatrick Venture                             entry("ERRNO=%s", std::strerror(errno)));
806cd5135fSPatrick Venture             std::fclose(fp);
81155c34fbSMatthew Barth             return rc;
82155c34fbSMatthew Barth         }
83155c34fbSMatthew Barth 
846cd5135fSPatrick Venture         std::fclose(fp);
85155c34fbSMatthew Barth     }
86155c34fbSMatthew Barth     else
87155c34fbSMatthew Barth     {
886cd5135fSPatrick Venture         log<level::ERR>("Error trying to write to fru file",
89b25fb9fdSPatrick Venture                         entry("FILE=%s", fruFilename));
90155c34fbSMatthew Barth         return rc;
91155c34fbSMatthew Barth     }
92155c34fbSMatthew Barth 
93bc5725d0SManojkiran Eda     // If we got here then set the response byte
94155c34fbSMatthew Barth     // to the number of bytes written
956cd5135fSPatrick Venture     std::memcpy(response, &len, 1);
96b25fb9fdSPatrick Venture     *dataLen = 1;
97155c34fbSMatthew Barth     rc = IPMI_CC_OK;
98155c34fbSMatthew Barth 
99155c34fbSMatthew Barth     // Get the reference to global sd_bus object
100155c34fbSMatthew Barth     sd_bus* bus_type = ipmid_get_sd_bus_connection();
101155c34fbSMatthew Barth 
102155c34fbSMatthew Barth     // We received some bytes. It may be full or partial. Send a valid
103155c34fbSMatthew Barth     // FRU file to the inventory controller on DBus for the correct number
1045e8829e8SPatrick Williams     sdbusplus::bus_t bus{bus_type};
105b25fb9fdSPatrick Venture     bool bmcOnlyFru = false;
106b25fb9fdSPatrick Venture     validateFRUArea(reqptr->frunum, fruFilename, bus, bmcOnlyFru);
107155c34fbSMatthew Barth 
108155c34fbSMatthew Barth     return rc;
109155c34fbSMatthew Barth }
110155c34fbSMatthew Barth 
111155c34fbSMatthew Barth //-------------------------------------------------------
112155c34fbSMatthew Barth // Registering WRITE FRU DATA command handler with daemon
113155c34fbSMatthew Barth //-------------------------------------------------------
register_netfn_storage_write_fru()114155c34fbSMatthew Barth void register_netfn_storage_write_fru()
115155c34fbSMatthew Barth {
1166cd5135fSPatrick Venture     std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
117c9508db8SPatrick Venture                 IPMI_CMD_WRITE_FRU_DATA);
1186cd5135fSPatrick Venture 
119*883aa420SJayanth Othayoth     ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, nullptr,
1206de1d925SPatrick Venture                            ipmiStorageWriteFruData, SYSTEM_INTERFACE);
121155c34fbSMatthew Barth }
122