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> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 4 | #include <unistd.h> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 5 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 10 | void register_netfn_storage_write_fru() __attribute__((constructor)); |
| 11 | |
| 12 | sd_bus* ipmid_get_sd_bus_connection(void); |
| 13 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 14 | using namespace phosphor::logging; |
| 15 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 16 | ///------------------------------------------------------- |
| 17 | // Called by IPMI netfn router for write fru data command |
| 18 | //-------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 19 | 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] | 20 | ipmi_request_t request, |
| 21 | ipmi_response_t response, |
| 22 | ipmi_data_len_t data_len, |
| 23 | ipmi_context_t context) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 24 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 25 | FILE* fp = NULL; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 26 | char fru_file_name[16] = {0}; |
| 27 | uint8_t offset = 0; |
| 28 | uint16_t len = 0; |
| 29 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 30 | const char* mode = NULL; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 31 | |
| 32 | // From the payload, extract the header that has fruid and the offsets |
Patrick Venture | b65eef6 | 2018-10-17 13:18:55 -0700 | [diff] [blame] | 33 | auto reqptr = static_cast<write_fru_data_t*>(request); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 34 | |
| 35 | // Maintaining a temporary file to pump the data |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 36 | std::sprintf(fru_file_name, "%s%02x", "/tmp/ipmifru", reqptr->frunum); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 37 | |
| 38 | offset = ((uint16_t)reqptr->offsetms) << 8 | reqptr->offsetls; |
| 39 | |
| 40 | // Length is the number of request bytes minus the header itself. |
| 41 | // The header contains an extra byte to indicate the start of |
| 42 | // the data (so didn't need to worry about word/byte boundaries) |
| 43 | // hence the -1... |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 44 | len = ((uint16_t)*data_len) - (sizeof(write_fru_data_t) - 1); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 45 | |
| 46 | // On error there is no response data for this command. |
| 47 | *data_len = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 48 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 49 | #ifdef __IPMI__DEBUG__ |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 50 | log<level::DEBUG>("IPMI WRITE-FRU-DATA", entry("FILE=%s", fru_file_name), |
| 51 | entry("OFFSET=%d", offset), entry("LENGTH=%d", len)); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 52 | #endif |
| 53 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 54 | if (access(fru_file_name, F_OK) == -1) |
| 55 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 56 | mode = "wb"; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 57 | } |
| 58 | else |
| 59 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 60 | mode = "rb+"; |
| 61 | } |
| 62 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 63 | if ((fp = std::fopen(fru_file_name, mode)) != NULL) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 64 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 65 | if (std::fseek(fp, offset, SEEK_SET)) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 66 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 67 | log<level::ERR>("Seek into fru file failed", |
| 68 | entry("FILE=%s", fru_file_name), |
| 69 | entry("ERRNO=%s", std::strerror(errno))); |
| 70 | std::fclose(fp); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 71 | return rc; |
| 72 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 73 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 74 | if (std::fwrite(&reqptr->data, len, 1, fp) != 1) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 75 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 76 | log<level::ERR>("Write into fru file failed", |
| 77 | entry("FILE=%s", fru_file_name), |
| 78 | entry("ERRNO=%s", std::strerror(errno))); |
| 79 | std::fclose(fp); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 80 | return rc; |
| 81 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 82 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 83 | std::fclose(fp); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 84 | } |
| 85 | else |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 86 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 87 | log<level::ERR>("Error trying to write to fru file", |
| 88 | entry("FILE=%s", fru_file_name)); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 89 | return rc; |
| 90 | } |
| 91 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 92 | // If we got here then set the resonse byte |
| 93 | // to the number of bytes written |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 94 | std::memcpy(response, &len, 1); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 95 | *data_len = 1; |
| 96 | rc = IPMI_CC_OK; |
| 97 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 98 | // Get the reference to global sd_bus object |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 99 | sd_bus* bus_type = ipmid_get_sd_bus_connection(); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 100 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 101 | // We received some bytes. It may be full or partial. Send a valid |
| 102 | // FRU file to the inventory controller on DBus for the correct number |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 103 | bool bmc_fru = false; |
| 104 | ipmi_validate_fru_area(reqptr->frunum, fru_file_name, bus_type, bmc_fru); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 105 | |
| 106 | return rc; |
| 107 | } |
| 108 | |
| 109 | //------------------------------------------------------- |
| 110 | // Registering WRITE FRU DATA command handler with daemon |
| 111 | //------------------------------------------------------- |
| 112 | void register_netfn_storage_write_fru() |
| 113 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 114 | std::printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE, |
| 115 | IPMI_CMD_WRITE_FRU_DATA); |
| 116 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 117 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WRITE_FRU_DATA, NULL, |
| 118 | ipmi_storage_write_fru_data, SYSTEM_INTERFACE); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 119 | } |