Bring in acpica 20110211, still not ok anyway
[dragonfly.git] / sys / contrib / dev / acpica-unix / 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 - 2011, 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 static char             *FileList[ASL_MAX_FILES];
128 static BOOLEAN          AslToFile = TRUE;
129
130
131 /* Local prototypes */
132
133 static char **
134 AsDoWildcard (
135     char                    *DirectoryPathname,
136     char                    *FileSpecifier);
137
138 static UINT8
139 AslDetectSourceFileType (
140     ASL_FILE_INFO           *Info);
141
142
143 /*******************************************************************************
144  *
145  * FUNCTION:    AslInitializeGlobals
146  *
147  * PARAMETERS:  None
148  *
149  * RETURN:      None
150  *
151  * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
152  *              allows multiple files to be disassembled and/or compiled.
153  *
154  ******************************************************************************/
155
156 void
157 AslInitializeGlobals (
158     void)
159 {
160     UINT32                  i;
161
162
163     /* Init compiler globals */
164
165     Gbl_CurrentColumn = 0;
166     Gbl_CurrentLineNumber = 1;
167     Gbl_LogicalLineNumber = 1;
168     Gbl_CurrentLineOffset = 0;
169     Gbl_InputFieldCount = 0;
170     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
171
172     Gbl_ErrorLog = NULL;
173     Gbl_NextError = NULL;
174     Gbl_Signature = NULL;
175     Gbl_FileType = 0;
176
177     AslGbl_NextEvent = 0;
178     for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
179     {
180         Gbl_ExceptionCount[i] = 0;
181     }
182
183     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
184     Gbl_Files[ASL_FILE_AML_OUTPUT].Handle = NULL;
185
186     Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename = NULL;
187     Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle = NULL;
188 }
189
190
191 /******************************************************************************
192  *
193  * FUNCTION:    AsDoWildcard
194  *
195  * PARAMETERS:  None
196  *
197  * RETURN:      None
198  *
199  * DESCRIPTION: Process files via wildcards. This function is for the Windows
200  *              case only.
201  *
202  ******************************************************************************/
203
204 static char **
205 AsDoWildcard (
206     char                    *DirectoryPathname,
207     char                    *FileSpecifier)
208 {
209 #ifdef WIN32
210     void                    *DirInfo;
211     char                    *Filename;
212     int                     FileCount;
213
214
215     FileCount = 0;
216
217     /* Open parent directory */
218
219     DirInfo = AcpiOsOpenDirectory (DirectoryPathname, FileSpecifier, REQUEST_FILE_ONLY);
220     if (!DirInfo)
221     {
222         /* Either the directory of file does not exist */
223
224         Gbl_Files[ASL_FILE_INPUT].Filename = FileSpecifier;
225         FlFileError (ASL_FILE_INPUT, ASL_MSG_OPEN);
226         AslAbort ();
227     }
228
229     /* Process each file that matches the wildcard specification */
230
231     while ((Filename = AcpiOsGetNextFilename (DirInfo)))
232     {
233         /* Add the filename to the file list */
234
235         FileList[FileCount] = AcpiOsAllocate (strlen (Filename) + 1);
236         strcpy (FileList[FileCount], Filename);
237         FileCount++;
238
239         if (FileCount >= ASL_MAX_FILES)
240         {
241             printf ("Max files reached\n");
242             FileList[0] = NULL;
243             return (FileList);
244         }
245     }
246
247     /* Cleanup */
248
249     AcpiOsCloseDirectory (DirInfo);
250     FileList[FileCount] = NULL;
251     return (FileList);
252
253 #else
254     /*
255      * Linux/Unix cases - Wildcards are expanded by the shell automatically.
256      * Just return the filename in a null terminated list
257      */
258     FileList[0] = AcpiOsAllocate (strlen (FileSpecifier) + 1);
259     strcpy (FileList[0], FileSpecifier);
260     FileList[1] = NULL;
261
262     return (FileList);
263 #endif
264 }
265
266
267 /*******************************************************************************
268  *
269  * FUNCTION:    AslDetectSourceFileType
270  *
271  * PARAMETERS:  Info            - Name/Handle for the file (must be open)
272  *
273  * RETURN:      File Type
274  *
275  * DESCRIPTION: Determine the type of the input file. Either binary (contains
276  *              non-ASCII characters), ASL file, or an ACPI Data Table file.
277  *
278  ******************************************************************************/
279
280 static UINT8
281 AslDetectSourceFileType (
282     ASL_FILE_INFO           *Info)
283 {
284     char                    *FileChar;
285     UINT8                   Type;
286     ACPI_STATUS             Status;
287
288
289     /* Check for 100% ASCII source file (comments are ignored) */
290
291     Status = FlCheckForAscii (Info);
292     if (ACPI_FAILURE (Status))
293     {
294         printf ("Non-ascii input file - %s\n", Info->Filename);
295         Type = ASL_INPUT_TYPE_BINARY;
296         goto Cleanup;
297     }
298
299     /*
300      * File is ASCII. Determine if this is an ASL file or an ACPI data
301      * table file.
302      */
303     while (fgets (Gbl_CurrentLineBuffer, ASL_LINE_BUFFER_SIZE, Info->Handle))
304     {
305         /* Uppercase the buffer for caseless compare */
306
307         FileChar = Gbl_CurrentLineBuffer;
308         while (*FileChar)
309         {
310             *FileChar = (char) toupper ((int) *FileChar);
311             FileChar++;
312         }
313
314         /* Presence of "DefinitionBlock" indicates actual ASL code */
315
316         if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
317         {
318             /* Appears to be an ASL file */
319
320             Type = ASL_INPUT_TYPE_ASCII_ASL;
321             goto Cleanup;
322         }
323     }
324
325     /* Not an ASL source file, default to a data table source file */
326
327     Type = ASL_INPUT_TYPE_ASCII_DATA;
328
329 Cleanup:
330
331     /* Must seek back to the start of the file */
332
333     fseek (Info->Handle, 0, SEEK_SET);
334     return (Type);
335 }
336
337
338 /*******************************************************************************
339  *
340  * FUNCTION:    AslDoOneFile
341  *
342  * PARAMETERS:  Filename        - Name of the file
343  *
344  * RETURN:      Status
345  *
346  * DESCRIPTION: Process a single file - either disassemble, compile, or both
347  *
348  ******************************************************************************/
349
350 ACPI_STATUS
351 AslDoOneFile (
352     char                    *Filename)
353 {
354     ACPI_STATUS             Status;
355
356
357     Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
358
359     /* Re-initialize "some" compiler globals */
360
361     AslInitializeGlobals ();
362
363     /*
364      * AML Disassembly (Optional)
365      */
366     if (Gbl_DisasmFlag || Gbl_GetAllTables)
367     {
368         /* ACPICA subsystem initialization */
369
370         Status = AdInitialize ();
371         if (ACPI_FAILURE (Status))
372         {
373             return (Status);
374         }
375
376         Status = AcpiAllocateRootTable (4);
377         if (ACPI_FAILURE (Status))
378         {
379             AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
380                 AcpiFormatException (Status));
381             return (Status);
382         }
383
384         /* This is where the disassembly happens */
385
386         AcpiGbl_DbOpt_disasm = TRUE;
387         Status = AdAmlDisassemble (AslToFile,
388                     Gbl_Files[ASL_FILE_INPUT].Filename,
389                     Gbl_OutputFilenamePrefix,
390                     &Gbl_Files[ASL_FILE_INPUT].Filename,
391                     Gbl_GetAllTables);
392         if (ACPI_FAILURE (Status))
393         {
394             return (Status);
395         }
396
397         /* Shutdown compiler and ACPICA subsystem */
398
399         AeClearErrorLog ();
400         (void) AcpiTerminate ();
401
402         /*
403          * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
404          * .DSL disassembly file, which can now be compiled if requested
405          */
406         if (Gbl_DoCompile)
407         {
408             AcpiOsPrintf ("\nCompiling \"%s\"\n",
409                 Gbl_Files[ASL_FILE_INPUT].Filename);
410         }
411         else
412         {
413             Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
414             return (AE_OK);
415         }
416     }
417
418     /*
419      * Open the input file. Here, this should be an ASCII source file,
420      * either an ASL file or a Data Table file
421      */
422     Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
423     if (ACPI_FAILURE (Status))
424     {
425         AePrintErrorLog (ASL_FILE_STDERR);
426         return (AE_ERROR);
427     }
428
429     /* Determine input file type */
430
431     Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
432     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
433     {
434         return (AE_ERROR);
435     }
436
437     /*
438      * If -p not specified, we will use the input filename as the
439      * output filename prefix
440      */
441     if (Gbl_UseDefaultAmlFilename)
442     {
443         Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
444     }
445
446     /* Open the optional output files (listings, etc.) */
447
448     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
449     if (ACPI_FAILURE (Status))
450     {
451         AePrintErrorLog (ASL_FILE_STDERR);
452         return (AE_ERROR);
453     }
454
455     /*
456      * Compilation of ASL source versus DataTable source uses different
457      * compiler subsystems
458      */
459     switch (Gbl_FileType)
460     {
461     /*
462      * Data Table Compilation
463      */
464     case ASL_INPUT_TYPE_ASCII_DATA:
465
466         Status = DtDoCompile ();
467
468         if (Gbl_Signature)
469         {
470             ACPI_FREE (Gbl_Signature);
471             Gbl_Signature = NULL;
472         }
473         AeClearErrorLog ();
474         return (Status);
475
476     /*
477      * ASL Compilation (Optional)
478      */
479     case ASL_INPUT_TYPE_ASCII_ASL:
480
481         /* ACPICA subsystem initialization */
482
483         Status = AdInitialize ();
484         if (ACPI_FAILURE (Status))
485         {
486             return (Status);
487         }
488
489         Status = CmDoCompile ();
490         (void) AcpiTerminate ();
491
492         /*
493          * Return non-zero exit code if there have been errors, unless the
494          * global ignore error flag has been set
495          */
496         if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors))
497         {
498             return (AE_ERROR);
499         }
500
501         AeClearErrorLog ();
502         return (AE_OK);
503
504     case ASL_INPUT_TYPE_BINARY:
505
506         AePrintErrorLog (ASL_FILE_STDERR);
507         return (AE_ERROR);
508
509     default:
510         printf ("Unknown file type %X\n", Gbl_FileType);
511         return (AE_ERROR);
512     }
513 }
514
515
516 /*******************************************************************************
517  *
518  * FUNCTION:    AslDoOnePathname
519  *
520  * PARAMETERS:  Pathname            - Full pathname, possibly with wildcards
521  *
522  * RETURN:      Status
523  *
524  * DESCRIPTION: Process one pathname, possible terminated with a wildcard
525  *              specification. If a wildcard, it is expanded and the multiple
526  *              files are processed.
527  *
528  ******************************************************************************/
529
530 ACPI_STATUS
531 AslDoOnePathname (
532     char                    *Pathname,
533     ASL_PATHNAME_CALLBACK   PathCallback)
534 {
535     ACPI_STATUS             Status = AE_OK;
536     char                    **WildcardList;
537     char                    *Filename;
538     char                    *FullPathname;
539
540
541     /* Split incoming path into a directory/filename combo */
542
543     Status = FlSplitInputPathname (Pathname, &Gbl_DirectoryPath, &Filename);
544     if (ACPI_FAILURE (Status))
545     {
546         return (Status);
547     }
548
549     /* Expand possible wildcard into a file list (Windows/DOS only) */
550
551     WildcardList = AsDoWildcard (Gbl_DirectoryPath, Filename);
552     while (*WildcardList)
553     {
554         FullPathname = ACPI_ALLOCATE (
555             strlen (Gbl_DirectoryPath) + strlen (*WildcardList) + 1);
556
557         /* Construct a full path to the file */
558
559         strcpy (FullPathname, Gbl_DirectoryPath);
560         strcat (FullPathname, *WildcardList);
561
562         /*
563          * If -p not specified, we will use the input filename as the
564          * output filename prefix
565          */
566         if (Gbl_UseDefaultAmlFilename)
567         {
568             Gbl_OutputFilenamePrefix = FullPathname;
569         }
570
571         /* Save status from all compiles */
572
573         Status |= (*PathCallback) (FullPathname);
574
575         ACPI_FREE (FullPathname);
576         ACPI_FREE (*WildcardList);
577         *WildcardList = NULL;
578         WildcardList++;
579     }
580
581     ACPI_FREE (Gbl_DirectoryPath);
582     ACPI_FREE (Filename);
583     return (Status);
584 }
585