]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/ParseInf.c
3 Copyright (c) 2004 - 2010, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This contains some useful functions for parsing INF files.
26 #include "EfiUtilityMsgs.h"
31 IN MEMORY_FILE
*InputFile
,
32 IN OUT CHAR8
*InputBuffer
,
39 This function reads a line, stripping any comments.
40 The function reads a string from the input stream argument and stores it in
41 the input string. ReadLine reads characters from the current file position
42 to and including the first newline character, to the end of the stream, or
43 until the number of characters read is equal to MaxLength - 1, whichever
44 comes first. The newline character, if read, is replaced with a \0.
48 InputFile Memory file image.
49 InputBuffer Buffer to read into, must be _MAX_PATH size.
50 MaxLength The maximum size of the input buffer.
64 // Verify input parameters are not null
67 assert (InputFile
->FileImage
);
68 assert (InputFile
->Eof
);
69 assert (InputFile
->CurrentFilePointer
);
72 // Check for end of file condition
74 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
78 // Find the next newline char
80 EndOfLine
= strchr (InputFile
->CurrentFilePointer
, '\n');
83 // Determine the number of characters to copy.
87 // If no newline found, copy to the end of the file.
89 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
90 } else if (EndOfLine
>= InputFile
->Eof
) {
92 // If the newline found was beyond the end of file, copy to the eof.
94 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
97 // Newline found in the file.
99 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
102 // If the end of line is too big for the current buffer, set it to the max
103 // size of the buffer (leaving room for the \0.
105 if (CharsToCopy
> MaxLength
- 1) {
106 CharsToCopy
= MaxLength
- 1;
111 memcpy (InputBuffer
, InputFile
->CurrentFilePointer
, CharsToCopy
);
114 // Add the null termination over the 0x0D
116 if (InputBuffer
[CharsToCopy
- 1] == '\r') {
118 InputBuffer
[CharsToCopy
- 1] = '\0';
122 InputBuffer
[CharsToCopy
] = '\0';
127 // Increment the current file pointer (include the 0x0A)
129 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
132 // Strip any comments
134 CharPtr
= strstr (InputBuffer
, "//");
146 IN MEMORY_FILE
*InputFile
,
153 This function parses a file from the beginning to find a section.
154 The section string may be anywhere within a line.
158 InputFile Memory file image.
159 Section Section to search for
163 FALSE if error or EOF
164 TRUE if section found
168 CHAR8 InputBuffer
[_MAX_PATH
];
172 // Verify input is not NULL
174 assert (InputFile
->FileImage
);
175 assert (InputFile
->Eof
);
176 assert (InputFile
->CurrentFilePointer
);
180 // Rewind to beginning of file
182 InputFile
->CurrentFilePointer
= InputFile
->FileImage
;
185 // Read lines until the section is found
187 while (InputFile
->CurrentFilePointer
< InputFile
->Eof
) {
191 ReadLine (InputFile
, InputBuffer
, _MAX_PATH
);
194 // Check if the section is found
196 CurrentToken
= strstr (InputBuffer
, Section
);
197 if (CurrentToken
!= NULL
) {
207 IN MEMORY_FILE
*InputFile
,
217 Finds a token value given the section and token to search for.
221 InputFile Memory file image.
222 Section The section to search for, a string within [].
223 Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
224 Instance The instance of the token to search for. Zero is the first instance.
225 Value The string that holds the value following the =. Must be _MAX_PATH in size.
229 EFI_SUCCESS Value found.
230 EFI_ABORTED Format error detected in INF file.
231 EFI_INVALID_PARAMETER Input argument was null.
232 EFI_LOAD_ERROR Error reading from the file.
233 EFI_NOT_FOUND Section/Token/Value not found.
237 CHAR8 InputBuffer
[_MAX_PATH
];
244 // Check input parameters
246 if (InputFile
->FileImage
== NULL
||
247 InputFile
->Eof
== NULL
||
248 InputFile
->CurrentFilePointer
== NULL
||
250 strlen (Section
) == 0 ||
252 strlen (Token
) == 0 ||
255 return EFI_INVALID_PARAMETER
;
258 // Initialize error codes
264 // Initialize our instance counter for the search token
268 if (FindSection (InputFile
, Section
)) {
270 // Found the desired section, find and read the desired token
274 // Read a line from the file
276 if (ReadLine (InputFile
, InputBuffer
, _MAX_PATH
) == NULL
) {
278 // Error reading from input file
284 // Get the first non-whitespace string
286 CurrentToken
= strtok (InputBuffer
, " \t\n");
287 if (CurrentToken
== NULL
) {
289 // Whitespace line found (or comment) so continue
291 CurrentToken
= InputBuffer
;
295 // Make sure we have not reached the end of the current section
297 if (CurrentToken
[0] == '[') {
301 // Compare the current token with the desired token
303 if (strcmp (CurrentToken
, Token
) == 0) {
308 // Check if it is the correct instance
310 if (Instance
== Occurrance
) {
312 // Copy the contents following the =
314 CurrentToken
= strtok (NULL
, "= \t\n");
315 if (CurrentToken
== NULL
) {
317 // Nothing found, parsing error
322 // Copy the current token to the output value
324 strcpy (Value
, CurrentToken
);
329 // Increment the occurrance found
337 InputFile
->CurrentFilePointer
< InputFile
->Eof
&&
338 CurrentToken
[0] != '[' &&
339 Occurrance
<= Instance
343 // Distinguish between read errors and INF file format errors.
346 return EFI_LOAD_ERROR
;
353 return EFI_NOT_FOUND
;
358 IN CHAR8
*AsciiGuidBuffer
,
359 OUT EFI_GUID
*GuidBuffer
365 Converts a string to an EFI_GUID. The string must be in the
366 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
370 AsciiGuidBuffer - pointer to ascii string
371 GuidBuffer - pointer to destination Guid
375 EFI_ABORTED Could not convert the string
376 EFI_SUCCESS The string was successfully converted
377 EFI_INVALID_PARAMETER Input parameter is invalid.
387 if (AsciiGuidBuffer
== NULL
|| GuidBuffer
== NULL
) {
388 return EFI_INVALID_PARAMETER
;
391 // Check Guid Format strictly xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
393 for (Index
= 0; AsciiGuidBuffer
[Index
] != '\0' && Index
< 37; Index
++) {
394 if (Index
== 8 || Index
== 13 || Index
== 18 || Index
== 23) {
395 if (AsciiGuidBuffer
[Index
] != '-') {
399 if (((AsciiGuidBuffer
[Index
] >= '0') && (AsciiGuidBuffer
[Index
] <= '9')) ||
400 ((AsciiGuidBuffer
[Index
] >= 'a') && (AsciiGuidBuffer
[Index
] <= 'f')) ||
401 ((AsciiGuidBuffer
[Index
] >= 'A') && (AsciiGuidBuffer
[Index
] <= 'F'))) {
409 if (Index
< 36 || AsciiGuidBuffer
[36] != '\0') {
410 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
415 // Scan the guid string into the buffer
419 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
434 // Verify the correct number of items were scanned.
437 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
441 // Copy the data into our GUID.
443 GuidBuffer
->Data1
= (UINT32
) Data1
;
444 GuidBuffer
->Data2
= (UINT16
) Data2
;
445 GuidBuffer
->Data3
= (UINT16
) Data3
;
446 GuidBuffer
->Data4
[0] = (UINT8
) Data4
[0];
447 GuidBuffer
->Data4
[1] = (UINT8
) Data4
[1];
448 GuidBuffer
->Data4
[2] = (UINT8
) Data4
[2];
449 GuidBuffer
->Data4
[3] = (UINT8
) Data4
[3];
450 GuidBuffer
->Data4
[4] = (UINT8
) Data4
[4];
451 GuidBuffer
->Data4
[5] = (UINT8
) Data4
[5];
452 GuidBuffer
->Data4
[6] = (UINT8
) Data4
[6];
453 GuidBuffer
->Data4
[7] = (UINT8
) Data4
[7];
459 AsciiStringToUint64 (
460 IN CONST CHAR8
*AsciiString
,
462 OUT UINT64
*ReturnValue
468 Converts a null terminated ascii string that represents a number into a
469 UINT64 value. A hex number may be preceeded by a 0x, but may not be
470 succeeded by an h. A number without 0x or 0X is considered to be base 10
471 unless the IsHex input is true.
475 AsciiString The string to convert.
476 IsHex Force the string to be treated as a hex number.
477 ReturnValue The return value.
481 EFI_SUCCESS Number successfully converted.
482 EFI_ABORTED Invalid character encountered.
491 // Initialize the result
497 // Check input paramter
499 if (AsciiString
== NULL
|| ReturnValue
== NULL
) {
500 return EFI_INVALID_PARAMETER
;
502 while (AsciiString
[Index
] == ' ') {
507 // Add each character to the result
509 if (IsHex
|| (AsciiString
[Index
] == '0' && (AsciiString
[Index
+ 1] == 'x' || AsciiString
[Index
+ 1] == 'X'))) {
511 // Convert the hex string.
513 for (Index
= Index
+ 2; AsciiString
[Index
] != '\0'; Index
++) {
514 CurrentChar
= AsciiString
[Index
];
515 if (CurrentChar
== ' ') {
521 if (isxdigit ((int)CurrentChar
) == 0) {
528 if (CurrentChar
>= '0' && CurrentChar
<= '9') {
529 Value
+= CurrentChar
- '0';
530 } else if (CurrentChar
>= 'a' && CurrentChar
<= 'f') {
531 Value
+= CurrentChar
- 'a' + 10;
532 } else if (CurrentChar
>= 'A' && CurrentChar
<= 'F') {
533 Value
+= CurrentChar
- 'A' + 10;
537 *ReturnValue
= Value
;
540 // Convert dec string is a number
542 for (; Index
< strlen (AsciiString
); Index
++) {
543 CurrentChar
= AsciiString
[Index
];
544 if (CurrentChar
== ' ') {
550 if (isdigit ((int)CurrentChar
) == 0) {
557 Value
+= CurrentChar
- '0';
560 *ReturnValue
= Value
;
569 IN OUT CHAR8
*InputBuffer
575 This function reads a line, stripping any comments.
576 // BUGBUG: This is obsolete once genmake goes away...
580 InputFile Stream pointer.
581 InputBuffer Buffer to read into, must be _MAX_PATH size.
586 InputBuffer otherwise
593 // Verify input parameters are not null
596 assert (InputBuffer
);
601 if (fgets (InputBuffer
, _MAX_PATH
, InputFile
) == NULL
) {
605 // Strip any comments
607 CharPtr
= strstr (InputBuffer
, "//");
612 CharPtr
= strstr (InputBuffer
, "#");
623 FindSectionInStream (
631 This function parses a stream file from the beginning to find a section.
632 The section string may be anywhere within a line.
633 // BUGBUG: This is obsolete once genmake goes away...
637 InputFile Stream pointer.
638 Section Section to search for
642 FALSE if error or EOF
643 TRUE if section found
647 CHAR8 InputBuffer
[_MAX_PATH
];
651 // Verify input is not NULL
657 // Rewind to beginning of file
659 if (fseek (InputFile
, 0, SEEK_SET
) != 0) {
663 // Read lines until the section is found
665 while (feof (InputFile
) == 0) {
669 ReadLineInStream (InputFile
, InputBuffer
);
672 // Check if the section is found
674 CurrentToken
= strstr (InputBuffer
, Section
);
675 if (CurrentToken
!= NULL
) {