1 /******************************************************************************
2  *
3  * Module Name: aemain - Main routine for the AcpiExec utility
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, 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 BOOLEAN                     AcpiGbl_AeLoadOnly = FALSE;
94 static UINT8                AcpiGbl_ExecutionMode = AE_MODE_COMMAND_LOOP;
95 static char                 BatchBuffer[AE_BUFFER_SIZE];    /* Batch command buffer */
96 static AE_TABLE_DESC        *AeTableListHead = NULL;
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 ("-ef",                 "Enable display of final memory statistics");
156     ACPI_OPTION ("-ei",                 "Enable additional tests for ACPICA interfaces");
157     ACPI_OPTION ("-el",                 "Enable loading of additional test tables");
158     ACPI_OPTION ("-es",                 "Enable Interpreter Slack Mode");
159     ACPI_OPTION ("-et",                 "Enable debug semaphore timeout");
160     printf ("\n");
161 
162     ACPI_OPTION ("-fi <File>",          "Specify namespace initialization file");
163     ACPI_OPTION ("-fv <Value>",         "Operation Region initialization fill value");
164     printf ("\n");
165 
166     ACPI_OPTION ("-i <Count>",          "Maximum iterations for AML while loops");
167     ACPI_OPTION ("-l",                  "Load tables and namespace only");
168     ACPI_OPTION ("-r",                  "Use hardware-reduced FADT V5");
169     ACPI_OPTION ("-v",                  "Display version information");
170     ACPI_OPTION ("-vi",                 "Verbose initialization output");
171     ACPI_OPTION ("-vr",                 "Verbose region handler output");
172     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
173 
174     printf ("\n  From within the interactive mode, use '?' or \"help\" to see\n"
175         "  a list of available AML Debugger commands\n");
176 }
177 
178 
179 /******************************************************************************
180  *
181  * FUNCTION:    AeDoOptions
182  *
183  * PARAMETERS:  argc/argv           - Standard argc/argv
184  *
185  * RETURN:      Status
186  *
187  * DESCRIPTION: Command line option processing
188  *
189  *****************************************************************************/
190 
191 static int
192 AeDoOptions (
193     int                     argc,
194     char                    **argv)
195 {
196     int                     j;
197     UINT32                  Temp;
198 
199 
200     while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
201     {
202     case 'b':
203 
204         if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
205         {
206             printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
207                 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
208             return (-1);
209         }
210         AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
211         strcpy (BatchBuffer, AcpiGbl_Optarg);
212         break;
213 
214     case 'd':
215 
216         switch (AcpiGbl_Optarg[0])
217         {
218         case 'a':
219 
220             AcpiGbl_IgnoreErrors = TRUE;
221             break;
222 
223         case 'i':
224 
225             AcpiGbl_DbOpt_NoIniMethods = TRUE;
226             break;
227 
228         case 'o':
229 
230             AcpiGbl_DbOpt_NoRegionSupport = TRUE;
231             break;
232 
233         case 'r':
234 
235             AcpiGbl_DisableAutoRepair = TRUE;
236             break;
237 
238         case 's':
239 
240             AcpiGbl_AutoSerializeMethods = FALSE;
241             break;
242 
243         case 't':
244 
245             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
246                 AcpiGbl_DisableMemTracking = TRUE;
247             #endif
248             break;
249 
250         default:
251 
252             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
253             return (-1);
254         }
255         break;
256 
257     case 'e':
258 
259         switch (AcpiGbl_Optarg[0])
260         {
261         case 'f':
262 
263             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
264                 AcpiGbl_DisplayFinalMemStats = TRUE;
265             #endif
266             break;
267 
268         case 'i':
269 
270             AcpiGbl_DoInterfaceTests = TRUE;
271             break;
272 
273         case 'l':
274 
275             AcpiGbl_LoadTestTables = TRUE;
276             break;
277 
278         case 's':
279 
280             AcpiGbl_EnableInterpreterSlack = TRUE;
281             printf ("Enabling AML Interpreter slack mode\n");
282             break;
283 
284         case 't':
285 
286             AcpiGbl_DebugTimeout = TRUE;
287             break;
288 
289         default:
290 
291             printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
292             return (-1);
293         }
294         break;
295 
296     case 'f':
297 
298         switch (AcpiGbl_Optarg[0])
299         {
300         case 'v':   /* -fv: region fill value */
301 
302             if (AcpiGetoptArgument (argc, argv))
303             {
304                 return (-1);
305             }
306 
307             AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
308             break;
309 
310         case 'i':   /* -fi: specify initialization file */
311 
312             if (AcpiGetoptArgument (argc, argv))
313             {
314                 return (-1);
315             }
316 
317             if (AeOpenInitializationFile (AcpiGbl_Optarg))
318             {
319                 return (-1);
320             }
321             break;
322 
323         default:
324 
325             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
326             return (-1);
327         }
328         break;
329 
330     case 'g':
331 
332         AcpiGbl_DbFilename = NULL;
333         break;
334 
335     case 'h':
336     case '?':
337 
338         usage();
339         return (1);
340 
341     case 'i':
342 
343         Temp = strtoul (AcpiGbl_Optarg, NULL, 0);
344         if (!Temp || (Temp > ACPI_UINT16_MAX))
345         {
346             printf ("%s: Invalid max loops value\n", AcpiGbl_Optarg);
347             return (-1);
348         }
349 
350         AcpiGbl_MaxLoopIterations = (UINT16) Temp;
351         printf ("Max Loop Iterations is %u (0x%X)\n",
352             AcpiGbl_MaxLoopIterations, AcpiGbl_MaxLoopIterations);
353         break;
354 
355     case 'l':
356 
357         AcpiGbl_AeLoadOnly = TRUE;
358         break;
359 
360     case 'm':
361 
362         AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
363         switch (AcpiGbl_Optarg[0])
364         {
365         case '^':
366 
367             strcpy (BatchBuffer, "MAIN");
368             break;
369 
370         default:
371 
372             strcpy (BatchBuffer, AcpiGbl_Optarg);
373             break;
374         }
375         break;
376 
377     case 'r':
378 
379         AcpiGbl_UseHwReducedFadt = TRUE;
380         printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
381         break;
382 
383     case 'v':
384 
385         switch (AcpiGbl_Optarg[0])
386         {
387         case '^':  /* -v: (Version): signon already emitted, just exit */
388 
389             (void) AcpiOsTerminate ();
390             return (1);
391 
392         case 'i':
393 
394             AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
395             break;
396 
397         case 'r':
398 
399             AcpiGbl_DisplayRegionAccess = TRUE;
400             break;
401 
402         default:
403 
404             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
405             return (-1);
406         }
407         break;
408 
409     case 'x':
410 
411         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
412         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
413         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
414         break;
415 
416     default:
417 
418         usage();
419         return (-1);
420     }
421 
422     return (0);
423 }
424 
425 
426 /******************************************************************************
427  *
428  * FUNCTION:    main
429  *
430  * PARAMETERS:  argc, argv
431  *
432  * RETURN:      Status
433  *
434  * DESCRIPTION: Main routine for AcpiExec utility
435  *
436  *****************************************************************************/
437 
438 int ACPI_SYSTEM_XFACE
439 main (
440     int                     argc,
441     char                    **argv)
442 {
443     ACPI_STATUS             Status;
444     UINT32                  InitFlags;
445     ACPI_TABLE_HEADER       *Table = NULL;
446     UINT32                  TableCount;
447     AE_TABLE_DESC           *TableDesc;
448     int                     ExitCode = 0;
449 
450 
451     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
452     signal (SIGINT, AeCtrlCHandler);
453 
454     /* Init debug globals */
455 
456     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
457     AcpiDbgLayer = 0xFFFFFFFF;
458 
459     /* Init ACPICA and start debugger thread */
460 
461     Status = AcpiInitializeSubsystem ();
462     AE_CHECK_OK (AcpiInitializeSubsystem, Status);
463     if (ACPI_FAILURE (Status))
464     {
465         goto ErrorExit;
466     }
467 
468     /* ACPICA runtime configuration */
469 
470     AcpiGbl_MaxLoopIterations = 400;
471 
472 
473     /* Initialize the AML debugger */
474 
475     Status = AcpiInitializeDebugger ();
476     AE_CHECK_OK (AcpiInitializeDebugger, Status);
477     if (ACPI_FAILURE (Status))
478     {
479         goto ErrorExit;
480     }
481 
482     printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
483     if (argc < 2)
484     {
485         usage ();
486         (void) AcpiOsTerminate ();
487         return (0);
488     }
489 
490     /* Get the command line options */
491 
492     ExitCode = AeDoOptions (argc, argv);
493     if (ExitCode)
494     {
495         if (ExitCode > 0)
496         {
497             ExitCode = 0;
498         }
499         goto ErrorExit;
500     }
501 
502     /* The remaining arguments are filenames for ACPI tables */
503 
504     if (!argv[AcpiGbl_Optind])
505     {
506         goto EnterDebugger;
507     }
508 
509     AcpiGbl_CstyleDisassembly = FALSE; /* Not supported for AcpiExec */
510     TableCount = 0;
511 
512     /* Get each of the ACPI table files on the command line */
513 
514     while (argv[AcpiGbl_Optind])
515     {
516         /* Get one entire table */
517 
518         Status = AcpiUtReadTableFromFile (argv[AcpiGbl_Optind], &Table);
519         if (ACPI_FAILURE (Status))
520         {
521             fprintf (stderr, "**** Could not get table from file %s, %s\n",
522                 argv[AcpiGbl_Optind], AcpiFormatException (Status));
523             goto ErrorExit;
524         }
525 
526         /* Ignore non-AML tables, we can't use them. Except for an FADT */
527 
528         if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_FADT) &&
529             !AcpiUtIsAmlTable (Table))
530         {
531             fprintf (stderr, "    %s: [%4.4s] is not an AML table - ignoring\n",
532                  argv[AcpiGbl_Optind], Table->Signature);
533 
534             AcpiOsFree (Table);
535         }
536         else
537         {
538             /* Allocate and link a table descriptor */
539 
540             TableDesc = AcpiOsAllocate (sizeof (AE_TABLE_DESC));
541             TableDesc->Table = Table;
542             TableDesc->Next = AeTableListHead;
543             AeTableListHead = TableDesc;
544 
545             TableCount++;
546         }
547 
548         AcpiGbl_Optind++;
549     }
550 
551     printf ("\n");
552 
553     /* Build a local RSDT with all tables and let ACPICA process the RSDT */
554 
555     Status = AeBuildLocalTables (TableCount, AeTableListHead);
556     if (ACPI_FAILURE (Status))
557     {
558         goto ErrorExit;
559     }
560 
561     Status = AeInstallTables ();
562 
563     /*
564      * Exit namespace initialization for the "load namespace only" option.
565      * No control methods will be executed. However, still enter the
566      * the debugger.
567      */
568     if (AcpiGbl_AeLoadOnly)
569     {
570         goto EnterDebugger;
571     }
572 
573     if (ACPI_FAILURE (Status))
574     {
575         printf ("**** Could not load ACPI tables, %s\n",
576             AcpiFormatException (Status));
577         goto EnterDebugger;
578     }
579 
580     /*
581      * Install most of the handlers.
582      * Override some default region handlers, especially SystemMemory
583      */
584     Status = AeInstallEarlyHandlers ();
585     if (ACPI_FAILURE (Status))
586     {
587         goto EnterDebugger;
588     }
589 
590     /* Setup initialization flags for ACPICA */
591 
592     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
593     if (AcpiGbl_DbOpt_NoIniMethods)
594     {
595         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
596     }
597 
598     /*
599      * Main initialization for ACPICA subsystem
600      * TBD: Need a way to call this after the ACPI table "LOAD" command
601      */
602     Status = AcpiEnableSubsystem (InitFlags);
603     if (ACPI_FAILURE (Status))
604     {
605         printf ("**** Could not EnableSubsystem, %s\n",
606             AcpiFormatException (Status));
607         goto EnterDebugger;
608     }
609 
610     /*
611      * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
612      * and fixed event handlers
613      */
614     AeInstallLateHandlers ();
615 
616     /* Finish the ACPICA initialization */
617 
618     Status = AcpiInitializeObjects (InitFlags);
619     if (ACPI_FAILURE (Status))
620     {
621         printf ("**** Could not InitializeObjects, %s\n",
622             AcpiFormatException (Status));
623         goto EnterDebugger;
624     }
625 
626     AeMiscellaneousTests ();
627 
628 
629 EnterDebugger:
630 
631     /* Exit if error above and we are in one of the batch modes */
632 
633     if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
634     {
635         goto ErrorExit;
636     }
637 
638     /* Run a batch command or enter the command loop */
639 
640     switch (AcpiGbl_ExecutionMode)
641     {
642     default:
643     case AE_MODE_COMMAND_LOOP:
644 
645         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
646         break;
647 
648     case AE_MODE_BATCH_MULTIPLE:
649 
650         AcpiDbRunBatchMode ();
651         break;
652 
653     case AE_MODE_BATCH_SINGLE:
654 
655         AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
656 
657         /* Shut down the debugger */
658 
659         AcpiTerminateDebugger ();
660         Status = AcpiTerminate ();
661         break;
662     }
663 
664     return (0);
665 
666 
667 ErrorExit:
668     (void) AcpiOsTerminate ();
669     return (ExitCode);
670 }
671 
672 
673 /******************************************************************************
674  *
675  * FUNCTION:    AcpiDbRunBatchMode
676  *
677  * PARAMETERS:  BatchCommandLine    - A semicolon separated list of commands
678  *                                    to be executed.
679  *                                    Use only commas to separate elements of
680  *                                    particular command.
681  * RETURN:      Status
682  *
683  * DESCRIPTION: For each command of list separated by ';' prepare the command
684  *              buffer and pass it to AcpiDbCommandDispatch.
685  *
686  *****************************************************************************/
687 
688 static ACPI_STATUS
689 AcpiDbRunBatchMode (
690     void)
691 {
692     ACPI_STATUS             Status;
693     char                    *Ptr = BatchBuffer;
694     char                    *Cmd = Ptr;
695     UINT8                   Run = 0;
696 
697 
698     AcpiGbl_MethodExecuting = FALSE;
699     AcpiGbl_StepToNextCall = FALSE;
700 
701     while (*Ptr)
702     {
703         if (*Ptr == ',')
704         {
705             /* Convert commas to spaces */
706             *Ptr = ' ';
707         }
708         else if (*Ptr == ';')
709         {
710             *Ptr = '\0';
711             Run = 1;
712         }
713 
714         Ptr++;
715 
716         if (Run || (*Ptr == '\0'))
717         {
718             (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
719             Run = 0;
720             Cmd = Ptr;
721         }
722     }
723 
724     /* Shut down the debugger */
725 
726     AcpiTerminateDebugger ();
727     Status = AcpiTerminate ();
728     return (Status);
729 }
730