blob: a00688f54297f1b3fc188a91eb77b88706bbe4de [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>
5#include "writefrudata.H"
6
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//--------------------------------------------------------
14ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
15 ipmi_request_t request, ipmi_response_t response,
16 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;
41
42#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 }
62
63 if(fwrite(&reqptr->data, len, 1, fp) != 1)
64 {
65 perror("Error:");
66 fclose(fp);
67 return rc;
68 }
69
70 fclose(fp);
71 }
72 else
73 {
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
85 // Get the reference to global sd_bus object
86 sd_bus *bus_type = ipmid_get_sd_bus_connection();
87
vishwaf3ca3522015-12-02 10:35:13 -060088 // Do not need to update present status in the inventory. Its only for
89 // eeprom requirement at this moment. But we may have a need in the future
90 bool set_present = false;
91
vishwa13555bd2015-11-10 12:10:38 -060092 // We received some bytes. It may be full or partial. Send a valid
93 // FRU file to the inventory controller on DBus for the correct number
vishwaf3ca3522015-12-02 10:35:13 -060094 ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, set_present);
vishwa13555bd2015-11-10 12:10:38 -060095
96 return rc;
97}
98
99//-------------------------------------------------------
100// Registering WRITE FRU DATA command handler with daemon
101//-------------------------------------------------------
102void register_netfn_storage_write_fru()
103{
104 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA);
105 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL, ipmi_storage_write_fru_data);
106}