utpredef.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utpredef - support functions for predefined names
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acpredef.h"
  12. #define _COMPONENT ACPI_UTILITIES
  13. ACPI_MODULE_NAME("utpredef")
  14. /*
  15. * Names for the types that can be returned by the predefined objects.
  16. * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
  17. */
  18. static const char *ut_rtype_names[] = {
  19. "/Integer",
  20. "/String",
  21. "/Buffer",
  22. "/Package",
  23. "/Reference",
  24. };
  25. /*******************************************************************************
  26. *
  27. * FUNCTION: acpi_ut_get_next_predefined_method
  28. *
  29. * PARAMETERS: this_name - Entry in the predefined method/name table
  30. *
  31. * RETURN: Pointer to next entry in predefined table.
  32. *
  33. * DESCRIPTION: Get the next entry in the predefine method table. Handles the
  34. * cases where a package info entry follows a method name that
  35. * returns a package.
  36. *
  37. ******************************************************************************/
  38. const union acpi_predefined_info *acpi_ut_get_next_predefined_method(const union
  39. acpi_predefined_info
  40. *this_name)
  41. {
  42. /*
  43. * Skip next entry in the table if this name returns a Package
  44. * (next entry contains the package info)
  45. */
  46. if ((this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) &&
  47. (this_name->info.expected_btypes != ACPI_RTYPE_ALL)) {
  48. this_name++;
  49. }
  50. this_name++;
  51. return (this_name);
  52. }
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_ut_match_predefined_method
  56. *
  57. * PARAMETERS: name - Name to find
  58. *
  59. * RETURN: Pointer to entry in predefined table. NULL indicates not found.
  60. *
  61. * DESCRIPTION: Check an object name against the predefined object list.
  62. *
  63. ******************************************************************************/
  64. const union acpi_predefined_info *acpi_ut_match_predefined_method(char *name)
  65. {
  66. const union acpi_predefined_info *this_name;
  67. /* Quick check for a predefined name, first character must be underscore */
  68. if (name[0] != '_') {
  69. return (NULL);
  70. }
  71. /* Search info table for a predefined method/object name */
  72. this_name = acpi_gbl_predefined_methods;
  73. while (this_name->info.name[0]) {
  74. if (ACPI_COMPARE_NAMESEG(name, this_name->info.name)) {
  75. return (this_name);
  76. }
  77. this_name = acpi_ut_get_next_predefined_method(this_name);
  78. }
  79. return (NULL); /* Not found */
  80. }
  81. /*******************************************************************************
  82. *
  83. * FUNCTION: acpi_ut_get_expected_return_types
  84. *
  85. * PARAMETERS: buffer - Where the formatted string is returned
  86. * expected_Btypes - Bitfield of expected data types
  87. *
  88. * RETURN: Formatted string in Buffer.
  89. *
  90. * DESCRIPTION: Format the expected object types into a printable string.
  91. *
  92. ******************************************************************************/
  93. void acpi_ut_get_expected_return_types(char *buffer, u32 expected_btypes)
  94. {
  95. u32 this_rtype;
  96. u32 i;
  97. u32 j;
  98. if (!expected_btypes) {
  99. strcpy(buffer, "NONE");
  100. return;
  101. }
  102. j = 1;
  103. buffer[0] = 0;
  104. this_rtype = ACPI_RTYPE_INTEGER;
  105. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  106. /* If one of the expected types, concatenate the name of this type */
  107. if (expected_btypes & this_rtype) {
  108. strcat(buffer, &ut_rtype_names[i][j]);
  109. j = 0; /* Use name separator from now on */
  110. }
  111. this_rtype <<= 1; /* Next Rtype */
  112. }
  113. }
  114. /*******************************************************************************
  115. *
  116. * The remaining functions are used by iASL and acpi_help only
  117. *
  118. ******************************************************************************/
  119. #if (defined ACPI_ASL_COMPILER || defined ACPI_HELP_APP)
  120. /* Local prototypes */
  121. static u32 acpi_ut_get_argument_types(char *buffer, u16 argument_types);
  122. /* Types that can be returned externally by a predefined name */
  123. static const char *ut_external_type_names[] = /* Indexed by ACPI_TYPE_* */
  124. {
  125. ", Type_ANY",
  126. ", Integer",
  127. ", String",
  128. ", Buffer",
  129. ", Package"
  130. };
  131. /* Bit widths for resource descriptor predefined names */
  132. static const char *ut_resource_type_names[] = {
  133. "/1",
  134. "/2",
  135. "/3",
  136. "/8",
  137. "/16",
  138. "/32",
  139. "/64",
  140. "/variable",
  141. };
  142. /*******************************************************************************
  143. *
  144. * FUNCTION: acpi_ut_match_resource_name
  145. *
  146. * PARAMETERS: name - Name to find
  147. *
  148. * RETURN: Pointer to entry in the resource table. NULL indicates not
  149. * found.
  150. *
  151. * DESCRIPTION: Check an object name against the predefined resource
  152. * descriptor object list.
  153. *
  154. ******************************************************************************/
  155. const union acpi_predefined_info *acpi_ut_match_resource_name(char *name)
  156. {
  157. const union acpi_predefined_info *this_name;
  158. /*
  159. * Quick check for a predefined name, first character must
  160. * be underscore
  161. */
  162. if (name[0] != '_') {
  163. return (NULL);
  164. }
  165. /* Search info table for a predefined method/object name */
  166. this_name = acpi_gbl_resource_names;
  167. while (this_name->info.name[0]) {
  168. if (ACPI_COMPARE_NAMESEG(name, this_name->info.name)) {
  169. return (this_name);
  170. }
  171. this_name++;
  172. }
  173. return (NULL); /* Not found */
  174. }
  175. /*******************************************************************************
  176. *
  177. * FUNCTION: acpi_ut_display_predefined_method
  178. *
  179. * PARAMETERS: buffer - Scratch buffer for this function
  180. * this_name - Entry in the predefined method/name table
  181. * multi_line - TRUE if output should be on >1 line
  182. *
  183. * RETURN: None
  184. *
  185. * DESCRIPTION: Display information about a predefined method. Number and
  186. * type of the input arguments, and expected type(s) for the
  187. * return value, if any.
  188. *
  189. ******************************************************************************/
  190. void
  191. acpi_ut_display_predefined_method(char *buffer,
  192. const union acpi_predefined_info *this_name,
  193. u8 multi_line)
  194. {
  195. u32 arg_count;
  196. /*
  197. * Get the argument count and the string buffer
  198. * containing all argument types
  199. */
  200. arg_count = acpi_ut_get_argument_types(buffer,
  201. this_name->info.argument_list);
  202. if (multi_line) {
  203. printf(" ");
  204. }
  205. printf("%4.4s Requires %s%u argument%s",
  206. this_name->info.name,
  207. (this_name->info.argument_list & ARG_COUNT_IS_MINIMUM) ?
  208. "(at least) " : "", arg_count, arg_count != 1 ? "s" : "");
  209. /* Display the types for any arguments */
  210. if (arg_count > 0) {
  211. printf(" (%s)", buffer);
  212. }
  213. if (multi_line) {
  214. printf("\n ");
  215. }
  216. /* Get the return value type(s) allowed */
  217. if (this_name->info.expected_btypes) {
  218. acpi_ut_get_expected_return_types(buffer,
  219. this_name->info.
  220. expected_btypes);
  221. printf(" Return value types: %s\n", buffer);
  222. } else {
  223. printf(" No return value\n");
  224. }
  225. }
  226. /*******************************************************************************
  227. *
  228. * FUNCTION: acpi_ut_get_argument_types
  229. *
  230. * PARAMETERS: buffer - Where to return the formatted types
  231. * argument_types - Types field for this method
  232. *
  233. * RETURN: count - the number of arguments required for this method
  234. *
  235. * DESCRIPTION: Format the required data types for this method (Integer,
  236. * String, Buffer, or Package) and return the required argument
  237. * count.
  238. *
  239. ******************************************************************************/
  240. static u32 acpi_ut_get_argument_types(char *buffer, u16 argument_types)
  241. {
  242. u16 this_argument_type;
  243. u16 sub_index;
  244. u16 arg_count;
  245. u32 i;
  246. *buffer = 0;
  247. sub_index = 2;
  248. /* First field in the types list is the count of args to follow */
  249. arg_count = METHOD_GET_ARG_COUNT(argument_types);
  250. if (arg_count > METHOD_PREDEF_ARGS_MAX) {
  251. printf("**** Invalid argument count (%u) "
  252. "in predefined info structure\n", arg_count);
  253. return (arg_count);
  254. }
  255. /* Get each argument from the list, convert to ascii, store to buffer */
  256. for (i = 0; i < arg_count; i++) {
  257. this_argument_type = METHOD_GET_NEXT_TYPE(argument_types);
  258. if (this_argument_type > METHOD_MAX_ARG_TYPE) {
  259. printf("**** Invalid argument type (%u) "
  260. "in predefined info structure\n",
  261. this_argument_type);
  262. return (arg_count);
  263. }
  264. strcat(buffer,
  265. ut_external_type_names[this_argument_type] + sub_index);
  266. sub_index = 0;
  267. }
  268. return (arg_count);
  269. }
  270. /*******************************************************************************
  271. *
  272. * FUNCTION: acpi_ut_get_resource_bit_width
  273. *
  274. * PARAMETERS: buffer - Where the formatted string is returned
  275. * types - Bitfield of expected data types
  276. *
  277. * RETURN: Count of return types. Formatted string in Buffer.
  278. *
  279. * DESCRIPTION: Format the resource bit widths into a printable string.
  280. *
  281. ******************************************************************************/
  282. u32 acpi_ut_get_resource_bit_width(char *buffer, u16 types)
  283. {
  284. u32 i;
  285. u16 sub_index;
  286. u32 found;
  287. *buffer = 0;
  288. sub_index = 1;
  289. found = 0;
  290. for (i = 0; i < NUM_RESOURCE_WIDTHS; i++) {
  291. if (types & 1) {
  292. strcat(buffer, &(ut_resource_type_names[i][sub_index]));
  293. sub_index = 0;
  294. found++;
  295. }
  296. types >>= 1;
  297. }
  298. return (found);
  299. }
  300. #endif