Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 3 | #include "handler.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 4 | #include "utils.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 5 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 6 | #include <fcntl.h> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 7 | #include <stdint.h> |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <filesystem> |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 13 | #include <iostream> |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 14 | #include <vector> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 15 | |
| 16 | #include "libpldm/base.h" |
Deepak Kodihalli | da87ec6 | 2019-07-02 01:01:16 -0500 | [diff] [blame] | 17 | #include "oem/ibm/libpldm/file_io.h" |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 18 | |
| 19 | namespace pldm |
| 20 | { |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 21 | namespace responder |
| 22 | { |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 23 | namespace dma |
| 24 | { |
| 25 | |
| 26 | // The minimum data size of dma transfer in bytes |
| 27 | constexpr uint32_t minSize = 16; |
| 28 | |
| 29 | // 16MB - 4096B (16773120 bytes) is the maximum data size of DMA transfer |
| 30 | constexpr size_t maxSize = (16 * 1024 * 1024) - 4096; |
| 31 | |
| 32 | namespace fs = std::filesystem; |
| 33 | |
| 34 | /** |
| 35 | * @class DMA |
| 36 | * |
| 37 | * Expose API to initiate transfer of data by DMA |
| 38 | * |
| 39 | * This class only exposes the public API transferDataHost to transfer data |
| 40 | * between BMC and host using DMA. This allows for mocking the transferDataHost |
| 41 | * for unit testing purposes. |
| 42 | */ |
| 43 | class DMA |
| 44 | { |
| 45 | public: |
| 46 | /** @brief API to transfer data between BMC and host using DMA |
| 47 | * |
| 48 | * @param[in] path - pathname of the file to transfer data from or to |
| 49 | * @param[in] offset - offset in the file |
| 50 | * @param[in] length - length of the data to transfer |
| 51 | * @param[in] address - DMA address on the host |
| 52 | * @param[in] upstream - indicates direction of the transfer; true indicates |
| 53 | * transfer to the host |
| 54 | * |
| 55 | * @return returns 0 on success, negative errno on failure |
| 56 | */ |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 57 | int transferDataHost(int fd, uint32_t offset, uint32_t length, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 58 | uint64_t address, bool upstream); |
| 59 | }; |
| 60 | |
| 61 | /** @brief Transfer the data between BMC and host using DMA. |
| 62 | * |
| 63 | * There is a max size for each DMA operation, transferAll API abstracts this |
| 64 | * and the requested length is broken down into multiple DMA operations if the |
| 65 | * length exceed max size. |
| 66 | * |
| 67 | * @tparam[in] T - DMA interface type |
| 68 | * @param[in] intf - interface passed to invoke DMA transfer |
| 69 | * @param[in] command - PLDM command |
| 70 | * @param[in] path - pathname of the file to transfer data from or to |
| 71 | * @param[in] offset - offset in the file |
| 72 | * @param[in] length - length of the data to transfer |
| 73 | * @param[in] address - DMA address on the host |
| 74 | * @param[in] upstream - indicates direction of the transfer; true indicates |
| 75 | * transfer to the host |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 76 | * @param[in] instanceId - Message's instance id |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 77 | * @return PLDM response message |
| 78 | */ |
| 79 | |
| 80 | template <class DMAInterface> |
| 81 | Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path, |
| 82 | uint32_t offset, uint32_t length, uint64_t address, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 83 | bool upstream, uint8_t instanceId) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 84 | { |
| 85 | uint32_t origLength = length; |
| 86 | Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0); |
| 87 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 88 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 89 | int flags{}; |
| 90 | if (upstream) |
| 91 | { |
| 92 | flags = O_RDONLY; |
| 93 | } |
| 94 | else if (fs::exists(path)) |
| 95 | { |
| 96 | flags = O_RDWR; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | flags = O_WRONLY; |
| 101 | } |
| 102 | int file = open(path.string().c_str(), flags); |
| 103 | if (file == -1) |
| 104 | { |
| 105 | std::cerr << "File does not exist, path = " << path.string() << "\n"; |
| 106 | encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, |
| 107 | responsePtr); |
| 108 | return response; |
| 109 | } |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 110 | pldm::utils::CustomFD fd(file); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 111 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 112 | while (length > dma::maxSize) |
| 113 | { |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 114 | auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 115 | upstream); |
| 116 | if (rc < 0) |
| 117 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 118 | encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, |
| 119 | responsePtr); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 120 | return response; |
| 121 | } |
| 122 | |
| 123 | offset += dma::maxSize; |
| 124 | length -= dma::maxSize; |
| 125 | address += dma::maxSize; |
| 126 | } |
| 127 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 128 | auto rc = intf->transferDataHost(fd(), offset, length, address, upstream); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 129 | if (rc < 0) |
| 130 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 131 | encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, |
| 132 | responsePtr); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 133 | return response; |
| 134 | } |
| 135 | |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 136 | encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 137 | responsePtr); |
| 138 | return response; |
| 139 | } |
| 140 | |
| 141 | } // namespace dma |
| 142 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 143 | namespace oem_ibm |
| 144 | { |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 145 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 146 | class Handler : public CmdHandler |
| 147 | { |
| 148 | public: |
| 149 | Handler() |
| 150 | { |
| 151 | handlers.emplace(PLDM_READ_FILE_INTO_MEMORY, |
| 152 | [this](const pldm_msg* request, size_t payloadLength) { |
| 153 | return this->readFileIntoMemory(request, |
| 154 | payloadLength); |
| 155 | }); |
| 156 | handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY, |
| 157 | [this](const pldm_msg* request, size_t payloadLength) { |
| 158 | return this->writeFileFromMemory(request, |
| 159 | payloadLength); |
| 160 | }); |
| 161 | handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY, |
| 162 | [this](const pldm_msg* request, size_t payloadLength) { |
| 163 | return this->writeFileByTypeFromMemory( |
| 164 | request, payloadLength); |
| 165 | }); |
| 166 | handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, |
| 167 | [this](const pldm_msg* request, size_t payloadLength) { |
| 168 | return this->readFileByTypeIntoMemory( |
| 169 | request, payloadLength); |
| 170 | }); |
| 171 | handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request, |
| 172 | size_t payloadLength) { |
| 173 | return this->readFileByType(request, payloadLength); |
| 174 | }); |
| 175 | handlers.emplace(PLDM_GET_FILE_TABLE, |
| 176 | [this](const pldm_msg* request, size_t payloadLength) { |
| 177 | return this->getFileTable(request, payloadLength); |
| 178 | }); |
| 179 | handlers.emplace(PLDM_READ_FILE, |
| 180 | [this](const pldm_msg* request, size_t payloadLength) { |
| 181 | return this->readFile(request, payloadLength); |
| 182 | }); |
| 183 | handlers.emplace(PLDM_WRITE_FILE, |
| 184 | [this](const pldm_msg* request, size_t payloadLength) { |
| 185 | return this->writeFile(request, payloadLength); |
| 186 | }); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 187 | handlers.emplace(PLDM_FILE_ACK, |
| 188 | [this](const pldm_msg* request, size_t payloadLength) { |
| 189 | return this->fileAck(request, payloadLength); |
| 190 | }); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 191 | } |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 192 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 193 | /** @brief Handler for readFileIntoMemory command |
| 194 | * |
| 195 | * @param[in] request - pointer to PLDM request payload |
| 196 | * @param[in] payloadLength - length of the message |
| 197 | * |
| 198 | * @return PLDM response message |
| 199 | */ |
| 200 | Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 201 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 202 | /** @brief Handler for writeFileIntoMemory command |
| 203 | * |
| 204 | * @param[in] request - pointer to PLDM request payload |
| 205 | * @param[in] payloadLength - length of the message |
| 206 | * |
| 207 | * @return PLDM response message |
| 208 | */ |
| 209 | Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 210 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 211 | /** @brief Handler for writeFileByTypeFromMemory command |
| 212 | * |
| 213 | * @param[in] request - pointer to PLDM request payload |
| 214 | * @param[in] payloadLength - length of the message |
| 215 | * |
| 216 | * @return PLDM response message |
| 217 | */ |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 218 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 219 | Response writeFileByTypeFromMemory(const pldm_msg* request, |
| 220 | size_t payloadLength); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 221 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 222 | /** @brief Handler for readFileByTypeIntoMemory command |
| 223 | * |
| 224 | * @param[in] request - pointer to PLDM request payload |
| 225 | * @param[in] payloadLength - length of the message |
| 226 | * |
| 227 | * @return PLDM response message |
| 228 | */ |
| 229 | Response readFileByTypeIntoMemory(const pldm_msg* request, |
| 230 | size_t payloadLength); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 231 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 232 | /** @brief Handler for readFileByType command |
| 233 | * |
| 234 | * @param[in] request - pointer to PLDM request payload |
| 235 | * @param[in] payloadLength - length of the message |
| 236 | * |
| 237 | * @return PLDM response message |
| 238 | */ |
| 239 | Response readFileByType(const pldm_msg* request, size_t payloadLength); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 240 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 241 | /** @brief Handler for GetFileTable command |
| 242 | * |
| 243 | * @param[in] request - pointer to PLDM request payload |
| 244 | * @param[in] payloadLength - length of the message payload |
| 245 | * |
| 246 | * @return PLDM response message |
| 247 | */ |
| 248 | Response getFileTable(const pldm_msg* request, size_t payloadLength); |
| 249 | |
| 250 | /** @brief Handler for readFile command |
| 251 | * |
| 252 | * @param[in] request - PLDM request msg |
| 253 | * @param[in] payloadLength - length of the message payload |
| 254 | * |
| 255 | * @return PLDM response message |
| 256 | */ |
| 257 | Response readFile(const pldm_msg* request, size_t payloadLength); |
| 258 | |
| 259 | /** @brief Handler for writeFile command |
| 260 | * |
| 261 | * @param[in] request - PLDM request msg |
| 262 | * @param[in] payloadLength - length of the message payload |
| 263 | * |
| 264 | * @return PLDM response message |
| 265 | */ |
| 266 | Response writeFile(const pldm_msg* request, size_t payloadLength); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 267 | |
| 268 | Response fileAck(const pldm_msg* request, size_t payloadLength); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | } // namespace oem_ibm |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 272 | } // namespace responder |
| 273 | } // namespace pldm |