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