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 #include "requester/handler.hpp"
14 
15 #include <fcntl.h>
16 #include <stdint.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include <filesystem>
22 #include <iostream>
23 #include <vector>
24 
25 namespace pldm
26 {
27 namespace responder
28 {
29 namespace dma
30 {
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>
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         std::cerr << "File does not exist, path = " << path.string() << "\n";
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:
169     Handler(oem_platform::Handler* oemPlatformHandler, int hostSockFd,
170             uint8_t hostEid, dbus_api::Requester* dbusImplReqester,
171             pldm::requester::Handler<pldm::requester::Request>* handler) :
172         oemPlatformHandler(oemPlatformHandler),
173         hostSockFd(hostSockFd), hostEid(hostEid),
174         dbusImplReqester(dbusImplReqester), handler(handler)
175     {
176         handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
177                          [this](const pldm_msg* request, size_t payloadLength) {
178                              return this->readFileIntoMemory(request,
179                                                              payloadLength);
180                          });
181         handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
182                          [this](const pldm_msg* request, size_t payloadLength) {
183                              return this->writeFileFromMemory(request,
184                                                               payloadLength);
185                          });
186         handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
187                          [this](const pldm_msg* request, size_t payloadLength) {
188                              return this->writeFileByTypeFromMemory(
189                                  request, payloadLength);
190                          });
191         handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
192                          [this](const pldm_msg* request, size_t payloadLength) {
193                              return this->readFileByTypeIntoMemory(
194                                  request, payloadLength);
195                          });
196         handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
197                                                         size_t payloadLength) {
198             return this->readFileByType(request, payloadLength);
199         });
200         handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
201                          [this](const pldm_msg* request, size_t payloadLength) {
202                              return this->writeFileByType(request,
203                                                           payloadLength);
204                          });
205         handlers.emplace(PLDM_GET_FILE_TABLE,
206                          [this](const pldm_msg* request, size_t payloadLength) {
207                              return this->getFileTable(request, payloadLength);
208                          });
209         handlers.emplace(PLDM_READ_FILE,
210                          [this](const pldm_msg* request, size_t payloadLength) {
211                              return this->readFile(request, payloadLength);
212                          });
213         handlers.emplace(PLDM_WRITE_FILE,
214                          [this](const pldm_msg* request, size_t payloadLength) {
215                              return this->writeFile(request, payloadLength);
216                          });
217         handlers.emplace(PLDM_FILE_ACK,
218                          [this](const pldm_msg* request, size_t payloadLength) {
219                              return this->fileAck(request, payloadLength);
220                          });
221         handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
222                          [this](const pldm_msg* request, size_t payloadLength) {
223                              return this->getAlertStatus(request,
224                                                          payloadLength);
225                          });
226         handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
227                          [this](const pldm_msg* request, size_t payloadLength) {
228                              return this->newFileAvailable(request,
229                                                            payloadLength);
230                          });
231 
232         resDumpMatcher = std::make_unique<sdbusplus::bus::match::match>(
233             pldm::utils::DBusHandler::getBus(),
234             sdbusplus::bus::match::rules::interfacesAdded() +
235                 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath),
236             [this, hostSockFd, hostEid, dbusImplReqester,
237              &handler](sdbusplus::message::message& msg) {
238                 std::map<
239                     std::string,
240                     std::map<std::string, std::variant<std::string, uint32_t>>>
241                     interfaces;
242                 sdbusplus::message::object_path path;
243                 msg.read(path, interfaces);
244                 std::string vspstring;
245                 std::string password;
246 
247                 for (auto& interface : interfaces)
248                 {
249                     if (interface.first == resDumpEntry)
250                     {
251                         for (const auto& property : interface.second)
252                         {
253                             if (property.first == "VSPString")
254                             {
255                                 vspstring =
256                                     std::get<std::string>(property.second);
257                             }
258                             else if (property.first == "Password")
259                             {
260                                 password =
261                                     std::get<std::string>(property.second);
262                             }
263                         }
264                         dbusToFileHandlers
265                             .emplace_back(
266                                 std::make_unique<pldm::requester::oem_ibm::
267                                                      DbusToFileHandler>(
268                                     hostSockFd, hostEid, dbusImplReqester, path,
269                                     handler))
270                             ->processNewResourceDump(vspstring, password);
271                         break;
272                     }
273                 }
274             });
275         vmiCertMatcher = std::make_unique<sdbusplus::bus::match::match>(
276             pldm::utils::DBusHandler::getBus(),
277             sdbusplus::bus::match::rules::interfacesAdded() +
278                 sdbusplus::bus::match::rules::argNpath(0, certObjPath),
279             [this, hostSockFd, hostEid, dbusImplReqester,
280              &handler](sdbusplus::message::message& msg) {
281                 std::map<
282                     std::string,
283                     std::map<std::string, std::variant<std::string, uint32_t>>>
284                     interfaces;
285                 sdbusplus::message::object_path path;
286                 msg.read(path, interfaces);
287                 std::string csr;
288 
289                 for (auto& interface : interfaces)
290                 {
291                     if (interface.first == certAuthority)
292                     {
293                         for (const auto& property : interface.second)
294                         {
295                             if (property.first == "CSR")
296                             {
297                                 csr = std::get<std::string>(property.second);
298                                 auto fileHandle =
299                                     sdbusplus::message::object_path(path)
300                                         .filename();
301 
302                                 dbusToFileHandlers
303                                     .emplace_back(std::make_unique<
304                                                   pldm::requester::oem_ibm::
305                                                       DbusToFileHandler>(
306                                         hostSockFd, hostEid, dbusImplReqester,
307                                         path, handler))
308                                     ->newCsrFileAvailable(csr, fileHandle);
309                                 break;
310                             }
311                         }
312                         break;
313                     }
314                 }
315             });
316     }
317 
318     /** @brief Handler for readFileIntoMemory command
319      *
320      *  @param[in] request - pointer to PLDM request payload
321      *  @param[in] payloadLength - length of the message
322      *
323      *  @return PLDM response message
324      */
325     Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
326 
327     /** @brief Handler for writeFileIntoMemory command
328      *
329      *  @param[in] request - pointer to PLDM request payload
330      *  @param[in] payloadLength - length of the message
331      *
332      *  @return PLDM response message
333      */
334     Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
335 
336     /** @brief Handler for writeFileByTypeFromMemory command
337      *
338      *  @param[in] request - pointer to PLDM request payload
339      *  @param[in] payloadLength - length of the message
340      *
341      *  @return PLDM response message
342      */
343 
344     Response writeFileByTypeFromMemory(const pldm_msg* request,
345                                        size_t payloadLength);
346 
347     /** @brief Handler for readFileByTypeIntoMemory command
348      *
349      *  @param[in] request - pointer to PLDM request payload
350      *  @param[in] payloadLength - length of the message
351      *
352      *  @return PLDM response message
353      */
354     Response readFileByTypeIntoMemory(const pldm_msg* request,
355                                       size_t payloadLength);
356 
357     /** @brief Handler for writeFileByType command
358      *
359      *  @param[in] request - pointer to PLDM request payload
360      *  @param[in] payloadLength - length of the message
361      *
362      *  @return PLDM response message
363      */
364     Response readFileByType(const pldm_msg* request, size_t payloadLength);
365 
366     Response writeFileByType(const pldm_msg* request, size_t payloadLength);
367 
368     /** @brief Handler for GetFileTable command
369      *
370      *  @param[in] request - pointer to PLDM request payload
371      *  @param[in] payloadLength - length of the message payload
372      *
373      *  @return PLDM response message
374      */
375     Response getFileTable(const pldm_msg* request, size_t payloadLength);
376 
377     /** @brief Handler for readFile command
378      *
379      *  @param[in] request - PLDM request msg
380      *  @param[in] payloadLength - length of the message payload
381      *
382      *  @return PLDM response message
383      */
384     Response readFile(const pldm_msg* request, size_t payloadLength);
385 
386     /** @brief Handler for writeFile command
387      *
388      *  @param[in] request - PLDM request msg
389      *  @param[in] payloadLength - length of the message payload
390      *
391      *  @return PLDM response message
392      */
393     Response writeFile(const pldm_msg* request, size_t payloadLength);
394 
395     Response fileAck(const pldm_msg* request, size_t payloadLength);
396 
397     /** @brief Handler for getAlertStatus command
398      *
399      *  @param[in] request - PLDM request msg
400      *  @param[in] payloadLength - length of the message payload
401      *
402      *  @return PLDM response message
403      */
404     Response getAlertStatus(const pldm_msg* request, size_t payloadLength);
405 
406     /** @brief Handler for newFileAvailable command
407      *
408      *  @param[in] request - PLDM request msg
409      *  @param[in] payloadLength - length of the message payload
410      *
411      *  @return PLDM response message
412      */
413     Response newFileAvailable(const pldm_msg* request, size_t payloadLength);
414 
415   private:
416     oem_platform::Handler* oemPlatformHandler;
417     int hostSockFd;
418     uint8_t hostEid;
419     dbus_api::Requester* dbusImplReqester;
420     using DBusInterfaceAdded = std::vector<std::pair<
421         std::string,
422         std::vector<std::pair<std::string, std::variant<std::string>>>>>;
423     std::unique_ptr<pldm::requester::oem_ibm::DbusToFileHandler>
424         dbusToFileHandler; //!< pointer to send request to Host
425     std::unique_ptr<sdbusplus::bus::match::match>
426         resDumpMatcher; //!< Pointer to capture the interface added signal
427                         //!< for new resource dump
428     std::unique_ptr<sdbusplus::bus::match::match>
429         vmiCertMatcher; //!< Pointer to capture the interface added signal
430                         //!< for new csr string
431     /** @brief PLDM request handler */
432     pldm::requester::Handler<pldm::requester::Request>* handler;
433     std::vector<std::unique_ptr<pldm::requester::oem_ibm::DbusToFileHandler>>
434         dbusToFileHandlers;
435 };
436 
437 } // namespace oem_ibm
438 } // namespace responder
439 } // namespace pldm
440