aslstartup.c revision 281075
1193267Sjkim/****************************************************************************** 2193267Sjkim * 3193267Sjkim * Module Name: aslstartup - Compiler startup routines, called from main 4193267Sjkim * 5193267Sjkim *****************************************************************************/ 6193267Sjkim 7217365Sjkim/* 8245582Sjkim * Copyright (C) 2000 - 2015, Intel Corp. 9193267Sjkim * All rights reserved. 10193267Sjkim * 11217365Sjkim * Redistribution and use in source and binary forms, with or without 12217365Sjkim * modification, are permitted provided that the following conditions 13217365Sjkim * are met: 14217365Sjkim * 1. Redistributions of source code must retain the above copyright 15217365Sjkim * notice, this list of conditions, and the following disclaimer, 16217365Sjkim * without modification. 17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18217365Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 19217365Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 20217365Sjkim * including a substantially similar Disclaimer requirement for further 21217365Sjkim * binary redistribution. 22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 23217365Sjkim * of any contributors may be used to endorse or promote products derived 24217365Sjkim * from this software without specific prior written permission. 25193267Sjkim * 26217365Sjkim * Alternatively, this software may be distributed under the terms of the 27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 28217365Sjkim * Software Foundation. 29193267Sjkim * 30217365Sjkim * NO WARRANTY 31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41217365Sjkim * POSSIBILITY OF SUCH DAMAGES. 42217365Sjkim */ 43193267Sjkim 44193267Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h> 45193341Sjkim#include <contrib/dev/acpica/include/actables.h> 46193341Sjkim#include <contrib/dev/acpica/include/acdisasm.h> 47246849Sjkim#include <contrib/dev/acpica/include/acapps.h> 48193341Sjkim 49193267Sjkim#define _COMPONENT ACPI_COMPILER 50193267Sjkim ACPI_MODULE_NAME ("aslstartup") 51193267Sjkim 52193267Sjkim 53193267Sjkim/* Local prototypes */ 54193267Sjkim 55212761Sjkimstatic UINT8 56212761SjkimAslDetectSourceFileType ( 57193267Sjkim ASL_FILE_INFO *Info); 58193267Sjkim 59193267Sjkimstatic ACPI_STATUS 60193267SjkimAslDoDisassembly ( 61193267Sjkim void); 62193267Sjkim 63193267Sjkim 64193267Sjkim/* Globals */ 65193267Sjkim 66212761Sjkimstatic BOOLEAN AslToFile = TRUE; 67209746Sjkim 68209746Sjkim 69193267Sjkim/******************************************************************************* 70246849Sjkim * 71246849Sjkim * FUNCTION: AslInitializeGlobals 72246849Sjkim * 73209746Sjkim * PARAMETERS: None 74246849Sjkim * 75193267Sjkim * RETURN: None 76193267Sjkim * 77193267Sjkim * DESCRIPTION: Re-initialize globals needed to restart the compiler. This 78193267Sjkim * allows multiple files to be disassembled and/or compiled. 79193267Sjkim * 80193267Sjkim ******************************************************************************/ 81193267Sjkim 82193267Sjkimvoid 83193267SjkimAslInitializeGlobals ( 84193267Sjkim void) 85193267Sjkim{ 86193267Sjkim UINT32 i; 87193267Sjkim 88209746Sjkim 89193267Sjkim /* Init compiler globals */ 90193267Sjkim 91193267Sjkim Gbl_SyntaxError = 0; 92193267Sjkim Gbl_CurrentColumn = 0; 93193267Sjkim Gbl_CurrentLineNumber = 1; 94193267Sjkim Gbl_LogicalLineNumber = 1; 95193267Sjkim Gbl_CurrentLineOffset = 0; 96193267Sjkim Gbl_InputFieldCount = 0; 97193267Sjkim Gbl_InputByteCount = 0; 98193267Sjkim Gbl_NsLookupCount = 0; 99193267Sjkim Gbl_LineBufPtr = Gbl_CurrentLineBuffer; 100193267Sjkim 101209746Sjkim Gbl_ErrorLog = NULL; 102233250Sjkim Gbl_NextError = NULL; 103233250Sjkim Gbl_Signature = NULL; 104193267Sjkim Gbl_FileType = 0; 105193267Sjkim 106193267Sjkim TotalExecutableOpcodes = 0; 107193267Sjkim TotalNamedObjects = 0; 108209746Sjkim TotalKeywords = 0; 109209746Sjkim TotalParseNodes = 0; 110193267Sjkim TotalMethods = 0; 111233250Sjkim TotalAllocations = 0; 112233250Sjkim TotalAllocated = 0; 113233250Sjkim TotalFolds = 0; 114233250Sjkim 115233250Sjkim AslGbl_NextEvent = 0; 116233250Sjkim for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++) 117233250Sjkim { 118233250Sjkim Gbl_ExceptionCount[i] = 0; 119233250Sjkim } 120193267Sjkim 121193267Sjkim for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++) 122193267Sjkim { 123193267Sjkim Gbl_Files[i].Handle = NULL; 124193267Sjkim Gbl_Files[i].Filename = NULL; 125193267Sjkim } 126233250Sjkim} 127233250Sjkim 128233250Sjkim 129233250Sjkim/******************************************************************************* 130233250Sjkim * 131193267Sjkim * FUNCTION: AslDetectSourceFileType 132193267Sjkim * 133193267Sjkim * PARAMETERS: Info - Name/Handle for the file (must be open) 134193267Sjkim * 135193267Sjkim * RETURN: File Type 136193267Sjkim * 137193267Sjkim * DESCRIPTION: Determine the type of the input file. Either binary (contains 138193267Sjkim * non-ASCII characters), ASL file, or an ACPI Data Table file. 139193267Sjkim * 140193267Sjkim ******************************************************************************/ 141193267Sjkim 142193267Sjkimstatic UINT8 143193267SjkimAslDetectSourceFileType ( 144193267Sjkim ASL_FILE_INFO *Info) 145193267Sjkim{ 146193267Sjkim char *FileChar; 147193267Sjkim UINT8 Type; 148193267Sjkim ACPI_STATUS Status; 149193267Sjkim 150193267Sjkim 151193267Sjkim /* Check for a valid binary ACPI table */ 152193267Sjkim 153193267Sjkim Status = FlCheckForAcpiTable (Info->Handle); 154193267Sjkim if (ACPI_SUCCESS (Status)) 155212761Sjkim { 156193267Sjkim Type = ASL_INPUT_TYPE_ACPI_TABLE; 157193267Sjkim goto Cleanup; 158193267Sjkim } 159193267Sjkim 160193267Sjkim /* Check for 100% ASCII source file (comments are ignored) */ 161193267Sjkim 162193267Sjkim Status = FlCheckForAscii (Info->Handle, Info->Filename, TRUE); 163193267Sjkim if (ACPI_FAILURE (Status)) 164193267Sjkim { 165193267Sjkim printf ("Non-ascii input file - %s\n", Info->Filename); 166193267Sjkim 167193267Sjkim if (!Gbl_IgnoreErrors) 168193267Sjkim { 169193267Sjkim Type = ASL_INPUT_TYPE_BINARY; 170193267Sjkim goto Cleanup; 171193267Sjkim } 172193267Sjkim } 173193267Sjkim 174193267Sjkim /* 175193267Sjkim * File is ASCII. Determine if this is an ASL file or an ACPI data 176193267Sjkim * table file. 177193267Sjkim */ 178193267Sjkim while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle)) 179193267Sjkim { 180193267Sjkim /* Uppercase the buffer for caseless compare */ 181193267Sjkim 182193267Sjkim FileChar = Gbl_CurrentLineBuffer; 183193267Sjkim while (*FileChar) 184193267Sjkim { 185193267Sjkim *FileChar = (char) toupper ((int) *FileChar); 186193267Sjkim FileChar++; 187193267Sjkim } 188193267Sjkim 189193267Sjkim /* Presence of "DefinitionBlock" indicates actual ASL code */ 190193267Sjkim 191193267Sjkim if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK")) 192193267Sjkim { 193193267Sjkim /* Appears to be an ASL file */ 194193267Sjkim 195193267Sjkim Type = ASL_INPUT_TYPE_ASCII_ASL; 196193267Sjkim goto Cleanup; 197193267Sjkim } 198193267Sjkim } 199193267Sjkim 200193267Sjkim /* Not an ASL source file, default to a data table source file */ 201193267Sjkim 202193267Sjkim Type = ASL_INPUT_TYPE_ASCII_DATA; 203193267Sjkim 204193267SjkimCleanup: 205193267Sjkim 206193267Sjkim /* Must seek back to the start of the file */ 207193267Sjkim 208193267Sjkim fseek (Info->Handle, 0, SEEK_SET); 209193267Sjkim return (Type); 210193267Sjkim} 211193267Sjkim 212209746Sjkim 213209746Sjkim/******************************************************************************* 214209746Sjkim * 215209746Sjkim * FUNCTION: AslDoDisassembly 216209746Sjkim * 217209746Sjkim * PARAMETERS: None 218209746Sjkim * 219209746Sjkim * RETURN: Status 220209746Sjkim * 221209746Sjkim * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build 222209746Sjkim * namespace. 223212761Sjkim * 224209746Sjkim ******************************************************************************/ 225209746Sjkim 226209746Sjkimstatic ACPI_STATUS 227209746SjkimAslDoDisassembly ( 228209746Sjkim void) 229209746Sjkim{ 230209746Sjkim ACPI_STATUS Status; 231209746Sjkim 232246849Sjkim 233246849Sjkim /* ACPICA subsystem initialization */ 234246849Sjkim 235246849Sjkim Status = AdInitialize (); 236246849Sjkim if (ACPI_FAILURE (Status)) 237246849Sjkim { 238246849Sjkim return (Status); 239246849Sjkim } 240246849Sjkim 241209746Sjkim Status = AcpiAllocateRootTable (4); 242209746Sjkim if (ACPI_FAILURE (Status)) 243235945Sjkim { 244209746Sjkim AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n", 245209746Sjkim AcpiFormatException (Status)); 246209746Sjkim return (Status); 247241973Sjkim } 248241973Sjkim 249241973Sjkim /* This is where the disassembly happens */ 250241973Sjkim 251241973Sjkim AcpiGbl_DbOpt_disasm = TRUE; 252241973Sjkim Status = AdAmlDisassemble (AslToFile, 253209746Sjkim Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix, 254209746Sjkim &Gbl_Files[ASL_FILE_INPUT].Filename); 255209746Sjkim if (ACPI_FAILURE (Status)) 256209746Sjkim { 257209746Sjkim return (Status); 258209746Sjkim } 259240716Sjkim 260209746Sjkim /* Check if any control methods were unresolved */ 261209746Sjkim 262209746Sjkim AcpiDmUnresolvedWarning (0); 263209746Sjkim 264209746Sjkim#if 0 265209746Sjkim /* TBD: Handle additional output files for disassembler */ 266209746Sjkim 267209746Sjkim Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix); 268209746Sjkim NsDisplayNamespace (); 269209746Sjkim#endif 270209746Sjkim 271209746Sjkim /* Shutdown compiler and ACPICA subsystem */ 272209746Sjkim 273209746Sjkim AeClearErrorLog (); 274209746Sjkim (void) AcpiTerminate (); 275209746Sjkim 276209746Sjkim /* 277209746Sjkim * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the 278209746Sjkim * .DSL disassembly file, which can now be compiled if requested 279209746Sjkim */ 280209746Sjkim if (Gbl_DoCompile) 281209746Sjkim { 282209746Sjkim AcpiOsPrintf ("\nCompiling \"%s\"\n", 283209746Sjkim Gbl_Files[ASL_FILE_INPUT].Filename); 284209746Sjkim return (AE_CTRL_CONTINUE); 285209746Sjkim } 286209746Sjkim 287209746Sjkim /* No need to free the filename string */ 288209746Sjkim 289209746Sjkim Gbl_Files[ASL_FILE_INPUT].Filename = NULL; 290209746Sjkim 291209746Sjkim CmDeleteCaches (); 292209746Sjkim return (AE_OK); 293209746Sjkim} 294209746Sjkim 295209746Sjkim 296246849Sjkim/******************************************************************************* 297246849Sjkim * 298246849Sjkim * FUNCTION: AslDoOneFile 299246849Sjkim * 300246849Sjkim * PARAMETERS: Filename - Name of the file 301246849Sjkim * 302246849Sjkim * RETURN: Status 303246849Sjkim * 304246849Sjkim * DESCRIPTION: Process a single file - either disassemble, compile, or both 305246849Sjkim * 306246849Sjkim ******************************************************************************/ 307246849Sjkim 308246849SjkimACPI_STATUS 309246849SjkimAslDoOneFile ( 310246849Sjkim char *Filename) 311246849Sjkim{ 312246849Sjkim ACPI_STATUS Status; 313246849Sjkim 314246849Sjkim 315246849Sjkim /* Re-initialize "some" compiler/preprocessor globals */ 316246849Sjkim 317246849Sjkim AslInitializeGlobals (); 318246849Sjkim PrInitializeGlobals (); 319246849Sjkim 320246849Sjkim /* 321246849Sjkim * Extract the directory path. This path is used for possible include 322246849Sjkim * files and the optional AML filename embedded in the input file 323246849Sjkim * DefinitionBlock declaration. 324246849Sjkim */ 325246849Sjkim Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL); 326246849Sjkim if (ACPI_FAILURE (Status)) 327246849Sjkim { 328246849Sjkim return (Status); 329246849Sjkim } 330246849Sjkim 331246849Sjkim /* Take a copy of the input filename, convert any backslashes */ 332246849Sjkim 333246849Sjkim Gbl_Files[ASL_FILE_INPUT].Filename = 334246849Sjkim UtStringCacheCalloc (strlen (Filename) + 1); 335246849Sjkim 336246849Sjkim strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename); 337246849Sjkim UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename); 338246849Sjkim 339246849Sjkim /* 340246849Sjkim * AML Disassembly (Optional) 341246849Sjkim */ 342246849Sjkim if (Gbl_DisasmFlag) 343246849Sjkim { 344246849Sjkim Status = AslDoDisassembly (); 345246849Sjkim if (Status != AE_CTRL_CONTINUE) 346246849Sjkim { 347246849Sjkim return (Status); 348246849Sjkim } 349246849Sjkim } 350246849Sjkim 351246849Sjkim /* 352246849Sjkim * Open the input file. Here, this should be an ASCII source file, 353246849Sjkim * either an ASL file or a Data Table file 354246849Sjkim */ 355246849Sjkim Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename); 356246849Sjkim if (ACPI_FAILURE (Status)) 357246849Sjkim { 358246849Sjkim AePrintErrorLog (ASL_FILE_STDERR); 359246849Sjkim return (AE_ERROR); 360246849Sjkim } 361246849Sjkim 362246849Sjkim /* Determine input file type */ 363246849Sjkim 364246849Sjkim Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]); 365246849Sjkim if (Gbl_FileType == ASL_INPUT_TYPE_BINARY) 366246849Sjkim { 367246849Sjkim return (AE_ERROR); 368246849Sjkim } 369246849Sjkim 370246849Sjkim /* 371246849Sjkim * If -p not specified, we will use the input filename as the 372246849Sjkim * output filename prefix 373246849Sjkim */ 374246849Sjkim if (Gbl_UseDefaultAmlFilename) 375246849Sjkim { 376193267Sjkim Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename; 377193267Sjkim } 378193267Sjkim 379193267Sjkim /* Open the optional output files (listings, etc.) */ 380193267Sjkim 381193267Sjkim Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix); 382193267Sjkim if (ACPI_FAILURE (Status)) 383193267Sjkim { 384193267Sjkim AePrintErrorLog (ASL_FILE_STDERR); 385193267Sjkim return (AE_ERROR); 386193267Sjkim } 387193267Sjkim 388193267Sjkim /* 389193267Sjkim * Compilation of ASL source versus DataTable source uses different 390193267Sjkim * compiler subsystems 391193267Sjkim */ 392193267Sjkim switch (Gbl_FileType) 393233250Sjkim { 394193267Sjkim /* 395193267Sjkim * Data Table Compilation 396233250Sjkim */ 397193267Sjkim case ASL_INPUT_TYPE_ASCII_DATA: 398233250Sjkim 399233250Sjkim Status = DtDoCompile (); 400193267Sjkim if (ACPI_FAILURE (Status)) 401193267Sjkim { 402193267Sjkim return (Status); 403193267Sjkim } 404193267Sjkim 405246849Sjkim if (Gbl_Signature) 406246849Sjkim { 407193267Sjkim Gbl_Signature = NULL; 408193267Sjkim } 409193267Sjkim 410193267Sjkim /* Check if any errors occurred during compile */ 411193267Sjkim 412193267Sjkim Status = AslCheckForErrorExit (); 413209746Sjkim if (ACPI_FAILURE (Status)) 414209746Sjkim { 415193267Sjkim return (Status); 416209746Sjkim } 417209746Sjkim 418193267Sjkim /* Cleanup (for next source file) and exit */ 419209746Sjkim 420209746Sjkim AeClearErrorLog (); 421209746Sjkim PrTerminatePreprocessor (); 422209746Sjkim return (Status); 423209746Sjkim 424209746Sjkim /* 425209746Sjkim * ASL Compilation 426209746Sjkim */ 427209746Sjkim case ASL_INPUT_TYPE_ASCII_ASL: 428209746Sjkim 429209746Sjkim /* ACPICA subsystem initialization */ 430209746Sjkim 431209746Sjkim Status = AdInitialize (); 432209746Sjkim if (ACPI_FAILURE (Status)) 433209746Sjkim { 434209746Sjkim return (Status); 435209746Sjkim } 436209746Sjkim 437209746Sjkim (void) CmDoCompile (); 438209746Sjkim (void) AcpiTerminate (); 439209746Sjkim 440209746Sjkim /* Check if any errors occurred during compile */ 441209746Sjkim 442209746Sjkim Status = AslCheckForErrorExit (); 443209746Sjkim if (ACPI_FAILURE (Status)) 444209746Sjkim { 445209746Sjkim return (Status); 446209746Sjkim } 447209746Sjkim 448209746Sjkim /* Cleanup (for next source file) and exit */ 449209746Sjkim 450209746Sjkim AeClearErrorLog (); 451209746Sjkim PrTerminatePreprocessor (); 452209746Sjkim return (AE_OK); 453209746Sjkim 454209746Sjkim /* 455209746Sjkim * Binary ACPI table was auto-detected, disassemble it 456209746Sjkim */ 457209746Sjkim case ASL_INPUT_TYPE_ACPI_TABLE: 458209746Sjkim 459209746Sjkim /* We have what appears to be an ACPI table, disassemble it */ 460209746Sjkim 461234623Sjkim FlCloseFile (ASL_FILE_INPUT); 462234623Sjkim Gbl_DoCompile = FALSE; 463234623Sjkim Gbl_DisasmFlag = TRUE; 464234623Sjkim Status = AslDoDisassembly (); 465209746Sjkim return (Status); 466209746Sjkim 467193267Sjkim /* Unknown binary table */ 468209746Sjkim 469209746Sjkim case ASL_INPUT_TYPE_BINARY: 470193267Sjkim 471234623Sjkim AePrintErrorLog (ASL_FILE_STDERR); 472234623Sjkim return (AE_ERROR); 473234623Sjkim 474234623Sjkim default: 475234623Sjkim 476234623Sjkim printf ("Unknown file type %X\n", Gbl_FileType); 477234623Sjkim return (AE_ERROR); 478234623Sjkim } 479234623Sjkim} 480234623Sjkim 481234623Sjkim 482209746Sjkim/******************************************************************************* 483233250Sjkim * 484209746Sjkim * FUNCTION: AslCheckForErrorExit 485193267Sjkim * 486209746Sjkim * PARAMETERS: None. Examines global exception count array 487234623Sjkim * 488209746Sjkim * RETURN: Status 489209746Sjkim * 490193267Sjkim * DESCRIPTION: Determine if compiler should abort with error status 491209746Sjkim * 492209746Sjkim ******************************************************************************/ 493193267Sjkim 494193267SjkimACPI_STATUS 495193267SjkimAslCheckForErrorExit ( 496193267Sjkim void) 497193267Sjkim{ 498193267Sjkim 499234623Sjkim /* 500212761Sjkim * Return non-zero exit code if there have been errors, unless the 501193267Sjkim * global ignore error flag has been set 502234623Sjkim */ 503234623Sjkim if (!Gbl_IgnoreErrors) 504234623Sjkim { 505234623Sjkim if (Gbl_ExceptionCount[ASL_ERROR] > 0) 506193267Sjkim { 507234623Sjkim return (AE_ERROR); 508193267Sjkim } 509193267Sjkim 510234623Sjkim /* Optionally treat warnings as errors */ 511234623Sjkim 512193267Sjkim if (Gbl_WarningsAsErrors) 513233250Sjkim { 514209746Sjkim if ((Gbl_ExceptionCount[ASL_WARNING] > 0) || 515209746Sjkim (Gbl_ExceptionCount[ASL_WARNING2] > 0) || 516246849Sjkim (Gbl_ExceptionCount[ASL_WARNING3] > 0)) 517246849Sjkim { 518246849Sjkim return (AE_ERROR); 519246849Sjkim } 520246849Sjkim } 521246849Sjkim } 522246849Sjkim 523246849Sjkim return (AE_OK); 524246849Sjkim} 525246849Sjkim