blob: 430811ec3a37696f4a9f3dd0a8fb1b8be577bf0c [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"
6#include "oem/ibm/libpldm/file_io.h"
7#include "oem/ibm/libpldm/host.h"
8
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"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060013
Deepak Kodihalli15211b42019-12-14 02:24:49 -060014#include <fcntl.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,
169 uint8_t hostEid, dbus_api::Requester* dbusImplReqester) :
170 oemPlatformHandler(oemPlatformHandler),
171 hostSockFd(hostSockFd), hostEid(hostEid),
172 dbusImplReqester(dbusImplReqester)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600173 {
174 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
175 [this](const pldm_msg* request, size_t payloadLength) {
176 return this->readFileIntoMemory(request,
177 payloadLength);
178 });
179 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
180 [this](const pldm_msg* request, size_t payloadLength) {
181 return this->writeFileFromMemory(request,
182 payloadLength);
183 });
184 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
185 [this](const pldm_msg* request, size_t payloadLength) {
186 return this->writeFileByTypeFromMemory(
187 request, payloadLength);
188 });
189 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
190 [this](const pldm_msg* request, size_t payloadLength) {
191 return this->readFileByTypeIntoMemory(
192 request, payloadLength);
193 });
194 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
195 size_t payloadLength) {
196 return this->readFileByType(request, payloadLength);
197 });
Sampa Misra18967162020-01-14 02:31:41 -0600198 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
199 [this](const pldm_msg* request, size_t payloadLength) {
200 return this->writeFileByType(request,
201 payloadLength);
202 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600203 handlers.emplace(PLDM_GET_FILE_TABLE,
204 [this](const pldm_msg* request, size_t payloadLength) {
205 return this->getFileTable(request, payloadLength);
206 });
207 handlers.emplace(PLDM_READ_FILE,
208 [this](const pldm_msg* request, size_t payloadLength) {
209 return this->readFile(request, payloadLength);
210 });
211 handlers.emplace(PLDM_WRITE_FILE,
212 [this](const pldm_msg* request, size_t payloadLength) {
213 return this->writeFile(request, payloadLength);
214 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600215 handlers.emplace(PLDM_FILE_ACK,
216 [this](const pldm_msg* request, size_t payloadLength) {
217 return this->fileAck(request, payloadLength);
218 });
George Liu89aad712020-03-12 13:34:51 +0800219 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
220 [this](const pldm_msg* request, size_t payloadLength) {
221 return this->getAlertStatus(request,
222 payloadLength);
223 });
Sampa Misra18967162020-01-14 02:31:41 -0600224 handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
225 [this](const pldm_msg* request, size_t payloadLength) {
226 return this->newFileAvailable(request,
227 payloadLength);
228 });
Jayashankar Padathdb124362021-01-28 21:12:34 -0600229
230 resDumpMatcher = std::make_unique<sdbusplus::bus::match::match>(
231 pldm::utils::DBusHandler::getBus(),
232 sdbusplus::bus::match::rules::interfacesAdded() +
233 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath),
234 [hostSockFd, hostEid,
235 dbusImplReqester](sdbusplus::message::message& msg) {
236 std::map<
237 std::string,
238 std::map<std::string, std::variant<std::string, uint32_t>>>
239 interfaces;
240 sdbusplus::message::object_path path;
241 msg.read(path, interfaces);
242 std::string vspstring;
243 std::string password;
244
245 for (auto& interface : interfaces)
246 {
247 if (interface.first == resDumpEntry)
248 {
249 for (const auto& property : interface.second)
250 {
251 if (property.first == "VSPString")
252 {
253 vspstring =
254 std::get<std::string>(property.second);
255 }
256 else if (property.first == "Password")
257 {
258 password =
259 std::get<std::string>(property.second);
260 }
261 }
262 auto dbusToFileHandler = std::make_unique<
263 pldm::requester::oem_ibm::DbusToFileHandler>(
264 hostSockFd, hostEid, dbusImplReqester, path);
265 dbusToFileHandler->processNewResourceDump(vspstring,
266 password);
267 break;
268 }
269 }
270 });
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500271 vmiCertMatcher = std::make_unique<sdbusplus::bus::match::match>(
272 pldm::utils::DBusHandler::getBus(),
273 sdbusplus::bus::match::rules::interfacesAdded() +
274 sdbusplus::bus::match::rules::argNpath(0, certObjPath),
275 [hostSockFd, hostEid,
276 dbusImplReqester](sdbusplus::message::message& msg) {
277 std::map<
278 std::string,
279 std::map<std::string, std::variant<std::string, uint32_t>>>
280 interfaces;
281 sdbusplus::message::object_path path;
282 msg.read(path, interfaces);
283 std::string csr;
284
285 for (auto& interface : interfaces)
286 {
287 if (interface.first == certAuthority)
288 {
289 for (const auto& property : interface.second)
290 {
291 if (property.first == "CSR")
292 {
293 csr = std::get<std::string>(property.second);
294 auto fileHandle =
295 sdbusplus::message::object_path(path)
296 .filename();
297
298 auto dbusToFileHandler =
299 std::make_unique<pldm::requester::oem_ibm::
300 DbusToFileHandler>(
301 hostSockFd, hostEid, dbusImplReqester,
302 path);
303 dbusToFileHandler->newCsrFileAvailable(
304 csr, fileHandle);
305 break;
306 }
307 }
308 break;
309 }
310 }
311 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600312 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530313
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600314 /** @brief Handler for readFileIntoMemory command
315 *
316 * @param[in] request - pointer to PLDM request payload
317 * @param[in] payloadLength - length of the message
318 *
319 * @return PLDM response message
320 */
321 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500322
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600323 /** @brief Handler for writeFileIntoMemory command
324 *
325 * @param[in] request - pointer to PLDM request payload
326 * @param[in] payloadLength - length of the message
327 *
328 * @return PLDM response message
329 */
330 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500331
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600332 /** @brief Handler for writeFileByTypeFromMemory command
333 *
334 * @param[in] request - pointer to PLDM request payload
335 * @param[in] payloadLength - length of the message
336 *
337 * @return PLDM response message
338 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600339
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600340 Response writeFileByTypeFromMemory(const pldm_msg* request,
341 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600342
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600343 /** @brief Handler for readFileByTypeIntoMemory 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 Response readFileByTypeIntoMemory(const pldm_msg* request,
351 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500352
Sampa Misra18967162020-01-14 02:31:41 -0600353 /** @brief Handler for writeFileByType command
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600354 *
355 * @param[in] request - pointer to PLDM request payload
356 * @param[in] payloadLength - length of the message
357 *
358 * @return PLDM response message
359 */
360 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500361
Sampa Misra18967162020-01-14 02:31:41 -0600362 Response writeFileByType(const pldm_msg* request, size_t payloadLength);
363
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600364 /** @brief Handler for GetFileTable command
365 *
366 * @param[in] request - pointer to PLDM request payload
367 * @param[in] payloadLength - length of the message payload
368 *
369 * @return PLDM response message
370 */
371 Response getFileTable(const pldm_msg* request, size_t payloadLength);
372
373 /** @brief Handler for readFile command
374 *
375 * @param[in] request - PLDM request msg
376 * @param[in] payloadLength - length of the message payload
377 *
378 * @return PLDM response message
379 */
380 Response readFile(const pldm_msg* request, size_t payloadLength);
381
382 /** @brief Handler for writeFile command
383 *
384 * @param[in] request - PLDM request msg
385 * @param[in] payloadLength - length of the message payload
386 *
387 * @return PLDM response message
388 */
389 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600390
391 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800392
393 /** @brief Handler for getAlertStatus 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 getAlertStatus(const pldm_msg* request, size_t payloadLength);
Sampa Misra18967162020-01-14 02:31:41 -0600401
402 /** @brief Handler for newFileAvailable command
403 *
404 * @param[in] request - PLDM request msg
405 * @param[in] payloadLength - length of the message payload
406 *
407 * @return PLDM response message
408 */
409 Response newFileAvailable(const pldm_msg* request, size_t payloadLength);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500410
411 private:
412 oem_platform::Handler* oemPlatformHandler;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600413 int hostSockFd;
414 uint8_t hostEid;
415 dbus_api::Requester* dbusImplReqester;
416 using DBusInterfaceAdded = std::vector<std::pair<
417 std::string,
418 std::vector<std::pair<std::string, std::variant<std::string>>>>>;
419 std::unique_ptr<sdbusplus::bus::match::match>
420 resDumpMatcher; //!< Pointer to capture the interface added signal
421 //!< for new resource dump
Varsha Kaverappa219ace92021-04-01 02:50:11 -0500422 std::unique_ptr<sdbusplus::bus::match::match>
423 vmiCertMatcher; //!< Pointer to capture the interface added signal
424 //!< for new csr string
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600425};
426
427} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530428} // namespace responder
429} // namespace pldm