exnames.c 11 KB

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