xref: /freebsd/sys/contrib/dev/acpica/compiler/aslstartup.c (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
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 - 2018, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include <contrib/dev/acpica/include/actables.h>
154 #include <contrib/dev/acpica/include/acdisasm.h>
155 #include <contrib/dev/acpica/include/acapps.h>
156 #include <contrib/dev/acpica/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 (AcpiGbl_CaptureComments)
237     {
238         Gbl_CommentState.SpacesBefore          = 0;
239         Gbl_CommentState.CommentType           = 1;
240         Gbl_CommentState.LatestParseOp         = 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     else
320     {
321         fprintf (stderr,
322             "Binary file does not contain a valid ACPI table\n");
323     }
324 
325     Type = ASL_INPUT_TYPE_BINARY;
326 
327 
328 Cleanup:
329 
330     /* Must seek back to the start of the file */
331 
332     fseek (Info->Handle, 0, SEEK_SET);
333     return (Type);
334 }
335 
336 
337 /*******************************************************************************
338  *
339  * FUNCTION:    AslDoDisassembly
340  *
341  * PARAMETERS:  None
342  *
343  * RETURN:      Status
344  *
345  * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
346  *              namespace.
347  *
348  ******************************************************************************/
349 
350 static ACPI_STATUS
351 AslDoDisassembly (
352     void)
353 {
354     ACPI_STATUS             Status;
355 
356 
357     /* ACPICA subsystem initialization */
358 
359     Status = AdInitialize ();
360     if (ACPI_FAILURE (Status))
361     {
362         return (Status);
363     }
364 
365     Status = AcpiAllocateRootTable (4);
366     if (ACPI_FAILURE (Status))
367     {
368         AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
369             AcpiFormatException (Status));
370         return (Status);
371     }
372 
373     /* Handle additional output files for disassembler */
374 
375     Gbl_FileType = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
376     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
377 
378     /* This is where the disassembly happens */
379 
380     AcpiGbl_DmOpt_Disasm = TRUE;
381     Status = AdAmlDisassemble (AslToFile,
382         Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
383         &Gbl_Files[ASL_FILE_INPUT].Filename);
384     if (ACPI_FAILURE (Status))
385     {
386         return (Status);
387     }
388 
389     /* Check if any control methods were unresolved */
390 
391     AcpiDmUnresolvedWarning (0);
392 
393     /* Shutdown compiler and ACPICA subsystem */
394 
395     AeClearErrorLog ();
396     (void) AcpiTerminate ();
397 
398     /*
399      * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
400      * .DSL disassembly file, which can now be compiled if requested
401      */
402     if (Gbl_DoCompile)
403     {
404         AcpiOsPrintf ("\nCompiling \"%s\"\n",
405             Gbl_Files[ASL_FILE_INPUT].Filename);
406         return (AE_CTRL_CONTINUE);
407     }
408 
409     /* No need to free the filename string */
410 
411     Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
412 
413     UtDeleteLocalCaches ();
414     return (AE_OK);
415 }
416 
417 
418 /*******************************************************************************
419  *
420  * FUNCTION:    AslDoOneFile
421  *
422  * PARAMETERS:  Filename        - Name of the file
423  *
424  * RETURN:      Status
425  *
426  * DESCRIPTION: Process a single file - either disassemble, compile, or both
427  *
428  ******************************************************************************/
429 
430 ACPI_STATUS
431 AslDoOneFile (
432     char                    *Filename)
433 {
434     ACPI_STATUS             Status;
435 
436 
437     /* Re-initialize "some" compiler/preprocessor globals */
438 
439     AslInitializeGlobals ();
440     PrInitializeGlobals ();
441 
442     /*
443      * Extract the directory path. This path is used for possible include
444      * files and the optional AML filename embedded in the input file
445      * DefinitionBlock declaration.
446      */
447     Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL);
448     if (ACPI_FAILURE (Status))
449     {
450         return (Status);
451     }
452 
453     /* Take a copy of the input filename, convert any backslashes */
454 
455     Gbl_Files[ASL_FILE_INPUT].Filename =
456         UtLocalCacheCalloc (strlen (Filename) + 1);
457 
458     strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
459     UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename);
460 
461     /*
462      * AML Disassembly (Optional)
463      */
464     if (AcpiGbl_DisasmFlag)
465     {
466         Status = AslDoDisassembly ();
467         if (Status != AE_CTRL_CONTINUE)
468         {
469             return (Status);
470         }
471     }
472 
473     /*
474      * Open the input file. Here, this should be an ASCII source file,
475      * either an ASL file or a Data Table file
476      */
477     Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
478     if (ACPI_FAILURE (Status))
479     {
480         AePrintErrorLog (ASL_FILE_STDERR);
481         return (AE_ERROR);
482     }
483 
484     Gbl_OriginalInputFileSize = FlGetFileSize (ASL_FILE_INPUT);
485 
486     /* Determine input file type */
487 
488     Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
489     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
490     {
491         return (AE_ERROR);
492     }
493 
494     /*
495      * If -p not specified, we will use the input filename as the
496      * output filename prefix
497      */
498     if (Gbl_UseDefaultAmlFilename)
499     {
500         Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
501     }
502 
503     /* Open the optional output files (listings, etc.) */
504 
505     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
506     if (ACPI_FAILURE (Status))
507     {
508         AePrintErrorLog (ASL_FILE_STDERR);
509         return (AE_ERROR);
510     }
511 
512     /*
513      * Compilation of ASL source versus DataTable source uses different
514      * compiler subsystems
515      */
516     switch (Gbl_FileType)
517     {
518     /*
519      * Data Table Compilation
520      */
521     case ASL_INPUT_TYPE_ASCII_DATA:
522 
523         Status = DtDoCompile ();
524         if (ACPI_FAILURE (Status))
525         {
526             return (Status);
527         }
528 
529         if (Gbl_Signature)
530         {
531             Gbl_Signature = NULL;
532         }
533 
534         /* Check if any errors occurred during compile */
535 
536         Status = AslCheckForErrorExit ();
537         if (ACPI_FAILURE (Status))
538         {
539             return (Status);
540         }
541 
542         /* Cleanup (for next source file) and exit */
543 
544         AeClearErrorLog ();
545         PrTerminatePreprocessor ();
546         return (Status);
547 
548     /*
549      * ASL Compilation
550      */
551     case ASL_INPUT_TYPE_ASCII_ASL:
552 
553         /* ACPICA subsystem initialization */
554 
555         Status = AdInitialize ();
556         if (ACPI_FAILURE (Status))
557         {
558             return (Status);
559         }
560 
561         (void) CmDoCompile ();
562         (void) AcpiTerminate ();
563 
564         /* Check if any errors occurred during compile */
565 
566         Status = AslCheckForErrorExit ();
567         if (ACPI_FAILURE (Status))
568         {
569             return (Status);
570         }
571 
572         /* Cleanup (for next source file) and exit */
573 
574         AeClearErrorLog ();
575         PrTerminatePreprocessor ();
576 
577         /* ASL-to-ASL+ conversion - Perform immediate disassembly */
578 
579         if (Gbl_DoAslConversion)
580         {
581             /*
582              * New input file is the output AML file from above.
583              * New output is from the input ASL file from above.
584              */
585             Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
586         CvDbgPrint ("OUTPUTFILENAME: %s\n", Gbl_OutputFilenamePrefix);
587             Gbl_Files[ASL_FILE_INPUT].Filename =
588                 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
589             AcpiGbl_DisasmFlag = TRUE;
590             fprintf (stderr, "\n");
591             AslDoDisassembly ();
592 
593             /* delete the AML file. This AML file should never be utilized by AML interpreters. */
594 
595             FlDeleteFile (ASL_FILE_AML_OUTPUT);
596         }
597 
598         return (AE_OK);
599 
600     /*
601      * Binary ACPI table was auto-detected, disassemble it
602      */
603     case ASL_INPUT_TYPE_BINARY_ACPI_TABLE:
604 
605         /* We have what appears to be an ACPI table, disassemble it */
606 
607         FlCloseFile (ASL_FILE_INPUT);
608         Gbl_DoCompile = FALSE;
609         AcpiGbl_DisasmFlag = TRUE;
610         Status = AslDoDisassembly ();
611         return (Status);
612 
613     /* Unknown binary table */
614 
615     case ASL_INPUT_TYPE_BINARY:
616 
617         AePrintErrorLog (ASL_FILE_STDERR);
618         return (AE_ERROR);
619 
620     default:
621 
622         printf ("Unknown file type %X\n", Gbl_FileType);
623         return (AE_ERROR);
624     }
625 }
626 
627 
628 /*******************************************************************************
629  *
630  * FUNCTION:    AslCheckForErrorExit
631  *
632  * PARAMETERS:  None. Examines global exception count array
633  *
634  * RETURN:      Status
635  *
636  * DESCRIPTION: Determine if compiler should abort with error status
637  *
638  ******************************************************************************/
639 
640 ACPI_STATUS
641 AslCheckForErrorExit (
642     void)
643 {
644 
645     /*
646      * Return non-zero exit code if there have been errors, unless the
647      * global ignore error flag has been set
648      */
649     if (!Gbl_IgnoreErrors)
650     {
651         if (Gbl_ExceptionCount[ASL_ERROR] > 0)
652         {
653             return (AE_ERROR);
654         }
655 
656         /* Optionally treat warnings as errors */
657 
658         if (Gbl_WarningsAsErrors)
659         {
660             if ((Gbl_ExceptionCount[ASL_WARNING] > 0)  ||
661                 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
662                 (Gbl_ExceptionCount[ASL_WARNING3] > 0))
663             {
664                 return (AE_ERROR);
665             }
666         }
667     }
668 
669     return (AE_OK);
670 }
671