blob: 769e716f2a29fcdfe95efed7611369b9e144da62 [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
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05005#include "common/utils.hpp"
Jayashankar Padathdb124362021-01-28 21:12:34 -06006#include "oem/ibm/requester/dbus_to_file_handler.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -05007#include "oem_ibm_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05008#include "pldmd/handler.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05009#include "requester/handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060010
Deepak Kodihalli15211b42019-12-14 02:24:49 -060011#include <fcntl.h>
George Liuc453e162022-12-21 17:16:23 +080012#include <libpldm/base.h>
13#include <libpldm/file_io.h>
14#include <libpldm/host.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053015#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060016#include <sys/stat.h>
17#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053018#include <unistd.h>
19
20#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060021#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050022#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053024namespace pldm
25{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053026namespace responder
27{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053028namespace dma
29{
30
31// The minimum data size of dma transfer in bytes
32constexpr uint32_t minSize = 16;
33
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060034constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053035
36namespace 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 */
47class 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 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060061 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053062 uint64_t address, bool upstream);
Ravi Tejace1c96f2020-10-05 23:13:01 -050063
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);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053073};
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
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053090 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053091 * @return PLDM response message
92 */
93
94template <class DMAInterface>
95Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
96 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053097 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053098{
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
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600103 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 }
George Liu83409572019-12-24 18:42:54 +0800124 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600125
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530126 while (length > dma::maxSize)
127 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600128 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530129 upstream);
130 if (rc < 0)
131 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530132 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
133 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530134 return response;
135 }
136
137 offset += dma::maxSize;
138 length -= dma::maxSize;
139 address += dma::maxSize;
140 }
141
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600142 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530143 if (rc < 0)
144 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530145 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
146 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530147 return response;
148 }
149
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530150 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530151 responsePtr);
152 return response;
153}
154
155} // namespace dma
156
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600157namespace oem_ibm
158{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600159static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/resource/entry/";
160static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource";
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500161
162static constexpr auto certObjPath = "/xyz/openbmc_project/certs/ca/";
163static constexpr auto certAuthority =
164 "xyz.openbmc_project.PLDM.Provider.Certs.Authority.CSR";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600165class Handler : public CmdHandler
166{
167 public:
Jayashankar Padathdb124362021-01-28 21:12:34 -0600168 Handler(oem_platform::Handler* oemPlatformHandler, int hostSockFd,
Sampa Misrac0c79482021-06-02 08:01:54 -0500169 uint8_t hostEid, dbus_api::Requester* dbusImplReqester,
170 pldm::requester::Handler<pldm::requester::Request>* handler) :
Jayashankar Padathdb124362021-01-28 21:12:34 -0600171 oemPlatformHandler(oemPlatformHandler),
172 hostSockFd(hostSockFd), hostEid(hostEid),
Sampa Misrac0c79482021-06-02 08:01:54 -0500173 dbusImplReqester(dbusImplReqester), handler(handler)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600174 {
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 });
Sampa Misra18967162020-01-14 02:31:41 -0600199 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
200 [this](const pldm_msg* request, size_t payloadLength) {
201 return this->writeFileByType(request,
202 payloadLength);
203 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600204 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 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600216 handlers.emplace(PLDM_FILE_ACK,
217 [this](const pldm_msg* request, size_t payloadLength) {
218 return this->fileAck(request, payloadLength);
219 });
George Liu89aad712020-03-12 13:34:51 +0800220 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
221 [this](const pldm_msg* request, size_t payloadLength) {
222 return this->getAlertStatus(request,
223 payloadLength);
224 });
Sampa Misra18967162020-01-14 02:31:41 -0600225 handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
226 [this](const pldm_msg* request, size_t payloadLength) {
227 return this->newFileAvailable(request,
228 payloadLength);
229 });
Jayashankar Padathdb124362021-01-28 21:12:34 -0600230
Patrick Williams84b790c2022-07-22 19:26:56 -0500231 resDumpMatcher = std::make_unique<sdbusplus::bus::match_t>(
Jayashankar Padathdb124362021-01-28 21:12:34 -0600232 pldm::utils::DBusHandler::getBus(),
233 sdbusplus::bus::match::rules::interfacesAdded() +
234 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath),
Sampa Misrac0c79482021-06-02 08:01:54 -0500235 [this, hostSockFd, hostEid, dbusImplReqester,
Patrick Williams84b790c2022-07-22 19:26:56 -0500236 handler](sdbusplus::message_t& msg) {
Jayashankar Padathdb124362021-01-28 21:12:34 -0600237 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 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500263 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);
Jayashankar Padathdb124362021-01-28 21:12:34 -0600270 break;
271 }
272 }
273 });
Patrick Williams84b790c2022-07-22 19:26:56 -0500274 vmiCertMatcher = std::make_unique<sdbusplus::bus::match_t>(
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500275 pldm::utils::DBusHandler::getBus(),
276 sdbusplus::bus::match::rules::interfacesAdded() +
277 sdbusplus::bus::match::rules::argNpath(0, certObjPath),
Sampa Misrac0c79482021-06-02 08:01:54 -0500278 [this, hostSockFd, hostEid, dbusImplReqester,
Patrick Williams84b790c2022-07-22 19:26:56 -0500279 handler](sdbusplus::message_t& msg) {
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500280 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
Sampa Misrac0c79482021-06-02 08:01:54 -0500301 dbusToFileHandlers
302 .emplace_back(std::make_unique<
303 pldm::requester::oem_ibm::
304 DbusToFileHandler>(
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500305 hostSockFd, hostEid, dbusImplReqester,
Sampa Misrac0c79482021-06-02 08:01:54 -0500306 path, handler))
307 ->newCsrFileAvailable(csr, fileHandle);
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500308 break;
309 }
310 }
311 break;
312 }
313 }
314 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600315 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530316
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600317 /** @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);
Sampa Misra854e61f2019-08-22 04:36:47 -0500325
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600326 /** @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);
Sampa Misra854e61f2019-08-22 04:36:47 -0500334
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600335 /** @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 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600342
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600343 Response writeFileByTypeFromMemory(const pldm_msg* request,
344 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600345
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600346 /** @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);
vkaverap5b914c32019-06-30 22:23:54 -0500355
Sampa Misra18967162020-01-14 02:31:41 -0600356 /** @brief Handler for writeFileByType command
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600357 *
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);
vkaverap5b914c32019-06-30 22:23:54 -0500364
Sampa Misra18967162020-01-14 02:31:41 -0600365 Response writeFileByType(const pldm_msg* request, size_t payloadLength);
366
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600367 /** @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);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600393
394 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800395
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);
Sampa Misra18967162020-01-14 02:31:41 -0600404
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);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500413
414 private:
415 oem_platform::Handler* oemPlatformHandler;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600416 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>>>>>;
Sampa Misrac0c79482021-06-02 08:01:54 -0500422 std::unique_ptr<pldm::requester::oem_ibm::DbusToFileHandler>
423 dbusToFileHandler; //!< pointer to send request to Host
Patrick Williams84b790c2022-07-22 19:26:56 -0500424 std::unique_ptr<sdbusplus::bus::match_t>
Jayashankar Padathdb124362021-01-28 21:12:34 -0600425 resDumpMatcher; //!< Pointer to capture the interface added signal
426 //!< for new resource dump
Patrick Williams84b790c2022-07-22 19:26:56 -0500427 std::unique_ptr<sdbusplus::bus::match_t>
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500428 vmiCertMatcher; //!< Pointer to capture the interface added signal
429 //!< for new csr string
Sampa Misrac0c79482021-06-02 08:01:54 -0500430 /** @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;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600434};
435
436} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530437} // namespace responder
438} // namespace pldm