dbutils.c 10 KB

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