exnames.c 12 KB

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