1 /******************************************************************************
3 * Module Name: aslstartup - Compiler startup routines, called from main
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <contrib/dev/acpica/compiler/aslcompiler.h>
46 #include <contrib/dev/acpica/include/actables.h>
47 #include <contrib/dev/acpica/include/acdisasm.h>
48 #include <contrib/dev/acpica/include/acapps.h>
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslstartup")
54 #define ASL_MAX_FILES 256
55 static char *FileList[ASL_MAX_FILES];
56 static BOOLEAN AslToFile = TRUE;
59 /* Local prototypes */
63 char *DirectoryPathname,
67 AslDetectSourceFileType (
75 /*******************************************************************************
77 * FUNCTION: AslInitializeGlobals
83 * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
84 * allows multiple files to be disassembled and/or compiled.
86 ******************************************************************************/
89 AslInitializeGlobals (
95 /* Init compiler globals */
97 Gbl_CurrentColumn = 0;
98 Gbl_CurrentLineNumber = 1;
99 Gbl_LogicalLineNumber = 1;
100 Gbl_CurrentLineOffset = 0;
101 Gbl_InputFieldCount = 0;
102 Gbl_InputByteCount = 0;
103 Gbl_NsLookupCount = 0;
104 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
107 Gbl_NextError = NULL;
108 Gbl_Signature = NULL;
111 TotalExecutableOpcodes = 0;
112 TotalNamedObjects = 0;
116 TotalAllocations = 0;
120 AslGbl_NextEvent = 0;
121 for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
123 Gbl_ExceptionCount[i] = 0;
126 for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++)
128 Gbl_Files[i].Handle = NULL;
129 Gbl_Files[i].Filename = NULL;
134 /******************************************************************************
136 * FUNCTION: AsDoWildcard
142 * DESCRIPTION: Process files via wildcards. This function is for the Windows
145 ******************************************************************************/
149 char *DirectoryPathname,
160 /* Open parent directory */
162 DirInfo = AcpiOsOpenDirectory (DirectoryPathname, FileSpecifier, REQUEST_FILE_ONLY);
165 /* Either the directory of file does not exist */
167 Gbl_Files[ASL_FILE_INPUT].Filename = FileSpecifier;
168 FlFileError (ASL_FILE_INPUT, ASL_MSG_OPEN);
172 /* Process each file that matches the wildcard specification */
174 while ((Filename = AcpiOsGetNextFilename (DirInfo)))
176 /* Add the filename to the file list */
178 FileList[FileCount] = AcpiOsAllocate (strlen (Filename) + 1);
179 strcpy (FileList[FileCount], Filename);
182 if (FileCount >= ASL_MAX_FILES)
184 printf ("Max files reached\n");
192 AcpiOsCloseDirectory (DirInfo);
193 FileList[FileCount] = NULL;
198 * Linux/Unix cases - Wildcards are expanded by the shell automatically.
199 * Just return the filename in a null terminated list
201 FileList[0] = AcpiOsAllocate (strlen (FileSpecifier) + 1);
202 strcpy (FileList[0], FileSpecifier);
210 /*******************************************************************************
212 * FUNCTION: AslDetectSourceFileType
214 * PARAMETERS: Info - Name/Handle for the file (must be open)
218 * DESCRIPTION: Determine the type of the input file. Either binary (contains
219 * non-ASCII characters), ASL file, or an ACPI Data Table file.
221 ******************************************************************************/
224 AslDetectSourceFileType (
232 /* Check for a valid binary ACPI table */
234 Status = FlCheckForAcpiTable (Info->Handle);
235 if (ACPI_SUCCESS (Status))
237 Type = ASL_INPUT_TYPE_ACPI_TABLE;
241 /* Check for 100% ASCII source file (comments are ignored) */
243 Status = FlCheckForAscii (Info->Handle, Info->Filename, TRUE);
244 if (ACPI_FAILURE (Status))
246 printf ("Non-ascii input file - %s\n", Info->Filename);
248 if (!Gbl_IgnoreErrors)
250 Type = ASL_INPUT_TYPE_BINARY;
256 * File is ASCII. Determine if this is an ASL file or an ACPI data
259 while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle))
261 /* Uppercase the buffer for caseless compare */
263 FileChar = Gbl_CurrentLineBuffer;
266 *FileChar = (char) toupper ((int) *FileChar);
270 /* Presence of "DefinitionBlock" indicates actual ASL code */
272 if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
274 /* Appears to be an ASL file */
276 Type = ASL_INPUT_TYPE_ASCII_ASL;
281 /* Not an ASL source file, default to a data table source file */
283 Type = ASL_INPUT_TYPE_ASCII_DATA;
287 /* Must seek back to the start of the file */
289 fseek (Info->Handle, 0, SEEK_SET);
294 /*******************************************************************************
296 * FUNCTION: AslDoDisassembly
302 * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
305 ******************************************************************************/
314 /* ACPICA subsystem initialization */
316 Status = AdInitialize ();
317 if (ACPI_FAILURE (Status))
322 Status = AcpiAllocateRootTable (4);
323 if (ACPI_FAILURE (Status))
325 AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
326 AcpiFormatException (Status));
330 /* This is where the disassembly happens */
332 AcpiGbl_DbOpt_disasm = TRUE;
333 Status = AdAmlDisassemble (AslToFile,
334 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
335 &Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_GetAllTables);
336 if (ACPI_FAILURE (Status))
341 /* Check if any control methods were unresolved */
343 AcpiDmUnresolvedWarning (0);
346 /* TBD: Handle additional output files for disassembler */
348 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
349 NsDisplayNamespace ();
352 /* Shutdown compiler and ACPICA subsystem */
355 (void) AcpiTerminate ();
358 * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
359 * .DSL disassembly file, which can now be compiled if requested
363 AcpiOsPrintf ("\nCompiling \"%s\"\n",
364 Gbl_Files[ASL_FILE_INPUT].Filename);
365 return (AE_CTRL_CONTINUE);
368 ACPI_FREE (Gbl_Files[ASL_FILE_INPUT].Filename);
369 Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
374 /*******************************************************************************
376 * FUNCTION: AslDoOneFile
378 * PARAMETERS: Filename - Name of the file
382 * DESCRIPTION: Process a single file - either disassemble, compile, or both
384 ******************************************************************************/
393 /* Re-initialize "some" compiler/preprocessor globals */
395 AslInitializeGlobals ();
396 PrInitializeGlobals ();
398 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
401 * AML Disassembly (Optional)
403 if (Gbl_DisasmFlag || Gbl_GetAllTables)
405 Status = AslDoDisassembly ();
406 if (Status != AE_CTRL_CONTINUE)
413 * Open the input file. Here, this should be an ASCII source file,
414 * either an ASL file or a Data Table file
416 Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
417 if (ACPI_FAILURE (Status))
419 AePrintErrorLog (ASL_FILE_STDERR);
423 /* Determine input file type */
425 Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
426 if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
432 * If -p not specified, we will use the input filename as the
433 * output filename prefix
435 if (Gbl_UseDefaultAmlFilename)
437 Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
440 /* Open the optional output files (listings, etc.) */
442 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
443 if (ACPI_FAILURE (Status))
445 AePrintErrorLog (ASL_FILE_STDERR);
450 * Compilation of ASL source versus DataTable source uses different
451 * compiler subsystems
453 switch (Gbl_FileType)
456 * Data Table Compilation
458 case ASL_INPUT_TYPE_ASCII_DATA:
460 Status = DtDoCompile ();
461 if (ACPI_FAILURE (Status))
468 ACPI_FREE (Gbl_Signature);
469 Gbl_Signature = NULL;
472 /* Check if any errors occurred during compile */
474 Status = AslCheckForErrorExit ();
475 if (ACPI_FAILURE (Status))
480 /* Cleanup (for next source file) and exit */
483 PrTerminatePreprocessor ();
489 case ASL_INPUT_TYPE_ASCII_ASL:
491 /* ACPICA subsystem initialization */
493 Status = AdInitialize ();
494 if (ACPI_FAILURE (Status))
499 (void) CmDoCompile ();
500 (void) AcpiTerminate ();
502 /* Check if any errors occurred during compile */
504 Status = AslCheckForErrorExit ();
505 if (ACPI_FAILURE (Status))
510 /* Cleanup (for next source file) and exit */
513 PrTerminatePreprocessor ();
517 * Binary ACPI table was auto-detected, disassemble it
519 case ASL_INPUT_TYPE_ACPI_TABLE:
521 /* We have what appears to be an ACPI table, disassemble it */
523 FlCloseFile (ASL_FILE_INPUT);
524 Gbl_DoCompile = FALSE;
525 Gbl_DisasmFlag = TRUE;
526 Status = AslDoDisassembly ();
529 /* Unknown binary table */
531 case ASL_INPUT_TYPE_BINARY:
533 AePrintErrorLog (ASL_FILE_STDERR);
538 printf ("Unknown file type %X\n", Gbl_FileType);
544 /*******************************************************************************
546 * FUNCTION: AslDoOnePathname
548 * PARAMETERS: Pathname - Full pathname, possibly with wildcards
552 * DESCRIPTION: Process one pathname, possible terminated with a wildcard
553 * specification. If a wildcard, it is expanded and the multiple
554 * files are processed.
556 ******************************************************************************/
561 ASL_PATHNAME_CALLBACK PathCallback)
563 ACPI_STATUS Status = AE_OK;
569 /* Split incoming path into a directory/filename combo */
571 Status = FlSplitInputPathname (Pathname, &Gbl_DirectoryPath, &Filename);
572 if (ACPI_FAILURE (Status))
577 /* Expand possible wildcard into a file list (Windows/DOS only) */
579 WildcardList = AsDoWildcard (Gbl_DirectoryPath, Filename);
580 while (*WildcardList)
582 FullPathname = ACPI_ALLOCATE (
583 strlen (Gbl_DirectoryPath) + strlen (*WildcardList) + 1);
585 /* Construct a full path to the file */
587 strcpy (FullPathname, Gbl_DirectoryPath);
588 strcat (FullPathname, *WildcardList);
591 * If -p not specified, we will use the input filename as the
592 * output filename prefix
594 if (Gbl_UseDefaultAmlFilename)
596 Gbl_OutputFilenamePrefix = FullPathname;
599 /* Save status from all compiles */
601 Status |= (*PathCallback) (FullPathname);
603 ACPI_FREE (FullPathname);
604 ACPI_FREE (*WildcardList);
605 *WildcardList = NULL;
609 ACPI_FREE (Gbl_DirectoryPath);
610 ACPI_FREE (Filename);
615 /*******************************************************************************
617 * FUNCTION: AslCheckForErrorExit
619 * PARAMETERS: None. Examines global exception count array
623 * DESCRIPTION: Determine if compiler should abort with error status
625 ******************************************************************************/
628 AslCheckForErrorExit (
633 * Return non-zero exit code if there have been errors, unless the
634 * global ignore error flag has been set
636 if (!Gbl_IgnoreErrors)
638 if (Gbl_ExceptionCount[ASL_ERROR] > 0)
643 /* Optionally treat warnings as errors */
645 if (Gbl_WarningsAsErrors)
647 if ((Gbl_ExceptionCount[ASL_WARNING] > 0) ||
648 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
649 (Gbl_ExceptionCount[ASL_WARNING3] > 0))