1 /******************************************************************************
2  *
3  * Module Name: aemain - Main routine for the AcpiExec utility
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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 
62 /* Local prototypes */
63 
64 static int
65 AeDoOptions (
66     int                     argc,
67     char                    **argv);
68 
69 static void
70 AcpiDbRunBatchMode (
71     void);
72 
73 
74 #define AE_BUFFER_SIZE              1024
75 #define ASL_MAX_FILES               256
76 
77 /* Execution modes */
78 
79 #define AE_MODE_COMMAND_LOOP        0   /* Normal command execution loop */
80 #define AE_MODE_BATCH_MULTIPLE      1   /* -b option to execute a command line */
81 #define AE_MODE_BATCH_SINGLE        2   /* -m option to execute a single control method */
82 
83 
84 /* Globals */
85 
86 UINT8                       AcpiGbl_RegionFillValue = 0;
87 BOOLEAN                     AcpiGbl_IgnoreErrors = FALSE;
88 BOOLEAN                     AcpiGbl_DbOpt_NoRegionSupport = FALSE;
89 UINT8                       AcpiGbl_UseHwReducedFadt = FALSE;
90 BOOLEAN                     AcpiGbl_DoInterfaceTests = FALSE;
91 BOOLEAN                     AcpiGbl_LoadTestTables = FALSE;
92 BOOLEAN                     AcpiGbl_AeLoadOnly = FALSE;
93 static UINT8                AcpiGbl_ExecutionMode = AE_MODE_COMMAND_LOOP;
94 static char                 BatchBuffer[AE_BUFFER_SIZE];    /* Batch command buffer */
95 static char                 AeBuildDate[] = __DATE__;
96 static char                 AeBuildTime[] = __TIME__;
97 
98 #define ACPIEXEC_NAME               "AML Execution/Debug Utility"
99 #define AE_SUPPORTED_OPTIONS        "?b:d:e:f^ghi:lm^rv^:x:"
100 
101 
102 /* Stubs for the disassembler */
103 
104 void
105 MpSaveGpioInfo (
106     ACPI_PARSE_OBJECT       *Op,
107     AML_RESOURCE            *Resource,
108     UINT32                  PinCount,
109     UINT16                  *PinList,
110     char                    *DeviceName)
111 {
112 }
113 
114 void
115 MpSaveSerialInfo (
116     ACPI_PARSE_OBJECT       *Op,
117     AML_RESOURCE            *Resource,
118     char                    *DeviceName)
119 {
120 }
121 
122 
123 /******************************************************************************
124  *
125  * FUNCTION:    usage
126  *
127  * PARAMETERS:  None
128  *
129  * RETURN:      None
130  *
131  * DESCRIPTION: Print a usage message
132  *
133  *****************************************************************************/
134 
135 static void
136 usage (
137     void)
138 {
139 
140     ACPI_USAGE_HEADER ("acpiexec [options] AMLfile1 AMLfile2 ...");
141 
142     ACPI_OPTION ("-b \"CommandLine\"",  "Batch mode command line execution (cmd1;cmd2;...)");
143     ACPI_OPTION ("-h -?",               "Display this help message");
144     ACPI_OPTION ("-m [Method]",         "Batch mode method execution. Default=MAIN");
145     printf ("\n");
146 
147     ACPI_OPTION ("-da",                 "Disable method abort on error");
148     ACPI_OPTION ("-di",                 "Disable execution of STA/INI methods during init");
149     ACPI_OPTION ("-do",                 "Disable Operation Region address simulation");
150     ACPI_OPTION ("-dr",                 "Disable repair of method return values");
151     ACPI_OPTION ("-ds",                 "Disable method auto-serialization");
152     ACPI_OPTION ("-dt",                 "Disable allocation tracking (performance)");
153     printf ("\n");
154 
155     ACPI_OPTION ("-ed",                 "Enable timer output for Debug Object");
156     ACPI_OPTION ("-ef",                 "Enable display of final memory statistics");
157     ACPI_OPTION ("-ei",                 "Enable additional tests for ACPICA interfaces");
158     ACPI_OPTION ("-el",                 "Enable loading of additional test tables");
159     ACPI_OPTION ("-em",                 "Enable grouping of module-level code");
160     ACPI_OPTION ("-ep",                 "Enable TermList parsing for scope objects");
161     ACPI_OPTION ("-es",                 "Enable Interpreter Slack Mode");
162     ACPI_OPTION ("-et",                 "Enable debug semaphore timeout");
163     printf ("\n");
164 
165     ACPI_OPTION ("-fi <File>",          "Specify namespace initialization file");
166     ACPI_OPTION ("-fv <Value>",         "Operation Region initialization fill value");
167     printf ("\n");
168 
169     ACPI_OPTION ("-i <Count>",          "Maximum iterations for AML while loops");
170     ACPI_OPTION ("-l",                  "Load tables and namespace only");
171     ACPI_OPTION ("-r",                  "Use hardware-reduced FADT V5");
172     ACPI_OPTION ("-v",                  "Display version information");
173     ACPI_OPTION ("-vd",                 "Display build date and time");
174     ACPI_OPTION ("-vi",                 "Verbose initialization output");
175     ACPI_OPTION ("-vr",                 "Verbose region handler output");
176     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
177 
178     printf ("\n  From within the interactive mode, use '?' or \"help\" to see\n"
179         "  a list of available AML Debugger commands\n");
180 }
181 
182 
183 /******************************************************************************
184  *
185  * FUNCTION:    AeDoOptions
186  *
187  * PARAMETERS:  argc/argv           - Standard argc/argv
188  *
189  * RETURN:      Status
190  *
191  * DESCRIPTION: Command line option processing
192  *
193  *****************************************************************************/
194 
195 static int
196 AeDoOptions (
197     int                     argc,
198     char                    **argv)
199 {
200     int                     j;
201     UINT32                  Temp;
202 
203 
204     while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
205     {
206     case 'b':
207 
208         if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
209         {
210             printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
211                 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
212             return (-1);
213         }
214         AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
215         strcpy (BatchBuffer, AcpiGbl_Optarg);
216         break;
217 
218     case 'd':
219 
220         switch (AcpiGbl_Optarg[0])
221         {
222         case 'a':
223 
224             AcpiGbl_IgnoreErrors = TRUE;
225             break;
226 
227         case 'i':
228 
229             AcpiGbl_DbOpt_NoIniMethods = TRUE;
230             break;
231 
232         case 'o':
233 
234             AcpiGbl_DbOpt_NoRegionSupport = TRUE;
235             break;
236 
237         case 'r':
238 
239             AcpiGbl_DisableAutoRepair = TRUE;
240             break;
241 
242         case 's':
243 
244             AcpiGbl_AutoSerializeMethods = FALSE;
245             break;
246 
247         case 't':
248 
249             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
250                 AcpiGbl_DisableMemTracking = TRUE;
251             #endif
252             break;
253 
254         default:
255 
256             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
257             return (-1);
258         }
259         break;
260 
261     case 'e':
262 
263         switch (AcpiGbl_Optarg[0])
264         {
265         case 'd':
266 
267             AcpiGbl_DisplayDebugTimer = TRUE;
268             break;
269 
270         case 'f':
271 
272             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
273                 AcpiGbl_DisplayFinalMemStats = TRUE;
274             #endif
275             break;
276 
277         case 'i':
278 
279             AcpiGbl_DoInterfaceTests = TRUE;
280             break;
281 
282         case 'l':
283 
284             AcpiGbl_LoadTestTables = TRUE;
285             break;
286 
287         case 'm':
288 
289             AcpiGbl_GroupModuleLevelCode = TRUE;
290             break;
291 
292         case 'p':
293 
294             AcpiGbl_ParseTableAsTermList = TRUE;
295             break;
296 
297         case 's':
298 
299             AcpiGbl_EnableInterpreterSlack = TRUE;
300             printf ("Enabling AML Interpreter slack mode\n");
301             break;
302 
303         case 't':
304 
305             AcpiGbl_DebugTimeout = TRUE;
306             break;
307 
308         default:
309 
310             printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
311             return (-1);
312         }
313         break;
314 
315     case 'f':
316 
317         switch (AcpiGbl_Optarg[0])
318         {
319         case 'v':   /* -fv: region fill value */
320 
321             if (AcpiGetoptArgument (argc, argv))
322             {
323                 return (-1);
324             }
325 
326             AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
327             break;
328 
329         case 'i':   /* -fi: specify initialization file */
330 
331             if (AcpiGetoptArgument (argc, argv))
332             {
333                 return (-1);
334             }
335 
336             if (AeOpenInitializationFile (AcpiGbl_Optarg))
337             {
338                 return (-1);
339             }
340             break;
341 
342         default:
343 
344             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
345             return (-1);
346         }
347         break;
348 
349     case 'g':
350 
351         AcpiGbl_DbFilename = NULL;
352         break;
353 
354     case 'h':
355     case '?':
356 
357         usage();
358         return (1);
359 
360     case 'i':
361 
362         Temp = strtoul (AcpiGbl_Optarg, NULL, 0);
363         if (!Temp || (Temp > ACPI_UINT16_MAX))
364         {
365             printf ("%s: Invalid max loops value\n", AcpiGbl_Optarg);
366             return (-1);
367         }
368 
369         AcpiGbl_MaxLoopIterations = (UINT16) Temp;
370         printf ("Max Loop Iterations is %u (0x%X)\n",
371             AcpiGbl_MaxLoopIterations, AcpiGbl_MaxLoopIterations);
372         break;
373 
374     case 'l':
375 
376         AcpiGbl_AeLoadOnly = TRUE;
377         break;
378 
379     case 'm':
380 
381         AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
382         switch (AcpiGbl_Optarg[0])
383         {
384         case '^':
385 
386             strcpy (BatchBuffer, "MAIN");
387             break;
388 
389         default:
390 
391             strcpy (BatchBuffer, AcpiGbl_Optarg);
392             break;
393         }
394         break;
395 
396     case 'r':
397 
398         AcpiGbl_UseHwReducedFadt = TRUE;
399         printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
400         break;
401 
402     case 'v':
403 
404         switch (AcpiGbl_Optarg[0])
405         {
406         case '^':  /* -v: (Version): signon already emitted, just exit */
407 
408             (void) AcpiOsTerminate ();
409             return (1);
410 
411         case 'd':
412 
413             printf ("Build date/time: %s %s\n", AeBuildDate, AeBuildTime);
414             return (1);
415 
416         case 'i':
417 
418             AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
419             break;
420 
421         case 'r':
422 
423             AcpiGbl_DisplayRegionAccess = TRUE;
424             break;
425 
426         default:
427 
428             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
429             return (-1);
430         }
431         break;
432 
433     case 'x':
434 
435         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
436         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
437         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
438         break;
439 
440     default:
441 
442         usage();
443         return (-1);
444     }
445 
446     return (0);
447 }
448 
449 
450 /******************************************************************************
451  *
452  * FUNCTION:    main
453  *
454  * PARAMETERS:  argc, argv
455  *
456  * RETURN:      Status
457  *
458  * DESCRIPTION: Main routine for AcpiExec utility
459  *
460  *****************************************************************************/
461 
462 int ACPI_SYSTEM_XFACE
463 main (
464     int                     argc,
465     char                    **argv)
466 {
467     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
468     ACPI_STATUS             Status;
469     UINT32                  InitFlags;
470     int                     ExitCode = 0;
471 
472 
473     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
474     signal (SIGINT, AeCtrlCHandler);
475 
476     /* Init debug globals */
477 
478     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
479     AcpiDbgLayer = 0xFFFFFFFF;
480 
481     /*
482      * Initialize ACPICA and start debugger thread.
483      *
484      * NOTE: After ACPICA initialization, AcpiTerminate MUST be called
485      * before this procedure exits -- otherwise, the console may be
486      * left in an incorrect state.
487      */
488     Status = AcpiInitializeSubsystem ();
489     ACPI_CHECK_OK (AcpiInitializeSubsystem, Status);
490     if (ACPI_FAILURE (Status))
491     {
492         goto ErrorExit;
493     }
494 
495     /* ACPICA runtime configuration */
496 
497     AcpiGbl_MaxLoopIterations = 400;
498 
499 
500     /* Initialize the AML debugger */
501 
502     Status = AcpiInitializeDebugger ();
503     ACPI_CHECK_OK (AcpiInitializeDebugger, Status);
504     if (ACPI_FAILURE (Status))
505     {
506         goto ErrorExit;
507     }
508 
509     printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
510     if (argc < 2)
511     {
512         usage ();
513         goto NormalExit;
514     }
515 
516     /* Get the command line options */
517 
518     ExitCode = AeDoOptions (argc, argv);
519     if (ExitCode)
520     {
521         if (ExitCode > 0)
522         {
523             ExitCode = 0;
524         }
525 
526         goto ErrorExit;
527     }
528 
529     /* The remaining arguments are filenames for ACPI tables */
530 
531     if (!argv[AcpiGbl_Optind])
532     {
533         goto EnterDebugger;
534     }
535 
536     AcpiGbl_CstyleDisassembly = FALSE; /* Not supported for AcpiExec */
537 
538     /* Get each of the ACPI table files on the command line */
539 
540     while (argv[AcpiGbl_Optind])
541     {
542         /* Get all ACPI AML tables in this file */
543 
544         Status = AcGetAllTablesFromFile (argv[AcpiGbl_Optind],
545             ACPI_GET_ONLY_AML_TABLES, &ListHead);
546         if (ACPI_FAILURE (Status))
547         {
548             ExitCode = -1;
549             goto ErrorExit;
550         }
551 
552         AcpiGbl_Optind++;
553     }
554 
555     printf ("\n");
556 
557     /* Build a local RSDT with all tables and let ACPICA process the RSDT */
558 
559     Status = AeBuildLocalTables (ListHead);
560     if (ACPI_FAILURE (Status))
561     {
562         goto ErrorExit;
563     }
564 
565     /* Install all of the ACPI tables */
566 
567     Status = AeInstallTables ();
568     if (ACPI_FAILURE (Status))
569     {
570         printf ("**** Could not install ACPI tables, %s\n",
571             AcpiFormatException (Status));
572         goto EnterDebugger;
573     }
574 
575     /*
576      * Install most of the handlers (Regions, Notify, Table, etc.)
577      * Override the default region handlers, especially SystemMemory,
578      * which is simulated in this utility.
579      */
580     Status = AeInstallEarlyHandlers ();
581     if (ACPI_FAILURE (Status))
582     {
583         goto EnterDebugger;
584     }
585 
586     /* Setup initialization flags for ACPICA */
587 
588     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
589     if (AcpiGbl_DbOpt_NoIniMethods)
590     {
591         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
592     }
593 
594     /*
595      * Main initialization for ACPICA subsystem
596      * TBD: Need a way to call this after the ACPI table "LOAD" command?
597      *
598      * NOTE: This initialization does not match the _Lxx and _Exx methods
599      * to individual GPEs, as there are no real GPEs when the hardware
600      * is simulated - because there is no namespace until AeLoadTables is
601      * executed. This may have to change if AcpiExec is ever run natively
602      * on actual hardware (such as under UEFI).
603      */
604     Status = AcpiEnableSubsystem (InitFlags);
605     if (ACPI_FAILURE (Status))
606     {
607         printf ("**** Could not EnableSubsystem, %s\n",
608             AcpiFormatException (Status));
609         goto EnterDebugger;
610     }
611 
612     Status = AeLoadTables ();
613 
614     /*
615      * Exit namespace initialization for the "load namespace only" option.
616      * No control methods will be executed. However, still enter the
617      * the debugger.
618      */
619     if (AcpiGbl_AeLoadOnly)
620     {
621         goto EnterDebugger;
622     }
623 
624     if (ACPI_FAILURE (Status))
625     {
626         printf ("**** Could not load ACPI tables, %s\n",
627             AcpiFormatException (Status));
628         goto EnterDebugger;
629     }
630 
631     /*
632      * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
633      * and fixed event handlers
634      */
635     AeInstallLateHandlers ();
636 
637     /* Finish the ACPICA initialization */
638 
639     Status = AcpiInitializeObjects (InitFlags);
640     if (ACPI_FAILURE (Status))
641     {
642         printf ("**** Could not InitializeObjects, %s\n",
643             AcpiFormatException (Status));
644         goto EnterDebugger;
645     }
646 
647     AeMiscellaneousTests ();
648 
649 
650 EnterDebugger:
651 
652     /* Exit if error above and we are in one of the batch modes */
653 
654     if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
655     {
656         goto ErrorExit;
657     }
658 
659     /* Run a batch command or enter the command loop */
660 
661     switch (AcpiGbl_ExecutionMode)
662     {
663     default:
664     case AE_MODE_COMMAND_LOOP:
665 
666         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
667         break;
668 
669     case AE_MODE_BATCH_MULTIPLE:
670 
671         AcpiDbRunBatchMode ();
672         break;
673 
674     case AE_MODE_BATCH_SINGLE:
675 
676         AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
677         break;
678     }
679 
680     /* Shut down the debugger and ACPICA */
681 
682 #if 0
683 
684     /* Temporarily removed */
685     AcpiTerminateDebugger ();
686     (void) AcpiTerminate ();
687 #endif
688 
689 NormalExit:
690     ExitCode = 0;
691 
692 ErrorExit:
693     (void) AcpiOsTerminate ();
694     return (ExitCode);
695 }
696 
697 
698 /******************************************************************************
699  *
700  * FUNCTION:    AcpiDbRunBatchMode
701  *
702  * PARAMETERS:  BatchCommandLine    - A semicolon separated list of commands
703  *                                    to be executed.
704  *                                    Use only commas to separate elements of
705  *                                    particular command.
706  * RETURN:      None
707  *
708  * DESCRIPTION: For each command of list separated by ';' prepare the command
709  *              buffer and pass it to AcpiDbCommandDispatch.
710  *
711  *****************************************************************************/
712 
713 static void
714 AcpiDbRunBatchMode (
715     void)
716 {
717     char                    *Ptr = BatchBuffer;
718     char                    *Cmd = Ptr;
719     UINT8                   Run = 0;
720 
721 
722     AcpiGbl_MethodExecuting = FALSE;
723     AcpiGbl_StepToNextCall = FALSE;
724 
725     while (*Ptr)
726     {
727         if (*Ptr == ',')
728         {
729             /* Convert commas to spaces */
730             *Ptr = ' ';
731         }
732         else if (*Ptr == ';')
733         {
734             *Ptr = '\0';
735             Run = 1;
736         }
737 
738         Ptr++;
739 
740         if (Run || (*Ptr == '\0'))
741         {
742             (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
743             Run = 0;
744             Cmd = Ptr;
745         }
746     }
747 }
748