2a06a864c093a8e0b553395914c2d81e2c728cad
[platform/kernel/linux-rpi.git] / tools / power / acpi / tools / acpidump / apmain.c
1 /******************************************************************************
2  *
3  * Module Name: apmain - Main module for the acpidump 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 #define _DECLARE_GLOBALS
45 #include "acpidump.h"
46 #include "acapps.h"
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  *      acpi_os_get_table_by_address
58  *      acpi_os_get_table_by_index
59  *      acpi_os_get_table_by_name
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 /* Local prototypes */
70
71 static void ap_display_usage(void);
72
73 static int ap_do_options(int argc, char **argv);
74
75 static void ap_insert_action(char *argument, u32 to_be_done);
76
77 /* Table for deferred actions from command line options */
78
79 struct ap_dump_action action_table[AP_MAX_ACTIONS];
80 u32 current_action = 0;
81
82 #define AP_UTILITY_NAME             "ACPI Binary Table Dump Utility"
83 #define AP_SUPPORTED_OPTIONS        "?a:bcf:hn:o:r:svxz"
84
85 /******************************************************************************
86  *
87  * FUNCTION:    ap_display_usage
88  *
89  * DESCRIPTION: Usage message for the acpi_dump utility
90  *
91  ******************************************************************************/
92
93 static void ap_display_usage(void)
94 {
95
96         ACPI_USAGE_HEADER("acpidump [options]");
97
98         ACPI_OPTION("-b", "Dump tables to binary files");
99         ACPI_OPTION("-c", "Dump customized tables");
100         ACPI_OPTION("-h -?", "This help message");
101         ACPI_OPTION("-o <File>", "Redirect output to file");
102         ACPI_OPTION("-r <Address>", "Dump tables from specified RSDP");
103         ACPI_OPTION("-s", "Print table summaries only");
104         ACPI_OPTION("-v", "Display version information");
105         ACPI_OPTION("-z", "Verbose mode");
106
107         ACPI_USAGE_TEXT("\nTable Options:\n");
108
109         ACPI_OPTION("-a <Address>", "Get table via a physical address");
110         ACPI_OPTION("-f <BinaryFile>", "Get table via a binary file");
111         ACPI_OPTION("-n <Signature>", "Get table via a name/signature");
112         ACPI_OPTION("-x", "Do not use but dump XSDT");
113         ACPI_OPTION("-x -x", "Do not use or dump XSDT");
114
115         ACPI_USAGE_TEXT("\n"
116                         "Invocation without parameters dumps all available tables\n"
117                         "Multiple mixed instances of -a, -f, and -n are supported\n\n");
118 }
119
120 /******************************************************************************
121  *
122  * FUNCTION:    ap_insert_action
123  *
124  * PARAMETERS:  argument            - Pointer to the argument for this action
125  *              to_be_done          - What to do to process this action
126  *
127  * RETURN:      None. Exits program if action table becomes full.
128  *
129  * DESCRIPTION: Add an action item to the action table
130  *
131  ******************************************************************************/
132
133 static void ap_insert_action(char *argument, u32 to_be_done)
134 {
135
136         /* Insert action and check for table overflow */
137
138         action_table[current_action].argument = argument;
139         action_table[current_action].to_be_done = to_be_done;
140
141         current_action++;
142         if (current_action > AP_MAX_ACTIONS) {
143                 fprintf(stderr, "Too many table options (max %u)\n",
144                         AP_MAX_ACTIONS);
145                 exit(-1);
146         }
147 }
148
149 /******************************************************************************
150  *
151  * FUNCTION:    ap_do_options
152  *
153  * PARAMETERS:  argc/argv           - Standard argc/argv
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Command line option processing. The main actions for getting
158  *              and dumping tables are deferred via the action table.
159  *
160  *****************************************************************************/
161
162 static int ap_do_options(int argc, char **argv)
163 {
164         int j;
165         acpi_status status;
166
167         /* Command line options */
168
169         while ((j = acpi_getopt(argc, argv, AP_SUPPORTED_OPTIONS)) != EOF)
170                 switch (j) {
171                         /*
172                          * Global options
173                          */
174                 case 'b':       /* Dump all input tables to binary files */
175
176                         gbl_binary_mode = TRUE;
177                         continue;
178
179                 case 'c':       /* Dump customized tables */
180
181                         gbl_dump_customized_tables = TRUE;
182                         continue;
183
184                 case 'h':
185                 case '?':
186
187                         ap_display_usage();
188                         exit(0);
189
190                 case 'o':       /* Redirect output to a single file */
191
192                         if (ap_open_output_file(acpi_gbl_optarg)) {
193                                 exit(-1);
194                         }
195                         continue;
196
197                 case 'r':       /* Dump tables from specified RSDP */
198
199                         status =
200                             acpi_ut_strtoul64(acpi_gbl_optarg, 0,
201                                               &gbl_rsdp_base);
202                         if (ACPI_FAILURE(status)) {
203                                 fprintf(stderr,
204                                         "%s: Could not convert to a physical address\n",
205                                         acpi_gbl_optarg);
206                                 exit(-1);
207                         }
208                         continue;
209
210                 case 's':       /* Print table summaries only */
211
212                         gbl_summary_mode = TRUE;
213                         continue;
214
215                 case 'x':       /* Do not use XSDT */
216
217                         if (!acpi_gbl_do_not_use_xsdt) {
218                                 acpi_gbl_do_not_use_xsdt = TRUE;
219                         } else {
220                                 gbl_do_not_dump_xsdt = TRUE;
221                         }
222                         continue;
223
224                 case 'v':       /* Revision/version */
225
226                         printf(ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
227                         exit(0);
228
229                 case 'z':       /* Verbose mode */
230
231                         gbl_verbose_mode = TRUE;
232                         fprintf(stderr, ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
233                         continue;
234
235                         /*
236                          * Table options
237                          */
238                 case 'a':       /* Get table by physical address */
239
240                         ap_insert_action(acpi_gbl_optarg,
241                                          AP_DUMP_TABLE_BY_ADDRESS);
242                         break;
243
244                 case 'f':       /* Get table from a file */
245
246                         ap_insert_action(acpi_gbl_optarg,
247                                          AP_DUMP_TABLE_BY_FILE);
248                         break;
249
250                 case 'n':       /* Get table by input name (signature) */
251
252                         ap_insert_action(acpi_gbl_optarg,
253                                          AP_DUMP_TABLE_BY_NAME);
254                         break;
255
256                 default:
257
258                         ap_display_usage();
259                         exit(-1);
260                 }
261
262         /* If there are no actions, this means "get/dump all tables" */
263
264         if (current_action == 0) {
265                 ap_insert_action(NULL, AP_DUMP_ALL_TABLES);
266         }
267
268         return (0);
269 }
270
271 /******************************************************************************
272  *
273  * FUNCTION:    main
274  *
275  * PARAMETERS:  argc/argv           - Standard argc/argv
276  *
277  * RETURN:      Status
278  *
279  * DESCRIPTION: C main function for acpidump utility
280  *
281  ******************************************************************************/
282
283 int ACPI_SYSTEM_XFACE main(int argc, char *argv[])
284 {
285         int status = 0;
286         struct ap_dump_action *action;
287         u32 file_size;
288         u32 i;
289
290         ACPI_DEBUG_INITIALIZE();        /* For debug version only */
291         acpi_os_initialize();
292
293         /* Process command line options */
294
295         if (ap_do_options(argc, argv)) {
296                 return (-1);
297         }
298
299         /* Get/dump ACPI table(s) as requested */
300
301         for (i = 0; i < current_action; i++) {
302                 action = &action_table[i];
303                 switch (action->to_be_done) {
304                 case AP_DUMP_ALL_TABLES:
305
306                         status = ap_dump_all_tables();
307                         break;
308
309                 case AP_DUMP_TABLE_BY_ADDRESS:
310
311                         status = ap_dump_table_by_address(action->argument);
312                         break;
313
314                 case AP_DUMP_TABLE_BY_NAME:
315
316                         status = ap_dump_table_by_name(action->argument);
317                         break;
318
319                 case AP_DUMP_TABLE_BY_FILE:
320
321                         status = ap_dump_table_from_file(action->argument);
322                         break;
323
324                 default:
325
326                         fprintf(stderr,
327                                 "Internal error, invalid action: 0x%X\n",
328                                 action->to_be_done);
329                         return (-1);
330                 }
331
332                 if (status) {
333                         return (status);
334                 }
335         }
336
337         if (gbl_output_file) {
338                 if (gbl_verbose_mode) {
339
340                         /* Summary for the output file */
341
342                         file_size = cm_get_file_size(gbl_output_file);
343                         fprintf(stderr,
344                                 "Output file %s contains 0x%X (%u) bytes\n\n",
345                                 gbl_output_filename, file_size, file_size);
346                 }
347
348                 fclose(gbl_output_file);
349         }
350
351         return (status);
352 }