]>
Commit | Line | Data |
---|---|---|
7f293b25 SM |
1 | /** @file\r |
2 | AML Stream.\r | |
3 | \r | |
4 | Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>\r | |
5 | \r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | **/\r | |
8 | \r | |
9 | #ifndef AML_STREAM_H_\r | |
10 | #define AML_STREAM_H_\r | |
11 | \r | |
12 | #include <AmlInclude.h>\r | |
13 | \r | |
14 | /** Stream direction.\r | |
15 | \r | |
16 | Enum to choose the direction the stream is progressing.\r | |
17 | */\r | |
18 | typedef enum EAmlStreamDirection {\r | |
19 | EAmlStreamDirectionInvalid, ///< Invalid AML Stream direction.\r | |
20 | EAmlStreamDirectionForward, ///< Forward direction.\r | |
21 | /// The Stream goes toward higher addresses.\r | |
22 | EAmlStreamDirectionBackward, ///< Forward direction.\r | |
23 | /// The Stream goes toward lower addresses.\r | |
24 | EAmlStreamDirectionMax, ///< Max enum.\r | |
25 | } EAML_STREAM_DIRECTION;\r | |
26 | \r | |
27 | /** Stream.\r | |
28 | \r | |
29 | This structure is used as a wrapper around a buffer. It allows to do common\r | |
30 | buffer manipulations (read, write, etc.) while preventing buffer overflows.\r | |
31 | */\r | |
32 | typedef struct AmlStream {\r | |
33 | /// Pointer to a buffer.\r | |
731c67e1 | 34 | UINT8 *Buffer;\r |
7f293b25 SM |
35 | \r |
36 | /// Size of Buffer.\r | |
731c67e1 | 37 | UINT32 MaxBufferSize;\r |
7f293b25 SM |
38 | \r |
39 | /// Index in the Buffer.\r | |
40 | /// The Index field allows to keep track of how many bytes have been\r | |
41 | /// read/written in the Buffer, and to retrieve the current stream position.\r | |
42 | /// 0 <= Index <= MaxBufferSize.\r | |
43 | /// If Index == MaxBufferSize, no more action is allowed on the stream.\r | |
731c67e1 | 44 | UINT32 Index;\r |
7f293b25 SM |
45 | \r |
46 | /// The direction the stream is progressing.\r | |
47 | /// If the stream goes backward (toward lower addresses), the bytes written\r | |
48 | /// to the stream are not reverted.\r | |
49 | /// In the example below, writing "Hello" to the stream will not revert\r | |
50 | /// the string. The end of the stream buffer will contain "Hello world!".\r | |
51 | /// Similarly, moving the stream position will be done according to the\r | |
52 | /// direction of the stream.\r | |
53 | /// Stream buffer:\r | |
54 | /// +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r | |
55 | /// |-------------- | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |\r | |
56 | /// +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r | |
57 | /// ^\r | |
58 | /// Current position.\r | |
731c67e1 | 59 | EAML_STREAM_DIRECTION Direction;\r |
7f293b25 SM |
60 | } AML_STREAM;\r |
61 | \r | |
62 | /** Check whether a StreamPtr is a valid Stream.\r | |
63 | \r | |
64 | @param [in] Stream Pointer to a stream.\r | |
65 | \r | |
66 | @retval TRUE Stream is a pointer to a stream.\r | |
67 | @retval FALSE Otherwise.\r | |
68 | */\r | |
69 | #define IS_STREAM(Stream) ( \\r | |
70 | (((AML_STREAM*)Stream) != NULL) && \\r | |
71 | (((AML_STREAM*)Stream)->Buffer != NULL))\r | |
72 | \r | |
73 | /** Check whether a Stream is at the end of its buffer.\r | |
74 | \r | |
75 | @param [in] Stream Pointer to a stream.\r | |
76 | \r | |
77 | @retval TRUE Stream is a pointer to a non-full stream.\r | |
78 | @retval FALSE Otherwise.\r | |
79 | */\r | |
80 | #define IS_END_OF_STREAM(Stream) ( \\r | |
81 | (((AML_STREAM*)Stream)->Index == \\r | |
82 | ((AML_STREAM*)Stream)->MaxBufferSize))\r | |
83 | \r | |
84 | /** Check Stream goes forward.\r | |
85 | \r | |
86 | @param [in] Stream Pointer to a stream.\r | |
87 | \r | |
88 | @retval TRUE Stream goes forward.\r | |
89 | @retval FALSE Otherwise.\r | |
90 | */\r | |
731c67e1 | 91 | #define IS_STREAM_FORWARD(Stream) ( \\r |
7f293b25 SM |
92 | ((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionForward)\r |
93 | \r | |
94 | /** Check Stream goes backward.\r | |
95 | \r | |
96 | @param [in] Stream Pointer to a stream.\r | |
97 | \r | |
98 | @retval TRUE Stream goes backward.\r | |
99 | @retval FALSE Otherwise.\r | |
100 | */\r | |
731c67e1 | 101 | #define IS_STREAM_BACKWARD(Stream) ( \\r |
7f293b25 SM |
102 | ((AML_STREAM*)Stream)->Direction == EAmlStreamDirectionBackward)\r |
103 | \r | |
104 | /** Initialize a stream.\r | |
105 | \r | |
106 | @param [in, out] Stream Pointer to the stream to initialize.\r | |
107 | @param [in] Buffer Buffer to initialize Stream with.\r | |
108 | Point to the beginning of the Buffer.\r | |
109 | @param [in] MaxBufferSize Maximum size of Buffer.\r | |
110 | @param [in] Direction Direction Stream is progressing\r | |
111 | (forward, backward).\r | |
112 | \r | |
113 | @retval EFI_SUCCESS The function completed successfully.\r | |
114 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
115 | **/\r | |
116 | EFI_STATUS\r | |
117 | EFIAPI\r | |
118 | AmlStreamInit (\r | |
731c67e1 MK |
119 | IN OUT AML_STREAM *Stream,\r |
120 | IN UINT8 *Buffer,\r | |
121 | IN UINT32 MaxBufferSize,\r | |
122 | IN EAML_STREAM_DIRECTION Direction\r | |
7f293b25 SM |
123 | );\r |
124 | \r | |
125 | /** Clone a stream.\r | |
126 | \r | |
127 | Cloning a stream means copying all the values of the input Stream\r | |
128 | in the ClonedStream.\r | |
129 | \r | |
130 | @param [in] Stream Pointer to the stream to clone.\r | |
131 | @param [in] ClonedStream Pointer to the stream to initialize.\r | |
132 | \r | |
133 | @retval EFI_SUCCESS The function completed successfully.\r | |
134 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
135 | **/\r | |
136 | EFI_STATUS\r | |
137 | EFIAPI\r | |
138 | AmlStreamClone (\r | |
731c67e1 MK |
139 | IN CONST AML_STREAM *Stream,\r |
140 | OUT AML_STREAM *ClonedStream\r | |
7f293b25 SM |
141 | );\r |
142 | \r | |
143 | /** Initialize a sub-stream from a stream.\r | |
144 | \r | |
145 | A sub-stream is a stream initialized at the current position of the input\r | |
146 | stream:\r | |
147 | - the Buffer field points to the current position of the input stream;\r | |
148 | - the Index field is set to 0;\r | |
149 | - the MaxBufferSize field is set to the remaining size of the input stream;\r | |
150 | - the direction is conserved;\r | |
151 | \r | |
152 | E.g.: For a forward stream:\r | |
153 | +----------------+----------------+\r | |
154 | |ABCD.........XYZ| Free Space |\r | |
155 | +----------------+----------------+\r | |
156 | ^ ^ ^\r | |
157 | Stream: Buffer CurrPos EndOfBuff\r | |
158 | Sub-stream: Buffer/CurrPos EndOfBuff\r | |
159 | \r | |
160 | @param [in] Stream Pointer to the stream from which a sub-stream is\r | |
161 | created.\r | |
162 | @param [in] SubStream Pointer to the stream to initialize.\r | |
163 | \r | |
164 | @retval EFI_SUCCESS The function completed successfully.\r | |
165 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
166 | **/\r | |
167 | EFI_STATUS\r | |
168 | EFIAPI\r | |
169 | AmlStreamInitSubStream (\r | |
731c67e1 MK |
170 | IN CONST AML_STREAM *Stream,\r |
171 | OUT AML_STREAM *SubStream\r | |
7f293b25 SM |
172 | );\r |
173 | \r | |
174 | /** Get the buffer of a stream.\r | |
175 | \r | |
176 | @param [in] Stream Pointer to a stream.\r | |
177 | \r | |
178 | @return The stream's Buffer.\r | |
179 | NULL otherwise.\r | |
180 | **/\r | |
181 | UINT8 *\r | |
182 | EFIAPI\r | |
183 | AmlStreamGetBuffer (\r | |
731c67e1 | 184 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
185 | );\r |
186 | \r | |
187 | /** Get the size of Stream's Buffer.\r | |
188 | \r | |
189 | @param [in] Stream Pointer to a stream.\r | |
190 | \r | |
191 | @return The Size of Stream's Buffer.\r | |
192 | Return 0 if Stream is invalid.\r | |
193 | **/\r | |
194 | UINT32\r | |
195 | EFIAPI\r | |
196 | AmlStreamGetMaxBufferSize (\r | |
731c67e1 | 197 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
198 | );\r |
199 | \r | |
200 | /** Reduce the maximal size of Stream's Buffer (MaxBufferSize field).\r | |
201 | \r | |
202 | @param [in] Stream Pointer to a stream.\r | |
203 | @param [in] Diff Value to subtract to the Stream's MaxBufferSize.\r | |
204 | 0 < x < MaxBufferSize - Index.\r | |
205 | \r | |
206 | @retval EFI_SUCCESS The function completed successfully.\r | |
207 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
208 | **/\r | |
209 | EFI_STATUS\r | |
210 | EFIAPI\r | |
211 | AmlStreamReduceMaxBufferSize (\r | |
731c67e1 MK |
212 | IN AML_STREAM *Stream,\r |
213 | IN UINT32 Diff\r | |
7f293b25 SM |
214 | );\r |
215 | \r | |
216 | /** Get Stream's Index.\r | |
217 | \r | |
218 | Stream's Index is incremented when writing data, reading data,\r | |
219 | or moving the position in the Stream.\r | |
220 | It can be seen as an index:\r | |
221 | - starting at the beginning of Stream's Buffer if the stream goes forward;\r | |
222 | - starting at the end of Stream's Buffer if the stream goes backward.\r | |
223 | \r | |
224 | @param [in] Stream Pointer to a stream.\r | |
225 | \r | |
226 | @return Stream's Index.\r | |
227 | Return 0 if Stream is invalid.\r | |
228 | **/\r | |
229 | UINT32\r | |
230 | EFIAPI\r | |
231 | AmlStreamGetIndex (\r | |
731c67e1 | 232 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
233 | );\r |
234 | \r | |
235 | /** Get Stream's Direction.\r | |
236 | \r | |
237 | @param [in] Stream Pointer to a stream.\r | |
238 | \r | |
239 | @return Stream's Direction.\r | |
240 | Return EAmlStreamDirectionUnknown if Stream is invalid.\r | |
241 | **/\r | |
242 | EAML_STREAM_DIRECTION\r | |
243 | EFIAPI\r | |
244 | AmlStreamGetDirection (\r | |
731c67e1 | 245 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
246 | );\r |
247 | \r | |
248 | /** Return a pointer to the current position in the stream.\r | |
249 | \r | |
250 | @param [in] Stream Pointer to a stream.\r | |
251 | \r | |
252 | @return The current position in the stream.\r | |
253 | Return NULL if error.\r | |
254 | **/\r | |
255 | UINT8 *\r | |
256 | EFIAPI\r | |
257 | AmlStreamGetCurrPos (\r | |
731c67e1 | 258 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
259 | );\r |
260 | \r | |
261 | /** Get the space available in the stream.\r | |
262 | \r | |
263 | @param [in] Stream Pointer to a stream.\r | |
264 | \r | |
265 | @return Remaining space available in the stream.\r | |
266 | Zero in case of error or if the stream is at its end.\r | |
267 | **/\r | |
268 | UINT32\r | |
269 | EFIAPI\r | |
270 | AmlStreamGetFreeSpace (\r | |
731c67e1 | 271 | IN CONST AML_STREAM *Stream\r |
7f293b25 SM |
272 | );\r |
273 | \r | |
274 | /** Move Stream by Offset bytes.\r | |
275 | \r | |
276 | The stream current position is moved according to the stream direction\r | |
277 | (forward, backward).\r | |
278 | \r | |
279 | @param [in] Stream Pointer to a stream.\r | |
280 | The stream must not be at its end.\r | |
281 | @param [in] Offset Offset to move the stream of.\r | |
282 | \r | |
283 | @retval EFI_SUCCESS The function completed successfully.\r | |
284 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
285 | @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r | |
286 | **/\r | |
287 | EFI_STATUS\r | |
288 | EFIAPI\r | |
289 | AmlStreamProgress (\r | |
731c67e1 MK |
290 | IN AML_STREAM *Stream,\r |
291 | IN UINT32 Offset\r | |
7f293b25 SM |
292 | );\r |
293 | \r | |
294 | /** Rewind Stream of Offset bytes.\r | |
295 | \r | |
296 | The stream current position is rewound according to the stream direction\r | |
297 | (forward, backward). A stream going forward will be rewound backward.\r | |
298 | \r | |
299 | @param [in] Stream Pointer to a stream.\r | |
300 | @param [in] Offset Offset to rewind the stream of.\r | |
301 | \r | |
302 | @retval EFI_SUCCESS The function completed successfully.\r | |
303 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
304 | @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r | |
305 | **/\r | |
306 | EFI_STATUS\r | |
307 | EFIAPI\r | |
308 | AmlStreamRewind (\r | |
731c67e1 MK |
309 | IN AML_STREAM *Stream,\r |
310 | IN UINT32 Offset\r | |
7f293b25 SM |
311 | );\r |
312 | \r | |
313 | /** Reset the Stream (move the current position to the initial position).\r | |
314 | \r | |
315 | @param [in] Stream Pointer to a stream.\r | |
316 | \r | |
317 | @retval EFI_SUCCESS The function completed successfully.\r | |
318 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
319 | **/\r | |
320 | EFI_STATUS\r | |
321 | EFIAPI\r | |
322 | AmlStreamReset (\r | |
731c67e1 | 323 | IN AML_STREAM *Stream\r |
7f293b25 SM |
324 | );\r |
325 | \r | |
326 | /** Peek one byte at Stream's current position.\r | |
327 | \r | |
328 | Stream's position is not moved when peeking.\r | |
329 | \r | |
330 | @param [in] Stream Pointer to a stream.\r | |
331 | The stream must not be at its end.\r | |
332 | @param [out] OutByte Pointer holding the byte value of\r | |
333 | the stream current position.\r | |
334 | \r | |
335 | @retval EFI_SUCCESS The function completed successfully.\r | |
336 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
337 | @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r | |
338 | **/\r | |
339 | EFI_STATUS\r | |
340 | EFIAPI\r | |
341 | AmlStreamPeekByte (\r | |
731c67e1 MK |
342 | IN AML_STREAM *Stream,\r |
343 | OUT UINT8 *OutByte\r | |
7f293b25 SM |
344 | );\r |
345 | \r | |
346 | /** Read one byte at Stream's current position.\r | |
347 | \r | |
348 | The stream current position is moved when reading.\r | |
349 | \r | |
350 | @param [in] Stream Pointer to a stream.\r | |
351 | The stream must not be at its end.\r | |
352 | @param [out] OutByte Pointer holding the byte value of\r | |
353 | the stream current position.\r | |
354 | \r | |
355 | @retval EFI_SUCCESS The function completed successfully.\r | |
356 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
357 | @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r | |
358 | **/\r | |
359 | EFI_STATUS\r | |
360 | EFIAPI\r | |
361 | AmlStreamReadByte (\r | |
731c67e1 MK |
362 | IN AML_STREAM *Stream,\r |
363 | OUT UINT8 *OutByte\r | |
7f293b25 SM |
364 | );\r |
365 | \r | |
366 | /** Write Size bytes in the stream.\r | |
367 | \r | |
368 | If the stream goes backward (toward lower addresses), the bytes written\r | |
369 | to the stream are not reverted.\r | |
370 | In the example below, writing "Hello" to the stream will not revert\r | |
371 | the string. The end of the stream buffer will contain "Hello world!".\r | |
372 | Stream buffer:\r | |
373 | +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r | |
374 | | ..... | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' |\r | |
375 | +---------------+-----+-----+-----+-----+-----+-----+---- +------+\r | |
376 | ^\r | |
377 | Current position.\r | |
378 | \r | |
379 | @param [in] Stream Pointer to a stream.\r | |
380 | The stream must not be at its end.\r | |
381 | @param [in] Buffer Pointer to the data to write.\r | |
382 | @param [in] Size Number of bytes to write.\r | |
383 | \r | |
384 | @retval EFI_SUCCESS The function completed successfully.\r | |
385 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
386 | @retval EFI_BUFFER_TOO_SMALL No space left in the buffer.\r | |
387 | **/\r | |
388 | EFI_STATUS\r | |
389 | EFIAPI\r | |
390 | AmlStreamWrite (\r | |
731c67e1 MK |
391 | IN AML_STREAM *Stream,\r |
392 | IN CONST UINT8 *Buffer,\r | |
393 | IN UINT32 Size\r | |
7f293b25 SM |
394 | );\r |
395 | \r | |
396 | /** Compare Size bytes between Stream1 and Stream2 from their\r | |
397 | respective current position.\r | |
398 | \r | |
399 | Stream1 and Stream2 must go in the same direction.\r | |
400 | Stream1 and Stream2 are left unchanged.\r | |
401 | \r | |
402 | @param [in] Stream1 First stream to compare.\r | |
403 | The stream must not be at its end.\r | |
404 | @param [in] Stream2 Second stream to compare.\r | |
405 | The stream must not be at its end.\r | |
406 | @param [in] Size Number of bytes to compare.\r | |
407 | Must be lower than the minimum remaining space of\r | |
408 | Stream1 and Stream2.\r | |
409 | Must be non-zero.\r | |
410 | \r | |
411 | @retval TRUE If Stream1 and Stream2 have Size bytes equal,\r | |
412 | from their respective current position.\r | |
413 | The function completed successfully.\r | |
414 | @retval FALSE Otherwise.\r | |
415 | **/\r | |
416 | BOOLEAN\r | |
417 | EFIAPI\r | |
418 | AmlStreamCmp (\r | |
731c67e1 MK |
419 | IN CONST AML_STREAM *Stream1,\r |
420 | IN CONST AML_STREAM *Stream2,\r | |
421 | IN UINT32 Size\r | |
7f293b25 SM |
422 | );\r |
423 | \r | |
424 | /** Copy Size bytes of the stream's data to DstBuffer.\r | |
425 | \r | |
426 | For a backward stream, the bytes are copied starting from the\r | |
427 | current stream position.\r | |
428 | \r | |
429 | @param [out] DstBuffer Destination Buffer to copy the data to.\r | |
430 | @param [in] MaxDstBufferSize Maximum size of DstBuffer.\r | |
431 | Must be non-zero.\r | |
432 | @param [in] Stream Pointer to the stream to copy the data from.\r | |
433 | @param [in] Size Number of bytes to copy from the stream\r | |
434 | buffer.\r | |
435 | Must be lower than MaxDstBufferSize.\r | |
436 | Must be lower than Stream's MaxBufferSize.\r | |
437 | Return success if zero.\r | |
438 | \r | |
439 | @retval EFI_SUCCESS The function completed successfully.\r | |
440 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
441 | **/\r | |
442 | EFI_STATUS\r | |
443 | EFIAPI\r | |
444 | AmlStreamCpyS (\r | |
731c67e1 MK |
445 | OUT CHAR8 *DstBuffer,\r |
446 | IN UINT32 MaxDstBufferSize,\r | |
447 | IN AML_STREAM *Stream,\r | |
448 | IN UINT32 Size\r | |
7f293b25 SM |
449 | );\r |
450 | \r | |
451 | #endif // AML_STREAM_H_\r |