utmisc.c 28 KB

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