utmisc.c 26 KB

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