xref: /openbmc/phosphor-ipmi-blobs/ipmi.hpp (revision cd8dab49)
1 #pragma once
2 
3 #include "manager.hpp"
4 
5 #include <host-ipmid/ipmid-api.h>
6 
7 #include <string>
8 
9 namespace blobs
10 {
11 
12 enum BlobOEMCommands
13 {
14     bmcBlobGetCount = 0,
15     bmcBlobEnumerate = 1,
16     bmcBlobOpen = 2,
17     bmcBlobRead = 3,
18     bmcBlobWrite = 4,
19     bmcBlobCommit = 5,
20     bmcBlobClose = 6,
21     bmcBlobDelete = 7,
22     bmcBlobStat = 8,
23     bmcBlobSessionStat = 9,
24     bmcBlobWriteMeta = 10,
25 };
26 
27 /* Used by bmcBlobGetCount */
28 struct BmcBlobCountTx
29 {
30     uint8_t cmd; /* bmcBlobGetCount */
31 } __attribute__((packed));
32 
33 struct BmcBlobCountRx
34 {
35     uint16_t crc;
36     uint32_t blobCount;
37 } __attribute__((packed));
38 
39 /* Used by bmcBlobEnumerate */
40 struct BmcBlobEnumerateTx
41 {
42     uint8_t cmd; /* bmcBlobEnumerate */
43     uint16_t crc;
44     uint32_t blobIdx;
45 } __attribute__((packed));
46 
47 struct BmcBlobEnumerateRx
48 {
49     uint16_t crc;
50     char blobId[];
51 } __attribute__((packed));
52 
53 /* Used by bmcBlobOpen */
54 struct BmcBlobOpenTx
55 {
56     uint8_t cmd; /* bmcBlobOpen */
57     uint16_t crc;
58     uint16_t flags;
59     char blobId[]; /* Must correspond to a valid blob. */
60 } __attribute__((packed));
61 
62 struct BmcBlobOpenRx
63 {
64     uint16_t crc;
65     uint16_t sessionId;
66 } __attribute__((packed));
67 
68 /* Used by bmcBlobClose */
69 struct BmcBlobCloseTx
70 {
71     uint8_t cmd; /* bmcBlobClose */
72     uint16_t crc;
73     uint16_t sessionId; /* Returned from BmcBlobOpen. */
74 } __attribute__((packed));
75 
76 /* Used by bmcBlobDelete */
77 struct BmcBlobDeleteTx
78 {
79     uint8_t cmd; /* bmcBlobDelete */
80     uint16_t crc;
81     char blobId[];
82 } __attribute__((packed));
83 
84 /* Used by bmcBlobStat */
85 struct BmcBlobStatTx
86 {
87     uint8_t cmd; /* bmcBlobStat */
88     uint16_t crc;
89     char blobId[];
90 } __attribute__((packed));
91 
92 struct BmcBlobStatRx
93 {
94     uint16_t crc;
95     uint16_t blobState;
96     uint32_t size; /* Size in bytes of the blob. */
97     uint8_t metadataLen;
98     uint8_t metadata[]; /* Optional blob-specific metadata. */
99 } __attribute__((packed));
100 
101 /* Used by bmcBlobSessionStat */
102 struct BmcBlobSessionStatTx
103 {
104     uint8_t cmd; /* bmcBlobSessionStat */
105     uint16_t crc;
106     uint16_t sessionId;
107 } __attribute__((packed));
108 
109 /* Used by bmcBlobCommit */
110 struct BmcBlobCommitTx
111 {
112     uint8_t cmd; /* bmcBlobCommit */
113     uint16_t crc;
114     uint16_t sessionId;
115     uint8_t commitDataLen;
116     uint8_t commitData[]; /* Optional blob-specific commit data. */
117 } __attribute__((packed));
118 
119 /* Used by bmcBlobRead */
120 struct BmcBlobReadTx
121 {
122     uint8_t cmd; /* bmcBlobRead */
123     uint16_t crc;
124     uint16_t sessionId;
125     uint32_t offset;        /* The byte sequence start, 0-based. */
126     uint32_t requestedSize; /* The number of bytes requested for reading. */
127 } __attribute__((packed));
128 
129 struct BmcBlobReadRx
130 {
131     uint16_t crc;
132     uint8_t data[];
133 } __attribute__((packed));
134 
135 /* Used by bmcBlobWrite */
136 struct BmcBlobWriteTx
137 {
138     uint8_t cmd; /* bmcBlobWrite */
139     uint16_t crc;
140     uint16_t sessionId;
141     uint32_t offset; /* The byte sequence start, 0-based. */
142     uint8_t data[];
143 } __attribute__((packed));
144 
145 /* Used by bmcBlobWriteMeta */
146 struct BmcBlobWriteMetaTx
147 {
148     uint8_t cmd; /* bmcBlobWriteMeta */
149     uint16_t crc;
150     uint16_t sessionId; /* Returned from BmcBlobOpen. */
151     uint32_t offset;    /* The byte sequence start, 0-based. */
152     uint8_t data[];
153 } __attribute__((packed));
154 
155 /**
156  * Validate the minimum request length if there is one.
157  *
158  * @param[in] subcommand - the command
159  * @param[in] requestLength - the length of the request
160  * @return bool - true if valid.
161  */
162 bool validateRequestLength(BlobOEMCommands command, size_t requestLen);
163 
164 /**
165  * Given a pointer into an IPMI request buffer and the length of the remaining
166  * buffer, builds a string.  This does no string validation w.r.t content.
167  *
168  * @param[in] start - the start of the expected string.
169  * @param[in] length - the number of bytes remaining in the buffer.
170  * @return the string if valid otherwise an empty string.
171  */
172 std::string stringFromBuffer(const char* start, size_t length);
173 
174 /**
175  * Writes out a BmcBlobCountRx structure and returns IPMI_OK.
176  */
177 ipmi_ret_t getBlobCount(ManagerInterface* mgr, const uint8_t* reqBuf,
178                         uint8_t* replyCmdBuf, size_t* dataLen);
179 
180 /**
181  * Writes out a BmcBlobEnumerateRx in response to a BmcBlobEnumerateTx
182  * request.  If the index does not correspond to a blob, then this will
183  * return failure.
184  *
185  * It will also return failure if the response buffer is of an invalid
186  * length.
187  */
188 ipmi_ret_t enumerateBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
189                          uint8_t* replyCmdBuf, size_t* dataLen);
190 
191 /**
192  * Attempts to open the blobId specified and associate with a session id.
193  */
194 ipmi_ret_t openBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
195                     uint8_t* replyCmdBuf, size_t* dataLen);
196 
197 /**
198  * Attempts to close the session specified.
199  */
200 ipmi_ret_t closeBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
201                      uint8_t* replyCmdBuf, size_t* dataLen);
202 
203 /**
204  * Attempts to delete the blobId specified.
205  */
206 ipmi_ret_t deleteBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
207                       uint8_t* replyCmdBuf, size_t* dataLen);
208 
209 /**
210  * Attempts to retrieve the Stat for the blobId specified.
211  */
212 ipmi_ret_t statBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
213                     uint8_t* replyCmdBuf, size_t* dataLen);
214 
215 /**
216  * Attempts to retrieve the Stat for the session specified.
217  */
218 ipmi_ret_t sessionStatBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
219                            uint8_t* replyCmdBuf, size_t* dataLen);
220 
221 /**
222  * Attempts to commit the data in the blob.
223  */
224 ipmi_ret_t commitBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
225                       uint8_t* replyCmdBuf, size_t* dataLen);
226 
227 /**
228  * Attempt to read data from the blob.
229  */
230 ipmi_ret_t readBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
231                     uint8_t* replyCmdBuf, size_t* dataLen);
232 
233 /**
234  * Attempt to write data to the blob.
235  */
236 ipmi_ret_t writeBlob(ManagerInterface* mgr, const uint8_t* reqBuf,
237                      uint8_t* replyCmdBuf, size_t* dataLen);
238 
239 /**
240  * Attempt to write metadata to the blob.
241  */
242 ipmi_ret_t writeMeta(ManagerInterface* mgr, const uint8_t* reqBuf,
243                      uint8_t* replyCmdBuf, size_t* dataLen);
244 
245 } // namespace blobs
246