Added code to clear "Buffer" before using it. This is supposed to fix the odd issue...
[mirror_edk2.git] / Tools / Source / TianoTools / GenDepex / GenDepex.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2004, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 GenDepex.c\r
15\r
16Abstract:\r
17\r
18 Generate Dependency Expression ("GenDepex")\r
19\r
20 Infix to Postfix Algorithm\r
21\r
22 This code has been scrubbed to be free of having any EFI core tree dependencies.\r
23 It should build in any environment that supports a standard C-library w/ string\r
24 operations and File I/O services.\r
25\r
26 As an example of usage, consider the following:\r
27\r
28 The input user file could be something like "Sample.DXS" whose contents are\r
29\r
30 #include "Tiano.h"\r
31\r
32 DEPENDENCY_START\r
33 NOT (DISK_IO_PROTOCOL AND SIMPLE_FILE_SYSTEM_PROTOCOL) \r
34 OR EFI_PXE_BASE_CODE_PROTOCOL\r
35 DEPENDENCY_END\r
36\r
37 This file is then washed through the C-preprocessor, viz.,\r
38\r
39 cl /EP Sample.DXS > Sample.TMP1\r
40\r
41 This yields the following file "Sample.TMP1" whose contents are\r
42\r
43 DEPENDENCY_START\r
44 NOT ({ 0xce345171, 0xba0b, 0x11d2, 0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72,\r
45 0x3b } AND { 0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69,\r
46 0x72, 0x3b }) OR { 0x03c4e603, 0xac28, 0x11d3, 0x9a, 0x2d, 0x00, 0x90, 0x27,\r
47 0x3f, 0xc1, 0x4d }\r
48 DEPENDENCY_END\r
49\r
50 This file, in turn, will be fed into the utility, viz.,\r
51\r
52 GenDepex Sample.TMP1 Sample.TMP2\r
53\r
54 With a file that is 55 bytes long:\r
55\r
56 55 bytes for the grammar binary\r
57 PUSH opcode - 1 byte\r
58 GUID Instance - 16 bytes\r
59 PUSH opcode - 1 byte\r
60 GUID Instance - 16 bytes\r
61 AND opcode - 1 byte\r
62 NOT opcode - 1 byte\r
63 PUSH opcode - 1 byte\r
64 GUID Instance - 16 bytes\r
65 OR opcode - 1 byte\r
66 END opcode - 1 byte\r
67\r
68 The file "Sample.TMP2" could be fed via a Section-builder utility \r
69 (GenSection) that would be used for the creation of a dependency\r
70 section file (.DPX) which in turn would be used by a generate FFS\r
71 utility (GenFfsFile) to produce a DXE driver/core (.DXE) or \r
72 a DXE application (.APP) file.\r
73\r
74 Complies with Tiano C Coding Standards Document, version 0.31, 12 Dec 2000.\r
75\r
76--*/\r
77\r
78#include "GenDepex.h"\r
79\r
80#define TOOL_NAME "GenDepex"\r
81\r
82extern\r
83ParseDepex (\r
84 IN INT8 *Pbegin,\r
85 IN UINT32 length\r
86 );\r
87\r
88VOID\r
89PrintGenDepexUtilityInfo (\r
90 VOID\r
91 )\r
92/*++\r
93\r
94Routine Description:\r
95\r
96 Displays the standard utility information to SDTOUT.\r
97\r
98Arguments:\r
99\r
100 None\r
101\r
102Returns:\r
103\r
104 None\r
105\r
106--*/\r
107{\r
108 printf (\r
109 "%s, Tiano Dependency Expression Generation Utility. Version %d.%d.\n",\r
110 UTILITY_NAME,\r
111 UTILITY_MAJOR_VERSION,\r
112 UTILITY_MINOR_VERSION\r
113 );\r
114 printf ("Copyright (C) 1996-2002 Intel Corporation. All rights reserved.\n\n");\r
115}\r
116\r
117VOID\r
118PrintGenDepexUsageInfo (\r
119 VOID\r
120 )\r
121/*++\r
122\r
123Routine Description:\r
124\r
125 Displays the utility usage syntax to STDOUT.\r
126\r
127Arguments:\r
128\r
129 None\r
130\r
131Returns:\r
132\r
133 None\r
134\r
135--*/\r
136{\r
137 printf (\r
138 "Usage: %s -I <INFILE> -O <OUTFILE> [-P <Optional Boundary for padding up>] \n",\r
139 UTILITY_NAME\r
140 );\r
141 printf (" Where:\n");\r
142 printf (" <INFILE> is the input pre-processed dependency text files name.\n");\r
143 printf (" <OUTFILE> is the output binary dependency files name.\n");\r
144 printf (" <Optional Boundary for padding up> is the padding integer value.\n");\r
145 printf (" This is the boundary to align the output file size to.\n");\r
146}\r
147\r
148DEPENDENCY_OPCODE\r
149PopOpCode (\r
150 IN OUT VOID **Stack\r
151 )\r
152/*++\r
153\r
154Routine Description:\r
155\r
156 Pop an element from the Opcode stack.\r
157\r
158Arguments:\r
159\r
160 Stack Current top of the OpCode stack location\r
161\r
162Returns:\r
163\r
164 DEPENDENCY_OPCODE OpCode at the top of the OpCode stack.\r
165 Stack New top of the OpCode stack location\r
166\r
167\r
168--*/\r
169{\r
170 DEPENDENCY_OPCODE *OpCodePtr;\r
171\r
172 OpCodePtr = *Stack;\r
173 OpCodePtr--;\r
174 *Stack = OpCodePtr;\r
175 return *OpCodePtr;\r
176}\r
177\r
178VOID\r
179PushOpCode (\r
180 IN OUT VOID **Stack,\r
181 IN DEPENDENCY_OPCODE OpCode\r
182 )\r
183/*++\r
184\r
185Routine Description:\r
186\r
187 Push an element onto the Opcode Stack\r
188\r
189Arguments:\r
190\r
191 Stack Current top of the OpCode stack location\r
192 OpCode OpCode to push onto the stack\r
193\r
194Returns:\r
195\r
196 Stack New top of the OpCode stack location\r
197\r
198--*/\r
199{\r
200 DEPENDENCY_OPCODE *OpCodePtr;\r
201\r
202 OpCodePtr = *Stack;\r
203 *OpCodePtr = OpCode;\r
204 OpCodePtr++;\r
205 *Stack = OpCodePtr;\r
206}\r
207\r
208EFI_STATUS\r
209GenerateDependencyExpression (\r
210 IN FILE *InFile,\r
211 IN OUT FILE *OutFile,\r
9776330c 212 IN INT8 Padding OPTIONAL\r
878ddf1f 213 )\r
214/*++\r
215\r
216Routine Description:\r
217\r
218 This takes the pre-compiled dependency text file and \r
219 converts it into a binary dependency file.\r
220\r
221 The BNF for the dependency expression is as follows \r
222 (from the DXE 1.0 Draft specification).\r
223\r
224 The inputted BNF grammar is thus:\r
225 <depex> ::= sor <dep> | \r
226 before GUID <dep> | \r
227 after GUID <dep> | \r
228 <bool>\r
229\r
230 <dep> ::= <bool> |\r
231\r
232 <bool> ::= <bool> and <term> | \r
233 <bool> or <term> | \r
234 <term>\r
235\r
236 <term> ::= not <factor> | \r
237 <factor>\r
238\r
239 <factor> ::= ( <bool> ) | \r
240 <term> <term> | \r
241 GUID | \r
242 <boolval>\r
243\r
244 <boolval> ::= true | \r
245 false\r
246\r
247 The outputed binary grammer is thus:\r
248 <depex> ::= sor <dep> | \r
249 before <depinst> <dep> | \r
250 after <depinst> <dep> | \r
251 <bool>\r
252\r
253 <dep> ::= <bool> |\r
254\r
255 <bool> ::= <bool> and <term> | \r
256 <bool> or <term> | <term>\r
257\r
258 <term> ::= not <factor> | \r
259 <factor>\r
260\r
261 <factor> ::= ( <bool> ) | \r
262 <term> <term> | \r
263 <boolval> | \r
264 <depinst> | \r
265 <termval>\r
266\r
267 <boolval> ::= true | \r
268 false\r
269\r
270 <depinst> ::= push GUID\r
271\r
272 <termval> ::= end\r
273\r
274 BugBug: A correct grammer is parsed correctly. A file that violates the\r
275 grammer may parse when it should generate an error. There is some\r
276 error checking and it covers most of the case when it's an include\r
277 of definition issue. An ill formed expresion may not be detected.\r
278\r
279Arguments:\r
280\r
281 InFile - Input pre-compiled text file of the dependency expression.\r
282 This needs to be in ASCII.\r
283 The file pointer can not be NULL.\r
284\r
285 OutFile - Binary dependency file.\r
286 The file pointer can not be NULL.\r
287\r
288 Padding - OPTIONAL integer value to pad the output file to.\r
289\r
290\r
291Returns:\r
292\r
293 EFI_SUCCESS The function completed successfully.\r
294 EFI_INVALID_PARAMETER One of the parameters in the text file was invalid.\r
295 EFI_OUT_OF_RESOURCES Unable to allocate memory.\r
296 EFI_ABORTED An misc error occurred.\r
297\r
298--*/\r
299{\r
300 INT8 *Ptrx;\r
301 INT8 *Pend;\r
302 INT8 *EvaluationStack;\r
303 INT8 *StackPtr;\r
304 INT8 *Buffer;\r
305 INT8 Line[LINESIZE];\r
306 UINTN Index;\r
307 UINTN OutFileSize;\r
308 UINTN FileSize;\r
309 UINTN Results;\r
310 BOOLEAN NotDone;\r
311 BOOLEAN Before_Flag;\r
312 BOOLEAN After_Flag;\r
313 BOOLEAN Dep_Flag;\r
314 BOOLEAN SOR_Flag;\r
315 EFI_GUID Guid;\r
316 UINTN ArgCountParsed;\r
317 DEPENDENCY_OPCODE Opcode;\r
318\r
319 Before_Flag = FALSE;\r
320 After_Flag = FALSE;\r
321 Dep_Flag = FALSE;\r
322 SOR_Flag = FALSE;\r
323\r
324 memset (Line, 0, LINESIZE);\r
325\r
326 OutFileSize = 0;\r
327\r
328 EvaluationStack = (INT8 *) malloc (EVAL_STACK_SIZE);\r
329\r
330 if (EvaluationStack != NULL) {\r
331 StackPtr = EvaluationStack;\r
332 } else {\r
333 printf ("Unable to allocate memory to EvaluationStack - Out of resources\n");\r
334 return EFI_OUT_OF_RESOURCES;\r
335 }\r
336\r
337 Results = (UINTN) fseek (InFile, 0, SEEK_END);\r
338\r
339 if (Results != 0) {\r
340 printf ("FSEEK failed - Aborted\n");\r
341 return EFI_ABORTED;\r
342 }\r
343\r
344 FileSize = ftell (InFile);\r
345\r
346 if (FileSize == -1L) {\r
347 printf ("FTELL failed - Aborted\n");\r
348 return EFI_ABORTED;\r
349 }\r
350\r
351 Buffer = (INT8 *) malloc (FileSize + BUFFER_SIZE);\r
352\r
353 if (Buffer == NULL) {\r
354 printf ("Unable to allocate memory to Buffer - Out of resources\n");\r
355 free (EvaluationStack);\r
356\r
357 Results = (UINTN) fclose (InFile);\r
358 if (Results != 0) {\r
359 printf ("FCLOSE failed\n");\r
360 }\r
361\r
362 Results = (UINTN) fclose (OutFile);\r
363 if (Results != 0) {\r
364 printf ("FCLOSE failed\n");\r
365 }\r
366\r
367 return EFI_OUT_OF_RESOURCES;\r
368 }\r
369\r
370 Results = (UINTN) fseek (InFile, 0, SEEK_SET);\r
371\r
372 if (Results != 0) {\r
373 printf ("FSEEK failed - Aborted\n");\r
374 return EFI_ABORTED;\r
375 }\r
376\r
a7aa21c9 377 memset (Buffer, 0, FileSize + BUFFER_SIZE);\r
878ddf1f 378 fread (Buffer, FileSize, 1, InFile);\r
379\r
380 Ptrx = Buffer;\r
381 Pend = Ptrx + FileSize - strlen (DEPENDENCY_END);\r
382 Index = FileSize;\r
383\r
384 NotDone = TRUE;\r
385 while ((Index--) && NotDone) {\r
386\r
387 if (strncmp (Pend, DEPENDENCY_END, strlen (DEPENDENCY_END)) == 0) {\r
388 NotDone = FALSE;\r
389 } else {\r
390 Pend--;\r
391 }\r
392 }\r
393\r
394 if (NotDone) {\r
395 printf ("Couldn't find end string %s\n", DEPENDENCY_END);\r
396\r
397 Results = (UINTN) fclose (InFile);\r
398 if (Results != 0) {\r
399 printf ("FCLOSE failed\n");\r
400 }\r
401\r
402 Results = (UINTN) fclose (OutFile);\r
403 if (Results != 0) {\r
404 printf ("FCLOSE failed\n");\r
405 }\r
406\r
407 free (Buffer);\r
408 free (EvaluationStack);\r
409\r
410 return EFI_INVALID_PARAMETER;\r
411 }\r
412\r
413 Index = FileSize;\r
414\r
415 NotDone = TRUE;\r
416 while ((Index--) && NotDone) {\r
417\r
418 if (strncmp (Ptrx, DEPENDENCY_START, strlen (DEPENDENCY_START)) == 0) {\r
419 Ptrx += sizeof (DEPENDENCY_START);\r
420 NotDone = FALSE;\r
421 //\r
422 // BUGBUG -- should Index be decremented by sizeof(DEPENDENCY_START)?\r
423 //\r
424 } else {\r
425 Ptrx++;\r
426 }\r
427 }\r
428\r
429 if (NotDone) {\r
430 printf ("Couldn't find start string %s\n", DEPENDENCY_START);\r
431\r
432 Results = (UINTN) fclose (InFile);\r
433 if (Results != 0) {\r
434 printf ("FCLOSE failed\n");\r
435 }\r
436\r
437 Results = (UINTN) fclose (OutFile);\r
438 if (Results != 0) {\r
439 printf ("FCLOSE failed\n");\r
440 }\r
441\r
442 free (Buffer);\r
443 free (EvaluationStack);\r
444\r
445 return EFI_INVALID_PARAMETER;\r
446 }\r
447 //\r
448 // validate the syntax of expression\r
449 //\r
450 if (!ParseDepex (Ptrx, Pend - Ptrx - 1)) {\r
451 printf ("The syntax of expression is wrong\n");\r
452\r
453 Results = (UINTN) fclose (InFile);\r
454 if (Results != 0) {\r
455 printf ("FCLOSE failed\n");\r
456 }\r
457\r
458 Results = (UINTN) fclose (OutFile);\r
459 if (Results != 0) {\r
460 printf ("FCLOSE failed\n");\r
461 }\r
462\r
463 free (Buffer);\r
464 free (EvaluationStack);\r
465\r
466 return EFI_INVALID_PARAMETER;\r
467 }\r
468\r
469 NotDone = TRUE;\r
470\r
471 while ((Index--) && NotDone) {\r
472\r
473 if (*Ptrx == ' ') {\r
474 Ptrx++;\r
475 } else if (*Ptrx == '\n' || *Ptrx == '\r') {\r
476 Ptrx++;\r
477 } else if (strncmp (Ptrx, OPERATOR_SOR, strlen (OPERATOR_SOR)) == 0) {\r
478 //\r
479 // Checks for some invalid dependencies\r
480 //\r
481 if (Before_Flag) {\r
482\r
483 printf ("A BEFORE operator was detected.\n");\r
484 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
485 return EFI_INVALID_PARAMETER;\r
486\r
487 } else if (After_Flag) {\r
488\r
489 printf ("An AFTER operator was detected.\n");\r
490 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
491 return EFI_INVALID_PARAMETER;\r
492\r
493 } else if (SOR_Flag) {\r
494\r
495 printf ("Another SOR operator was detected.\n");\r
496 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
497 return EFI_INVALID_PARAMETER;\r
498\r
499 } else if (Dep_Flag) {\r
500\r
501 printf ("The Schedule On Request - SOR operator must be the first operator following DEPENDENCY_START\n");\r
502 return EFI_INVALID_PARAMETER;\r
503\r
504 } else {\r
505 //\r
506 // BUGBUG - This was not in the spec but is in the CORE code\r
507 // An OPERATOR_SOR has to be first - following the DEPENDENCY_START\r
508 //\r
509 fputc (EFI_DEP_SOR, OutFile);\r
510 OutFileSize++;\r
511 Ptrx += sizeof (OPERATOR_SOR);\r
512 SOR_Flag = TRUE;\r
513\r
514 }\r
515 } else if (strncmp (Ptrx, OPERATOR_BEFORE, strlen (OPERATOR_BEFORE)) == 0) {\r
516 //\r
517 // Checks for some invalid dependencies\r
518 //\r
519 if (Before_Flag) {\r
520\r
521 printf ("Another BEFORE operator was detected.\n");\r
522 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
523 return EFI_INVALID_PARAMETER;\r
524\r
525 } else if (After_Flag) {\r
526\r
527 printf ("An AFTER operator was detected.\n");\r
528 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
529 return EFI_INVALID_PARAMETER;\r
530\r
531 } else if (SOR_Flag) {\r
532\r
533 printf ("A SOR operator was detected.\n");\r
534 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
535 return EFI_INVALID_PARAMETER;\r
536\r
537 } else if (Dep_Flag) {\r
538\r
539 printf ("The BEFORE operator must be the first operator following DEPENDENCY_START\n");\r
540 return EFI_INVALID_PARAMETER;\r
541\r
542 } else {\r
543 fputc (EFI_DEP_BEFORE, OutFile);\r
544 OutFileSize++;\r
545 Ptrx += sizeof (OPERATOR_BEFORE);\r
546 Before_Flag = TRUE;\r
547 }\r
548 } else if (strncmp (Ptrx, OPERATOR_AFTER, strlen (OPERATOR_AFTER)) == 0) {\r
549 //\r
550 // Checks for some invalid dependencies\r
551 //\r
552 if (Before_Flag) {\r
553\r
554 printf ("A BEFORE operator was detected.\n");\r
555 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
556 return EFI_INVALID_PARAMETER;\r
557\r
558 } else if (After_Flag) {\r
559\r
560 printf ("Another AFTER operator was detected.\n");\r
561 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
562 return EFI_INVALID_PARAMETER;\r
563\r
564 } else if (SOR_Flag) {\r
565\r
566 printf ("A SOR operator was detected.\n");\r
567 printf ("There can only be one SOR or one AFTER or one BEFORE operator\n");\r
568 return EFI_INVALID_PARAMETER;\r
569\r
570 } else if (Dep_Flag) {\r
571\r
572 printf ("The AFTER operator must be the first operator following DEPENDENCY_START\n");\r
573 return EFI_INVALID_PARAMETER;\r
574\r
575 } else {\r
576 fputc (EFI_DEP_AFTER, OutFile);\r
577 OutFileSize++;\r
578 Ptrx += sizeof (OPERATOR_AFTER);\r
579 Dep_Flag = TRUE;\r
580 After_Flag = TRUE;\r
581 }\r
582 } else if (strncmp (Ptrx, OPERATOR_AND, strlen (OPERATOR_AND)) == 0) {\r
583 while (StackPtr != EvaluationStack) {\r
584 Opcode = PopOpCode ((VOID **) &StackPtr);\r
585 if (Opcode != DXE_DEP_LEFT_PARENTHESIS) {\r
586 fputc (Opcode, OutFile);\r
587 OutFileSize++;\r
588 } else {\r
589 PushOpCode ((VOID **) &StackPtr, DXE_DEP_LEFT_PARENTHESIS);\r
590 break;\r
591 }\r
592 }\r
593\r
594 PushOpCode ((VOID **) &StackPtr, EFI_DEP_AND);\r
595 Ptrx += sizeof (OPERATOR_AND);\r
596 Dep_Flag = TRUE;\r
597\r
598 } else if (strncmp (Ptrx, OPERATOR_OR, strlen (OPERATOR_OR)) == 0) {\r
599 while (StackPtr != EvaluationStack) {\r
600 Opcode = PopOpCode ((VOID **) &StackPtr);\r
601 if (Opcode != DXE_DEP_LEFT_PARENTHESIS) {\r
602 fputc (Opcode, OutFile);\r
603 OutFileSize++;\r
604 } else {\r
605 PushOpCode ((VOID **) &StackPtr, DXE_DEP_LEFT_PARENTHESIS);\r
606 break;\r
607 }\r
608 }\r
609\r
610 PushOpCode ((VOID **) &StackPtr, EFI_DEP_OR);\r
611 Ptrx += sizeof (OPERATOR_OR);\r
612 Dep_Flag = TRUE;\r
613\r
614 } else if (strncmp (Ptrx, OPERATOR_NOT, strlen (OPERATOR_NOT)) == 0) {\r
615 while (StackPtr != EvaluationStack) {\r
616 Opcode = PopOpCode ((VOID **) &StackPtr);\r
617 if (Opcode != DXE_DEP_LEFT_PARENTHESIS) {\r
618 fputc (Opcode, OutFile);\r
619 OutFileSize++;\r
620 } else {\r
621 PushOpCode ((VOID **) &StackPtr, DXE_DEP_LEFT_PARENTHESIS);\r
622 break;\r
623 }\r
624 }\r
625\r
626 PushOpCode ((VOID **) &StackPtr, EFI_DEP_NOT);\r
627 Ptrx += sizeof (OPERATOR_NOT);\r
628 Dep_Flag = TRUE;\r
629\r
630 } else if (*Ptrx == '\t') {\r
631\r
632 printf ("File contains tabs. This violates the coding standard\n");\r
633 return EFI_INVALID_PARAMETER;\r
634\r
635 } else if (*Ptrx == '\n') {\r
636 //\r
637 // Skip the newline character in the file\r
638 //\r
639 Ptrx++;\r
640\r
641 } else if (strncmp (Ptrx, OPERATOR_LEFT_PARENTHESIS, strlen (OPERATOR_LEFT_PARENTHESIS)) == 0) {\r
642 PushOpCode ((VOID **) &StackPtr, DXE_DEP_LEFT_PARENTHESIS);\r
643\r
644 Ptrx += strlen (OPERATOR_LEFT_PARENTHESIS);\r
645 Dep_Flag = TRUE;\r
646\r
647 } else if (strncmp (Ptrx, OPERATOR_RIGHT_PARENTHESIS, strlen (OPERATOR_RIGHT_PARENTHESIS)) == 0) {\r
648 while (StackPtr != EvaluationStack) {\r
649 Opcode = PopOpCode ((VOID **) &StackPtr);\r
650 if (Opcode != DXE_DEP_LEFT_PARENTHESIS) {\r
651 fputc (Opcode, OutFile);\r
652 OutFileSize++;\r
653 } else {\r
654 break;\r
655 }\r
656 }\r
657\r
658 Ptrx += strlen (OPERATOR_RIGHT_PARENTHESIS);\r
659 Dep_Flag = TRUE;\r
660\r
661 } else if (strncmp (Ptrx, OPERATOR_TRUE, strlen (OPERATOR_TRUE)) == 0) {\r
662\r
663 fputc (EFI_DEP_TRUE, OutFile);\r
664\r
665 OutFileSize++;\r
666\r
667 //\r
668 // OutFileSize += sizeof (EFI_DEP_TRUE);\r
669 //\r
670 Dep_Flag = TRUE;\r
671\r
672 Ptrx += strlen (OPERATOR_TRUE);\r
673\r
674 } else if (strncmp (Ptrx, OPERATOR_FALSE, strlen (OPERATOR_FALSE)) == 0) {\r
675\r
676 fputc (EFI_DEP_FALSE, OutFile);\r
677\r
678 OutFileSize++;\r
679\r
680 //\r
681 // OutFileSize += sizeof (EFI_DEP_FALSE);\r
682 //\r
683 Dep_Flag = TRUE;\r
684\r
685 Ptrx += strlen (OPERATOR_FALSE);\r
686\r
687 } else if (*Ptrx == '{') {\r
688 Ptrx++;\r
689\r
690 if (*Ptrx == ' ') {\r
691 Ptrx++;\r
692 }\r
693\r
8d3adb74 694 {\r
695 int byte_index;\r
696 // This is an array of UINT32s. sscanf will trash memory\r
697 // if you try to read into a UINT8 with a %x formatter.\r
698 UINT32 Guid_Data4[8];\r
699\r
700 ArgCountParsed = sscanf (\r
701 Ptrx,\r
702 "%x, %x, %x, { %x, %x, %x, %x, %x, %x, %x, %x }",\r
703 &Guid.Data1,\r
704 &Guid.Data2,\r
705 &Guid.Data3,\r
706 &Guid_Data4[0],\r
707 &Guid_Data4[1],\r
708 &Guid_Data4[2],\r
709 &Guid_Data4[3],\r
710 &Guid_Data4[4],\r
711 &Guid_Data4[5],\r
712 &Guid_Data4[6],\r
713 &Guid_Data4[7]\r
714 );\r
715 \r
716 // Now we can copy the 32 bit ints into the GUID.\r
717 for (byte_index=0; byte_index<8; byte_index++) {\r
718 Guid.Data4[byte_index] = (UINT8) Guid_Data4[byte_index];\r
719 }\r
720 }\r
878ddf1f 721\r
722 if (ArgCountParsed != 11) {\r
723 printf ("We have found an illegal GUID\n");\r
724 printf ("Fix your depex\n");\r
725 exit (-1);\r
726 }\r
727\r
728 while (*Ptrx != '}') {\r
729 Ptrx++;\r
730 }\r
731\r
ce53a8c3 732 Ptrx++;\r
878ddf1f 733 while (*Ptrx != '}') {\r
734 Ptrx++;\r
735 }\r
ce53a8c3 736 //\r
878ddf1f 737 // Absorb the closing }\r
738 //\r
739 Ptrx++;\r
740\r
741 //\r
742 // Don't provide a PUSH Opcode for the Before and After case\r
743 //\r
744 if ((!Before_Flag) && (!After_Flag)) {\r
745 fputc (EFI_DEP_PUSH, OutFile);\r
746 OutFileSize++;\r
747 }\r
748\r
749 fwrite (&Guid, sizeof (EFI_GUID), 1, OutFile);\r
750\r
751 OutFileSize += sizeof (EFI_GUID);\r
752 Dep_Flag = TRUE;\r
753\r
754 } else if (strncmp (Ptrx, DEPENDENCY_END, strlen (DEPENDENCY_END)) == 0) {\r
755 NotDone = FALSE;\r
756 } else {\r
757 //\r
758 // Not a valid construct. Null terminate somewhere out there and\r
759 // print an error message.\r
760 //\r
761 *(Ptrx + 20) = 0;\r
762 printf (TOOL_NAME " ERROR: Unrecognized input at: \"%s\"...\n", Ptrx);\r
763 return EFI_INVALID_PARAMETER;\r
764 }\r
765 }\r
766 //\r
767 // DRAIN();\r
768 //\r
769 while (StackPtr != EvaluationStack) {\r
770 fputc (PopOpCode ((VOID **) &StackPtr), OutFile);\r
771 OutFileSize++;\r
772 }\r
773\r
774 if (OutFileSize == 0) {\r
775 printf ("Grammer contains no operators or constants\n");\r
776 return EFI_INVALID_PARAMETER;\r
777 }\r
778\r
779 fputc (EFI_DEP_END, OutFile);\r
780\r
781 OutFileSize++;\r
782\r
783 //\r
784 // Checks for invalid padding values\r
785 //\r
786 if (Padding < 0) {\r
787\r
788 printf ("The inputted padding value was %d\n", Padding);\r
789 printf ("The optional padding value can not be less than ZERO\n");\r
790 return EFI_INVALID_PARAMETER;\r
791\r
792 } else if (Padding > 0) {\r
793\r
794 while ((OutFileSize % Padding) != 0) {\r
795\r
796 fputc (' ', OutFile);\r
797 OutFileSize++;\r
798 }\r
799 }\r
800\r
801 Results = (UINTN) fclose (InFile);\r
802 if (Results != 0) {\r
803 printf ("FCLOSE failed\n");\r
804 }\r
805\r
806 Results = (UINTN) fclose (OutFile);\r
807 if (Results != 0) {\r
808 printf ("FCLOSE failed\n");\r
809 }\r
810\r
811 free (Buffer);\r
812 free (EvaluationStack);\r
813\r
814 return EFI_SUCCESS;\r
815} // End GenerateDependencyExpression function\r
816\r
817int\r
818main (\r
819 IN UINTN argc,\r
820 IN CHAR8 *argv[]\r
821 )\r
822/*++\r
823\r
824Routine Description:\r
825\r
826 Parse user entries. Print some rudimentary help\r
827\r
828Arguments:\r
829\r
830 argc The count of input arguments\r
831 argv The input arguments string array\r
832\r
833Returns:\r
834\r
835 EFI_SUCCESS The function completed successfully.\r
836 EFI_INVALID_PARAMETER One of the input parameters was invalid or one of the parameters in the text file was invalid.\r
837 EFI_OUT_OF_RESOURCES Unable to allocate memory.\r
838 EFI_ABORTED Unable to open/create a file or a misc error.\r
839\r
840--*/\r
841// TODO: ] - add argument and description to function comment\r
842{\r
843 FILE *OutFile;\r
844 FILE *InFile;\r
845 UINT8 Padding;\r
846 UINTN Index;\r
847 BOOLEAN Input_Flag;\r
848 BOOLEAN Output_Flag;\r
849 BOOLEAN Pad_Flag;\r
850\r
851 InFile = NULL;\r
852 OutFile = NULL;\r
853 Padding = 0;\r
854 Input_Flag = FALSE;\r
855 Output_Flag = FALSE;\r
856 Pad_Flag = FALSE;\r
857\r
878ddf1f 858 if (argc < 5) {\r
859 printf ("Not enough arguments\n");\r
860 PrintGenDepexUsageInfo ();\r
861 return EFI_INVALID_PARAMETER;\r
862 }\r
863\r
864 for (Index = 1; Index < argc - 1; Index++) {\r
865\r
866 if ((strcmp (argv[Index], "-I") == 0) || (strcmp (argv[Index], "-i") == 0)) {\r
867\r
868 if (!Input_Flag) {\r
869\r
870 InFile = fopen (argv[Index + 1], "rb");\r
871 Input_Flag = TRUE;\r
872\r
873 } else {\r
874 printf ("GenDepex only allows one INPUT (-I) argument\n");\r
875 return EFI_INVALID_PARAMETER;\r
876 }\r
877\r
878 } else if ((strcmp (argv[Index], "-O") == 0) || (strcmp (argv[Index], "-o") == 0)) {\r
879\r
880 if (!Output_Flag) {\r
881\r
882 OutFile = fopen (argv[Index + 1], "wb");\r
883 Output_Flag = TRUE;\r
884\r
885 } else {\r
886 printf ("GenDepex only allows one OUTPUT (-O) argument\n");\r
887 return EFI_INVALID_PARAMETER;\r
888 }\r
889\r
890 } else if ((strcmp (argv[Index], "-P") == 0) || (strcmp (argv[Index], "-p") == 0)) {\r
891\r
892 if (!Pad_Flag) {\r
893\r
894 Padding = (UINT8) atoi (argv[Index + 1]);\r
895 Pad_Flag = TRUE;\r
896\r
897 } else {\r
898 printf ("GenDepex only allows one PADDING (-P) argument\n");\r
899 return EFI_INVALID_PARAMETER;\r
900 }\r
901 }\r
902 }\r
903\r
904 PrintGenDepexUtilityInfo ();\r
905\r
906 if (InFile == NULL) {\r
907 printf ("Can not open <INFILE> for reading.\n");\r
908 PrintGenDepexUsageInfo ();\r
909 return EFI_ABORTED;\r
910 }\r
911\r
912 if (OutFile == NULL) {\r
913 printf ("Can not open <OUTFILE> for writting.\n");\r
914 PrintGenDepexUsageInfo ();\r
915 return EFI_ABORTED;\r
916 }\r
917\r
918 return GenerateDependencyExpression (InFile, OutFile, Padding);\r
919}\r