1 /******************************************************************************
   2  *
   3  * Module Name: aemain - Main routine for the AcpiExec utility
   4  *
   5  *****************************************************************************/
   6 
   7 /*
   8  * Copyright (C) 2000 - 2014, Intel Corp.
   9  * All rights reserved.
  10  *
  11  * Redistribution and use in source and binary forms, with or without
  12  * modification, are permitted provided that the following conditions
  13  * are met:
  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.
  25  *
  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.
  29  *
  30  * NO WARRANTY
  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.
  42  */
  43 
  44 #include "aecommon.h"
  45 
  46 #define _COMPONENT          ACPI_TOOLS
  47         ACPI_MODULE_NAME    ("aemain")
  48 
  49 
  50 /*
  51  * Main routine for the ACPI user-space execution utility.
  52  *
  53  * Portability note: The utility depends upon the host for command-line
  54  * wildcard support - it is not implemented locally. For example:
  55  *
  56  * Linux/Unix systems: Shell expands wildcards automatically.
  57  *
  58  * Windows: The setargv.obj module must be linked in to automatically
  59  * expand wildcards.
  60  */
  61 extern BOOLEAN              AcpiGbl_DebugTimeout;
  62 
  63 /* Local prototypes */
  64 
  65 static int
  66 AeDoOptions (
  67     int                     argc,
  68     char                    **argv);
  69 
  70 static ACPI_STATUS
  71 AcpiDbRunBatchMode (
  72     void);
  73 
  74 
  75 #define AE_BUFFER_SIZE              1024
  76 #define ASL_MAX_FILES               256
  77 
  78 /* Execution modes */
  79 
  80 #define AE_MODE_COMMAND_LOOP        0   /* Normal command execution loop */
  81 #define AE_MODE_BATCH_MULTIPLE      1   /* -b option to execute a command line */
  82 #define AE_MODE_BATCH_SINGLE        2   /* -m option to execute a single control method */
  83 
  84 
  85 /* Globals */
  86 
  87 UINT8                       AcpiGbl_RegionFillValue = 0;
  88 BOOLEAN                     AcpiGbl_IgnoreErrors = FALSE;
  89 BOOLEAN                     AcpiGbl_DbOpt_NoRegionSupport = FALSE;
  90 UINT8                       AcpiGbl_UseHwReducedFadt = FALSE;
  91 BOOLEAN                     AcpiGbl_DoInterfaceTests = FALSE;
  92 BOOLEAN                     AcpiGbl_LoadTestTables = FALSE;
  93 static UINT8                AcpiGbl_ExecutionMode = AE_MODE_COMMAND_LOOP;
  94 static char                 BatchBuffer[AE_BUFFER_SIZE];    /* Batch command buffer */
  95 static AE_TABLE_DESC        *AeTableListHead = NULL;
  96 
  97 #define ACPIEXEC_NAME               "AML Execution/Debug Utility"
  98 #define AE_SUPPORTED_OPTIONS        "?b:d:e:f:ghm^orv^:x:"
  99 
 100 
 101 /******************************************************************************
 102  *
 103  * FUNCTION:    usage
 104  *
 105  * PARAMETERS:  None
 106  *
 107  * RETURN:      None
 108  *
 109  * DESCRIPTION: Print a usage message
 110  *
 111  *****************************************************************************/
 112 
 113 static void
 114 usage (
 115     void)
 116 {
 117 
 118     ACPI_USAGE_HEADER ("acpiexec [options] AMLfile1 AMLfile2 ...");
 119 
 120     ACPI_OPTION ("-b \"CommandLine\"",  "Batch mode command line execution (cmd1;cmd2;...)");
 121     ACPI_OPTION ("-h -?",               "Display this help message");
 122     ACPI_OPTION ("-m [Method]",         "Batch mode method execution. Default=MAIN");
 123     printf ("\n");
 124 
 125     ACPI_OPTION ("-da",                 "Disable method abort on error");
 126     ACPI_OPTION ("-di",                 "Disable execution of STA/INI methods during init");
 127     ACPI_OPTION ("-do",                 "Disable Operation Region address simulation");
 128     ACPI_OPTION ("-dr",                 "Disable repair of method return values");
 129     ACPI_OPTION ("-dt",                 "Disable allocation tracking (performance)");
 130     printf ("\n");
 131 
 132     ACPI_OPTION ("-ef",                 "Enable display of final memory statistics");
 133     ACPI_OPTION ("-ei",                 "Enable additional tests for ACPICA interfaces");
 134     ACPI_OPTION ("-el",                 "Enable loading of additional test tables");
 135     ACPI_OPTION ("-em",                 "Enable Interpreter Serialized Mode");
 136     ACPI_OPTION ("-es",                 "Enable Interpreter Slack Mode");
 137     ACPI_OPTION ("-et",                 "Enable debug semaphore timeout");
 138     printf ("\n");
 139 
 140     ACPI_OPTION ("-f <Value>",          "Operation Region initialization fill value");
 141     ACPI_OPTION ("-r",                  "Use hardware-reduced FADT V5");
 142     ACPI_OPTION ("-v",                  "Display version information");
 143     ACPI_OPTION ("-vi",                 "Verbose initialization output");
 144     ACPI_OPTION ("-vr",                 "Verbose region handler output");
 145     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
 146 }
 147 
 148 
 149 /******************************************************************************
 150  *
 151  * FUNCTION:    AeDoOptions
 152  *
 153  * PARAMETERS:  argc/argv           - Standard argc/argv
 154  *
 155  * RETURN:      Status
 156  *
 157  * DESCRIPTION: Command line option processing
 158  *
 159  *****************************************************************************/
 160 
 161 static int
 162 AeDoOptions (
 163     int                     argc,
 164     char                    **argv)
 165 {
 166     int                     j;
 167 
 168 
 169     while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != EOF) switch (j)
 170     {
 171     case 'b':
 172 
 173         if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
 174         {
 175             printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
 176                 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
 177             return (-1);
 178         }
 179         AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
 180         strcpy (BatchBuffer, AcpiGbl_Optarg);
 181         break;
 182 
 183     case 'd':
 184 
 185         switch (AcpiGbl_Optarg[0])
 186         {
 187         case 'a':
 188 
 189             AcpiGbl_IgnoreErrors = TRUE;
 190             break;
 191 
 192         case 'i':
 193 
 194             AcpiGbl_DbOpt_ini_methods = FALSE;
 195             break;
 196 
 197         case 'o':
 198 
 199             AcpiGbl_DbOpt_NoRegionSupport = TRUE;
 200             break;
 201 
 202         case 'r':
 203 
 204             AcpiGbl_DisableAutoRepair = TRUE;
 205             break;
 206 
 207         case 't':
 208 
 209             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
 210                 AcpiGbl_DisableMemTracking = TRUE;
 211             #endif
 212             break;
 213 
 214         default:
 215 
 216             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
 217             return (-1);
 218         }
 219         break;
 220 
 221     case 'e':
 222 
 223         switch (AcpiGbl_Optarg[0])
 224         {
 225         case 'f':
 226 
 227             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
 228                 AcpiGbl_DisplayFinalMemStats = TRUE;
 229             #endif
 230             break;
 231 
 232         case 'i':
 233 
 234             AcpiGbl_DoInterfaceTests = TRUE;
 235             break;
 236 
 237         case 'l':
 238 
 239             AcpiGbl_LoadTestTables = TRUE;
 240             break;
 241 
 242         case 'm':
 243 
 244             AcpiGbl_AllMethodsSerialized = TRUE;
 245             printf ("Enabling AML Interpreter serialized mode\n");
 246             break;
 247 
 248         case 's':
 249 
 250             AcpiGbl_EnableInterpreterSlack = TRUE;
 251             printf ("Enabling AML Interpreter slack mode\n");
 252             break;
 253 
 254         case 't':
 255 
 256             AcpiGbl_DebugTimeout = TRUE;
 257             break;
 258 
 259         default:
 260 
 261             printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
 262             return (-1);
 263         }
 264         break;
 265 
 266     case 'f':
 267 
 268         AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
 269         break;
 270 
 271     case 'g':
 272 
 273         AcpiGbl_DbOpt_tables = TRUE;
 274         AcpiGbl_DbFilename = NULL;
 275         break;
 276 
 277     case 'h':
 278     case '?':
 279 
 280         usage();
 281         return (0);
 282 
 283     case 'm':
 284 
 285         AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
 286         switch (AcpiGbl_Optarg[0])
 287         {
 288         case '^':
 289 
 290             strcpy (BatchBuffer, "MAIN");
 291             break;
 292 
 293         default:
 294 
 295             strcpy (BatchBuffer, AcpiGbl_Optarg);
 296             break;
 297         }
 298         break;
 299 
 300     case 'o':
 301 
 302         AcpiGbl_DbOpt_disasm = TRUE;
 303         AcpiGbl_DbOpt_stats = TRUE;
 304         break;
 305 
 306     case 'r':
 307 
 308         AcpiGbl_UseHwReducedFadt = TRUE;
 309         printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
 310         break;
 311 
 312     case 'v':
 313 
 314         switch (AcpiGbl_Optarg[0])
 315         {
 316         case '^':  /* -v: (Version): signon already emitted, just exit */
 317 
 318             exit (0);
 319 
 320         case 'i':
 321 
 322             AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
 323             break;
 324 
 325         case 'r':
 326 
 327             AcpiGbl_DisplayRegionAccess = TRUE;
 328             break;
 329 
 330         default:
 331 
 332             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
 333             return (-1);
 334         }
 335         break;
 336 
 337     case 'x':
 338 
 339         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
 340         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
 341         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
 342         break;
 343 
 344     default:
 345 
 346         usage();
 347         return (-1);
 348     }
 349 
 350     return (0);
 351 }
 352 
 353 
 354 /******************************************************************************
 355  *
 356  * FUNCTION:    main
 357  *
 358  * PARAMETERS:  argc, argv
 359  *
 360  * RETURN:      Status
 361  *
 362  * DESCRIPTION: Main routine for AcpiExec utility
 363  *
 364  *****************************************************************************/
 365 
 366 int ACPI_SYSTEM_XFACE
 367 main (
 368     int                     argc,
 369     char                    **argv)
 370 {
 371     ACPI_STATUS             Status;
 372     UINT32                  InitFlags;
 373     ACPI_TABLE_HEADER       *Table = NULL;
 374     UINT32                  TableCount;
 375     AE_TABLE_DESC           *TableDesc;
 376 
 377 
 378     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
 379 
 380     printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
 381     if (argc < 2)
 382     {
 383         usage ();
 384         return (0);
 385     }
 386 
 387     signal (SIGINT, AeCtrlCHandler);
 388 
 389     /* Init globals */
 390 
 391     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
 392     AcpiDbgLayer = 0xFFFFFFFF;
 393 
 394     /* Init ACPI and start debugger thread */
 395 
 396     Status = AcpiInitializeSubsystem ();
 397     AE_CHECK_OK (AcpiInitializeSubsystem, Status);
 398     if (ACPI_FAILURE (Status))
 399     {
 400         goto ErrorExit;
 401     }
 402 
 403     /* Get the command line options */
 404 
 405     if (AeDoOptions (argc, argv))
 406     {
 407         goto ErrorExit;
 408     }
 409 
 410     /* The remaining arguments are filenames for ACPI tables */
 411 
 412     if (!argv[AcpiGbl_Optind])
 413     {
 414         goto EnterDebugger;
 415     }
 416 
 417     AcpiGbl_DbOpt_tables = TRUE;
 418     TableCount = 0;
 419 
 420     /* Get each of the ACPI table files on the command line */
 421 
 422     while (argv[AcpiGbl_Optind])
 423     {
 424         /* Get one entire table */
 425 
 426         Status = AcpiDbReadTableFromFile (argv[AcpiGbl_Optind], &Table);
 427         if (ACPI_FAILURE (Status))
 428         {
 429             printf ("**** Could not get table from file %s, %s\n",
 430                 argv[AcpiGbl_Optind], AcpiFormatException (Status));
 431             goto ErrorExit;
 432         }
 433 
 434         /* Ignore non-AML tables, we can't use them. Except for an FADT */
 435 
 436         if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_FADT) &&
 437             !AcpiUtIsAmlTable (Table))
 438         {
 439             ACPI_INFO ((AE_INFO,
 440                 "Table [%4.4s] is not an AML table, ignoring",
 441                 Table->Signature));
 442             AcpiOsFree (Table);
 443         }
 444         else
 445         {
 446             /* Allocate and link a table descriptor */
 447 
 448             TableDesc = AcpiOsAllocate (sizeof (AE_TABLE_DESC));
 449             TableDesc->Table = Table;
 450             TableDesc->Next = AeTableListHead;
 451             AeTableListHead = TableDesc;
 452 
 453             TableCount++;
 454         }
 455 
 456         AcpiGbl_Optind++;
 457     }
 458 
 459     printf ("\n");
 460 
 461     /* Build a local RSDT with all tables and let ACPICA process the RSDT */
 462 
 463     Status = AeBuildLocalTables (TableCount, AeTableListHead);
 464     if (ACPI_FAILURE (Status))
 465     {
 466         goto ErrorExit;
 467     }
 468 
 469     Status = AeInstallTables ();
 470     if (ACPI_FAILURE (Status))
 471     {
 472         printf ("**** Could not load ACPI tables, %s\n",
 473             AcpiFormatException (Status));
 474         goto EnterDebugger;
 475     }
 476 
 477     /*
 478      * Install most of the handlers.
 479      * Override some default region handlers, especially SystemMemory
 480      */
 481     Status = AeInstallEarlyHandlers ();
 482     if (ACPI_FAILURE (Status))
 483     {
 484         goto EnterDebugger;
 485     }
 486 
 487     /* Setup initialization flags for ACPICA */
 488 
 489     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
 490     if (!AcpiGbl_DbOpt_ini_methods)
 491     {
 492         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
 493     }
 494 
 495     /*
 496      * Main initialization for ACPICA subsystem
 497      * TBD: Need a way to call this after the ACPI table "LOAD" command
 498      */
 499     Status = AcpiEnableSubsystem (InitFlags);
 500     if (ACPI_FAILURE (Status))
 501     {
 502         printf ("**** Could not EnableSubsystem, %s\n",
 503             AcpiFormatException (Status));
 504         goto EnterDebugger;
 505     }
 506 
 507     /*
 508      * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
 509      * and fixed event handlers
 510      */
 511     AeInstallLateHandlers ();
 512 
 513     /* Finish the ACPICA initialization */
 514 
 515     Status = AcpiInitializeObjects (InitFlags);
 516     if (ACPI_FAILURE (Status))
 517     {
 518         printf ("**** Could not InitializeObjects, %s\n",
 519             AcpiFormatException (Status));
 520         goto EnterDebugger;
 521     }
 522 
 523     AeMiscellaneousTests ();
 524 
 525 
 526 EnterDebugger:
 527 
 528     /* Exit if error above and we are in one of the batch modes */
 529 
 530     if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
 531     {
 532         goto ErrorExit;
 533     }
 534 
 535     /* Run a batch command or enter the command loop */
 536 
 537     switch (AcpiGbl_ExecutionMode)
 538     {
 539     default:
 540     case AE_MODE_COMMAND_LOOP:
 541 
 542         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
 543         break;
 544 
 545     case AE_MODE_BATCH_MULTIPLE:
 546 
 547         AcpiDbRunBatchMode ();
 548         break;
 549 
 550     case AE_MODE_BATCH_SINGLE:
 551 
 552         AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
 553         Status = AcpiTerminate ();
 554         break;
 555     }
 556 
 557     return (0);
 558 
 559 
 560 ErrorExit:
 561 
 562     (void) AcpiOsTerminate ();
 563     return (-1);
 564 }
 565 
 566 
 567 /******************************************************************************
 568  *
 569  * FUNCTION:    AcpiDbRunBatchMode
 570  *
 571  * PARAMETERS:  BatchCommandLine    - A semicolon separated list of commands
 572  *                                    to be executed.
 573  *                                    Use only commas to separate elements of
 574  *                                    particular command.
 575  * RETURN:      Status
 576  *
 577  * DESCRIPTION: For each command of list separated by ';' prepare the command
 578  *              buffer and pass it to AcpiDbCommandDispatch.
 579  *
 580  *****************************************************************************/
 581 
 582 static ACPI_STATUS
 583 AcpiDbRunBatchMode (
 584     void)
 585 {
 586     ACPI_STATUS             Status;
 587     char                    *Ptr = BatchBuffer;
 588     char                    *Cmd = Ptr;
 589     UINT8                   Run = 0;
 590 
 591 
 592     AcpiGbl_MethodExecuting = FALSE;
 593     AcpiGbl_StepToNextCall = FALSE;
 594 
 595     while (*Ptr)
 596     {
 597         if (*Ptr == ',')
 598         {
 599             /* Convert commas to spaces */
 600             *Ptr = ' ';
 601         }
 602         else if (*Ptr == ';')
 603         {
 604             *Ptr = '\0';
 605             Run = 1;
 606         }
 607 
 608         Ptr++;
 609 
 610         if (Run || (*Ptr == '\0'))
 611         {
 612             (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
 613             Run = 0;
 614             Cmd = Ptr;
 615         }
 616     }
 617 
 618     Status = AcpiTerminate ();
 619     return (Status);
 620 }