1 /******************************************************************************
3 * Module Name: utcopy - Internal to external object translation utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2007, 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.
44 #include <acpi/acpi.h>
45 #include <acpi/amlcode.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utcopy")
50 /* Local prototypes */
52 acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
53 union acpi_object *external_object,
54 u8 * data_space, acpi_size * buffer_space_used);
57 acpi_ut_copy_ielement_to_ielement(u8 object_type,
58 union acpi_operand_object *source_object,
59 union acpi_generic_state *state,
63 acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
64 u8 * buffer, acpi_size * space_used);
67 acpi_ut_copy_esimple_to_isimple(union acpi_object *user_obj,
68 union acpi_operand_object **return_obj);
71 acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
72 union acpi_operand_object **internal_object);
75 acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
76 union acpi_operand_object *dest_desc);
79 acpi_ut_copy_ielement_to_eelement(u8 object_type,
80 union acpi_operand_object *source_object,
81 union acpi_generic_state *state,
85 acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
86 union acpi_operand_object *dest_obj,
87 struct acpi_walk_state *walk_state);
89 /*******************************************************************************
91 * FUNCTION: acpi_ut_copy_isimple_to_esimple
93 * PARAMETERS: internal_object - Source object to be copied
94 * external_object - Where to return the copied object
95 * data_space - Where object data is returned (such as
96 * buffer and string data)
97 * buffer_space_used - Length of data_space that was used
101 * DESCRIPTION: This function is called to copy a simple internal object to
102 * an external object.
104 * The data_space buffer is assumed to have sufficient space for
107 ******************************************************************************/
110 acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
111 union acpi_object *external_object,
112 u8 * data_space, acpi_size * buffer_space_used)
114 acpi_status status = AE_OK;
116 ACPI_FUNCTION_TRACE(ut_copy_isimple_to_esimple);
118 *buffer_space_used = 0;
121 * Check for NULL object case (could be an uninitialized
124 if (!internal_object) {
125 return_ACPI_STATUS(AE_OK);
128 /* Always clear the external object */
130 ACPI_MEMSET(external_object, 0, sizeof(union acpi_object));
133 * In general, the external object will be the same type as
134 * the internal object
136 external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
138 /* However, only a limited number of external types are supported */
140 switch (ACPI_GET_OBJECT_TYPE(internal_object)) {
141 case ACPI_TYPE_STRING:
143 external_object->string.pointer = (char *)data_space;
144 external_object->string.length = internal_object->string.length;
145 *buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
150 ACPI_MEMCPY((void *)data_space,
151 (void *)internal_object->string.pointer,
152 (acpi_size) internal_object->string.length + 1);
155 case ACPI_TYPE_BUFFER:
157 external_object->buffer.pointer = data_space;
158 external_object->buffer.length = internal_object->buffer.length;
160 ACPI_ROUND_UP_TO_NATIVE_WORD(internal_object->string.
163 ACPI_MEMCPY((void *)data_space,
164 (void *)internal_object->buffer.pointer,
165 internal_object->buffer.length);
168 case ACPI_TYPE_INTEGER:
170 external_object->integer.value = internal_object->integer.value;
173 case ACPI_TYPE_LOCAL_REFERENCE:
176 * This is an object reference. Attempt to dereference it.
178 switch (internal_object->reference.opcode) {
179 case AML_INT_NAMEPATH_OP:
181 /* For namepath, return the object handle ("reference") */
185 * Use the object type of "Any" to indicate a reference
186 * to object containing a handle to an ACPI named object.
188 external_object->type = ACPI_TYPE_ANY;
189 external_object->reference.handle =
190 internal_object->reference.node;
195 case ACPI_TYPE_PROCESSOR:
197 external_object->processor.proc_id =
198 internal_object->processor.proc_id;
199 external_object->processor.pblk_address =
200 internal_object->processor.address;
201 external_object->processor.pblk_length =
202 internal_object->processor.length;
205 case ACPI_TYPE_POWER:
207 external_object->power_resource.system_level =
208 internal_object->power_resource.system_level;
210 external_object->power_resource.resource_order =
211 internal_object->power_resource.resource_order;
216 * There is no corresponding external object type
218 return_ACPI_STATUS(AE_SUPPORT);
221 return_ACPI_STATUS(status);
224 /*******************************************************************************
226 * FUNCTION: acpi_ut_copy_ielement_to_eelement
228 * PARAMETERS: acpi_pkg_callback
232 * DESCRIPTION: Copy one package element to another package element
234 ******************************************************************************/
237 acpi_ut_copy_ielement_to_eelement(u8 object_type,
238 union acpi_operand_object *source_object,
239 union acpi_generic_state *state,
242 acpi_status status = AE_OK;
243 struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
244 acpi_size object_space;
246 union acpi_object *target_object;
248 ACPI_FUNCTION_ENTRY();
250 this_index = state->pkg.index;
251 target_object = (union acpi_object *)
252 &((union acpi_object *)(state->pkg.dest_object))->package.
253 elements[this_index];
255 switch (object_type) {
256 case ACPI_COPY_TYPE_SIMPLE:
259 * This is a simple or null object
261 status = acpi_ut_copy_isimple_to_esimple(source_object,
265 if (ACPI_FAILURE(status)) {
270 case ACPI_COPY_TYPE_PACKAGE:
273 * Build the package object
275 target_object->type = ACPI_TYPE_PACKAGE;
276 target_object->package.count = source_object->package.count;
277 target_object->package.elements =
278 ACPI_CAST_PTR(union acpi_object, info->free_space);
281 * Pass the new package object back to the package walk routine
283 state->pkg.this_target_obj = target_object;
286 * Save space for the array of objects (Package elements)
287 * update the buffer length counter
289 object_space = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
297 return (AE_BAD_PARAMETER);
300 info->free_space += object_space;
301 info->length += object_space;
305 /*******************************************************************************
307 * FUNCTION: acpi_ut_copy_ipackage_to_epackage
309 * PARAMETERS: internal_object - Pointer to the object we are returning
310 * Buffer - Where the object is returned
311 * space_used - Where the object length is returned
315 * DESCRIPTION: This function is called to place a package object in a user
316 * buffer. A package object by definition contains other objects.
318 * The buffer is assumed to have sufficient space for the object.
319 * The caller must have verified the buffer length needed using the
320 * acpi_ut_get_object_size function before calling this function.
322 ******************************************************************************/
325 acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
326 u8 * buffer, acpi_size * space_used)
328 union acpi_object *external_object;
330 struct acpi_pkg_info info;
332 ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_epackage);
335 * First package at head of the buffer
337 external_object = ACPI_CAST_PTR(union acpi_object, buffer);
340 * Free space begins right after the first package
342 info.length = ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
344 buffer + ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
345 info.object_space = 0;
346 info.num_packages = 1;
348 external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
349 external_object->package.count = internal_object->package.count;
350 external_object->package.elements = ACPI_CAST_PTR(union acpi_object,
354 * Leave room for an array of ACPI_OBJECTS in the buffer
355 * and move the free space past it
357 info.length += (acpi_size) external_object->package.count *
358 ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
359 info.free_space += external_object->package.count *
360 ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
362 status = acpi_ut_walk_package_tree(internal_object, external_object,
363 acpi_ut_copy_ielement_to_eelement,
366 *space_used = info.length;
367 return_ACPI_STATUS(status);
370 /*******************************************************************************
372 * FUNCTION: acpi_ut_copy_iobject_to_eobject
374 * PARAMETERS: internal_object - The internal object to be converted
375 * buffer_ptr - Where the object is returned
379 * DESCRIPTION: This function is called to build an API object to be returned to
382 ******************************************************************************/
385 acpi_ut_copy_iobject_to_eobject(union acpi_operand_object *internal_object,
386 struct acpi_buffer *ret_buffer)
390 ACPI_FUNCTION_TRACE(ut_copy_iobject_to_eobject);
392 if (ACPI_GET_OBJECT_TYPE(internal_object) == ACPI_TYPE_PACKAGE) {
394 * Package object: Copy all subobjects (including
397 status = acpi_ut_copy_ipackage_to_epackage(internal_object,
399 &ret_buffer->length);
402 * Build a simple object (no nested objects)
404 status = acpi_ut_copy_isimple_to_esimple(internal_object,
412 ACPI_ROUND_UP_TO_NATIVE_WORD
416 &ret_buffer->length);
418 * build simple does not include the object size in the length
419 * so we add it in here
421 ret_buffer->length += sizeof(union acpi_object);
424 return_ACPI_STATUS(status);
427 /*******************************************************************************
429 * FUNCTION: acpi_ut_copy_esimple_to_isimple
431 * PARAMETERS: external_object - The external object to be converted
432 * ret_internal_object - Where the internal object is returned
436 * DESCRIPTION: This function copies an external object to an internal one.
437 * NOTE: Pointers can be copied, we don't need to copy data.
438 * (The pointers have to be valid in our address space no matter
439 * what we do with them!)
441 ******************************************************************************/
444 acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
445 union acpi_operand_object **ret_internal_object)
447 union acpi_operand_object *internal_object;
449 ACPI_FUNCTION_TRACE(ut_copy_esimple_to_isimple);
452 * Simple types supported are: String, Buffer, Integer
454 switch (external_object->type) {
455 case ACPI_TYPE_STRING:
456 case ACPI_TYPE_BUFFER:
457 case ACPI_TYPE_INTEGER:
459 internal_object = acpi_ut_create_internal_object((u8)
462 if (!internal_object) {
463 return_ACPI_STATUS(AE_NO_MEMORY);
468 /* All other types are not supported */
470 return_ACPI_STATUS(AE_SUPPORT);
473 /* Must COPY string and buffer contents */
475 switch (external_object->type) {
476 case ACPI_TYPE_STRING:
478 internal_object->string.pointer =
479 ACPI_ALLOCATE_ZEROED((acpi_size) external_object->string.
481 if (!internal_object->string.pointer) {
485 ACPI_MEMCPY(internal_object->string.pointer,
486 external_object->string.pointer,
487 external_object->string.length);
489 internal_object->string.length = external_object->string.length;
492 case ACPI_TYPE_BUFFER:
494 internal_object->buffer.pointer =
495 ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
496 if (!internal_object->buffer.pointer) {
500 ACPI_MEMCPY(internal_object->buffer.pointer,
501 external_object->buffer.pointer,
502 external_object->buffer.length);
504 internal_object->buffer.length = external_object->buffer.length;
507 case ACPI_TYPE_INTEGER:
509 internal_object->integer.value = external_object->integer.value;
513 /* Other types can't get here */
517 *ret_internal_object = internal_object;
518 return_ACPI_STATUS(AE_OK);
521 acpi_ut_remove_reference(internal_object);
522 return_ACPI_STATUS(AE_NO_MEMORY);
525 /*******************************************************************************
527 * FUNCTION: acpi_ut_copy_epackage_to_ipackage
529 * PARAMETERS: external_object - The external object to be converted
530 * internal_object - Where the internal object is returned
534 * DESCRIPTION: Copy an external package object to an internal package.
535 * Handles nested packages.
537 ******************************************************************************/
540 acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
541 union acpi_operand_object **internal_object)
543 acpi_status status = AE_OK;
544 union acpi_operand_object *package_object;
545 union acpi_operand_object **package_elements;
548 ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
550 /* Create the package object */
553 acpi_ut_create_package_object(external_object->package.count);
554 if (!package_object) {
555 return_ACPI_STATUS(AE_NO_MEMORY);
558 package_elements = package_object->package.elements;
561 * Recursive implementation. Probably ok, since nested external packages
562 * as parameters should be very rare.
564 for (i = 0; i < external_object->package.count; i++) {
566 acpi_ut_copy_eobject_to_iobject(&external_object->package.
568 &package_elements[i]);
569 if (ACPI_FAILURE(status)) {
571 /* Truncate package and delete it */
573 package_object->package.count = i;
574 package_elements[i] = NULL;
575 acpi_ut_remove_reference(package_object);
576 return_ACPI_STATUS(status);
580 *internal_object = package_object;
581 return_ACPI_STATUS(status);
584 /*******************************************************************************
586 * FUNCTION: acpi_ut_copy_eobject_to_iobject
588 * PARAMETERS: external_object - The external object to be converted
589 * internal_object - Where the internal object is returned
591 * RETURN: Status - the status of the call
593 * DESCRIPTION: Converts an external object to an internal object.
595 ******************************************************************************/
598 acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
599 union acpi_operand_object **internal_object)
603 ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
605 if (external_object->type == ACPI_TYPE_PACKAGE) {
607 acpi_ut_copy_epackage_to_ipackage(external_object,
611 * Build a simple object (no nested objects)
614 acpi_ut_copy_esimple_to_isimple(external_object,
618 return_ACPI_STATUS(status);
621 /*******************************************************************************
623 * FUNCTION: acpi_ut_copy_simple_object
625 * PARAMETERS: source_desc - The internal object to be copied
626 * dest_desc - New target object
630 * DESCRIPTION: Simple copy of one internal object to another. Reference count
631 * of the destination object is preserved.
633 ******************************************************************************/
636 acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
637 union acpi_operand_object *dest_desc)
640 union acpi_operand_object *next_object;
642 /* Save fields from destination that we don't want to overwrite */
644 reference_count = dest_desc->common.reference_count;
645 next_object = dest_desc->common.next_object;
647 /* Copy the entire source object over the destination object */
649 ACPI_MEMCPY((char *)dest_desc, (char *)source_desc,
650 sizeof(union acpi_operand_object));
652 /* Restore the saved fields */
654 dest_desc->common.reference_count = reference_count;
655 dest_desc->common.next_object = next_object;
657 /* New object is not static, regardless of source */
659 dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
661 /* Handle the objects with extra data */
663 switch (ACPI_GET_OBJECT_TYPE(dest_desc)) {
664 case ACPI_TYPE_BUFFER:
666 * Allocate and copy the actual buffer if and only if:
667 * 1) There is a valid buffer pointer
668 * 2) The buffer has a length > 0
670 if ((source_desc->buffer.pointer) &&
671 (source_desc->buffer.length)) {
672 dest_desc->buffer.pointer =
673 ACPI_ALLOCATE(source_desc->buffer.length);
674 if (!dest_desc->buffer.pointer) {
675 return (AE_NO_MEMORY);
678 /* Copy the actual buffer data */
680 ACPI_MEMCPY(dest_desc->buffer.pointer,
681 source_desc->buffer.pointer,
682 source_desc->buffer.length);
686 case ACPI_TYPE_STRING:
688 * Allocate and copy the actual string if and only if:
689 * 1) There is a valid string pointer
690 * (Pointer to a NULL string is allowed)
692 if (source_desc->string.pointer) {
693 dest_desc->string.pointer =
694 ACPI_ALLOCATE((acpi_size) source_desc->string.
696 if (!dest_desc->string.pointer) {
697 return (AE_NO_MEMORY);
700 /* Copy the actual string data */
702 ACPI_MEMCPY(dest_desc->string.pointer,
703 source_desc->string.pointer,
704 (acpi_size) source_desc->string.length + 1);
708 case ACPI_TYPE_LOCAL_REFERENCE:
710 * We copied the reference object, so we now must add a reference
711 * to the object pointed to by the reference
713 acpi_ut_add_reference(source_desc->reference.object);
716 case ACPI_TYPE_REGION:
718 * We copied the Region Handler, so we now must add a reference
720 if (dest_desc->region.handler) {
721 acpi_ut_add_reference(dest_desc->region.handler);
726 /* Nothing to do for other simple objects */
733 /*******************************************************************************
735 * FUNCTION: acpi_ut_copy_ielement_to_ielement
737 * PARAMETERS: acpi_pkg_callback
741 * DESCRIPTION: Copy one package element to another package element
743 ******************************************************************************/
746 acpi_ut_copy_ielement_to_ielement(u8 object_type,
747 union acpi_operand_object *source_object,
748 union acpi_generic_state *state,
751 acpi_status status = AE_OK;
753 union acpi_operand_object **this_target_ptr;
754 union acpi_operand_object *target_object;
756 ACPI_FUNCTION_ENTRY();
758 this_index = state->pkg.index;
759 this_target_ptr = (union acpi_operand_object **)
760 &state->pkg.dest_object->package.elements[this_index];
762 switch (object_type) {
763 case ACPI_COPY_TYPE_SIMPLE:
765 /* A null source object indicates a (legal) null package element */
769 * This is a simple object, just copy it
772 acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE
774 if (!target_object) {
775 return (AE_NO_MEMORY);
779 acpi_ut_copy_simple_object(source_object,
781 if (ACPI_FAILURE(status)) {
785 *this_target_ptr = target_object;
787 /* Pass through a null element */
789 *this_target_ptr = NULL;
793 case ACPI_COPY_TYPE_PACKAGE:
796 * This object is a package - go down another nesting level
797 * Create and build the package object
800 acpi_ut_create_package_object(source_object->package.count);
801 if (!target_object) {
802 return (AE_NO_MEMORY);
805 target_object->common.flags = source_object->common.flags;
807 /* Pass the new package object back to the package walk routine */
809 state->pkg.this_target_obj = target_object;
811 /* Store the object pointer in the parent package object */
813 *this_target_ptr = target_object;
817 return (AE_BAD_PARAMETER);
823 acpi_ut_remove_reference(target_object);
827 /*******************************************************************************
829 * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
831 * PARAMETERS: *source_obj - Pointer to the source package object
832 * *dest_obj - Where the internal object is returned
834 * RETURN: Status - the status of the call
836 * DESCRIPTION: This function is called to copy an internal package object
837 * into another internal package object.
839 ******************************************************************************/
842 acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
843 union acpi_operand_object *dest_obj,
844 struct acpi_walk_state *walk_state)
846 acpi_status status = AE_OK;
848 ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
850 dest_obj->common.type = ACPI_GET_OBJECT_TYPE(source_obj);
851 dest_obj->common.flags = source_obj->common.flags;
852 dest_obj->package.count = source_obj->package.count;
855 * Create the object array and walk the source package tree
857 dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
860 1) * sizeof(void *));
861 if (!dest_obj->package.elements) {
862 ACPI_ERROR((AE_INFO, "Package allocation failure"));
863 return_ACPI_STATUS(AE_NO_MEMORY);
867 * Copy the package element-by-element by walking the package "tree".
868 * This handles nested packages of arbitrary depth.
870 status = acpi_ut_walk_package_tree(source_obj, dest_obj,
871 acpi_ut_copy_ielement_to_ielement,
873 if (ACPI_FAILURE(status)) {
875 /* On failure, delete the destination package object */
877 acpi_ut_remove_reference(dest_obj);
880 return_ACPI_STATUS(status);
883 /*******************************************************************************
885 * FUNCTION: acpi_ut_copy_iobject_to_iobject
887 * PARAMETERS: walk_state - Current walk state
888 * source_desc - The internal object to be copied
889 * dest_desc - Where the copied object is returned
893 * DESCRIPTION: Copy an internal object to a new internal object
895 ******************************************************************************/
898 acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
899 union acpi_operand_object **dest_desc,
900 struct acpi_walk_state *walk_state)
902 acpi_status status = AE_OK;
904 ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
906 /* Create the top level object */
909 acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE(source_desc));
911 return_ACPI_STATUS(AE_NO_MEMORY);
914 /* Copy the object and possible subobjects */
916 if (ACPI_GET_OBJECT_TYPE(source_desc) == ACPI_TYPE_PACKAGE) {
918 acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
921 status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
924 return_ACPI_STATUS(status);