xref: /linux/drivers/acpi/acpica/utmisc.c (revision 08ec212c0f92cbf30e3ecc7349f18151714041d6)
1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2012, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <linux/module.h>
45 
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48 #include "acnamesp.h"
49 
50 #define _COMPONENT          ACPI_UTILITIES
51 ACPI_MODULE_NAME("utmisc")
52 
53 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
54 /*******************************************************************************
55  *
56  * FUNCTION:    ut_convert_backslashes
57  *
58  * PARAMETERS:  pathname        - File pathname string to be converted
59  *
60  * RETURN:      Modifies the input Pathname
61  *
62  * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
63  *              the entire input file pathname string.
64  *
65  ******************************************************************************/
66 void ut_convert_backslashes(char *pathname)
67 {
68 
69 	if (!pathname) {
70 		return;
71 	}
72 
73 	while (*pathname) {
74 		if (*pathname == '\\') {
75 			*pathname = '/';
76 		}
77 
78 		pathname++;
79 	}
80 }
81 #endif
82 
83 /*******************************************************************************
84  *
85  * FUNCTION:    acpi_ut_is_pci_root_bridge
86  *
87  * PARAMETERS:  id              - The HID/CID in string format
88  *
89  * RETURN:      TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
90  *
91  * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
92  *
93  ******************************************************************************/
94 
95 u8 acpi_ut_is_pci_root_bridge(char *id)
96 {
97 
98 	/*
99 	 * Check if this is a PCI root bridge.
100 	 * ACPI 3.0+: check for a PCI Express root also.
101 	 */
102 	if (!(ACPI_STRCMP(id,
103 			  PCI_ROOT_HID_STRING)) ||
104 	    !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
105 		return (TRUE);
106 	}
107 
108 	return (FALSE);
109 }
110 
111 /*******************************************************************************
112  *
113  * FUNCTION:    acpi_ut_is_aml_table
114  *
115  * PARAMETERS:  table               - An ACPI table
116  *
117  * RETURN:      TRUE if table contains executable AML; FALSE otherwise
118  *
119  * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
120  *              Currently, these are DSDT,SSDT,PSDT. All other table types are
121  *              data tables that do not contain AML code.
122  *
123  ******************************************************************************/
124 
125 u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
126 {
127 
128 	/* These are the only tables that contain executable AML */
129 
130 	if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
131 	    ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
132 	    ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
133 		return (TRUE);
134 	}
135 
136 	return (FALSE);
137 }
138 
139 /*******************************************************************************
140  *
141  * FUNCTION:    acpi_ut_allocate_owner_id
142  *
143  * PARAMETERS:  owner_id        - Where the new owner ID is returned
144  *
145  * RETURN:      Status
146  *
147  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
148  *              track objects created by the table or method, to be deleted
149  *              when the method exits or the table is unloaded.
150  *
151  ******************************************************************************/
152 
153 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
154 {
155 	u32 i;
156 	u32 j;
157 	u32 k;
158 	acpi_status status;
159 
160 	ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
161 
162 	/* Guard against multiple allocations of ID to the same location */
163 
164 	if (*owner_id) {
165 		ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
166 			    *owner_id));
167 		return_ACPI_STATUS(AE_ALREADY_EXISTS);
168 	}
169 
170 	/* Mutex for the global ID mask */
171 
172 	status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
173 	if (ACPI_FAILURE(status)) {
174 		return_ACPI_STATUS(status);
175 	}
176 
177 	/*
178 	 * Find a free owner ID, cycle through all possible IDs on repeated
179 	 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
180 	 * to be scanned twice.
181 	 */
182 	for (i = 0, j = acpi_gbl_last_owner_id_index;
183 	     i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
184 		if (j >= ACPI_NUM_OWNERID_MASKS) {
185 			j = 0;	/* Wraparound to start of mask array */
186 		}
187 
188 		for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
189 			if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
190 
191 				/* There are no free IDs in this mask */
192 
193 				break;
194 			}
195 
196 			if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
197 				/*
198 				 * Found a free ID. The actual ID is the bit index plus one,
199 				 * making zero an invalid Owner ID. Save this as the last ID
200 				 * allocated and update the global ID mask.
201 				 */
202 				acpi_gbl_owner_id_mask[j] |= (1 << k);
203 
204 				acpi_gbl_last_owner_id_index = (u8) j;
205 				acpi_gbl_next_owner_id_offset = (u8) (k + 1);
206 
207 				/*
208 				 * Construct encoded ID from the index and bit position
209 				 *
210 				 * Note: Last [j].k (bit 255) is never used and is marked
211 				 * permanently allocated (prevents +1 overflow)
212 				 */
213 				*owner_id =
214 				    (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
215 
216 				ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
217 						  "Allocated OwnerId: %2.2X\n",
218 						  (unsigned int)*owner_id));
219 				goto exit;
220 			}
221 		}
222 
223 		acpi_gbl_next_owner_id_offset = 0;
224 	}
225 
226 	/*
227 	 * All owner_ids have been allocated. This typically should
228 	 * not happen since the IDs are reused after deallocation. The IDs are
229 	 * allocated upon table load (one per table) and method execution, and
230 	 * they are released when a table is unloaded or a method completes
231 	 * execution.
232 	 *
233 	 * If this error happens, there may be very deep nesting of invoked control
234 	 * methods, or there may be a bug where the IDs are not released.
235 	 */
236 	status = AE_OWNER_ID_LIMIT;
237 	ACPI_ERROR((AE_INFO,
238 		    "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
239 
240       exit:
241 	(void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
242 	return_ACPI_STATUS(status);
243 }
244 
245 /*******************************************************************************
246  *
247  * FUNCTION:    acpi_ut_release_owner_id
248  *
249  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_ID
250  *
251  * RETURN:      None. No error is returned because we are either exiting a
252  *              control method or unloading a table. Either way, we would
253  *              ignore any error anyway.
254  *
255  * DESCRIPTION: Release a table or method owner ID.  Valid IDs are 1 - 255
256  *
257  ******************************************************************************/
258 
259 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
260 {
261 	acpi_owner_id owner_id = *owner_id_ptr;
262 	acpi_status status;
263 	u32 index;
264 	u32 bit;
265 
266 	ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
267 
268 	/* Always clear the input owner_id (zero is an invalid ID) */
269 
270 	*owner_id_ptr = 0;
271 
272 	/* Zero is not a valid owner_ID */
273 
274 	if (owner_id == 0) {
275 		ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
276 		return_VOID;
277 	}
278 
279 	/* Mutex for the global ID mask */
280 
281 	status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
282 	if (ACPI_FAILURE(status)) {
283 		return_VOID;
284 	}
285 
286 	/* Normalize the ID to zero */
287 
288 	owner_id--;
289 
290 	/* Decode ID to index/offset pair */
291 
292 	index = ACPI_DIV_32(owner_id);
293 	bit = 1 << ACPI_MOD_32(owner_id);
294 
295 	/* Free the owner ID only if it is valid */
296 
297 	if (acpi_gbl_owner_id_mask[index] & bit) {
298 		acpi_gbl_owner_id_mask[index] ^= bit;
299 	} else {
300 		ACPI_ERROR((AE_INFO,
301 			    "Release of non-allocated OwnerId: 0x%2.2X",
302 			    owner_id + 1));
303 	}
304 
305 	(void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
306 	return_VOID;
307 }
308 
309 /*******************************************************************************
310  *
311  * FUNCTION:    acpi_ut_strupr (strupr)
312  *
313  * PARAMETERS:  src_string      - The source string to convert
314  *
315  * RETURN:      None
316  *
317  * DESCRIPTION: Convert string to uppercase
318  *
319  * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
320  *
321  ******************************************************************************/
322 
323 void acpi_ut_strupr(char *src_string)
324 {
325 	char *string;
326 
327 	ACPI_FUNCTION_ENTRY();
328 
329 	if (!src_string) {
330 		return;
331 	}
332 
333 	/* Walk entire string, uppercasing the letters */
334 
335 	for (string = src_string; *string; string++) {
336 		*string = (char)ACPI_TOUPPER(*string);
337 	}
338 
339 	return;
340 }
341 
342 /*******************************************************************************
343  *
344  * FUNCTION:    acpi_ut_print_string
345  *
346  * PARAMETERS:  string          - Null terminated ASCII string
347  *              max_length      - Maximum output length
348  *
349  * RETURN:      None
350  *
351  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
352  *              sequences.
353  *
354  ******************************************************************************/
355 
356 void acpi_ut_print_string(char *string, u8 max_length)
357 {
358 	u32 i;
359 
360 	if (!string) {
361 		acpi_os_printf("<\"NULL STRING PTR\">");
362 		return;
363 	}
364 
365 	acpi_os_printf("\"");
366 	for (i = 0; string[i] && (i < max_length); i++) {
367 
368 		/* Escape sequences */
369 
370 		switch (string[i]) {
371 		case 0x07:
372 			acpi_os_printf("\\a");	/* BELL */
373 			break;
374 
375 		case 0x08:
376 			acpi_os_printf("\\b");	/* BACKSPACE */
377 			break;
378 
379 		case 0x0C:
380 			acpi_os_printf("\\f");	/* FORMFEED */
381 			break;
382 
383 		case 0x0A:
384 			acpi_os_printf("\\n");	/* LINEFEED */
385 			break;
386 
387 		case 0x0D:
388 			acpi_os_printf("\\r");	/* CARRIAGE RETURN */
389 			break;
390 
391 		case 0x09:
392 			acpi_os_printf("\\t");	/* HORIZONTAL TAB */
393 			break;
394 
395 		case 0x0B:
396 			acpi_os_printf("\\v");	/* VERTICAL TAB */
397 			break;
398 
399 		case '\'':	/* Single Quote */
400 		case '\"':	/* Double Quote */
401 		case '\\':	/* Backslash */
402 			acpi_os_printf("\\%c", (int)string[i]);
403 			break;
404 
405 		default:
406 
407 			/* Check for printable character or hex escape */
408 
409 			if (ACPI_IS_PRINT(string[i])) {
410 				/* This is a normal character */
411 
412 				acpi_os_printf("%c", (int)string[i]);
413 			} else {
414 				/* All others will be Hex escapes */
415 
416 				acpi_os_printf("\\x%2.2X", (s32) string[i]);
417 			}
418 			break;
419 		}
420 	}
421 	acpi_os_printf("\"");
422 
423 	if (i == max_length && string[i]) {
424 		acpi_os_printf("...");
425 	}
426 }
427 
428 /*******************************************************************************
429  *
430  * FUNCTION:    acpi_ut_dword_byte_swap
431  *
432  * PARAMETERS:  value           - Value to be converted
433  *
434  * RETURN:      u32 integer with bytes swapped
435  *
436  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
437  *
438  ******************************************************************************/
439 
440 u32 acpi_ut_dword_byte_swap(u32 value)
441 {
442 	union {
443 		u32 value;
444 		u8 bytes[4];
445 	} out;
446 	union {
447 		u32 value;
448 		u8 bytes[4];
449 	} in;
450 
451 	ACPI_FUNCTION_ENTRY();
452 
453 	in.value = value;
454 
455 	out.bytes[0] = in.bytes[3];
456 	out.bytes[1] = in.bytes[2];
457 	out.bytes[2] = in.bytes[1];
458 	out.bytes[3] = in.bytes[0];
459 
460 	return (out.value);
461 }
462 
463 /*******************************************************************************
464  *
465  * FUNCTION:    acpi_ut_set_integer_width
466  *
467  * PARAMETERS:  Revision            From DSDT header
468  *
469  * RETURN:      None
470  *
471  * DESCRIPTION: Set the global integer bit width based upon the revision
472  *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
473  *              For Revision 2 and above, Integers are 64 bits.  Yes, this
474  *              makes a difference.
475  *
476  ******************************************************************************/
477 
478 void acpi_ut_set_integer_width(u8 revision)
479 {
480 
481 	if (revision < 2) {
482 
483 		/* 32-bit case */
484 
485 		acpi_gbl_integer_bit_width = 32;
486 		acpi_gbl_integer_nybble_width = 8;
487 		acpi_gbl_integer_byte_width = 4;
488 	} else {
489 		/* 64-bit case (ACPI 2.0+) */
490 
491 		acpi_gbl_integer_bit_width = 64;
492 		acpi_gbl_integer_nybble_width = 16;
493 		acpi_gbl_integer_byte_width = 8;
494 	}
495 }
496 
497 #ifdef ACPI_DEBUG_OUTPUT
498 /*******************************************************************************
499  *
500  * FUNCTION:    acpi_ut_display_init_pathname
501  *
502  * PARAMETERS:  type                - Object type of the node
503  *              obj_handle          - Handle whose pathname will be displayed
504  *              path                - Additional path string to be appended.
505  *                                      (NULL if no extra path)
506  *
507  * RETURN:      acpi_status
508  *
509  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
510  *
511  ******************************************************************************/
512 
513 void
514 acpi_ut_display_init_pathname(u8 type,
515 			      struct acpi_namespace_node *obj_handle,
516 			      char *path)
517 {
518 	acpi_status status;
519 	struct acpi_buffer buffer;
520 
521 	ACPI_FUNCTION_ENTRY();
522 
523 	/* Only print the path if the appropriate debug level is enabled */
524 
525 	if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
526 		return;
527 	}
528 
529 	/* Get the full pathname to the node */
530 
531 	buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
532 	status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
533 	if (ACPI_FAILURE(status)) {
534 		return;
535 	}
536 
537 	/* Print what we're doing */
538 
539 	switch (type) {
540 	case ACPI_TYPE_METHOD:
541 		acpi_os_printf("Executing  ");
542 		break;
543 
544 	default:
545 		acpi_os_printf("Initializing ");
546 		break;
547 	}
548 
549 	/* Print the object type and pathname */
550 
551 	acpi_os_printf("%-12s %s",
552 		       acpi_ut_get_type_name(type), (char *)buffer.pointer);
553 
554 	/* Extra path is used to append names like _STA, _INI, etc. */
555 
556 	if (path) {
557 		acpi_os_printf(".%s", path);
558 	}
559 	acpi_os_printf("\n");
560 
561 	ACPI_FREE(buffer.pointer);
562 }
563 #endif
564 
565 /*******************************************************************************
566  *
567  * FUNCTION:    acpi_ut_valid_acpi_char
568  *
569  * PARAMETERS:  char            - The character to be examined
570  *              position        - Byte position (0-3)
571  *
572  * RETURN:      TRUE if the character is valid, FALSE otherwise
573  *
574  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
575  *              1) Upper case alpha
576  *              2) numeric
577  *              3) underscore
578  *
579  *              We allow a '!' as the last character because of the ASF! table
580  *
581  ******************************************************************************/
582 
583 u8 acpi_ut_valid_acpi_char(char character, u32 position)
584 {
585 
586 	if (!((character >= 'A' && character <= 'Z') ||
587 	      (character >= '0' && character <= '9') || (character == '_'))) {
588 
589 		/* Allow a '!' in the last position */
590 
591 		if (character == '!' && position == 3) {
592 			return (TRUE);
593 		}
594 
595 		return (FALSE);
596 	}
597 
598 	return (TRUE);
599 }
600 
601 /*******************************************************************************
602  *
603  * FUNCTION:    acpi_ut_valid_acpi_name
604  *
605  * PARAMETERS:  name            - The name to be examined
606  *
607  * RETURN:      TRUE if the name is valid, FALSE otherwise
608  *
609  * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
610  *              1) Upper case alpha
611  *              2) numeric
612  *              3) underscore
613  *
614  ******************************************************************************/
615 
616 u8 acpi_ut_valid_acpi_name(u32 name)
617 {
618 	u32 i;
619 
620 	ACPI_FUNCTION_ENTRY();
621 
622 	for (i = 0; i < ACPI_NAME_SIZE; i++) {
623 		if (!acpi_ut_valid_acpi_char
624 		    ((ACPI_CAST_PTR(char, &name))[i], i)) {
625 			return (FALSE);
626 		}
627 	}
628 
629 	return (TRUE);
630 }
631 
632 /*******************************************************************************
633  *
634  * FUNCTION:    acpi_ut_repair_name
635  *
636  * PARAMETERS:  name            - The ACPI name to be repaired
637  *
638  * RETURN:      Repaired version of the name
639  *
640  * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
641  *              return the new name.
642  *
643  ******************************************************************************/
644 
645 acpi_name acpi_ut_repair_name(char *name)
646 {
647        u32 i;
648 	char new_name[ACPI_NAME_SIZE];
649 
650 	for (i = 0; i < ACPI_NAME_SIZE; i++) {
651 		new_name[i] = name[i];
652 
653 		/*
654 		 * Replace a bad character with something printable, yet technically
655 		 * still invalid. This prevents any collisions with existing "good"
656 		 * names in the namespace.
657 		 */
658 		if (!acpi_ut_valid_acpi_char(name[i], i)) {
659 			new_name[i] = '*';
660 		}
661 	}
662 
663 	return (*(u32 *) new_name);
664 }
665 
666 /*******************************************************************************
667  *
668  * FUNCTION:    acpi_ut_strtoul64
669  *
670  * PARAMETERS:  string          - Null terminated string
671  *              base            - Radix of the string: 16 or ACPI_ANY_BASE;
672  *                                ACPI_ANY_BASE means 'in behalf of to_integer'
673  *              ret_integer     - Where the converted integer is returned
674  *
675  * RETURN:      Status and Converted value
676  *
677  * DESCRIPTION: Convert a string into an unsigned value. Performs either a
678  *              32-bit or 64-bit conversion, depending on the current mode
679  *              of the interpreter.
680  *              NOTE: Does not support Octal strings, not needed.
681  *
682  ******************************************************************************/
683 
684 acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
685 {
686 	u32 this_digit = 0;
687 	u64 return_value = 0;
688 	u64 quotient;
689 	u64 dividend;
690 	u32 to_integer_op = (base == ACPI_ANY_BASE);
691 	u32 mode32 = (acpi_gbl_integer_byte_width == 4);
692 	u8 valid_digits = 0;
693 	u8 sign_of0x = 0;
694 	u8 term = 0;
695 
696 	ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
697 
698 	switch (base) {
699 	case ACPI_ANY_BASE:
700 	case 16:
701 		break;
702 
703 	default:
704 		/* Invalid Base */
705 		return_ACPI_STATUS(AE_BAD_PARAMETER);
706 	}
707 
708 	if (!string) {
709 		goto error_exit;
710 	}
711 
712 	/* Skip over any white space in the buffer */
713 
714 	while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
715 		string++;
716 	}
717 
718 	if (to_integer_op) {
719 		/*
720 		 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
721 		 * We need to determine if it is decimal or hexadecimal.
722 		 */
723 		if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
724 			sign_of0x = 1;
725 			base = 16;
726 
727 			/* Skip over the leading '0x' */
728 			string += 2;
729 		} else {
730 			base = 10;
731 		}
732 	}
733 
734 	/* Any string left? Check that '0x' is not followed by white space. */
735 
736 	if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
737 		if (to_integer_op) {
738 			goto error_exit;
739 		} else {
740 			goto all_done;
741 		}
742 	}
743 
744 	/*
745 	 * Perform a 32-bit or 64-bit conversion, depending upon the current
746 	 * execution mode of the interpreter
747 	 */
748 	dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
749 
750 	/* Main loop: convert the string to a 32- or 64-bit integer */
751 
752 	while (*string) {
753 		if (ACPI_IS_DIGIT(*string)) {
754 
755 			/* Convert ASCII 0-9 to Decimal value */
756 
757 			this_digit = ((u8) * string) - '0';
758 		} else if (base == 10) {
759 
760 			/* Digit is out of range; possible in to_integer case only */
761 
762 			term = 1;
763 		} else {
764 			this_digit = (u8) ACPI_TOUPPER(*string);
765 			if (ACPI_IS_XDIGIT((char)this_digit)) {
766 
767 				/* Convert ASCII Hex char to value */
768 
769 				this_digit = this_digit - 'A' + 10;
770 			} else {
771 				term = 1;
772 			}
773 		}
774 
775 		if (term) {
776 			if (to_integer_op) {
777 				goto error_exit;
778 			} else {
779 				break;
780 			}
781 		} else if ((valid_digits == 0) && (this_digit == 0)
782 			   && !sign_of0x) {
783 
784 			/* Skip zeros */
785 			string++;
786 			continue;
787 		}
788 
789 		valid_digits++;
790 
791 		if (sign_of0x && ((valid_digits > 16)
792 				  || ((valid_digits > 8) && mode32))) {
793 			/*
794 			 * This is to_integer operation case.
795 			 * No any restrictions for string-to-integer conversion,
796 			 * see ACPI spec.
797 			 */
798 			goto error_exit;
799 		}
800 
801 		/* Divide the digit into the correct position */
802 
803 		(void)acpi_ut_short_divide((dividend - (u64) this_digit),
804 					   base, &quotient, NULL);
805 
806 		if (return_value > quotient) {
807 			if (to_integer_op) {
808 				goto error_exit;
809 			} else {
810 				break;
811 			}
812 		}
813 
814 		return_value *= base;
815 		return_value += this_digit;
816 		string++;
817 	}
818 
819 	/* All done, normal exit */
820 
821       all_done:
822 
823 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
824 			  ACPI_FORMAT_UINT64(return_value)));
825 
826 	*ret_integer = return_value;
827 	return_ACPI_STATUS(AE_OK);
828 
829       error_exit:
830 	/* Base was set/validated above */
831 
832 	if (base == 10) {
833 		return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
834 	} else {
835 		return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
836 	}
837 }
838 
839 /*******************************************************************************
840  *
841  * FUNCTION:    acpi_ut_create_update_state_and_push
842  *
843  * PARAMETERS:  object          - Object to be added to the new state
844  *              action          - Increment/Decrement
845  *              state_list      - List the state will be added to
846  *
847  * RETURN:      Status
848  *
849  * DESCRIPTION: Create a new state and push it
850  *
851  ******************************************************************************/
852 
853 acpi_status
854 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
855 				     u16 action,
856 				     union acpi_generic_state **state_list)
857 {
858 	union acpi_generic_state *state;
859 
860 	ACPI_FUNCTION_ENTRY();
861 
862 	/* Ignore null objects; these are expected */
863 
864 	if (!object) {
865 		return (AE_OK);
866 	}
867 
868 	state = acpi_ut_create_update_state(object, action);
869 	if (!state) {
870 		return (AE_NO_MEMORY);
871 	}
872 
873 	acpi_ut_push_generic_state(state_list, state);
874 	return (AE_OK);
875 }
876 
877 /*******************************************************************************
878  *
879  * FUNCTION:    acpi_ut_walk_package_tree
880  *
881  * PARAMETERS:  source_object       - The package to walk
882  *              target_object       - Target object (if package is being copied)
883  *              walk_callback       - Called once for each package element
884  *              context             - Passed to the callback function
885  *
886  * RETURN:      Status
887  *
888  * DESCRIPTION: Walk through a package
889  *
890  ******************************************************************************/
891 
892 acpi_status
893 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
894 			  void *target_object,
895 			  acpi_pkg_callback walk_callback, void *context)
896 {
897 	acpi_status status = AE_OK;
898 	union acpi_generic_state *state_list = NULL;
899 	union acpi_generic_state *state;
900 	u32 this_index;
901 	union acpi_operand_object *this_source_obj;
902 
903 	ACPI_FUNCTION_TRACE(ut_walk_package_tree);
904 
905 	state = acpi_ut_create_pkg_state(source_object, target_object, 0);
906 	if (!state) {
907 		return_ACPI_STATUS(AE_NO_MEMORY);
908 	}
909 
910 	while (state) {
911 
912 		/* Get one element of the package */
913 
914 		this_index = state->pkg.index;
915 		this_source_obj = (union acpi_operand_object *)
916 		    state->pkg.source_object->package.elements[this_index];
917 
918 		/*
919 		 * Check for:
920 		 * 1) An uninitialized package element.  It is completely
921 		 *    legal to declare a package and leave it uninitialized
922 		 * 2) Not an internal object - can be a namespace node instead
923 		 * 3) Any type other than a package.  Packages are handled in else
924 		 *    case below.
925 		 */
926 		if ((!this_source_obj) ||
927 		    (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
928 		     ACPI_DESC_TYPE_OPERAND)
929 		    || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
930 			status =
931 			    walk_callback(ACPI_COPY_TYPE_SIMPLE,
932 					  this_source_obj, state, context);
933 			if (ACPI_FAILURE(status)) {
934 				return_ACPI_STATUS(status);
935 			}
936 
937 			state->pkg.index++;
938 			while (state->pkg.index >=
939 			       state->pkg.source_object->package.count) {
940 				/*
941 				 * We've handled all of the objects at this level,  This means
942 				 * that we have just completed a package.  That package may
943 				 * have contained one or more packages itself.
944 				 *
945 				 * Delete this state and pop the previous state (package).
946 				 */
947 				acpi_ut_delete_generic_state(state);
948 				state = acpi_ut_pop_generic_state(&state_list);
949 
950 				/* Finished when there are no more states */
951 
952 				if (!state) {
953 					/*
954 					 * We have handled all of the objects in the top level
955 					 * package just add the length of the package objects
956 					 * and exit
957 					 */
958 					return_ACPI_STATUS(AE_OK);
959 				}
960 
961 				/*
962 				 * Go back up a level and move the index past the just
963 				 * completed package object.
964 				 */
965 				state->pkg.index++;
966 			}
967 		} else {
968 			/* This is a subobject of type package */
969 
970 			status =
971 			    walk_callback(ACPI_COPY_TYPE_PACKAGE,
972 					  this_source_obj, state, context);
973 			if (ACPI_FAILURE(status)) {
974 				return_ACPI_STATUS(status);
975 			}
976 
977 			/*
978 			 * Push the current state and create a new one
979 			 * The callback above returned a new target package object.
980 			 */
981 			acpi_ut_push_generic_state(&state_list, state);
982 			state = acpi_ut_create_pkg_state(this_source_obj,
983 							 state->pkg.
984 							 this_target_obj, 0);
985 			if (!state) {
986 
987 				/* Free any stacked Update State objects */
988 
989 				while (state_list) {
990 					state =
991 					    acpi_ut_pop_generic_state
992 					    (&state_list);
993 					acpi_ut_delete_generic_state(state);
994 				}
995 				return_ACPI_STATUS(AE_NO_MEMORY);
996 			}
997 		}
998 	}
999 
1000 	/* We should never get here */
1001 
1002 	return_ACPI_STATUS(AE_AML_INTERNAL);
1003 }
1004