utmisc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmisc - common utility procedures
  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 "acnamesp.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utmisc")
  47. #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: ut_convert_backslashes
  51. *
  52. * PARAMETERS: pathname - File pathname string to be converted
  53. *
  54. * RETURN: Modifies the input Pathname
  55. *
  56. * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
  57. * the entire input file pathname string.
  58. *
  59. ******************************************************************************/
  60. void ut_convert_backslashes(char *pathname)
  61. {
  62. if (!pathname) {
  63. return;
  64. }
  65. while (*pathname) {
  66. if (*pathname == '\\') {
  67. *pathname = '/';
  68. }
  69. pathname++;
  70. }
  71. }
  72. #endif
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ut_is_pci_root_bridge
  76. *
  77. * PARAMETERS: id - The HID/CID in string format
  78. *
  79. * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
  80. *
  81. * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
  82. *
  83. ******************************************************************************/
  84. u8 acpi_ut_is_pci_root_bridge(char *id)
  85. {
  86. /*
  87. * Check if this is a PCI root bridge.
  88. * ACPI 3.0+: check for a PCI Express root also.
  89. */
  90. if (!(ACPI_STRCMP(id,
  91. PCI_ROOT_HID_STRING)) ||
  92. !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
  93. return (TRUE);
  94. }
  95. return (FALSE);
  96. }
  97. /*******************************************************************************
  98. *
  99. * FUNCTION: acpi_ut_is_aml_table
  100. *
  101. * PARAMETERS: table - An ACPI table
  102. *
  103. * RETURN: TRUE if table contains executable AML; FALSE otherwise
  104. *
  105. * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
  106. * Currently, these are DSDT,SSDT,PSDT. All other table types are
  107. * data tables that do not contain AML code.
  108. *
  109. ******************************************************************************/
  110. u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
  111. {
  112. /* These are the only tables that contain executable AML */
  113. if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
  114. ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
  115. ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
  116. return (TRUE);
  117. }
  118. return (FALSE);
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_ut_allocate_owner_id
  123. *
  124. * PARAMETERS: owner_id - Where the new owner ID is returned
  125. *
  126. * RETURN: Status
  127. *
  128. * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
  129. * track objects created by the table or method, to be deleted
  130. * when the method exits or the table is unloaded.
  131. *
  132. ******************************************************************************/
  133. acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
  134. {
  135. u32 i;
  136. u32 j;
  137. u32 k;
  138. acpi_status status;
  139. ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
  140. /* Guard against multiple allocations of ID to the same location */
  141. if (*owner_id) {
  142. ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
  143. *owner_id));
  144. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  145. }
  146. /* Mutex for the global ID mask */
  147. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  148. if (ACPI_FAILURE(status)) {
  149. return_ACPI_STATUS(status);
  150. }
  151. /*
  152. * Find a free owner ID, cycle through all possible IDs on repeated
  153. * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
  154. * to be scanned twice.
  155. */
  156. for (i = 0, j = acpi_gbl_last_owner_id_index;
  157. i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
  158. if (j >= ACPI_NUM_OWNERID_MASKS) {
  159. j = 0; /* Wraparound to start of mask array */
  160. }
  161. for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
  162. if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
  163. /* There are no free IDs in this mask */
  164. break;
  165. }
  166. if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
  167. /*
  168. * Found a free ID. The actual ID is the bit index plus one,
  169. * making zero an invalid Owner ID. Save this as the last ID
  170. * allocated and update the global ID mask.
  171. */
  172. acpi_gbl_owner_id_mask[j] |= (1 << k);
  173. acpi_gbl_last_owner_id_index = (u8)j;
  174. acpi_gbl_next_owner_id_offset = (u8)(k + 1);
  175. /*
  176. * Construct encoded ID from the index and bit position
  177. *
  178. * Note: Last [j].k (bit 255) is never used and is marked
  179. * permanently allocated (prevents +1 overflow)
  180. */
  181. *owner_id =
  182. (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
  183. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  184. "Allocated OwnerId: %2.2X\n",
  185. (unsigned int)*owner_id));
  186. goto exit;
  187. }
  188. }
  189. acpi_gbl_next_owner_id_offset = 0;
  190. }
  191. /*
  192. * All owner_ids have been allocated. This typically should
  193. * not happen since the IDs are reused after deallocation. The IDs are
  194. * allocated upon table load (one per table) and method execution, and
  195. * they are released when a table is unloaded or a method completes
  196. * execution.
  197. *
  198. * If this error happens, there may be very deep nesting of invoked control
  199. * methods, or there may be a bug where the IDs are not released.
  200. */
  201. status = AE_OWNER_ID_LIMIT;
  202. ACPI_ERROR((AE_INFO,
  203. "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
  204. exit:
  205. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  206. return_ACPI_STATUS(status);
  207. }
  208. /*******************************************************************************
  209. *
  210. * FUNCTION: acpi_ut_release_owner_id
  211. *
  212. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
  213. *
  214. * RETURN: None. No error is returned because we are either exiting a
  215. * control method or unloading a table. Either way, we would
  216. * ignore any error anyway.
  217. *
  218. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  219. *
  220. ******************************************************************************/
  221. void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
  222. {
  223. acpi_owner_id owner_id = *owner_id_ptr;
  224. acpi_status status;
  225. u32 index;
  226. u32 bit;
  227. ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
  228. /* Always clear the input owner_id (zero is an invalid ID) */
  229. *owner_id_ptr = 0;
  230. /* Zero is not a valid owner_ID */
  231. if (owner_id == 0) {
  232. ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
  233. return_VOID;
  234. }
  235. /* Mutex for the global ID mask */
  236. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  237. if (ACPI_FAILURE(status)) {
  238. return_VOID;
  239. }
  240. /* Normalize the ID to zero */
  241. owner_id--;
  242. /* Decode ID to index/offset pair */
  243. index = ACPI_DIV_32(owner_id);
  244. bit = 1 << ACPI_MOD_32(owner_id);
  245. /* Free the owner ID only if it is valid */
  246. if (acpi_gbl_owner_id_mask[index] & bit) {
  247. acpi_gbl_owner_id_mask[index] ^= bit;
  248. } else {
  249. ACPI_ERROR((AE_INFO,
  250. "Release of non-allocated OwnerId: 0x%2.2X",
  251. owner_id + 1));
  252. }
  253. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  254. return_VOID;
  255. }
  256. /*******************************************************************************
  257. *
  258. * FUNCTION: acpi_ut_strupr (strupr)
  259. *
  260. * PARAMETERS: src_string - The source string to convert
  261. *
  262. * RETURN: None
  263. *
  264. * DESCRIPTION: Convert string to uppercase
  265. *
  266. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  267. *
  268. ******************************************************************************/
  269. void acpi_ut_strupr(char *src_string)
  270. {
  271. char *string;
  272. ACPI_FUNCTION_ENTRY();
  273. if (!src_string) {
  274. return;
  275. }
  276. /* Walk entire string, uppercasing the letters */
  277. for (string = src_string; *string; string++) {
  278. *string = (char)ACPI_TOUPPER(*string);
  279. }
  280. return;
  281. }
  282. #ifdef ACPI_ASL_COMPILER
  283. /*******************************************************************************
  284. *
  285. * FUNCTION: acpi_ut_strlwr (strlwr)
  286. *
  287. * PARAMETERS: src_string - The source string to convert
  288. *
  289. * RETURN: None
  290. *
  291. * DESCRIPTION: Convert string to lowercase
  292. *
  293. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  294. *
  295. ******************************************************************************/
  296. void acpi_ut_strlwr(char *src_string)
  297. {
  298. char *string;
  299. ACPI_FUNCTION_ENTRY();
  300. if (!src_string) {
  301. return;
  302. }
  303. /* Walk entire string, lowercasing the letters */
  304. for (string = src_string; *string; string++) {
  305. *string = (char)ACPI_TOLOWER(*string);
  306. }
  307. return;
  308. }
  309. /******************************************************************************
  310. *
  311. * FUNCTION: acpi_ut_stricmp
  312. *
  313. * PARAMETERS: string1 - first string to compare
  314. * string2 - second string to compare
  315. *
  316. * RETURN: int that signifies string relationship. Zero means strings
  317. * are equal.
  318. *
  319. * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
  320. * strings with no case sensitivity)
  321. *
  322. ******************************************************************************/
  323. int acpi_ut_stricmp(char *string1, char *string2)
  324. {
  325. int c1;
  326. int c2;
  327. do {
  328. c1 = tolower((int)*string1);
  329. c2 = tolower((int)*string2);
  330. string1++;
  331. string2++;
  332. }
  333. while ((c1 == c2) && (c1));
  334. return (c1 - c2);
  335. }
  336. #endif
  337. /*******************************************************************************
  338. *
  339. * FUNCTION: acpi_ut_print_string
  340. *
  341. * PARAMETERS: string - Null terminated ASCII string
  342. * max_length - Maximum output length
  343. *
  344. * RETURN: None
  345. *
  346. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  347. * sequences.
  348. *
  349. ******************************************************************************/
  350. void acpi_ut_print_string(char *string, u8 max_length)
  351. {
  352. u32 i;
  353. if (!string) {
  354. acpi_os_printf("<\"NULL STRING PTR\">");
  355. return;
  356. }
  357. acpi_os_printf("\"");
  358. for (i = 0; string[i] && (i < max_length); i++) {
  359. /* Escape sequences */
  360. switch (string[i]) {
  361. case 0x07:
  362. acpi_os_printf("\\a"); /* BELL */
  363. break;
  364. case 0x08:
  365. acpi_os_printf("\\b"); /* BACKSPACE */
  366. break;
  367. case 0x0C:
  368. acpi_os_printf("\\f"); /* FORMFEED */
  369. break;
  370. case 0x0A:
  371. acpi_os_printf("\\n"); /* LINEFEED */
  372. break;
  373. case 0x0D:
  374. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  375. break;
  376. case 0x09:
  377. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  378. break;
  379. case 0x0B:
  380. acpi_os_printf("\\v"); /* VERTICAL TAB */
  381. break;
  382. case '\'': /* Single Quote */
  383. case '\"': /* Double Quote */
  384. case '\\': /* Backslash */
  385. acpi_os_printf("\\%c", (int)string[i]);
  386. break;
  387. default:
  388. /* Check for printable character or hex escape */
  389. if (ACPI_IS_PRINT(string[i])) {
  390. /* This is a normal character */
  391. acpi_os_printf("%c", (int)string[i]);
  392. } else {
  393. /* All others will be Hex escapes */
  394. acpi_os_printf("\\x%2.2X", (s32) string[i]);
  395. }
  396. break;
  397. }
  398. }
  399. acpi_os_printf("\"");
  400. if (i == max_length && string[i]) {
  401. acpi_os_printf("...");
  402. }
  403. }
  404. /*******************************************************************************
  405. *
  406. * FUNCTION: acpi_ut_dword_byte_swap
  407. *
  408. * PARAMETERS: value - Value to be converted
  409. *
  410. * RETURN: u32 integer with bytes swapped
  411. *
  412. * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
  413. *
  414. ******************************************************************************/
  415. u32 acpi_ut_dword_byte_swap(u32 value)
  416. {
  417. union {
  418. u32 value;
  419. u8 bytes[4];
  420. } out;
  421. union {
  422. u32 value;
  423. u8 bytes[4];
  424. } in;
  425. ACPI_FUNCTION_ENTRY();
  426. in.value = value;
  427. out.bytes[0] = in.bytes[3];
  428. out.bytes[1] = in.bytes[2];
  429. out.bytes[2] = in.bytes[1];
  430. out.bytes[3] = in.bytes[0];
  431. return (out.value);
  432. }
  433. /*******************************************************************************
  434. *
  435. * FUNCTION: acpi_ut_set_integer_width
  436. *
  437. * PARAMETERS: Revision From DSDT header
  438. *
  439. * RETURN: None
  440. *
  441. * DESCRIPTION: Set the global integer bit width based upon the revision
  442. * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
  443. * For Revision 2 and above, Integers are 64 bits. Yes, this
  444. * makes a difference.
  445. *
  446. ******************************************************************************/
  447. void acpi_ut_set_integer_width(u8 revision)
  448. {
  449. if (revision < 2) {
  450. /* 32-bit case */
  451. acpi_gbl_integer_bit_width = 32;
  452. acpi_gbl_integer_nybble_width = 8;
  453. acpi_gbl_integer_byte_width = 4;
  454. } else {
  455. /* 64-bit case (ACPI 2.0+) */
  456. acpi_gbl_integer_bit_width = 64;
  457. acpi_gbl_integer_nybble_width = 16;
  458. acpi_gbl_integer_byte_width = 8;
  459. }
  460. }
  461. #ifdef ACPI_DEBUG_OUTPUT
  462. /*******************************************************************************
  463. *
  464. * FUNCTION: acpi_ut_display_init_pathname
  465. *
  466. * PARAMETERS: type - Object type of the node
  467. * obj_handle - Handle whose pathname will be displayed
  468. * path - Additional path string to be appended.
  469. * (NULL if no extra path)
  470. *
  471. * RETURN: acpi_status
  472. *
  473. * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
  474. *
  475. ******************************************************************************/
  476. void
  477. acpi_ut_display_init_pathname(u8 type,
  478. struct acpi_namespace_node *obj_handle,
  479. char *path)
  480. {
  481. acpi_status status;
  482. struct acpi_buffer buffer;
  483. ACPI_FUNCTION_ENTRY();
  484. /* Only print the path if the appropriate debug level is enabled */
  485. if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
  486. return;
  487. }
  488. /* Get the full pathname to the node */
  489. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  490. status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
  491. if (ACPI_FAILURE(status)) {
  492. return;
  493. }
  494. /* Print what we're doing */
  495. switch (type) {
  496. case ACPI_TYPE_METHOD:
  497. acpi_os_printf("Executing ");
  498. break;
  499. default:
  500. acpi_os_printf("Initializing ");
  501. break;
  502. }
  503. /* Print the object type and pathname */
  504. acpi_os_printf("%-12s %s",
  505. acpi_ut_get_type_name(type), (char *)buffer.pointer);
  506. /* Extra path is used to append names like _STA, _INI, etc. */
  507. if (path) {
  508. acpi_os_printf(".%s", path);
  509. }
  510. acpi_os_printf("\n");
  511. ACPI_FREE(buffer.pointer);
  512. }
  513. #endif
  514. /*******************************************************************************
  515. *
  516. * FUNCTION: acpi_ut_valid_acpi_char
  517. *
  518. * PARAMETERS: char - The character to be examined
  519. * position - Byte position (0-3)
  520. *
  521. * RETURN: TRUE if the character is valid, FALSE otherwise
  522. *
  523. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  524. * 1) Upper case alpha
  525. * 2) numeric
  526. * 3) underscore
  527. *
  528. * We allow a '!' as the last character because of the ASF! table
  529. *
  530. ******************************************************************************/
  531. u8 acpi_ut_valid_acpi_char(char character, u32 position)
  532. {
  533. if (!((character >= 'A' && character <= 'Z') ||
  534. (character >= '0' && character <= '9') || (character == '_'))) {
  535. /* Allow a '!' in the last position */
  536. if (character == '!' && position == 3) {
  537. return (TRUE);
  538. }
  539. return (FALSE);
  540. }
  541. return (TRUE);
  542. }
  543. /*******************************************************************************
  544. *
  545. * FUNCTION: acpi_ut_valid_acpi_name
  546. *
  547. * PARAMETERS: name - The name to be examined
  548. *
  549. * RETURN: TRUE if the name is valid, FALSE otherwise
  550. *
  551. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  552. * 1) Upper case alpha
  553. * 2) numeric
  554. * 3) underscore
  555. *
  556. ******************************************************************************/
  557. u8 acpi_ut_valid_acpi_name(u32 name)
  558. {
  559. u32 i;
  560. ACPI_FUNCTION_ENTRY();
  561. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  562. if (!acpi_ut_valid_acpi_char
  563. ((ACPI_CAST_PTR(char, &name))[i], i)) {
  564. return (FALSE);
  565. }
  566. }
  567. return (TRUE);
  568. }
  569. /*******************************************************************************
  570. *
  571. * FUNCTION: acpi_ut_repair_name
  572. *
  573. * PARAMETERS: name - The ACPI name to be repaired
  574. *
  575. * RETURN: Repaired version of the name
  576. *
  577. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  578. * return the new name. NOTE: the Name parameter must reside in
  579. * read/write memory, cannot be a const.
  580. *
  581. * An ACPI Name must consist of valid ACPI characters. We will repair the name
  582. * if necessary because we don't want to abort because of this, but we want
  583. * all namespace names to be printable. A warning message is appropriate.
  584. *
  585. * This issue came up because there are in fact machines that exhibit
  586. * this problem, and we want to be able to enable ACPI support for them,
  587. * even though there are a few bad names.
  588. *
  589. ******************************************************************************/
  590. void acpi_ut_repair_name(char *name)
  591. {
  592. u32 i;
  593. u8 found_bad_char = FALSE;
  594. u32 original_name;
  595. ACPI_FUNCTION_NAME(ut_repair_name);
  596. ACPI_MOVE_NAME(&original_name, name);
  597. /* Check each character in the name */
  598. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  599. if (acpi_ut_valid_acpi_char(name[i], i)) {
  600. continue;
  601. }
  602. /*
  603. * Replace a bad character with something printable, yet technically
  604. * still invalid. This prevents any collisions with existing "good"
  605. * names in the namespace.
  606. */
  607. name[i] = '*';
  608. found_bad_char = TRUE;
  609. }
  610. if (found_bad_char) {
  611. /* Report warning only if in strict mode or debug mode */
  612. if (!acpi_gbl_enable_interpreter_slack) {
  613. ACPI_WARNING((AE_INFO,
  614. "Found bad character(s) in name, repaired: [%4.4s]\n",
  615. name));
  616. } else {
  617. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  618. "Found bad character(s) in name, repaired: [%4.4s]\n",
  619. name));
  620. }
  621. }
  622. }
  623. /*******************************************************************************
  624. *
  625. * FUNCTION: acpi_ut_strtoul64
  626. *
  627. * PARAMETERS: string - Null terminated string
  628. * base - Radix of the string: 16 or ACPI_ANY_BASE;
  629. * ACPI_ANY_BASE means 'in behalf of to_integer'
  630. * ret_integer - Where the converted integer is returned
  631. *
  632. * RETURN: Status and Converted value
  633. *
  634. * DESCRIPTION: Convert a string into an unsigned value. Performs either a
  635. * 32-bit or 64-bit conversion, depending on the current mode
  636. * of the interpreter.
  637. * NOTE: Does not support Octal strings, not needed.
  638. *
  639. ******************************************************************************/
  640. acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer)
  641. {
  642. u32 this_digit = 0;
  643. u64 return_value = 0;
  644. u64 quotient;
  645. u64 dividend;
  646. u32 to_integer_op = (base == ACPI_ANY_BASE);
  647. u32 mode32 = (acpi_gbl_integer_byte_width == 4);
  648. u8 valid_digits = 0;
  649. u8 sign_of0x = 0;
  650. u8 term = 0;
  651. ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
  652. switch (base) {
  653. case ACPI_ANY_BASE:
  654. case 16:
  655. break;
  656. default:
  657. /* Invalid Base */
  658. return_ACPI_STATUS(AE_BAD_PARAMETER);
  659. }
  660. if (!string) {
  661. goto error_exit;
  662. }
  663. /* Skip over any white space in the buffer */
  664. while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
  665. string++;
  666. }
  667. if (to_integer_op) {
  668. /*
  669. * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
  670. * We need to determine if it is decimal or hexadecimal.
  671. */
  672. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  673. sign_of0x = 1;
  674. base = 16;
  675. /* Skip over the leading '0x' */
  676. string += 2;
  677. } else {
  678. base = 10;
  679. }
  680. }
  681. /* Any string left? Check that '0x' is not followed by white space. */
  682. if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
  683. if (to_integer_op) {
  684. goto error_exit;
  685. } else {
  686. goto all_done;
  687. }
  688. }
  689. /*
  690. * Perform a 32-bit or 64-bit conversion, depending upon the current
  691. * execution mode of the interpreter
  692. */
  693. dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  694. /* Main loop: convert the string to a 32- or 64-bit integer */
  695. while (*string) {
  696. if (ACPI_IS_DIGIT(*string)) {
  697. /* Convert ASCII 0-9 to Decimal value */
  698. this_digit = ((u8)*string) - '0';
  699. } else if (base == 10) {
  700. /* Digit is out of range; possible in to_integer case only */
  701. term = 1;
  702. } else {
  703. this_digit = (u8)ACPI_TOUPPER(*string);
  704. if (ACPI_IS_XDIGIT((char)this_digit)) {
  705. /* Convert ASCII Hex char to value */
  706. this_digit = this_digit - 'A' + 10;
  707. } else {
  708. term = 1;
  709. }
  710. }
  711. if (term) {
  712. if (to_integer_op) {
  713. goto error_exit;
  714. } else {
  715. break;
  716. }
  717. } else if ((valid_digits == 0) && (this_digit == 0)
  718. && !sign_of0x) {
  719. /* Skip zeros */
  720. string++;
  721. continue;
  722. }
  723. valid_digits++;
  724. if (sign_of0x
  725. && ((valid_digits > 16)
  726. || ((valid_digits > 8) && mode32))) {
  727. /*
  728. * This is to_integer operation case.
  729. * No any restrictions for string-to-integer conversion,
  730. * see ACPI spec.
  731. */
  732. goto error_exit;
  733. }
  734. /* Divide the digit into the correct position */
  735. (void)acpi_ut_short_divide((dividend - (u64)this_digit),
  736. base, &quotient, NULL);
  737. if (return_value > quotient) {
  738. if (to_integer_op) {
  739. goto error_exit;
  740. } else {
  741. break;
  742. }
  743. }
  744. return_value *= base;
  745. return_value += this_digit;
  746. string++;
  747. }
  748. /* All done, normal exit */
  749. all_done:
  750. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  751. ACPI_FORMAT_UINT64(return_value)));
  752. *ret_integer = return_value;
  753. return_ACPI_STATUS(AE_OK);
  754. error_exit:
  755. /* Base was set/validated above */
  756. if (base == 10) {
  757. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  758. } else {
  759. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  760. }
  761. }
  762. /*******************************************************************************
  763. *
  764. * FUNCTION: acpi_ut_create_update_state_and_push
  765. *
  766. * PARAMETERS: object - Object to be added to the new state
  767. * action - Increment/Decrement
  768. * state_list - List the state will be added to
  769. *
  770. * RETURN: Status
  771. *
  772. * DESCRIPTION: Create a new state and push it
  773. *
  774. ******************************************************************************/
  775. acpi_status
  776. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  777. u16 action,
  778. union acpi_generic_state **state_list)
  779. {
  780. union acpi_generic_state *state;
  781. ACPI_FUNCTION_ENTRY();
  782. /* Ignore null objects; these are expected */
  783. if (!object) {
  784. return (AE_OK);
  785. }
  786. state = acpi_ut_create_update_state(object, action);
  787. if (!state) {
  788. return (AE_NO_MEMORY);
  789. }
  790. acpi_ut_push_generic_state(state_list, state);
  791. return (AE_OK);
  792. }
  793. /*******************************************************************************
  794. *
  795. * FUNCTION: acpi_ut_walk_package_tree
  796. *
  797. * PARAMETERS: source_object - The package to walk
  798. * target_object - Target object (if package is being copied)
  799. * walk_callback - Called once for each package element
  800. * context - Passed to the callback function
  801. *
  802. * RETURN: Status
  803. *
  804. * DESCRIPTION: Walk through a package
  805. *
  806. ******************************************************************************/
  807. acpi_status
  808. acpi_ut_walk_package_tree(union acpi_operand_object *source_object,
  809. void *target_object,
  810. acpi_pkg_callback walk_callback, void *context)
  811. {
  812. acpi_status status = AE_OK;
  813. union acpi_generic_state *state_list = NULL;
  814. union acpi_generic_state *state;
  815. u32 this_index;
  816. union acpi_operand_object *this_source_obj;
  817. ACPI_FUNCTION_TRACE(ut_walk_package_tree);
  818. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  819. if (!state) {
  820. return_ACPI_STATUS(AE_NO_MEMORY);
  821. }
  822. while (state) {
  823. /* Get one element of the package */
  824. this_index = state->pkg.index;
  825. this_source_obj = (union acpi_operand_object *)
  826. state->pkg.source_object->package.elements[this_index];
  827. /*
  828. * Check for:
  829. * 1) An uninitialized package element. It is completely
  830. * legal to declare a package and leave it uninitialized
  831. * 2) Not an internal object - can be a namespace node instead
  832. * 3) Any type other than a package. Packages are handled in else
  833. * case below.
  834. */
  835. if ((!this_source_obj) ||
  836. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  837. ACPI_DESC_TYPE_OPERAND)
  838. || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
  839. status =
  840. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  841. this_source_obj, state, context);
  842. if (ACPI_FAILURE(status)) {
  843. return_ACPI_STATUS(status);
  844. }
  845. state->pkg.index++;
  846. while (state->pkg.index >=
  847. state->pkg.source_object->package.count) {
  848. /*
  849. * We've handled all of the objects at this level, This means
  850. * that we have just completed a package. That package may
  851. * have contained one or more packages itself.
  852. *
  853. * Delete this state and pop the previous state (package).
  854. */
  855. acpi_ut_delete_generic_state(state);
  856. state = acpi_ut_pop_generic_state(&state_list);
  857. /* Finished when there are no more states */
  858. if (!state) {
  859. /*
  860. * We have handled all of the objects in the top level
  861. * package just add the length of the package objects
  862. * and exit
  863. */
  864. return_ACPI_STATUS(AE_OK);
  865. }
  866. /*
  867. * Go back up a level and move the index past the just
  868. * completed package object.
  869. */
  870. state->pkg.index++;
  871. }
  872. } else {
  873. /* This is a subobject of type package */
  874. status =
  875. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  876. this_source_obj, state, context);
  877. if (ACPI_FAILURE(status)) {
  878. return_ACPI_STATUS(status);
  879. }
  880. /*
  881. * Push the current state and create a new one
  882. * The callback above returned a new target package object.
  883. */
  884. acpi_ut_push_generic_state(&state_list, state);
  885. state = acpi_ut_create_pkg_state(this_source_obj,
  886. state->pkg.
  887. this_target_obj, 0);
  888. if (!state) {
  889. /* Free any stacked Update State objects */
  890. while (state_list) {
  891. state =
  892. acpi_ut_pop_generic_state
  893. (&state_list);
  894. acpi_ut_delete_generic_state(state);
  895. }
  896. return_ACPI_STATUS(AE_NO_MEMORY);
  897. }
  898. }
  899. }
  900. /* We should never get here */
  901. return_ACPI_STATUS(AE_AML_INTERNAL);
  902. }