blob: adce4ec53cffc44933daef1751cab84acb0bbef3 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#include "file_io.hpp"
2
Sampa Misra854e61f2019-08-22 04:36:47 -05003#include "file_io_by_type.hpp"
Tom Joseph0c6d22c2019-06-26 09:58:41 +05304#include "file_table.hpp"
Ravi Tejace1c96f2020-10-05 23:13:01 -05005#include "utils.hpp"
Sampa Misra854e61f2019-08-22 04:36:47 -05006#include "xyz/openbmc_project/Common/error.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05007
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05308#include <fcntl.h>
George Liuc453e162022-12-21 17:16:23 +08009#include <libpldm/base.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053010#include <sys/mman.h>
11#include <sys/stat.h>
12#include <sys/types.h>
13#include <unistd.h>
14
Riya Dixit49cfb132023-03-02 04:26:53 -060015#include <phosphor-logging/lg2.hpp>
16
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053017#include <cstring>
18#include <fstream>
Sampa Misra854e61f2019-08-22 04:36:47 -050019#include <memory>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053020
Riya Dixit49cfb132023-03-02 04:26:53 -060021PHOSPHOR_LOG2_USING;
22
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023namespace pldm
24{
Ravi Tejace1c96f2020-10-05 23:13:01 -050025using namespace pldm::responder::utils;
Sampa Misra854e61f2019-08-22 04:36:47 -050026using namespace sdbusplus::xyz::openbmc_project::Common::Error;
27
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053028namespace responder
29{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053030namespace fs = std::filesystem;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053031
32namespace dma
33{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053034/** @struct AspeedXdmaOp
35 *
36 * Structure representing XDMA operation
37 */
38struct AspeedXdmaOp
39{
40 uint64_t hostAddr; //!< the DMA address on the host side, configured by
41 //!< PCI subsystem.
42 uint32_t len; //!< the size of the transfer in bytes, it should be a
43 //!< multiple of 16 bytes
44 uint32_t upstream; //!< boolean indicating the direction of the DMA
45 //!< operation, true means a transfer from BMC to host.
46};
47
Deepak Kodihalli134cc1b2019-10-04 01:51:02 -050048constexpr auto xdmaDev = "/dev/aspeed-xdma";
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053049
Ravi Tejace1c96f2020-10-05 23:13:01 -050050int DMA::transferHostDataToSocket(int fd, uint32_t length, uint64_t address)
51{
52 static const size_t pageSize = getpagesize();
53 uint32_t numPages = length / pageSize;
54 uint32_t pageAlignedLength = numPages * pageSize;
55
56 if (length > pageAlignedLength)
57 {
58 pageAlignedLength += pageSize;
59 }
60
61 auto mmapCleanup = [pageAlignedLength](void* vgaMem) {
62 munmap(vgaMem, pageAlignedLength);
63 };
64
65 int dmaFd = -1;
66 int rc = 0;
67 dmaFd = open(xdmaDev, O_RDWR);
68 if (dmaFd < 0)
69 {
70 rc = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -060071 error(
Riya Dixitfc84f632024-04-06 14:00:02 -050072 "Failed to open the XDMA device for transferring remote terminus data to socket with response code '{RC}'",
Riya Dixit49cfb132023-03-02 04:26:53 -060073 "RC", rc);
Ravi Tejace1c96f2020-10-05 23:13:01 -050074 return rc;
75 }
76
77 pldm::utils::CustomFD xdmaFd(dmaFd);
78
79 void* vgaMem;
Patrick Williams6da4f912023-05-10 07:50:53 -050080 vgaMem = mmap(nullptr, pageAlignedLength, PROT_READ, MAP_SHARED, xdmaFd(),
81 0);
Ravi Tejace1c96f2020-10-05 23:13:01 -050082 if (MAP_FAILED == vgaMem)
83 {
84 rc = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -060085 error(
Riya Dixitfc84f632024-04-06 14:00:02 -050086 "Failed to mmap the XDMA device for transferring remote terminus data to socket with response code '{RC}'",
Riya Dixit49cfb132023-03-02 04:26:53 -060087 "RC", rc);
Ravi Tejace1c96f2020-10-05 23:13:01 -050088 return rc;
89 }
90
91 std::unique_ptr<void, decltype(mmapCleanup)> vgaMemPtr(vgaMem, mmapCleanup);
92
93 AspeedXdmaOp xdmaOp;
94 xdmaOp.upstream = 0;
95 xdmaOp.hostAddr = address;
96 xdmaOp.len = length;
97
98 rc = write(xdmaFd(), &xdmaOp, sizeof(xdmaOp));
99 if (rc < 0)
100 {
101 rc = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600102 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500103 "Failed to execute the DMA operation for transfering remote terminus data to socket at address '{ADDRESS}' and length '{LENGTH}' with response code '{RC}'",
104 "RC", rc, "ADDRESS", address, "LENGTH", length);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500105 return rc;
106 }
107
108 rc = writeToUnixSocket(fd, static_cast<const char*>(vgaMemPtr.get()),
109 length);
110 if (rc < 0)
111 {
112 rc = -errno;
113 close(fd);
Riya Dixit49cfb132023-03-02 04:26:53 -0600114 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500115 "Failed to write to Unix socket, closing socket for transfering remote terminus data to socket with response code '{RC}'",
Riya Dixit49cfb132023-03-02 04:26:53 -0600116 "RC", rc);
Ravi Tejace1c96f2020-10-05 23:13:01 -0500117 return rc;
118 }
119 return 0;
120}
121
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600122int DMA::transferDataHost(int fd, uint32_t offset, uint32_t length,
123 uint64_t address, bool upstream)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530124{
125 static const size_t pageSize = getpagesize();
126 uint32_t numPages = length / pageSize;
127 uint32_t pageAlignedLength = numPages * pageSize;
128
129 if (length > pageAlignedLength)
130 {
131 pageAlignedLength += pageSize;
132 }
133
ArchanaKakani93409752022-04-19 00:36:04 -0500134 int rc = 0;
135 auto mmapCleanup = [pageAlignedLength, &rc](void* vgaMem) {
136 if (rc != -EINTR)
137 {
138 munmap(vgaMem, pageAlignedLength);
139 }
140 else
141 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600142 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500143 "Received interrupt during DMA transfer for data between BMC and remote terminus. Skipping Unmap.");
ArchanaKakani93409752022-04-19 00:36:04 -0500144 }
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530145 };
146
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600147 int dmaFd = -1;
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600148 dmaFd = open(xdmaDev, O_RDWR);
149 if (dmaFd < 0)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530150 {
151 rc = -errno;
Riya Dixitfc84f632024-04-06 14:00:02 -0500152 error(
153 "Failed to open the XDMA device for data transfer between BMC and remote terminus with response code '{RC}'",
154 "RC", rc);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530155 return rc;
156 }
157
George Liu83409572019-12-24 18:42:54 +0800158 pldm::utils::CustomFD xdmaFd(dmaFd);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530159
160 void* vgaMem;
161 vgaMem = mmap(nullptr, pageAlignedLength, upstream ? PROT_WRITE : PROT_READ,
162 MAP_SHARED, xdmaFd(), 0);
163 if (MAP_FAILED == vgaMem)
164 {
165 rc = -errno;
Riya Dixitfc84f632024-04-06 14:00:02 -0500166 error(
167 "Failed to mmap the XDMA device for data transfer between BMC and remote terminus with response code '{RC}'",
168 "RC", rc);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530169 return rc;
170 }
171
172 std::unique_ptr<void, decltype(mmapCleanup)> vgaMemPtr(vgaMem, mmapCleanup);
173
174 if (upstream)
175 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600176 rc = lseek(fd, offset, SEEK_SET);
177 if (rc == -1)
178 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600179 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500180 "Failed to transfer data between BMC and remote terminus due to lseek failure with upstream '{UPSTREAM}' at offset '{OFFSET}', error number - {ERROR_NUM}",
181 "ERROR_NUM", errno, "UPSTREAM", upstream, "OFFSET", offset);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600182 return rc;
183 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530184
185 // Writing to the VGA memory should be aligned at page boundary,
186 // otherwise write data into a buffer aligned at page boundary and
187 // then write to the VGA memory.
188 std::vector<char> buffer{};
189 buffer.resize(pageAlignedLength);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600190 rc = read(fd, buffer.data(), length);
191 if (rc == -1)
192 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600193 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500194 "Failed to transfer data between BMC and remote terminus with file read on upstream '{UPSTREAM}' of length '{LENGTH}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}",
195 "ERROR_NUM", errno, "UPSTREAM", upstream, "LENGTH", length,
196 "OFFSET", offset);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600197 return rc;
198 }
199 if (rc != static_cast<int>(length))
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530200 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600201 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500202 "Failed to transfer data between BMC and remote terminus mismatched for number of characters to read on upstream '{UPSTREAM}' and the length '{LENGTH}' read and count '{RC}'",
203 "UPSTREAM", upstream, "LENGTH", length, "RC", rc);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530204 return -1;
205 }
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600206 memcpy(static_cast<char*>(vgaMemPtr.get()), buffer.data(),
207 pageAlignedLength);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530208 }
209
210 AspeedXdmaOp xdmaOp;
211 xdmaOp.upstream = upstream ? 1 : 0;
212 xdmaOp.hostAddr = address;
213 xdmaOp.len = length;
214
215 rc = write(xdmaFd(), &xdmaOp, sizeof(xdmaOp));
216 if (rc < 0)
217 {
218 rc = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600219 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500220 "Failed to execute the DMA operation on data between BMC and remote terminus for upstream '{UPSTREAM}' of length '{LENGTH}' at address '{ADDRESS}', response code '{RC}'",
221 "RC", rc, "UPSTREAM", upstream, "ADDRESS", address, "LENGTH",
222 length);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530223 return rc;
224 }
225
226 if (!upstream)
227 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600228 rc = lseek(fd, offset, SEEK_SET);
229 if (rc == -1)
Sampa Misra854e61f2019-08-22 04:36:47 -0500230 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600231 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500232 "Failed to transfer data between BMC and remote terminus due to lseek failure '{UPSTREAM}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}",
233 "ERROR_NUM", errno, "UPSTREAM", upstream, "OFFSET", offset);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600234 return rc;
Sampa Misra854e61f2019-08-22 04:36:47 -0500235 }
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600236 rc = write(fd, static_cast<const char*>(vgaMemPtr.get()), length);
237 if (rc == -1)
238 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600239 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500240 "Failed to transfer data between BMC and remote terminus where file write upstream '{UPSTREAM}' of length '{LENGTH}' at offset '{OFFSET}' failed, error number - {ERROR_NUM}",
241 "ERROR_NUM", errno, "UPSTREAM", upstream, "LENGTH", length,
242 "OFFSET", offset);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600243 return rc;
244 }
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530245 }
246
247 return 0;
248}
249
250} // namespace dma
251
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600252namespace oem_ibm
253{
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600254Response Handler::readFileIntoMemory(const pldm_msg* request,
255 size_t payloadLength)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530256{
257 uint32_t fileHandle = 0;
258 uint32_t offset = 0;
259 uint32_t length = 0;
260 uint64_t address = 0;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530261
262 Response response((sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES), 0);
263 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
264
265 if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES)
266 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530267 encode_rw_file_memory_resp(request->hdr.instance_id,
268 PLDM_READ_FILE_INTO_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530269 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
270 return response;
271 }
272
Zahed Hossain223a73d2019-07-04 12:46:18 -0500273 decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset,
274 &length, &address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530275
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530276 using namespace pldm::filetable;
277 auto& table = buildFileTable(FILE_TABLE_JSON);
278 FileEntry value{};
279
280 try
281 {
282 value = table.at(fileHandle);
283 }
Patrick Williams51330582021-10-06 12:48:56 -0500284 catch (const std::exception& e)
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530285 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500286 error(
287 "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}",
288 "HANDLE", fileHandle, "ERROR", e);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530289 encode_rw_file_memory_resp(request->hdr.instance_id,
290 PLDM_READ_FILE_INTO_MEMORY,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530291 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
292 return response;
293 }
294
295 if (!fs::exists(value.fsPath))
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530296 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500297 error("File '{PATH}' and handle '{FILE_HANDLE}' with does not exist",
298 "PATH", value.fsPath, "FILE_HANDLE", fileHandle);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530299 encode_rw_file_memory_resp(request->hdr.instance_id,
300 PLDM_READ_FILE_INTO_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530301 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
302 return response;
303 }
304
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530305 auto fileSize = fs::file_size(value.fsPath);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530306 if (offset >= fileSize)
307 {
Sagar Srinivas82257922023-07-10 08:12:22 -0500308 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500309 "Offset '{OFFSET}' exceeds file size '{SIZE}' and file handle '{FILE_HANDLE}'",
310 "OFFSET", offset, "SIZE", fileSize, "FILE_HANDLE", fileHandle);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530311 encode_rw_file_memory_resp(request->hdr.instance_id,
312 PLDM_READ_FILE_INTO_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530313 PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
314 return response;
315 }
316
317 if (offset + length > fileSize)
318 {
319 length = fileSize - offset;
320 }
321
322 if (length % dma::minSize)
323 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500324 error("Packet length '{LENGTH}' is non multiple of minimum DMA size",
325 "LENGTH", length);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530326 encode_rw_file_memory_resp(request->hdr.instance_id,
327 PLDM_READ_FILE_INTO_MEMORY,
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500328 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530329 return response;
330 }
331
332 using namespace dma;
333 DMA intf;
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530334 return transferAll<DMA>(&intf, PLDM_READ_FILE_INTO_MEMORY, value.fsPath,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530335 offset, length, address, true,
336 request->hdr.instance_id);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530337}
338
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600339Response Handler::writeFileFromMemory(const pldm_msg* request,
340 size_t payloadLength)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530341{
342 uint32_t fileHandle = 0;
343 uint32_t offset = 0;
344 uint32_t length = 0;
345 uint64_t address = 0;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530346
347 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
348 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
349
350 if (payloadLength != PLDM_RW_FILE_MEM_REQ_BYTES)
351 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530352 encode_rw_file_memory_resp(request->hdr.instance_id,
353 PLDM_WRITE_FILE_FROM_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530354 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
355 return response;
356 }
357
Zahed Hossain223a73d2019-07-04 12:46:18 -0500358 decode_rw_file_memory_req(request, payloadLength, &fileHandle, &offset,
359 &length, &address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530360
361 if (length % dma::minSize)
362 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500363 error("Packet length '{LENGTH}' is non multiple of minimum DMA size",
364 "LENGTH", length);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530365 encode_rw_file_memory_resp(request->hdr.instance_id,
366 PLDM_WRITE_FILE_FROM_MEMORY,
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500367 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530368 return response;
369 }
370
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530371 using namespace pldm::filetable;
372 auto& table = buildFileTable(FILE_TABLE_JSON);
373 FileEntry value{};
374
375 try
376 {
377 value = table.at(fileHandle);
378 }
Patrick Williams51330582021-10-06 12:48:56 -0500379 catch (const std::exception& e)
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530380 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500381 error(
382 "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}",
383 "HANDLE", fileHandle, "ERROR", e);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530384 encode_rw_file_memory_resp(request->hdr.instance_id,
385 PLDM_WRITE_FILE_FROM_MEMORY,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530386 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
387 return response;
388 }
389
390 if (!fs::exists(value.fsPath))
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530391 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500392 error("File '{PATH}' does not exist for file handle '{FILE_HANDLE}'",
393 "PATH", value.fsPath, "FILE_HANDLE", fileHandle);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530394 encode_rw_file_memory_resp(request->hdr.instance_id,
395 PLDM_WRITE_FILE_FROM_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530396 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
397 return response;
398 }
399
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530400 auto fileSize = fs::file_size(value.fsPath);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530401 if (offset >= fileSize)
402 {
Sagar Srinivas82257922023-07-10 08:12:22 -0500403 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500404 "Offset '{OFFSET}' exceeds file size {SIZE} for file '{PATH} and handle {FILE_HANDLE}",
405 "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath,
406 "FILE_HANDLE", fileHandle);
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530407 encode_rw_file_memory_resp(request->hdr.instance_id,
408 PLDM_WRITE_FILE_FROM_MEMORY,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530409 PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
410 return response;
411 }
412
413 using namespace dma;
414 DMA intf;
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530415 return transferAll<DMA>(&intf, PLDM_WRITE_FILE_FROM_MEMORY, value.fsPath,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530416 offset, length, address, false,
417 request->hdr.instance_id);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530418}
419
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600420Response Handler::getFileTable(const pldm_msg* request, size_t payloadLength)
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530421{
422 uint32_t transferHandle = 0;
423 uint8_t transferFlag = 0;
424 uint8_t tableType = 0;
425
426 Response response(sizeof(pldm_msg_hdr) +
427 PLDM_GET_FILE_TABLE_MIN_RESP_BYTES);
428 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
429
430 if (payloadLength != PLDM_GET_FILE_TABLE_REQ_BYTES)
431 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530432 encode_get_file_table_resp(request->hdr.instance_id,
433 PLDM_ERROR_INVALID_LENGTH, 0, 0, nullptr, 0,
434 responsePtr);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530435 return response;
436 }
437
Zahed Hossain223a73d2019-07-04 12:46:18 -0500438 auto rc = decode_get_file_table_req(request, payloadLength, &transferHandle,
439 &transferFlag, &tableType);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530440 if (rc)
441 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530442 encode_get_file_table_resp(request->hdr.instance_id, rc, 0, 0, nullptr,
443 0, responsePtr);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530444 return response;
445 }
446
447 if (tableType != PLDM_FILE_ATTRIBUTE_TABLE)
448 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530449 encode_get_file_table_resp(request->hdr.instance_id,
450 PLDM_INVALID_FILE_TABLE_TYPE, 0, 0, nullptr,
451 0, responsePtr);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530452 return response;
453 }
454
455 using namespace pldm::filetable;
456 auto table = buildFileTable(FILE_TABLE_JSON);
457 auto attrTable = table();
458 response.resize(response.size() + attrTable.size());
459 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
460
461 if (attrTable.empty())
462 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530463 encode_get_file_table_resp(request->hdr.instance_id,
464 PLDM_FILE_TABLE_UNAVAILABLE, 0, 0, nullptr,
465 0, responsePtr);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530466 return response;
467 }
468
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530469 encode_get_file_table_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
470 PLDM_START_AND_END, attrTable.data(),
471 attrTable.size(), responsePtr);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530472 return response;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530473}
474
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600475Response Handler::readFile(const pldm_msg* request, size_t payloadLength)
vkaverap5b914c32019-06-30 22:23:54 -0500476{
477 uint32_t fileHandle = 0;
478 uint32_t offset = 0;
479 uint32_t length = 0;
480
481 Response response(sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES);
482 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
483
484 if (payloadLength != PLDM_READ_FILE_REQ_BYTES)
485 {
486 encode_read_file_resp(request->hdr.instance_id,
487 PLDM_ERROR_INVALID_LENGTH, length, responsePtr);
488 return response;
489 }
490
491 auto rc = decode_read_file_req(request, payloadLength, &fileHandle, &offset,
492 &length);
493
494 if (rc)
495 {
496 encode_read_file_resp(request->hdr.instance_id, rc, 0, responsePtr);
497 return response;
498 }
499
500 using namespace pldm::filetable;
501 auto& table = buildFileTable(FILE_TABLE_JSON);
502 FileEntry value{};
503
504 try
505 {
506 value = table.at(fileHandle);
507 }
Patrick Williams51330582021-10-06 12:48:56 -0500508 catch (const std::exception& e)
vkaverap5b914c32019-06-30 22:23:54 -0500509 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500510 error(
511 "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}",
512 "HANDLE", fileHandle, "ERROR", e);
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500513
vkaverap5b914c32019-06-30 22:23:54 -0500514 encode_read_file_resp(request->hdr.instance_id,
515 PLDM_INVALID_FILE_HANDLE, length, responsePtr);
516 return response;
517 }
518
519 if (!fs::exists(value.fsPath))
520 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500521 error("File '{PATH}' and handle {FILE_HANDLE} does not exist", "PATH",
522 value.fsPath, "FILE_HANDLE", fileHandle);
vkaverap5b914c32019-06-30 22:23:54 -0500523 encode_read_file_resp(request->hdr.instance_id,
524 PLDM_INVALID_FILE_HANDLE, length, responsePtr);
525 return response;
526 }
527
528 auto fileSize = fs::file_size(value.fsPath);
529 if (offset >= fileSize)
530 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500531 error(
532 "Offset '{OFFSET}' exceeds file size '{SIZE}' for file '{PATH}' and file handle '{HANDLE}'",
533 "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath, "HANDLE",
534 fileHandle);
vkaverap5b914c32019-06-30 22:23:54 -0500535 encode_read_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE,
536 length, responsePtr);
537 return response;
538 }
539
540 if (offset + length > fileSize)
541 {
542 length = fileSize - offset;
543 }
544
545 response.resize(response.size() + length);
546 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
547 auto fileDataPos = reinterpret_cast<char*>(responsePtr);
548 fileDataPos += sizeof(pldm_msg_hdr) + sizeof(uint8_t) + sizeof(length);
549
550 std::ifstream stream(value.fsPath, std::ios::in | std::ios::binary);
551 stream.seekg(offset);
552 stream.read(fileDataPos, length);
553
554 encode_read_file_resp(request->hdr.instance_id, PLDM_SUCCESS, length,
555 responsePtr);
556
557 return response;
558}
559
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600560Response Handler::writeFile(const pldm_msg* request, size_t payloadLength)
vkaverap5b914c32019-06-30 22:23:54 -0500561{
562 uint32_t fileHandle = 0;
563 uint32_t offset = 0;
564 uint32_t length = 0;
565 size_t fileDataOffset = 0;
566
567 Response response(sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES);
568 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
569
570 if (payloadLength < PLDM_WRITE_FILE_REQ_BYTES)
571 {
572 encode_write_file_resp(request->hdr.instance_id,
573 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
574 return response;
575 }
576
577 auto rc = decode_write_file_req(request, payloadLength, &fileHandle,
578 &offset, &length, &fileDataOffset);
579
580 if (rc)
581 {
582 encode_write_file_resp(request->hdr.instance_id, rc, 0, responsePtr);
583 return response;
584 }
585
586 using namespace pldm::filetable;
587 auto& table = buildFileTable(FILE_TABLE_JSON);
588 FileEntry value{};
589
590 try
591 {
592 value = table.at(fileHandle);
593 }
Patrick Williams51330582021-10-06 12:48:56 -0500594 catch (const std::exception& e)
vkaverap5b914c32019-06-30 22:23:54 -0500595 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500596 error(
597 "File handle '{HANDLE}' does not exist in the file table, error - {ERROR}",
598 "HANDLE", fileHandle, "ERROR", e);
vkaverap5b914c32019-06-30 22:23:54 -0500599 encode_write_file_resp(request->hdr.instance_id,
600 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
601 return response;
602 }
603
604 if (!fs::exists(value.fsPath))
605 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500606 error("File '{PATH}' and handle {FILE_HANDLE} does not exist", "PATH",
607 value.fsPath, "FILE_HANDLE", fileHandle);
vkaverap5b914c32019-06-30 22:23:54 -0500608 encode_write_file_resp(request->hdr.instance_id,
609 PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
610 return response;
611 }
612
613 auto fileSize = fs::file_size(value.fsPath);
614 if (offset >= fileSize)
615 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500616 error(
617 "Offset '{OFFSET}' exceeds file size '{SIZE}' for file '{PATH}' and handle {FILE_HANDLE}",
618 "OFFSET", offset, "SIZE", fileSize, "PATH", value.fsPath,
619 "FILE_HANDLE", fileHandle);
vkaverap5b914c32019-06-30 22:23:54 -0500620 encode_write_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE,
621 0, responsePtr);
622 return response;
623 }
624
Patrick Williams6da4f912023-05-10 07:50:53 -0500625 auto fileDataPos = reinterpret_cast<const char*>(request->payload) +
626 fileDataOffset;
vkaverap5b914c32019-06-30 22:23:54 -0500627
628 std::ofstream stream(value.fsPath,
629 std::ios::in | std::ios::out | std::ios::binary);
630 stream.seekp(offset);
631 stream.write(fileDataPos, length);
632
633 encode_write_file_resp(request->hdr.instance_id, PLDM_SUCCESS, length,
634 responsePtr);
635
636 return response;
637}
638
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600639Response rwFileByTypeIntoMemory(uint8_t cmd, const pldm_msg* request,
Sampa Misra69508502020-09-08 00:08:21 -0500640 size_t payloadLength,
641 oem_platform::Handler* oemPlatformHandler)
Sampa Misra854e61f2019-08-22 04:36:47 -0500642{
643 Response response(
644 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES, 0);
645 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
646
647 if (payloadLength != PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES)
648 {
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600649 encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd,
650 PLDM_ERROR_INVALID_LENGTH, 0,
651 responsePtr);
Sampa Misra854e61f2019-08-22 04:36:47 -0500652 return response;
653 }
654
655 uint16_t fileType{};
656 uint32_t fileHandle{};
657 uint32_t offset{};
658 uint32_t length{};
659 uint64_t address{};
660 auto rc = decode_rw_file_by_type_memory_req(request, payloadLength,
661 &fileType, &fileHandle, &offset,
662 &length, &address);
663 if (rc != PLDM_SUCCESS)
664 {
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600665 encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, rc, 0,
666 responsePtr);
Sampa Misra854e61f2019-08-22 04:36:47 -0500667 return response;
668 }
669 if (length % dma::minSize)
670 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500671 error("Packet length '{LENGTH}' is non multiple of minimum DMA size",
672 "LENGTH", length);
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600673 encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd,
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500674 PLDM_ERROR_INVALID_LENGTH, 0,
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600675 responsePtr);
Sampa Misra854e61f2019-08-22 04:36:47 -0500676 return response;
677 }
678
679 std::unique_ptr<FileHandler> handler{};
680 try
681 {
682 handler = getHandlerByType(fileType, fileHandle);
683 }
684 catch (const InternalFailure& e)
685 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500686 error("Unknown file type '{TYPE}', error - {ERROR} ", "TYPE", fileType,
687 "ERROR", e);
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600688 encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd,
689 PLDM_INVALID_FILE_TYPE, 0,
690 responsePtr);
Sampa Misra854e61f2019-08-22 04:36:47 -0500691 return response;
692 }
693
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600694 rc = cmd == PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY
Sampa Misra69508502020-09-08 00:08:21 -0500695 ? handler->writeFromMemory(offset, length, address,
696 oemPlatformHandler)
697 : handler->readIntoMemory(offset, length, address,
698 oemPlatformHandler);
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600699 encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd, rc,
Sampa Misra854e61f2019-08-22 04:36:47 -0500700 length, responsePtr);
701 return response;
702}
703
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600704Response Handler::writeFileByTypeFromMemory(const pldm_msg* request,
705 size_t payloadLength)
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600706{
707 return rwFileByTypeIntoMemory(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY, request,
Sampa Misra69508502020-09-08 00:08:21 -0500708 payloadLength, oemPlatformHandler);
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600709}
710
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600711Response Handler::readFileByTypeIntoMemory(const pldm_msg* request,
712 size_t payloadLength)
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600713{
714 return rwFileByTypeIntoMemory(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, request,
Sampa Misra69508502020-09-08 00:08:21 -0500715 payloadLength, oemPlatformHandler);
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600716}
717
Sampa Misra18967162020-01-14 02:31:41 -0600718Response Handler::writeFileByType(const pldm_msg* request, size_t payloadLength)
719{
720 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES);
721 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
722
Sampa Misrad823cc02020-03-24 04:53:20 -0500723 if (payloadLength < PLDM_RW_FILE_BY_TYPE_REQ_BYTES)
Sampa Misra18967162020-01-14 02:31:41 -0600724 {
725 encode_rw_file_by_type_resp(request->hdr.instance_id,
726 PLDM_WRITE_FILE_BY_TYPE,
727 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
728 return response;
729 }
730 uint16_t fileType{};
731 uint32_t fileHandle{};
732 uint32_t offset{};
733 uint32_t length{};
734
735 auto rc = decode_rw_file_by_type_req(request, payloadLength, &fileType,
736 &fileHandle, &offset, &length);
737 if (rc != PLDM_SUCCESS)
738 {
739 encode_rw_file_by_type_resp(request->hdr.instance_id,
740 PLDM_WRITE_FILE_BY_TYPE, rc, 0,
741 responsePtr);
742 return response;
743 }
744
745 std::unique_ptr<FileHandler> handler{};
746 try
747 {
748 handler = getHandlerByType(fileType, fileHandle);
749 }
750 catch (const InternalFailure& e)
751 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500752 error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType,
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500753 "ERROR", e);
Sampa Misra18967162020-01-14 02:31:41 -0600754 encode_rw_file_by_type_resp(request->hdr.instance_id,
755 PLDM_WRITE_FILE_BY_TYPE,
756 PLDM_INVALID_FILE_TYPE, 0, responsePtr);
757 return response;
758 }
759
760 rc = handler->write(reinterpret_cast<const char*>(
Sampa Misrad823cc02020-03-24 04:53:20 -0500761 request->payload + PLDM_RW_FILE_BY_TYPE_REQ_BYTES),
Sampa Misra69508502020-09-08 00:08:21 -0500762 offset, length, oemPlatformHandler);
Sampa Misra18967162020-01-14 02:31:41 -0600763 encode_rw_file_by_type_resp(request->hdr.instance_id,
764 PLDM_WRITE_FILE_BY_TYPE, rc, length,
765 responsePtr);
766 return response;
767}
768
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600769Response Handler::readFileByType(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600770{
771 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES);
772 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
773
774 if (payloadLength != PLDM_RW_FILE_BY_TYPE_REQ_BYTES)
775 {
776 encode_rw_file_by_type_resp(request->hdr.instance_id,
777 PLDM_READ_FILE_BY_TYPE,
778 PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
779 return response;
780 }
781 uint16_t fileType{};
782 uint32_t fileHandle{};
783 uint32_t offset{};
784 uint32_t length{};
785
786 auto rc = decode_rw_file_by_type_req(request, payloadLength, &fileType,
787 &fileHandle, &offset, &length);
788 if (rc != PLDM_SUCCESS)
789 {
790 encode_rw_file_by_type_resp(request->hdr.instance_id,
791 PLDM_READ_FILE_BY_TYPE, rc, 0, responsePtr);
792 return response;
793 }
794
795 std::unique_ptr<FileHandler> handler{};
796 try
797 {
798 handler = getHandlerByType(fileType, fileHandle);
799 }
800 catch (const InternalFailure& e)
801 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500802 error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType,
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500803 "ERROR", e);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600804 encode_rw_file_by_type_resp(request->hdr.instance_id,
805 PLDM_READ_FILE_BY_TYPE,
806 PLDM_INVALID_FILE_TYPE, 0, responsePtr);
807 return response;
808 }
809
Sampa Misra69508502020-09-08 00:08:21 -0500810 rc = handler->read(offset, length, response, oemPlatformHandler);
Deepak Kodihalli9a26f892019-12-10 06:48:06 -0600811 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600812 encode_rw_file_by_type_resp(request->hdr.instance_id,
813 PLDM_READ_FILE_BY_TYPE, rc, length,
814 responsePtr);
815 return response;
816}
817
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600818Response Handler::fileAck(const pldm_msg* request, size_t payloadLength)
819{
820 Response response(sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES);
821 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
822
823 if (payloadLength != PLDM_FILE_ACK_REQ_BYTES)
824 {
825 encode_file_ack_resp(request->hdr.instance_id,
826 PLDM_ERROR_INVALID_LENGTH, responsePtr);
827 return response;
828 }
829 uint16_t fileType{};
830 uint32_t fileHandle{};
831 uint8_t fileStatus{};
832
833 auto rc = decode_file_ack_req(request, payloadLength, &fileType,
834 &fileHandle, &fileStatus);
835 if (rc != PLDM_SUCCESS)
836 {
837 encode_file_ack_resp(request->hdr.instance_id, rc, responsePtr);
838 return response;
839 }
840
841 std::unique_ptr<FileHandler> handler{};
842 try
843 {
844 handler = getHandlerByType(fileType, fileHandle);
845 }
Sampa Misra18967162020-01-14 02:31:41 -0600846
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600847 catch (const InternalFailure& e)
848 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500849 error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType,
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500850 "ERROR", e);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600851 encode_file_ack_resp(request->hdr.instance_id, PLDM_INVALID_FILE_TYPE,
852 responsePtr);
853 return response;
854 }
855
856 rc = handler->fileAck(fileStatus);
857 encode_file_ack_resp(request->hdr.instance_id, rc, responsePtr);
858 return response;
859}
860
George Liu89aad712020-03-12 13:34:51 +0800861Response Handler::getAlertStatus(const pldm_msg* request, size_t payloadLength)
862{
863 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_ALERT_STATUS_RESP_BYTES);
864 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
865 if (payloadLength != PLDM_GET_ALERT_STATUS_REQ_BYTES)
866 {
867 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
868 }
869
870 uint8_t versionId{};
871
872 auto rc = decode_get_alert_status_req(request, payloadLength, &versionId);
873 if (rc != PLDM_SUCCESS)
874 {
875 return CmdHandler::ccOnlyResponse(request, rc);
876 }
877
878 if (versionId != 0)
879 {
880 return CmdHandler::ccOnlyResponse(request,
881 PLDM_HOST_UNSUPPORTED_FORMAT_VERSION);
882 }
883
884 constexpr uint32_t rackEntry = 0xFF000030;
885 constexpr uint32_t priCecNode = 0x00008030;
886 rc = encode_get_alert_status_resp(request->hdr.instance_id, PLDM_SUCCESS,
887 rackEntry, priCecNode, responsePtr,
888 PLDM_GET_ALERT_STATUS_RESP_BYTES);
889 if (rc != PLDM_SUCCESS)
890 {
891 return CmdHandler::ccOnlyResponse(request, rc);
892 }
893
894 return response;
895}
896
Sampa Misra18967162020-01-14 02:31:41 -0600897Response Handler::newFileAvailable(const pldm_msg* request,
898 size_t payloadLength)
899{
900 Response response(sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES);
901
902 if (payloadLength != PLDM_NEW_FILE_REQ_BYTES)
903 {
904 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
905 }
906 uint16_t fileType{};
907 uint32_t fileHandle{};
908 uint64_t length{};
909
910 auto rc = decode_new_file_req(request, payloadLength, &fileType,
911 &fileHandle, &length);
912
913 if (rc != PLDM_SUCCESS)
914 {
915 return CmdHandler::ccOnlyResponse(request, rc);
916 }
917
918 std::unique_ptr<FileHandler> handler{};
919 try
920 {
921 handler = getHandlerByType(fileType, fileHandle);
922 }
923 catch (const InternalFailure& e)
924 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500925 error("Unknown file type '{TYPE}', error - {ERROR}", "TYPE", fileType,
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500926 "ERROR", e);
Sampa Misra18967162020-01-14 02:31:41 -0600927 return CmdHandler::ccOnlyResponse(request, PLDM_INVALID_FILE_TYPE);
928 }
929
930 rc = handler->newFileAvailable(length);
931 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
932 encode_new_file_resp(request->hdr.instance_id, rc, responsePtr);
933 return response;
934}
935
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600936} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530937} // namespace responder
938} // namespace pldm