3 Copyright (c) 2004, 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 Generate Dependency Expression ("GenDepex")
20 Infix to Postfix Algorithm
22 This code has been scrubbed to be free of having any EFI core tree dependencies.
23 It should build in any environment that supports a standard C-library w/ string
24 operations and File I/O services.
26 As an example of usage, consider the following:
28 The input user file could be something like "Sample.DXS" whose contents are
33 NOT (DISK_IO_PROTOCOL AND SIMPLE_FILE_SYSTEM_PROTOCOL)
34 OR EFI_PXE_BASE_CODE_PROTOCOL
37 This file is then washed through the C-preprocessor, viz.,
39 cl /EP Sample.DXS > Sample.TMP1
41 This yields the following file "Sample.TMP1" whose contents are
44 NOT ({ 0xce345171, 0xba0b, 0x11d2, 0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72,
45 0x3b } AND { 0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69,
46 0x72, 0x3b }) OR { 0x03c4e603, 0xac28, 0x11d3, 0x9a, 0x2d, 0x00, 0x90, 0x27,
50 This file, in turn, will be fed into the utility, viz.,
52 GenDepex Sample.TMP1 Sample.TMP2
54 With a file that is 55 bytes long:
56 55 bytes for the grammar binary
58 GUID Instance - 16 bytes
60 GUID Instance - 16 bytes
64 GUID Instance - 16 bytes
68 The file "Sample.TMP2" could be fed via a Section-builder utility
69 (GenSection) that would be used for the creation of a dependency
70 section file (.DPX) which in turn would be used by a generate FFS
71 utility (GenFfsFile) to produce a DXE driver/core (.DXE) or
72 a DXE application (.APP) file.
74 Complies with Tiano C Coding Standards Document, version 0.31, 12 Dec 2000.
80 #define TOOL_NAME "GenDepex"
89 PrintGenDepexUtilityInfo (
96 Displays the standard utility information to SDTOUT.
109 "%s, Tiano Dependency Expression Generation Utility. Version %d.%d.\n",
111 UTILITY_MAJOR_VERSION
,
112 UTILITY_MINOR_VERSION
114 printf ("Copyright (C) 1996-2002 Intel Corporation. All rights reserved.\n\n");
118 PrintGenDepexUsageInfo (
125 Displays the utility usage syntax to STDOUT.
138 "Usage: %s -I <INFILE> -O <OUTFILE> [-P <Optional Boundary for padding up>] \n",
141 printf (" Where:\n");
142 printf (" <INFILE> is the input pre-processed dependency text files name.\n");
143 printf (" <OUTFILE> is the output binary dependency files name.\n");
144 printf (" <Optional Boundary for padding up> is the padding integer value.\n");
145 printf (" This is the boundary to align the output file size to.\n");
156 Pop an element from the Opcode stack.
160 Stack Current top of the OpCode stack location
164 DEPENDENCY_OPCODE OpCode at the top of the OpCode stack.
165 Stack New top of the OpCode stack location
170 DEPENDENCY_OPCODE
*OpCodePtr
;
181 IN DEPENDENCY_OPCODE OpCode
187 Push an element onto the Opcode Stack
191 Stack Current top of the OpCode stack location
192 OpCode OpCode to push onto the stack
196 Stack New top of the OpCode stack location
200 DEPENDENCY_OPCODE
*OpCodePtr
;
209 GenerateDependencyExpression (
211 IN OUT
FILE *OutFile
,
212 IN INT8 Padding OPTIONAL
218 This takes the pre-compiled dependency text file and
219 converts it into a binary dependency file.
221 The BNF for the dependency expression is as follows
222 (from the DXE 1.0 Draft specification).
224 The inputted BNF grammar is thus:
225 <depex> ::= sor <dep> |
232 <bool> ::= <bool> and <term> |
236 <term> ::= not <factor> |
239 <factor> ::= ( <bool> ) |
247 The outputed binary grammer is thus:
248 <depex> ::= sor <dep> |
249 before <depinst> <dep> |
250 after <depinst> <dep> |
255 <bool> ::= <bool> and <term> |
256 <bool> or <term> | <term>
258 <term> ::= not <factor> |
261 <factor> ::= ( <bool> ) |
270 <depinst> ::= push GUID
274 BugBug: A correct grammer is parsed correctly. A file that violates the
275 grammer may parse when it should generate an error. There is some
276 error checking and it covers most of the case when it's an include
277 of definition issue. An ill formed expresion may not be detected.
281 InFile - Input pre-compiled text file of the dependency expression.
282 This needs to be in ASCII.
283 The file pointer can not be NULL.
285 OutFile - Binary dependency file.
286 The file pointer can not be NULL.
288 Padding - OPTIONAL integer value to pad the output file to.
293 EFI_SUCCESS The function completed successfully.
294 EFI_INVALID_PARAMETER One of the parameters in the text file was invalid.
295 EFI_OUT_OF_RESOURCES Unable to allocate memory.
296 EFI_ABORTED An misc error occurred.
302 INT8
*EvaluationStack
;
316 UINTN ArgCountParsed
;
317 DEPENDENCY_OPCODE Opcode
;
324 memset (Line
, 0, LINESIZE
);
328 EvaluationStack
= (INT8
*) malloc (EVAL_STACK_SIZE
);
330 if (EvaluationStack
!= NULL
) {
331 StackPtr
= EvaluationStack
;
333 printf ("Unable to allocate memory to EvaluationStack - Out of resources\n");
334 return EFI_OUT_OF_RESOURCES
;
337 Results
= (UINTN
) fseek (InFile
, 0, SEEK_END
);
340 printf ("FSEEK failed - Aborted\n");
344 FileSize
= ftell (InFile
);
346 if (FileSize
== -1L) {
347 printf ("FTELL failed - Aborted\n");
351 Buffer
= (INT8
*) malloc (FileSize
+ BUFFER_SIZE
);
353 if (Buffer
== NULL
) {
354 printf ("Unable to allocate memory to Buffer - Out of resources\n");
355 free (EvaluationStack
);
357 Results
= (UINTN
) fclose (InFile
);
359 printf ("FCLOSE failed\n");
362 Results
= (UINTN
) fclose (OutFile
);
364 printf ("FCLOSE failed\n");
367 return EFI_OUT_OF_RESOURCES
;
370 Results
= (UINTN
) fseek (InFile
, 0, SEEK_SET
);
373 printf ("FSEEK failed - Aborted\n");
377 memset (Buffer
, 0, FileSize
+ BUFFER_SIZE
);
378 fread (Buffer
, FileSize
, 1, InFile
);
381 Pend
= Ptrx
+ FileSize
- strlen (DEPENDENCY_END
);
385 while ((Index
--) && NotDone
) {
387 if (strncmp (Pend
, DEPENDENCY_END
, strlen (DEPENDENCY_END
)) == 0) {
395 printf ("Couldn't find end string %s\n", DEPENDENCY_END
);
397 Results
= (UINTN
) fclose (InFile
);
399 printf ("FCLOSE failed\n");
402 Results
= (UINTN
) fclose (OutFile
);
404 printf ("FCLOSE failed\n");
408 free (EvaluationStack
);
410 return EFI_INVALID_PARAMETER
;
416 while ((Index
--) && NotDone
) {
418 if (strncmp (Ptrx
, DEPENDENCY_START
, strlen (DEPENDENCY_START
)) == 0) {
419 Ptrx
+= sizeof (DEPENDENCY_START
);
422 // BUGBUG -- should Index be decremented by sizeof(DEPENDENCY_START)?
430 printf ("Couldn't find start string %s\n", DEPENDENCY_START
);
432 Results
= (UINTN
) fclose (InFile
);
434 printf ("FCLOSE failed\n");
437 Results
= (UINTN
) fclose (OutFile
);
439 printf ("FCLOSE failed\n");
443 free (EvaluationStack
);
445 return EFI_INVALID_PARAMETER
;
448 // validate the syntax of expression
450 if (!ParseDepex (Ptrx
, Pend
- Ptrx
- 1)) {
451 printf ("The syntax of expression is wrong\n");
453 Results
= (UINTN
) fclose (InFile
);
455 printf ("FCLOSE failed\n");
458 Results
= (UINTN
) fclose (OutFile
);
460 printf ("FCLOSE failed\n");
464 free (EvaluationStack
);
466 return EFI_INVALID_PARAMETER
;
471 while ((Index
--) && NotDone
) {
475 } else if (*Ptrx
== '\n' || *Ptrx
== '\r') {
477 } else if (strncmp (Ptrx
, OPERATOR_SOR
, strlen (OPERATOR_SOR
)) == 0) {
479 // Checks for some invalid dependencies
483 printf ("A BEFORE operator was detected.\n");
484 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
485 return EFI_INVALID_PARAMETER
;
487 } else if (After_Flag
) {
489 printf ("An AFTER operator was detected.\n");
490 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
491 return EFI_INVALID_PARAMETER
;
493 } else if (SOR_Flag
) {
495 printf ("Another SOR operator was detected.\n");
496 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
497 return EFI_INVALID_PARAMETER
;
499 } else if (Dep_Flag
) {
501 printf ("The Schedule On Request - SOR operator must be the first operator following DEPENDENCY_START\n");
502 return EFI_INVALID_PARAMETER
;
506 // BUGBUG - This was not in the spec but is in the CORE code
507 // An OPERATOR_SOR has to be first - following the DEPENDENCY_START
509 fputc (EFI_DEP_SOR
, OutFile
);
511 Ptrx
+= sizeof (OPERATOR_SOR
);
515 } else if (strncmp (Ptrx
, OPERATOR_BEFORE
, strlen (OPERATOR_BEFORE
)) == 0) {
517 // Checks for some invalid dependencies
521 printf ("Another BEFORE operator was detected.\n");
522 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
523 return EFI_INVALID_PARAMETER
;
525 } else if (After_Flag
) {
527 printf ("An AFTER operator was detected.\n");
528 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
529 return EFI_INVALID_PARAMETER
;
531 } else if (SOR_Flag
) {
533 printf ("A SOR operator was detected.\n");
534 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
535 return EFI_INVALID_PARAMETER
;
537 } else if (Dep_Flag
) {
539 printf ("The BEFORE operator must be the first operator following DEPENDENCY_START\n");
540 return EFI_INVALID_PARAMETER
;
543 fputc (EFI_DEP_BEFORE
, OutFile
);
545 Ptrx
+= sizeof (OPERATOR_BEFORE
);
548 } else if (strncmp (Ptrx
, OPERATOR_AFTER
, strlen (OPERATOR_AFTER
)) == 0) {
550 // Checks for some invalid dependencies
554 printf ("A BEFORE operator was detected.\n");
555 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
556 return EFI_INVALID_PARAMETER
;
558 } else if (After_Flag
) {
560 printf ("Another AFTER operator was detected.\n");
561 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
562 return EFI_INVALID_PARAMETER
;
564 } else if (SOR_Flag
) {
566 printf ("A SOR operator was detected.\n");
567 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");
568 return EFI_INVALID_PARAMETER
;
570 } else if (Dep_Flag
) {
572 printf ("The AFTER operator must be the first operator following DEPENDENCY_START\n");
573 return EFI_INVALID_PARAMETER
;
576 fputc (EFI_DEP_AFTER
, OutFile
);
578 Ptrx
+= sizeof (OPERATOR_AFTER
);
582 } else if (strncmp (Ptrx
, OPERATOR_AND
, strlen (OPERATOR_AND
)) == 0) {
583 while (StackPtr
!= EvaluationStack
) {
584 Opcode
= PopOpCode ((VOID
**) &StackPtr
);
585 if (Opcode
!= DXE_DEP_LEFT_PARENTHESIS
) {
586 fputc (Opcode
, OutFile
);
589 PushOpCode ((VOID
**) &StackPtr
, DXE_DEP_LEFT_PARENTHESIS
);
594 PushOpCode ((VOID
**) &StackPtr
, EFI_DEP_AND
);
595 Ptrx
+= sizeof (OPERATOR_AND
);
598 } else if (strncmp (Ptrx
, OPERATOR_OR
, strlen (OPERATOR_OR
)) == 0) {
599 while (StackPtr
!= EvaluationStack
) {
600 Opcode
= PopOpCode ((VOID
**) &StackPtr
);
601 if (Opcode
!= DXE_DEP_LEFT_PARENTHESIS
) {
602 fputc (Opcode
, OutFile
);
605 PushOpCode ((VOID
**) &StackPtr
, DXE_DEP_LEFT_PARENTHESIS
);
610 PushOpCode ((VOID
**) &StackPtr
, EFI_DEP_OR
);
611 Ptrx
+= sizeof (OPERATOR_OR
);
614 } else if (strncmp (Ptrx
, OPERATOR_NOT
, strlen (OPERATOR_NOT
)) == 0) {
615 while (StackPtr
!= EvaluationStack
) {
616 Opcode
= PopOpCode ((VOID
**) &StackPtr
);
617 if (Opcode
!= DXE_DEP_LEFT_PARENTHESIS
) {
618 fputc (Opcode
, OutFile
);
621 PushOpCode ((VOID
**) &StackPtr
, DXE_DEP_LEFT_PARENTHESIS
);
626 PushOpCode ((VOID
**) &StackPtr
, EFI_DEP_NOT
);
627 Ptrx
+= sizeof (OPERATOR_NOT
);
630 } else if (*Ptrx
== '\t') {
632 printf ("File contains tabs. This violates the coding standard\n");
633 return EFI_INVALID_PARAMETER
;
635 } else if (*Ptrx
== '\n') {
637 // Skip the newline character in the file
641 } else if (strncmp (Ptrx
, OPERATOR_LEFT_PARENTHESIS
, strlen (OPERATOR_LEFT_PARENTHESIS
)) == 0) {
642 PushOpCode ((VOID
**) &StackPtr
, DXE_DEP_LEFT_PARENTHESIS
);
644 Ptrx
+= strlen (OPERATOR_LEFT_PARENTHESIS
);
647 } else if (strncmp (Ptrx
, OPERATOR_RIGHT_PARENTHESIS
, strlen (OPERATOR_RIGHT_PARENTHESIS
)) == 0) {
648 while (StackPtr
!= EvaluationStack
) {
649 Opcode
= PopOpCode ((VOID
**) &StackPtr
);
650 if (Opcode
!= DXE_DEP_LEFT_PARENTHESIS
) {
651 fputc (Opcode
, OutFile
);
658 Ptrx
+= strlen (OPERATOR_RIGHT_PARENTHESIS
);
661 } else if (strncmp (Ptrx
, OPERATOR_TRUE
, strlen (OPERATOR_TRUE
)) == 0) {
663 fputc (EFI_DEP_TRUE
, OutFile
);
668 // OutFileSize += sizeof (EFI_DEP_TRUE);
672 Ptrx
+= strlen (OPERATOR_TRUE
);
674 } else if (strncmp (Ptrx
, OPERATOR_FALSE
, strlen (OPERATOR_FALSE
)) == 0) {
676 fputc (EFI_DEP_FALSE
, OutFile
);
681 // OutFileSize += sizeof (EFI_DEP_FALSE);
685 Ptrx
+= strlen (OPERATOR_FALSE
);
687 } else if (*Ptrx
== '{') {
696 // This is an array of UINT32s. sscanf will trash memory
697 // if you try to read into a UINT8 with a %x formatter.
698 UINT32 Guid_Data4
[8];
700 ArgCountParsed
= sscanf (
702 "%x, %x, %x, { %x, %x, %x, %x, %x, %x, %x, %x }",
716 // Now we can copy the 32 bit ints into the GUID.
717 for (byte_index
=0; byte_index
<8; byte_index
++) {
718 Guid
.Data4
[byte_index
] = (UINT8
) Guid_Data4
[byte_index
];
722 if (ArgCountParsed
!= 11) {
723 printf ("We have found an illegal GUID\n");
724 printf ("Fix your depex\n");
728 while (*Ptrx
!= '}') {
733 while (*Ptrx
!= '}') {
737 // Absorb the closing }
742 // Don't provide a PUSH Opcode for the Before and After case
744 if ((!Before_Flag
) && (!After_Flag
)) {
745 fputc (EFI_DEP_PUSH
, OutFile
);
749 fwrite (&Guid
, sizeof (EFI_GUID
), 1, OutFile
);
751 OutFileSize
+= sizeof (EFI_GUID
);
754 } else if (strncmp (Ptrx
, DEPENDENCY_END
, strlen (DEPENDENCY_END
)) == 0) {
758 // Not a valid construct. Null terminate somewhere out there and
759 // print an error message.
762 printf (TOOL_NAME
" ERROR: Unrecognized input at: \"%s\"...\n", Ptrx
);
763 return EFI_INVALID_PARAMETER
;
769 while (StackPtr
!= EvaluationStack
) {
770 fputc (PopOpCode ((VOID
**) &StackPtr
), OutFile
);
774 if (OutFileSize
== 0) {
775 printf ("Grammer contains no operators or constants\n");
776 return EFI_INVALID_PARAMETER
;
779 fputc (EFI_DEP_END
, OutFile
);
784 // Checks for invalid padding values
788 printf ("The inputted padding value was %d\n", Padding
);
789 printf ("The optional padding value can not be less than ZERO\n");
790 return EFI_INVALID_PARAMETER
;
792 } else if (Padding
> 0) {
794 while ((OutFileSize
% Padding
) != 0) {
796 fputc (' ', OutFile
);
801 Results
= (UINTN
) fclose (InFile
);
803 printf ("FCLOSE failed\n");
806 Results
= (UINTN
) fclose (OutFile
);
808 printf ("FCLOSE failed\n");
812 free (EvaluationStack
);
815 } // End GenerateDependencyExpression function
826 Parse user entries. Print some rudimentary help
830 argc The count of input arguments
831 argv The input arguments string array
835 EFI_SUCCESS The function completed successfully.
836 EFI_INVALID_PARAMETER One of the input parameters was invalid or one of the parameters in the text file was invalid.
837 EFI_OUT_OF_RESOURCES Unable to allocate memory.
838 EFI_ABORTED Unable to open/create a file or a misc error.
841 // TODO: ] - add argument and description to function comment
859 printf ("Not enough arguments\n");
860 PrintGenDepexUsageInfo ();
861 return EFI_INVALID_PARAMETER
;
864 for (Index
= 1; Index
< argc
- 1; Index
++) {
866 if ((strcmp (argv
[Index
], "-I") == 0) || (strcmp (argv
[Index
], "-i") == 0)) {
870 InFile
= fopen (argv
[Index
+ 1], "rb");
874 printf ("GenDepex only allows one INPUT (-I) argument\n");
875 return EFI_INVALID_PARAMETER
;
878 } else if ((strcmp (argv
[Index
], "-O") == 0) || (strcmp (argv
[Index
], "-o") == 0)) {
882 OutFile
= fopen (argv
[Index
+ 1], "wb");
886 printf ("GenDepex only allows one OUTPUT (-O) argument\n");
887 return EFI_INVALID_PARAMETER
;
890 } else if ((strcmp (argv
[Index
], "-P") == 0) || (strcmp (argv
[Index
], "-p") == 0)) {
894 Padding
= (UINT8
) atoi (argv
[Index
+ 1]);
898 printf ("GenDepex only allows one PADDING (-P) argument\n");
899 return EFI_INVALID_PARAMETER
;
904 PrintGenDepexUtilityInfo ();
906 if (InFile
== NULL
) {
907 printf ("Can not open <INFILE> for reading.\n");
908 PrintGenDepexUsageInfo ();
912 if (OutFile
== NULL
) {
913 printf ("Can not open <OUTFILE> for writting.\n");
914 PrintGenDepexUsageInfo ();
918 return GenerateDependencyExpression (InFile
, OutFile
, Padding
);