exnames.c 11 KB

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