1 /******************************************************************************
2  *
3  * Module Name: apmain - Main module for the acpidump 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 #define _DECLARE_GLOBALS
45 #include "acpidump.h"
46 
47 
48 /*
49  * acpidump - A portable utility for obtaining system ACPI tables and dumping
50  * them in an ASCII hex format suitable for binary extraction via acpixtract.
51  *
52  * Obtaining the system ACPI tables is an OS-specific operation.
53  *
54  * This utility can be ported to any host operating system by providing a
55  * module containing system-specific versions of these interfaces:
56  *
57  *      AcpiOsGetTableByAddress
58  *      AcpiOsGetTableByIndex
59  *      AcpiOsGetTableByName
60  *
61  * See the ACPICA Reference Guide for the exact definitions of these
62  * interfaces. Also, see these ACPICA source code modules for example
63  * implementations:
64  *
65  *      source/os_specific/service_layers/oswintbl.c
66  *      source/os_specific/service_layers/oslinuxtbl.c
67  */
68 
69 
70 /* Local prototypes */
71 
72 static void
73 ApDisplayUsage (
74     void);
75 
76 static int
77 ApDoOptions (
78     int                     argc,
79     char                    **argv);
80 
81 static int
82 ApInsertAction (
83     char                    *Argument,
84     UINT32                  ToBeDone);
85 
86 
87 /* Table for deferred actions from command line options */
88 
89 AP_DUMP_ACTION              ActionTable [AP_MAX_ACTIONS];
90 UINT32                      CurrentAction = 0;
91 
92 
93 #define AP_UTILITY_NAME             "ACPI Binary Table Dump Utility"
94 #define AP_SUPPORTED_OPTIONS        "?a:bc:f:hn:o:r:svxz"
95 
96 
97 /******************************************************************************
98  *
99  * FUNCTION:    ApDisplayUsage
100  *
101  * DESCRIPTION: Usage message for the AcpiDump utility
102  *
103  ******************************************************************************/
104 
105 static void
106 ApDisplayUsage (
107     void)
108 {
109 
110     ACPI_USAGE_HEADER ("acpidump [options]");
111 
112     ACPI_OPTION ("-b",                      "Dump tables to binary files");
113     ACPI_OPTION ("-h -?",                   "This help message");
114     ACPI_OPTION ("-o <File>",               "Redirect output to file");
115     ACPI_OPTION ("-r <Address>",            "Dump tables from specified RSDP");
116     ACPI_OPTION ("-s",                      "Print table summaries only");
117     ACPI_OPTION ("-v",                      "Display version information");
118     ACPI_OPTION ("-z",                      "Verbose mode");
119 
120     ACPI_USAGE_TEXT ("\nTable Options:\n");
121 
122     ACPI_OPTION ("-a <Address>",            "Get table via a physical address");
123     ACPI_OPTION ("-c <on|off>",             "Turning on/off customized table dumping");
124     ACPI_OPTION ("-f <BinaryFile>",         "Get table via a binary file");
125     ACPI_OPTION ("-n <Signature>",          "Get table via a name/signature");
126     ACPI_OPTION ("-x",                      "Do not use but dump XSDT");
127     ACPI_OPTION ("-x -x",                   "Do not use or dump XSDT");
128 
129     ACPI_USAGE_TEXT (
130         "\n"
131         "Invocation without parameters dumps all available tables\n"
132         "Multiple mixed instances of -a, -f, and -n are supported\n\n");
133 }
134 
135 
136 /******************************************************************************
137  *
138  * FUNCTION:    ApInsertAction
139  *
140  * PARAMETERS:  Argument            - Pointer to the argument for this action
141  *              ToBeDone            - What to do to process this action
142  *
143  * RETURN:      Status
144  *
145  * DESCRIPTION: Add an action item to the action table
146  *
147  ******************************************************************************/
148 
149 static int
150 ApInsertAction (
151     char                    *Argument,
152     UINT32                  ToBeDone)
153 {
154 
155     /* Insert action and check for table overflow */
156 
157     ActionTable [CurrentAction].Argument = Argument;
158     ActionTable [CurrentAction].ToBeDone = ToBeDone;
159 
160     CurrentAction++;
161     if (CurrentAction > AP_MAX_ACTIONS)
162     {
163         fprintf (stderr, "Too many table options (max %u)\n", AP_MAX_ACTIONS);
164         return (-1);
165     }
166 
167     return (0);
168 }
169 
170 
171 /******************************************************************************
172  *
173  * FUNCTION:    ApDoOptions
174  *
175  * PARAMETERS:  argc/argv           - Standard argc/argv
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Command line option processing. The main actions for getting
180  *              and dumping tables are deferred via the action table.
181  *
182  *****************************************************************************/
183 
184 static int
185 ApDoOptions (
186     int                     argc,
187     char                    **argv)
188 {
189     int                     j;
190     ACPI_STATUS             Status;
191 
192 
193     /* Command line options */
194 
195     while ((j = AcpiGetopt (argc, argv, AP_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
196     {
197     /*
198      * Global options
199      */
200     case 'b':   /* Dump all input tables to binary files */
201 
202         Gbl_BinaryMode = TRUE;
203         continue;
204 
205     case 'c':   /* Dump customized tables */
206 
207         if (!strcmp (AcpiGbl_Optarg, "on"))
208         {
209             Gbl_DumpCustomizedTables = TRUE;
210         }
211         else if (!strcmp (AcpiGbl_Optarg, "off"))
212         {
213             Gbl_DumpCustomizedTables = FALSE;
214         }
215         else
216         {
217             fprintf (stderr, "%s: Cannot handle this switch, please use on|off\n",
218                 AcpiGbl_Optarg);
219             return (-1);
220         }
221         continue;
222 
223     case 'h':
224     case '?':
225 
226         ApDisplayUsage ();
227         return (1);
228 
229     case 'o':   /* Redirect output to a single file */
230 
231         if (ApOpenOutputFile (AcpiGbl_Optarg))
232         {
233             return (-1);
234         }
235         continue;
236 
237     case 'r':   /* Dump tables from specified RSDP */
238 
239         Status = AcpiUtStrtoul64 (AcpiGbl_Optarg, ACPI_STRTOUL_64BIT,
240             &Gbl_RsdpBase);
241         if (ACPI_FAILURE (Status))
242         {
243             fprintf (stderr, "%s: Could not convert to a physical address\n",
244                 AcpiGbl_Optarg);
245             return (-1);
246         }
247         continue;
248 
249     case 's':   /* Print table summaries only */
250 
251         Gbl_SummaryMode = TRUE;
252         continue;
253 
254     case 'x':   /* Do not use XSDT */
255 
256         if (!AcpiGbl_DoNotUseXsdt)
257         {
258             AcpiGbl_DoNotUseXsdt = TRUE;
259         }
260         else
261         {
262             Gbl_DoNotDumpXsdt = TRUE;
263         }
264         continue;
265 
266     case 'v':   /* Revision/version */
267 
268         AcpiOsPrintf (ACPI_COMMON_SIGNON (AP_UTILITY_NAME));
269         return (1);
270 
271     case 'z':   /* Verbose mode */
272 
273         Gbl_VerboseMode = TRUE;
274         fprintf (stderr, ACPI_COMMON_SIGNON (AP_UTILITY_NAME));
275         continue;
276 
277     /*
278      * Table options
279      */
280     case 'a':   /* Get table by physical address */
281 
282         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_ADDRESS))
283         {
284             return (-1);
285         }
286         break;
287 
288     case 'f':   /* Get table from a file */
289 
290         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_FILE))
291         {
292             return (-1);
293         }
294         break;
295 
296     case 'n':   /* Get table by input name (signature) */
297 
298         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_NAME))
299         {
300             return (-1);
301         }
302         break;
303 
304     default:
305 
306         ApDisplayUsage ();
307         return (-1);
308     }
309 
310     /* If there are no actions, this means "get/dump all tables" */
311 
312     if (CurrentAction == 0)
313     {
314         if (ApInsertAction (NULL, AP_DUMP_ALL_TABLES))
315         {
316             return (-1);
317         }
318     }
319 
320     return (0);
321 }
322 
323 
324 /******************************************************************************
325  *
326  * FUNCTION:    main
327  *
328  * PARAMETERS:  argc/argv           - Standard argc/argv
329  *
330  * RETURN:      Status
331  *
332  * DESCRIPTION: C main function for acpidump utility
333  *
334  ******************************************************************************/
335 
336 #ifndef _GNU_EFI
337 int ACPI_SYSTEM_XFACE
338 main (
339     int                     argc,
340     char                    *argv[])
341 #else
342 int ACPI_SYSTEM_XFACE
343 acpi_main (
344     int                     argc,
345     char                    *argv[])
346 #endif
347 {
348     int                     Status = 0;
349     AP_DUMP_ACTION          *Action;
350     UINT32                  FileSize;
351     UINT32                  i;
352 
353 
354     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
355     AcpiOsInitialize ();
356     Gbl_OutputFile = ACPI_FILE_OUT;
357     AcpiGbl_IntegerByteWidth = 8;
358 
359     /* Process command line options */
360 
361     Status = ApDoOptions (argc, argv);
362     if (Status > 0)
363     {
364         return (0);
365     }
366     if (Status < 0)
367     {
368         return (Status);
369     }
370 
371     /* Get/dump ACPI table(s) as requested */
372 
373     for (i = 0; i < CurrentAction; i++)
374     {
375         Action = &ActionTable[i];
376         switch (Action->ToBeDone)
377         {
378         case AP_DUMP_ALL_TABLES:
379 
380             Status = ApDumpAllTables ();
381             break;
382 
383         case AP_DUMP_TABLE_BY_ADDRESS:
384 
385             Status = ApDumpTableByAddress (Action->Argument);
386             break;
387 
388         case AP_DUMP_TABLE_BY_NAME:
389 
390             Status = ApDumpTableByName (Action->Argument);
391             break;
392 
393         case AP_DUMP_TABLE_BY_FILE:
394 
395             Status = ApDumpTableFromFile (Action->Argument);
396             break;
397 
398         default:
399 
400             fprintf (stderr, "Internal error, invalid action: 0x%X\n",
401                 Action->ToBeDone);
402             return (-1);
403         }
404 
405         if (Status)
406         {
407             return (Status);
408         }
409     }
410 
411     if (Gbl_OutputFilename)
412     {
413         if (Gbl_VerboseMode)
414         {
415             /* Summary for the output file */
416 
417             FileSize = CmGetFileSize (Gbl_OutputFile);
418             fprintf (stderr, "Output file %s contains 0x%X (%u) bytes\n\n",
419                 Gbl_OutputFilename, FileSize, FileSize);
420         }
421 
422         fclose (Gbl_OutputFile);
423     }
424 
425     return (Status);
426 }
427