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