blob: 9526525a8232d254c7fe358bed5c0a7e6ca037f5 [file] [log] [blame]
Patrick Venturec9508db2018-10-16 17:18:43 -07001#include "writefrudata.hpp"
2
vishwa13555bd2015-11-10 12:10:38 -06003#include <host-ipmid/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//--------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060020ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venturec9508db2018-10-16 17:18:43 -070021 ipmi_request_t request,
22 ipmi_response_t response,
23 ipmi_data_len_t data_len,
24 ipmi_context_t context)
vishwa13555bd2015-11-10 12:10:38 -060025{
Patrick Venturec9508db2018-10-16 17:18:43 -070026 FILE* fp = NULL;
vishwa13555bd2015-11-10 12:10:38 -060027 char fru_file_name[16] = {0};
28 uint8_t offset = 0;
29 uint16_t len = 0;
30 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 Venture6cd51352018-10-17 13:26:06 -070037 std::sprintf(fru_file_name, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
vishwa13555bd2015-11-10 12:10:38 -060038
39 offset = ((uint16_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...
Patrick Venturec9508db2018-10-16 17:18:43 -070045 len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t) - 1);
vishwa13555bd2015-11-10 12:10:38 -060046
47 // On error there is no response data for this command.
48 *data_len = 0;
vishwac93d6d42015-12-16 11:55:16 -060049
vishwa13555bd2015-11-10 12:10:38 -060050#ifdef __IPMI__DEBUG__
Patrick Venture6cd51352018-10-17 13:26:06 -070051 log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fru_file_name),
52 entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
vishwa13555bd2015-11-10 12:10:38 -060053#endif
54
Patrick Venturec9508db2018-10-16 17:18:43 -070055 if (access(fru_file_name, F_OK) == -1)
56 {
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 Venture6cd51352018-10-17 13:26:06 -070064 if ((fp = std::fopen(fru_file_name, 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",
69 entry("FILE=%s", fru_file_name),
70 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",
78 entry("FILE=%s", fru_file_name),
79 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",
89 entry("FILE=%s", fru_file_name));
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);
vishwa13555bd2015-11-10 12:10:38 -060096 *data_len = 1;
97 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 Venturea8093a22018-10-21 09:07:11 -0700104 sdbusplus::bus::bus bus{bus_type};
vishwac93d6d42015-12-16 11:55:16 -0600105 bool bmc_fru = false;
Patrick Venturea8093a22018-10-21 09:07:11 -0700106 validateFRUArea(reqptr->frunum, fru_file_name, bus, bmc_fru);
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,
120 ipmi_storage_write_fru_data, SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600121}