blob: 3a504bf618370a5688a7b3f88311cc071f9f9c72 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#pragma once
2
Deepak Kodihallibc669f12019-11-28 08:52:07 -06003#include "handler.hpp"
George Liu83409572019-12-24 18:42:54 +08004#include "utils.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06005
Deepak Kodihalli15211b42019-12-14 02:24:49 -06006#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05307#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -06008#include <sys/stat.h>
9#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053010#include <unistd.h>
11
12#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060013#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050014#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053015
16#include "libpldm/base.h"
Deepak Kodihallida87ec62019-07-02 01:01:16 -050017#include "oem/ibm/libpldm/file_io.h"
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053018
19namespace pldm
20{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053021namespace responder
22{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023namespace dma
24{
25
26// The minimum data size of dma transfer in bytes
27constexpr uint32_t minSize = 16;
28
29// 16MB - 4096B (16773120 bytes) is the maximum data size of DMA transfer
30constexpr size_t maxSize = (16 * 1024 * 1024) - 4096;
31
32namespace fs = std::filesystem;
33
34/**
35 * @class DMA
36 *
37 * Expose API to initiate transfer of data by DMA
38 *
39 * This class only exposes the public API transferDataHost to transfer data
40 * between BMC and host using DMA. This allows for mocking the transferDataHost
41 * for unit testing purposes.
42 */
43class DMA
44{
45 public:
46 /** @brief API to transfer data between BMC and host using DMA
47 *
48 * @param[in] path - pathname of the file to transfer data from or to
49 * @param[in] offset - offset in the file
50 * @param[in] length - length of the data to transfer
51 * @param[in] address - DMA address on the host
52 * @param[in] upstream - indicates direction of the transfer; true indicates
53 * transfer to the host
54 *
55 * @return returns 0 on success, negative errno on failure
56 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060057 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053058 uint64_t address, bool upstream);
59};
60
61/** @brief Transfer the data between BMC and host using DMA.
62 *
63 * There is a max size for each DMA operation, transferAll API abstracts this
64 * and the requested length is broken down into multiple DMA operations if the
65 * length exceed max size.
66 *
67 * @tparam[in] T - DMA interface type
68 * @param[in] intf - interface passed to invoke DMA transfer
69 * @param[in] command - PLDM command
70 * @param[in] path - pathname of the file to transfer data from or to
71 * @param[in] offset - offset in the file
72 * @param[in] length - length of the data to transfer
73 * @param[in] address - DMA address on the host
74 * @param[in] upstream - indicates direction of the transfer; true indicates
75 * transfer to the host
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053076 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053077 * @return PLDM response message
78 */
79
80template <class DMAInterface>
81Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
82 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053083 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053084{
85 uint32_t origLength = length;
86 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
87 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
88
Deepak Kodihalli15211b42019-12-14 02:24:49 -060089 int flags{};
90 if (upstream)
91 {
92 flags = O_RDONLY;
93 }
94 else if (fs::exists(path))
95 {
96 flags = O_RDWR;
97 }
98 else
99 {
100 flags = O_WRONLY;
101 }
102 int file = open(path.string().c_str(), flags);
103 if (file == -1)
104 {
105 std::cerr << "File does not exist, path = " << path.string() << "\n";
106 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
107 responsePtr);
108 return response;
109 }
George Liu83409572019-12-24 18:42:54 +0800110 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600111
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530112 while (length > dma::maxSize)
113 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600114 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530115 upstream);
116 if (rc < 0)
117 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530118 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
119 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530120 return response;
121 }
122
123 offset += dma::maxSize;
124 length -= dma::maxSize;
125 address += dma::maxSize;
126 }
127
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600128 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530129 if (rc < 0)
130 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530131 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
132 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530133 return response;
134 }
135
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530136 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530137 responsePtr);
138 return response;
139}
140
141} // namespace dma
142
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600143namespace oem_ibm
144{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530145
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600146class Handler : public CmdHandler
147{
148 public:
149 Handler()
150 {
151 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
152 [this](const pldm_msg* request, size_t payloadLength) {
153 return this->readFileIntoMemory(request,
154 payloadLength);
155 });
156 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
157 [this](const pldm_msg* request, size_t payloadLength) {
158 return this->writeFileFromMemory(request,
159 payloadLength);
160 });
161 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
162 [this](const pldm_msg* request, size_t payloadLength) {
163 return this->writeFileByTypeFromMemory(
164 request, payloadLength);
165 });
166 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
167 [this](const pldm_msg* request, size_t payloadLength) {
168 return this->readFileByTypeIntoMemory(
169 request, payloadLength);
170 });
171 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
172 size_t payloadLength) {
173 return this->readFileByType(request, payloadLength);
174 });
175 handlers.emplace(PLDM_GET_FILE_TABLE,
176 [this](const pldm_msg* request, size_t payloadLength) {
177 return this->getFileTable(request, payloadLength);
178 });
179 handlers.emplace(PLDM_READ_FILE,
180 [this](const pldm_msg* request, size_t payloadLength) {
181 return this->readFile(request, payloadLength);
182 });
183 handlers.emplace(PLDM_WRITE_FILE,
184 [this](const pldm_msg* request, size_t payloadLength) {
185 return this->writeFile(request, payloadLength);
186 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600187 handlers.emplace(PLDM_FILE_ACK,
188 [this](const pldm_msg* request, size_t payloadLength) {
189 return this->fileAck(request, payloadLength);
190 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600191 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530192
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600193 /** @brief Handler for readFileIntoMemory command
194 *
195 * @param[in] request - pointer to PLDM request payload
196 * @param[in] payloadLength - length of the message
197 *
198 * @return PLDM response message
199 */
200 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500201
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600202 /** @brief Handler for writeFileIntoMemory command
203 *
204 * @param[in] request - pointer to PLDM request payload
205 * @param[in] payloadLength - length of the message
206 *
207 * @return PLDM response message
208 */
209 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500210
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600211 /** @brief Handler for writeFileByTypeFromMemory command
212 *
213 * @param[in] request - pointer to PLDM request payload
214 * @param[in] payloadLength - length of the message
215 *
216 * @return PLDM response message
217 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600218
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600219 Response writeFileByTypeFromMemory(const pldm_msg* request,
220 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600221
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600222 /** @brief Handler for readFileByTypeIntoMemory command
223 *
224 * @param[in] request - pointer to PLDM request payload
225 * @param[in] payloadLength - length of the message
226 *
227 * @return PLDM response message
228 */
229 Response readFileByTypeIntoMemory(const pldm_msg* request,
230 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500231
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600232 /** @brief Handler for readFileByType command
233 *
234 * @param[in] request - pointer to PLDM request payload
235 * @param[in] payloadLength - length of the message
236 *
237 * @return PLDM response message
238 */
239 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500240
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600241 /** @brief Handler for GetFileTable command
242 *
243 * @param[in] request - pointer to PLDM request payload
244 * @param[in] payloadLength - length of the message payload
245 *
246 * @return PLDM response message
247 */
248 Response getFileTable(const pldm_msg* request, size_t payloadLength);
249
250 /** @brief Handler for readFile command
251 *
252 * @param[in] request - PLDM request msg
253 * @param[in] payloadLength - length of the message payload
254 *
255 * @return PLDM response message
256 */
257 Response readFile(const pldm_msg* request, size_t payloadLength);
258
259 /** @brief Handler for writeFile command
260 *
261 * @param[in] request - PLDM request msg
262 * @param[in] payloadLength - length of the message payload
263 *
264 * @return PLDM response message
265 */
266 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600267
268 Response fileAck(const pldm_msg* request, size_t payloadLength);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600269};
270
271} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530272} // namespace responder
273} // namespace pldm