nsaccess.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsaccess - Top-level functions for accessing ACPI namespace
  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/amlcode.h>
  44. #include <acpi/acnamesp.h>
  45. #include <acpi/acdispat.h>
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME ("nsaccess")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ns_root_initialize
  51. *
  52. * PARAMETERS: None
  53. *
  54. * RETURN: Status
  55. *
  56. * DESCRIPTION: Allocate and initialize the default root named objects
  57. *
  58. * MUTEX: Locks namespace for entire execution
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_ns_root_initialize (
  63. void)
  64. {
  65. acpi_status status;
  66. const struct acpi_predefined_names *init_val = NULL;
  67. struct acpi_namespace_node *new_node;
  68. union acpi_operand_object *obj_desc;
  69. acpi_string val = NULL;
  70. ACPI_FUNCTION_TRACE ("ns_root_initialize");
  71. status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  72. if (ACPI_FAILURE (status)) {
  73. return_ACPI_STATUS (status);
  74. }
  75. /*
  76. * The global root ptr is initially NULL, so a non-NULL value indicates
  77. * that acpi_ns_root_initialize() has already been called; just return.
  78. */
  79. if (acpi_gbl_root_node) {
  80. status = AE_OK;
  81. goto unlock_and_exit;
  82. }
  83. /*
  84. * Tell the rest of the subsystem that the root is initialized
  85. * (This is OK because the namespace is locked)
  86. */
  87. acpi_gbl_root_node = &acpi_gbl_root_node_struct;
  88. /* Enter the pre-defined names in the name table */
  89. ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
  90. "Entering predefined entries into namespace\n"));
  91. for (init_val = acpi_gbl_pre_defined_names; init_val->name; init_val++) {
  92. /* _OSI is optional for now, will be permanent later */
  93. if (!ACPI_STRCMP (init_val->name, "_OSI") && !acpi_gbl_create_osi_method) {
  94. continue;
  95. }
  96. status = acpi_ns_lookup (NULL, init_val->name, init_val->type,
  97. ACPI_IMODE_LOAD_PASS2, ACPI_NS_NO_UPSEARCH,
  98. NULL, &new_node);
  99. if (ACPI_FAILURE (status) || (!new_node)) /* Must be on same line for code converter */ {
  100. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  101. "Could not create predefined name %s, %s\n",
  102. init_val->name, acpi_format_exception (status)));
  103. }
  104. /*
  105. * Name entered successfully.
  106. * If entry in pre_defined_names[] specifies an
  107. * initial value, create the initial value.
  108. */
  109. if (init_val->val) {
  110. status = acpi_os_predefined_override (init_val, &val);
  111. if (ACPI_FAILURE (status)) {
  112. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  113. "Could not override predefined %s\n",
  114. init_val->name));
  115. }
  116. if (!val) {
  117. val = init_val->val;
  118. }
  119. /*
  120. * Entry requests an initial value, allocate a
  121. * descriptor for it.
  122. */
  123. obj_desc = acpi_ut_create_internal_object (init_val->type);
  124. if (!obj_desc) {
  125. status = AE_NO_MEMORY;
  126. goto unlock_and_exit;
  127. }
  128. /*
  129. * Convert value string from table entry to
  130. * internal representation. Only types actually
  131. * used for initial values are implemented here.
  132. */
  133. switch (init_val->type) {
  134. case ACPI_TYPE_METHOD:
  135. obj_desc->method.param_count = (u8) ACPI_TO_INTEGER (val);
  136. obj_desc->common.flags |= AOPOBJ_DATA_VALID;
  137. #if defined (ACPI_ASL_COMPILER) || defined (ACPI_DUMP_App)
  138. /*
  139. * i_aSL Compiler cheats by putting parameter count
  140. * in the owner_iD (param_count max is 7)
  141. */
  142. new_node->owner_id = obj_desc->method.param_count;
  143. #else
  144. /* Mark this as a very SPECIAL method */
  145. obj_desc->method.method_flags = AML_METHOD_INTERNAL_ONLY;
  146. obj_desc->method.implementation = acpi_ut_osi_implementation;
  147. #endif
  148. break;
  149. case ACPI_TYPE_INTEGER:
  150. obj_desc->integer.value = ACPI_TO_INTEGER (val);
  151. break;
  152. case ACPI_TYPE_STRING:
  153. /*
  154. * Build an object around the static string
  155. */
  156. obj_desc->string.length = (u32) ACPI_STRLEN (val);
  157. obj_desc->string.pointer = val;
  158. obj_desc->common.flags |= AOPOBJ_STATIC_POINTER;
  159. break;
  160. case ACPI_TYPE_MUTEX:
  161. obj_desc->mutex.node = new_node;
  162. obj_desc->mutex.sync_level = (u8) (ACPI_TO_INTEGER (val) - 1);
  163. if (ACPI_STRCMP (init_val->name, "_GL_") == 0) {
  164. /*
  165. * Create a counting semaphore for the
  166. * global lock
  167. */
  168. status = acpi_os_create_semaphore (ACPI_NO_UNIT_LIMIT,
  169. 1, &obj_desc->mutex.semaphore);
  170. if (ACPI_FAILURE (status)) {
  171. acpi_ut_remove_reference (obj_desc);
  172. goto unlock_and_exit;
  173. }
  174. /*
  175. * We just created the mutex for the
  176. * global lock, save it
  177. */
  178. acpi_gbl_global_lock_semaphore = obj_desc->mutex.semaphore;
  179. }
  180. else {
  181. /* Create a mutex */
  182. status = acpi_os_create_semaphore (1, 1,
  183. &obj_desc->mutex.semaphore);
  184. if (ACPI_FAILURE (status)) {
  185. acpi_ut_remove_reference (obj_desc);
  186. goto unlock_and_exit;
  187. }
  188. }
  189. break;
  190. default:
  191. ACPI_REPORT_ERROR (("Unsupported initial type value %X\n",
  192. init_val->type));
  193. acpi_ut_remove_reference (obj_desc);
  194. obj_desc = NULL;
  195. continue;
  196. }
  197. /* Store pointer to value descriptor in the Node */
  198. status = acpi_ns_attach_object (new_node, obj_desc,
  199. ACPI_GET_OBJECT_TYPE (obj_desc));
  200. /* Remove local reference to the object */
  201. acpi_ut_remove_reference (obj_desc);
  202. }
  203. }
  204. unlock_and_exit:
  205. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  206. /* Save a handle to "_GPE", it is always present */
  207. if (ACPI_SUCCESS (status)) {
  208. status = acpi_ns_get_node_by_path ("\\_GPE", NULL, ACPI_NS_NO_UPSEARCH,
  209. &acpi_gbl_fadt_gpe_device);
  210. }
  211. return_ACPI_STATUS (status);
  212. }
  213. /*******************************************************************************
  214. *
  215. * FUNCTION: acpi_ns_lookup
  216. *
  217. * PARAMETERS: scope_info - Current scope info block
  218. * Pathname - Search pathname, in internal format
  219. * (as represented in the AML stream)
  220. * Type - Type associated with name
  221. * interpreter_mode - IMODE_LOAD_PASS2 => add name if not found
  222. * Flags - Flags describing the search restrictions
  223. * walk_state - Current state of the walk
  224. * return_node - Where the Node is placed (if found
  225. * or created successfully)
  226. *
  227. * RETURN: Status
  228. *
  229. * DESCRIPTION: Find or enter the passed name in the name space.
  230. * Log an error if name not found in Exec mode.
  231. *
  232. * MUTEX: Assumes namespace is locked.
  233. *
  234. ******************************************************************************/
  235. acpi_status
  236. acpi_ns_lookup (
  237. union acpi_generic_state *scope_info,
  238. char *pathname,
  239. acpi_object_type type,
  240. acpi_interpreter_mode interpreter_mode,
  241. u32 flags,
  242. struct acpi_walk_state *walk_state,
  243. struct acpi_namespace_node **return_node)
  244. {
  245. acpi_status status;
  246. char *path = pathname;
  247. struct acpi_namespace_node *prefix_node;
  248. struct acpi_namespace_node *current_node = NULL;
  249. struct acpi_namespace_node *this_node = NULL;
  250. u32 num_segments;
  251. u32 num_carats;
  252. acpi_name simple_name;
  253. acpi_object_type type_to_check_for;
  254. acpi_object_type this_search_type;
  255. u32 search_parent_flag = ACPI_NS_SEARCH_PARENT;
  256. u32 local_flags = flags & ~(ACPI_NS_ERROR_IF_FOUND |
  257. ACPI_NS_SEARCH_PARENT);
  258. ACPI_FUNCTION_TRACE ("ns_lookup");
  259. if (!return_node) {
  260. return_ACPI_STATUS (AE_BAD_PARAMETER);
  261. }
  262. acpi_gbl_ns_lookup_count++;
  263. *return_node = ACPI_ENTRY_NOT_FOUND;
  264. if (!acpi_gbl_root_node) {
  265. return_ACPI_STATUS (AE_NO_NAMESPACE);
  266. }
  267. /*
  268. * Get the prefix scope.
  269. * A null scope means use the root scope
  270. */
  271. if ((!scope_info) ||
  272. (!scope_info->scope.node)) {
  273. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  274. "Null scope prefix, using root node (%p)\n",
  275. acpi_gbl_root_node));
  276. prefix_node = acpi_gbl_root_node;
  277. }
  278. else {
  279. prefix_node = scope_info->scope.node;
  280. if (ACPI_GET_DESCRIPTOR_TYPE (prefix_node) != ACPI_DESC_TYPE_NAMED) {
  281. ACPI_REPORT_ERROR (("ns_lookup: %p is not a namespace node [%s]\n",
  282. prefix_node, acpi_ut_get_descriptor_name (prefix_node)));
  283. return_ACPI_STATUS (AE_AML_INTERNAL);
  284. }
  285. /*
  286. * This node might not be a actual "scope" node (such as a
  287. * Device/Method, etc.) It could be a Package or other object node.
  288. * Backup up the tree to find the containing scope node.
  289. */
  290. while (!acpi_ns_opens_scope (prefix_node->type) &&
  291. prefix_node->type != ACPI_TYPE_ANY) {
  292. prefix_node = acpi_ns_get_parent_node (prefix_node);
  293. }
  294. }
  295. /* Save type TBD: may be no longer necessary */
  296. type_to_check_for = type;
  297. /*
  298. * Begin examination of the actual pathname
  299. */
  300. if (!pathname) {
  301. /* A Null name_path is allowed and refers to the root */
  302. num_segments = 0;
  303. this_node = acpi_gbl_root_node;
  304. path = "";
  305. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  306. "Null Pathname (Zero segments), Flags=%X\n", flags));
  307. }
  308. else {
  309. /*
  310. * Name pointer is valid (and must be in internal name format)
  311. *
  312. * Check for scope prefixes:
  313. *
  314. * As represented in the AML stream, a namepath consists of an
  315. * optional scope prefix followed by a name segment part.
  316. *
  317. * If present, the scope prefix is either a Root Prefix (in
  318. * which case the name is fully qualified), or one or more
  319. * Parent Prefixes (in which case the name's scope is relative
  320. * to the current scope).
  321. */
  322. if (*path == (u8) AML_ROOT_PREFIX) {
  323. /* Pathname is fully qualified, start from the root */
  324. this_node = acpi_gbl_root_node;
  325. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  326. /* Point to name segment part */
  327. path++;
  328. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  329. "Path is absolute from root [%p]\n", this_node));
  330. }
  331. else {
  332. /* Pathname is relative to current scope, start there */
  333. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  334. "Searching relative to prefix scope [%4.4s] (%p)\n",
  335. acpi_ut_get_node_name (prefix_node), prefix_node));
  336. /*
  337. * Handle multiple Parent Prefixes (carat) by just getting
  338. * the parent node for each prefix instance.
  339. */
  340. this_node = prefix_node;
  341. num_carats = 0;
  342. while (*path == (u8) AML_PARENT_PREFIX) {
  343. /* Name is fully qualified, no search rules apply */
  344. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  345. /*
  346. * Point past this prefix to the name segment
  347. * part or the next Parent Prefix
  348. */
  349. path++;
  350. /* Backup to the parent node */
  351. num_carats++;
  352. this_node = acpi_ns_get_parent_node (this_node);
  353. if (!this_node) {
  354. /* Current scope has no parent scope */
  355. ACPI_REPORT_ERROR (
  356. ("ACPI path has too many parent prefixes (^) - reached beyond root node\n"));
  357. return_ACPI_STATUS (AE_NOT_FOUND);
  358. }
  359. }
  360. if (search_parent_flag == ACPI_NS_NO_UPSEARCH) {
  361. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  362. "Search scope is [%4.4s], path has %d carat(s)\n",
  363. acpi_ut_get_node_name (this_node), num_carats));
  364. }
  365. }
  366. /*
  367. * Determine the number of ACPI name segments in this pathname.
  368. *
  369. * The segment part consists of either:
  370. * - A Null name segment (0)
  371. * - A dual_name_prefix followed by two 4-byte name segments
  372. * - A multi_name_prefix followed by a byte indicating the
  373. * number of segments and the segments themselves.
  374. * - A single 4-byte name segment
  375. *
  376. * Examine the name prefix opcode, if any, to determine the number of
  377. * segments.
  378. */
  379. switch (*path) {
  380. case 0:
  381. /*
  382. * Null name after a root or parent prefixes. We already
  383. * have the correct target node and there are no name segments.
  384. */
  385. num_segments = 0;
  386. type = this_node->type;
  387. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  388. "Prefix-only Pathname (Zero name segments), Flags=%X\n",
  389. flags));
  390. break;
  391. case AML_DUAL_NAME_PREFIX:
  392. /* More than one name_seg, search rules do not apply */
  393. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  394. /* Two segments, point to first name segment */
  395. num_segments = 2;
  396. path++;
  397. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  398. "Dual Pathname (2 segments, Flags=%X)\n", flags));
  399. break;
  400. case AML_MULTI_NAME_PREFIX_OP:
  401. /* More than one name_seg, search rules do not apply */
  402. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  403. /* Extract segment count, point to first name segment */
  404. path++;
  405. num_segments = (u32) (u8) *path;
  406. path++;
  407. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  408. "Multi Pathname (%d Segments, Flags=%X) \n",
  409. num_segments, flags));
  410. break;
  411. default:
  412. /*
  413. * Not a Null name, no Dual or Multi prefix, hence there is
  414. * only one name segment and Pathname is already pointing to it.
  415. */
  416. num_segments = 1;
  417. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  418. "Simple Pathname (1 segment, Flags=%X)\n", flags));
  419. break;
  420. }
  421. ACPI_DEBUG_EXEC (acpi_ns_print_pathname (num_segments, path));
  422. }
  423. /*
  424. * Search namespace for each segment of the name. Loop through and
  425. * verify (or add to the namespace) each name segment.
  426. *
  427. * The object type is significant only at the last name
  428. * segment. (We don't care about the types along the path, only
  429. * the type of the final target object.)
  430. */
  431. this_search_type = ACPI_TYPE_ANY;
  432. current_node = this_node;
  433. while (num_segments && current_node) {
  434. num_segments--;
  435. if (!num_segments) {
  436. /*
  437. * This is the last segment, enable typechecking
  438. */
  439. this_search_type = type;
  440. /*
  441. * Only allow automatic parent search (search rules) if the caller
  442. * requested it AND we have a single, non-fully-qualified name_seg
  443. */
  444. if ((search_parent_flag != ACPI_NS_NO_UPSEARCH) &&
  445. (flags & ACPI_NS_SEARCH_PARENT)) {
  446. local_flags |= ACPI_NS_SEARCH_PARENT;
  447. }
  448. /* Set error flag according to caller */
  449. if (flags & ACPI_NS_ERROR_IF_FOUND) {
  450. local_flags |= ACPI_NS_ERROR_IF_FOUND;
  451. }
  452. }
  453. /* Extract one ACPI name from the front of the pathname */
  454. ACPI_MOVE_32_TO_32 (&simple_name, path);
  455. /* Try to find the single (4 character) ACPI name */
  456. status = acpi_ns_search_and_enter (simple_name, walk_state, current_node,
  457. interpreter_mode, this_search_type, local_flags, &this_node);
  458. if (ACPI_FAILURE (status)) {
  459. if (status == AE_NOT_FOUND) {
  460. /* Name not found in ACPI namespace */
  461. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  462. "Name [%4.4s] not found in scope [%4.4s] %p\n",
  463. (char *) &simple_name, (char *) &current_node->name,
  464. current_node));
  465. }
  466. *return_node = this_node;
  467. return_ACPI_STATUS (status);
  468. }
  469. /*
  470. * Sanity typecheck of the target object:
  471. *
  472. * If 1) This is the last segment (num_segments == 0)
  473. * 2) And we are looking for a specific type
  474. * (Not checking for TYPE_ANY)
  475. * 3) Which is not an alias
  476. * 4) Which is not a local type (TYPE_SCOPE)
  477. * 5) And the type of target object is known (not TYPE_ANY)
  478. * 6) And target object does not match what we are looking for
  479. *
  480. * Then we have a type mismatch. Just warn and ignore it.
  481. */
  482. if ((num_segments == 0) &&
  483. (type_to_check_for != ACPI_TYPE_ANY) &&
  484. (type_to_check_for != ACPI_TYPE_LOCAL_ALIAS) &&
  485. (type_to_check_for != ACPI_TYPE_LOCAL_METHOD_ALIAS) &&
  486. (type_to_check_for != ACPI_TYPE_LOCAL_SCOPE) &&
  487. (this_node->type != ACPI_TYPE_ANY) &&
  488. (this_node->type != type_to_check_for)) {
  489. /* Complain about a type mismatch */
  490. ACPI_REPORT_WARNING (
  491. ("ns_lookup: Type mismatch on %4.4s (%s), searching for (%s)\n",
  492. (char *) &simple_name, acpi_ut_get_type_name (this_node->type),
  493. acpi_ut_get_type_name (type_to_check_for)));
  494. }
  495. /*
  496. * If this is the last name segment and we are not looking for a
  497. * specific type, but the type of found object is known, use that type
  498. * to see if it opens a scope.
  499. */
  500. if ((num_segments == 0) && (type == ACPI_TYPE_ANY)) {
  501. type = this_node->type;
  502. }
  503. /* Point to next name segment and make this node current */
  504. path += ACPI_NAME_SIZE;
  505. current_node = this_node;
  506. }
  507. /*
  508. * Always check if we need to open a new scope
  509. */
  510. if (!(flags & ACPI_NS_DONT_OPEN_SCOPE) && (walk_state)) {
  511. /*
  512. * If entry is a type which opens a scope, push the new scope on the
  513. * scope stack.
  514. */
  515. if (acpi_ns_opens_scope (type)) {
  516. status = acpi_ds_scope_stack_push (this_node, type, walk_state);
  517. if (ACPI_FAILURE (status)) {
  518. return_ACPI_STATUS (status);
  519. }
  520. }
  521. }
  522. *return_node = this_node;
  523. return_ACPI_STATUS (AE_OK);
  524. }