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