1 /******************************************************************************
3 * Module Name: utcopy - Internal to external object translation utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2004, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/amlcode.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utcopy")
53 /*******************************************************************************
55 * FUNCTION: acpi_ut_copy_isimple_to_esimple
57 * PARAMETERS: *internal_object - Pointer to the object we are examining
58 * *Buffer - Where the object is returned
59 * *space_used - Where the data length is returned
63 * DESCRIPTION: This function is called to place a simple object in a user
66 * The buffer is assumed to have sufficient space for the object.
68 ******************************************************************************/
71 acpi_ut_copy_isimple_to_esimple (
72 union acpi_operand_object *internal_object,
73 union acpi_object *external_object,
75 acpi_size *buffer_space_used)
77 acpi_status status = AE_OK;
80 ACPI_FUNCTION_TRACE ("ut_copy_isimple_to_esimple");
83 *buffer_space_used = 0;
86 * Check for NULL object case (could be an uninitialized
89 if (!internal_object) {
90 return_ACPI_STATUS (AE_OK);
93 /* Always clear the external object */
95 ACPI_MEMSET (external_object, 0, sizeof (union acpi_object));
98 * In general, the external object will be the same type as
101 external_object->type = ACPI_GET_OBJECT_TYPE (internal_object);
103 /* However, only a limited number of external types are supported */
105 switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
106 case ACPI_TYPE_STRING:
108 external_object->string.pointer = (char *) data_space;
109 external_object->string.length = internal_object->string.length;
110 *buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD ((acpi_size) internal_object->string.length + 1);
112 ACPI_MEMCPY ((void *) data_space, (void *) internal_object->string.pointer,
113 (acpi_size) internal_object->string.length + 1);
117 case ACPI_TYPE_BUFFER:
119 external_object->buffer.pointer = data_space;
120 external_object->buffer.length = internal_object->buffer.length;
121 *buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD (internal_object->string.length);
123 ACPI_MEMCPY ((void *) data_space, (void *) internal_object->buffer.pointer,
124 internal_object->buffer.length);
128 case ACPI_TYPE_INTEGER:
130 external_object->integer.value = internal_object->integer.value;
134 case ACPI_TYPE_LOCAL_REFERENCE:
137 * This is an object reference. Attempt to dereference it.
139 switch (internal_object->reference.opcode) {
140 case AML_INT_NAMEPATH_OP:
142 /* For namepath, return the object handle ("reference") */
146 * Use the object type of "Any" to indicate a reference
147 * to object containing a handle to an ACPI named object.
149 external_object->type = ACPI_TYPE_ANY;
150 external_object->reference.handle = internal_object->reference.node;
156 case ACPI_TYPE_PROCESSOR:
158 external_object->processor.proc_id = internal_object->processor.proc_id;
159 external_object->processor.pblk_address = internal_object->processor.address;
160 external_object->processor.pblk_length = internal_object->processor.length;
164 case ACPI_TYPE_POWER:
166 external_object->power_resource.system_level =
167 internal_object->power_resource.system_level;
169 external_object->power_resource.resource_order =
170 internal_object->power_resource.resource_order;
176 * There is no corresponding external object type
178 return_ACPI_STATUS (AE_SUPPORT);
181 return_ACPI_STATUS (status);
185 /*******************************************************************************
187 * FUNCTION: acpi_ut_copy_ielement_to_eelement
189 * PARAMETERS: acpi_pkg_callback
193 * DESCRIPTION: Copy one package element to another package element
195 ******************************************************************************/
198 acpi_ut_copy_ielement_to_eelement (
200 union acpi_operand_object *source_object,
201 union acpi_generic_state *state,
204 acpi_status status = AE_OK;
205 struct acpi_pkg_info *info = (struct acpi_pkg_info *) context;
206 acpi_size object_space;
208 union acpi_object *target_object;
211 ACPI_FUNCTION_ENTRY ();
214 this_index = state->pkg.index;
215 target_object = (union acpi_object *)
216 &((union acpi_object *)(state->pkg.dest_object))->package.elements[this_index];
218 switch (object_type) {
219 case ACPI_COPY_TYPE_SIMPLE:
222 * This is a simple or null object
224 status = acpi_ut_copy_isimple_to_esimple (source_object,
225 target_object, info->free_space, &object_space);
226 if (ACPI_FAILURE (status)) {
232 case ACPI_COPY_TYPE_PACKAGE:
235 * Build the package object
237 target_object->type = ACPI_TYPE_PACKAGE;
238 target_object->package.count = source_object->package.count;
239 target_object->package.elements = ACPI_CAST_PTR (union acpi_object, info->free_space);
242 * Pass the new package object back to the package walk routine
244 state->pkg.this_target_obj = target_object;
247 * Save space for the array of objects (Package elements)
248 * update the buffer length counter
250 object_space = ACPI_ROUND_UP_TO_NATIVE_WORD (
251 (acpi_size) target_object->package.count * sizeof (union acpi_object));
256 return (AE_BAD_PARAMETER);
259 info->free_space += object_space;
260 info->length += object_space;
265 /*******************************************************************************
267 * FUNCTION: acpi_ut_copy_ipackage_to_epackage
269 * PARAMETERS: *internal_object - Pointer to the object we are returning
270 * *Buffer - Where the object is returned
271 * *space_used - Where the object length is returned
275 * DESCRIPTION: This function is called to place a package object in a user
276 * buffer. A package object by definition contains other objects.
278 * The buffer is assumed to have sufficient space for the object.
279 * The caller must have verified the buffer length needed using the
280 * acpi_ut_get_object_size function before calling this function.
282 ******************************************************************************/
285 acpi_ut_copy_ipackage_to_epackage (
286 union acpi_operand_object *internal_object,
288 acpi_size *space_used)
290 union acpi_object *external_object;
292 struct acpi_pkg_info info;
295 ACPI_FUNCTION_TRACE ("ut_copy_ipackage_to_epackage");
299 * First package at head of the buffer
301 external_object = ACPI_CAST_PTR (union acpi_object, buffer);
304 * Free space begins right after the first package
306 info.length = ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object));
307 info.free_space = buffer + ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object));
308 info.object_space = 0;
309 info.num_packages = 1;
311 external_object->type = ACPI_GET_OBJECT_TYPE (internal_object);
312 external_object->package.count = internal_object->package.count;
313 external_object->package.elements = ACPI_CAST_PTR (union acpi_object, info.free_space);
316 * Leave room for an array of ACPI_OBJECTS in the buffer
317 * and move the free space past it
319 info.length += (acpi_size) external_object->package.count *
320 ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object));
321 info.free_space += external_object->package.count *
322 ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object));
324 status = acpi_ut_walk_package_tree (internal_object, external_object,
325 acpi_ut_copy_ielement_to_eelement, &info);
327 *space_used = info.length;
328 return_ACPI_STATUS (status);
332 /*******************************************************************************
334 * FUNCTION: acpi_ut_copy_iobject_to_eobject
336 * PARAMETERS: *internal_object - The internal object to be converted
337 * *buffer_ptr - Where the object is returned
341 * DESCRIPTION: This function is called to build an API object to be returned to
344 ******************************************************************************/
347 acpi_ut_copy_iobject_to_eobject (
348 union acpi_operand_object *internal_object,
349 struct acpi_buffer *ret_buffer)
354 ACPI_FUNCTION_TRACE ("ut_copy_iobject_to_eobject");
357 if (ACPI_GET_OBJECT_TYPE (internal_object) == ACPI_TYPE_PACKAGE) {
359 * Package object: Copy all subobjects (including
362 status = acpi_ut_copy_ipackage_to_epackage (internal_object,
363 ret_buffer->pointer, &ret_buffer->length);
367 * Build a simple object (no nested objects)
369 status = acpi_ut_copy_isimple_to_esimple (internal_object,
370 (union acpi_object *) ret_buffer->pointer,
371 ((u8 *) ret_buffer->pointer +
372 ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object))),
373 &ret_buffer->length);
375 * build simple does not include the object size in the length
376 * so we add it in here
378 ret_buffer->length += sizeof (union acpi_object);
381 return_ACPI_STATUS (status);
385 /*******************************************************************************
387 * FUNCTION: acpi_ut_copy_esimple_to_isimple
389 * PARAMETERS: *external_object - The external object to be converted
390 * *internal_object - Where the internal object is returned
394 * DESCRIPTION: This function copies an external object to an internal one.
395 * NOTE: Pointers can be copied, we don't need to copy data.
396 * (The pointers have to be valid in our address space no matter
397 * what we do with them!)
399 ******************************************************************************/
402 acpi_ut_copy_esimple_to_isimple (
403 union acpi_object *external_object,
404 union acpi_operand_object **ret_internal_object)
406 union acpi_operand_object *internal_object;
409 ACPI_FUNCTION_TRACE ("ut_copy_esimple_to_isimple");
413 * Simple types supported are: String, Buffer, Integer
415 switch (external_object->type) {
416 case ACPI_TYPE_STRING:
417 case ACPI_TYPE_BUFFER:
418 case ACPI_TYPE_INTEGER:
420 internal_object = acpi_ut_create_internal_object ((u8) external_object->type);
421 if (!internal_object) {
422 return_ACPI_STATUS (AE_NO_MEMORY);
428 * Whatever other type -- it is not supported
430 return_ACPI_STATUS (AE_SUPPORT);
434 switch (external_object->type) {
436 /* Must COPY string and buffer contents */
438 case ACPI_TYPE_STRING:
440 internal_object->string.pointer =
441 ACPI_MEM_CALLOCATE ((acpi_size) external_object->string.length + 1);
442 if (!internal_object->string.pointer) {
443 return_ACPI_STATUS (AE_NO_MEMORY);
446 ACPI_MEMCPY (internal_object->string.pointer,
447 external_object->string.pointer,
448 external_object->string.length);
450 internal_object->string.length = external_object->string.length;
454 case ACPI_TYPE_BUFFER:
456 internal_object->buffer.pointer =
457 ACPI_MEM_CALLOCATE (external_object->buffer.length);
458 if (!internal_object->buffer.pointer) {
459 return_ACPI_STATUS (AE_NO_MEMORY);
462 ACPI_MEMCPY (internal_object->buffer.pointer,
463 external_object->buffer.pointer,
464 external_object->buffer.length);
466 internal_object->buffer.length = external_object->buffer.length;
470 case ACPI_TYPE_INTEGER:
472 internal_object->integer.value = external_object->integer.value;
476 /* Other types can't get here */
480 *ret_internal_object = internal_object;
481 return_ACPI_STATUS (AE_OK);
485 #ifdef ACPI_FUTURE_IMPLEMENTATION
487 /* Code to convert packages that are parameters to control methods */
489 /*******************************************************************************
491 * FUNCTION: acpi_ut_copy_epackage_to_ipackage
493 * PARAMETERS: *internal_object - Pointer to the object we are returning
494 * *Buffer - Where the object is returned
495 * *space_used - Where the length of the object is returned
499 * DESCRIPTION: This function is called to place a package object in a user
500 * buffer. A package object by definition contains other objects.
502 * The buffer is assumed to have sufficient space for the object.
503 * The caller must have verified the buffer length needed using the
504 * acpi_ut_get_object_size function before calling this function.
506 ******************************************************************************/
509 acpi_ut_copy_epackage_to_ipackage (
510 union acpi_operand_object *internal_object,
515 union acpi_object *external_object;
518 u32 object_space = 0;
519 union acpi_operand_object *this_internal_obj;
520 union acpi_object *this_external_obj;
523 ACPI_FUNCTION_TRACE ("ut_copy_epackage_to_ipackage");
527 * First package at head of the buffer
529 external_object = (union acpi_object *)buffer;
532 * Free space begins right after the first package
534 free_space = buffer + sizeof(union acpi_object);
537 external_object->type = ACPI_GET_OBJECT_TYPE (internal_object);
538 external_object->package.count = internal_object->package.count;
539 external_object->package.elements = (union acpi_object *)free_space;
542 * Build an array of ACPI_OBJECTS in the buffer
543 * and move the free space past it
545 free_space += external_object->package.count * sizeof(union acpi_object);
548 /* Call walk_package */
552 #endif /* Future implementation */
555 /*******************************************************************************
557 * FUNCTION: acpi_ut_copy_eobject_to_iobject
559 * PARAMETERS: *internal_object - The external object to be converted
560 * *buffer_ptr - Where the internal object is returned
562 * RETURN: Status - the status of the call
564 * DESCRIPTION: Converts an external object to an internal object.
566 ******************************************************************************/
569 acpi_ut_copy_eobject_to_iobject (
570 union acpi_object *external_object,
571 union acpi_operand_object **internal_object)
576 ACPI_FUNCTION_TRACE ("ut_copy_eobject_to_iobject");
579 if (external_object->type == ACPI_TYPE_PACKAGE) {
581 * Packages as external input to control methods are not supported,
583 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
584 "Packages as parameters not implemented!\n"));
586 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
591 * Build a simple object (no nested objects)
593 status = acpi_ut_copy_esimple_to_isimple (external_object, internal_object);
596 return_ACPI_STATUS (status);
600 /*******************************************************************************
602 * FUNCTION: acpi_ut_copy_simple_object
604 * PARAMETERS: source_desc - The internal object to be copied
605 * dest_desc - New target object
609 * DESCRIPTION: Simple copy of one internal object to another. Reference count
610 * of the destination object is preserved.
612 ******************************************************************************/
615 acpi_ut_copy_simple_object (
616 union acpi_operand_object *source_desc,
617 union acpi_operand_object *dest_desc)
620 union acpi_operand_object *next_object;
623 /* Save fields from destination that we don't want to overwrite */
625 reference_count = dest_desc->common.reference_count;
626 next_object = dest_desc->common.next_object;
628 /* Copy the entire source object over the destination object*/
630 ACPI_MEMCPY ((char *) dest_desc, (char *) source_desc,
631 sizeof (union acpi_operand_object));
633 /* Restore the saved fields */
635 dest_desc->common.reference_count = reference_count;
636 dest_desc->common.next_object = next_object;
638 /* Handle the objects with extra data */
640 switch (ACPI_GET_OBJECT_TYPE (dest_desc)) {
641 case ACPI_TYPE_BUFFER:
643 dest_desc->buffer.node = NULL;
644 dest_desc->common.flags = source_desc->common.flags;
647 * Allocate and copy the actual buffer if and only if:
648 * 1) There is a valid buffer pointer
649 * 2) The buffer is not static (not in an ACPI table) (in this case,
650 * the actual pointer was already copied above)
652 if ((source_desc->buffer.pointer) &&
653 (!(source_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
654 dest_desc->buffer.pointer = NULL;
656 /* Create an actual buffer only if length > 0 */
658 if (source_desc->buffer.length) {
659 dest_desc->buffer.pointer = ACPI_MEM_ALLOCATE (source_desc->buffer.length);
660 if (!dest_desc->buffer.pointer) {
661 return (AE_NO_MEMORY);
664 /* Copy the actual buffer data */
666 ACPI_MEMCPY (dest_desc->buffer.pointer, source_desc->buffer.pointer,
667 source_desc->buffer.length);
672 case ACPI_TYPE_STRING:
675 * Allocate and copy the actual string if and only if:
676 * 1) There is a valid string pointer
677 * 2) The string is not static (not in an ACPI table) (in this case,
678 * the actual pointer was already copied above)
680 if ((source_desc->string.pointer) &&
681 (!(source_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
682 dest_desc->string.pointer = ACPI_MEM_ALLOCATE ((acpi_size) source_desc->string.length + 1);
683 if (!dest_desc->string.pointer) {
684 return (AE_NO_MEMORY);
687 ACPI_MEMCPY (dest_desc->string.pointer, source_desc->string.pointer,
688 (acpi_size) source_desc->string.length + 1);
693 /* Nothing to do for other simple objects */
701 /*******************************************************************************
703 * FUNCTION: acpi_ut_copy_ielement_to_ielement
705 * PARAMETERS: acpi_pkg_callback
709 * DESCRIPTION: Copy one package element to another package element
711 ******************************************************************************/
714 acpi_ut_copy_ielement_to_ielement (
716 union acpi_operand_object *source_object,
717 union acpi_generic_state *state,
720 acpi_status status = AE_OK;
722 union acpi_operand_object **this_target_ptr;
723 union acpi_operand_object *target_object;
726 ACPI_FUNCTION_ENTRY ();
729 this_index = state->pkg.index;
730 this_target_ptr = (union acpi_operand_object **)
731 &state->pkg.dest_object->package.elements[this_index];
733 switch (object_type) {
734 case ACPI_COPY_TYPE_SIMPLE:
736 /* A null source object indicates a (legal) null package element */
740 * This is a simple object, just copy it
742 target_object = acpi_ut_create_internal_object (
743 ACPI_GET_OBJECT_TYPE (source_object));
744 if (!target_object) {
745 return (AE_NO_MEMORY);
748 status = acpi_ut_copy_simple_object (source_object, target_object);
749 if (ACPI_FAILURE (status)) {
753 *this_target_ptr = target_object;
756 /* Pass through a null element */
758 *this_target_ptr = NULL;
763 case ACPI_COPY_TYPE_PACKAGE:
766 * This object is a package - go down another nesting level
767 * Create and build the package object
769 target_object = acpi_ut_create_internal_object (ACPI_TYPE_PACKAGE);
770 if (!target_object) {
771 return (AE_NO_MEMORY);
774 target_object->package.count = source_object->package.count;
775 target_object->common.flags = source_object->common.flags;
778 * Create the object array
780 target_object->package.elements =
781 ACPI_MEM_CALLOCATE (((acpi_size) source_object->package.count + 1) *
783 if (!target_object->package.elements) {
784 ACPI_MEM_FREE (target_object);
785 return (AE_NO_MEMORY);
789 * Pass the new package object back to the package walk routine
791 state->pkg.this_target_obj = target_object;
794 * Store the object pointer in the parent package object
796 *this_target_ptr = target_object;
801 return (AE_BAD_PARAMETER);
808 /*******************************************************************************
810 * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
812 * PARAMETERS: *source_obj - Pointer to the source package object
813 * *dest_obj - Where the internal object is returned
815 * RETURN: Status - the status of the call
817 * DESCRIPTION: This function is called to copy an internal package object
818 * into another internal package object.
820 ******************************************************************************/
823 acpi_ut_copy_ipackage_to_ipackage (
824 union acpi_operand_object *source_obj,
825 union acpi_operand_object *dest_obj,
826 struct acpi_walk_state *walk_state)
828 acpi_status status = AE_OK;
831 ACPI_FUNCTION_TRACE ("ut_copy_ipackage_to_ipackage");
834 dest_obj->common.type = ACPI_GET_OBJECT_TYPE (source_obj);
835 dest_obj->common.flags = source_obj->common.flags;
836 dest_obj->package.count = source_obj->package.count;
839 * Create the object array and walk the source package tree
841 dest_obj->package.elements = ACPI_MEM_CALLOCATE (
842 ((acpi_size) source_obj->package.count + 1) *
844 if (!dest_obj->package.elements) {
846 ("aml_build_copy_internal_package_object: Package allocation failure\n"));
847 return_ACPI_STATUS (AE_NO_MEMORY);
851 * Copy the package element-by-element by walking the package "tree".
852 * This handles nested packages of arbitrary depth.
854 status = acpi_ut_walk_package_tree (source_obj, dest_obj,
855 acpi_ut_copy_ielement_to_ielement, walk_state);
856 if (ACPI_FAILURE (status)) {
857 /* On failure, delete the destination package object */
859 acpi_ut_remove_reference (dest_obj);
862 return_ACPI_STATUS (status);
866 /*******************************************************************************
868 * FUNCTION: acpi_ut_copy_iobject_to_iobject
870 * PARAMETERS: walk_state - Current walk state
871 * source_desc - The internal object to be copied
872 * dest_desc - Where the copied object is returned
876 * DESCRIPTION: Copy an internal object to a new internal object
878 ******************************************************************************/
881 acpi_ut_copy_iobject_to_iobject (
882 union acpi_operand_object *source_desc,
883 union acpi_operand_object **dest_desc,
884 struct acpi_walk_state *walk_state)
886 acpi_status status = AE_OK;
889 ACPI_FUNCTION_TRACE ("ut_copy_iobject_to_iobject");
892 /* Create the top level object */
894 *dest_desc = acpi_ut_create_internal_object (ACPI_GET_OBJECT_TYPE (source_desc));
896 return_ACPI_STATUS (AE_NO_MEMORY);
899 /* Copy the object and possible subobjects */
901 if (ACPI_GET_OBJECT_TYPE (source_desc) == ACPI_TYPE_PACKAGE) {
902 status = acpi_ut_copy_ipackage_to_ipackage (source_desc, *dest_desc,
906 status = acpi_ut_copy_simple_object (source_desc, *dest_desc);
909 return_ACPI_STATUS (status);