utmisc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 owner_id: %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 owner_id (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 owner_id: %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 owner_id: %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: 10, 16, or ACPI_ANY_BASE
  496. * ret_integer - Where the converted integer is returned
  497. *
  498. * RETURN: Status and Converted value
  499. *
  500. * DESCRIPTION: Convert a string into an unsigned value.
  501. * NOTE: Does not support Octal strings, not needed.
  502. *
  503. ******************************************************************************/
  504. acpi_status
  505. acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
  506. {
  507. u32 this_digit = 0;
  508. acpi_integer return_value = 0;
  509. acpi_integer quotient;
  510. ACPI_FUNCTION_TRACE("ut_stroul64");
  511. if ((!string) || !(*string)) {
  512. goto error_exit;
  513. }
  514. switch (base) {
  515. case ACPI_ANY_BASE:
  516. case 10:
  517. case 16:
  518. break;
  519. default:
  520. /* Invalid Base */
  521. return_ACPI_STATUS(AE_BAD_PARAMETER);
  522. }
  523. /* Skip over any white space in the buffer */
  524. while (ACPI_IS_SPACE(*string) || *string == '\t') {
  525. string++;
  526. }
  527. /*
  528. * If the input parameter Base is zero, then we need to
  529. * determine if it is decimal or hexadecimal:
  530. */
  531. if (base == 0) {
  532. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  533. base = 16;
  534. string += 2;
  535. } else {
  536. base = 10;
  537. }
  538. }
  539. /*
  540. * For hexadecimal base, skip over the leading
  541. * 0 or 0x, if they are present.
  542. */
  543. if ((base == 16) &&
  544. (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  545. string += 2;
  546. }
  547. /* Any string left? */
  548. if (!(*string)) {
  549. goto error_exit;
  550. }
  551. /* Main loop: convert the string to a 64-bit integer */
  552. while (*string) {
  553. if (ACPI_IS_DIGIT(*string)) {
  554. /* Convert ASCII 0-9 to Decimal value */
  555. this_digit = ((u8) * string) - '0';
  556. } else {
  557. if (base == 10) {
  558. /* Digit is out of range */
  559. goto error_exit;
  560. }
  561. this_digit = (u8) ACPI_TOUPPER(*string);
  562. if (ACPI_IS_XDIGIT((char)this_digit)) {
  563. /* Convert ASCII Hex char to value */
  564. this_digit = this_digit - 'A' + 10;
  565. } else {
  566. /*
  567. * We allow non-hex chars, just stop now, same as end-of-string.
  568. * See ACPI spec, string-to-integer conversion.
  569. */
  570. break;
  571. }
  572. }
  573. /* Divide the digit into the correct position */
  574. (void)
  575. acpi_ut_short_divide((ACPI_INTEGER_MAX -
  576. (acpi_integer) this_digit), base,
  577. &quotient, NULL);
  578. if (return_value > quotient) {
  579. goto error_exit;
  580. }
  581. return_value *= base;
  582. return_value += this_digit;
  583. string++;
  584. }
  585. /* All done, normal exit */
  586. *ret_integer = return_value;
  587. return_ACPI_STATUS(AE_OK);
  588. error_exit:
  589. /* Base was set/validated above */
  590. if (base == 10) {
  591. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  592. } else {
  593. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  594. }
  595. }
  596. /*******************************************************************************
  597. *
  598. * FUNCTION: acpi_ut_create_update_state_and_push
  599. *
  600. * PARAMETERS: Object - Object to be added to the new state
  601. * Action - Increment/Decrement
  602. * state_list - List the state will be added to
  603. *
  604. * RETURN: Status
  605. *
  606. * DESCRIPTION: Create a new state and push it
  607. *
  608. ******************************************************************************/
  609. acpi_status
  610. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  611. u16 action,
  612. union acpi_generic_state **state_list)
  613. {
  614. union acpi_generic_state *state;
  615. ACPI_FUNCTION_ENTRY();
  616. /* Ignore null objects; these are expected */
  617. if (!object) {
  618. return (AE_OK);
  619. }
  620. state = acpi_ut_create_update_state(object, action);
  621. if (!state) {
  622. return (AE_NO_MEMORY);
  623. }
  624. acpi_ut_push_generic_state(state_list, state);
  625. return (AE_OK);
  626. }
  627. /*******************************************************************************
  628. *
  629. * FUNCTION: acpi_ut_walk_package_tree
  630. *
  631. * PARAMETERS: source_object - The package to walk
  632. * target_object - Target object (if package is being copied)
  633. * walk_callback - Called once for each package element
  634. * Context - Passed to the callback function
  635. *
  636. * RETURN: Status
  637. *
  638. * DESCRIPTION: Walk through a package
  639. *
  640. ******************************************************************************/
  641. acpi_status
  642. acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
  643. void *target_object,
  644. acpi_pkg_callback walk_callback, void *context)
  645. {
  646. acpi_status status = AE_OK;
  647. union acpi_generic_state *state_list = NULL;
  648. union acpi_generic_state *state;
  649. u32 this_index;
  650. union acpi_operand_object *this_source_obj;
  651. ACPI_FUNCTION_TRACE("ut_walk_package_tree");
  652. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  653. if (!state) {
  654. return_ACPI_STATUS(AE_NO_MEMORY);
  655. }
  656. while (state) {
  657. /* Get one element of the package */
  658. this_index = state->pkg.index;
  659. this_source_obj = (union acpi_operand_object *)
  660. state->pkg.source_object->package.elements[this_index];
  661. /*
  662. * Check for:
  663. * 1) An uninitialized package element. It is completely
  664. * legal to declare a package and leave it uninitialized
  665. * 2) Not an internal object - can be a namespace node instead
  666. * 3) Any type other than a package. Packages are handled in else
  667. * case below.
  668. */
  669. if ((!this_source_obj) ||
  670. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  671. ACPI_DESC_TYPE_OPERAND)
  672. || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
  673. ACPI_TYPE_PACKAGE)) {
  674. status =
  675. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  676. this_source_obj, state, context);
  677. if (ACPI_FAILURE(status)) {
  678. return_ACPI_STATUS(status);
  679. }
  680. state->pkg.index++;
  681. while (state->pkg.index >=
  682. state->pkg.source_object->package.count) {
  683. /*
  684. * We've handled all of the objects at this level, This means
  685. * that we have just completed a package. That package may
  686. * have contained one or more packages itself.
  687. *
  688. * Delete this state and pop the previous state (package).
  689. */
  690. acpi_ut_delete_generic_state(state);
  691. state = acpi_ut_pop_generic_state(&state_list);
  692. /* Finished when there are no more states */
  693. if (!state) {
  694. /*
  695. * We have handled all of the objects in the top level
  696. * package just add the length of the package objects
  697. * and exit
  698. */
  699. return_ACPI_STATUS(AE_OK);
  700. }
  701. /*
  702. * Go back up a level and move the index past the just
  703. * completed package object.
  704. */
  705. state->pkg.index++;
  706. }
  707. } else {
  708. /* This is a subobject of type package */
  709. status =
  710. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  711. this_source_obj, state, context);
  712. if (ACPI_FAILURE(status)) {
  713. return_ACPI_STATUS(status);
  714. }
  715. /*
  716. * Push the current state and create a new one
  717. * The callback above returned a new target package object.
  718. */
  719. acpi_ut_push_generic_state(&state_list, state);
  720. state = acpi_ut_create_pkg_state(this_source_obj,
  721. state->pkg.
  722. this_target_obj, 0);
  723. if (!state) {
  724. return_ACPI_STATUS(AE_NO_MEMORY);
  725. }
  726. }
  727. }
  728. /* We should never get here */
  729. return_ACPI_STATUS(AE_AML_INTERNAL);
  730. }
  731. /*******************************************************************************
  732. *
  733. * FUNCTION: acpi_ut_error, acpi_ut_warning, acpi_ut_info
  734. *
  735. * PARAMETERS: module_name - Caller's module name (for error output)
  736. * line_number - Caller's line number (for error output)
  737. * Format - Printf format string + additional args
  738. *
  739. * RETURN: None
  740. *
  741. * DESCRIPTION: Print message with module/line/version info
  742. *
  743. ******************************************************************************/
  744. void ACPI_INTERNAL_VAR_XFACE
  745. acpi_ut_error(char *module_name, u32 line_number, char *format, ...)
  746. {
  747. va_list args;
  748. acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
  749. va_start(args, format);
  750. acpi_os_vprintf(format, args);
  751. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  752. }
  753. void ACPI_INTERNAL_VAR_XFACE
  754. acpi_ut_exception(char *module_name,
  755. u32 line_number, acpi_status status, char *format, ...)
  756. {
  757. va_list args;
  758. acpi_os_printf("ACPI Exception (%s-%04d): %s, ", module_name,
  759. line_number, acpi_format_exception(status));
  760. va_start(args, format);
  761. acpi_os_vprintf(format, args);
  762. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  763. }
  764. void ACPI_INTERNAL_VAR_XFACE
  765. acpi_ut_warning(char *module_name, u32 line_number, char *format, ...)
  766. {
  767. va_list args;
  768. acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
  769. va_start(args, format);
  770. acpi_os_vprintf(format, args);
  771. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  772. }
  773. void ACPI_INTERNAL_VAR_XFACE
  774. acpi_ut_info(char *module_name, u32 line_number, char *format, ...)
  775. {
  776. va_list args;
  777. acpi_os_printf("ACPI (%s-%04d): ", module_name, line_number);
  778. va_start(args, format);
  779. acpi_os_vprintf(format, args);
  780. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  781. }