utmisc.c 26 KB

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