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