Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 1 | #include "file_io.hpp" |
| 2 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 3 | #include "file_io_by_type.hpp" |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 4 | #include "file_table.hpp" |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 5 | #include "utils.hpp" |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 7 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 8 | #include <fcntl.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 9 | #include <libpldm/base.h> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 10 | #include <sys/mman.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 15 | #include <phosphor-logging/lg2.hpp> |
| 16 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <fstream> |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 19 | #include <memory> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 20 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 21 | PHOSPHOR_LOG2_USING; |
| 22 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 23 | namespace pldm |
| 24 | { |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 25 | using namespace pldm::responder::utils; |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 27 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 28 | namespace responder |
| 29 | { |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 30 | namespace fs = std::filesystem; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 31 | |
| 32 | namespace dma |
| 33 | { |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 34 | /** @struct AspeedXdmaOp |
| 35 | * |
| 36 | * Structure representing XDMA operation |
| 37 | */ |
| 38 | struct AspeedXdmaOp |
| 39 | { |
| 40 | uint64_t hostAddr; //!< the DMA address on the host side, configured by |
| 41 | //!< PCI subsystem. |
| 42 | uint32_t len; //!< the size of the transfer in bytes, it should be a |
| 43 | //!< multiple of 16 bytes |
| 44 | uint32_t upstream; //!< boolean indicating the direction of the DMA |
| 45 | //!< operation, true means a transfer from BMC to host. |
| 46 | }; |
| 47 | |
Deepak Kodihalli | 134cc1b | 2019-10-04 01:51:02 -0500 | [diff] [blame] | 48 | constexpr auto xdmaDev = "/dev/aspeed-xdma"; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 49 | |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 50 | int DMA::transferHostDataToSocket(int fd, uint32_t length, uint64_t address) |
| 51 | { |
| 52 | static const size_t pageSize = getpagesize(); |
| 53 | uint32_t numPages = length / pageSize; |
| 54 | uint32_t pageAlignedLength = numPages * pageSize; |
| 55 | |
| 56 | if (length > pageAlignedLength) |
| 57 | { |
| 58 | pageAlignedLength += pageSize; |
| 59 | } |
| 60 | |
| 61 | auto mmapCleanup = [pageAlignedLength](void* vgaMem) { |
| 62 | munmap(vgaMem, pageAlignedLength); |
| 63 | }; |
| 64 | |
| 65 | int dmaFd = -1; |
| 66 | int rc = 0; |
| 67 | dmaFd = open(xdmaDev, O_RDWR); |
| 68 | if (dmaFd < 0) |
| 69 | { |
| 70 | rc = -errno; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 71 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 72 | "Failed to open the XDMA device for transferring remote terminus data to socket with response code '{RC}'", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 73 | "RC", rc); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 74 | return rc; |
| 75 | } |
| 76 | |
| 77 | pldm::utils::CustomFD xdmaFd(dmaFd); |
| 78 | |
| 79 | void* vgaMem; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 80 | vgaMem = mmap(nullptr, pageAlignedLength, PROT_READ, MAP_SHARED, xdmaFd(), |
| 81 | 0); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 82 | if (MAP_FAILED == vgaMem) |
| 83 | { |
| 84 | rc = -errno; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 85 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 86 | "Failed to mmap the XDMA device for transferring remote terminus data to socket with response code '{RC}'", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 87 | "RC", rc); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<void, decltype(mmapCleanup)> vgaMemPtr(vgaMem, mmapCleanup); |
| 92 | |
| 93 | AspeedXdmaOp xdmaOp; |
| 94 | xdmaOp.upstream = 0; |
| 95 | xdmaOp.hostAddr = address; |
| 96 | xdmaOp.len = length; |
| 97 | |
| 98 | rc = write(xdmaFd(), &xdmaOp, sizeof(xdmaOp)); |
| 99 | if (rc < 0) |
| 100 | { |
| 101 | rc = -errno; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 102 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 103 | "Failed to execute the DMA operation for transfering remote terminus data to socket at address '{ADDRESS}' and length '{LENGTH}' with response code '{RC}'", |
| 104 | "RC", rc, "ADDRESS", address, "LENGTH", length); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 105 | return rc; |
| 106 | } |
| 107 | |
| 108 | rc = writeToUnixSocket(fd, static_cast<const char*>(vgaMemPtr.get()), |
| 109 | length); |
| 110 | if (rc < 0) |
| 111 | { |
| 112 | rc = -errno; |
| 113 | close(fd); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 114 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 115 | "Failed to write to Unix socket, closing socket for transfering remote terminus data to socket with response code '{RC}'", |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 116 | "RC", rc); |
Ravi Teja | ce1c96f | 2020-10-05 23:13:01 -0500 | [diff] [blame] | 117 | return rc; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 122 | int DMA::transferDataHost(int fd, uint32_t offset, uint32_t length, |
| 123 | uint64_t address, bool upstream) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 124 | { |
| 125 | static const size_t pageSize = getpagesize(); |
| 126 | uint32_t numPages = length / pageSize; |
| 127 | uint32_t pageAlignedLength = numPages * pageSize; |
| 128 | |
| 129 | if (length > pageAlignedLength) |
| 130 | { |
| 131 | pageAlignedLength += pageSize; |
| 132 | } |
| 133 | |
ArchanaKakani | 9340975 | 2022-04-19 00:36:04 -0500 | [diff] [blame] | 134 | int rc = 0; |
| 135 | auto mmapCleanup = [pageAlignedLength, &rc](void* vgaMem) { |
| 136 | if (rc != -EINTR) |
| 137 | { |
| 138 | munmap(vgaMem, pageAlignedLength); |
| 139 | } |
| 140 | else |
| 141 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 142 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 143 | "Received interrupt during DMA transfer for data between BMC and remote terminus. Skipping Unmap."); |
ArchanaKakani | 9340975 | 2022-04-19 00:36:04 -0500 | [diff] [blame] | 144 | } |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 145 | }; |
| 146 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 147 | int dmaFd = -1; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 148 | dmaFd = open(xdmaDev, O_RDWR); |
| 149 | if (dmaFd < 0) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 150 | { |
| 151 | rc = -errno; |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 152 | error( |
| 153 | "Failed to open the XDMA device for data transfer between BMC and remote terminus with response code '{RC}'", |
| 154 | "RC", rc); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 155 | return rc; |
| 156 | } |
| 157 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 158 | pldm::utils::CustomFD xdmaFd(dmaFd); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 159 | |
| 160 | void* vgaMem; |
| 161 | vgaMem = mmap(nullptr, pageAlignedLength, upstream ? PROT_WRITE : PROT_READ, |
| 162 | MAP_SHARED, xdmaFd(), 0); |
| 163 | if (MAP_FAILED == vgaMem) |
| 164 | { |
| 165 | rc = -errno; |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 166 | error( |
| 167 | "Failed to mmap the XDMA device for data transfer between BMC and remote terminus with response code '{RC}'", |
| 168 | "RC", rc); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | std::unique_ptr<void, decltype(mmapCleanup)> vgaMemPtr(vgaMem, mmapCleanup); |
| 173 | |
| 174 | if (upstream) |
| 175 | { |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 176 | rc = lseek(fd, offset, SEEK_SET); |
| 177 | if (rc == -1) |
| 178 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 179 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 180 | "Failed to transfer data between BMC and remote terminus due to lseek failure with upstream '{UPSTREAM}' at offset '{OFFSET}', error number - {ERROR_NUM}", |
| 181 | "ERROR_NUM", errno, "UPSTREAM", upstream, "OFFSET", offset); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 182 | return rc; |
| 183 | } |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 184 | |
| 185 | // Writing to the VGA memory should be aligned at page boundary, |
| 186 | // otherwise write data into a buffer aligned at page boundary and |
| 187 | // then write to the VGA memory. |
| 188 | std::vector<char> buffer{}; |
| 189 | buffer.resize(pageAlignedLength); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 190 | rc = read(fd, buffer.data(), length); |
| 191 | if (rc == -1) |
| 192 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 193 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 194 | "Failed to transfer data between BMC and remote terminus with file read on upstream '{UPSTREAM}' of length '{LENGTH}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}", |
| 195 | "ERROR_NUM", errno, "UPSTREAM", upstream, "LENGTH", length, |
| 196 | "OFFSET", offset); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 197 | return rc; |
| 198 | } |
| 199 | if (rc != static_cast<int>(length)) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 200 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 201 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 202 | "Failed to transfer data between BMC and remote terminus mismatched for number of characters to read on upstream '{UPSTREAM}' and the length '{LENGTH}' read and count '{RC}'", |
| 203 | "UPSTREAM", upstream, "LENGTH", length, "RC", rc); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 204 | return -1; |
| 205 | } |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 206 | memcpy(static_cast<char*>(vgaMemPtr.get()), buffer.data(), |
| 207 | pageAlignedLength); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | AspeedXdmaOp xdmaOp; |
| 211 | xdmaOp.upstream = upstream ? 1 : 0; |
| 212 | xdmaOp.hostAddr = address; |
| 213 | xdmaOp.len = length; |
| 214 | |
| 215 | rc = write(xdmaFd(), &xdmaOp, sizeof(xdmaOp)); |
| 216 | if (rc < 0) |
| 217 | { |
| 218 | rc = -errno; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 219 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 220 | "Failed to execute the DMA operation on data between BMC and remote terminus for upstream '{UPSTREAM}' of length '{LENGTH}' at address '{ADDRESS}', response code '{RC}'", |
| 221 | "RC", rc, "UPSTREAM", upstream, "ADDRESS", address, "LENGTH", |
| 222 | length); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | if (!upstream) |
| 227 | { |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 228 | rc = lseek(fd, offset, SEEK_SET); |
| 229 | if (rc == -1) |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 230 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 231 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 232 | "Failed to transfer data between BMC and remote terminus due to lseek failure '{UPSTREAM}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}", |
| 233 | "ERROR_NUM", errno, "UPSTREAM", upstream, "OFFSET", offset); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 234 | return rc; |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 235 | } |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 236 | rc = write(fd, static_cast<const char*>(vgaMemPtr.get()), length); |
| 237 | if (rc == -1) |
| 238 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 239 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 240 | "Failed to transfer data between BMC and remote terminus where file write upstream '{UPSTREAM}' of length '{LENGTH}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}", |
| 241 | "ERROR_NUM", errno, "UPSTREAM", upstream, "LENGTH", length, |
| 242 | "OFFSET", offset); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 243 | return rc; |
| 244 | } |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | } // namespace dma |
| 251 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 252 | namespace oem_ibm |
| 253 | { |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 254 | Response Handler::readFileIntoMemory(const pldm_msg* request, |
| 255 | size_t payloadLength) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 256 | { |
| 257 | uint32_t fileHandle = 0; |
| 258 | uint32_t offset = 0; |
| 259 | uint32_t length = 0; |
| 260 | uint64_t address = 0; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 261 | |
| 262 | Response response((sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES), 0); |
| 263 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 264 | |
| 265 | if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES) |
| 266 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 267 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 268 | PLDM_READ_FILE_INTO_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 269 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
| 270 | return response; |
| 271 | } |
| 272 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 273 | decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset, |
| 274 | &length, &address); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 275 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 276 | using namespace pldm::filetable; |
| 277 | auto& table = buildFileTable(FILE_TABLE_JSON); |
| 278 | FileEntry value{}; |
| 279 | |
| 280 | try |
| 281 | { |
| 282 | value = table.at(fileHandle); |
| 283 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 284 | catch (const std::exception& e) |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 285 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 286 | error( |
| 287 | "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}", |
| 288 | "HANDLE", fileHandle, "ERROR", e); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 289 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 290 | PLDM_READ_FILE_INTO_MEMORY, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 291 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 292 | return response; |
| 293 | } |
| 294 | |
| 295 | if (!fs::exists(value.fsPath)) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 296 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 297 | error("File '{PATH}' and handle '{FILE_HANDLE}' with does not exist", |
| 298 | "PATH", value.fsPath, "FILE_HANDLE", fileHandle); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 299 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 300 | PLDM_READ_FILE_INTO_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 301 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 302 | return response; |
| 303 | } |
| 304 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 305 | auto fileSize = fs::file_size(value.fsPath); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 306 | if (offset >= fileSize) |
| 307 | { |
Sagar Srinivas | 8225792 | 2023-07-10 08:12:22 -0500 | [diff] [blame] | 308 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 309 | "Offset '{OFFSET}' exceeds file size '{SIZE}' and file handle '{FILE_HANDLE}'", |
| 310 | "OFFSET", offset, "SIZE", fileSize, "FILE_HANDLE", fileHandle); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 311 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 312 | PLDM_READ_FILE_INTO_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 313 | PLDM_DATA_OUT_OF_RANGE, 0, responsePtr); |
| 314 | return response; |
| 315 | } |
| 316 | |
| 317 | if (offset + length > fileSize) |
| 318 | { |
| 319 | length = fileSize - offset; |
| 320 | } |
| 321 | |
| 322 | if (length % dma::minSize) |
| 323 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 324 | error("Packet length '{LENGTH}' is non multiple of minimum DMA size", |
| 325 | "LENGTH", length); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 326 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 327 | PLDM_READ_FILE_INTO_MEMORY, |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 328 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 329 | return response; |
| 330 | } |
| 331 | |
| 332 | using namespace dma; |
| 333 | DMA intf; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 334 | return transferAll<DMA>(&intf, PLDM_READ_FILE_INTO_MEMORY, value.fsPath, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 335 | offset, length, address, true, |
| 336 | request->hdr.instance_id); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 337 | } |
| 338 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 339 | Response Handler::writeFileFromMemory(const pldm_msg* request, |
| 340 | size_t payloadLength) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 341 | { |
| 342 | uint32_t fileHandle = 0; |
| 343 | uint32_t offset = 0; |
| 344 | uint32_t length = 0; |
| 345 | uint64_t address = 0; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 346 | |
| 347 | Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0); |
| 348 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 349 | |
| 350 | if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES) |
| 351 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 352 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 353 | PLDM_WRITE_FILE_FROM_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 354 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
| 355 | return response; |
| 356 | } |
| 357 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 358 | decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset, |
| 359 | &length, &address); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 360 | |
| 361 | if (length % dma::minSize) |
| 362 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 363 | error("Packet length '{LENGTH}' is non multiple of minimum DMA size", |
| 364 | "LENGTH", length); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 365 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 366 | PLDM_WRITE_FILE_FROM_MEMORY, |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 367 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 368 | return response; |
| 369 | } |
| 370 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 371 | using namespace pldm::filetable; |
| 372 | auto& table = buildFileTable(FILE_TABLE_JSON); |
| 373 | FileEntry value{}; |
| 374 | |
| 375 | try |
| 376 | { |
| 377 | value = table.at(fileHandle); |
| 378 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 379 | catch (const std::exception& e) |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 380 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 381 | error( |
| 382 | "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}", |
| 383 | "HANDLE", fileHandle, "ERROR", e); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 384 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 385 | PLDM_WRITE_FILE_FROM_MEMORY, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 386 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 387 | return response; |
| 388 | } |
| 389 | |
| 390 | if (!fs::exists(value.fsPath)) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 391 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 392 | error("File '{PATH}' does not exist for file handle '{FILE_HANDLE}'", |
| 393 | "PATH", value.fsPath, "FILE_HANDLE", fileHandle); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 394 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 395 | PLDM_WRITE_FILE_FROM_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 396 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 397 | return response; |
| 398 | } |
| 399 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 400 | auto fileSize = fs::file_size(value.fsPath); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 401 | if (offset >= fileSize) |
| 402 | { |
Sagar Srinivas | 8225792 | 2023-07-10 08:12:22 -0500 | [diff] [blame] | 403 | error( |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 404 | "Offset '{OFFSET}' exceeds file size {SIZE} for file '{PATH} and handle {FILE_HANDLE}", |
| 405 | "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath, |
| 406 | "FILE_HANDLE", fileHandle); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 407 | encode_rw_file_memory_resp(request->hdr.instance_id, |
| 408 | PLDM_WRITE_FILE_FROM_MEMORY, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 409 | PLDM_DATA_OUT_OF_RANGE, 0, responsePtr); |
| 410 | return response; |
| 411 | } |
| 412 | |
| 413 | using namespace dma; |
| 414 | DMA intf; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 415 | return transferAll<DMA>(&intf, PLDM_WRITE_FILE_FROM_MEMORY, value.fsPath, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 416 | offset, length, address, false, |
| 417 | request->hdr.instance_id); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 418 | } |
| 419 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 420 | Response Handler::getFileTable(const pldm_msg* request, size_t payloadLength) |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 421 | { |
| 422 | uint32_t transferHandle = 0; |
| 423 | uint8_t transferFlag = 0; |
| 424 | uint8_t tableType = 0; |
| 425 | |
| 426 | Response response(sizeof(pldm_msg_hdr) + |
| 427 | PLDM_GET_FILE_TABLE_MIN_RESP_BYTES); |
| 428 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 429 | |
| 430 | if (payloadLength != PLDM_GET_FILE_TABLE_REQ_BYTES) |
| 431 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 432 | encode_get_file_table_resp(request->hdr.instance_id, |
| 433 | PLDM_ERROR_INVALID_LENGTH, 0, 0, nullptr, 0, |
| 434 | responsePtr); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 435 | return response; |
| 436 | } |
| 437 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 438 | auto rc = decode_get_file_table_req(request, payloadLength, &transferHandle, |
| 439 | &transferFlag, &tableType); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 440 | if (rc) |
| 441 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 442 | encode_get_file_table_resp(request->hdr.instance_id, rc, 0, 0, nullptr, |
| 443 | 0, responsePtr); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 444 | return response; |
| 445 | } |
| 446 | |
| 447 | if (tableType != PLDM_FILE_ATTRIBUTE_TABLE) |
| 448 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 449 | encode_get_file_table_resp(request->hdr.instance_id, |
| 450 | PLDM_INVALID_FILE_TABLE_TYPE, 0, 0, nullptr, |
| 451 | 0, responsePtr); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 452 | return response; |
| 453 | } |
| 454 | |
| 455 | using namespace pldm::filetable; |
| 456 | auto table = buildFileTable(FILE_TABLE_JSON); |
| 457 | auto attrTable = table(); |
| 458 | response.resize(response.size() + attrTable.size()); |
| 459 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 460 | |
| 461 | if (attrTable.empty()) |
| 462 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 463 | encode_get_file_table_resp(request->hdr.instance_id, |
| 464 | PLDM_FILE_TABLE_UNAVAILABLE, 0, 0, nullptr, |
| 465 | 0, responsePtr); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 466 | return response; |
| 467 | } |
| 468 | |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 469 | encode_get_file_table_resp(request->hdr.instance_id, PLDM_SUCCESS, 0, |
| 470 | PLDM_START_AND_END, attrTable.data(), |
| 471 | attrTable.size(), responsePtr); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 472 | return response; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 473 | } |
| 474 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 475 | Response Handler::readFile(const pldm_msg* request, size_t payloadLength) |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 476 | { |
| 477 | uint32_t fileHandle = 0; |
| 478 | uint32_t offset = 0; |
| 479 | uint32_t length = 0; |
| 480 | |
| 481 | Response response(sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES); |
| 482 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 483 | |
| 484 | if (payloadLength != PLDM_READ_FILE_REQ_BYTES) |
| 485 | { |
| 486 | encode_read_file_resp(request->hdr.instance_id, |
| 487 | PLDM_ERROR_INVALID_LENGTH, length, responsePtr); |
| 488 | return response; |
| 489 | } |
| 490 | |
| 491 | auto rc = decode_read_file_req(request, payloadLength, &fileHandle, &offset, |
| 492 | &length); |
| 493 | |
| 494 | if (rc) |
| 495 | { |
| 496 | encode_read_file_resp(request->hdr.instance_id, rc, 0, responsePtr); |
| 497 | return response; |
| 498 | } |
| 499 | |
| 500 | using namespace pldm::filetable; |
| 501 | auto& table = buildFileTable(FILE_TABLE_JSON); |
| 502 | FileEntry value{}; |
| 503 | |
| 504 | try |
| 505 | { |
| 506 | value = table.at(fileHandle); |
| 507 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 508 | catch (const std::exception& e) |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 509 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 510 | error( |
| 511 | "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}", |
| 512 | "HANDLE", fileHandle, "ERROR", e); |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 513 | |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 514 | encode_read_file_resp(request->hdr.instance_id, |
| 515 | PLDM_INVALID_FILE_HANDLE, length, responsePtr); |
| 516 | return response; |
| 517 | } |
| 518 | |
| 519 | if (!fs::exists(value.fsPath)) |
| 520 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 521 | error("File '{PATH}' and handle {FILE_HANDLE} does not exist", "PATH", |
| 522 | value.fsPath, "FILE_HANDLE", fileHandle); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 523 | encode_read_file_resp(request->hdr.instance_id, |
| 524 | PLDM_INVALID_FILE_HANDLE, length, responsePtr); |
| 525 | return response; |
| 526 | } |
| 527 | |
| 528 | auto fileSize = fs::file_size(value.fsPath); |
| 529 | if (offset >= fileSize) |
| 530 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 531 | error( |
| 532 | "Offset '{OFFSET}' exceeds file size '{SIZE}' for file '{PATH}' and file handle '{HANDLE}'", |
| 533 | "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath, "HANDLE", |
| 534 | fileHandle); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 535 | encode_read_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE, |
| 536 | length, responsePtr); |
| 537 | return response; |
| 538 | } |
| 539 | |
| 540 | if (offset + length > fileSize) |
| 541 | { |
| 542 | length = fileSize - offset; |
| 543 | } |
| 544 | |
| 545 | response.resize(response.size() + length); |
| 546 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 547 | auto fileDataPos = reinterpret_cast<char*>(responsePtr); |
| 548 | fileDataPos += sizeof(pldm_msg_hdr) + sizeof(uint8_t) + sizeof(length); |
| 549 | |
| 550 | std::ifstream stream(value.fsPath, std::ios::in | std::ios::binary); |
| 551 | stream.seekg(offset); |
| 552 | stream.read(fileDataPos, length); |
| 553 | |
| 554 | encode_read_file_resp(request->hdr.instance_id, PLDM_SUCCESS, length, |
| 555 | responsePtr); |
| 556 | |
| 557 | return response; |
| 558 | } |
| 559 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 560 | Response Handler::writeFile(const pldm_msg* request, size_t payloadLength) |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 561 | { |
| 562 | uint32_t fileHandle = 0; |
| 563 | uint32_t offset = 0; |
| 564 | uint32_t length = 0; |
| 565 | size_t fileDataOffset = 0; |
| 566 | |
| 567 | Response response(sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES); |
| 568 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 569 | |
| 570 | if (payloadLength < PLDM_WRITE_FILE_REQ_BYTES) |
| 571 | { |
| 572 | encode_write_file_resp(request->hdr.instance_id, |
| 573 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
| 574 | return response; |
| 575 | } |
| 576 | |
| 577 | auto rc = decode_write_file_req(request, payloadLength, &fileHandle, |
| 578 | &offset, &length, &fileDataOffset); |
| 579 | |
| 580 | if (rc) |
| 581 | { |
| 582 | encode_write_file_resp(request->hdr.instance_id, rc, 0, responsePtr); |
| 583 | return response; |
| 584 | } |
| 585 | |
| 586 | using namespace pldm::filetable; |
| 587 | auto& table = buildFileTable(FILE_TABLE_JSON); |
| 588 | FileEntry value{}; |
| 589 | |
| 590 | try |
| 591 | { |
| 592 | value = table.at(fileHandle); |
| 593 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 594 | catch (const std::exception& e) |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 595 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 596 | error( |
| 597 | "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}", |
| 598 | "HANDLE", fileHandle, "ERROR", e); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 599 | encode_write_file_resp(request->hdr.instance_id, |
| 600 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 601 | return response; |
| 602 | } |
| 603 | |
| 604 | if (!fs::exists(value.fsPath)) |
| 605 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 606 | error("File '{PATH}' and handle {FILE_HANDLE} does not exist", "PATH", |
| 607 | value.fsPath, "FILE_HANDLE", fileHandle); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 608 | encode_write_file_resp(request->hdr.instance_id, |
| 609 | PLDM_INVALID_FILE_HANDLE, 0, responsePtr); |
| 610 | return response; |
| 611 | } |
| 612 | |
| 613 | auto fileSize = fs::file_size(value.fsPath); |
| 614 | if (offset >= fileSize) |
| 615 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 616 | error( |
| 617 | "Offset '{OFFSET}' exceeds file size '{SIZE}' for file '{PATH}' and handle {FILE_HANDLE}", |
| 618 | "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath, |
| 619 | "FILE_HANDLE", fileHandle); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 620 | encode_write_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE, |
| 621 | 0, responsePtr); |
| 622 | return response; |
| 623 | } |
| 624 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 625 | auto fileDataPos = reinterpret_cast<const char*>(request->payload) + |
| 626 | fileDataOffset; |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 627 | |
| 628 | std::ofstream stream(value.fsPath, |
| 629 | std::ios::in | std::ios::out | std::ios::binary); |
| 630 | stream.seekp(offset); |
| 631 | stream.write(fileDataPos, length); |
| 632 | |
| 633 | encode_write_file_resp(request->hdr.instance_id, PLDM_SUCCESS, length, |
| 634 | responsePtr); |
| 635 | |
| 636 | return response; |
| 637 | } |
| 638 | |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 639 | Response rwFileByTypeIntoMemory(uint8_t cmd, const pldm_msg* request, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 640 | size_t payloadLength, |
| 641 | oem_platform::Handler* oemPlatformHandler) |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 642 | { |
| 643 | Response response( |
| 644 | sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES, 0); |
| 645 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 646 | |
| 647 | if (payloadLength != PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES) |
| 648 | { |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 649 | encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, |
| 650 | PLDM_ERROR_INVALID_LENGTH, 0, |
| 651 | responsePtr); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 652 | return response; |
| 653 | } |
| 654 | |
| 655 | uint16_t fileType{}; |
| 656 | uint32_t fileHandle{}; |
| 657 | uint32_t offset{}; |
| 658 | uint32_t length{}; |
| 659 | uint64_t address{}; |
| 660 | auto rc = decode_rw_file_by_type_memory_req(request, payloadLength, |
| 661 | &fileType, &fileHandle, &offset, |
| 662 | &length, &address); |
| 663 | if (rc != PLDM_SUCCESS) |
| 664 | { |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 665 | encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, rc, 0, |
| 666 | responsePtr); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 667 | return response; |
| 668 | } |
| 669 | if (length % dma::minSize) |
| 670 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 671 | error("Packet length '{LENGTH}' is non multiple of minimum DMA size", |
| 672 | "LENGTH", length); |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 673 | encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 674 | PLDM_ERROR_INVALID_LENGTH, 0, |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 675 | responsePtr); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 676 | return response; |
| 677 | } |
| 678 | |
| 679 | std::unique_ptr<FileHandler> handler{}; |
| 680 | try |
| 681 | { |
| 682 | handler = getHandlerByType(fileType, fileHandle); |
| 683 | } |
| 684 | catch (const InternalFailure& e) |
| 685 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 686 | error("Unknown file type '{TYPE}', error - {ERROR} ", "TYPE", fileType, |
| 687 | "ERROR", e); |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 688 | encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, |
| 689 | PLDM_INVALID_FILE_TYPE, 0, |
| 690 | responsePtr); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 691 | return response; |
| 692 | } |
| 693 | |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 694 | rc = cmd == PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 695 | ? handler->writeFromMemory(offset, length, address, |
| 696 | oemPlatformHandler) |
| 697 | : handler->readIntoMemory(offset, length, address, |
| 698 | oemPlatformHandler); |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 699 | encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, rc, |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 700 | length, responsePtr); |
| 701 | return response; |
| 702 | } |
| 703 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 704 | Response Handler::writeFileByTypeFromMemory(const pldm_msg* request, |
| 705 | size_t payloadLength) |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 706 | { |
| 707 | return rwFileByTypeIntoMemory(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY, request, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 708 | payloadLength, oemPlatformHandler); |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 709 | } |
| 710 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 711 | Response Handler::readFileByTypeIntoMemory(const pldm_msg* request, |
| 712 | size_t payloadLength) |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 713 | { |
| 714 | return rwFileByTypeIntoMemory(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, request, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 715 | payloadLength, oemPlatformHandler); |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 716 | } |
| 717 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 718 | Response Handler::writeFileByType(const pldm_msg* request, size_t payloadLength) |
| 719 | { |
| 720 | Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES); |
| 721 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 722 | |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 723 | if (payloadLength < PLDM_RW_FILE_BY_TYPE_REQ_BYTES) |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 724 | { |
| 725 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 726 | PLDM_WRITE_FILE_BY_TYPE, |
| 727 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
| 728 | return response; |
| 729 | } |
| 730 | uint16_t fileType{}; |
| 731 | uint32_t fileHandle{}; |
| 732 | uint32_t offset{}; |
| 733 | uint32_t length{}; |
| 734 | |
| 735 | auto rc = decode_rw_file_by_type_req(request, payloadLength, &fileType, |
| 736 | &fileHandle, &offset, &length); |
| 737 | if (rc != PLDM_SUCCESS) |
| 738 | { |
| 739 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 740 | PLDM_WRITE_FILE_BY_TYPE, rc, 0, |
| 741 | responsePtr); |
| 742 | return response; |
| 743 | } |
| 744 | |
| 745 | std::unique_ptr<FileHandler> handler{}; |
| 746 | try |
| 747 | { |
| 748 | handler = getHandlerByType(fileType, fileHandle); |
| 749 | } |
| 750 | catch (const InternalFailure& e) |
| 751 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 752 | error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType, |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 753 | "ERROR", e); |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 754 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 755 | PLDM_WRITE_FILE_BY_TYPE, |
| 756 | PLDM_INVALID_FILE_TYPE, 0, responsePtr); |
| 757 | return response; |
| 758 | } |
| 759 | |
| 760 | rc = handler->write(reinterpret_cast<const char*>( |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 761 | request->payload + PLDM_RW_FILE_BY_TYPE_REQ_BYTES), |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 762 | offset, length, oemPlatformHandler); |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 763 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 764 | PLDM_WRITE_FILE_BY_TYPE, rc, length, |
| 765 | responsePtr); |
| 766 | return response; |
| 767 | } |
| 768 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 769 | Response Handler::readFileByType(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 770 | { |
| 771 | Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES); |
| 772 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 773 | |
| 774 | if (payloadLength != PLDM_RW_FILE_BY_TYPE_REQ_BYTES) |
| 775 | { |
| 776 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 777 | PLDM_READ_FILE_BY_TYPE, |
| 778 | PLDM_ERROR_INVALID_LENGTH, 0, responsePtr); |
| 779 | return response; |
| 780 | } |
| 781 | uint16_t fileType{}; |
| 782 | uint32_t fileHandle{}; |
| 783 | uint32_t offset{}; |
| 784 | uint32_t length{}; |
| 785 | |
| 786 | auto rc = decode_rw_file_by_type_req(request, payloadLength, &fileType, |
| 787 | &fileHandle, &offset, &length); |
| 788 | if (rc != PLDM_SUCCESS) |
| 789 | { |
| 790 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 791 | PLDM_READ_FILE_BY_TYPE, rc, 0, responsePtr); |
| 792 | return response; |
| 793 | } |
| 794 | |
| 795 | std::unique_ptr<FileHandler> handler{}; |
| 796 | try |
| 797 | { |
| 798 | handler = getHandlerByType(fileType, fileHandle); |
| 799 | } |
| 800 | catch (const InternalFailure& e) |
| 801 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 802 | error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType, |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 803 | "ERROR", e); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 804 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 805 | PLDM_READ_FILE_BY_TYPE, |
| 806 | PLDM_INVALID_FILE_TYPE, 0, responsePtr); |
| 807 | return response; |
| 808 | } |
| 809 | |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 810 | rc = handler->read(offset, length, response, oemPlatformHandler); |
Deepak Kodihalli | 9a26f89 | 2019-12-10 06:48:06 -0600 | [diff] [blame] | 811 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 812 | encode_rw_file_by_type_resp(request->hdr.instance_id, |
| 813 | PLDM_READ_FILE_BY_TYPE, rc, length, |
| 814 | responsePtr); |
| 815 | return response; |
| 816 | } |
| 817 | |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 818 | Response Handler::fileAck(const pldm_msg* request, size_t payloadLength) |
| 819 | { |
| 820 | Response response(sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES); |
| 821 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 822 | |
| 823 | if (payloadLength != PLDM_FILE_ACK_REQ_BYTES) |
| 824 | { |
| 825 | encode_file_ack_resp(request->hdr.instance_id, |
| 826 | PLDM_ERROR_INVALID_LENGTH, responsePtr); |
| 827 | return response; |
| 828 | } |
| 829 | uint16_t fileType{}; |
| 830 | uint32_t fileHandle{}; |
| 831 | uint8_t fileStatus{}; |
| 832 | |
| 833 | auto rc = decode_file_ack_req(request, payloadLength, &fileType, |
| 834 | &fileHandle, &fileStatus); |
| 835 | if (rc != PLDM_SUCCESS) |
| 836 | { |
| 837 | encode_file_ack_resp(request->hdr.instance_id, rc, responsePtr); |
| 838 | return response; |
| 839 | } |
| 840 | |
| 841 | std::unique_ptr<FileHandler> handler{}; |
| 842 | try |
| 843 | { |
| 844 | handler = getHandlerByType(fileType, fileHandle); |
| 845 | } |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 846 | |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 847 | catch (const InternalFailure& e) |
| 848 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 849 | error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType, |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 850 | "ERROR", e); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 851 | encode_file_ack_resp(request->hdr.instance_id, PLDM_INVALID_FILE_TYPE, |
| 852 | responsePtr); |
| 853 | return response; |
| 854 | } |
| 855 | |
| 856 | rc = handler->fileAck(fileStatus); |
| 857 | encode_file_ack_resp(request->hdr.instance_id, rc, responsePtr); |
| 858 | return response; |
| 859 | } |
| 860 | |
George Liu | 89aad71 | 2020-03-12 13:34:51 +0800 | [diff] [blame] | 861 | Response Handler::getAlertStatus(const pldm_msg* request, size_t payloadLength) |
| 862 | { |
| 863 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_ALERT_STATUS_RESP_BYTES); |
| 864 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 865 | if (payloadLength != PLDM_GET_ALERT_STATUS_REQ_BYTES) |
| 866 | { |
| 867 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 868 | } |
| 869 | |
| 870 | uint8_t versionId{}; |
| 871 | |
| 872 | auto rc = decode_get_alert_status_req(request, payloadLength, &versionId); |
| 873 | if (rc != PLDM_SUCCESS) |
| 874 | { |
| 875 | return CmdHandler::ccOnlyResponse(request, rc); |
| 876 | } |
| 877 | |
| 878 | if (versionId != 0) |
| 879 | { |
| 880 | return CmdHandler::ccOnlyResponse(request, |
| 881 | PLDM_HOST_UNSUPPORTED_FORMAT_VERSION); |
| 882 | } |
| 883 | |
| 884 | constexpr uint32_t rackEntry = 0xFF000030; |
| 885 | constexpr uint32_t priCecNode = 0x00008030; |
| 886 | rc = encode_get_alert_status_resp(request->hdr.instance_id, PLDM_SUCCESS, |
| 887 | rackEntry, priCecNode, responsePtr, |
| 888 | PLDM_GET_ALERT_STATUS_RESP_BYTES); |
| 889 | if (rc != PLDM_SUCCESS) |
| 890 | { |
| 891 | return CmdHandler::ccOnlyResponse(request, rc); |
| 892 | } |
| 893 | |
| 894 | return response; |
| 895 | } |
| 896 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 897 | Response Handler::newFileAvailable(const pldm_msg* request, |
| 898 | size_t payloadLength) |
| 899 | { |
| 900 | Response response(sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES); |
| 901 | |
| 902 | if (payloadLength != PLDM_NEW_FILE_REQ_BYTES) |
| 903 | { |
| 904 | return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH); |
| 905 | } |
| 906 | uint16_t fileType{}; |
| 907 | uint32_t fileHandle{}; |
| 908 | uint64_t length{}; |
| 909 | |
| 910 | auto rc = decode_new_file_req(request, payloadLength, &fileType, |
| 911 | &fileHandle, &length); |
| 912 | |
| 913 | if (rc != PLDM_SUCCESS) |
| 914 | { |
| 915 | return CmdHandler::ccOnlyResponse(request, rc); |
| 916 | } |
| 917 | |
| 918 | std::unique_ptr<FileHandler> handler{}; |
| 919 | try |
| 920 | { |
| 921 | handler = getHandlerByType(fileType, fileHandle); |
| 922 | } |
| 923 | catch (const InternalFailure& e) |
| 924 | { |
Riya Dixit | fc84f63 | 2024-04-06 14:00:02 -0500 | [diff] [blame^] | 925 | error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType, |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 926 | "ERROR", e); |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 927 | return CmdHandler::ccOnlyResponse(request, PLDM_INVALID_FILE_TYPE); |
| 928 | } |
| 929 | |
| 930 | rc = handler->newFileAvailable(length); |
| 931 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 932 | encode_new_file_resp(request->hdr.instance_id, rc, responsePtr); |
| 933 | return response; |
| 934 | } |
| 935 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 936 | } // namespace oem_ibm |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 937 | } // namespace responder |
| 938 | } // namespace pldm |