blob: ed3cbf4e9b7c035b9a6501f465c8d283e6c926dd [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 <phosphor-logging/log.hpp>
Patrick Venturea8093a22018-10-21 09:07:11 -07007#include <sdbusplus/bus.hpp>
Patrick Venture6cd51352018-10-17 13:26:06 -07008
Patrick Williamscfa96af2023-05-10 07:50:26 -05009#include <cstdio>
10#include <cstring>
11
vishwa13555bd2015-11-10 12:10:38 -060012void register_netfn_storage_write_fru() __attribute__((constructor));
13
14sd_bus* ipmid_get_sd_bus_connection(void);
15
Patrick Venture6cd51352018-10-17 13:26:06 -070016using namespace phosphor::logging;
17
vishwa13555bd2015-11-10 12:10:38 -060018///-------------------------------------------------------
19// Called by IPMI netfn router for write fru data command
20//--------------------------------------------------------
Patrick Venture6de1d922018-10-21 12:38:46 -070021ipmi_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)
vishwa13555bd2015-11-10 12:10:38 -060026{
Patrick Venturec9508db2018-10-16 17:18:43 -070027 FILE* fp = NULL;
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070028 char fruFilename[16] = {0};
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070029 size_t offset = 0;
30 size_t len = 0;
vishwa13555bd2015-11-10 12:10:38 -060031 ipmi_ret_t rc = IPMI_CC_INVALID;
Patrick Venturec9508db2018-10-16 17:18:43 -070032 const char* mode = NULL;
vishwa13555bd2015-11-10 12:10:38 -060033
34 // From the payload, extract the header that has fruid and the offsets
Patrick Ventureb65eef62018-10-17 13:18:55 -070035 auto reqptr = static_cast<write_fru_data_t*>(request);
vishwa13555bd2015-11-10 12:10:38 -060036
37 // Maintaining a temporary file to pump the data
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070038 std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
vishwa13555bd2015-11-10 12:10:38 -060039
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070040 offset = ((size_t)reqptr->offsetms) << 8 | reqptr->offsetls;
vishwa13555bd2015-11-10 12:10:38 -060041
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...
Jean-Marie Verdun6d3b8052021-10-01 13:37:40 -070046 len = ((size_t)*dataLen) - (sizeof(write_fru_data_t) - 1);
vishwa13555bd2015-11-10 12:10:38 -060047
48 // On error there is no response data for this command.
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070049 *dataLen = 0;
vishwac93d6d42015-12-16 11:55:16 -060050
vishwa13555bd2015-11-10 12:10:38 -060051#ifdef __IPMI__DEBUG__
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070052 log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070053 entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
vishwa13555bd2015-11-10 12:10:38 -060054#endif
55
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070056 if (access(fruFilename, F_OK) == -1)
Patrick Venturec9508db2018-10-16 17:18:43 -070057 {
vishwa13555bd2015-11-10 12:10:38 -060058 mode = "wb";
Patrick Venturec9508db2018-10-16 17:18:43 -070059 }
60 else
61 {
vishwa13555bd2015-11-10 12:10:38 -060062 mode = "rb+";
63 }
64
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070065 if ((fp = std::fopen(fruFilename, mode)) != NULL)
vishwa13555bd2015-11-10 12:10:38 -060066 {
Patrick Venture6cd51352018-10-17 13:26:06 -070067 if (std::fseek(fp, offset, SEEK_SET))
vishwa13555bd2015-11-10 12:10:38 -060068 {
Patrick Venture6cd51352018-10-17 13:26:06 -070069 log<level::ERR>("Seek into fru file failed",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070070 entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070071 entry("ERRNO=%s", std::strerror(errno)));
72 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060073 return rc;
74 }
vishwac93d6d42015-12-16 11:55:16 -060075
Patrick Venture6cd51352018-10-17 13:26:06 -070076 if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
vishwa13555bd2015-11-10 12:10:38 -060077 {
Patrick Venture6cd51352018-10-17 13:26:06 -070078 log<level::ERR>("Write into fru file failed",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070079 entry("FILE=%s", fruFilename),
Patrick Venture6cd51352018-10-17 13:26:06 -070080 entry("ERRNO=%s", std::strerror(errno)));
81 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060082 return rc;
83 }
vishwac93d6d42015-12-16 11:55:16 -060084
Patrick Venture6cd51352018-10-17 13:26:06 -070085 std::fclose(fp);
vishwac93d6d42015-12-16 11:55:16 -060086 }
87 else
vishwa13555bd2015-11-10 12:10:38 -060088 {
Patrick Venture6cd51352018-10-17 13:26:06 -070089 log<level::ERR>("Error trying to write to fru file",
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070090 entry("FILE=%s", fruFilename));
vishwa13555bd2015-11-10 12:10:38 -060091 return rc;
92 }
93
vishwa13555bd2015-11-10 12:10:38 -060094 // If we got here then set the resonse byte
95 // to the number of bytes written
Patrick Venture6cd51352018-10-17 13:26:06 -070096 std::memcpy(response, &len, 1);
Patrick Ventureb25fb9f2018-10-21 12:35:03 -070097 *dataLen = 1;
vishwa13555bd2015-11-10 12:10:38 -060098 rc = IPMI_CC_OK;
99
vishwac93d6d42015-12-16 11:55:16 -0600100 // Get the reference to global sd_bus object
Patrick Venturec9508db2018-10-16 17:18:43 -0700101 sd_bus* bus_type = ipmid_get_sd_bus_connection();
vishwaf3ca3522015-12-02 10:35:13 -0600102
vishwa13555bd2015-11-10 12:10:38 -0600103 // 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
Patrick Williams5e8829e2022-07-22 19:26:53 -0500105 sdbusplus::bus_t bus{bus_type};
Patrick Ventureb25fb9f2018-10-21 12:35:03 -0700106 bool bmcOnlyFru = false;
107 validateFRUArea(reqptr->frunum, fruFilename, bus, bmcOnlyFru);
vishwa13555bd2015-11-10 12:10:38 -0600108
109 return rc;
110}
111
112//-------------------------------------------------------
113// Registering WRITE FRU DATA command handler with daemon
114//-------------------------------------------------------
115void register_netfn_storage_write_fru()
116{
Patrick Venture6cd51352018-10-17 13:26:06 -0700117 std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
118 IPMI_CMD_WRITE_FRU_DATA);
119
Patrick Venturec9508db2018-10-16 17:18:43 -0700120 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL,
Patrick Venture6de1d922018-10-21 12:38:46 -0700121 ipmiStorageWriteFruData, SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600122}