dbutils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*******************************************************************************
  2. *
  3. * Module Name: dbutils - AML debugger utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "acdebug.h"
  46. #define _COMPONENT ACPI_CA_DEBUGGER
  47. ACPI_MODULE_NAME("dbutils")
  48. /* Local prototypes */
  49. #ifdef ACPI_OBSOLETE_FUNCTIONS
  50. acpi_status acpi_db_second_pass_parse(union acpi_parse_object *root);
  51. void acpi_db_dump_buffer(u32 address);
  52. #endif
  53. static char *gbl_hex_to_ascii = "0123456789ABCDEF";
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_db_match_argument
  57. *
  58. * PARAMETERS: user_argument - User command line
  59. * arguments - Array of commands to match against
  60. *
  61. * RETURN: Index into command array or ACPI_TYPE_NOT_FOUND if not found
  62. *
  63. * DESCRIPTION: Search command array for a command match
  64. *
  65. ******************************************************************************/
  66. acpi_object_type
  67. acpi_db_match_argument(char *user_argument,
  68. struct acpi_db_argument_info *arguments)
  69. {
  70. u32 i;
  71. if (!user_argument || user_argument[0] == 0) {
  72. return (ACPI_TYPE_NOT_FOUND);
  73. }
  74. for (i = 0; arguments[i].name; i++) {
  75. if (strstr(arguments[i].name, user_argument) ==
  76. arguments[i].name) {
  77. return (i);
  78. }
  79. }
  80. /* Argument not recognized */
  81. return (ACPI_TYPE_NOT_FOUND);
  82. }
  83. /*******************************************************************************
  84. *
  85. * FUNCTION: acpi_db_set_output_destination
  86. *
  87. * PARAMETERS: output_flags - Current flags word
  88. *
  89. * RETURN: None
  90. *
  91. * DESCRIPTION: Set the current destination for debugger output. Also sets
  92. * the debug output level accordingly.
  93. *
  94. ******************************************************************************/
  95. void acpi_db_set_output_destination(u32 output_flags)
  96. {
  97. acpi_gbl_db_output_flags = (u8)output_flags;
  98. if ((output_flags & ACPI_DB_REDIRECTABLE_OUTPUT) &&
  99. acpi_gbl_db_output_to_file) {
  100. acpi_dbg_level = acpi_gbl_db_debug_level;
  101. } else {
  102. acpi_dbg_level = acpi_gbl_db_console_debug_level;
  103. }
  104. }
  105. /*******************************************************************************
  106. *
  107. * FUNCTION: acpi_db_dump_external_object
  108. *
  109. * PARAMETERS: obj_desc - External ACPI object to dump
  110. * level - Nesting level.
  111. *
  112. * RETURN: None
  113. *
  114. * DESCRIPTION: Dump the contents of an ACPI external object
  115. *
  116. ******************************************************************************/
  117. void acpi_db_dump_external_object(union acpi_object *obj_desc, u32 level)
  118. {
  119. u32 i;
  120. if (!obj_desc) {
  121. acpi_os_printf("[Null Object]\n");
  122. return;
  123. }
  124. for (i = 0; i < level; i++) {
  125. acpi_os_printf(" ");
  126. }
  127. switch (obj_desc->type) {
  128. case ACPI_TYPE_ANY:
  129. acpi_os_printf("[Null Object] (Type=0)\n");
  130. break;
  131. case ACPI_TYPE_INTEGER:
  132. acpi_os_printf("[Integer] = %8.8X%8.8X\n",
  133. ACPI_FORMAT_UINT64(obj_desc->integer.value));
  134. break;
  135. case ACPI_TYPE_STRING:
  136. acpi_os_printf("[String] Length %.2X = ",
  137. obj_desc->string.length);
  138. acpi_ut_print_string(obj_desc->string.pointer, ACPI_UINT8_MAX);
  139. acpi_os_printf("\n");
  140. break;
  141. case ACPI_TYPE_BUFFER:
  142. acpi_os_printf("[Buffer] Length %.2X = ",
  143. obj_desc->buffer.length);
  144. if (obj_desc->buffer.length) {
  145. if (obj_desc->buffer.length > 16) {
  146. acpi_os_printf("\n");
  147. }
  148. acpi_ut_debug_dump_buffer(ACPI_CAST_PTR
  149. (u8,
  150. obj_desc->buffer.pointer),
  151. obj_desc->buffer.length,
  152. DB_BYTE_DISPLAY, _COMPONENT);
  153. } else {
  154. acpi_os_printf("\n");
  155. }
  156. break;
  157. case ACPI_TYPE_PACKAGE:
  158. acpi_os_printf("[Package] Contains %u Elements:\n",
  159. obj_desc->package.count);
  160. for (i = 0; i < obj_desc->package.count; i++) {
  161. acpi_db_dump_external_object(&obj_desc->package.
  162. elements[i], level + 1);
  163. }
  164. break;
  165. case ACPI_TYPE_LOCAL_REFERENCE:
  166. acpi_os_printf("[Object Reference] = ");
  167. acpi_db_display_internal_object(obj_desc->reference.handle,
  168. NULL);
  169. break;
  170. case ACPI_TYPE_PROCESSOR:
  171. acpi_os_printf("[Processor]\n");
  172. break;
  173. case ACPI_TYPE_POWER:
  174. acpi_os_printf("[Power Resource]\n");
  175. break;
  176. default:
  177. acpi_os_printf("[Unknown Type] %X\n", obj_desc->type);
  178. break;
  179. }
  180. }
  181. /*******************************************************************************
  182. *
  183. * FUNCTION: acpi_db_prep_namestring
  184. *
  185. * PARAMETERS: name - String to prepare
  186. *
  187. * RETURN: None
  188. *
  189. * DESCRIPTION: Translate all forward slashes and dots to backslashes.
  190. *
  191. ******************************************************************************/
  192. void acpi_db_prep_namestring(char *name)
  193. {
  194. if (!name) {
  195. return;
  196. }
  197. acpi_ut_strupr(name);
  198. /* Convert a leading forward slash to a backslash */
  199. if (*name == '/') {
  200. *name = '\\';
  201. }
  202. /* Ignore a leading backslash, this is the root prefix */
  203. if (ACPI_IS_ROOT_PREFIX(*name)) {
  204. name++;
  205. }
  206. /* Convert all slash path separators to dots */
  207. while (*name) {
  208. if ((*name == '/') || (*name == '\\')) {
  209. *name = '.';
  210. }
  211. name++;
  212. }
  213. }
  214. /*******************************************************************************
  215. *
  216. * FUNCTION: acpi_db_local_ns_lookup
  217. *
  218. * PARAMETERS: name - Name to lookup
  219. *
  220. * RETURN: Pointer to a namespace node, null on failure
  221. *
  222. * DESCRIPTION: Lookup a name in the ACPI namespace
  223. *
  224. * Note: Currently begins search from the root. Could be enhanced to use
  225. * the current prefix (scope) node as the search beginning point.
  226. *
  227. ******************************************************************************/
  228. struct acpi_namespace_node *acpi_db_local_ns_lookup(char *name)
  229. {
  230. char *internal_path;
  231. acpi_status status;
  232. struct acpi_namespace_node *node = NULL;
  233. acpi_db_prep_namestring(name);
  234. /* Build an internal namestring */
  235. status = acpi_ns_internalize_name(name, &internal_path);
  236. if (ACPI_FAILURE(status)) {
  237. acpi_os_printf("Invalid namestring: %s\n", name);
  238. return (NULL);
  239. }
  240. /*
  241. * Lookup the name.
  242. * (Uses root node as the search starting point)
  243. */
  244. status = acpi_ns_lookup(NULL, internal_path, ACPI_TYPE_ANY,
  245. ACPI_IMODE_EXECUTE,
  246. ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE,
  247. NULL, &node);
  248. if (ACPI_FAILURE(status)) {
  249. acpi_os_printf("Could not locate name: %s, %s\n",
  250. name, acpi_format_exception(status));
  251. }
  252. ACPI_FREE(internal_path);
  253. return (node);
  254. }
  255. /*******************************************************************************
  256. *
  257. * FUNCTION: acpi_db_uint32_to_hex_string
  258. *
  259. * PARAMETERS: value - The value to be converted to string
  260. * buffer - Buffer for result (not less than 11 bytes)
  261. *
  262. * RETURN: None
  263. *
  264. * DESCRIPTION: Convert the unsigned 32-bit value to the hexadecimal image
  265. *
  266. * NOTE: It is the caller's responsibility to ensure that the length of buffer
  267. * is sufficient.
  268. *
  269. ******************************************************************************/
  270. void acpi_db_uint32_to_hex_string(u32 value, char *buffer)
  271. {
  272. int i;
  273. if (value == 0) {
  274. strcpy(buffer, "0");
  275. return;
  276. }
  277. buffer[8] = '\0';
  278. for (i = 7; i >= 0; i--) {
  279. buffer[i] = gbl_hex_to_ascii[value & 0x0F];
  280. value = value >> 4;
  281. }
  282. }
  283. #ifdef ACPI_OBSOLETE_FUNCTIONS
  284. /*******************************************************************************
  285. *
  286. * FUNCTION: acpi_db_second_pass_parse
  287. *
  288. * PARAMETERS: root - Root of the parse tree
  289. *
  290. * RETURN: Status
  291. *
  292. * DESCRIPTION: Second pass parse of the ACPI tables. We need to wait until
  293. * second pass to parse the control methods
  294. *
  295. ******************************************************************************/
  296. acpi_status acpi_db_second_pass_parse(union acpi_parse_object *root)
  297. {
  298. union acpi_parse_object *op = root;
  299. union acpi_parse_object *method;
  300. union acpi_parse_object *search_op;
  301. union acpi_parse_object *start_op;
  302. acpi_status status = AE_OK;
  303. u32 base_aml_offset;
  304. struct acpi_walk_state *walk_state;
  305. ACPI_FUNCTION_ENTRY();
  306. acpi_os_printf("Pass two parse ....\n");
  307. while (op) {
  308. if (op->common.aml_opcode == AML_METHOD_OP) {
  309. method = op;
  310. /* Create a new walk state for the parse */
  311. walk_state =
  312. acpi_ds_create_walk_state(0, NULL, NULL, NULL);
  313. if (!walk_state) {
  314. return (AE_NO_MEMORY);
  315. }
  316. /* Init the Walk State */
  317. walk_state->parser_state.aml =
  318. walk_state->parser_state.aml_start =
  319. method->named.data;
  320. walk_state->parser_state.aml_end =
  321. walk_state->parser_state.pkg_end =
  322. method->named.data + method->named.length;
  323. walk_state->parser_state.start_scope = op;
  324. walk_state->descending_callback =
  325. acpi_ds_load1_begin_op;
  326. walk_state->ascending_callback = acpi_ds_load1_end_op;
  327. /* Perform the AML parse */
  328. status = acpi_ps_parse_aml(walk_state);
  329. base_aml_offset =
  330. (method->common.value.arg)->common.aml_offset + 1;
  331. start_op = (method->common.value.arg)->common.next;
  332. search_op = start_op;
  333. while (search_op) {
  334. search_op->common.aml_offset += base_aml_offset;
  335. search_op =
  336. acpi_ps_get_depth_next(start_op, search_op);
  337. }
  338. }
  339. if (op->common.aml_opcode == AML_REGION_OP) {
  340. /* TBD: [Investigate] this isn't quite the right thing to do! */
  341. /*
  342. *
  343. * Method = (ACPI_DEFERRED_OP *) Op;
  344. * Status = acpi_ps_parse_aml (Op, Method->Body, Method->body_length);
  345. */
  346. }
  347. if (ACPI_FAILURE(status)) {
  348. break;
  349. }
  350. op = acpi_ps_get_depth_next(root, op);
  351. }
  352. return (status);
  353. }
  354. /*******************************************************************************
  355. *
  356. * FUNCTION: acpi_db_dump_buffer
  357. *
  358. * PARAMETERS: address - Pointer to the buffer
  359. *
  360. * RETURN: None
  361. *
  362. * DESCRIPTION: Print a portion of a buffer
  363. *
  364. ******************************************************************************/
  365. void acpi_db_dump_buffer(u32 address)
  366. {
  367. acpi_os_printf("\nLocation %X:\n", address);
  368. acpi_dbg_level |= ACPI_LV_TABLES;
  369. acpi_ut_debug_dump_buffer(ACPI_TO_POINTER(address), 64, DB_BYTE_DISPLAY,
  370. ACPI_UINT32_MAX);
  371. }
  372. #endif