blob: 4c74463c98cd2bcf8aabaf831e890776f8f3abc9 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#pragma once
2
Deepak Kodihalli4c164b02020-03-07 03:23:31 -06003#include "config.h"
4
George Liu6492f522020-06-16 10:34:05 +08005#include "libpldm/base.h"
Andrew Jeffery27a022c2022-08-10 23:12:49 +09306#include "libpldm/file_io.h"
7#include "libpldm/host.h"
George Liu6492f522020-06-16 10:34:05 +08008
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05009#include "common/utils.hpp"
Jayashankar Padathdb124362021-01-28 21:12:34 -060010#include "oem/ibm/requester/dbus_to_file_handler.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050011#include "oem_ibm_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050012#include "pldmd/handler.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -050013#include "requester/handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060014
Deepak Kodihalli15211b42019-12-14 02:24:49 -060015#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053016#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060017#include <sys/stat.h>
18#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053019#include <unistd.h>
20
21#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060022#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050023#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053024
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053025namespace pldm
26{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053027namespace responder
28{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053029namespace dma
30{
31
32// The minimum data size of dma transfer in bytes
33constexpr uint32_t minSize = 16;
34
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060035constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053036
37namespace 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 */
48class 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 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060062 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053063 uint64_t address, bool upstream);
Ravi Tejace1c96f2020-10-05 23:13:01 -050064
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);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053074};
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
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053091 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053092 * @return PLDM response message
93 */
94
95template <class DMAInterface>
96Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
97 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053098 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053099{
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
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600104 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 }
George Liu83409572019-12-24 18:42:54 +0800125 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600126
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530127 while (length > dma::maxSize)
128 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600129 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530130 upstream);
131 if (rc < 0)
132 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530133 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
134 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530135 return response;
136 }
137
138 offset += dma::maxSize;
139 length -= dma::maxSize;
140 address += dma::maxSize;
141 }
142
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600143 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530144 if (rc < 0)
145 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530146 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
147 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530148 return response;
149 }
150
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530151 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530152 responsePtr);
153 return response;
154}
155
156} // namespace dma
157
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600158namespace oem_ibm
159{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600160static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/resource/entry/";
161static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource";
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500162
163static constexpr auto certObjPath = "/xyz/openbmc_project/certs/ca/";
164static constexpr auto certAuthority =
165 "xyz.openbmc_project.PLDM.Provider.Certs.Authority.CSR";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600166class Handler : public CmdHandler
167{
168 public:
Jayashankar Padathdb124362021-01-28 21:12:34 -0600169 Handler(oem_platform::Handler* oemPlatformHandler, int hostSockFd,
Sampa Misrac0c79482021-06-02 08:01:54 -0500170 uint8_t hostEid, dbus_api::Requester* dbusImplReqester,
171 pldm::requester::Handler<pldm::requester::Request>* handler) :
Jayashankar Padathdb124362021-01-28 21:12:34 -0600172 oemPlatformHandler(oemPlatformHandler),
173 hostSockFd(hostSockFd), hostEid(hostEid),
Sampa Misrac0c79482021-06-02 08:01:54 -0500174 dbusImplReqester(dbusImplReqester), handler(handler)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600175 {
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 });
Sampa Misra18967162020-01-14 02:31:41 -0600200 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
201 [this](const pldm_msg* request, size_t payloadLength) {
202 return this->writeFileByType(request,
203 payloadLength);
204 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600205 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 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600217 handlers.emplace(PLDM_FILE_ACK,
218 [this](const pldm_msg* request, size_t payloadLength) {
219 return this->fileAck(request, payloadLength);
220 });
George Liu89aad712020-03-12 13:34:51 +0800221 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
222 [this](const pldm_msg* request, size_t payloadLength) {
223 return this->getAlertStatus(request,
224 payloadLength);
225 });
Sampa Misra18967162020-01-14 02:31:41 -0600226 handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
227 [this](const pldm_msg* request, size_t payloadLength) {
228 return this->newFileAvailable(request,
229 payloadLength);
230 });
Jayashankar Padathdb124362021-01-28 21:12:34 -0600231
Patrick Williams84b790c2022-07-22 19:26:56 -0500232 resDumpMatcher = std::make_unique<sdbusplus::bus::match_t>(
Jayashankar Padathdb124362021-01-28 21:12:34 -0600233 pldm::utils::DBusHandler::getBus(),
234 sdbusplus::bus::match::rules::interfacesAdded() +
235 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath),
Sampa Misrac0c79482021-06-02 08:01:54 -0500236 [this, hostSockFd, hostEid, dbusImplReqester,
Patrick Williams84b790c2022-07-22 19:26:56 -0500237 handler](sdbusplus::message_t& msg) {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600238 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 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500264 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);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600271 break;
272 }
273 }
274 });
Patrick Williams84b790c2022-07-22 19:26:56 -0500275 vmiCertMatcher = std::make_unique<sdbusplus::bus::match_t>(
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500276 pldm::utils::DBusHandler::getBus(),
277 sdbusplus::bus::match::rules::interfacesAdded() +
278 sdbusplus::bus::match::rules::argNpath(0, certObjPath),
Sampa Misrac0c79482021-06-02 08:01:54 -0500279 [this, hostSockFd, hostEid, dbusImplReqester,
Patrick Williams84b790c2022-07-22 19:26:56 -0500280 handler](sdbusplus::message_t& msg) {
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500281 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
Sampa Misrac0c79482021-06-02 08:01:54 -0500302 dbusToFileHandlers
303 .emplace_back(std::make_unique<
304 pldm::requester::oem_ibm::
305 DbusToFileHandler>(
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500306 hostSockFd, hostEid, dbusImplReqester,
Sampa Misrac0c79482021-06-02 08:01:54 -0500307 path, handler))
308 ->newCsrFileAvailable(csr, fileHandle);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500309 break;
310 }
311 }
312 break;
313 }
314 }
315 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600316 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530317
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600318 /** @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);
Sampa Misra854e61f2019-08-22 04:36:47 -0500326
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600327 /** @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);
Sampa Misra854e61f2019-08-22 04:36:47 -0500335
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600336 /** @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 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600343
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600344 Response writeFileByTypeFromMemory(const pldm_msg* request,
345 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600346
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600347 /** @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);
vkaverap5b914c32019-06-30 22:23:54 -0500356
Sampa Misra18967162020-01-14 02:31:41 -0600357 /** @brief Handler for writeFileByType command
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600358 *
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);
vkaverap5b914c32019-06-30 22:23:54 -0500365
Sampa Misra18967162020-01-14 02:31:41 -0600366 Response writeFileByType(const pldm_msg* request, size_t payloadLength);
367
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600368 /** @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);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600394
395 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800396
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);
Sampa Misra18967162020-01-14 02:31:41 -0600405
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);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500414
415 private:
416 oem_platform::Handler* oemPlatformHandler;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600417 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>>>>>;
Sampa Misrac0c79482021-06-02 08:01:54 -0500423 std::unique_ptr<pldm::requester::oem_ibm::DbusToFileHandler>
424 dbusToFileHandler; //!< pointer to send request to Host
Patrick Williams84b790c2022-07-22 19:26:56 -0500425 std::unique_ptr<sdbusplus::bus::match_t>
Jayashankar Padathdb124362021-01-28 21:12:34 -0600426 resDumpMatcher; //!< Pointer to capture the interface added signal
427 //!< for new resource dump
Patrick Williams84b790c2022-07-22 19:26:56 -0500428 std::unique_ptr<sdbusplus::bus::match_t>
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500429 vmiCertMatcher; //!< Pointer to capture the interface added signal
430 //!< for new csr string
Sampa Misrac0c79482021-06-02 08:01:54 -0500431 /** @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;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600435};
436
437} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530438} // namespace responder
439} // namespace pldm