2 Parse the contents of named fw_cfg files as simple (scalar) data types.
4 Copyright (C) 2020, Red Hat, Inc.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/BaseLib.h>
10 #include <Library/QemuFwCfgLib.h>
11 #include <Library/QemuFwCfgSimpleParserLib.h>
14 // Size of the longest valid UINT64 string, including the terminating NUL.
16 #define UINT64_STRING_MAX_SIZE \
17 MAX (sizeof "18446744073709551615", sizeof "0xFFFFFFFFFFFFFFFF")
20 // Size of the longest valid BOOL string (see the "mTrueString" and
21 // "mFalseString" arrays below), including the terminating NUL.
23 #define BOOL_STRING_MAX_SIZE (sizeof "disabled")
26 // Length of "\r\n", not including the terminating NUL.
28 #define CRLF_LENGTH (sizeof "\r\n" - 1)
31 // Words recognized as representing TRUE or FALSE.
33 STATIC CONST CHAR8
*CONST mTrueString
[] = {
34 "true", "yes", "y", "enable", "enabled", "1"
36 STATIC CONST CHAR8
*CONST mFalseString
[] = {
37 "false", "no", "n", "disable", "disabled", "0"
45 Look up FileName with QemuFwCfgFindFile() from QemuFwCfgLib. Read the fw_cfg
46 file into the caller-provided CHAR8 array. NUL-terminate the array.
48 @param[in] FileName The name of the fw_cfg file to look up and read.
50 @param[in,out] BufferSize On input, number of bytes available in Buffer.
52 On output, the number of bytes that have been
55 On error, BufferSize is indeterminate.
57 @param[out] Buffer The buffer to read the fw_cfg file into. If the
58 fw_cfg file contents are not NUL-terminated, then
59 a NUL character is placed into Buffer after the
62 On error, Buffer is indeterminate.
64 @retval RETURN_SUCCESS Buffer has been populated with the fw_cfg file
65 contents. Buffer is NUL-terminated regardless
66 of whether the fw_cfg file itself was
69 @retval RETURN_UNSUPPORTED Firmware configuration is unavailable.
71 @retval RETURN_PROTOCOL_ERROR The fw_cfg file does not fit into Buffer.
72 (This is considered a QEMU configuration
73 error; BufferSize is considered authoritative
74 for the contents of the fw_cfg file identified
77 @retval RETURN_PROTOCOL_ERROR The fw_cfg file contents are not themselves
78 NUL-terminated, and an extra NUL byte does not
79 fit into Buffer. (Again a QEMU configuration
82 @return Error codes propagated from
87 QemuFwCfgGetAsString (
88 IN CONST CHAR8
*FileName
,
89 IN OUT UINTN
*BufferSize
,
94 FIRMWARE_CONFIG_ITEM FwCfgItem
;
97 if (!QemuFwCfgIsAvailable ()) {
98 return RETURN_UNSUPPORTED
;
101 Status
= QemuFwCfgFindFile (FileName
, &FwCfgItem
, &FwCfgSize
);
102 if (RETURN_ERROR (Status
)) {
106 if (FwCfgSize
> *BufferSize
) {
107 return RETURN_PROTOCOL_ERROR
;
110 QemuFwCfgSelectItem (FwCfgItem
);
111 QemuFwCfgReadBytes (FwCfgSize
, Buffer
);
114 // If Buffer is already NUL-terminated due to fw_cfg contents, we're done.
116 if ((FwCfgSize
> 0) && (Buffer
[FwCfgSize
- 1] == '\0')) {
117 *BufferSize
= FwCfgSize
;
118 return RETURN_SUCCESS
;
122 // Otherwise, append a NUL byte to Buffer (if we have room for it).
124 if (FwCfgSize
== *BufferSize
) {
125 return RETURN_PROTOCOL_ERROR
;
128 Buffer
[FwCfgSize
] = '\0';
129 *BufferSize
= FwCfgSize
+ 1;
130 return RETURN_SUCCESS
;
134 Remove a trailing \r\n or \n sequence from a string.
136 @param[in,out] BufferSize On input, the number of bytes in Buffer, including
139 On output, the adjusted string size (including the
140 terminating NUL), after stripping the \r\n or \n
143 @param[in,out] Buffer The NUL-terminated string to trim.
148 IN OUT UINTN
*BufferSize
,
152 UINTN InSize
, OutSize
;
154 InSize
= *BufferSize
;
158 (Buffer
[InSize
- 3] == '\r') && (Buffer
[InSize
- 2] == '\n'))
160 OutSize
= InSize
- 2;
161 } else if ((InSize
>= 2) && (Buffer
[InSize
- 2] == '\n')) {
162 OutSize
= InSize
- 1;
165 if (OutSize
< InSize
) {
166 Buffer
[OutSize
- 1] = '\0';
167 *BufferSize
= OutSize
;
172 Read the fw_cfg file identified by FileName as a string into a small array
173 with automatic storage duration, using QemuFwCfgGetAsString(). Parse the
174 string as a UINT64. Perform a range-check on the parsed value.
176 @param[in] FileName The name of the fw_cfg file to look up and parse.
178 @param[in] ParseAsHex If TRUE, call BaseLib's AsciiStrHexToUint64S() for
179 parsing the fw_cfg file.
181 If FALSE, call BaseLib's AsciiStrDecimalToUint64S()
182 for parsing the fw_cfg file.
184 @param[in] Limit The inclusive upper bound on the parsed UINT64 value.
186 @param[out] Value On success, Value has been parsed with the BaseLib
187 function determined by ParseAsHex, and has been
188 range-checked against [0, Limit].
190 On failure, Value is not changed.
192 @retval RETURN_SUCCESS Parsing successful. Value has been set.
194 @retval RETURN_UNSUPPORTED Firmware configuration is unavailable.
196 @retval RETURN_PROTOCOL_ERROR Parsing failed. Value has not been changed.
198 @retval RETURN_PROTOCOL_ERROR Parsing succeeded, but the result does not fit
199 in the [0, Limit] range. Value has not been
202 @return Error codes propagated from
203 QemuFwCfgFindFile() and from the BaseLib
204 function selected by ParseAsHex. Value has not
209 QemuFwCfgParseUint64WithLimit (
210 IN CONST CHAR8
*FileName
,
211 IN BOOLEAN ParseAsHex
,
216 UINTN Uint64StringSize
;
217 CHAR8 Uint64String
[UINT64_STRING_MAX_SIZE
+ CRLF_LENGTH
];
218 RETURN_STATUS Status
;
222 Uint64StringSize
= sizeof Uint64String
;
223 Status
= QemuFwCfgGetAsString (FileName
, &Uint64StringSize
, Uint64String
);
224 if (RETURN_ERROR (Status
)) {
228 StripNewline (&Uint64StringSize
, Uint64String
);
231 Status
= AsciiStrHexToUint64S (Uint64String
, &EndPointer
, &Uint64
);
233 Status
= AsciiStrDecimalToUint64S (Uint64String
, &EndPointer
, &Uint64
);
236 if (RETURN_ERROR (Status
)) {
241 // Report a wire protocol error if the subject sequence is empty, or trailing
242 // garbage is present, or Limit is not honored.
244 if ((EndPointer
== Uint64String
) || (*EndPointer
!= '\0') || (Uint64
> Limit
)) {
245 return RETURN_PROTOCOL_ERROR
;
249 return RETURN_SUCCESS
;
258 QemuFwCfgSimpleParserInit (
263 // Do nothing, just participate in constructor dependency ordering.
265 return RETURN_SUCCESS
;
271 IN CONST CHAR8
*FileName
,
275 UINTN BoolStringSize
;
276 CHAR8 BoolString
[BOOL_STRING_MAX_SIZE
+ CRLF_LENGTH
];
277 RETURN_STATUS Status
;
280 BoolStringSize
= sizeof BoolString
;
281 Status
= QemuFwCfgGetAsString (FileName
, &BoolStringSize
, BoolString
);
282 if (RETURN_ERROR (Status
)) {
286 StripNewline (&BoolStringSize
, BoolString
);
288 for (Idx
= 0; Idx
< ARRAY_SIZE (mTrueString
); ++Idx
) {
289 if (AsciiStriCmp (BoolString
, mTrueString
[Idx
]) == 0) {
291 return RETURN_SUCCESS
;
295 for (Idx
= 0; Idx
< ARRAY_SIZE (mFalseString
); ++Idx
) {
296 if (AsciiStriCmp (BoolString
, mFalseString
[Idx
]) == 0) {
298 return RETURN_SUCCESS
;
302 return RETURN_PROTOCOL_ERROR
;
307 QemuFwCfgParseUint8 (
308 IN CONST CHAR8
*FileName
,
309 IN BOOLEAN ParseAsHex
,
313 RETURN_STATUS Status
;
316 Status
= QemuFwCfgParseUint64WithLimit (
322 if (RETURN_ERROR (Status
)) {
326 *Value
= (UINT8
)Uint64
;
327 return RETURN_SUCCESS
;
332 QemuFwCfgParseUint16 (
333 IN CONST CHAR8
*FileName
,
334 IN BOOLEAN ParseAsHex
,
338 RETURN_STATUS Status
;
341 Status
= QemuFwCfgParseUint64WithLimit (
347 if (RETURN_ERROR (Status
)) {
351 *Value
= (UINT16
)Uint64
;
352 return RETURN_SUCCESS
;
357 QemuFwCfgParseUint32 (
358 IN CONST CHAR8
*FileName
,
359 IN BOOLEAN ParseAsHex
,
363 RETURN_STATUS Status
;
366 Status
= QemuFwCfgParseUint64WithLimit (
372 if (RETURN_ERROR (Status
)) {
376 *Value
= (UINT32
)Uint64
;
377 return RETURN_SUCCESS
;
382 QemuFwCfgParseUint64 (
383 IN CONST CHAR8
*FileName
,
384 IN BOOLEAN ParseAsHex
,
388 RETURN_STATUS Status
;
391 Status
= QemuFwCfgParseUint64WithLimit (
397 if (RETURN_ERROR (Status
)) {
402 return RETURN_SUCCESS
;
407 QemuFwCfgParseUintn (
408 IN CONST CHAR8
*FileName
,
409 IN BOOLEAN ParseAsHex
,
413 RETURN_STATUS Status
;
416 Status
= QemuFwCfgParseUint64WithLimit (
422 if (RETURN_ERROR (Status
)) {
426 *Value
= (UINTN
)Uint64
;
427 return RETURN_SUCCESS
;