blob: 4e0cad4faa2c21c583a28cc7923bc936ccf86431 [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 Kodihallibc669f12019-11-28 08:52:07 -06005#include "handler.hpp"
George Liu83409572019-12-24 18:42:54 +08006#include "utils.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06007
Deepak Kodihalli15211b42019-12-14 02:24:49 -06008#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05309#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060010#include <sys/stat.h>
11#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053012#include <unistd.h>
13
14#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060015#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050016#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053017
18#include "libpldm/base.h"
Deepak Kodihallida87ec62019-07-02 01:01:16 -050019#include "oem/ibm/libpldm/file_io.h"
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053020
21namespace pldm
22{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023namespace responder
24{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053025namespace dma
26{
27
28// The minimum data size of dma transfer in bytes
29constexpr uint32_t minSize = 16;
30
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060031constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053032
33namespace fs = std::filesystem;
34
35/**
36 * @class DMA
37 *
38 * Expose API to initiate transfer of data by DMA
39 *
40 * This class only exposes the public API transferDataHost to transfer data
41 * between BMC and host using DMA. This allows for mocking the transferDataHost
42 * for unit testing purposes.
43 */
44class DMA
45{
46 public:
47 /** @brief API to transfer data between BMC and host using DMA
48 *
49 * @param[in] path - pathname of the file to transfer data from or to
50 * @param[in] offset - offset in the file
51 * @param[in] length - length of the data to transfer
52 * @param[in] address - DMA address on the host
53 * @param[in] upstream - indicates direction of the transfer; true indicates
54 * transfer to the host
55 *
56 * @return returns 0 on success, negative errno on failure
57 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060058 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053059 uint64_t address, bool upstream);
60};
61
62/** @brief Transfer the data between BMC and host using DMA.
63 *
64 * There is a max size for each DMA operation, transferAll API abstracts this
65 * and the requested length is broken down into multiple DMA operations if the
66 * length exceed max size.
67 *
68 * @tparam[in] T - DMA interface type
69 * @param[in] intf - interface passed to invoke DMA transfer
70 * @param[in] command - PLDM command
71 * @param[in] path - pathname of the file to transfer data from or to
72 * @param[in] offset - offset in the file
73 * @param[in] length - length of the data to transfer
74 * @param[in] address - DMA address on the host
75 * @param[in] upstream - indicates direction of the transfer; true indicates
76 * transfer to the host
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053077 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053078 * @return PLDM response message
79 */
80
81template <class DMAInterface>
82Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
83 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053084 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053085{
86 uint32_t origLength = length;
87 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
88 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
89
Deepak Kodihalli15211b42019-12-14 02:24:49 -060090 int flags{};
91 if (upstream)
92 {
93 flags = O_RDONLY;
94 }
95 else if (fs::exists(path))
96 {
97 flags = O_RDWR;
98 }
99 else
100 {
101 flags = O_WRONLY;
102 }
103 int file = open(path.string().c_str(), flags);
104 if (file == -1)
105 {
106 std::cerr << "File does not exist, path = " << path.string() << "\n";
107 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
108 responsePtr);
109 return response;
110 }
George Liu83409572019-12-24 18:42:54 +0800111 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600112
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530113 while (length > dma::maxSize)
114 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600115 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530116 upstream);
117 if (rc < 0)
118 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530119 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
120 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530121 return response;
122 }
123
124 offset += dma::maxSize;
125 length -= dma::maxSize;
126 address += dma::maxSize;
127 }
128
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600129 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530130 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
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530137 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530138 responsePtr);
139 return response;
140}
141
142} // namespace dma
143
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600144namespace oem_ibm
145{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530146
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600147class Handler : public CmdHandler
148{
149 public:
150 Handler()
151 {
152 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
153 [this](const pldm_msg* request, size_t payloadLength) {
154 return this->readFileIntoMemory(request,
155 payloadLength);
156 });
157 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
158 [this](const pldm_msg* request, size_t payloadLength) {
159 return this->writeFileFromMemory(request,
160 payloadLength);
161 });
162 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
163 [this](const pldm_msg* request, size_t payloadLength) {
164 return this->writeFileByTypeFromMemory(
165 request, payloadLength);
166 });
167 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
168 [this](const pldm_msg* request, size_t payloadLength) {
169 return this->readFileByTypeIntoMemory(
170 request, payloadLength);
171 });
172 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
173 size_t payloadLength) {
174 return this->readFileByType(request, payloadLength);
175 });
176 handlers.emplace(PLDM_GET_FILE_TABLE,
177 [this](const pldm_msg* request, size_t payloadLength) {
178 return this->getFileTable(request, payloadLength);
179 });
180 handlers.emplace(PLDM_READ_FILE,
181 [this](const pldm_msg* request, size_t payloadLength) {
182 return this->readFile(request, payloadLength);
183 });
184 handlers.emplace(PLDM_WRITE_FILE,
185 [this](const pldm_msg* request, size_t payloadLength) {
186 return this->writeFile(request, payloadLength);
187 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600188 handlers.emplace(PLDM_FILE_ACK,
189 [this](const pldm_msg* request, size_t payloadLength) {
190 return this->fileAck(request, payloadLength);
191 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600192 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530193
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600194 /** @brief Handler for readFileIntoMemory command
195 *
196 * @param[in] request - pointer to PLDM request payload
197 * @param[in] payloadLength - length of the message
198 *
199 * @return PLDM response message
200 */
201 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500202
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600203 /** @brief Handler for writeFileIntoMemory command
204 *
205 * @param[in] request - pointer to PLDM request payload
206 * @param[in] payloadLength - length of the message
207 *
208 * @return PLDM response message
209 */
210 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500211
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600212 /** @brief Handler for writeFileByTypeFromMemory command
213 *
214 * @param[in] request - pointer to PLDM request payload
215 * @param[in] payloadLength - length of the message
216 *
217 * @return PLDM response message
218 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600219
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600220 Response writeFileByTypeFromMemory(const pldm_msg* request,
221 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600222
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600223 /** @brief Handler for readFileByTypeIntoMemory command
224 *
225 * @param[in] request - pointer to PLDM request payload
226 * @param[in] payloadLength - length of the message
227 *
228 * @return PLDM response message
229 */
230 Response readFileByTypeIntoMemory(const pldm_msg* request,
231 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500232
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600233 /** @brief Handler for readFileByType command
234 *
235 * @param[in] request - pointer to PLDM request payload
236 * @param[in] payloadLength - length of the message
237 *
238 * @return PLDM response message
239 */
240 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500241
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600242 /** @brief Handler for GetFileTable command
243 *
244 * @param[in] request - pointer to PLDM request payload
245 * @param[in] payloadLength - length of the message payload
246 *
247 * @return PLDM response message
248 */
249 Response getFileTable(const pldm_msg* request, size_t payloadLength);
250
251 /** @brief Handler for readFile command
252 *
253 * @param[in] request - PLDM request msg
254 * @param[in] payloadLength - length of the message payload
255 *
256 * @return PLDM response message
257 */
258 Response readFile(const pldm_msg* request, size_t payloadLength);
259
260 /** @brief Handler for writeFile command
261 *
262 * @param[in] request - PLDM request msg
263 * @param[in] payloadLength - length of the message payload
264 *
265 * @return PLDM response message
266 */
267 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600268
269 Response fileAck(const pldm_msg* request, size_t payloadLength);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600270};
271
272} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530273} // namespace responder
274} // namespace pldm