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 | |
George Liu | 1c62954 | 2025-04-03 10:23:47 +0800 | [diff] [blame] | 6 | #include <ipmid/api-types.hpp> |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
Patrick Venture | a8093a2 | 2018-10-21 09:07:11 -0700 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 9 | |
Patrick Williams | cfa96af | 2023-05-10 07:50:26 -0500 | [diff] [blame] | 10 | #include <cstdio> |
| 11 | #include <cstring> |
| 12 | |
George Liu | eed0953 | 2025-07-30 10:28:03 +0800 | [diff] [blame^] | 13 | void registerNetFnStorageWriteFru() __attribute__((constructor)); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 14 | |
| 15 | sd_bus* ipmid_get_sd_bus_connection(void); |
| 16 | |
| 17 | ///------------------------------------------------------- |
| 18 | // Called by IPMI netfn router for write fru data command |
| 19 | //-------------------------------------------------------- |
Patrick Williams | dedaef5 | 2025-02-01 08:22:00 -0500 | [diff] [blame] | 20 | ipmi_ret_t ipmiStorageWriteFruData( |
| 21 | ipmi_netfn_t /*netfn*/, ipmi_cmd_t /*cmd*/, ipmi_request_t request, |
| 22 | ipmi_response_t response, ipmi_data_len_t dataLen, |
| 23 | ipmi_context_t /*context*/) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 24 | { |
Jayanth Othayoth | 883aa42 | 2024-12-18 10:33:06 -0600 | [diff] [blame] | 25 | FILE* fp = nullptr; |
Patrick Venture | b25fb9f | 2018-10-21 12:35:03 -0700 | [diff] [blame] | 26 | char fruFilename[16] = {0}; |
Jean-Marie Verdun | 6d3b805 | 2021-10-01 13:37:40 -0700 | [diff] [blame] | 27 | size_t offset = 0; |
| 28 | size_t len = 0; |
George Liu | 1a843d0 | 2025-07-02 15:21:51 +0800 | [diff] [blame] | 29 | ipmi_ret_t rc = ipmi::ccInvalidCommand; |
Jayanth Othayoth | 883aa42 | 2024-12-18 10:33:06 -0600 | [diff] [blame] | 30 | const char* mode = nullptr; |
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 | b25fb9f | 2018-10-21 12:35:03 -0700 | [diff] [blame] | 36 | std::sprintf(fruFilename, "%s%02x", "/tmp/ipmifru", reqptr->frunum); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 37 | |
Jean-Marie Verdun | 6d3b805 | 2021-10-01 13:37:40 -0700 | [diff] [blame] | 38 | offset = ((size_t)reqptr->offsetms) << 8 | reqptr->offsetls; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 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... |
Jean-Marie Verdun | 6d3b805 | 2021-10-01 13:37:40 -0700 | [diff] [blame] | 44 | len = ((size_t)*dataLen) - (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. |
Patrick Venture | b25fb9f | 2018-10-21 12:35:03 -0700 | [diff] [blame] | 47 | *dataLen = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 48 | |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 49 | lg2::debug( |
| 50 | "IPMI WRITE-FRU-DATA, file name: {FILE}, offset: {OFFSET}, length: {LENGTH}", |
| 51 | "FILE", fruFilename, "OFFSET", offset, "LENGTH", len); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 52 | |
Patrick Venture | b25fb9f | 2018-10-21 12:35:03 -0700 | [diff] [blame] | 53 | if (access(fruFilename, F_OK) == -1) |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 54 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 55 | mode = "wb"; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 56 | } |
| 57 | else |
| 58 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 59 | mode = "rb+"; |
| 60 | } |
| 61 | |
Jayanth Othayoth | 883aa42 | 2024-12-18 10:33:06 -0600 | [diff] [blame] | 62 | if ((fp = std::fopen(fruFilename, mode)) != nullptr) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 63 | { |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 64 | if (std::fseek(fp, offset, SEEK_SET)) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 65 | { |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 66 | lg2::error( |
| 67 | "Seek into fru file failed, file name: {FILE}, errno: {ERRNO}", |
| 68 | "FILE", fruFilename, "ERRNO", errno); |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 69 | std::fclose(fp); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 70 | return rc; |
| 71 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 72 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 73 | if (std::fwrite(&reqptr->data, len, 1, fp) != 1) |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 74 | { |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 75 | lg2::error( |
| 76 | "Write into fru file failed, file name: {FILE}, errno: {ERRNO}", |
| 77 | "FILE", fruFilename, "ERRNO", errno); |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 78 | std::fclose(fp); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 79 | return rc; |
| 80 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 81 | |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 82 | std::fclose(fp); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 83 | } |
| 84 | else |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 85 | { |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 86 | lg2::error("Error trying to write to {FILE}", "FILE", fruFilename); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 87 | return rc; |
| 88 | } |
| 89 | |
Manojkiran Eda | bc5725d | 2024-06-17 11:50:54 +0530 | [diff] [blame] | 90 | // If we got here then set the response byte |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 91 | // to the number of bytes written |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 92 | std::memcpy(response, &len, 1); |
Patrick Venture | b25fb9f | 2018-10-21 12:35:03 -0700 | [diff] [blame] | 93 | *dataLen = 1; |
George Liu | 1a843d0 | 2025-07-02 15:21:51 +0800 | [diff] [blame] | 94 | rc = ipmi::ccSuccess; |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 95 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 96 | // Get the reference to global sd_bus object |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 97 | sd_bus* bus_type = ipmid_get_sd_bus_connection(); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 98 | |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 99 | // We received some bytes. It may be full or partial. Send a valid |
| 100 | // FRU file to the inventory controller on DBus for the correct number |
Patrick Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 101 | sdbusplus::bus_t bus{bus_type}; |
Jayanth Othayoth | 70cb067 | 2025-06-07 02:13:23 -0500 | [diff] [blame] | 102 | validateFRUArea(reqptr->frunum, fruFilename, bus); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 103 | |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | //------------------------------------------------------- |
| 108 | // Registering WRITE FRU DATA command handler with daemon |
| 109 | //------------------------------------------------------- |
George Liu | eed0953 | 2025-07-30 10:28:03 +0800 | [diff] [blame^] | 110 | void registerNetFnStorageWriteFru() |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 111 | { |
George Liu | 98de42a | 2025-07-30 09:54:21 +0800 | [diff] [blame] | 112 | lg2::info( |
| 113 | "Registering WRITE FRU DATA command handler, netfn:{NETFN}, cmd:{CMD}", |
| 114 | "NETFN", lg2::hex, ipmi::netFnStorage, "CMD", lg2::hex, |
| 115 | ipmi::storage::cmdWriteFruData); |
Patrick Venture | 6cd5135 | 2018-10-17 13:26:06 -0700 | [diff] [blame] | 116 | |
George Liu | 1a843d0 | 2025-07-02 15:21:51 +0800 | [diff] [blame] | 117 | ipmi_register_callback(ipmi::netFnStorage, ipmi::storage::cmdWriteFruData, |
| 118 | nullptr, ipmiStorageWriteFruData, SYSTEM_INTERFACE); |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 119 | } |