blob: a00fc73efc806722f2fd646cdc4dd8c1984d76a3 [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>
9
vishwa13555bd2015-11-10 12:10:38 -060010void register_netfn_storage_write_fru() __attribute__((constructor));
11
12sd_bus* ipmid_get_sd_bus_connection(void);
13
Patrick Venture6cd51352018-10-17 13:26:06 -070014using namespace phosphor::logging;
15
vishwa13555bd2015-11-10 12:10:38 -060016///-------------------------------------------------------
17// Called by IPMI netfn router for write fru data command
18//--------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060019ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venturec9508db2018-10-16 17:18:43 -070020 ipmi_request_t request,
21 ipmi_response_t response,
22 ipmi_data_len_t data_len,
23 ipmi_context_t context)
vishwa13555bd2015-11-10 12:10:38 -060024{
Patrick Venturec9508db2018-10-16 17:18:43 -070025 FILE* fp = NULL;
vishwa13555bd2015-11-10 12:10:38 -060026 char fru_file_name[16] = {0};
27 uint8_t offset = 0;
28 uint16_t len = 0;
29 ipmi_ret_t rc = IPMI_CC_INVALID;
Patrick Venturec9508db2018-10-16 17:18:43 -070030 const char* mode = NULL;
vishwa13555bd2015-11-10 12:10:38 -060031
32 // From the payload, extract the header that has fruid and the offsets
Patrick Ventureb65eef62018-10-17 13:18:55 -070033 auto reqptr = static_cast<write_fru_data_t*>(request);
vishwa13555bd2015-11-10 12:10:38 -060034
35 // Maintaining a temporary file to pump the data
Patrick Venture6cd51352018-10-17 13:26:06 -070036 std::sprintf(fru_file_name, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
vishwa13555bd2015-11-10 12:10:38 -060037
38 offset = ((uint16_t)reqptr->offsetms) << 8 | reqptr->offsetls;
39
40 // Length is the number of request bytes minus the header itself.
41 // The header contains an extra byte to indicate the start of
42 // the data (so didn't need to worry about word/byte boundaries)
43 // hence the -1...
Patrick Venturec9508db2018-10-16 17:18:43 -070044 len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t) - 1);
vishwa13555bd2015-11-10 12:10:38 -060045
46 // On error there is no response data for this command.
47 *data_len = 0;
vishwac93d6d42015-12-16 11:55:16 -060048
vishwa13555bd2015-11-10 12:10:38 -060049#ifdef __IPMI__DEBUG__
Patrick Venture6cd51352018-10-17 13:26:06 -070050 log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fru_file_name),
51 entry("OFFSET=%d", offset), entry("LENGTH=%d", len));
vishwa13555bd2015-11-10 12:10:38 -060052#endif
53
Patrick Venturec9508db2018-10-16 17:18:43 -070054 if (access(fru_file_name, F_OK) == -1)
55 {
vishwa13555bd2015-11-10 12:10:38 -060056 mode = "wb";
Patrick Venturec9508db2018-10-16 17:18:43 -070057 }
58 else
59 {
vishwa13555bd2015-11-10 12:10:38 -060060 mode = "rb+";
61 }
62
Patrick Venture6cd51352018-10-17 13:26:06 -070063 if ((fp = std::fopen(fru_file_name, mode)) != NULL)
vishwa13555bd2015-11-10 12:10:38 -060064 {
Patrick Venture6cd51352018-10-17 13:26:06 -070065 if (std::fseek(fp, offset, SEEK_SET))
vishwa13555bd2015-11-10 12:10:38 -060066 {
Patrick Venture6cd51352018-10-17 13:26:06 -070067 log<level::ERR>("Seek into fru file failed",
68 entry("FILE=%s", fru_file_name),
69 entry("ERRNO=%s", std::strerror(errno)));
70 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060071 return rc;
72 }
vishwac93d6d42015-12-16 11:55:16 -060073
Patrick Venture6cd51352018-10-17 13:26:06 -070074 if (std::fwrite(&reqptr->data, len, 1, fp) != 1)
vishwa13555bd2015-11-10 12:10:38 -060075 {
Patrick Venture6cd51352018-10-17 13:26:06 -070076 log<level::ERR>("Write into fru file failed",
77 entry("FILE=%s", fru_file_name),
78 entry("ERRNO=%s", std::strerror(errno)));
79 std::fclose(fp);
vishwa13555bd2015-11-10 12:10:38 -060080 return rc;
81 }
vishwac93d6d42015-12-16 11:55:16 -060082
Patrick Venture6cd51352018-10-17 13:26:06 -070083 std::fclose(fp);
vishwac93d6d42015-12-16 11:55:16 -060084 }
85 else
vishwa13555bd2015-11-10 12:10:38 -060086 {
Patrick Venture6cd51352018-10-17 13:26:06 -070087 log<level::ERR>("Error trying to write to fru file",
88 entry("FILE=%s", fru_file_name));
vishwa13555bd2015-11-10 12:10:38 -060089 return rc;
90 }
91
vishwa13555bd2015-11-10 12:10:38 -060092 // If we got here then set the resonse byte
93 // to the number of bytes written
Patrick Venture6cd51352018-10-17 13:26:06 -070094 std::memcpy(response, &len, 1);
vishwa13555bd2015-11-10 12:10:38 -060095 *data_len = 1;
96 rc = IPMI_CC_OK;
97
vishwac93d6d42015-12-16 11:55:16 -060098 // Get the reference to global sd_bus object
Patrick Venturec9508db2018-10-16 17:18:43 -070099 sd_bus* bus_type = ipmid_get_sd_bus_connection();
vishwaf3ca3522015-12-02 10:35:13 -0600100
vishwa13555bd2015-11-10 12:10:38 -0600101 // We received some bytes. It may be full or partial. Send a valid
102 // FRU file to the inventory controller on DBus for the correct number
vishwac93d6d42015-12-16 11:55:16 -0600103 bool bmc_fru = false;
Patrick Venture98072dc2018-10-20 09:31:35 -0700104 validateFRUArea(reqptr->frunum, fru_file_name, bus_type, bmc_fru);
vishwa13555bd2015-11-10 12:10:38 -0600105
106 return rc;
107}
108
109//-------------------------------------------------------
110// Registering WRITE FRU DATA command handler with daemon
111//-------------------------------------------------------
112void register_netfn_storage_write_fru()
113{
Patrick Venture6cd51352018-10-17 13:26:06 -0700114 std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
115 IPMI_CMD_WRITE_FRU_DATA);
116
Patrick Venturec9508db2018-10-16 17:18:43 -0700117 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL,
118 ipmi_storage_write_fru_data, SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600119}