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