]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Tools/Source/GenAprioriFile/GenAprioriFile.c
Sync all bug fixes between EDK1.04 and EDK1.06 into EdkCompatibilityPkg.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / GenAprioriFile / GenAprioriFile.c
CommitLineData
3eb9473e 1/*++\r
2\r
3e99020d 3 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
4b1e1121 4 This program and the accompanying materials \r
3eb9473e 5 are licensed and made available under the terms and conditions of the BSD License \r
6 which accompanies this distribution. The full text of the license may be found at \r
7 http://opensource.org/licenses/bsd-license.php \r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12\r
13Module Name:\r
14\r
15 GenAprioriFile.c \r
16\r
17Abstract:\r
18\r
19 Given an input file containing a list of GUIDs (or Guided file names),\r
20 convert the file to an Apriori file consumable by the dispatcher.\r
21\r
22--*/\r
23\r
24#include <stdio.h>\r
25#include <string.h>\r
26#include <ctype.h>\r
27\r
28#include "EfiCommon.h"\r
29#include "ParseInf.h"\r
30#include "CommonLib.h" // for compare guid\r
31#include "EfiUtilityMsgs.h"\r
32\r
33#define MAX_LINE_LEN 200\r
34#define MAX_PATH 200\r
35\r
36//\r
37// typedef unsigned int STATUS;\r
38// #define STATUS_SUCCESS 0\r
39// #define STATUS_WARNING 1\r
40// #define STATUS_ERROR 2\r
41//\r
3e99020d
LG
42#define UTILITY_NAME "GenAprioriFile"\r
43#define UTILITY_VERSION "v1.0"\r
3eb9473e 44//\r
45// Here's all our globals.\r
46//\r
47static struct {\r
48 FILE *BinFptr; // output dependencies to this file\r
49 INT8 *AprioriFileName;\r
50 INT8 *OutputFileName;\r
51 BOOLEAN Intelligent;\r
52 BOOLEAN Verbose;\r
53 BOOLEAN NullTerminate;\r
54} mGlobals;\r
55\r
56static\r
57STATUS\r
58ProcessArgs (\r
59 int Argc,\r
60 char *Argv[]\r
61 );\r
62\r
63static\r
64BOOLEAN\r
65IsCommentLine (\r
66 INT8 *Line\r
67 );\r
68\r
69static\r
70void\r
71Usage (\r
72 VOID\r
73 );\r
74\r
75int\r
76main (\r
77 int Argc,\r
78 char *Argv[]\r
79 )\r
80/*++\r
81\r
82Routine Description:\r
83\r
84 Call the routine to parse the command-line options, then process the\r
85 Apriori list file and generate the GUID file.\r
86 \r
87Arguments:\r
88\r
89 Standard C main() argc and argv.\r
90\r
91Returns:\r
92\r
93 0 if successful\r
94 nonzero otherwise\r
95 \r
96--*/\r
97// GC_TODO: Argc - add argument and description to function comment\r
98// GC_TODO: ] - add argument and description to function comment\r
99{\r
100 STATUS Status;\r
101 FILE *AprioriFptr;\r
102 FILE *BinFptr;\r
103 INT8 Line[MAX_LINE_LEN];\r
104 EFI_GUID Guid;\r
105 EFI_GUID GuidIn;\r
106 EFI_GUID ZeroGuid;\r
107 UINT32 LineCounter;\r
108 //\r
109 // Initialize the error printing routines\r
110 //\r
111 SetUtilityName (UTILITY_NAME);\r
112 //\r
113 // Clear our globals\r
114 //\r
115 memset ((char *) &mGlobals, 0, sizeof (mGlobals));\r
116 memset ((char *) &ZeroGuid, 0, sizeof (ZeroGuid));\r
117 AprioriFptr = NULL;\r
118 BinFptr = NULL;\r
119\r
120 //\r
121 // Process the command-line arguments\r
122 //\r
123 Status = ProcessArgs (Argc, Argv);\r
124 if (Status != STATUS_SUCCESS) {\r
125 return Status;\r
126 }\r
127 //\r
128 // If arguments were ok, then open the Apriori file and process it.\r
129 //\r
130 if ((AprioriFptr = fopen (mGlobals.AprioriFileName, "r")) == NULL) {\r
131 Error (NULL, 0, 0, mGlobals.AprioriFileName, "failed to open file for reading");\r
132 goto FinishUp;\r
133 }\r
134 //\r
135 // If -i intelligent option specified, then attempt to read and\r
136 // existing output file and see if we'd be creating an identical file.\r
137 //\r
138 if (mGlobals.Intelligent) {\r
139 if ((BinFptr = fopen (mGlobals.OutputFileName, "rb")) == NULL) {\r
140 if (mGlobals.Verbose) {\r
141 DebugMsg (NULL, 0, 0, "Creating new apriori file -- no existing file", NULL);\r
142 }\r
143\r
144 goto CreateFile;\r
145 }\r
146 //\r
147 // Read lines from the input file until done. Convert each to a guid, then\r
148 // read a guid from the input file and compare them.\r
149 //\r
150 while (fgets (Line, sizeof (Line), AprioriFptr) != NULL) {\r
151\r
152 if (IsCommentLine (Line)) {\r
153 continue;\r
154 }\r
155 //\r
156 // Convert to a guid\r
157 //\r
158 if (StringToGuid (Line, &Guid) != EFI_SUCCESS) {\r
159 if (mGlobals.Verbose) {\r
160 DebugMsg (NULL, 0, 0, "failed to read GUID from input text file -- creating new file", NULL);\r
161 }\r
162\r
163 goto CreateFile;\r
164 }\r
165 //\r
166 // Read guid from input file, then compare\r
167 //\r
168 if (fread (&GuidIn, sizeof (GuidIn), 1, BinFptr) != 1) {\r
169 if (mGlobals.Verbose) {\r
170 DebugMsg (NULL, 0, 0, "failed to read GUID from input binary file -- creating new file", NULL);\r
171 }\r
172\r
173 goto CreateFile;\r
174 }\r
175\r
176 if (CompareGuid (&Guid, &GuidIn) != 0) {\r
177 if (mGlobals.Verbose) {\r
178 DebugMsg (NULL, 0, 0, "GUID comparison failed -- creating new file", NULL);\r
179 }\r
180\r
181 goto CreateFile;\r
182 }\r
183 }\r
184 //\r
185 // May be one more NULL guid in the binary file\r
186 //\r
187 if (mGlobals.NullTerminate) {\r
188 if (fread (&GuidIn, sizeof (GuidIn), 1, BinFptr) != 1) {\r
189 if (mGlobals.Verbose) {\r
190 DebugMsg (NULL, 0, 0, "failed to read NULL GUID from input binary file -- creating new file", NULL);\r
191 }\r
192\r
193 goto CreateFile;\r
194 }\r
195\r
196 if (CompareGuid (&GuidIn, &ZeroGuid) != 0) {\r
197 if (mGlobals.Verbose) {\r
198 DebugMsg (NULL, 0, 0, "NULL GUID comparison failed -- creating new file", NULL);\r
199 }\r
200\r
201 goto CreateFile;\r
202 }\r
203 }\r
204 //\r
205 // Make sure we're at the end of both files.\r
206 //\r
207 if ((fgets (Line, sizeof (Line), AprioriFptr) != NULL) || (fread (&GuidIn, 1, 1, BinFptr) != 0)) {\r
208 if (mGlobals.Verbose) {\r
209 DebugMsg (NULL, 0, 0, "file sizes different, -i test failed -- creating new file", NULL);\r
210 }\r
211\r
212 goto CreateFile;\r
213 }\r
214\r
215 if (mGlobals.Verbose) {\r
216 DebugMsg (NULL, 0, 0, "existing file would be unchanged -- keeping existing apriori file", NULL);\r
217 }\r
218\r
219 goto FinishUp;\r
220 }\r
221\r
222CreateFile:\r
223 //\r
224 // Rewind the Apriori file in case -i was specified. Also\r
225 // try to close the output file for the case where we prescanned\r
226 // it (again, because of -i).\r
227 //\r
228 rewind (AprioriFptr);\r
229 if (BinFptr != NULL) {\r
230 fclose (BinFptr);\r
231 }\r
232 //\r
233 // Open the output file\r
234 //\r
235 if ((BinFptr = fopen (mGlobals.OutputFileName, "wb")) == NULL) {\r
236 Error (NULL, 0, 0, mGlobals.OutputFileName, "could not open input file");\r
237 goto FinishUp;\r
238 }\r
239 //\r
240 // Read lines until we're done\r
241 //\r
242 LineCounter = 0;\r
243 while (fgets (Line, sizeof (Line), AprioriFptr) != NULL) {\r
244 LineCounter++;\r
245 if (IsCommentLine (Line)) {\r
246 continue;\r
247 }\r
248 //\r
249 // Convert to a GUID\r
250 //\r
251 if (StringToGuid (Line, &Guid) != EFI_SUCCESS) {\r
252 Error (mGlobals.AprioriFileName, LineCounter, 0, "failed to convert GUID", NULL);\r
253 goto FinishUp;\r
254 }\r
255 //\r
256 // Write the guid to the output file\r
257 //\r
258 if (fwrite (&Guid, sizeof (Guid), 1, BinFptr) != 1) {\r
259 Error (NULL, 0, 0, mGlobals.OutputFileName, "failed to write GUID to output file");\r
260 goto FinishUp;\r
261 }\r
262 }\r
263 //\r
264 // Write a null guid out to terminate the list\r
265 //\r
266 if (mGlobals.NullTerminate) {\r
267 memset ((void *) &Guid, 0, sizeof (Guid));\r
268 if (fwrite (&Guid, sizeof (Guid), 1, BinFptr) != 1) {\r
269 Error (NULL, 0, 0, mGlobals.OutputFileName, "failed to write NULL termination GUID to output file");\r
270 }\r
271 }\r
272\r
273FinishUp:\r
274\r
275 if (AprioriFptr != NULL) {\r
276 fclose (AprioriFptr);\r
277 }\r
278\r
279 if (BinFptr != NULL) {\r
280 fclose (BinFptr);\r
281 }\r
282\r
283 return GetUtilityStatus ();\r
284}\r
285\r
286static\r
287BOOLEAN\r
288IsCommentLine (\r
289 INT8 *Line\r
290 )\r
291/*++\r
292\r
293Routine Description:\r
294\r
295 GC_TODO: Add function description\r
296\r
297Arguments:\r
298\r
299 Line - GC_TODO: add argument description\r
300\r
301Returns:\r
302\r
303 GC_TODO: add return values\r
304\r
305--*/\r
306{\r
307 for (; isspace (*Line) && *Line; Line++)\r
308 ;\r
309\r
310 //\r
311 // Allow # or // comments\r
312 //\r
313 if ((*Line == '#') || ((*Line == '/') && (*(Line + 1) == '/')) || (*Line == '\n') || (*Line == 0)) {\r
314 return TRUE;\r
315 }\r
316\r
317 return FALSE;\r
318}\r
319//\r
320// Process the command-line arguments\r
321//\r
322static\r
323STATUS\r
324ProcessArgs (\r
325 int Argc,\r
326 char *Argv[]\r
327 )\r
328/*++\r
329\r
330Routine Description:\r
331\r
332 GC_TODO: Add function description\r
333\r
334Arguments:\r
335\r
336 Argc - GC_TODO: add argument description\r
337 ] - GC_TODO: add argument description\r
338\r
339Returns:\r
340\r
341 GC_TODO: add return values\r
342\r
343--*/\r
344{\r
345 //\r
346 // Skip program name\r
347 //\r
348 Argc--;\r
349 Argv++;\r
350\r
351 //\r
352 // Process until no more args\r
353 //\r
354 while (Argc) {\r
355 //\r
356 // -f AprioriFile\r
357 //\r
358 if (_stricmp (Argv[0], "-f") == 0) {\r
359 //\r
360 // check for one more arg\r
361 //\r
362 if (Argc > 1) {\r
363 mGlobals.AprioriFileName = Argv[1];\r
364 } else {\r
365 Error (NULL, 0, 0, NULL, "missing filename with %s", Argv[0]);\r
366 Usage ();\r
367 return STATUS_ERROR;\r
368 }\r
369\r
370 Argc--;\r
371 Argv++;\r
372 } else if (_stricmp (Argv[0], "-i") == 0) {\r
373 //\r
374 // intelligent creation of output file. That is to say, if\r
375 // there's already a file there, and it's the same as what\r
376 // we'd create, then don't re-create. This is to support\r
377 // incremental builds (that is to say, running nmake a second time\r
378 // does nothing).\r
379 //\r
380 mGlobals.Intelligent = TRUE;\r
381 } else if (_stricmp (Argv[0], "-v") == 0) {\r
382 mGlobals.Verbose = TRUE;\r
383 } else if (_stricmp (Argv[0], "-null") == 0) {\r
384 mGlobals.NullTerminate = TRUE;\r
385 } else if (_stricmp (Argv[0], "-o") == 0) {\r
386 //\r
387 // -o OutputFileName\r
388 // check for one more arg\r
389 //\r
390 if (Argc > 1) {\r
391 mGlobals.OutputFileName = Argv[1];\r
392 } else {\r
393 Error (NULL, 0, 0, NULL, "missing filename argument with %s", Argv[0]);\r
394 Usage ();\r
395 return STATUS_ERROR;\r
396 }\r
397\r
398 Argc--;\r
399 Argv++;\r
400 } else if ((_stricmp (Argv[0], "-h") == 0) || (strcmp (Argv[0], "-?") == 0)) {\r
401 Usage ();\r
402 return STATUS_ERROR;\r
403 } else {\r
404 Error (NULL, 0, 0, Argv[0], "unrecognized option");\r
405 Usage ();\r
406 return STATUS_ERROR;\r
407 }\r
408\r
409 Argc--;\r
410 Argv++;\r
411 }\r
412 //\r
413 // Had to specify the apriori input file and output file names\r
414 //\r
415 if (mGlobals.AprioriFileName == NULL) {\r
416 Error (NULL, 0, 0, "must specify -f AprioriFile", NULL);\r
417 Usage ();\r
418 return STATUS_ERROR;\r
419 }\r
420\r
421 if (mGlobals.OutputFileName == NULL) {\r
422 Error (NULL, 0, 0, "must specify -o OutputFile", NULL);\r
423 Usage ();\r
424 return STATUS_ERROR;\r
425 }\r
426\r
427 return STATUS_SUCCESS;\r
428}\r
429\r
430static\r
431void\r
432Usage (\r
433 VOID\r
434 )\r
435/*++\r
436\r
437Routine Description:\r
438\r
439 Print usage information for this utility.\r
440 \r
441Arguments:\r
442\r
443 None.\r
444\r
445Returns:\r
446\r
447 Nothing.\r
448 \r
449--*/\r
450{\r
3e99020d
LG
451 int Index;\r
452 const char *Str[] = {\r
453 UTILITY_NAME" "UTILITY_VERSION" - Intel Generate Apriori File Utility",\r
454 " Copyright (C), 2006 - 2008 Intel Corporation",\r
455 \r
456#if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )\r
457 " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,\r
458#endif\r
3eb9473e 459 "",\r
3e99020d
LG
460 "Usage:",\r
461 " "UTILITY_NAME" [OPTION]...",\r
462 "Description:",\r
463 " Generate an Apriori file consumable by the DXE or PEI dispatcher.",\r
464 "Options:",\r
465 " -h or -? for this help information",\r
466 " -f AprioriFile parse the GUID'ed files in AprioriFile (required)",\r
467 " -o OutputFile write output to OutputFile (required)",\r
468 " -i for intelligent re-creation of OutputFile",\r
469 " -null to terminate the output file with a NULL GUID",\r
470 " -v verbose option",\r
3eb9473e 471 NULL\r
472 };\r
473 for (Index = 0; Str[Index] != NULL; Index++) {\r
474 fprintf (stdout, "%s\n", Str[Index]);\r
475 }\r
476}\r