blob: c4e4c581c63d34db444fcbf0c90c31c5e7c39f23 [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>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
vishwa13555bd2015-11-10 12:10:38 -06007
8void register_netfn_storage_write_fru() __attribute__((constructor));
9
10sd_bus* ipmid_get_sd_bus_connection(void);
11
12///-------------------------------------------------------
13// Called by IPMI netfn router for write fru data command
14//--------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060015ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venturec9508db2018-10-16 17:18:43 -070016 ipmi_request_t request,
17 ipmi_response_t response,
18 ipmi_data_len_t data_len,
19 ipmi_context_t context)
vishwa13555bd2015-11-10 12:10:38 -060020{
Patrick Venturec9508db2018-10-16 17:18:43 -070021 FILE* fp = NULL;
vishwa13555bd2015-11-10 12:10:38 -060022 char fru_file_name[16] = {0};
23 uint8_t offset = 0;
24 uint16_t len = 0;
25 ipmi_ret_t rc = IPMI_CC_INVALID;
Patrick Venturec9508db2018-10-16 17:18:43 -070026 const char* mode = NULL;
vishwa13555bd2015-11-10 12:10:38 -060027
28 // From the payload, extract the header that has fruid and the offsets
Patrick Ventureb65eef62018-10-17 13:18:55 -070029 auto reqptr = static_cast<write_fru_data_t*>(request);
vishwa13555bd2015-11-10 12:10:38 -060030
31 // Maintaining a temporary file to pump the data
32 sprintf(fru_file_name, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
33
34 offset = ((uint16_t)reqptr->offsetms) << 8 | reqptr->offsetls;
35
36 // Length is the number of request bytes minus the header itself.
37 // The header contains an extra byte to indicate the start of
38 // the data (so didn't need to worry about word/byte boundaries)
39 // hence the -1...
Patrick Venturec9508db2018-10-16 17:18:43 -070040 len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t) - 1);
vishwa13555bd2015-11-10 12:10:38 -060041
42 // On error there is no response data for this command.
43 *data_len = 0;
vishwac93d6d42015-12-16 11:55:16 -060044
vishwa13555bd2015-11-10 12:10:38 -060045#ifdef __IPMI__DEBUG__
46 printf("IPMI WRITE-FRU-DATA for [%s] Offset = [%d] Length = [%d]\n",
Patrick Venturec9508db2018-10-16 17:18:43 -070047 fru_file_name, offset, len);
vishwa13555bd2015-11-10 12:10:38 -060048#endif
49
Patrick Venturec9508db2018-10-16 17:18:43 -070050 if (access(fru_file_name, F_OK) == -1)
51 {
vishwa13555bd2015-11-10 12:10:38 -060052 mode = "wb";
Patrick Venturec9508db2018-10-16 17:18:43 -070053 }
54 else
55 {
vishwa13555bd2015-11-10 12:10:38 -060056 mode = "rb+";
57 }
58
59 if ((fp = fopen(fru_file_name, mode)) != NULL)
60 {
Patrick Venturec9508db2018-10-16 17:18:43 -070061 if (fseek(fp, offset, SEEK_SET))
vishwa13555bd2015-11-10 12:10:38 -060062 {
63 perror("Error:");
64 fclose(fp);
65 return rc;
66 }
vishwac93d6d42015-12-16 11:55:16 -060067
Patrick Venturec9508db2018-10-16 17:18:43 -070068 if (fwrite(&reqptr->data, len, 1, fp) != 1)
vishwa13555bd2015-11-10 12:10:38 -060069 {
70 perror("Error:");
71 fclose(fp);
72 return rc;
73 }
vishwac93d6d42015-12-16 11:55:16 -060074
vishwa13555bd2015-11-10 12:10:38 -060075 fclose(fp);
vishwac93d6d42015-12-16 11:55:16 -060076 }
77 else
vishwa13555bd2015-11-10 12:10:38 -060078 {
Patrick Venturec9508db2018-10-16 17:18:43 -070079 fprintf(stderr, "Error trying to write to fru file %s\n",
80 fru_file_name);
vishwa13555bd2015-11-10 12:10:38 -060081 return rc;
82 }
83
vishwa13555bd2015-11-10 12:10:38 -060084 // If we got here then set the resonse byte
85 // to the number of bytes written
86 memcpy(response, &len, 1);
87 *data_len = 1;
88 rc = IPMI_CC_OK;
89
vishwac93d6d42015-12-16 11:55:16 -060090 // Get the reference to global sd_bus object
Patrick Venturec9508db2018-10-16 17:18:43 -070091 sd_bus* bus_type = ipmid_get_sd_bus_connection();
vishwaf3ca3522015-12-02 10:35:13 -060092
vishwa13555bd2015-11-10 12:10:38 -060093 // We received some bytes. It may be full or partial. Send a valid
94 // FRU file to the inventory controller on DBus for the correct number
vishwac93d6d42015-12-16 11:55:16 -060095 bool bmc_fru = false;
96 ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, bmc_fru);
vishwa13555bd2015-11-10 12:10:38 -060097
98 return rc;
99}
100
101//-------------------------------------------------------
102// Registering WRITE FRU DATA command handler with daemon
103//-------------------------------------------------------
104void register_netfn_storage_write_fru()
105{
Patrick Venturec9508db2018-10-16 17:18:43 -0700106 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE,
107 IPMI_CMD_WRITE_FRU_DATA);
108 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL,
109 ipmi_storage_write_fru_data, SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600110}