utmisc.c 25 KB

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