utmisc.c 27 KB

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