blob: ae475b958194602ab945bddc383a8fe51e6f46ba [file] [log] [blame]
Tom Josephf8329ac2019-04-11 22:13:22 +05301#pragma once
2
3#include <stdint.h>
4#include <unistd.h>
5
6#include <filesystem>
7
8#include "libpldm/base.h"
9#include "libpldm/file_io.h"
10
11namespace pldm
12{
13
14namespace responder
15{
16
17namespace utils
18{
19
20/** @struct CustomFD
21 *
22 * RAII wrapper for file descriptor.
23 */
24struct CustomFD
25{
26 CustomFD(const CustomFD&) = delete;
27 CustomFD& operator=(const CustomFD&) = delete;
28 CustomFD(CustomFD&&) = delete;
29 CustomFD& operator=(CustomFD&&) = delete;
30
31 CustomFD(int fd) : fd(fd)
32 {
33 }
34
35 ~CustomFD()
36 {
37 if (fd >= 0)
38 {
39 close(fd);
40 }
41 }
42
43 int operator()() const
44 {
45 return fd;
46 }
47
48 private:
49 int fd = -1;
50};
51
52} // namespace utils
53
Eddie James3b02e272019-04-22 20:13:55 +000054namespace fs = std::filesystem;
55
Tom Josephf8329ac2019-04-11 22:13:22 +053056namespace dma
57{
58
Tom Josephf8329ac2019-04-11 22:13:22 +053059// The minimum data size of dma transfer in bytes
60constexpr uint32_t minSize = 16;
61
62// 16MB - 4096B (16773120 bytes) is the maximum data size of DMA transfer
63constexpr size_t maxSize = (16 * 1024 * 1024) - 4096;
64
Eddie James3b02e272019-04-22 20:13:55 +000065/** @brief API to transfer data between BMC and host using DMA
Tom Josephf8329ac2019-04-11 22:13:22 +053066 *
Eddie James3b02e272019-04-22 20:13:55 +000067 * @param[in] path - pathname of the file to transfer data from or to
68 * @param[in] offset - offset in the file
69 * @param[in] length - length of the data to transfer
70 * @param[in] address - DMA address on the host
71 * @param[in] upstream - indicates directon of the transfer; true indicates
72 * transfer to the host
73 *
74 * @return - returns 0 on success, negative errno on failure
Tom Josephf8329ac2019-04-11 22:13:22 +053075 */
Eddie James3b02e272019-04-22 20:13:55 +000076int transferDataHost(const fs::path& path, uint32_t offset, uint32_t length,
77 uint64_t address, bool upstream);
Tom Josephf8329ac2019-04-11 22:13:22 +053078} // namespace dma
79
Eddie James3b02e272019-04-22 20:13:55 +000080/** @brief Transfer the data between BMC and host using DMA.
81 *
82 * There is a max size for each DMA operation, transferAll API abstracts this
83 * and the requested length is broken down into multiple DMA operations if the
84 * length exceed max size.
85 *
86 * @param[in] command - PLDM command
87 * @param[in] path - pathname of the file to transfer data from or to
88 * @param[in] offset - offset in the file
89 * @param[in] length - length of the data to transfer
90 * @param[in] address - DMA address on the host
91 * @param[in] upstream - indicates direction of the transfer; true indicates
92 * transfer to the host
93 * @param[out] response - response message location
94 */
95void transferAll(uint8_t command, fs::path& path, uint32_t offset,
96 uint32_t length, uint64_t address, bool upstream,
97 pldm_msg* response);
98
Tom Josephf8329ac2019-04-11 22:13:22 +053099/** @brief Handler for readFileIntoMemory command
100 *
101 * @param[in] request - pointer to PLDM request payload
102 * @param[in] payloadLength - length of the message payload
103 * @param[out] response - response message location
104 */
105void readFileIntoMemory(const uint8_t* request, size_t payloadLength,
106 pldm_msg* response);
107
Eddie James3b02e272019-04-22 20:13:55 +0000108/** @brief Handler for writeFileIntoMemory command
109 *
110 * @param[in] request - pointer to PLDM request payload
111 * @param[in] payloadLength - length of the message payload
112 * @param[out] response - response message location
113 */
114void writeFileFromMemory(const uint8_t* request, size_t payloadLength,
115 pldm_msg* response);
116
Tom Josephf8329ac2019-04-11 22:13:22 +0530117} // namespace responder
118} // namespace pldm