1 /******************************************************************************
2  *
3  * Module Name: aslstartup - Compiler startup routines, called from main
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "aslcompiler.h"
153 #include "actables.h"
154 #include "acdisasm.h"
155 #include "acapps.h"
156 #include "acconvert.h"
157 
158 #define _COMPONENT          ACPI_COMPILER
159         ACPI_MODULE_NAME    ("aslstartup")
160 
161 
162 /* Local prototypes */
163 
164 static UINT8
165 AslDetectSourceFileType (
166     ASL_FILE_INFO           *Info);
167 
168 static ACPI_STATUS
169 AslDoDisassembly (
170     void);
171 
172 
173 /* Globals */
174 
175 static BOOLEAN          AslToFile = TRUE;
176 
177 
178 /*******************************************************************************
179  *
180  * FUNCTION:    AslInitializeGlobals
181  *
182  * PARAMETERS:  None
183  *
184  * RETURN:      None
185  *
186  * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
187  *              allows multiple files to be disassembled and/or compiled.
188  *
189  ******************************************************************************/
190 
191 void
192 AslInitializeGlobals (
193     void)
194 {
195     UINT32                  i;
196 
197 
198     /* Init compiler globals */
199 
200     Gbl_SyntaxError = 0;
201     Gbl_CurrentColumn = 0;
202     Gbl_CurrentLineNumber = 1;
203     Gbl_LogicalLineNumber = 1;
204     Gbl_CurrentLineOffset = 0;
205     Gbl_InputFieldCount = 0;
206     Gbl_InputByteCount = 0;
207     Gbl_NsLookupCount = 0;
208     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
209 
210     Gbl_ErrorLog = NULL;
211     Gbl_NextError = NULL;
212     Gbl_Signature = NULL;
213     Gbl_FileType = 0;
214 
215     TotalExecutableOpcodes = 0;
216     TotalNamedObjects = 0;
217     TotalKeywords = 0;
218     TotalParseNodes = 0;
219     TotalMethods = 0;
220     TotalAllocations = 0;
221     TotalAllocated = 0;
222     TotalFolds = 0;
223 
224     AslGbl_NextEvent = 0;
225     for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
226     {
227         Gbl_ExceptionCount[i] = 0;
228     }
229 
230     for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++)
231     {
232         Gbl_Files[i].Handle = NULL;
233         Gbl_Files[i].Filename = NULL;
234     }
235 
236     if (Gbl_CaptureComments)
237     {
238         Gbl_CommentState.SpacesBefore          = 0;
239         Gbl_CommentState.CommentType           = 1;
240         Gbl_CommentState.Latest_Parse_Node     = NULL;
241         Gbl_CommentState.ParsingParenBraceNode = NULL;
242         Gbl_CommentState.CaptureComments       = TRUE;
243     }
244 }
245 
246 
247 /*******************************************************************************
248  *
249  * FUNCTION:    AslDetectSourceFileType
250  *
251  * PARAMETERS:  Info            - Name/Handle for the file (must be open)
252  *
253  * RETURN:      File Type
254  *
255  * DESCRIPTION: Determine the type of the input file. Either binary (contains
256  *              non-ASCII characters), ASL file, or an ACPI Data Table file.
257  *
258  ******************************************************************************/
259 
260 static UINT8
261 AslDetectSourceFileType (
262     ASL_FILE_INFO           *Info)
263 {
264     char                    *FileChar;
265     UINT8                   Type = ASL_INPUT_TYPE_ASCII_DATA; /* default */
266     ACPI_STATUS             Status;
267 
268 
269     /* Check for 100% ASCII source file (comments are ignored) */
270 
271     Status = FlIsFileAsciiSource (Info->Filename, FALSE);
272     if (ACPI_SUCCESS (Status))
273     {
274         /*
275          * File contains ASCII source code. Determine if this is an ASL
276          * file or an ACPI data table file.
277          */
278         while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle))
279         {
280             /* Uppercase the buffer for caseless compare */
281 
282             FileChar = Gbl_CurrentLineBuffer;
283             while (*FileChar)
284             {
285                 *FileChar = (char) toupper ((int) *FileChar);
286                 FileChar++;
287             }
288 
289             /* Presence of "DefinitionBlock" indicates actual ASL code */
290 
291             if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
292             {
293                 /* Appears to be an ASL file */
294 
295                 Type = ASL_INPUT_TYPE_ASCII_ASL;
296                 goto Cleanup;
297             }
298         }
299 
300         /* Appears to be an ASCII data table source file */
301 
302         Type = ASL_INPUT_TYPE_ASCII_DATA;
303         goto Cleanup;
304     }
305 
306     /* We have some sort of binary table, check for valid ACPI table */
307 
308     fseek (Info->Handle, 0, SEEK_SET);
309 
310     Status = AcValidateTableHeader (Info->Handle, 0);
311     if (ACPI_SUCCESS (Status))
312     {
313         fprintf (stderr,
314             "Binary file appears to be a valid ACPI table, disassembling\n");
315 
316         Type = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
317         goto Cleanup;
318     }
319 
320     Type = ASL_INPUT_TYPE_BINARY;
321 
322 
323 Cleanup:
324 
325     /* Must seek back to the start of the file */
326 
327     fseek (Info->Handle, 0, SEEK_SET);
328     return (Type);
329 }
330 
331 
332 /*******************************************************************************
333  *
334  * FUNCTION:    AslDoDisassembly
335  *
336  * PARAMETERS:  None
337  *
338  * RETURN:      Status
339  *
340  * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
341  *              namespace.
342  *
343  ******************************************************************************/
344 
345 static ACPI_STATUS
346 AslDoDisassembly (
347     void)
348 {
349     ACPI_STATUS             Status;
350 
351 
352     /* ACPICA subsystem initialization */
353 
354     Status = AdInitialize ();
355     if (ACPI_FAILURE (Status))
356     {
357         return (Status);
358     }
359 
360     Status = AcpiAllocateRootTable (4);
361     if (ACPI_FAILURE (Status))
362     {
363         AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
364             AcpiFormatException (Status));
365         return (Status);
366     }
367 
368     /* Handle additional output files for disassembler */
369 
370     Gbl_FileType = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
371     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
372 
373     /* This is where the disassembly happens */
374 
375     AcpiGbl_DmOpt_Disasm = TRUE;
376     Status = AdAmlDisassemble (AslToFile,
377         Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
378         &Gbl_Files[ASL_FILE_INPUT].Filename);
379     if (ACPI_FAILURE (Status))
380     {
381         return (Status);
382     }
383 
384     /* Check if any control methods were unresolved */
385 
386     AcpiDmUnresolvedWarning (0);
387 
388     /* Shutdown compiler and ACPICA subsystem */
389 
390     AeClearErrorLog ();
391     (void) AcpiTerminate ();
392 
393     /*
394      * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
395      * .DSL disassembly file, which can now be compiled if requested
396      */
397     if (Gbl_DoCompile)
398     {
399         AcpiOsPrintf ("\nCompiling \"%s\"\n",
400             Gbl_Files[ASL_FILE_INPUT].Filename);
401         return (AE_CTRL_CONTINUE);
402     }
403 
404     /* No need to free the filename string */
405 
406     Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
407 
408     CmDeleteCaches ();
409     return (AE_OK);
410 }
411 
412 
413 /*******************************************************************************
414  *
415  * FUNCTION:    AslDoOneFile
416  *
417  * PARAMETERS:  Filename        - Name of the file
418  *
419  * RETURN:      Status
420  *
421  * DESCRIPTION: Process a single file - either disassemble, compile, or both
422  *
423  ******************************************************************************/
424 
425 ACPI_STATUS
426 AslDoOneFile (
427     char                    *Filename)
428 {
429     ACPI_STATUS             Status;
430 
431 
432     /* Re-initialize "some" compiler/preprocessor globals */
433 
434     AslInitializeGlobals ();
435     PrInitializeGlobals ();
436 
437     /*
438      * Extract the directory path. This path is used for possible include
439      * files and the optional AML filename embedded in the input file
440      * DefinitionBlock declaration.
441      */
442     Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL);
443     if (ACPI_FAILURE (Status))
444     {
445         return (Status);
446     }
447 
448     /* Take a copy of the input filename, convert any backslashes */
449 
450     Gbl_Files[ASL_FILE_INPUT].Filename =
451         UtStringCacheCalloc (strlen (Filename) + 1);
452 
453     strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
454     UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename);
455 
456     /*
457      * AML Disassembly (Optional)
458      */
459     if (Gbl_DisasmFlag)
460     {
461         Status = AslDoDisassembly ();
462         if (Status != AE_CTRL_CONTINUE)
463         {
464             return (Status);
465         }
466     }
467 
468     /*
469      * Open the input file. Here, this should be an ASCII source file,
470      * either an ASL file or a Data Table file
471      */
472     Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
473     if (ACPI_FAILURE (Status))
474     {
475         AePrintErrorLog (ASL_FILE_STDERR);
476         return (AE_ERROR);
477     }
478 
479     Gbl_OriginalInputFileSize = FlGetFileSize (ASL_FILE_INPUT);
480 
481     /* Determine input file type */
482 
483     Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
484     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
485     {
486         return (AE_ERROR);
487     }
488 
489     /*
490      * If -p not specified, we will use the input filename as the
491      * output filename prefix
492      */
493     if (Gbl_UseDefaultAmlFilename)
494     {
495         Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
496     }
497 
498     /* Open the optional output files (listings, etc.) */
499 
500     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
501     if (ACPI_FAILURE (Status))
502     {
503         AePrintErrorLog (ASL_FILE_STDERR);
504         return (AE_ERROR);
505     }
506 
507     /*
508      * Compilation of ASL source versus DataTable source uses different
509      * compiler subsystems
510      */
511     switch (Gbl_FileType)
512     {
513     /*
514      * Data Table Compilation
515      */
516     case ASL_INPUT_TYPE_ASCII_DATA:
517 
518         Status = DtDoCompile ();
519         if (ACPI_FAILURE (Status))
520         {
521             return (Status);
522         }
523 
524         if (Gbl_Signature)
525         {
526             Gbl_Signature = NULL;
527         }
528 
529         /* Check if any errors occurred during compile */
530 
531         Status = AslCheckForErrorExit ();
532         if (ACPI_FAILURE (Status))
533         {
534             return (Status);
535         }
536 
537         /* Cleanup (for next source file) and exit */
538 
539         AeClearErrorLog ();
540         PrTerminatePreprocessor ();
541         return (Status);
542 
543     /*
544      * ASL Compilation
545      */
546     case ASL_INPUT_TYPE_ASCII_ASL:
547 
548         /* ACPICA subsystem initialization */
549 
550         Status = AdInitialize ();
551         if (ACPI_FAILURE (Status))
552         {
553             return (Status);
554         }
555 
556         (void) CmDoCompile ();
557         (void) AcpiTerminate ();
558 
559         /* Check if any errors occurred during compile */
560 
561         Status = AslCheckForErrorExit ();
562         if (ACPI_FAILURE (Status))
563         {
564             return (Status);
565         }
566 
567         /* Cleanup (for next source file) and exit */
568 
569         AeClearErrorLog ();
570         PrTerminatePreprocessor ();
571 
572         /* ASL-to-ASL+ conversion - Perform immediate disassembly */
573 
574         if (Gbl_DoAslConversion)
575         {
576             /*
577              * New input file is the output AML file from above.
578              * New output is from the input ASL file from above.
579              */
580             Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
581         CvDbgPrint ("OUTPUTFILENAME: %s\n", Gbl_OutputFilenamePrefix);
582             Gbl_Files[ASL_FILE_INPUT].Filename =
583                 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
584 
585             fprintf (stderr, "\n");
586             AslDoDisassembly ();
587 
588             /* delete the AML file. This AML file should never be utilized by AML interpreters. */
589 
590             FlDeleteFile (ASL_FILE_AML_OUTPUT);
591         }
592 
593         return (AE_OK);
594 
595     /*
596      * Binary ACPI table was auto-detected, disassemble it
597      */
598     case ASL_INPUT_TYPE_BINARY_ACPI_TABLE:
599 
600         /* We have what appears to be an ACPI table, disassemble it */
601 
602         FlCloseFile (ASL_FILE_INPUT);
603         Gbl_DoCompile = FALSE;
604         Gbl_DisasmFlag = TRUE;
605         Status = AslDoDisassembly ();
606         return (Status);
607 
608     /* Unknown binary table */
609 
610     case ASL_INPUT_TYPE_BINARY:
611 
612         AePrintErrorLog (ASL_FILE_STDERR);
613         return (AE_ERROR);
614 
615     default:
616 
617         printf ("Unknown file type %X\n", Gbl_FileType);
618         return (AE_ERROR);
619     }
620 }
621 
622 
623 /*******************************************************************************
624  *
625  * FUNCTION:    AslCheckForErrorExit
626  *
627  * PARAMETERS:  None. Examines global exception count array
628  *
629  * RETURN:      Status
630  *
631  * DESCRIPTION: Determine if compiler should abort with error status
632  *
633  ******************************************************************************/
634 
635 ACPI_STATUS
636 AslCheckForErrorExit (
637     void)
638 {
639 
640     /*
641      * Return non-zero exit code if there have been errors, unless the
642      * global ignore error flag has been set
643      */
644     if (!Gbl_IgnoreErrors)
645     {
646         if (Gbl_ExceptionCount[ASL_ERROR] > 0)
647         {
648             return (AE_ERROR);
649         }
650 
651         /* Optionally treat warnings as errors */
652 
653         if (Gbl_WarningsAsErrors)
654         {
655             if ((Gbl_ExceptionCount[ASL_WARNING] > 0)  ||
656                 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
657                 (Gbl_ExceptionCount[ASL_WARNING3] > 0))
658             {
659                 return (AE_ERROR);
660             }
661         }
662     }
663 
664     return (AE_OK);
665 }
666