blob: e870fd6c0925d067df3e5a3deae81727798e0935 [file] [log] [blame]
Patrick Venturec9508db2018-10-16 17:18:43 -07001#include "writefrudata.hpp"
2
William A. Kennington III8ab57842019-02-12 12:49:51 -08003#include <ipmid/api.h>
vishwa13555bd2015-11-10 12:10:38 -06004#include <unistd.h>
vishwa13555bd2015-11-10 12:10:38 -06005
Patrick Venture6cd51352018-10-17 13:26:06 -07006#include <cstdio>
7#include <cstring>
8#include <phosphor-logging/log.hpp>
Patrick Venturea8093a22018-10-21 09:07:11 -07009#include <sdbusplus/bus.hpp>
Patrick Venture6cd51352018-10-17 13:26:06 -070010
vishwa13555bd2015-11-10 12:10:38 -060011void register_netfn_storage_write_fru() __attribute__((constructor));
12
13sd_bus* ipmid_get_sd_bus_connection(void);
14
Patrick Venture6cd51352018-10-17 13:26:06 -070015using namespace phosphor::logging;
16
vishwa13555bd2015-11-10 12:10:38 -060017///-------------------------------------------------------
18// Called by IPMI netfn router for write fru data command
19//--------------------------------------------------------
Patrick Venture6de1d922018-10-21 12:38:46 -070020ipmi_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)
vishwa13555bd2015-11-10 12:10:38 -060025{
Patrick Venturec9508db2018-10-16 17:18:43 -070026 FILE* fp = NULL;
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070027 char fruFilename[16] = {0};
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070028 size_t offset = 0;
29 size_t len = 0;
vishwa13555bd2015-11-10 12:10:38 -060030 ipmi_ret_t rc = IPMI_CC_INVALID;
Patrick Venturec9508db2018-10-16 17:18:43 -070031 const char* mode = NULL;
vishwa13555bd2015-11-10 12:10:38 -060032
33 // From the payload, extract the header that has fruid and the offsets
Patrick Ventureb65eef62018-10-17 13:18:55 -070034 auto reqptr = static_cast<write_fru_data_t*>(request);
vishwa13555bd2015-11-10 12:10:38 -060035
36 // Maintaining a temporary file to pump the data
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070037 std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
vishwa13555bd2015-11-10 12:10:38 -060038
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070039 offset = ((size_t)reqptr->offsetms) << 8 | reqptr->offsetls;
vishwa13555bd2015-11-10 12:10:38 -060040
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...
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070045 len = ((size_t)*dataLen) - (sizeof(write_fru_data_t) - 1);
vishwa13555bd2015-11-10 12:10:38 -060046
47 // On error there is no response data for this command.
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070048 *dataLen = 0;
vishwac93d6d42015-12-16 11:55:16 -060049
vishwa13555bd2015-11-10 12:10:38 -060050#ifdef __IPMI__DEBUG__
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070051 log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070052 entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
vishwa13555bd2015-11-10 12:10:38 -060053#endif
54
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070055 if (access(fruFilename, F_OK) == -1)
Patrick Venturec9508db2018-10-16 17:18:43 -070056 {
vishwa13555bd2015-11-10 12:10:38 -060057 mode = "wb";
Patrick Venturec9508db2018-10-16 17:18:43 -070058 }
59 else
60 {
vishwa13555bd2015-11-10 12:10:38 -060061 mode = "rb+";
62 }
63
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070064 if ((fp = std::fopen(fruFilename, mode)) != NULL)
vishwa13555bd2015-11-10 12:10:38 -060065 {
Patrick Venture6cd51352018-10-17 13:26:06 -070066 if (std::fseek(fp, offset, SEEK_SET))
vishwa13555bd2015-11-10 12:10:38 -060067 {
Patrick Venture6cd51352018-10-17 13:26:06 -070068 log<level::ERR>("Seek into fru file failed",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070069 entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070070 entry("ERRNO=%s", std::strerror(errno)));
71 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060072 return rc;
73 }
vishwac93d6d42015-12-16 11:55:16 -060074
Patrick Venture6cd51352018-10-17 13:26:06 -070075 if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
vishwa13555bd2015-11-10 12:10:38 -060076 {
Patrick Venture6cd51352018-10-17 13:26:06 -070077 log<level::ERR>("Write into fru file failed",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070078 entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070079 entry("ERRNO=%s", std::strerror(errno)));
80 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060081 return rc;
82 }
vishwac93d6d42015-12-16 11:55:16 -060083
Patrick Venture6cd51352018-10-17 13:26:06 -070084 std::fclose(fp);
vishwac93d6d42015-12-16 11:55:16 -060085 }
86 else
vishwa13555bd2015-11-10 12:10:38 -060087 {
Patrick Venture6cd51352018-10-17 13:26:06 -070088 log<level::ERR>("Error trying to write to fru file",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070089 entry("FILE=%s", fruFilename));
vishwa13555bd2015-11-10 12:10:38 -060090 return rc;
91 }
92
vishwa13555bd2015-11-10 12:10:38 -060093 // If we got here then set the resonse byte
94 // to the number of bytes written
Patrick Venture6cd51352018-10-17 13:26:06 -070095 std::memcpy(response, &len, 1);
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070096 *dataLen = 1;
vishwa13555bd2015-11-10 12:10:38 -060097 rc = IPMI_CC_OK;
98
vishwac93d6d42015-12-16 11:55:16 -060099 // Get the reference to global sd_bus object
Patrick Venturec9508db2018-10-16 17:18:43 -0700100 sd_bus* bus_type = ipmid_get_sd_bus_connection();
vishwaf3ca3522015-12-02 10:35:13 -0600101
vishwa13555bd2015-11-10 12:10:38 -0600102 // 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
Patrick Williams5e8829e2022-07-22 19:26:53 -0500104 sdbusplus::bus_t bus{bus_type};
Patrick Ventureb25fb9f2018-10-21 12:35:03 -0700105 bool bmcOnlyFru = false;
106 validateFRUArea(reqptr->frunum, fruFilename, bus, bmcOnlyFru);
vishwa13555bd2015-11-10 12:10:38 -0600107
108 return rc;
109}
110
111//-------------------------------------------------------
112// Registering WRITE FRU DATA command handler with daemon
113//-------------------------------------------------------
114void register_netfn_storage_write_fru()
115{
Patrick Venture6cd51352018-10-17 13:26:06 -0700116 std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
117 IPMI_CMD_WRITE_FRU_DATA);
118
Patrick Venturec9508db2018-10-16 17:18:43 -0700119 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL,
Patrick Venture6de1d922018-10-21 12:38:46 -0700120 ipmiStorageWriteFruData, SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600121}