aslstartup.c revision 209746
198937Sdes
298937Sdes/******************************************************************************
398937Sdes *
498937Sdes * Module Name: aslstartup - Compiler startup routines, called from main
598937Sdes *
698937Sdes *****************************************************************************/
798937Sdes
898937Sdes/******************************************************************************
998937Sdes *
1098937Sdes * 1. Copyright Notice
1198937Sdes *
1298937Sdes * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
1398937Sdes * All rights reserved.
1498937Sdes *
1598937Sdes * 2. License
1698937Sdes *
1798937Sdes * 2.1. This is your license from Intel Corp. under its intellectual property
1898937Sdes * rights.  You may have additional license terms from the party that provided
1998937Sdes * you this software, covering your right to use that party's intellectual
2098937Sdes * property rights.
2198937Sdes *
2298937Sdes * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2398937Sdes * copy of the source code appearing in this file ("Covered Code") an
2498937Sdes * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25240075Sdes * base code distributed originally by Intel ("Original Intel Code") to copy,
2698937Sdes * make derivatives, distribute, use and display any portion of the Covered
2798937Sdes * Code in any form, with the right to sublicense such rights; and
2898937Sdes *
2998937Sdes * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30157016Sdes * license (with the right to sublicense), under only those claims of Intel
31157016Sdes * patents that are infringed by the Original Intel Code, to make, use, sell,
3298937Sdes * offer to sell, and import the Covered Code and derivative works thereof
3398937Sdes * solely to the minimum extent necessary to exercise the above copyright
34157016Sdes * license, and in no event shall the patent license extend to any additions
35157016Sdes * to or modifications of the Original Intel Code.  No other license or right
36157016Sdes * is granted directly or by implication, estoppel or otherwise;
3798937Sdes *
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 <contrib/dev/acpica/compiler/aslcompiler.h>
119#include <contrib/dev/acpica/include/actables.h>
120#include <contrib/dev/acpica/include/acapps.h>
121
122#define _COMPONENT          ACPI_COMPILER
123        ACPI_MODULE_NAME    ("aslstartup")
124
125
126#define ASL_MAX_FILES   256
127char                    *FileList[ASL_MAX_FILES];
128int                     FileCount;
129BOOLEAN                 AslToFile = TRUE;
130
131
132/* Local prototypes */
133
134static char **
135AsDoWildcard (
136    char                    *DirectoryPathname,
137    char                    *FileSpecifier);
138
139UINT8
140AslDetectSourceFileType (
141    ASL_FILE_INFO           *Info);
142
143
144/*******************************************************************************
145 *
146 * FUNCTION:    AslInitializeGlobals
147 *
148 * PARAMETERS:  None
149 *
150 * RETURN:      None
151 *
152 * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
153 *              allows multiple files to be disassembled and/or compiled.
154 *
155 ******************************************************************************/
156
157void
158AslInitializeGlobals (
159    void)
160{
161    UINT32                  i;
162
163
164    /* Init compiler globals */
165
166    Gbl_CurrentColumn = 0;
167    Gbl_CurrentLineNumber = 1;
168    Gbl_LogicalLineNumber = 1;
169    Gbl_CurrentLineOffset = 0;
170    Gbl_InputFieldCount = 0;
171    Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
172
173    Gbl_ErrorLog = NULL;
174    Gbl_NextError = NULL;
175    Gbl_Signature = NULL;
176    Gbl_FileType = 0;
177
178    AslGbl_NextEvent = 0;
179    for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
180    {
181        Gbl_ExceptionCount[i] = 0;
182    }
183
184    Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
185    Gbl_Files[ASL_FILE_AML_OUTPUT].Handle = NULL;
186
187    Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename = NULL;
188    Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle = NULL;
189}
190
191
192/******************************************************************************
193 *
194 * FUNCTION:    AsDoWildcard
195 *
196 * PARAMETERS:  None
197 *
198 * RETURN:      None
199 *
200 * DESCRIPTION: Process files via wildcards. This function is for the Windows
201 *              case only.
202 *
203 ******************************************************************************/
204
205static char **
206AsDoWildcard (
207    char                    *DirectoryPathname,
208    char                    *FileSpecifier)
209{
210#ifdef WIN32
211    void                    *DirInfo;
212    char                    *Filename;
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
280UINT8
281AslDetectSourceFileType (
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
329Cleanup:
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
350ACPI_STATUS
351AslDoOneFile (
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        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        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
530ACPI_STATUS
531AslDoOnePathname (
532    char                    *Pathname)
533{
534    ACPI_STATUS             Status = AE_OK;
535    char                    **FileList;
536    char                    *Filename;
537    char                    *FullPathname;
538
539
540    /* Split incoming path into a directory/filename combo */
541
542    Status = FlSplitInputPathname (Pathname, &Gbl_DirectoryPath, &Filename);
543    if (ACPI_FAILURE (Status))
544    {
545        return (Status);
546    }
547
548    /* Expand possible wildcard into a file list (Windows/DOS only) */
549
550    FileList = AsDoWildcard (Gbl_DirectoryPath, Filename);
551    while (*FileList)
552    {
553        FullPathname = ACPI_ALLOCATE (
554            strlen (Gbl_DirectoryPath) + strlen (*FileList) + 1);
555
556        /* Construct a full path to the file */
557
558        strcpy (FullPathname, Gbl_DirectoryPath);
559        strcat (FullPathname, *FileList);
560
561        /*
562         * If -p not specified, we will use the input filename as the
563         * output filename prefix
564         */
565        if (Gbl_UseDefaultAmlFilename)
566        {
567            Gbl_OutputFilenamePrefix = FullPathname;
568        }
569
570        /* Save status from all compiles */
571
572        Status |= AslDoOneFile (FullPathname);
573
574        ACPI_FREE (FullPathname);
575        ACPI_FREE (*FileList);
576        *FileList = NULL;
577        FileList++;
578    }
579
580    ACPI_FREE (Gbl_DirectoryPath);
581    ACPI_FREE (Filename);
582    return (Status);
583}
584
585