xref: /freebsd/sys/contrib/dev/acpica/compiler/aslstartup.c (revision af051161437e44a6ceabf9e196f0eb8f33d92b86)
1a9f12690SJung-uk Kim /******************************************************************************
2a9f12690SJung-uk Kim  *
3a9f12690SJung-uk Kim  * Module Name: aslstartup - Compiler startup routines, called from main
4a9f12690SJung-uk Kim  *
5a9f12690SJung-uk Kim  *****************************************************************************/
6a9f12690SJung-uk Kim 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
110d84335fSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12a9f12690SJung-uk Kim  * All rights reserved.
13a9f12690SJung-uk Kim  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-uk Kim  *
119d244b227SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120d244b227SJung-uk Kim  * modification, are permitted provided that the following conditions
121d244b227SJung-uk Kim  * are met:
122d244b227SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123d244b227SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124d244b227SJung-uk Kim  *    without modification.
125d244b227SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126d244b227SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127d244b227SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128d244b227SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129d244b227SJung-uk Kim  *    binary redistribution.
130d244b227SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131d244b227SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132d244b227SJung-uk Kim  *    from this software without specific prior written permission.
133a9f12690SJung-uk Kim  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147d244b227SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148d244b227SJung-uk Kim  * Software Foundation.
149a9f12690SJung-uk Kim  *
1500d84335fSJung-uk Kim  *****************************************************************************/
151a9f12690SJung-uk Kim 
152ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
153ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/actables.h>
1549c48c75eSJung-uk Kim #include <contrib/dev/acpica/include/acdisasm.h>
155ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/acapps.h>
1560d84335fSJung-uk Kim #include <contrib/dev/acpica/include/acconvert.h>
157a9f12690SJung-uk Kim 
158a9f12690SJung-uk Kim #define _COMPONENT          ACPI_COMPILER
159a9f12690SJung-uk Kim         ACPI_MODULE_NAME    ("aslstartup")
160a9f12690SJung-uk Kim 
161a9f12690SJung-uk Kim 
162a9f12690SJung-uk Kim /* Local prototypes */
163a9f12690SJung-uk Kim 
16442fecd12SJung-uk Kim static UINT8
165a88e22b7SJung-uk Kim AslDetectSourceFileType (
166a88e22b7SJung-uk Kim     ASL_FILE_INFO           *Info);
167a88e22b7SJung-uk Kim 
1689c48c75eSJung-uk Kim static ACPI_STATUS
1699c48c75eSJung-uk Kim AslDoDisassembly (
1709c48c75eSJung-uk Kim     void);
1719c48c75eSJung-uk Kim 
172a9f12690SJung-uk Kim 
173313a0c13SJung-uk Kim /* Globals */
174313a0c13SJung-uk Kim 
175313a0c13SJung-uk Kim static BOOLEAN          AslToFile = TRUE;
176313a0c13SJung-uk Kim 
177313a0c13SJung-uk Kim 
178a9f12690SJung-uk Kim /*******************************************************************************
179a9f12690SJung-uk Kim  *
180a9f12690SJung-uk Kim  * FUNCTION:    AslInitializeGlobals
181a9f12690SJung-uk Kim  *
182a9f12690SJung-uk Kim  * PARAMETERS:  None
183a9f12690SJung-uk Kim  *
184a9f12690SJung-uk Kim  * RETURN:      None
185a9f12690SJung-uk Kim  *
186a9f12690SJung-uk Kim  * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
187a9f12690SJung-uk Kim  *              allows multiple files to be disassembled and/or compiled.
188a9f12690SJung-uk Kim  *
189a9f12690SJung-uk Kim  ******************************************************************************/
190a9f12690SJung-uk Kim 
191a88e22b7SJung-uk Kim void
192a9f12690SJung-uk Kim AslInitializeGlobals (
193a9f12690SJung-uk Kim     void)
194a9f12690SJung-uk Kim {
195a9f12690SJung-uk Kim     UINT32                  i;
196a9f12690SJung-uk Kim 
197a9f12690SJung-uk Kim 
198a9f12690SJung-uk Kim     /* Init compiler globals */
199a9f12690SJung-uk Kim 
2001c0e1b6dSJung-uk Kim     Gbl_SyntaxError = 0;
201a9f12690SJung-uk Kim     Gbl_CurrentColumn = 0;
202a9f12690SJung-uk Kim     Gbl_CurrentLineNumber = 1;
203a9f12690SJung-uk Kim     Gbl_LogicalLineNumber = 1;
204a9f12690SJung-uk Kim     Gbl_CurrentLineOffset = 0;
205a88e22b7SJung-uk Kim     Gbl_InputFieldCount = 0;
2064c52cad2SJung-uk Kim     Gbl_InputByteCount = 0;
2074c52cad2SJung-uk Kim     Gbl_NsLookupCount = 0;
208a9f12690SJung-uk Kim     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
209a9f12690SJung-uk Kim 
210a9f12690SJung-uk Kim     Gbl_ErrorLog = NULL;
211a9f12690SJung-uk Kim     Gbl_NextError = NULL;
212a88e22b7SJung-uk Kim     Gbl_Signature = NULL;
213a88e22b7SJung-uk Kim     Gbl_FileType = 0;
214a9f12690SJung-uk Kim 
2154c52cad2SJung-uk Kim     TotalExecutableOpcodes = 0;
2164c52cad2SJung-uk Kim     TotalNamedObjects = 0;
2174c52cad2SJung-uk Kim     TotalKeywords = 0;
2184c52cad2SJung-uk Kim     TotalParseNodes = 0;
2194c52cad2SJung-uk Kim     TotalMethods = 0;
2204c52cad2SJung-uk Kim     TotalAllocations = 0;
2214c52cad2SJung-uk Kim     TotalAllocated = 0;
2224c52cad2SJung-uk Kim     TotalFolds = 0;
2234c52cad2SJung-uk Kim 
224a9f12690SJung-uk Kim     AslGbl_NextEvent = 0;
225a9f12690SJung-uk Kim     for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
226a9f12690SJung-uk Kim     {
227a9f12690SJung-uk Kim         Gbl_ExceptionCount[i] = 0;
228a9f12690SJung-uk Kim     }
229a9f12690SJung-uk Kim 
2304c52cad2SJung-uk Kim     for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++)
2314c52cad2SJung-uk Kim     {
2324c52cad2SJung-uk Kim         Gbl_Files[i].Handle = NULL;
2334c52cad2SJung-uk Kim         Gbl_Files[i].Filename = NULL;
2344c52cad2SJung-uk Kim     }
2350d84335fSJung-uk Kim 
2360d84335fSJung-uk Kim     if (Gbl_CaptureComments)
2370d84335fSJung-uk Kim     {
2380d84335fSJung-uk Kim         Gbl_CommentState.SpacesBefore          = 0;
2390d84335fSJung-uk Kim         Gbl_CommentState.CommentType           = 1;
2400d84335fSJung-uk Kim         Gbl_CommentState.Latest_Parse_Node     = NULL;
2410d84335fSJung-uk Kim         Gbl_CommentState.ParsingParenBraceNode = NULL;
2420d84335fSJung-uk Kim         Gbl_CommentState.CaptureComments       = TRUE;
2430d84335fSJung-uk Kim     }
244a9f12690SJung-uk Kim }
245a9f12690SJung-uk Kim 
246a9f12690SJung-uk Kim 
247a9f12690SJung-uk Kim /*******************************************************************************
248a9f12690SJung-uk Kim  *
249a88e22b7SJung-uk Kim  * FUNCTION:    AslDetectSourceFileType
250a88e22b7SJung-uk Kim  *
251a88e22b7SJung-uk Kim  * PARAMETERS:  Info            - Name/Handle for the file (must be open)
252a88e22b7SJung-uk Kim  *
253a88e22b7SJung-uk Kim  * RETURN:      File Type
254a88e22b7SJung-uk Kim  *
255a88e22b7SJung-uk Kim  * DESCRIPTION: Determine the type of the input file. Either binary (contains
256a88e22b7SJung-uk Kim  *              non-ASCII characters), ASL file, or an ACPI Data Table file.
257a88e22b7SJung-uk Kim  *
258a88e22b7SJung-uk Kim  ******************************************************************************/
259a88e22b7SJung-uk Kim 
26042fecd12SJung-uk Kim static UINT8
261a88e22b7SJung-uk Kim AslDetectSourceFileType (
262a88e22b7SJung-uk Kim     ASL_FILE_INFO           *Info)
263a88e22b7SJung-uk Kim {
264a88e22b7SJung-uk Kim     char                    *FileChar;
265f8146b88SJung-uk Kim     UINT8                   Type = ASL_INPUT_TYPE_ASCII_DATA; /* default */
266a88e22b7SJung-uk Kim     ACPI_STATUS             Status;
267a88e22b7SJung-uk Kim 
268a88e22b7SJung-uk Kim 
269a88e22b7SJung-uk Kim     /* Check for 100% ASCII source file (comments are ignored) */
270a88e22b7SJung-uk Kim 
271f8146b88SJung-uk Kim     Status = FlIsFileAsciiSource (Info->Filename, FALSE);
272f8146b88SJung-uk Kim     if (ACPI_SUCCESS (Status))
273a88e22b7SJung-uk Kim     {
274a88e22b7SJung-uk Kim         /*
275f8146b88SJung-uk Kim          * File contains ASCII source code. Determine if this is an ASL
276f8146b88SJung-uk Kim          * file or an ACPI data table file.
277a88e22b7SJung-uk Kim          */
278042ff955SJung-uk Kim         while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle))
279a88e22b7SJung-uk Kim         {
280a88e22b7SJung-uk Kim             /* Uppercase the buffer for caseless compare */
281a88e22b7SJung-uk Kim 
282a88e22b7SJung-uk Kim             FileChar = Gbl_CurrentLineBuffer;
283a88e22b7SJung-uk Kim             while (*FileChar)
284a88e22b7SJung-uk Kim             {
285a88e22b7SJung-uk Kim                 *FileChar = (char) toupper ((int) *FileChar);
286a88e22b7SJung-uk Kim                 FileChar++;
287a88e22b7SJung-uk Kim             }
288a88e22b7SJung-uk Kim 
289a88e22b7SJung-uk Kim             /* Presence of "DefinitionBlock" indicates actual ASL code */
290a88e22b7SJung-uk Kim 
291a88e22b7SJung-uk Kim             if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
292a88e22b7SJung-uk Kim             {
293a88e22b7SJung-uk Kim                 /* Appears to be an ASL file */
294a88e22b7SJung-uk Kim 
295a88e22b7SJung-uk Kim                 Type = ASL_INPUT_TYPE_ASCII_ASL;
296a88e22b7SJung-uk Kim                 goto Cleanup;
297a88e22b7SJung-uk Kim             }
298a88e22b7SJung-uk Kim         }
299a88e22b7SJung-uk Kim 
300f8146b88SJung-uk Kim         /* Appears to be an ASCII data table source file */
301a88e22b7SJung-uk Kim 
302a88e22b7SJung-uk Kim         Type = ASL_INPUT_TYPE_ASCII_DATA;
303f8146b88SJung-uk Kim         goto Cleanup;
304f8146b88SJung-uk Kim     }
305f8146b88SJung-uk Kim 
306f8146b88SJung-uk Kim     /* We have some sort of binary table, check for valid ACPI table */
307f8146b88SJung-uk Kim 
308f8146b88SJung-uk Kim     fseek (Info->Handle, 0, SEEK_SET);
309f8146b88SJung-uk Kim 
310f8146b88SJung-uk Kim     Status = AcValidateTableHeader (Info->Handle, 0);
311f8146b88SJung-uk Kim     if (ACPI_SUCCESS (Status))
312f8146b88SJung-uk Kim     {
313f8146b88SJung-uk Kim         fprintf (stderr,
314f8146b88SJung-uk Kim             "Binary file appears to be a valid ACPI table, disassembling\n");
315f8146b88SJung-uk Kim 
316f8146b88SJung-uk Kim         Type = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
317f8146b88SJung-uk Kim         goto Cleanup;
318f8146b88SJung-uk Kim     }
319*af051161SJung-uk Kim     else
320*af051161SJung-uk Kim     {
321*af051161SJung-uk Kim         fprintf (stderr,
322*af051161SJung-uk Kim             "Binary file does not contain a valid ACPI table\n");
323*af051161SJung-uk Kim     }
324f8146b88SJung-uk Kim 
325f8146b88SJung-uk Kim     Type = ASL_INPUT_TYPE_BINARY;
326f8146b88SJung-uk Kim 
327a88e22b7SJung-uk Kim 
328a88e22b7SJung-uk Kim Cleanup:
329a88e22b7SJung-uk Kim 
330a88e22b7SJung-uk Kim     /* Must seek back to the start of the file */
331a88e22b7SJung-uk Kim 
332a88e22b7SJung-uk Kim     fseek (Info->Handle, 0, SEEK_SET);
333a88e22b7SJung-uk Kim     return (Type);
334a88e22b7SJung-uk Kim }
335a88e22b7SJung-uk Kim 
336a88e22b7SJung-uk Kim 
337a88e22b7SJung-uk Kim /*******************************************************************************
338a88e22b7SJung-uk Kim  *
3399c48c75eSJung-uk Kim  * FUNCTION:    AslDoDisassembly
3409c48c75eSJung-uk Kim  *
3419c48c75eSJung-uk Kim  * PARAMETERS:  None
3429c48c75eSJung-uk Kim  *
3439c48c75eSJung-uk Kim  * RETURN:      Status
3449c48c75eSJung-uk Kim  *
3459c48c75eSJung-uk Kim  * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
3469c48c75eSJung-uk Kim  *              namespace.
3479c48c75eSJung-uk Kim  *
3489c48c75eSJung-uk Kim  ******************************************************************************/
3499c48c75eSJung-uk Kim 
3509c48c75eSJung-uk Kim static ACPI_STATUS
3519c48c75eSJung-uk Kim AslDoDisassembly (
3529c48c75eSJung-uk Kim     void)
3539c48c75eSJung-uk Kim {
3549c48c75eSJung-uk Kim     ACPI_STATUS             Status;
3559c48c75eSJung-uk Kim 
3569c48c75eSJung-uk Kim 
3579c48c75eSJung-uk Kim     /* ACPICA subsystem initialization */
3589c48c75eSJung-uk Kim 
3599c48c75eSJung-uk Kim     Status = AdInitialize ();
3609c48c75eSJung-uk Kim     if (ACPI_FAILURE (Status))
3619c48c75eSJung-uk Kim     {
3629c48c75eSJung-uk Kim         return (Status);
3639c48c75eSJung-uk Kim     }
3649c48c75eSJung-uk Kim 
3659c48c75eSJung-uk Kim     Status = AcpiAllocateRootTable (4);
3669c48c75eSJung-uk Kim     if (ACPI_FAILURE (Status))
3679c48c75eSJung-uk Kim     {
3689c48c75eSJung-uk Kim         AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
3699c48c75eSJung-uk Kim             AcpiFormatException (Status));
3709c48c75eSJung-uk Kim         return (Status);
3719c48c75eSJung-uk Kim     }
3729c48c75eSJung-uk Kim 
373fe0f0bbbSJung-uk Kim     /* Handle additional output files for disassembler */
374fe0f0bbbSJung-uk Kim 
375f8146b88SJung-uk Kim     Gbl_FileType = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
376fe0f0bbbSJung-uk Kim     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
377fe0f0bbbSJung-uk Kim 
3789c48c75eSJung-uk Kim     /* This is where the disassembly happens */
3799c48c75eSJung-uk Kim 
38070e6ab8fSJung-uk Kim     AcpiGbl_DmOpt_Disasm = TRUE;
3819c48c75eSJung-uk Kim     Status = AdAmlDisassemble (AslToFile,
3829c48c75eSJung-uk Kim         Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
383313a0c13SJung-uk Kim         &Gbl_Files[ASL_FILE_INPUT].Filename);
3849c48c75eSJung-uk Kim     if (ACPI_FAILURE (Status))
3859c48c75eSJung-uk Kim     {
3869c48c75eSJung-uk Kim         return (Status);
3879c48c75eSJung-uk Kim     }
3889c48c75eSJung-uk Kim 
3899c48c75eSJung-uk Kim     /* Check if any control methods were unresolved */
3909c48c75eSJung-uk Kim 
3919c48c75eSJung-uk Kim     AcpiDmUnresolvedWarning (0);
3929c48c75eSJung-uk Kim 
3939c48c75eSJung-uk Kim     /* Shutdown compiler and ACPICA subsystem */
3949c48c75eSJung-uk Kim 
3959c48c75eSJung-uk Kim     AeClearErrorLog ();
3969c48c75eSJung-uk Kim     (void) AcpiTerminate ();
3979c48c75eSJung-uk Kim 
3989c48c75eSJung-uk Kim     /*
3999c48c75eSJung-uk Kim      * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
4009c48c75eSJung-uk Kim      * .DSL disassembly file, which can now be compiled if requested
4019c48c75eSJung-uk Kim      */
4029c48c75eSJung-uk Kim     if (Gbl_DoCompile)
4039c48c75eSJung-uk Kim     {
4049c48c75eSJung-uk Kim         AcpiOsPrintf ("\nCompiling \"%s\"\n",
4059c48c75eSJung-uk Kim             Gbl_Files[ASL_FILE_INPUT].Filename);
4069c48c75eSJung-uk Kim         return (AE_CTRL_CONTINUE);
4079c48c75eSJung-uk Kim     }
4089c48c75eSJung-uk Kim 
409313a0c13SJung-uk Kim     /* No need to free the filename string */
410313a0c13SJung-uk Kim 
4119c48c75eSJung-uk Kim     Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
412313a0c13SJung-uk Kim 
413313a0c13SJung-uk Kim     CmDeleteCaches ();
4149c48c75eSJung-uk Kim     return (AE_OK);
4159c48c75eSJung-uk Kim }
4169c48c75eSJung-uk Kim 
4179c48c75eSJung-uk Kim 
4189c48c75eSJung-uk Kim /*******************************************************************************
4199c48c75eSJung-uk Kim  *
420a9f12690SJung-uk Kim  * FUNCTION:    AslDoOneFile
421a9f12690SJung-uk Kim  *
422a9f12690SJung-uk Kim  * PARAMETERS:  Filename        - Name of the file
423a9f12690SJung-uk Kim  *
424a9f12690SJung-uk Kim  * RETURN:      Status
425a9f12690SJung-uk Kim  *
426a9f12690SJung-uk Kim  * DESCRIPTION: Process a single file - either disassemble, compile, or both
427a9f12690SJung-uk Kim  *
428a9f12690SJung-uk Kim  ******************************************************************************/
429a9f12690SJung-uk Kim 
430a9f12690SJung-uk Kim ACPI_STATUS
431a9f12690SJung-uk Kim AslDoOneFile (
432a9f12690SJung-uk Kim     char                    *Filename)
433a9f12690SJung-uk Kim {
434a9f12690SJung-uk Kim     ACPI_STATUS             Status;
435a9f12690SJung-uk Kim 
436a9f12690SJung-uk Kim 
4374c52cad2SJung-uk Kim     /* Re-initialize "some" compiler/preprocessor globals */
438a9f12690SJung-uk Kim 
439a9f12690SJung-uk Kim     AslInitializeGlobals ();
4404c52cad2SJung-uk Kim     PrInitializeGlobals ();
4414c52cad2SJung-uk Kim 
442313a0c13SJung-uk Kim     /*
443313a0c13SJung-uk Kim      * Extract the directory path. This path is used for possible include
444313a0c13SJung-uk Kim      * files and the optional AML filename embedded in the input file
445313a0c13SJung-uk Kim      * DefinitionBlock declaration.
446313a0c13SJung-uk Kim      */
447313a0c13SJung-uk Kim     Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL);
448313a0c13SJung-uk Kim     if (ACPI_FAILURE (Status))
449313a0c13SJung-uk Kim     {
450313a0c13SJung-uk Kim         return (Status);
451313a0c13SJung-uk Kim     }
452313a0c13SJung-uk Kim 
453313a0c13SJung-uk Kim     /* Take a copy of the input filename, convert any backslashes */
454313a0c13SJung-uk Kim 
455313a0c13SJung-uk Kim     Gbl_Files[ASL_FILE_INPUT].Filename =
456313a0c13SJung-uk Kim         UtStringCacheCalloc (strlen (Filename) + 1);
457313a0c13SJung-uk Kim 
458313a0c13SJung-uk Kim     strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
459313a0c13SJung-uk Kim     UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename);
460a9f12690SJung-uk Kim 
461a9f12690SJung-uk Kim     /*
462a9f12690SJung-uk Kim      * AML Disassembly (Optional)
463a9f12690SJung-uk Kim      */
464*af051161SJung-uk Kim     if (AcpiGbl_DisasmFlag)
465a9f12690SJung-uk Kim     {
4669c48c75eSJung-uk Kim         Status = AslDoDisassembly ();
4679c48c75eSJung-uk Kim         if (Status != AE_CTRL_CONTINUE)
468a9f12690SJung-uk Kim         {
469a9f12690SJung-uk Kim             return (Status);
470a9f12690SJung-uk Kim         }
471a9f12690SJung-uk Kim     }
472a9f12690SJung-uk Kim 
473a9f12690SJung-uk Kim     /*
474a88e22b7SJung-uk Kim      * Open the input file. Here, this should be an ASCII source file,
475a88e22b7SJung-uk Kim      * either an ASL file or a Data Table file
476a9f12690SJung-uk Kim      */
477a88e22b7SJung-uk Kim     Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
478a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
479a9f12690SJung-uk Kim     {
480a88e22b7SJung-uk Kim         AePrintErrorLog (ASL_FILE_STDERR);
481a88e22b7SJung-uk Kim         return (AE_ERROR);
482a88e22b7SJung-uk Kim     }
483a88e22b7SJung-uk Kim 
4845ef50723SJung-uk Kim     Gbl_OriginalInputFileSize = FlGetFileSize (ASL_FILE_INPUT);
4855ef50723SJung-uk Kim 
486a88e22b7SJung-uk Kim     /* Determine input file type */
487a88e22b7SJung-uk Kim 
488a88e22b7SJung-uk Kim     Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
489a88e22b7SJung-uk Kim     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
490a88e22b7SJung-uk Kim     {
491a88e22b7SJung-uk Kim         return (AE_ERROR);
492a88e22b7SJung-uk Kim     }
493a88e22b7SJung-uk Kim 
494a9f12690SJung-uk Kim     /*
495a9f12690SJung-uk Kim      * If -p not specified, we will use the input filename as the
496a9f12690SJung-uk Kim      * output filename prefix
497a9f12690SJung-uk Kim      */
498a9f12690SJung-uk Kim     if (Gbl_UseDefaultAmlFilename)
499a9f12690SJung-uk Kim     {
500a9f12690SJung-uk Kim         Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
501a9f12690SJung-uk Kim     }
502a9f12690SJung-uk Kim 
503a88e22b7SJung-uk Kim     /* Open the optional output files (listings, etc.) */
504a88e22b7SJung-uk Kim 
505a88e22b7SJung-uk Kim     Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
506a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
507a88e22b7SJung-uk Kim     {
508a88e22b7SJung-uk Kim         AePrintErrorLog (ASL_FILE_STDERR);
509a88e22b7SJung-uk Kim         return (AE_ERROR);
510a88e22b7SJung-uk Kim     }
511a88e22b7SJung-uk Kim 
512a88e22b7SJung-uk Kim     /*
513a88e22b7SJung-uk Kim      * Compilation of ASL source versus DataTable source uses different
514a88e22b7SJung-uk Kim      * compiler subsystems
515a88e22b7SJung-uk Kim      */
516a88e22b7SJung-uk Kim     switch (Gbl_FileType)
517a88e22b7SJung-uk Kim     {
518a88e22b7SJung-uk Kim     /*
519a88e22b7SJung-uk Kim      * Data Table Compilation
520a88e22b7SJung-uk Kim      */
521a88e22b7SJung-uk Kim     case ASL_INPUT_TYPE_ASCII_DATA:
522a88e22b7SJung-uk Kim 
523a88e22b7SJung-uk Kim         Status = DtDoCompile ();
524eef1b955SJung-uk Kim         if (ACPI_FAILURE (Status))
525eef1b955SJung-uk Kim         {
526eef1b955SJung-uk Kim             return (Status);
527eef1b955SJung-uk Kim         }
528a88e22b7SJung-uk Kim 
529a88e22b7SJung-uk Kim         if (Gbl_Signature)
530a88e22b7SJung-uk Kim         {
531a88e22b7SJung-uk Kim             Gbl_Signature = NULL;
532a88e22b7SJung-uk Kim         }
533eef1b955SJung-uk Kim 
534eef1b955SJung-uk Kim         /* Check if any errors occurred during compile */
535eef1b955SJung-uk Kim 
536eef1b955SJung-uk Kim         Status = AslCheckForErrorExit ();
537eef1b955SJung-uk Kim         if (ACPI_FAILURE (Status))
538eef1b955SJung-uk Kim         {
539eef1b955SJung-uk Kim             return (Status);
540eef1b955SJung-uk Kim         }
541eef1b955SJung-uk Kim 
542eef1b955SJung-uk Kim         /* Cleanup (for next source file) and exit */
543eef1b955SJung-uk Kim 
544a88e22b7SJung-uk Kim         AeClearErrorLog ();
5454c52cad2SJung-uk Kim         PrTerminatePreprocessor ();
546a88e22b7SJung-uk Kim         return (Status);
547a88e22b7SJung-uk Kim 
548a88e22b7SJung-uk Kim     /*
549eef1b955SJung-uk Kim      * ASL Compilation
550a88e22b7SJung-uk Kim      */
551a88e22b7SJung-uk Kim     case ASL_INPUT_TYPE_ASCII_ASL:
552a88e22b7SJung-uk Kim 
553a88e22b7SJung-uk Kim         /* ACPICA subsystem initialization */
554a9f12690SJung-uk Kim 
555a9f12690SJung-uk Kim         Status = AdInitialize ();
556a9f12690SJung-uk Kim         if (ACPI_FAILURE (Status))
557a9f12690SJung-uk Kim         {
558a9f12690SJung-uk Kim             return (Status);
559a9f12690SJung-uk Kim         }
560a9f12690SJung-uk Kim 
561eef1b955SJung-uk Kim         (void) CmDoCompile ();
56242fecd12SJung-uk Kim         (void) AcpiTerminate ();
563a9f12690SJung-uk Kim 
564eef1b955SJung-uk Kim         /* Check if any errors occurred during compile */
565eef1b955SJung-uk Kim 
566eef1b955SJung-uk Kim         Status = AslCheckForErrorExit ();
567eef1b955SJung-uk Kim         if (ACPI_FAILURE (Status))
568a9f12690SJung-uk Kim         {
569eef1b955SJung-uk Kim             return (Status);
570a9f12690SJung-uk Kim         }
571a9f12690SJung-uk Kim 
572eef1b955SJung-uk Kim         /* Cleanup (for next source file) and exit */
573eef1b955SJung-uk Kim 
574a9f12690SJung-uk Kim         AeClearErrorLog ();
5754c52cad2SJung-uk Kim         PrTerminatePreprocessor ();
5760d84335fSJung-uk Kim 
5770d84335fSJung-uk Kim         /* ASL-to-ASL+ conversion - Perform immediate disassembly */
5780d84335fSJung-uk Kim 
5790d84335fSJung-uk Kim         if (Gbl_DoAslConversion)
5800d84335fSJung-uk Kim         {
5810d84335fSJung-uk Kim             /*
5820d84335fSJung-uk Kim              * New input file is the output AML file from above.
5830d84335fSJung-uk Kim              * New output is from the input ASL file from above.
5840d84335fSJung-uk Kim              */
5850d84335fSJung-uk Kim             Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
5860d84335fSJung-uk Kim         CvDbgPrint ("OUTPUTFILENAME: %s\n", Gbl_OutputFilenamePrefix);
5870d84335fSJung-uk Kim             Gbl_Files[ASL_FILE_INPUT].Filename =
5880d84335fSJung-uk Kim                 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
589*af051161SJung-uk Kim             AcpiGbl_DisasmFlag = TRUE;
5900d84335fSJung-uk Kim             fprintf (stderr, "\n");
5910d84335fSJung-uk Kim             AslDoDisassembly ();
5920d84335fSJung-uk Kim 
5930d84335fSJung-uk Kim             /* delete the AML file. This AML file should never be utilized by AML interpreters. */
5940d84335fSJung-uk Kim 
5950d84335fSJung-uk Kim             FlDeleteFile (ASL_FILE_AML_OUTPUT);
5960d84335fSJung-uk Kim         }
5970d84335fSJung-uk Kim 
598a9f12690SJung-uk Kim         return (AE_OK);
599a88e22b7SJung-uk Kim 
6009c48c75eSJung-uk Kim     /*
6019c48c75eSJung-uk Kim      * Binary ACPI table was auto-detected, disassemble it
6029c48c75eSJung-uk Kim      */
603f8146b88SJung-uk Kim     case ASL_INPUT_TYPE_BINARY_ACPI_TABLE:
6049c48c75eSJung-uk Kim 
6059c48c75eSJung-uk Kim         /* We have what appears to be an ACPI table, disassemble it */
6069c48c75eSJung-uk Kim 
6079c48c75eSJung-uk Kim         FlCloseFile (ASL_FILE_INPUT);
6089c48c75eSJung-uk Kim         Gbl_DoCompile = FALSE;
609*af051161SJung-uk Kim         AcpiGbl_DisasmFlag = TRUE;
6109c48c75eSJung-uk Kim         Status = AslDoDisassembly ();
6119c48c75eSJung-uk Kim         return (Status);
6129c48c75eSJung-uk Kim 
6139c48c75eSJung-uk Kim     /* Unknown binary table */
6149c48c75eSJung-uk Kim 
615a88e22b7SJung-uk Kim     case ASL_INPUT_TYPE_BINARY:
616a88e22b7SJung-uk Kim 
617a88e22b7SJung-uk Kim         AePrintErrorLog (ASL_FILE_STDERR);
618a88e22b7SJung-uk Kim         return (AE_ERROR);
619a88e22b7SJung-uk Kim 
620a88e22b7SJung-uk Kim     default:
621a9d8d09cSJung-uk Kim 
622a88e22b7SJung-uk Kim         printf ("Unknown file type %X\n", Gbl_FileType);
623a88e22b7SJung-uk Kim         return (AE_ERROR);
624a88e22b7SJung-uk Kim     }
625a9f12690SJung-uk Kim }
626a9f12690SJung-uk Kim 
627a9f12690SJung-uk Kim 
628a9f12690SJung-uk Kim /*******************************************************************************
629a9f12690SJung-uk Kim  *
630eef1b955SJung-uk Kim  * FUNCTION:    AslCheckForErrorExit
631eef1b955SJung-uk Kim  *
632eef1b955SJung-uk Kim  * PARAMETERS:  None. Examines global exception count array
633eef1b955SJung-uk Kim  *
634eef1b955SJung-uk Kim  * RETURN:      Status
635eef1b955SJung-uk Kim  *
636eef1b955SJung-uk Kim  * DESCRIPTION: Determine if compiler should abort with error status
637eef1b955SJung-uk Kim  *
638eef1b955SJung-uk Kim  ******************************************************************************/
639eef1b955SJung-uk Kim 
640eef1b955SJung-uk Kim ACPI_STATUS
641eef1b955SJung-uk Kim AslCheckForErrorExit (
642eef1b955SJung-uk Kim     void)
643eef1b955SJung-uk Kim {
644eef1b955SJung-uk Kim 
645eef1b955SJung-uk Kim     /*
646eef1b955SJung-uk Kim      * Return non-zero exit code if there have been errors, unless the
647eef1b955SJung-uk Kim      * global ignore error flag has been set
648eef1b955SJung-uk Kim      */
649eef1b955SJung-uk Kim     if (!Gbl_IgnoreErrors)
650eef1b955SJung-uk Kim     {
651eef1b955SJung-uk Kim         if (Gbl_ExceptionCount[ASL_ERROR] > 0)
652eef1b955SJung-uk Kim         {
653eef1b955SJung-uk Kim             return (AE_ERROR);
654eef1b955SJung-uk Kim         }
655eef1b955SJung-uk Kim 
656eef1b955SJung-uk Kim         /* Optionally treat warnings as errors */
657eef1b955SJung-uk Kim 
658eef1b955SJung-uk Kim         if (Gbl_WarningsAsErrors)
659eef1b955SJung-uk Kim         {
660eef1b955SJung-uk Kim             if ((Gbl_ExceptionCount[ASL_WARNING] > 0)  ||
661eef1b955SJung-uk Kim                 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
662eef1b955SJung-uk Kim                 (Gbl_ExceptionCount[ASL_WARNING3] > 0))
663eef1b955SJung-uk Kim             {
664eef1b955SJung-uk Kim                 return (AE_ERROR);
665eef1b955SJung-uk Kim             }
666eef1b955SJung-uk Kim         }
667eef1b955SJung-uk Kim     }
668eef1b955SJung-uk Kim 
669eef1b955SJung-uk Kim     return (AE_OK);
670eef1b955SJung-uk Kim }
671