Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 1 | #include "writefrudata.hpp" |
| 2 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 3 | #include <host-ipmid/ipmid-api.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 7 | |
| 8 | void register_netfn_storage_write_fru() __attribute__((constructor)); |
| 9 | |
| 10 | sd_bus* ipmid_get_sd_bus_connection(void); |
| 11 | |
| 12 | ///------------------------------------------------------- |
| 13 | // Called by IPMI netfn router for write fru data command |
| 14 | //-------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 15 | ipmi_ret_t ipmi_storage_write_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 16 | ipmi_request_t request, |
| 17 | ipmi_response_t response, |
| 18 | ipmi_data_len_t data_len, |
| 19 | ipmi_context_t context) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 20 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 21 | FILE* fp = NULL; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 22 | 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 Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 26 | const char* mode = NULL; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 27 | |
| 28 | // From the payload, extract the header that has fruid and the offsets |
Patrick Venture | b65eef6 | 2018-10-17 13:18:55 -0700 | [diff] [blame^] | 29 | auto reqptr = static_cast<write_fru_data_t*>(request); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 30 | |
| 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 Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 40 | len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t) - 1); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 41 | |
| 42 | // On error there is no response data for this command. |
| 43 | *data_len = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 44 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 45 | #ifdef __IPMI__DEBUG__ |
| 46 | printf("IPMI WRITE-FRU-DATA for [%s] Offset = [%d] Length = [%d]\n", |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 47 | fru_file_name, offset, len); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 48 | #endif |
| 49 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 50 | if (access(fru_file_name, F_OK) == -1) |
| 51 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 52 | mode = "wb"; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 53 | } |
| 54 | else |
| 55 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 56 | mode = "rb+"; |
| 57 | } |
| 58 | |
| 59 | if ((fp = fopen(fru_file_name, mode)) != NULL) |
| 60 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 61 | if (fseek(fp, offset, SEEK_SET)) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 62 | { |
| 63 | perror("Error:"); |
| 64 | fclose(fp); |
| 65 | return rc; |
| 66 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 67 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 68 | if (fwrite(&reqptr->data, len, 1, fp) != 1) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 69 | { |
| 70 | perror("Error:"); |
| 71 | fclose(fp); |
| 72 | return rc; |
| 73 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 74 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 75 | fclose(fp); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 76 | } |
| 77 | else |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 78 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 79 | fprintf(stderr, "Error trying to write to fru file %s\n", |
| 80 | fru_file_name); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 81 | return rc; |
| 82 | } |
| 83 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 84 | // 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 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 90 | // Get the reference to global sd_bus object |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 91 | sd_bus* bus_type = ipmid_get_sd_bus_connection(); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 92 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 93 | // 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 |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 95 | bool bmc_fru = false; |
| 96 | ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, bmc_fru); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 97 | |
| 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | //------------------------------------------------------- |
| 102 | // Registering WRITE FRU DATA command handler with daemon |
| 103 | //------------------------------------------------------- |
| 104 | void register_netfn_storage_write_fru() |
| 105 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 106 | 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); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 110 | } |