utmisc.c 25 KB

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