Linux Audio

Check our new training course

Loading...
v4.6
 
   1/******************************************************************************
   2 *
   3 * Module Name: utcopy - Internal to external object translation utilities
   4 *
 
 
   5 *****************************************************************************/
   6
   7/*
   8 * Copyright (C) 2000 - 2016, 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 <acpi/acpi.h>
  45#include "accommon.h"
  46#include "acnamesp.h"
  47
  48
  49#define _COMPONENT          ACPI_UTILITIES
  50ACPI_MODULE_NAME("utcopy")
  51
  52/* Local prototypes */
  53static acpi_status
  54acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
  55				union acpi_object *external_object,
  56				u8 * data_space, acpi_size * buffer_space_used);
  57
  58static acpi_status
  59acpi_ut_copy_ielement_to_ielement(u8 object_type,
  60				  union acpi_operand_object *source_object,
  61				  union acpi_generic_state *state,
  62				  void *context);
  63
  64static acpi_status
  65acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
  66				  u8 * buffer, acpi_size * space_used);
  67
  68static acpi_status
  69acpi_ut_copy_esimple_to_isimple(union acpi_object *user_obj,
  70				union acpi_operand_object **return_obj);
  71
  72static acpi_status
  73acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
  74				  union acpi_operand_object **internal_object);
  75
  76static acpi_status
  77acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  78			   union acpi_operand_object *dest_desc);
  79
  80static acpi_status
  81acpi_ut_copy_ielement_to_eelement(u8 object_type,
  82				  union acpi_operand_object *source_object,
  83				  union acpi_generic_state *state,
  84				  void *context);
  85
  86static acpi_status
  87acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  88				  union acpi_operand_object *dest_obj,
  89				  struct acpi_walk_state *walk_state);
  90
  91/*******************************************************************************
  92 *
  93 * FUNCTION:    acpi_ut_copy_isimple_to_esimple
  94 *
  95 * PARAMETERS:  internal_object     - Source object to be copied
  96 *              external_object     - Where to return the copied object
  97 *              data_space          - Where object data is returned (such as
  98 *                                    buffer and string data)
  99 *              buffer_space_used   - Length of data_space that was used
 100 *
 101 * RETURN:      Status
 102 *
 103 * DESCRIPTION: This function is called to copy a simple internal object to
 104 *              an external object.
 105 *
 106 *              The data_space buffer is assumed to have sufficient space for
 107 *              the object.
 108 *
 109 ******************************************************************************/
 110
 111static acpi_status
 112acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
 113				union acpi_object *external_object,
 114				u8 * data_space, acpi_size * buffer_space_used)
 115{
 116	acpi_status status = AE_OK;
 117
 118	ACPI_FUNCTION_TRACE(ut_copy_isimple_to_esimple);
 119
 120	*buffer_space_used = 0;
 121
 122	/*
 123	 * Check for NULL object case (could be an uninitialized
 124	 * package element)
 125	 */
 126	if (!internal_object) {
 127		return_ACPI_STATUS(AE_OK);
 128	}
 129
 130	/* Always clear the external object */
 131
 132	memset(external_object, 0, sizeof(union acpi_object));
 133
 134	/*
 135	 * In general, the external object will be the same type as
 136	 * the internal object
 137	 */
 138	external_object->type = internal_object->common.type;
 139
 140	/* However, only a limited number of external types are supported */
 141
 142	switch (internal_object->common.type) {
 143	case ACPI_TYPE_STRING:
 144
 145		external_object->string.pointer = (char *)data_space;
 146		external_object->string.length = internal_object->string.length;
 147		*buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
 148								  internal_object->
 149								  string.
 150								  length + 1);
 151
 152		memcpy((void *)data_space,
 153		       (void *)internal_object->string.pointer,
 154		       (acpi_size) internal_object->string.length + 1);
 155		break;
 156
 157	case ACPI_TYPE_BUFFER:
 158
 159		external_object->buffer.pointer = data_space;
 160		external_object->buffer.length = internal_object->buffer.length;
 161		*buffer_space_used =
 162		    ACPI_ROUND_UP_TO_NATIVE_WORD(internal_object->string.
 163						 length);
 164
 165		memcpy((void *)data_space,
 166		       (void *)internal_object->buffer.pointer,
 167		       internal_object->buffer.length);
 168		break;
 169
 170	case ACPI_TYPE_INTEGER:
 171
 172		external_object->integer.value = internal_object->integer.value;
 173		break;
 174
 175	case ACPI_TYPE_LOCAL_REFERENCE:
 176
 177		/* This is an object reference. */
 178
 179		switch (internal_object->reference.class) {
 180		case ACPI_REFCLASS_NAME:
 181			/*
 182			 * For namepath, return the object handle ("reference")
 183			 * We are referring to the namespace node
 184			 */
 185			external_object->reference.handle =
 186			    internal_object->reference.node;
 187			external_object->reference.actual_type =
 188			    acpi_ns_get_type(internal_object->reference.node);
 189			break;
 190
 191		default:
 192
 193			/* All other reference types are unsupported */
 194
 195			return_ACPI_STATUS(AE_TYPE);
 196		}
 197		break;
 198
 199	case ACPI_TYPE_PROCESSOR:
 200
 201		external_object->processor.proc_id =
 202		    internal_object->processor.proc_id;
 203		external_object->processor.pblk_address =
 204		    internal_object->processor.address;
 205		external_object->processor.pblk_length =
 206		    internal_object->processor.length;
 207		break;
 208
 209	case ACPI_TYPE_POWER:
 210
 211		external_object->power_resource.system_level =
 212		    internal_object->power_resource.system_level;
 213
 214		external_object->power_resource.resource_order =
 215		    internal_object->power_resource.resource_order;
 216		break;
 217
 218	default:
 219		/*
 220		 * There is no corresponding external object type
 221		 */
 222		ACPI_ERROR((AE_INFO,
 223			    "Unsupported object type, cannot convert to external object: %s",
 224			    acpi_ut_get_type_name(internal_object->common.
 225						  type)));
 226
 227		return_ACPI_STATUS(AE_SUPPORT);
 228	}
 229
 230	return_ACPI_STATUS(status);
 231}
 232
 233/*******************************************************************************
 234 *
 235 * FUNCTION:    acpi_ut_copy_ielement_to_eelement
 236 *
 237 * PARAMETERS:  acpi_pkg_callback
 238 *
 239 * RETURN:      Status
 240 *
 241 * DESCRIPTION: Copy one package element to another package element
 242 *
 243 ******************************************************************************/
 244
 245static acpi_status
 246acpi_ut_copy_ielement_to_eelement(u8 object_type,
 247				  union acpi_operand_object *source_object,
 248				  union acpi_generic_state *state,
 249				  void *context)
 250{
 251	acpi_status status = AE_OK;
 252	struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
 253	acpi_size object_space;
 254	u32 this_index;
 255	union acpi_object *target_object;
 256
 257	ACPI_FUNCTION_ENTRY();
 258
 259	this_index = state->pkg.index;
 260	target_object = (union acpi_object *)&((union acpi_object *)
 261					       (state->pkg.dest_object))->
 262	    package.elements[this_index];
 263
 264	switch (object_type) {
 265	case ACPI_COPY_TYPE_SIMPLE:
 266		/*
 267		 * This is a simple or null object
 268		 */
 269		status = acpi_ut_copy_isimple_to_esimple(source_object,
 270							 target_object,
 271							 info->free_space,
 272							 &object_space);
 273		if (ACPI_FAILURE(status)) {
 274			return (status);
 275		}
 276		break;
 277
 278	case ACPI_COPY_TYPE_PACKAGE:
 279		/*
 280		 * Build the package object
 281		 */
 282		target_object->type = ACPI_TYPE_PACKAGE;
 283		target_object->package.count = source_object->package.count;
 284		target_object->package.elements =
 285		    ACPI_CAST_PTR(union acpi_object, info->free_space);
 286
 287		/*
 288		 * Pass the new package object back to the package walk routine
 289		 */
 290		state->pkg.this_target_obj = target_object;
 291
 292		/*
 293		 * Save space for the array of objects (Package elements)
 294		 * update the buffer length counter
 295		 */
 296		object_space = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
 297							    target_object->
 298							    package.count *
 299							    sizeof(union
 300								   acpi_object));
 301		break;
 302
 303	default:
 304
 305		return (AE_BAD_PARAMETER);
 306	}
 307
 308	info->free_space += object_space;
 309	info->length += object_space;
 310	return (status);
 311}
 312
 313/*******************************************************************************
 314 *
 315 * FUNCTION:    acpi_ut_copy_ipackage_to_epackage
 316 *
 317 * PARAMETERS:  internal_object     - Pointer to the object we are returning
 318 *              buffer              - Where the object is returned
 319 *              space_used          - Where the object length is returned
 320 *
 321 * RETURN:      Status
 322 *
 323 * DESCRIPTION: This function is called to place a package object in a user
 324 *              buffer. A package object by definition contains other objects.
 325 *
 326 *              The buffer is assumed to have sufficient space for the object.
 327 *              The caller must have verified the buffer length needed using
 328 *              the acpi_ut_get_object_size function before calling this function.
 329 *
 330 ******************************************************************************/
 331
 332static acpi_status
 333acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
 334				  u8 * buffer, acpi_size * space_used)
 335{
 336	union acpi_object *external_object;
 337	acpi_status status;
 338	struct acpi_pkg_info info;
 339
 340	ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_epackage);
 341
 342	/*
 343	 * First package at head of the buffer
 344	 */
 345	external_object = ACPI_CAST_PTR(union acpi_object, buffer);
 346
 347	/*
 348	 * Free space begins right after the first package
 349	 */
 350	info.length = ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
 351	info.free_space = buffer +
 352	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
 353	info.object_space = 0;
 354	info.num_packages = 1;
 355
 356	external_object->type = internal_object->common.type;
 357	external_object->package.count = internal_object->package.count;
 358	external_object->package.elements =
 359	    ACPI_CAST_PTR(union acpi_object, info.free_space);
 360
 361	/*
 362	 * Leave room for an array of ACPI_OBJECTS in the buffer
 363	 * and move the free space past it
 364	 */
 365	info.length += (acpi_size) external_object->package.count *
 366	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
 367	info.free_space += external_object->package.count *
 368	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
 369
 370	status = acpi_ut_walk_package_tree(internal_object, external_object,
 371					   acpi_ut_copy_ielement_to_eelement,
 372					   &info);
 373
 374	*space_used = info.length;
 375	return_ACPI_STATUS(status);
 376}
 377
 378/*******************************************************************************
 379 *
 380 * FUNCTION:    acpi_ut_copy_iobject_to_eobject
 381 *
 382 * PARAMETERS:  internal_object     - The internal object to be converted
 383 *              ret_buffer          - Where the object is returned
 384 *
 385 * RETURN:      Status
 386 *
 387 * DESCRIPTION: This function is called to build an API object to be returned
 388 *              to the caller.
 389 *
 390 ******************************************************************************/
 391
 392acpi_status
 393acpi_ut_copy_iobject_to_eobject(union acpi_operand_object *internal_object,
 394				struct acpi_buffer *ret_buffer)
 395{
 396	acpi_status status;
 397
 398	ACPI_FUNCTION_TRACE(ut_copy_iobject_to_eobject);
 399
 400	if (internal_object->common.type == ACPI_TYPE_PACKAGE) {
 401		/*
 402		 * Package object:  Copy all subobjects (including
 403		 * nested packages)
 404		 */
 405		status = acpi_ut_copy_ipackage_to_epackage(internal_object,
 406							   ret_buffer->pointer,
 407							   &ret_buffer->length);
 408	} else {
 409		/*
 410		 * Build a simple object (no nested objects)
 411		 */
 412		status = acpi_ut_copy_isimple_to_esimple(internal_object,
 413							 ACPI_CAST_PTR(union
 414								       acpi_object,
 415								       ret_buffer->
 416								       pointer),
 417							 ACPI_ADD_PTR(u8,
 418								      ret_buffer->
 419								      pointer,
 420								      ACPI_ROUND_UP_TO_NATIVE_WORD
 421								      (sizeof
 422								       (union
 423									acpi_object))),
 424							 &ret_buffer->length);
 425		/*
 426		 * build simple does not include the object size in the length
 427		 * so we add it in here
 428		 */
 429		ret_buffer->length += sizeof(union acpi_object);
 430	}
 431
 432	return_ACPI_STATUS(status);
 433}
 434
 435/*******************************************************************************
 436 *
 437 * FUNCTION:    acpi_ut_copy_esimple_to_isimple
 438 *
 439 * PARAMETERS:  external_object     - The external object to be converted
 440 *              ret_internal_object - Where the internal object is returned
 441 *
 442 * RETURN:      Status
 443 *
 444 * DESCRIPTION: This function copies an external object to an internal one.
 445 *              NOTE: Pointers can be copied, we don't need to copy data.
 446 *              (The pointers have to be valid in our address space no matter
 447 *              what we do with them!)
 448 *
 449 ******************************************************************************/
 450
 451static acpi_status
 452acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
 453				union acpi_operand_object **ret_internal_object)
 454{
 455	union acpi_operand_object *internal_object;
 456
 457	ACPI_FUNCTION_TRACE(ut_copy_esimple_to_isimple);
 458
 459	/*
 460	 * Simple types supported are: String, Buffer, Integer
 461	 */
 462	switch (external_object->type) {
 463	case ACPI_TYPE_STRING:
 464	case ACPI_TYPE_BUFFER:
 465	case ACPI_TYPE_INTEGER:
 466	case ACPI_TYPE_LOCAL_REFERENCE:
 467
 468		internal_object = acpi_ut_create_internal_object((u8)
 469								 external_object->
 470								 type);
 471		if (!internal_object) {
 472			return_ACPI_STATUS(AE_NO_MEMORY);
 473		}
 474		break;
 475
 476	case ACPI_TYPE_ANY:	/* This is the case for a NULL object */
 477
 478		*ret_internal_object = NULL;
 479		return_ACPI_STATUS(AE_OK);
 480
 481	default:
 482
 483		/* All other types are not supported */
 484
 485		ACPI_ERROR((AE_INFO,
 486			    "Unsupported object type, cannot convert to internal object: %s",
 487			    acpi_ut_get_type_name(external_object->type)));
 488
 489		return_ACPI_STATUS(AE_SUPPORT);
 490	}
 491
 492	/* Must COPY string and buffer contents */
 493
 494	switch (external_object->type) {
 495	case ACPI_TYPE_STRING:
 496
 497		internal_object->string.pointer =
 498		    ACPI_ALLOCATE_ZEROED((acpi_size)
 499					 external_object->string.length + 1);
 500
 501		if (!internal_object->string.pointer) {
 502			goto error_exit;
 503		}
 504
 505		memcpy(internal_object->string.pointer,
 506		       external_object->string.pointer,
 507		       external_object->string.length);
 508
 509		internal_object->string.length = external_object->string.length;
 510		break;
 511
 512	case ACPI_TYPE_BUFFER:
 513
 514		internal_object->buffer.pointer =
 515		    ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
 516		if (!internal_object->buffer.pointer) {
 517			goto error_exit;
 518		}
 519
 520		memcpy(internal_object->buffer.pointer,
 521		       external_object->buffer.pointer,
 522		       external_object->buffer.length);
 523
 524		internal_object->buffer.length = external_object->buffer.length;
 525
 526		/* Mark buffer data valid */
 527
 528		internal_object->buffer.flags |= AOPOBJ_DATA_VALID;
 529		break;
 530
 531	case ACPI_TYPE_INTEGER:
 532
 533		internal_object->integer.value = external_object->integer.value;
 534		break;
 535
 536	case ACPI_TYPE_LOCAL_REFERENCE:
 537
 538		/* An incoming reference is defined to be a namespace node */
 539
 540		internal_object->reference.class = ACPI_REFCLASS_REFOF;
 541		internal_object->reference.object =
 542		    external_object->reference.handle;
 543		break;
 544
 545	default:
 546
 547		/* Other types can't get here */
 548
 549		break;
 550	}
 551
 552	*ret_internal_object = internal_object;
 553	return_ACPI_STATUS(AE_OK);
 554
 555error_exit:
 556	acpi_ut_remove_reference(internal_object);
 557	return_ACPI_STATUS(AE_NO_MEMORY);
 558}
 559
 560/*******************************************************************************
 561 *
 562 * FUNCTION:    acpi_ut_copy_epackage_to_ipackage
 563 *
 564 * PARAMETERS:  external_object     - The external object to be converted
 565 *              internal_object     - Where the internal object is returned
 566 *
 567 * RETURN:      Status
 568 *
 569 * DESCRIPTION: Copy an external package object to an internal package.
 570 *              Handles nested packages.
 571 *
 572 ******************************************************************************/
 573
 574static acpi_status
 575acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
 576				  union acpi_operand_object **internal_object)
 577{
 578	acpi_status status = AE_OK;
 579	union acpi_operand_object *package_object;
 580	union acpi_operand_object **package_elements;
 581	u32 i;
 582
 583	ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
 584
 585	/* Create the package object */
 586
 587	package_object =
 588	    acpi_ut_create_package_object(external_object->package.count);
 589	if (!package_object) {
 590		return_ACPI_STATUS(AE_NO_MEMORY);
 591	}
 592
 593	package_elements = package_object->package.elements;
 594
 595	/*
 596	 * Recursive implementation. Probably ok, since nested external
 597	 * packages as parameters should be very rare.
 598	 */
 599	for (i = 0; i < external_object->package.count; i++) {
 600		status =
 601		    acpi_ut_copy_eobject_to_iobject(&external_object->package.
 602						    elements[i],
 603						    &package_elements[i]);
 604		if (ACPI_FAILURE(status)) {
 605
 606			/* Truncate package and delete it */
 607
 608			package_object->package.count = i;
 609			package_elements[i] = NULL;
 610			acpi_ut_remove_reference(package_object);
 611			return_ACPI_STATUS(status);
 612		}
 613	}
 614
 615	/* Mark package data valid */
 616
 617	package_object->package.flags |= AOPOBJ_DATA_VALID;
 618
 619	*internal_object = package_object;
 620	return_ACPI_STATUS(status);
 621}
 622
 623/*******************************************************************************
 624 *
 625 * FUNCTION:    acpi_ut_copy_eobject_to_iobject
 626 *
 627 * PARAMETERS:  external_object     - The external object to be converted
 628 *              internal_object     - Where the internal object is returned
 629 *
 630 * RETURN:      Status
 631 *
 632 * DESCRIPTION: Converts an external object to an internal object.
 633 *
 634 ******************************************************************************/
 635
 636acpi_status
 637acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
 638				union acpi_operand_object **internal_object)
 639{
 640	acpi_status status;
 641
 642	ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
 643
 644	if (external_object->type == ACPI_TYPE_PACKAGE) {
 645		status =
 646		    acpi_ut_copy_epackage_to_ipackage(external_object,
 647						      internal_object);
 648	} else {
 649		/*
 650		 * Build a simple object (no nested objects)
 651		 */
 652		status = acpi_ut_copy_esimple_to_isimple(external_object,
 653							 internal_object);
 654	}
 655
 656	return_ACPI_STATUS(status);
 657}
 658
 659/*******************************************************************************
 660 *
 661 * FUNCTION:    acpi_ut_copy_simple_object
 662 *
 663 * PARAMETERS:  source_desc         - The internal object to be copied
 664 *              dest_desc           - New target object
 665 *
 666 * RETURN:      Status
 667 *
 668 * DESCRIPTION: Simple copy of one internal object to another. Reference count
 669 *              of the destination object is preserved.
 670 *
 671 ******************************************************************************/
 672
 673static acpi_status
 674acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
 675			   union acpi_operand_object *dest_desc)
 676{
 677	u16 reference_count;
 678	union acpi_operand_object *next_object;
 679	acpi_status status;
 680	acpi_size copy_size;
 681
 682	/* Save fields from destination that we don't want to overwrite */
 683
 684	reference_count = dest_desc->common.reference_count;
 685	next_object = dest_desc->common.next_object;
 686
 687	/*
 688	 * Copy the entire source object over the destination object.
 689	 * Note: Source can be either an operand object or namespace node.
 690	 */
 691	copy_size = sizeof(union acpi_operand_object);
 692	if (ACPI_GET_DESCRIPTOR_TYPE(source_desc) == ACPI_DESC_TYPE_NAMED) {
 693		copy_size = sizeof(struct acpi_namespace_node);
 694	}
 695
 696	memcpy(ACPI_CAST_PTR(char, dest_desc),
 697	       ACPI_CAST_PTR(char, source_desc), copy_size);
 698
 699	/* Restore the saved fields */
 700
 701	dest_desc->common.reference_count = reference_count;
 702	dest_desc->common.next_object = next_object;
 703
 704	/* New object is not static, regardless of source */
 705
 706	dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
 707
 708	/* Handle the objects with extra data */
 709
 710	switch (dest_desc->common.type) {
 711	case ACPI_TYPE_BUFFER:
 712		/*
 713		 * Allocate and copy the actual buffer if and only if:
 714		 * 1) There is a valid buffer pointer
 715		 * 2) The buffer has a length > 0
 716		 */
 717		if ((source_desc->buffer.pointer) &&
 718		    (source_desc->buffer.length)) {
 719			dest_desc->buffer.pointer =
 720			    ACPI_ALLOCATE(source_desc->buffer.length);
 721			if (!dest_desc->buffer.pointer) {
 722				return (AE_NO_MEMORY);
 723			}
 724
 725			/* Copy the actual buffer data */
 726
 727			memcpy(dest_desc->buffer.pointer,
 728			       source_desc->buffer.pointer,
 729			       source_desc->buffer.length);
 730		}
 731		break;
 732
 733	case ACPI_TYPE_STRING:
 734		/*
 735		 * Allocate and copy the actual string if and only if:
 736		 * 1) There is a valid string pointer
 737		 * (Pointer to a NULL string is allowed)
 738		 */
 739		if (source_desc->string.pointer) {
 740			dest_desc->string.pointer =
 741			    ACPI_ALLOCATE((acpi_size) source_desc->string.
 742					  length + 1);
 743			if (!dest_desc->string.pointer) {
 744				return (AE_NO_MEMORY);
 745			}
 746
 747			/* Copy the actual string data */
 748
 749			memcpy(dest_desc->string.pointer,
 750			       source_desc->string.pointer,
 751			       (acpi_size) source_desc->string.length + 1);
 752		}
 753		break;
 754
 755	case ACPI_TYPE_LOCAL_REFERENCE:
 756		/*
 757		 * We copied the reference object, so we now must add a reference
 758		 * to the object pointed to by the reference
 759		 *
 760		 * DDBHandle reference (from Load/load_table) is a special reference,
 761		 * it does not have a Reference.Object, so does not need to
 762		 * increase the reference count
 763		 */
 764		if (source_desc->reference.class == ACPI_REFCLASS_TABLE) {
 765			break;
 766		}
 767
 768		acpi_ut_add_reference(source_desc->reference.object);
 769		break;
 770
 771	case ACPI_TYPE_REGION:
 772		/*
 773		 * We copied the Region Handler, so we now must add a reference
 774		 */
 775		if (dest_desc->region.handler) {
 776			acpi_ut_add_reference(dest_desc->region.handler);
 777		}
 778		break;
 779
 780		/*
 781		 * For Mutex and Event objects, we cannot simply copy the underlying
 782		 * OS object. We must create a new one.
 783		 */
 784	case ACPI_TYPE_MUTEX:
 785
 786		status = acpi_os_create_mutex(&dest_desc->mutex.os_mutex);
 787		if (ACPI_FAILURE(status)) {
 788			return (status);
 789		}
 790		break;
 791
 792	case ACPI_TYPE_EVENT:
 793
 794		status = acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0,
 795						  &dest_desc->event.
 796						  os_semaphore);
 797		if (ACPI_FAILURE(status)) {
 798			return (status);
 799		}
 800		break;
 801
 802	default:
 803
 804		/* Nothing to do for other simple objects */
 805
 806		break;
 807	}
 808
 809	return (AE_OK);
 810}
 811
 812/*******************************************************************************
 813 *
 814 * FUNCTION:    acpi_ut_copy_ielement_to_ielement
 815 *
 816 * PARAMETERS:  acpi_pkg_callback
 817 *
 818 * RETURN:      Status
 819 *
 820 * DESCRIPTION: Copy one package element to another package element
 821 *
 822 ******************************************************************************/
 823
 824static acpi_status
 825acpi_ut_copy_ielement_to_ielement(u8 object_type,
 826				  union acpi_operand_object *source_object,
 827				  union acpi_generic_state *state,
 828				  void *context)
 829{
 830	acpi_status status = AE_OK;
 831	u32 this_index;
 832	union acpi_operand_object **this_target_ptr;
 833	union acpi_operand_object *target_object;
 834
 835	ACPI_FUNCTION_ENTRY();
 836
 837	this_index = state->pkg.index;
 838	this_target_ptr = (union acpi_operand_object **)
 839	    &state->pkg.dest_object->package.elements[this_index];
 840
 841	switch (object_type) {
 842	case ACPI_COPY_TYPE_SIMPLE:
 843
 844		/* A null source object indicates a (legal) null package element */
 845
 846		if (source_object) {
 847			/*
 848			 * This is a simple object, just copy it
 849			 */
 850			target_object =
 851			    acpi_ut_create_internal_object(source_object->
 852							   common.type);
 853			if (!target_object) {
 854				return (AE_NO_MEMORY);
 855			}
 856
 857			status =
 858			    acpi_ut_copy_simple_object(source_object,
 859						       target_object);
 860			if (ACPI_FAILURE(status)) {
 861				goto error_exit;
 862			}
 863
 864			*this_target_ptr = target_object;
 865		} else {
 866			/* Pass through a null element */
 867
 868			*this_target_ptr = NULL;
 869		}
 870		break;
 871
 872	case ACPI_COPY_TYPE_PACKAGE:
 873		/*
 874		 * This object is a package - go down another nesting level
 875		 * Create and build the package object
 876		 */
 877		target_object =
 878		    acpi_ut_create_package_object(source_object->package.count);
 879		if (!target_object) {
 880			return (AE_NO_MEMORY);
 881		}
 882
 883		target_object->common.flags = source_object->common.flags;
 884
 885		/* Pass the new package object back to the package walk routine */
 886
 887		state->pkg.this_target_obj = target_object;
 888
 889		/* Store the object pointer in the parent package object */
 890
 891		*this_target_ptr = target_object;
 892		break;
 893
 894	default:
 895
 896		return (AE_BAD_PARAMETER);
 897	}
 898
 899	return (status);
 900
 901error_exit:
 902	acpi_ut_remove_reference(target_object);
 903	return (status);
 904}
 905
 906/*******************************************************************************
 907 *
 908 * FUNCTION:    acpi_ut_copy_ipackage_to_ipackage
 909 *
 910 * PARAMETERS:  source_obj      - Pointer to the source package object
 911 *              dest_obj        - Where the internal object is returned
 912 *              walk_state      - Current Walk state descriptor
 913 *
 914 * RETURN:      Status
 915 *
 916 * DESCRIPTION: This function is called to copy an internal package object
 917 *              into another internal package object.
 918 *
 919 ******************************************************************************/
 920
 921static acpi_status
 922acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
 923				  union acpi_operand_object *dest_obj,
 924				  struct acpi_walk_state *walk_state)
 925{
 926	acpi_status status = AE_OK;
 927
 928	ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
 929
 930	dest_obj->common.type = source_obj->common.type;
 931	dest_obj->common.flags = source_obj->common.flags;
 932	dest_obj->package.count = source_obj->package.count;
 933
 934	/*
 935	 * Create the object array and walk the source package tree
 936	 */
 937	dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
 938							   source_obj->package.
 939							   count +
 940							   1) * sizeof(void *));
 941	if (!dest_obj->package.elements) {
 942		ACPI_ERROR((AE_INFO, "Package allocation failure"));
 943		return_ACPI_STATUS(AE_NO_MEMORY);
 944	}
 945
 946	/*
 947	 * Copy the package element-by-element by walking the package "tree".
 948	 * This handles nested packages of arbitrary depth.
 949	 */
 950	status = acpi_ut_walk_package_tree(source_obj, dest_obj,
 951					   acpi_ut_copy_ielement_to_ielement,
 952					   walk_state);
 953	if (ACPI_FAILURE(status)) {
 954
 955		/* On failure, delete the destination package object */
 956
 957		acpi_ut_remove_reference(dest_obj);
 958	}
 959
 960	return_ACPI_STATUS(status);
 961}
 962
 963/*******************************************************************************
 964 *
 965 * FUNCTION:    acpi_ut_copy_iobject_to_iobject
 966 *
 967 * PARAMETERS:  source_desc         - The internal object to be copied
 968 *              dest_desc           - Where the copied object is returned
 969 *              walk_state          - Current walk state
 970 *
 971 * RETURN:      Status
 972 *
 973 * DESCRIPTION: Copy an internal object to a new internal object
 974 *
 975 ******************************************************************************/
 976
 977acpi_status
 978acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
 979				union acpi_operand_object **dest_desc,
 980				struct acpi_walk_state *walk_state)
 981{
 982	acpi_status status = AE_OK;
 983
 984	ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
 985
 986	/* Create the top level object */
 987
 988	*dest_desc = acpi_ut_create_internal_object(source_desc->common.type);
 989	if (!*dest_desc) {
 990		return_ACPI_STATUS(AE_NO_MEMORY);
 991	}
 992
 993	/* Copy the object and possible subobjects */
 994
 995	if (source_desc->common.type == ACPI_TYPE_PACKAGE) {
 996		status =
 997		    acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
 998						      walk_state);
 999	} else {
1000		status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
1001	}
1002
1003	/* Delete the allocated object if copy failed */
1004
1005	if (ACPI_FAILURE(status)) {
1006		acpi_ut_remove_reference(*dest_desc);
1007	}
1008
1009	return_ACPI_STATUS(status);
1010}
v5.4
  1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2/******************************************************************************
  3 *
  4 * Module Name: utcopy - Internal to external object translation utilities
  5 *
  6 * Copyright (C) 2000 - 2019, Intel Corp.
  7 *
  8 *****************************************************************************/
  9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 10#include <acpi/acpi.h>
 11#include "accommon.h"
 12#include "acnamesp.h"
 13
 14
 15#define _COMPONENT          ACPI_UTILITIES
 16ACPI_MODULE_NAME("utcopy")
 17
 18/* Local prototypes */
 19static acpi_status
 20acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
 21				union acpi_object *external_object,
 22				u8 *data_space, acpi_size *buffer_space_used);
 23
 24static acpi_status
 25acpi_ut_copy_ielement_to_ielement(u8 object_type,
 26				  union acpi_operand_object *source_object,
 27				  union acpi_generic_state *state,
 28				  void *context);
 29
 30static acpi_status
 31acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
 32				  u8 *buffer, acpi_size *space_used);
 33
 34static acpi_status
 35acpi_ut_copy_esimple_to_isimple(union acpi_object *user_obj,
 36				union acpi_operand_object **return_obj);
 37
 38static acpi_status
 39acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
 40				  union acpi_operand_object **internal_object);
 41
 42static acpi_status
 43acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
 44			   union acpi_operand_object *dest_desc);
 45
 46static acpi_status
 47acpi_ut_copy_ielement_to_eelement(u8 object_type,
 48				  union acpi_operand_object *source_object,
 49				  union acpi_generic_state *state,
 50				  void *context);
 51
 52static acpi_status
 53acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
 54				  union acpi_operand_object *dest_obj,
 55				  struct acpi_walk_state *walk_state);
 56
 57/*******************************************************************************
 58 *
 59 * FUNCTION:    acpi_ut_copy_isimple_to_esimple
 60 *
 61 * PARAMETERS:  internal_object     - Source object to be copied
 62 *              external_object     - Where to return the copied object
 63 *              data_space          - Where object data is returned (such as
 64 *                                    buffer and string data)
 65 *              buffer_space_used   - Length of data_space that was used
 66 *
 67 * RETURN:      Status
 68 *
 69 * DESCRIPTION: This function is called to copy a simple internal object to
 70 *              an external object.
 71 *
 72 *              The data_space buffer is assumed to have sufficient space for
 73 *              the object.
 74 *
 75 ******************************************************************************/
 76
 77static acpi_status
 78acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
 79				union acpi_object *external_object,
 80				u8 *data_space, acpi_size *buffer_space_used)
 81{
 82	acpi_status status = AE_OK;
 83
 84	ACPI_FUNCTION_TRACE(ut_copy_isimple_to_esimple);
 85
 86	*buffer_space_used = 0;
 87
 88	/*
 89	 * Check for NULL object case (could be an uninitialized
 90	 * package element)
 91	 */
 92	if (!internal_object) {
 93		return_ACPI_STATUS(AE_OK);
 94	}
 95
 96	/* Always clear the external object */
 97
 98	memset(external_object, 0, sizeof(union acpi_object));
 99
100	/*
101	 * In general, the external object will be the same type as
102	 * the internal object
103	 */
104	external_object->type = internal_object->common.type;
105
106	/* However, only a limited number of external types are supported */
107
108	switch (internal_object->common.type) {
109	case ACPI_TYPE_STRING:
110
111		external_object->string.pointer = (char *)data_space;
112		external_object->string.length = internal_object->string.length;
113		*buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
114								  internal_object->
115								  string.
116								  length + 1);
117
118		memcpy((void *)data_space,
119		       (void *)internal_object->string.pointer,
120		       (acpi_size)internal_object->string.length + 1);
121		break;
122
123	case ACPI_TYPE_BUFFER:
124
125		external_object->buffer.pointer = data_space;
126		external_object->buffer.length = internal_object->buffer.length;
127		*buffer_space_used =
128		    ACPI_ROUND_UP_TO_NATIVE_WORD(internal_object->string.
129						 length);
130
131		memcpy((void *)data_space,
132		       (void *)internal_object->buffer.pointer,
133		       internal_object->buffer.length);
134		break;
135
136	case ACPI_TYPE_INTEGER:
137
138		external_object->integer.value = internal_object->integer.value;
139		break;
140
141	case ACPI_TYPE_LOCAL_REFERENCE:
142
143		/* This is an object reference. */
144
145		switch (internal_object->reference.class) {
146		case ACPI_REFCLASS_NAME:
147			/*
148			 * For namepath, return the object handle ("reference")
149			 * We are referring to the namespace node
150			 */
151			external_object->reference.handle =
152			    internal_object->reference.node;
153			external_object->reference.actual_type =
154			    acpi_ns_get_type(internal_object->reference.node);
155			break;
156
157		default:
158
159			/* All other reference types are unsupported */
160
161			return_ACPI_STATUS(AE_TYPE);
162		}
163		break;
164
165	case ACPI_TYPE_PROCESSOR:
166
167		external_object->processor.proc_id =
168		    internal_object->processor.proc_id;
169		external_object->processor.pblk_address =
170		    internal_object->processor.address;
171		external_object->processor.pblk_length =
172		    internal_object->processor.length;
173		break;
174
175	case ACPI_TYPE_POWER:
176
177		external_object->power_resource.system_level =
178		    internal_object->power_resource.system_level;
179
180		external_object->power_resource.resource_order =
181		    internal_object->power_resource.resource_order;
182		break;
183
184	default:
185		/*
186		 * There is no corresponding external object type
187		 */
188		ACPI_ERROR((AE_INFO,
189			    "Unsupported object type, cannot convert to external object: %s",
190			    acpi_ut_get_type_name(internal_object->common.
191						  type)));
192
193		return_ACPI_STATUS(AE_SUPPORT);
194	}
195
196	return_ACPI_STATUS(status);
197}
198
199/*******************************************************************************
200 *
201 * FUNCTION:    acpi_ut_copy_ielement_to_eelement
202 *
203 * PARAMETERS:  acpi_pkg_callback
204 *
205 * RETURN:      Status
206 *
207 * DESCRIPTION: Copy one package element to another package element
208 *
209 ******************************************************************************/
210
211static acpi_status
212acpi_ut_copy_ielement_to_eelement(u8 object_type,
213				  union acpi_operand_object *source_object,
214				  union acpi_generic_state *state,
215				  void *context)
216{
217	acpi_status status = AE_OK;
218	struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
219	acpi_size object_space;
220	u32 this_index;
221	union acpi_object *target_object;
222
223	ACPI_FUNCTION_ENTRY();
224
225	this_index = state->pkg.index;
226	target_object = (union acpi_object *)&((union acpi_object *)
227					       (state->pkg.dest_object))->
228	    package.elements[this_index];
229
230	switch (object_type) {
231	case ACPI_COPY_TYPE_SIMPLE:
232		/*
233		 * This is a simple or null object
234		 */
235		status = acpi_ut_copy_isimple_to_esimple(source_object,
236							 target_object,
237							 info->free_space,
238							 &object_space);
239		if (ACPI_FAILURE(status)) {
240			return (status);
241		}
242		break;
243
244	case ACPI_COPY_TYPE_PACKAGE:
245		/*
246		 * Build the package object
247		 */
248		target_object->type = ACPI_TYPE_PACKAGE;
249		target_object->package.count = source_object->package.count;
250		target_object->package.elements =
251		    ACPI_CAST_PTR(union acpi_object, info->free_space);
252
253		/*
254		 * Pass the new package object back to the package walk routine
255		 */
256		state->pkg.this_target_obj = target_object;
257
258		/*
259		 * Save space for the array of objects (Package elements)
260		 * update the buffer length counter
261		 */
262		object_space = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
263							    target_object->
264							    package.count *
265							    sizeof(union
266								   acpi_object));
267		break;
268
269	default:
270
271		return (AE_BAD_PARAMETER);
272	}
273
274	info->free_space += object_space;
275	info->length += object_space;
276	return (status);
277}
278
279/*******************************************************************************
280 *
281 * FUNCTION:    acpi_ut_copy_ipackage_to_epackage
282 *
283 * PARAMETERS:  internal_object     - Pointer to the object we are returning
284 *              buffer              - Where the object is returned
285 *              space_used          - Where the object length is returned
286 *
287 * RETURN:      Status
288 *
289 * DESCRIPTION: This function is called to place a package object in a user
290 *              buffer. A package object by definition contains other objects.
291 *
292 *              The buffer is assumed to have sufficient space for the object.
293 *              The caller must have verified the buffer length needed using
294 *              the acpi_ut_get_object_size function before calling this function.
295 *
296 ******************************************************************************/
297
298static acpi_status
299acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
300				  u8 *buffer, acpi_size *space_used)
301{
302	union acpi_object *external_object;
303	acpi_status status;
304	struct acpi_pkg_info info;
305
306	ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_epackage);
307
308	/*
309	 * First package at head of the buffer
310	 */
311	external_object = ACPI_CAST_PTR(union acpi_object, buffer);
312
313	/*
314	 * Free space begins right after the first package
315	 */
316	info.length = ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
317	info.free_space = buffer +
318	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
319	info.object_space = 0;
320	info.num_packages = 1;
321
322	external_object->type = internal_object->common.type;
323	external_object->package.count = internal_object->package.count;
324	external_object->package.elements =
325	    ACPI_CAST_PTR(union acpi_object, info.free_space);
326
327	/*
328	 * Leave room for an array of ACPI_OBJECTS in the buffer
329	 * and move the free space past it
330	 */
331	info.length += (acpi_size)external_object->package.count *
332	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
333	info.free_space += external_object->package.count *
334	    ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
335
336	status = acpi_ut_walk_package_tree(internal_object, external_object,
337					   acpi_ut_copy_ielement_to_eelement,
338					   &info);
339
340	*space_used = info.length;
341	return_ACPI_STATUS(status);
342}
343
344/*******************************************************************************
345 *
346 * FUNCTION:    acpi_ut_copy_iobject_to_eobject
347 *
348 * PARAMETERS:  internal_object     - The internal object to be converted
349 *              ret_buffer          - Where the object is returned
350 *
351 * RETURN:      Status
352 *
353 * DESCRIPTION: This function is called to build an API object to be returned
354 *              to the caller.
355 *
356 ******************************************************************************/
357
358acpi_status
359acpi_ut_copy_iobject_to_eobject(union acpi_operand_object *internal_object,
360				struct acpi_buffer *ret_buffer)
361{
362	acpi_status status;
363
364	ACPI_FUNCTION_TRACE(ut_copy_iobject_to_eobject);
365
366	if (internal_object->common.type == ACPI_TYPE_PACKAGE) {
367		/*
368		 * Package object:  Copy all subobjects (including
369		 * nested packages)
370		 */
371		status = acpi_ut_copy_ipackage_to_epackage(internal_object,
372							   ret_buffer->pointer,
373							   &ret_buffer->length);
374	} else {
375		/*
376		 * Build a simple object (no nested objects)
377		 */
378		status = acpi_ut_copy_isimple_to_esimple(internal_object,
379							 ACPI_CAST_PTR(union
380								       acpi_object,
381								       ret_buffer->
382								       pointer),
383							 ACPI_ADD_PTR(u8,
384								      ret_buffer->
385								      pointer,
386								      ACPI_ROUND_UP_TO_NATIVE_WORD
387								      (sizeof
388								       (union
389									acpi_object))),
390							 &ret_buffer->length);
391		/*
392		 * build simple does not include the object size in the length
393		 * so we add it in here
394		 */
395		ret_buffer->length += sizeof(union acpi_object);
396	}
397
398	return_ACPI_STATUS(status);
399}
400
401/*******************************************************************************
402 *
403 * FUNCTION:    acpi_ut_copy_esimple_to_isimple
404 *
405 * PARAMETERS:  external_object     - The external object to be converted
406 *              ret_internal_object - Where the internal object is returned
407 *
408 * RETURN:      Status
409 *
410 * DESCRIPTION: This function copies an external object to an internal one.
411 *              NOTE: Pointers can be copied, we don't need to copy data.
412 *              (The pointers have to be valid in our address space no matter
413 *              what we do with them!)
414 *
415 ******************************************************************************/
416
417static acpi_status
418acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
419				union acpi_operand_object **ret_internal_object)
420{
421	union acpi_operand_object *internal_object;
422
423	ACPI_FUNCTION_TRACE(ut_copy_esimple_to_isimple);
424
425	/*
426	 * Simple types supported are: String, Buffer, Integer
427	 */
428	switch (external_object->type) {
429	case ACPI_TYPE_STRING:
430	case ACPI_TYPE_BUFFER:
431	case ACPI_TYPE_INTEGER:
432	case ACPI_TYPE_LOCAL_REFERENCE:
433
434		internal_object = acpi_ut_create_internal_object((u8)
435								 external_object->
436								 type);
437		if (!internal_object) {
438			return_ACPI_STATUS(AE_NO_MEMORY);
439		}
440		break;
441
442	case ACPI_TYPE_ANY:	/* This is the case for a NULL object */
443
444		*ret_internal_object = NULL;
445		return_ACPI_STATUS(AE_OK);
446
447	default:
448
449		/* All other types are not supported */
450
451		ACPI_ERROR((AE_INFO,
452			    "Unsupported object type, cannot convert to internal object: %s",
453			    acpi_ut_get_type_name(external_object->type)));
454
455		return_ACPI_STATUS(AE_SUPPORT);
456	}
457
458	/* Must COPY string and buffer contents */
459
460	switch (external_object->type) {
461	case ACPI_TYPE_STRING:
462
463		internal_object->string.pointer =
464		    ACPI_ALLOCATE_ZEROED((acpi_size)
465					 external_object->string.length + 1);
466
467		if (!internal_object->string.pointer) {
468			goto error_exit;
469		}
470
471		memcpy(internal_object->string.pointer,
472		       external_object->string.pointer,
473		       external_object->string.length);
474
475		internal_object->string.length = external_object->string.length;
476		break;
477
478	case ACPI_TYPE_BUFFER:
479
480		internal_object->buffer.pointer =
481		    ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
482		if (!internal_object->buffer.pointer) {
483			goto error_exit;
484		}
485
486		memcpy(internal_object->buffer.pointer,
487		       external_object->buffer.pointer,
488		       external_object->buffer.length);
489
490		internal_object->buffer.length = external_object->buffer.length;
491
492		/* Mark buffer data valid */
493
494		internal_object->buffer.flags |= AOPOBJ_DATA_VALID;
495		break;
496
497	case ACPI_TYPE_INTEGER:
498
499		internal_object->integer.value = external_object->integer.value;
500		break;
501
502	case ACPI_TYPE_LOCAL_REFERENCE:
503
504		/* An incoming reference is defined to be a namespace node */
505
506		internal_object->reference.class = ACPI_REFCLASS_REFOF;
507		internal_object->reference.object =
508		    external_object->reference.handle;
509		break;
510
511	default:
512
513		/* Other types can't get here */
514
515		break;
516	}
517
518	*ret_internal_object = internal_object;
519	return_ACPI_STATUS(AE_OK);
520
521error_exit:
522	acpi_ut_remove_reference(internal_object);
523	return_ACPI_STATUS(AE_NO_MEMORY);
524}
525
526/*******************************************************************************
527 *
528 * FUNCTION:    acpi_ut_copy_epackage_to_ipackage
529 *
530 * PARAMETERS:  external_object     - The external object to be converted
531 *              internal_object     - Where the internal object is returned
532 *
533 * RETURN:      Status
534 *
535 * DESCRIPTION: Copy an external package object to an internal package.
536 *              Handles nested packages.
537 *
538 ******************************************************************************/
539
540static acpi_status
541acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
542				  union acpi_operand_object **internal_object)
543{
544	acpi_status status = AE_OK;
545	union acpi_operand_object *package_object;
546	union acpi_operand_object **package_elements;
547	u32 i;
548
549	ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
550
551	/* Create the package object */
552
553	package_object =
554	    acpi_ut_create_package_object(external_object->package.count);
555	if (!package_object) {
556		return_ACPI_STATUS(AE_NO_MEMORY);
557	}
558
559	package_elements = package_object->package.elements;
560
561	/*
562	 * Recursive implementation. Probably ok, since nested external
563	 * packages as parameters should be very rare.
564	 */
565	for (i = 0; i < external_object->package.count; i++) {
566		status =
567		    acpi_ut_copy_eobject_to_iobject(&external_object->package.
568						    elements[i],
569						    &package_elements[i]);
570		if (ACPI_FAILURE(status)) {
571
572			/* Truncate package and delete it */
573
574			package_object->package.count = i;
575			package_elements[i] = NULL;
576			acpi_ut_remove_reference(package_object);
577			return_ACPI_STATUS(status);
578		}
579	}
580
581	/* Mark package data valid */
582
583	package_object->package.flags |= AOPOBJ_DATA_VALID;
584
585	*internal_object = package_object;
586	return_ACPI_STATUS(status);
587}
588
589/*******************************************************************************
590 *
591 * FUNCTION:    acpi_ut_copy_eobject_to_iobject
592 *
593 * PARAMETERS:  external_object     - The external object to be converted
594 *              internal_object     - Where the internal object is returned
595 *
596 * RETURN:      Status
597 *
598 * DESCRIPTION: Converts an external object to an internal object.
599 *
600 ******************************************************************************/
601
602acpi_status
603acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
604				union acpi_operand_object **internal_object)
605{
606	acpi_status status;
607
608	ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
609
610	if (external_object->type == ACPI_TYPE_PACKAGE) {
611		status =
612		    acpi_ut_copy_epackage_to_ipackage(external_object,
613						      internal_object);
614	} else {
615		/*
616		 * Build a simple object (no nested objects)
617		 */
618		status = acpi_ut_copy_esimple_to_isimple(external_object,
619							 internal_object);
620	}
621
622	return_ACPI_STATUS(status);
623}
624
625/*******************************************************************************
626 *
627 * FUNCTION:    acpi_ut_copy_simple_object
628 *
629 * PARAMETERS:  source_desc         - The internal object to be copied
630 *              dest_desc           - New target object
631 *
632 * RETURN:      Status
633 *
634 * DESCRIPTION: Simple copy of one internal object to another. Reference count
635 *              of the destination object is preserved.
636 *
637 ******************************************************************************/
638
639static acpi_status
640acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
641			   union acpi_operand_object *dest_desc)
642{
643	u16 reference_count;
644	union acpi_operand_object *next_object;
645	acpi_status status;
646	acpi_size copy_size;
647
648	/* Save fields from destination that we don't want to overwrite */
649
650	reference_count = dest_desc->common.reference_count;
651	next_object = dest_desc->common.next_object;
652
653	/*
654	 * Copy the entire source object over the destination object.
655	 * Note: Source can be either an operand object or namespace node.
656	 */
657	copy_size = sizeof(union acpi_operand_object);
658	if (ACPI_GET_DESCRIPTOR_TYPE(source_desc) == ACPI_DESC_TYPE_NAMED) {
659		copy_size = sizeof(struct acpi_namespace_node);
660	}
661
662	memcpy(ACPI_CAST_PTR(char, dest_desc),
663	       ACPI_CAST_PTR(char, source_desc), copy_size);
664
665	/* Restore the saved fields */
666
667	dest_desc->common.reference_count = reference_count;
668	dest_desc->common.next_object = next_object;
669
670	/* New object is not static, regardless of source */
671
672	dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
673
674	/* Handle the objects with extra data */
675
676	switch (dest_desc->common.type) {
677	case ACPI_TYPE_BUFFER:
678		/*
679		 * Allocate and copy the actual buffer if and only if:
680		 * 1) There is a valid buffer pointer
681		 * 2) The buffer has a length > 0
682		 */
683		if ((source_desc->buffer.pointer) &&
684		    (source_desc->buffer.length)) {
685			dest_desc->buffer.pointer =
686			    ACPI_ALLOCATE(source_desc->buffer.length);
687			if (!dest_desc->buffer.pointer) {
688				return (AE_NO_MEMORY);
689			}
690
691			/* Copy the actual buffer data */
692
693			memcpy(dest_desc->buffer.pointer,
694			       source_desc->buffer.pointer,
695			       source_desc->buffer.length);
696		}
697		break;
698
699	case ACPI_TYPE_STRING:
700		/*
701		 * Allocate and copy the actual string if and only if:
702		 * 1) There is a valid string pointer
703		 * (Pointer to a NULL string is allowed)
704		 */
705		if (source_desc->string.pointer) {
706			dest_desc->string.pointer =
707			    ACPI_ALLOCATE((acpi_size)source_desc->string.
708					  length + 1);
709			if (!dest_desc->string.pointer) {
710				return (AE_NO_MEMORY);
711			}
712
713			/* Copy the actual string data */
714
715			memcpy(dest_desc->string.pointer,
716			       source_desc->string.pointer,
717			       (acpi_size)source_desc->string.length + 1);
718		}
719		break;
720
721	case ACPI_TYPE_LOCAL_REFERENCE:
722		/*
723		 * We copied the reference object, so we now must add a reference
724		 * to the object pointed to by the reference
725		 *
726		 * DDBHandle reference (from Load/load_table) is a special reference,
727		 * it does not have a Reference.Object, so does not need to
728		 * increase the reference count
729		 */
730		if (source_desc->reference.class == ACPI_REFCLASS_TABLE) {
731			break;
732		}
733
734		acpi_ut_add_reference(source_desc->reference.object);
735		break;
736
737	case ACPI_TYPE_REGION:
738		/*
739		 * We copied the Region Handler, so we now must add a reference
740		 */
741		if (dest_desc->region.handler) {
742			acpi_ut_add_reference(dest_desc->region.handler);
743		}
744		break;
745
746		/*
747		 * For Mutex and Event objects, we cannot simply copy the underlying
748		 * OS object. We must create a new one.
749		 */
750	case ACPI_TYPE_MUTEX:
751
752		status = acpi_os_create_mutex(&dest_desc->mutex.os_mutex);
753		if (ACPI_FAILURE(status)) {
754			return (status);
755		}
756		break;
757
758	case ACPI_TYPE_EVENT:
759
760		status = acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0,
761						  &dest_desc->event.
762						  os_semaphore);
763		if (ACPI_FAILURE(status)) {
764			return (status);
765		}
766		break;
767
768	default:
769
770		/* Nothing to do for other simple objects */
771
772		break;
773	}
774
775	return (AE_OK);
776}
777
778/*******************************************************************************
779 *
780 * FUNCTION:    acpi_ut_copy_ielement_to_ielement
781 *
782 * PARAMETERS:  acpi_pkg_callback
783 *
784 * RETURN:      Status
785 *
786 * DESCRIPTION: Copy one package element to another package element
787 *
788 ******************************************************************************/
789
790static acpi_status
791acpi_ut_copy_ielement_to_ielement(u8 object_type,
792				  union acpi_operand_object *source_object,
793				  union acpi_generic_state *state,
794				  void *context)
795{
796	acpi_status status = AE_OK;
797	u32 this_index;
798	union acpi_operand_object **this_target_ptr;
799	union acpi_operand_object *target_object;
800
801	ACPI_FUNCTION_ENTRY();
802
803	this_index = state->pkg.index;
804	this_target_ptr = (union acpi_operand_object **)
805	    &state->pkg.dest_object->package.elements[this_index];
806
807	switch (object_type) {
808	case ACPI_COPY_TYPE_SIMPLE:
809
810		/* A null source object indicates a (legal) null package element */
811
812		if (source_object) {
813			/*
814			 * This is a simple object, just copy it
815			 */
816			target_object =
817			    acpi_ut_create_internal_object(source_object->
818							   common.type);
819			if (!target_object) {
820				return (AE_NO_MEMORY);
821			}
822
823			status =
824			    acpi_ut_copy_simple_object(source_object,
825						       target_object);
826			if (ACPI_FAILURE(status)) {
827				goto error_exit;
828			}
829
830			*this_target_ptr = target_object;
831		} else {
832			/* Pass through a null element */
833
834			*this_target_ptr = NULL;
835		}
836		break;
837
838	case ACPI_COPY_TYPE_PACKAGE:
839		/*
840		 * This object is a package - go down another nesting level
841		 * Create and build the package object
842		 */
843		target_object =
844		    acpi_ut_create_package_object(source_object->package.count);
845		if (!target_object) {
846			return (AE_NO_MEMORY);
847		}
848
849		target_object->common.flags = source_object->common.flags;
850
851		/* Pass the new package object back to the package walk routine */
852
853		state->pkg.this_target_obj = target_object;
854
855		/* Store the object pointer in the parent package object */
856
857		*this_target_ptr = target_object;
858		break;
859
860	default:
861
862		return (AE_BAD_PARAMETER);
863	}
864
865	return (status);
866
867error_exit:
868	acpi_ut_remove_reference(target_object);
869	return (status);
870}
871
872/*******************************************************************************
873 *
874 * FUNCTION:    acpi_ut_copy_ipackage_to_ipackage
875 *
876 * PARAMETERS:  source_obj      - Pointer to the source package object
877 *              dest_obj        - Where the internal object is returned
878 *              walk_state      - Current Walk state descriptor
879 *
880 * RETURN:      Status
881 *
882 * DESCRIPTION: This function is called to copy an internal package object
883 *              into another internal package object.
884 *
885 ******************************************************************************/
886
887static acpi_status
888acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
889				  union acpi_operand_object *dest_obj,
890				  struct acpi_walk_state *walk_state)
891{
892	acpi_status status = AE_OK;
893
894	ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
895
896	dest_obj->common.type = source_obj->common.type;
897	dest_obj->common.flags = source_obj->common.flags;
898	dest_obj->package.count = source_obj->package.count;
899
900	/*
901	 * Create the object array and walk the source package tree
902	 */
903	dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
904							   source_obj->package.
905							   count +
906							   1) * sizeof(void *));
907	if (!dest_obj->package.elements) {
908		ACPI_ERROR((AE_INFO, "Package allocation failure"));
909		return_ACPI_STATUS(AE_NO_MEMORY);
910	}
911
912	/*
913	 * Copy the package element-by-element by walking the package "tree".
914	 * This handles nested packages of arbitrary depth.
915	 */
916	status = acpi_ut_walk_package_tree(source_obj, dest_obj,
917					   acpi_ut_copy_ielement_to_ielement,
918					   walk_state);
919	if (ACPI_FAILURE(status)) {
920
921		/* On failure, delete the destination package object */
922
923		acpi_ut_remove_reference(dest_obj);
924	}
925
926	return_ACPI_STATUS(status);
927}
928
929/*******************************************************************************
930 *
931 * FUNCTION:    acpi_ut_copy_iobject_to_iobject
932 *
933 * PARAMETERS:  source_desc         - The internal object to be copied
934 *              dest_desc           - Where the copied object is returned
935 *              walk_state          - Current walk state
936 *
937 * RETURN:      Status
938 *
939 * DESCRIPTION: Copy an internal object to a new internal object
940 *
941 ******************************************************************************/
942
943acpi_status
944acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
945				union acpi_operand_object **dest_desc,
946				struct acpi_walk_state *walk_state)
947{
948	acpi_status status = AE_OK;
949
950	ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
951
952	/* Create the top level object */
953
954	*dest_desc = acpi_ut_create_internal_object(source_desc->common.type);
955	if (!*dest_desc) {
956		return_ACPI_STATUS(AE_NO_MEMORY);
957	}
958
959	/* Copy the object and possible subobjects */
960
961	if (source_desc->common.type == ACPI_TYPE_PACKAGE) {
962		status =
963		    acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
964						      walk_state);
965	} else {
966		status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
967	}
968
969	/* Delete the allocated object if copy failed */
970
971	if (ACPI_FAILURE(status)) {
972		acpi_ut_remove_reference(*dest_desc);
973	}
974
975	return_ACPI_STATUS(status);
976}