1 #include <host-ipmid/ipmid-api.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include "writefrudata.hpp" 6 7 void register_netfn_storage_write_fru() __attribute__((constructor)); 8 9 sd_bus* ipmid_get_sd_bus_connection(void); 10 11 ///------------------------------------------------------- 12 // Called by IPMI netfn router for write fru data command 13 //-------------------------------------------------------- 14 ipmi_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 88 // 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 90 bool bmc_fru = false; 91 ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, bmc_fru); 92 93 return rc; 94 } 95 96 //------------------------------------------------------- 97 // Registering WRITE FRU DATA command handler with daemon 98 //------------------------------------------------------- 99 void register_netfn_storage_write_fru() 100 { 101 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA); 102 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL, ipmi_storage_write_fru_data, 103 SYSTEM_INTERFACE); 104 } 105