utmisc.c 27 KB

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