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