1 #pragma once 2 3 #include "config.h" 4 5 #include "libpldm/base.h" 6 #include "oem/ibm/libpldm/file_io.h" 7 #include "oem/ibm/libpldm/host.h" 8 9 #include "common/utils.hpp" 10 #include "oem/ibm/requester/dbus_to_file_handler.hpp" 11 #include "oem_ibm_handler.hpp" 12 #include "pldmd/handler.hpp" 13 14 #include <fcntl.h> 15 #include <stdint.h> 16 #include <sys/stat.h> 17 #include <sys/types.h> 18 #include <unistd.h> 19 20 #include <filesystem> 21 #include <iostream> 22 #include <vector> 23 24 namespace pldm 25 { 26 namespace responder 27 { 28 namespace dma 29 { 30 31 // The minimum data size of dma transfer in bytes 32 constexpr uint32_t minSize = 16; 33 34 constexpr size_t maxSize = DMA_MAXSIZE; 35 36 namespace fs = std::filesystem; 37 38 /** 39 * @class DMA 40 * 41 * Expose API to initiate transfer of data by DMA 42 * 43 * This class only exposes the public API transferDataHost to transfer data 44 * between BMC and host using DMA. This allows for mocking the transferDataHost 45 * for unit testing purposes. 46 */ 47 class DMA 48 { 49 public: 50 /** @brief API to transfer data between BMC and host using DMA 51 * 52 * @param[in] path - pathname of the file to transfer data from or to 53 * @param[in] offset - offset in the file 54 * @param[in] length - length of the data to transfer 55 * @param[in] address - DMA address on the host 56 * @param[in] upstream - indicates direction of the transfer; true indicates 57 * transfer to the host 58 * 59 * @return returns 0 on success, negative errno on failure 60 */ 61 int transferDataHost(int fd, uint32_t offset, uint32_t length, 62 uint64_t address, bool upstream); 63 64 /** @brief API to transfer data on to unix socket from host using DMA 65 * 66 * @param[in] path - pathname of the file to transfer data from or to 67 * @param[in] length - length of the data to transfer 68 * @param[in] address - DMA address on the host 69 * 70 * @return returns 0 on success, negative errno on failure 71 */ 72 int transferHostDataToSocket(int fd, uint32_t length, uint64_t address); 73 }; 74 75 /** @brief Transfer the data between BMC and host using DMA. 76 * 77 * There is a max size for each DMA operation, transferAll API abstracts this 78 * and the requested length is broken down into multiple DMA operations if the 79 * length exceed max size. 80 * 81 * @tparam[in] T - DMA interface type 82 * @param[in] intf - interface passed to invoke DMA transfer 83 * @param[in] command - PLDM command 84 * @param[in] path - pathname of the file to transfer data from or to 85 * @param[in] offset - offset in the file 86 * @param[in] length - length of the data to transfer 87 * @param[in] address - DMA address on the host 88 * @param[in] upstream - indicates direction of the transfer; true indicates 89 * transfer to the host 90 * @param[in] instanceId - Message's instance id 91 * @return PLDM response message 92 */ 93 94 template <class DMAInterface> 95 Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path, 96 uint32_t offset, uint32_t length, uint64_t address, 97 bool upstream, uint8_t instanceId) 98 { 99 uint32_t origLength = length; 100 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0); 101 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); 102 103 int flags{}; 104 if (upstream) 105 { 106 flags = O_RDONLY; 107 } 108 else if (fs::exists(path)) 109 { 110 flags = O_RDWR; 111 } 112 else 113 { 114 flags = O_WRONLY; 115 } 116 int file = open(path.string().c_str(), flags); 117 if (file == -1) 118 { 119 std::cerr << "File does not exist, path = " << path.string() << "\n"; 120 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, 121 responsePtr); 122 return response; 123 } 124 pldm::utils::CustomFD fd(file); 125 126 while (length > dma::maxSize) 127 { 128 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address, 129 upstream); 130 if (rc < 0) 131 { 132 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, 133 responsePtr); 134 return response; 135 } 136 137 offset += dma::maxSize; 138 length -= dma::maxSize; 139 address += dma::maxSize; 140 } 141 142 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream); 143 if (rc < 0) 144 { 145 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0, 146 responsePtr); 147 return response; 148 } 149 150 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength, 151 responsePtr); 152 return response; 153 } 154 155 } // namespace dma 156 157 namespace oem_ibm 158 { 159 static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/resource/entry/"; 160 static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource"; 161 162 static constexpr auto certObjPath = "/xyz/openbmc_project/certs/ca/"; 163 static constexpr auto certAuthority = 164 "xyz.openbmc_project.PLDM.Provider.Certs.Authority.CSR"; 165 class Handler : public CmdHandler 166 { 167 public: 168 Handler(oem_platform::Handler* oemPlatformHandler, int hostSockFd, 169 uint8_t hostEid, dbus_api::Requester* dbusImplReqester) : 170 oemPlatformHandler(oemPlatformHandler), 171 hostSockFd(hostSockFd), hostEid(hostEid), 172 dbusImplReqester(dbusImplReqester) 173 { 174 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY, 175 [this](const pldm_msg* request, size_t payloadLength) { 176 return this->readFileIntoMemory(request, 177 payloadLength); 178 }); 179 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY, 180 [this](const pldm_msg* request, size_t payloadLength) { 181 return this->writeFileFromMemory(request, 182 payloadLength); 183 }); 184 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY, 185 [this](const pldm_msg* request, size_t payloadLength) { 186 return this->writeFileByTypeFromMemory( 187 request, payloadLength); 188 }); 189 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, 190 [this](const pldm_msg* request, size_t payloadLength) { 191 return this->readFileByTypeIntoMemory( 192 request, payloadLength); 193 }); 194 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request, 195 size_t payloadLength) { 196 return this->readFileByType(request, payloadLength); 197 }); 198 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE, 199 [this](const pldm_msg* request, size_t payloadLength) { 200 return this->writeFileByType(request, 201 payloadLength); 202 }); 203 handlers.emplace(PLDM_GET_FILE_TABLE, 204 [this](const pldm_msg* request, size_t payloadLength) { 205 return this->getFileTable(request, payloadLength); 206 }); 207 handlers.emplace(PLDM_READ_FILE, 208 [this](const pldm_msg* request, size_t payloadLength) { 209 return this->readFile(request, payloadLength); 210 }); 211 handlers.emplace(PLDM_WRITE_FILE, 212 [this](const pldm_msg* request, size_t payloadLength) { 213 return this->writeFile(request, payloadLength); 214 }); 215 handlers.emplace(PLDM_FILE_ACK, 216 [this](const pldm_msg* request, size_t payloadLength) { 217 return this->fileAck(request, payloadLength); 218 }); 219 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS, 220 [this](const pldm_msg* request, size_t payloadLength) { 221 return this->getAlertStatus(request, 222 payloadLength); 223 }); 224 handlers.emplace(PLDM_NEW_FILE_AVAILABLE, 225 [this](const pldm_msg* request, size_t payloadLength) { 226 return this->newFileAvailable(request, 227 payloadLength); 228 }); 229 230 resDumpMatcher = std::make_unique<sdbusplus::bus::match::match>( 231 pldm::utils::DBusHandler::getBus(), 232 sdbusplus::bus::match::rules::interfacesAdded() + 233 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath), 234 [hostSockFd, hostEid, 235 dbusImplReqester](sdbusplus::message::message& msg) { 236 std::map< 237 std::string, 238 std::map<std::string, std::variant<std::string, uint32_t>>> 239 interfaces; 240 sdbusplus::message::object_path path; 241 msg.read(path, interfaces); 242 std::string vspstring; 243 std::string password; 244 245 for (auto& interface : interfaces) 246 { 247 if (interface.first == resDumpEntry) 248 { 249 for (const auto& property : interface.second) 250 { 251 if (property.first == "VSPString") 252 { 253 vspstring = 254 std::get<std::string>(property.second); 255 } 256 else if (property.first == "Password") 257 { 258 password = 259 std::get<std::string>(property.second); 260 } 261 } 262 auto dbusToFileHandler = std::make_unique< 263 pldm::requester::oem_ibm::DbusToFileHandler>( 264 hostSockFd, hostEid, dbusImplReqester, path); 265 dbusToFileHandler->processNewResourceDump(vspstring, 266 password); 267 break; 268 } 269 } 270 }); 271 vmiCertMatcher = std::make_unique<sdbusplus::bus::match::match>( 272 pldm::utils::DBusHandler::getBus(), 273 sdbusplus::bus::match::rules::interfacesAdded() + 274 sdbusplus::bus::match::rules::argNpath(0, certObjPath), 275 [hostSockFd, hostEid, 276 dbusImplReqester](sdbusplus::message::message& msg) { 277 std::map< 278 std::string, 279 std::map<std::string, std::variant<std::string, uint32_t>>> 280 interfaces; 281 sdbusplus::message::object_path path; 282 msg.read(path, interfaces); 283 std::string csr; 284 285 for (auto& interface : interfaces) 286 { 287 if (interface.first == certAuthority) 288 { 289 for (const auto& property : interface.second) 290 { 291 if (property.first == "CSR") 292 { 293 csr = std::get<std::string>(property.second); 294 auto fileHandle = 295 sdbusplus::message::object_path(path) 296 .filename(); 297 298 auto dbusToFileHandler = 299 std::make_unique<pldm::requester::oem_ibm:: 300 DbusToFileHandler>( 301 hostSockFd, hostEid, dbusImplReqester, 302 path); 303 dbusToFileHandler->newCsrFileAvailable( 304 csr, fileHandle); 305 break; 306 } 307 } 308 break; 309 } 310 } 311 }); 312 } 313 314 /** @brief Handler for readFileIntoMemory command 315 * 316 * @param[in] request - pointer to PLDM request payload 317 * @param[in] payloadLength - length of the message 318 * 319 * @return PLDM response message 320 */ 321 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength); 322 323 /** @brief Handler for writeFileIntoMemory command 324 * 325 * @param[in] request - pointer to PLDM request payload 326 * @param[in] payloadLength - length of the message 327 * 328 * @return PLDM response message 329 */ 330 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength); 331 332 /** @brief Handler for writeFileByTypeFromMemory command 333 * 334 * @param[in] request - pointer to PLDM request payload 335 * @param[in] payloadLength - length of the message 336 * 337 * @return PLDM response message 338 */ 339 340 Response writeFileByTypeFromMemory(const pldm_msg* request, 341 size_t payloadLength); 342 343 /** @brief Handler for readFileByTypeIntoMemory command 344 * 345 * @param[in] request - pointer to PLDM request payload 346 * @param[in] payloadLength - length of the message 347 * 348 * @return PLDM response message 349 */ 350 Response readFileByTypeIntoMemory(const pldm_msg* request, 351 size_t payloadLength); 352 353 /** @brief Handler for writeFileByType command 354 * 355 * @param[in] request - pointer to PLDM request payload 356 * @param[in] payloadLength - length of the message 357 * 358 * @return PLDM response message 359 */ 360 Response readFileByType(const pldm_msg* request, size_t payloadLength); 361 362 Response writeFileByType(const pldm_msg* request, size_t payloadLength); 363 364 /** @brief Handler for GetFileTable command 365 * 366 * @param[in] request - pointer to PLDM request payload 367 * @param[in] payloadLength - length of the message payload 368 * 369 * @return PLDM response message 370 */ 371 Response getFileTable(const pldm_msg* request, size_t payloadLength); 372 373 /** @brief Handler for readFile command 374 * 375 * @param[in] request - PLDM request msg 376 * @param[in] payloadLength - length of the message payload 377 * 378 * @return PLDM response message 379 */ 380 Response readFile(const pldm_msg* request, size_t payloadLength); 381 382 /** @brief Handler for writeFile command 383 * 384 * @param[in] request - PLDM request msg 385 * @param[in] payloadLength - length of the message payload 386 * 387 * @return PLDM response message 388 */ 389 Response writeFile(const pldm_msg* request, size_t payloadLength); 390 391 Response fileAck(const pldm_msg* request, size_t payloadLength); 392 393 /** @brief Handler for getAlertStatus command 394 * 395 * @param[in] request - PLDM request msg 396 * @param[in] payloadLength - length of the message payload 397 * 398 * @return PLDM response message 399 */ 400 Response getAlertStatus(const pldm_msg* request, size_t payloadLength); 401 402 /** @brief Handler for newFileAvailable command 403 * 404 * @param[in] request - PLDM request msg 405 * @param[in] payloadLength - length of the message payload 406 * 407 * @return PLDM response message 408 */ 409 Response newFileAvailable(const pldm_msg* request, size_t payloadLength); 410 411 private: 412 oem_platform::Handler* oemPlatformHandler; 413 int hostSockFd; 414 uint8_t hostEid; 415 dbus_api::Requester* dbusImplReqester; 416 using DBusInterfaceAdded = std::vector<std::pair< 417 std::string, 418 std::vector<std::pair<std::string, std::variant<std::string>>>>>; 419 std::unique_ptr<sdbusplus::bus::match::match> 420 resDumpMatcher; //!< Pointer to capture the interface added signal 421 //!< for new resource dump 422 std::unique_ptr<sdbusplus::bus::match::match> 423 vmiCertMatcher; //!< Pointer to capture the interface added signal 424 //!< for new csr string 425 }; 426 427 } // namespace oem_ibm 428 } // namespace responder 429 } // namespace pldm 430