utmisc.c 27 KB

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