exnames.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /******************************************************************************
  2. *
  3. * Module Name: exnames - interpreter/scanner name load/execute
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acinterp.h>
  44. #include <acpi/amlcode.h>
  45. #define _COMPONENT ACPI_EXECUTER
  46. ACPI_MODULE_NAME ("exnames")
  47. /* AML Package Length encodings */
  48. #define ACPI_AML_PACKAGE_TYPE1 0x40
  49. #define ACPI_AML_PACKAGE_TYPE2 0x4000
  50. #define ACPI_AML_PACKAGE_TYPE3 0x400000
  51. #define ACPI_AML_PACKAGE_TYPE4 0x40000000
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ex_allocate_name_string
  55. *
  56. * PARAMETERS: prefix_count - Count of parent levels. Special cases:
  57. * (-1) = root, 0 = none
  58. * num_name_segs - count of 4-character name segments
  59. *
  60. * RETURN: A pointer to the allocated string segment. This segment must
  61. * be deleted by the caller.
  62. *
  63. * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
  64. * string is long enough, and set up prefix if any.
  65. *
  66. ******************************************************************************/
  67. char *
  68. acpi_ex_allocate_name_string (
  69. u32 prefix_count,
  70. u32 num_name_segs)
  71. {
  72. char *temp_ptr;
  73. char *name_string;
  74. u32 size_needed;
  75. ACPI_FUNCTION_TRACE ("ex_allocate_name_string");
  76. /*
  77. * Allow room for all \ and ^ prefixes, all segments, and a multi_name_prefix.
  78. * Also, one byte for the null terminator.
  79. * This may actually be somewhat longer than needed.
  80. */
  81. if (prefix_count == ACPI_UINT32_MAX) {
  82. /* Special case for root */
  83. size_needed = 1 + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
  84. }
  85. else {
  86. size_needed = prefix_count + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
  87. }
  88. /*
  89. * Allocate a buffer for the name.
  90. * This buffer must be deleted by the caller!
  91. */
  92. name_string = ACPI_MEM_ALLOCATE (size_needed);
  93. if (!name_string) {
  94. ACPI_REPORT_ERROR (("ex_allocate_name_string: Could not allocate size %d\n", size_needed));
  95. return_PTR (NULL);
  96. }
  97. temp_ptr = name_string;
  98. /* Set up Root or Parent prefixes if needed */
  99. if (prefix_count == ACPI_UINT32_MAX) {
  100. *temp_ptr++ = AML_ROOT_PREFIX;
  101. }
  102. else {
  103. while (prefix_count--) {
  104. *temp_ptr++ = AML_PARENT_PREFIX;
  105. }
  106. }
  107. /* Set up Dual or Multi prefixes if needed */
  108. if (num_name_segs > 2) {
  109. /* Set up multi prefixes */
  110. *temp_ptr++ = AML_MULTI_NAME_PREFIX_OP;
  111. *temp_ptr++ = (char) num_name_segs;
  112. }
  113. else if (2 == num_name_segs) {
  114. /* Set up dual prefixes */
  115. *temp_ptr++ = AML_DUAL_NAME_PREFIX;
  116. }
  117. /*
  118. * Terminate string following prefixes. acpi_ex_name_segment() will
  119. * append the segment(s)
  120. */
  121. *temp_ptr = 0;
  122. return_PTR (name_string);
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_ex_name_segment
  127. *
  128. * PARAMETERS: interpreter_mode - Current running mode (load1/Load2/Exec)
  129. *
  130. * RETURN: Status
  131. *
  132. * DESCRIPTION: Execute a name segment (4 bytes)
  133. *
  134. ******************************************************************************/
  135. acpi_status
  136. acpi_ex_name_segment (
  137. u8 **in_aml_address,
  138. char *name_string)
  139. {
  140. char *aml_address = (void *) *in_aml_address;
  141. acpi_status status = AE_OK;
  142. u32 index;
  143. char char_buf[5];
  144. ACPI_FUNCTION_TRACE ("ex_name_segment");
  145. /*
  146. * If first character is a digit, then we know that we aren't looking at a
  147. * valid name segment
  148. */
  149. char_buf[0] = *aml_address;
  150. if ('0' <= char_buf[0] && char_buf[0] <= '9') {
  151. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "leading digit: %c\n", char_buf[0]));
  152. return_ACPI_STATUS (AE_CTRL_PENDING);
  153. }
  154. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
  155. for (index = 0;
  156. (index < ACPI_NAME_SIZE) && (acpi_ut_valid_acpi_character (*aml_address));
  157. index++) {
  158. char_buf[index] = *aml_address++;
  159. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", char_buf[index]));
  160. }
  161. /* Valid name segment */
  162. if (index == 4) {
  163. /* Found 4 valid characters */
  164. char_buf[4] = '\0';
  165. if (name_string) {
  166. ACPI_STRCAT (name_string, char_buf);
  167. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  168. "Appended to - %s \n", name_string));
  169. }
  170. else {
  171. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  172. "No Name string - %s \n", char_buf));
  173. }
  174. }
  175. else if (index == 0) {
  176. /*
  177. * First character was not a valid name character,
  178. * so we are looking at something other than a name.
  179. */
  180. ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
  181. "Leading character is not alpha: %02Xh (not a name)\n",
  182. char_buf[0]));
  183. status = AE_CTRL_PENDING;
  184. }
  185. else {
  186. /* Segment started with one or more valid characters, but fewer than 4 */
  187. status = AE_AML_BAD_NAME;
  188. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Bad character %02x in name, at %p\n",
  189. *aml_address, aml_address));
  190. }
  191. *in_aml_address = (u8 *) aml_address;
  192. return_ACPI_STATUS (status);
  193. }
  194. /*******************************************************************************
  195. *
  196. * FUNCTION: acpi_ex_get_name_string
  197. *
  198. * PARAMETERS: data_type - Data type to be associated with this name
  199. *
  200. * RETURN: Status
  201. *
  202. * DESCRIPTION: Get a name, including any prefixes.
  203. *
  204. ******************************************************************************/
  205. acpi_status
  206. acpi_ex_get_name_string (
  207. acpi_object_type data_type,
  208. u8 *in_aml_address,
  209. char **out_name_string,
  210. u32 *out_name_length)
  211. {
  212. acpi_status status = AE_OK;
  213. u8 *aml_address = in_aml_address;
  214. char *name_string = NULL;
  215. u32 num_segments;
  216. u32 prefix_count = 0;
  217. u8 has_prefix = FALSE;
  218. ACPI_FUNCTION_TRACE_PTR ("ex_get_name_string", aml_address);
  219. if (ACPI_TYPE_LOCAL_REGION_FIELD == data_type ||
  220. ACPI_TYPE_LOCAL_BANK_FIELD == data_type ||
  221. ACPI_TYPE_LOCAL_INDEX_FIELD == data_type) {
  222. /* Disallow prefixes for types associated with field_unit names */
  223. name_string = acpi_ex_allocate_name_string (0, 1);
  224. if (!name_string) {
  225. status = AE_NO_MEMORY;
  226. }
  227. else {
  228. status = acpi_ex_name_segment (&aml_address, name_string);
  229. }
  230. }
  231. else {
  232. /*
  233. * data_type is not a field name.
  234. * Examine first character of name for root or parent prefix operators
  235. */
  236. switch (*aml_address) {
  237. case AML_ROOT_PREFIX:
  238. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "root_prefix(\\) at %p\n", aml_address));
  239. /*
  240. * Remember that we have a root_prefix --
  241. * see comment in acpi_ex_allocate_name_string()
  242. */
  243. aml_address++;
  244. prefix_count = ACPI_UINT32_MAX;
  245. has_prefix = TRUE;
  246. break;
  247. case AML_PARENT_PREFIX:
  248. /* Increment past possibly multiple parent prefixes */
  249. do {
  250. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "parent_prefix (^) at %p\n", aml_address));
  251. aml_address++;
  252. prefix_count++;
  253. } while (*aml_address == AML_PARENT_PREFIX);
  254. has_prefix = TRUE;
  255. break;
  256. default:
  257. /* Not a prefix character */
  258. break;
  259. }
  260. /* Examine first character of name for name segment prefix operator */
  261. switch (*aml_address) {
  262. case AML_DUAL_NAME_PREFIX:
  263. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "dual_name_prefix at %p\n", aml_address));
  264. aml_address++;
  265. name_string = acpi_ex_allocate_name_string (prefix_count, 2);
  266. if (!name_string) {
  267. status = AE_NO_MEMORY;
  268. break;
  269. }
  270. /* Indicate that we processed a prefix */
  271. has_prefix = TRUE;
  272. status = acpi_ex_name_segment (&aml_address, name_string);
  273. if (ACPI_SUCCESS (status)) {
  274. status = acpi_ex_name_segment (&aml_address, name_string);
  275. }
  276. break;
  277. case AML_MULTI_NAME_PREFIX_OP:
  278. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "multi_name_prefix at %p\n", aml_address));
  279. /* Fetch count of segments remaining in name path */
  280. aml_address++;
  281. num_segments = *aml_address;
  282. name_string = acpi_ex_allocate_name_string (prefix_count, num_segments);
  283. if (!name_string) {
  284. status = AE_NO_MEMORY;
  285. break;
  286. }
  287. /* Indicate that we processed a prefix */
  288. aml_address++;
  289. has_prefix = TRUE;
  290. while (num_segments &&
  291. (status = acpi_ex_name_segment (&aml_address, name_string)) == AE_OK) {
  292. num_segments--;
  293. }
  294. break;
  295. case 0:
  296. /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
  297. if (prefix_count == ACPI_UINT32_MAX) {
  298. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "name_seg is \"\\\" followed by NULL\n"));
  299. }
  300. /* Consume the NULL byte */
  301. aml_address++;
  302. name_string = acpi_ex_allocate_name_string (prefix_count, 0);
  303. if (!name_string) {
  304. status = AE_NO_MEMORY;
  305. break;
  306. }
  307. break;
  308. default:
  309. /* Name segment string */
  310. name_string = acpi_ex_allocate_name_string (prefix_count, 1);
  311. if (!name_string) {
  312. status = AE_NO_MEMORY;
  313. break;
  314. }
  315. status = acpi_ex_name_segment (&aml_address, name_string);
  316. break;
  317. }
  318. }
  319. if (AE_CTRL_PENDING == status && has_prefix) {
  320. /* Ran out of segments after processing a prefix */
  321. ACPI_REPORT_ERROR (
  322. ("ex_do_name: Malformed Name at %p\n", name_string));
  323. status = AE_AML_BAD_NAME;
  324. }
  325. *out_name_string = name_string;
  326. *out_name_length = (u32) (aml_address - in_aml_address);
  327. return_ACPI_STATUS (status);
  328. }