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