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