blob: 2cbbf4df8209825dc9b8ab62bf869f85c5d355d3 [file] [log] [blame]
vishwa13555bd2015-11-10 12:10:38 -06001#include <host-ipmid/ipmid-api.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Matthew Barth155c34f2016-10-18 14:33:17 -05005#include "writefrudata.hpp"
vishwa13555bd2015-11-10 12:10:38 -06006
7void register_netfn_storage_write_fru() __attribute__((constructor));
8
9sd_bus* ipmid_get_sd_bus_connection(void);
10
11///-------------------------------------------------------
12// Called by IPMI netfn router for write fru data command
13//--------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -060014ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
15 ipmi_request_t request, ipmi_response_t response,
vishwa13555bd2015-11-10 12:10:38 -060016 ipmi_data_len_t data_len, ipmi_context_t context)
17{
18 FILE *fp = NULL;
19 char fru_file_name[16] = {0};
20 uint8_t offset = 0;
21 uint16_t len = 0;
22 ipmi_ret_t rc = IPMI_CC_INVALID;
23 const char *mode = NULL;
24
25 // From the payload, extract the header that has fruid and the offsets
26 write_fru_data_t *reqptr = (write_fru_data_t*)request;
27
28 // Maintaining a temporary file to pump the data
29 sprintf(fru_file_name, "%s%02x", "/tmp/ipmifru", reqptr->frunum);
30
31 offset = ((uint16_t)reqptr->offsetms) << 8 | reqptr->offsetls;
32
33 // Length is the number of request bytes minus the header itself.
34 // The header contains an extra byte to indicate the start of
35 // the data (so didn't need to worry about word/byte boundaries)
36 // hence the -1...
37 len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t)-1);
38
39 // On error there is no response data for this command.
40 *data_len = 0;
vishwac93d6d42015-12-16 11:55:16 -060041
vishwa13555bd2015-11-10 12:10:38 -060042#ifdef __IPMI__DEBUG__
43 printf("IPMI WRITE-FRU-DATA for [%s] Offset = [%d] Length = [%d]\n",
44 fru_file_name, offset, len);
45#endif
46
47
48 if( access( fru_file_name, F_OK ) == -1 ) {
49 mode = "wb";
50 } else {
51 mode = "rb+";
52 }
53
54 if ((fp = fopen(fru_file_name, mode)) != NULL)
55 {
56 if(fseek(fp, offset, SEEK_SET))
57 {
58 perror("Error:");
59 fclose(fp);
60 return rc;
61 }
vishwac93d6d42015-12-16 11:55:16 -060062
vishwa13555bd2015-11-10 12:10:38 -060063 if(fwrite(&reqptr->data, len, 1, fp) != 1)
64 {
65 perror("Error:");
66 fclose(fp);
67 return rc;
68 }
vishwac93d6d42015-12-16 11:55:16 -060069
vishwa13555bd2015-11-10 12:10:38 -060070 fclose(fp);
vishwac93d6d42015-12-16 11:55:16 -060071 }
72 else
vishwa13555bd2015-11-10 12:10:38 -060073 {
74 fprintf(stderr, "Error trying to write to fru file %s\n",fru_file_name);
75 return rc;
76 }
77
78
79 // If we got here then set the resonse byte
80 // to the number of bytes written
81 memcpy(response, &len, 1);
82 *data_len = 1;
83 rc = IPMI_CC_OK;
84
vishwac93d6d42015-12-16 11:55:16 -060085 // Get the reference to global sd_bus object
86 sd_bus *bus_type = ipmid_get_sd_bus_connection();
vishwaf3ca3522015-12-02 10:35:13 -060087
vishwa13555bd2015-11-10 12:10:38 -060088 // We received some bytes. It may be full or partial. Send a valid
89 // FRU file to the inventory controller on DBus for the correct number
vishwac93d6d42015-12-16 11:55:16 -060090 bool bmc_fru = false;
91 ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, bmc_fru);
vishwa13555bd2015-11-10 12:10:38 -060092
93 return rc;
94}
95
96//-------------------------------------------------------
97// Registering WRITE FRU DATA command handler with daemon
98//-------------------------------------------------------
99void register_netfn_storage_write_fru()
100{
101 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA);
Toma2ca1002016-09-06 18:21:42 +0530102 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL, ipmi_storage_write_fru_data,
103 SYSTEM_INTERFACE);
vishwa13555bd2015-11-10 12:10:38 -0600104}