]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - compiler/aslstartup.c
Import ACPICA 20100528.
[FreeBSD/FreeBSD.git] / compiler / aslstartup.c
1
2 /******************************************************************************
3  *
4  * Module Name: aslstartup - Compiler startup routines, called from main
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116
117
118 #include "aslcompiler.h"
119 #include "actables.h"
120 #include "acapps.h"
121
122 #define _COMPONENT          ACPI_COMPILER
123         ACPI_MODULE_NAME    ("aslstartup")
124
125
126 #define ASL_MAX_FILES   256
127 char                    *FileList[ASL_MAX_FILES];
128 int                     FileCount;
129 BOOLEAN                 AslToFile = TRUE;
130
131
132 /* Local prototypes */
133
134 static void
135 AslInitializeGlobals (
136     void);
137
138 static char **
139 AsDoWildcard (
140     char                    *DirectoryPathname,
141     char                    *FileSpecifier);
142
143 UINT8
144 AslDetectSourceFileType (
145     ASL_FILE_INFO           *Info);
146
147
148 /*******************************************************************************
149  *
150  * FUNCTION:    AslInitializeGlobals
151  *
152  * PARAMETERS:  None
153  *
154  * RETURN:      None
155  *
156  * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
157  *              allows multiple files to be disassembled and/or compiled.
158  *
159  ******************************************************************************/
160
161 static void
162 AslInitializeGlobals (
163     void)
164 {
165     UINT32                  i;
166
167
168     /* Init compiler globals */
169
170     Gbl_CurrentColumn = 0;
171     Gbl_CurrentLineNumber = 1;
172     Gbl_LogicalLineNumber = 1;
173     Gbl_CurrentLineOffset = 0;
174     Gbl_InputFieldCount = 0;
175     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
176
177     Gbl_ErrorLog = NULL;
178     Gbl_NextError = NULL;
179     Gbl_Signature = NULL;
180     Gbl_FileType = 0;
181
182     AslGbl_NextEvent = 0;
183     for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
184     {
185         Gbl_ExceptionCount[i] = 0;
186     }
187
188     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
189     Gbl_Files[ASL_FILE_AML_OUTPUT].Handle = NULL;
190 }
191
192
193 /******************************************************************************
194  *
195  * FUNCTION:    AsDoWildcard
196  *
197  * PARAMETERS:  None
198  *
199  * RETURN:      None
200  *
201  * DESCRIPTION: Process files via wildcards. This function is for the Windows
202  *              case only.
203  *
204  ******************************************************************************/
205
206 static char **
207 AsDoWildcard (
208     char                    *DirectoryPathname,
209     char                    *FileSpecifier)
210 {
211 #ifdef WIN32
212     void                    *DirInfo;
213     char                    *Filename;
214
215
216     FileCount = 0;
217
218     /* Open parent directory */
219
220     DirInfo = AcpiOsOpenDirectory (DirectoryPathname, FileSpecifier, REQUEST_FILE_ONLY);
221     if (!DirInfo)
222     {
223         /* Either the directory of file does not exist */
224
225         Gbl_Files[ASL_FILE_INPUT].Filename = FileSpecifier;
226         FlFileError (ASL_FILE_INPUT, ASL_MSG_OPEN);
227         AslAbort ();
228     }
229
230     /* Process each file that matches the wildcard specification */
231
232     while ((Filename = AcpiOsGetNextFilename (DirInfo)))
233     {
234         /* Add the filename to the file list */
235
236         FileList[FileCount] = AcpiOsAllocate (strlen (Filename) + 1);
237         strcpy (FileList[FileCount], Filename);
238         FileCount++;
239
240         if (FileCount >= ASL_MAX_FILES)
241         {
242             printf ("Max files reached\n");
243             FileList[0] = NULL;
244             return (FileList);
245         }
246     }
247
248     /* Cleanup */
249
250     AcpiOsCloseDirectory (DirInfo);
251     FileList[FileCount] = NULL;
252     return (FileList);
253
254 #else
255     /*
256      * Linux/Unix cases - Wildcards are expanded by the shell automatically.
257      * Just return the filename in a null terminated list
258      */
259     FileList[0] = AcpiOsAllocate (strlen (FileSpecifier) + 1);
260     strcpy (FileList[0], FileSpecifier);
261     FileList[1] = NULL;
262
263     return (FileList);
264 #endif
265 }
266
267
268 /*******************************************************************************
269  *
270  * FUNCTION:    AslDetectSourceFileType
271  *
272  * PARAMETERS:  Info            - Name/Handle for the file (must be open)
273  *
274  * RETURN:      File Type
275  *
276  * DESCRIPTION: Determine the type of the input file. Either binary (contains
277  *              non-ASCII characters), ASL file, or an ACPI Data Table file.
278  *
279  ******************************************************************************/
280
281 UINT8
282 AslDetectSourceFileType (
283     ASL_FILE_INFO           *Info)
284 {
285     char                    *FileChar;
286     UINT8                   Type;
287     ACPI_STATUS             Status;
288
289
290     /* Check for 100% ASCII source file (comments are ignored) */
291
292     Status = FlCheckForAscii (Info);
293     if (ACPI_FAILURE (Status))
294     {
295         printf ("Non-ascii input file - %s\n", Info->Filename);
296         Type = ASL_INPUT_TYPE_BINARY;
297         goto Cleanup;
298     }
299
300     /*
301      * File is ASCII. Determine if this is an ASL file or an ACPI data
302      * table file.
303      */
304     while (fgets (Gbl_CurrentLineBuffer, ASL_LINE_BUFFER_SIZE, Info->Handle))
305     {
306         /* Uppercase the buffer for caseless compare */
307
308         FileChar = Gbl_CurrentLineBuffer;
309         while (*FileChar)
310         {
311             *FileChar = (char) toupper ((int) *FileChar);
312             FileChar++;
313         }
314
315         /* Presence of "DefinitionBlock" indicates actual ASL code */
316
317         if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
318         {
319             /* Appears to be an ASL file */
320
321             Type = ASL_INPUT_TYPE_ASCII_ASL;
322             goto Cleanup;
323         }
324     }
325
326     /* Not an ASL source file, default to a data table source file */
327
328     Type = ASL_INPUT_TYPE_ASCII_DATA;
329
330 Cleanup:
331
332     /* Must seek back to the start of the file */
333
334     fseek (Info->Handle, 0, SEEK_SET);
335     return (Type);
336 }
337
338
339 /*******************************************************************************
340  *
341  * FUNCTION:    AslDoOneFile
342  *
343  * PARAMETERS:  Filename        - Name of the file
344  *
345  * RETURN:      Status
346  *
347  * DESCRIPTION: Process a single file - either disassemble, compile, or both
348  *
349  ******************************************************************************/
350
351 ACPI_STATUS
352 AslDoOneFile (
353     char                    *Filename)
354 {
355     ACPI_STATUS             Status;
356
357
358     Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
359
360     /* Re-initialize "some" compiler globals */
361
362     AslInitializeGlobals ();
363
364     /*
365      * AML Disassembly (Optional)
366      */
367     if (Gbl_DisasmFlag || Gbl_GetAllTables)
368     {
369         /* ACPICA subsystem initialization */
370
371         Status = AdInitialize ();
372         if (ACPI_FAILURE (Status))
373         {
374             return (Status);
375         }
376
377         Status = AcpiAllocateRootTable (4);
378         if (ACPI_FAILURE (Status))
379         {
380             AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
381                 AcpiFormatException (Status));
382             return (Status);
383         }
384
385         /* This is where the disassembly happens */
386
387         AcpiGbl_DbOpt_disasm = TRUE;
388         Status = AdAmlDisassemble (AslToFile,
389                     Gbl_Files[ASL_FILE_INPUT].Filename,
390                     Gbl_OutputFilenamePrefix,
391                     &Gbl_Files[ASL_FILE_INPUT].Filename,
392                     Gbl_GetAllTables);
393         if (ACPI_FAILURE (Status))
394         {
395             return (Status);
396         }
397
398         /* Shutdown compiler and ACPICA subsystem */
399
400         AeClearErrorLog ();
401         AcpiTerminate ();
402
403         /*
404          * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
405          * .DSL disassembly file, which can now be compiled if requested
406          */
407         if (Gbl_DoCompile)
408         {
409             AcpiOsPrintf ("\nCompiling \"%s\"\n",
410                 Gbl_Files[ASL_FILE_INPUT].Filename);
411         }
412         else
413         {
414             Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
415             return (AE_OK);
416         }
417     }
418
419     /*
420      * Open the input file. Here, this should be an ASCII source file,
421      * either an ASL file or a Data Table file
422      */
423     Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
424     if (ACPI_FAILURE (Status))
425     {
426         AePrintErrorLog (ASL_FILE_STDERR);
427         return (AE_ERROR);
428     }
429
430     /* Determine input file type */
431
432     Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
433     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
434     {
435         return (AE_ERROR);
436     }
437
438     /*
439      * If -p not specified, we will use the input filename as the
440      * output filename prefix
441      */
442     if (Gbl_UseDefaultAmlFilename)
443     {
444         Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
445     }
446
447     /* Open the optional output files (listings, etc.) */
448
449     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
450     if (ACPI_FAILURE (Status))
451     {
452         AePrintErrorLog (ASL_FILE_STDERR);
453         return (AE_ERROR);
454     }
455
456     /*
457      * Compilation of ASL source versus DataTable source uses different
458      * compiler subsystems
459      */
460     switch (Gbl_FileType)
461     {
462     /*
463      * Data Table Compilation
464      */
465     case ASL_INPUT_TYPE_ASCII_DATA:
466
467         /*
468          * Require use of command-line option to enable the data table
469          * compiler -- for now, until development of the compiler is
470          * complete.
471          */
472         if (!Gbl_DataTableCompilerAvailable)
473         {
474             printf ("Data Table Compiler is not available yet\n");
475             return (AE_SUPPORT);
476         }
477
478         Status = DtDoCompile ();
479
480         if (Gbl_Signature)
481         {
482             ACPI_FREE (Gbl_Signature);
483             Gbl_Signature = NULL;
484         }
485         AeClearErrorLog ();
486         return (Status);
487
488     /*
489      * ASL Compilation (Optional)
490      */
491     case ASL_INPUT_TYPE_ASCII_ASL:
492
493
494         /* ACPICA subsystem initialization */
495
496         Status = AdInitialize ();
497         if (ACPI_FAILURE (Status))
498         {
499             return (Status);
500         }
501
502         Status = CmDoCompile ();
503         AcpiTerminate ();
504
505         /*
506          * Return non-zero exit code if there have been errors, unless the
507          * global ignore error flag has been set
508          */
509         if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors))
510         {
511             return (AE_ERROR);
512         }
513
514         AeClearErrorLog ();
515         return (AE_OK);
516
517     case ASL_INPUT_TYPE_BINARY:
518
519         AePrintErrorLog (ASL_FILE_STDERR);
520         return (AE_ERROR);
521
522     default:
523         printf ("Unknown file type %X\n", Gbl_FileType);
524         return (AE_ERROR);
525     }
526 }
527
528
529 /*******************************************************************************
530  *
531  * FUNCTION:    AslDoOnePathname
532  *
533  * PARAMETERS:  Pathname            - Full pathname, possibly with wildcards
534  *
535  * RETURN:      Status
536  *
537  * DESCRIPTION: Process one pathname, possible terminated with a wildcard
538  *              specification. If a wildcard, it is expanded and the multiple
539  *              files are processed.
540  *
541  ******************************************************************************/
542
543 ACPI_STATUS
544 AslDoOnePathname (
545     char                    *Pathname)
546 {
547     ACPI_STATUS             Status = AE_OK;
548     char                    **FileList;
549     char                    *Filename;
550     char                    *FullPathname;
551
552
553     /* Split incoming path into a directory/filename combo */
554
555     Status = FlSplitInputPathname (Pathname, &Gbl_DirectoryPath, &Filename);
556     if (ACPI_FAILURE (Status))
557     {
558         return (Status);
559     }
560
561     /* Expand possible wildcard into a file list (Windows/DOS only) */
562
563     FileList = AsDoWildcard (Gbl_DirectoryPath, Filename);
564     while (*FileList)
565     {
566         FullPathname = ACPI_ALLOCATE (
567             strlen (Gbl_DirectoryPath) + strlen (*FileList) + 1);
568
569         /* Construct a full path to the file */
570
571         strcpy (FullPathname, Gbl_DirectoryPath);
572         strcat (FullPathname, *FileList);
573
574         /*
575          * If -p not specified, we will use the input filename as the
576          * output filename prefix
577          */
578         if (Gbl_UseDefaultAmlFilename)
579         {
580             Gbl_OutputFilenamePrefix = FullPathname;
581         }
582
583         /* Save status from all compiles */
584
585         Status |= AslDoOneFile (FullPathname);
586
587         ACPI_FREE (FullPathname);
588         ACPI_FREE (*FileList);
589         *FileList = NULL;
590         FileList++;
591     }
592
593     ACPI_FREE (Gbl_DirectoryPath);
594     ACPI_FREE (Filename);
595     return (Status);
596 }
597