apmain.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /******************************************************************************
  2. *
  3. * Module Name: apmain - Main module for the acpidump utility
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #define _DECLARE_GLOBALS
  43. #include "acpidump.h"
  44. /*
  45. * acpidump - A portable utility for obtaining system ACPI tables and dumping
  46. * them in an ASCII hex format suitable for binary extraction via acpixtract.
  47. *
  48. * Obtaining the system ACPI tables is an OS-specific operation.
  49. *
  50. * This utility can be ported to any host operating system by providing a
  51. * module containing system-specific versions of these interfaces:
  52. *
  53. * acpi_os_get_table_by_address
  54. * acpi_os_get_table_by_index
  55. * acpi_os_get_table_by_name
  56. *
  57. * See the ACPICA Reference Guide for the exact definitions of these
  58. * interfaces. Also, see these ACPICA source code modules for example
  59. * implementations:
  60. *
  61. * source/os_specific/service_layers/oswintbl.c
  62. * source/os_specific/service_layers/oslinuxtbl.c
  63. */
  64. /* Local prototypes */
  65. static void ap_display_usage(void);
  66. static int ap_do_options(int argc, char **argv);
  67. static int ap_insert_action(char *argument, u32 to_be_done);
  68. /* Table for deferred actions from command line options */
  69. struct ap_dump_action action_table[AP_MAX_ACTIONS];
  70. u32 current_action = 0;
  71. #define AP_UTILITY_NAME "ACPI Binary Table Dump Utility"
  72. #define AP_SUPPORTED_OPTIONS "?a:bc:f:hn:o:r:svxz"
  73. /******************************************************************************
  74. *
  75. * FUNCTION: ap_display_usage
  76. *
  77. * DESCRIPTION: Usage message for the acpi_dump utility
  78. *
  79. ******************************************************************************/
  80. static void ap_display_usage(void)
  81. {
  82. ACPI_USAGE_HEADER("acpidump [options]");
  83. ACPI_OPTION("-b", "Dump tables to binary files");
  84. ACPI_OPTION("-h -?", "This help message");
  85. ACPI_OPTION("-o <File>", "Redirect output to file");
  86. ACPI_OPTION("-r <Address>", "Dump tables from specified RSDP");
  87. ACPI_OPTION("-s", "Print table summaries only");
  88. ACPI_OPTION("-v", "Display version information");
  89. ACPI_OPTION("-z", "Verbose mode");
  90. ACPI_USAGE_TEXT("\nTable Options:\n");
  91. ACPI_OPTION("-a <Address>", "Get table via a physical address");
  92. ACPI_OPTION("-c <on|off>", "Turning on/off customized table dumping");
  93. ACPI_OPTION("-f <BinaryFile>", "Get table via a binary file");
  94. ACPI_OPTION("-n <Signature>", "Get table via a name/signature");
  95. ACPI_OPTION("-x", "Do not use but dump XSDT");
  96. ACPI_OPTION("-x -x", "Do not use or dump XSDT");
  97. ACPI_USAGE_TEXT("\n"
  98. "Invocation without parameters dumps all available tables\n"
  99. "Multiple mixed instances of -a, -f, and -n are supported\n\n");
  100. }
  101. /******************************************************************************
  102. *
  103. * FUNCTION: ap_insert_action
  104. *
  105. * PARAMETERS: argument - Pointer to the argument for this action
  106. * to_be_done - What to do to process this action
  107. *
  108. * RETURN: Status
  109. *
  110. * DESCRIPTION: Add an action item to the action table
  111. *
  112. ******************************************************************************/
  113. static int ap_insert_action(char *argument, u32 to_be_done)
  114. {
  115. /* Insert action and check for table overflow */
  116. action_table[current_action].argument = argument;
  117. action_table[current_action].to_be_done = to_be_done;
  118. current_action++;
  119. if (current_action > AP_MAX_ACTIONS) {
  120. fprintf(stderr, "Too many table options (max %u)\n",
  121. AP_MAX_ACTIONS);
  122. return (-1);
  123. }
  124. return (0);
  125. }
  126. /******************************************************************************
  127. *
  128. * FUNCTION: ap_do_options
  129. *
  130. * PARAMETERS: argc/argv - Standard argc/argv
  131. *
  132. * RETURN: Status
  133. *
  134. * DESCRIPTION: Command line option processing. The main actions for getting
  135. * and dumping tables are deferred via the action table.
  136. *
  137. *****************************************************************************/
  138. static int ap_do_options(int argc, char **argv)
  139. {
  140. int j;
  141. acpi_status status;
  142. /* Command line options */
  143. while ((j =
  144. acpi_getopt(argc, argv, AP_SUPPORTED_OPTIONS)) != ACPI_OPT_END)
  145. switch (j) {
  146. /*
  147. * Global options
  148. */
  149. case 'b': /* Dump all input tables to binary files */
  150. gbl_binary_mode = TRUE;
  151. continue;
  152. case 'c': /* Dump customized tables */
  153. if (!strcmp(acpi_gbl_optarg, "on")) {
  154. gbl_dump_customized_tables = TRUE;
  155. } else if (!strcmp(acpi_gbl_optarg, "off")) {
  156. gbl_dump_customized_tables = FALSE;
  157. } else {
  158. fprintf(stderr,
  159. "%s: Cannot handle this switch, please use on|off\n",
  160. acpi_gbl_optarg);
  161. return (-1);
  162. }
  163. continue;
  164. case 'h':
  165. case '?':
  166. ap_display_usage();
  167. return (1);
  168. case 'o': /* Redirect output to a single file */
  169. if (ap_open_output_file(acpi_gbl_optarg)) {
  170. return (-1);
  171. }
  172. continue;
  173. case 'r': /* Dump tables from specified RSDP */
  174. status =
  175. acpi_ut_strtoul64(acpi_gbl_optarg,
  176. ACPI_STRTOUL_64BIT,
  177. &gbl_rsdp_base);
  178. if (ACPI_FAILURE(status)) {
  179. fprintf(stderr,
  180. "%s: Could not convert to a physical address\n",
  181. acpi_gbl_optarg);
  182. return (-1);
  183. }
  184. continue;
  185. case 's': /* Print table summaries only */
  186. gbl_summary_mode = TRUE;
  187. continue;
  188. case 'x': /* Do not use XSDT */
  189. if (!acpi_gbl_do_not_use_xsdt) {
  190. acpi_gbl_do_not_use_xsdt = TRUE;
  191. } else {
  192. gbl_do_not_dump_xsdt = TRUE;
  193. }
  194. continue;
  195. case 'v': /* Revision/version */
  196. acpi_os_printf(ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
  197. return (1);
  198. case 'z': /* Verbose mode */
  199. gbl_verbose_mode = TRUE;
  200. fprintf(stderr, ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
  201. continue;
  202. /*
  203. * Table options
  204. */
  205. case 'a': /* Get table by physical address */
  206. if (ap_insert_action
  207. (acpi_gbl_optarg, AP_DUMP_TABLE_BY_ADDRESS)) {
  208. return (-1);
  209. }
  210. break;
  211. case 'f': /* Get table from a file */
  212. if (ap_insert_action
  213. (acpi_gbl_optarg, AP_DUMP_TABLE_BY_FILE)) {
  214. return (-1);
  215. }
  216. break;
  217. case 'n': /* Get table by input name (signature) */
  218. if (ap_insert_action
  219. (acpi_gbl_optarg, AP_DUMP_TABLE_BY_NAME)) {
  220. return (-1);
  221. }
  222. break;
  223. default:
  224. ap_display_usage();
  225. return (-1);
  226. }
  227. /* If there are no actions, this means "get/dump all tables" */
  228. if (current_action == 0) {
  229. if (ap_insert_action(NULL, AP_DUMP_ALL_TABLES)) {
  230. return (-1);
  231. }
  232. }
  233. return (0);
  234. }
  235. /******************************************************************************
  236. *
  237. * FUNCTION: main
  238. *
  239. * PARAMETERS: argc/argv - Standard argc/argv
  240. *
  241. * RETURN: Status
  242. *
  243. * DESCRIPTION: C main function for acpidump utility
  244. *
  245. ******************************************************************************/
  246. #if !defined(_GNU_EFI) && !defined(_EDK2_EFI)
  247. int ACPI_SYSTEM_XFACE main(int argc, char *argv[])
  248. #else
  249. int ACPI_SYSTEM_XFACE acpi_main(int argc, char *argv[])
  250. #endif
  251. {
  252. int status = 0;
  253. struct ap_dump_action *action;
  254. u32 file_size;
  255. u32 i;
  256. ACPI_DEBUG_INITIALIZE(); /* For debug version only */
  257. acpi_os_initialize();
  258. gbl_output_file = ACPI_FILE_OUT;
  259. acpi_gbl_integer_byte_width = 8;
  260. /* Process command line options */
  261. status = ap_do_options(argc, argv);
  262. if (status > 0) {
  263. return (0);
  264. }
  265. if (status < 0) {
  266. return (status);
  267. }
  268. /* Get/dump ACPI table(s) as requested */
  269. for (i = 0; i < current_action; i++) {
  270. action = &action_table[i];
  271. switch (action->to_be_done) {
  272. case AP_DUMP_ALL_TABLES:
  273. status = ap_dump_all_tables();
  274. break;
  275. case AP_DUMP_TABLE_BY_ADDRESS:
  276. status = ap_dump_table_by_address(action->argument);
  277. break;
  278. case AP_DUMP_TABLE_BY_NAME:
  279. status = ap_dump_table_by_name(action->argument);
  280. break;
  281. case AP_DUMP_TABLE_BY_FILE:
  282. status = ap_dump_table_from_file(action->argument);
  283. break;
  284. default:
  285. fprintf(stderr,
  286. "Internal error, invalid action: 0x%X\n",
  287. action->to_be_done);
  288. return (-1);
  289. }
  290. if (status) {
  291. return (status);
  292. }
  293. }
  294. if (gbl_output_filename) {
  295. if (gbl_verbose_mode) {
  296. /* Summary for the output file */
  297. file_size = cm_get_file_size(gbl_output_file);
  298. fprintf(stderr,
  299. "Output file %s contains 0x%X (%u) bytes\n\n",
  300. gbl_output_filename, file_size, file_size);
  301. }
  302. fclose(gbl_output_file);
  303. }
  304. return (status);
  305. }