utcopy.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /******************************************************************************
  2. *
  3. * Module Name: utcopy - Internal to external object translation utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2007, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include <acpi/amlcode.h>
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utcopy")
  46. /* Local prototypes */
  47. static acpi_status
  48. acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
  49. union acpi_object *external_object,
  50. u8 * data_space, acpi_size * buffer_space_used);
  51. static acpi_status
  52. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  53. union acpi_operand_object *source_object,
  54. union acpi_generic_state *state,
  55. void *context);
  56. static acpi_status
  57. acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
  58. u8 * buffer, acpi_size * space_used);
  59. static acpi_status
  60. acpi_ut_copy_esimple_to_isimple(union acpi_object *user_obj,
  61. union acpi_operand_object **return_obj);
  62. static acpi_status
  63. acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
  64. union acpi_operand_object **internal_object);
  65. static acpi_status
  66. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  67. union acpi_operand_object *dest_desc);
  68. static acpi_status
  69. acpi_ut_copy_ielement_to_eelement(u8 object_type,
  70. union acpi_operand_object *source_object,
  71. union acpi_generic_state *state,
  72. void *context);
  73. static acpi_status
  74. acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  75. union acpi_operand_object *dest_obj,
  76. struct acpi_walk_state *walk_state);
  77. /*******************************************************************************
  78. *
  79. * FUNCTION: acpi_ut_copy_isimple_to_esimple
  80. *
  81. * PARAMETERS: internal_object - Source object to be copied
  82. * external_object - Where to return the copied object
  83. * data_space - Where object data is returned (such as
  84. * buffer and string data)
  85. * buffer_space_used - Length of data_space that was used
  86. *
  87. * RETURN: Status
  88. *
  89. * DESCRIPTION: This function is called to copy a simple internal object to
  90. * an external object.
  91. *
  92. * The data_space buffer is assumed to have sufficient space for
  93. * the object.
  94. *
  95. ******************************************************************************/
  96. static acpi_status
  97. acpi_ut_copy_isimple_to_esimple(union acpi_operand_object *internal_object,
  98. union acpi_object *external_object,
  99. u8 * data_space, acpi_size * buffer_space_used)
  100. {
  101. acpi_status status = AE_OK;
  102. ACPI_FUNCTION_TRACE(ut_copy_isimple_to_esimple);
  103. *buffer_space_used = 0;
  104. /*
  105. * Check for NULL object case (could be an uninitialized
  106. * package element)
  107. */
  108. if (!internal_object) {
  109. return_ACPI_STATUS(AE_OK);
  110. }
  111. /* Always clear the external object */
  112. ACPI_MEMSET(external_object, 0, sizeof(union acpi_object));
  113. /*
  114. * In general, the external object will be the same type as
  115. * the internal object
  116. */
  117. external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
  118. /* However, only a limited number of external types are supported */
  119. switch (ACPI_GET_OBJECT_TYPE(internal_object)) {
  120. case ACPI_TYPE_STRING:
  121. external_object->string.pointer = (char *)data_space;
  122. external_object->string.length = internal_object->string.length;
  123. *buffer_space_used = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
  124. internal_object->
  125. string.
  126. length + 1);
  127. ACPI_MEMCPY((void *)data_space,
  128. (void *)internal_object->string.pointer,
  129. (acpi_size) internal_object->string.length + 1);
  130. break;
  131. case ACPI_TYPE_BUFFER:
  132. external_object->buffer.pointer = data_space;
  133. external_object->buffer.length = internal_object->buffer.length;
  134. *buffer_space_used =
  135. ACPI_ROUND_UP_TO_NATIVE_WORD(internal_object->string.
  136. length);
  137. ACPI_MEMCPY((void *)data_space,
  138. (void *)internal_object->buffer.pointer,
  139. internal_object->buffer.length);
  140. break;
  141. case ACPI_TYPE_INTEGER:
  142. external_object->integer.value = internal_object->integer.value;
  143. break;
  144. case ACPI_TYPE_LOCAL_REFERENCE:
  145. /*
  146. * This is an object reference. Attempt to dereference it.
  147. */
  148. switch (internal_object->reference.opcode) {
  149. case AML_INT_NAMEPATH_OP:
  150. /* For namepath, return the object handle ("reference") */
  151. default:
  152. /*
  153. * Use the object type of "Any" to indicate a reference
  154. * to object containing a handle to an ACPI named object.
  155. */
  156. external_object->type = ACPI_TYPE_ANY;
  157. external_object->reference.handle =
  158. internal_object->reference.node;
  159. break;
  160. }
  161. break;
  162. case ACPI_TYPE_PROCESSOR:
  163. external_object->processor.proc_id =
  164. internal_object->processor.proc_id;
  165. external_object->processor.pblk_address =
  166. internal_object->processor.address;
  167. external_object->processor.pblk_length =
  168. internal_object->processor.length;
  169. break;
  170. case ACPI_TYPE_POWER:
  171. external_object->power_resource.system_level =
  172. internal_object->power_resource.system_level;
  173. external_object->power_resource.resource_order =
  174. internal_object->power_resource.resource_order;
  175. break;
  176. default:
  177. /*
  178. * There is no corresponding external object type
  179. */
  180. ACPI_ERROR((AE_INFO,
  181. "Unsupported object type, cannot convert to external object: %s",
  182. acpi_ut_get_type_name(ACPI_GET_OBJECT_TYPE
  183. (internal_object))));
  184. return_ACPI_STATUS(AE_SUPPORT);
  185. }
  186. return_ACPI_STATUS(status);
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_ut_copy_ielement_to_eelement
  191. *
  192. * PARAMETERS: acpi_pkg_callback
  193. *
  194. * RETURN: Status
  195. *
  196. * DESCRIPTION: Copy one package element to another package element
  197. *
  198. ******************************************************************************/
  199. static acpi_status
  200. acpi_ut_copy_ielement_to_eelement(u8 object_type,
  201. union acpi_operand_object *source_object,
  202. union acpi_generic_state *state,
  203. void *context)
  204. {
  205. acpi_status status = AE_OK;
  206. struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
  207. acpi_size object_space;
  208. u32 this_index;
  209. union acpi_object *target_object;
  210. ACPI_FUNCTION_ENTRY();
  211. this_index = state->pkg.index;
  212. target_object = (union acpi_object *)
  213. &((union acpi_object *)(state->pkg.dest_object))->package.
  214. elements[this_index];
  215. switch (object_type) {
  216. case ACPI_COPY_TYPE_SIMPLE:
  217. /*
  218. * This is a simple or null object
  219. */
  220. status = acpi_ut_copy_isimple_to_esimple(source_object,
  221. target_object,
  222. info->free_space,
  223. &object_space);
  224. if (ACPI_FAILURE(status)) {
  225. return (status);
  226. }
  227. break;
  228. case ACPI_COPY_TYPE_PACKAGE:
  229. /*
  230. * Build the package object
  231. */
  232. target_object->type = ACPI_TYPE_PACKAGE;
  233. target_object->package.count = source_object->package.count;
  234. target_object->package.elements =
  235. ACPI_CAST_PTR(union acpi_object, info->free_space);
  236. /*
  237. * Pass the new package object back to the package walk routine
  238. */
  239. state->pkg.this_target_obj = target_object;
  240. /*
  241. * Save space for the array of objects (Package elements)
  242. * update the buffer length counter
  243. */
  244. object_space = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
  245. target_object->
  246. package.count *
  247. sizeof(union
  248. acpi_object));
  249. break;
  250. default:
  251. return (AE_BAD_PARAMETER);
  252. }
  253. info->free_space += object_space;
  254. info->length += object_space;
  255. return (status);
  256. }
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_ut_copy_ipackage_to_epackage
  260. *
  261. * PARAMETERS: internal_object - Pointer to the object we are returning
  262. * Buffer - Where the object is returned
  263. * space_used - Where the object length is returned
  264. *
  265. * RETURN: Status
  266. *
  267. * DESCRIPTION: This function is called to place a package object in a user
  268. * buffer. A package object by definition contains other objects.
  269. *
  270. * The buffer is assumed to have sufficient space for the object.
  271. * The caller must have verified the buffer length needed using the
  272. * acpi_ut_get_object_size function before calling this function.
  273. *
  274. ******************************************************************************/
  275. static acpi_status
  276. acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
  277. u8 * buffer, acpi_size * space_used)
  278. {
  279. union acpi_object *external_object;
  280. acpi_status status;
  281. struct acpi_pkg_info info;
  282. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_epackage);
  283. /*
  284. * First package at head of the buffer
  285. */
  286. external_object = ACPI_CAST_PTR(union acpi_object, buffer);
  287. /*
  288. * Free space begins right after the first package
  289. */
  290. info.length = ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  291. info.free_space =
  292. buffer + ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  293. info.object_space = 0;
  294. info.num_packages = 1;
  295. external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
  296. external_object->package.count = internal_object->package.count;
  297. external_object->package.elements = ACPI_CAST_PTR(union acpi_object,
  298. info.free_space);
  299. /*
  300. * Leave room for an array of ACPI_OBJECTS in the buffer
  301. * and move the free space past it
  302. */
  303. info.length += (acpi_size) external_object->package.count *
  304. ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  305. info.free_space += external_object->package.count *
  306. ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  307. status = acpi_ut_walk_package_tree(internal_object, external_object,
  308. acpi_ut_copy_ielement_to_eelement,
  309. &info);
  310. *space_used = info.length;
  311. return_ACPI_STATUS(status);
  312. }
  313. /*******************************************************************************
  314. *
  315. * FUNCTION: acpi_ut_copy_iobject_to_eobject
  316. *
  317. * PARAMETERS: internal_object - The internal object to be converted
  318. * buffer_ptr - Where the object is returned
  319. *
  320. * RETURN: Status
  321. *
  322. * DESCRIPTION: This function is called to build an API object to be returned to
  323. * the caller.
  324. *
  325. ******************************************************************************/
  326. acpi_status
  327. acpi_ut_copy_iobject_to_eobject(union acpi_operand_object *internal_object,
  328. struct acpi_buffer *ret_buffer)
  329. {
  330. acpi_status status;
  331. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_eobject);
  332. if (ACPI_GET_OBJECT_TYPE(internal_object) == ACPI_TYPE_PACKAGE) {
  333. /*
  334. * Package object: Copy all subobjects (including
  335. * nested packages)
  336. */
  337. status = acpi_ut_copy_ipackage_to_epackage(internal_object,
  338. ret_buffer->pointer,
  339. &ret_buffer->length);
  340. } else {
  341. /*
  342. * Build a simple object (no nested objects)
  343. */
  344. status = acpi_ut_copy_isimple_to_esimple(internal_object,
  345. ACPI_CAST_PTR(union
  346. acpi_object,
  347. ret_buffer->
  348. pointer),
  349. ACPI_ADD_PTR(u8,
  350. ret_buffer->
  351. pointer,
  352. ACPI_ROUND_UP_TO_NATIVE_WORD
  353. (sizeof
  354. (union
  355. acpi_object))),
  356. &ret_buffer->length);
  357. /*
  358. * build simple does not include the object size in the length
  359. * so we add it in here
  360. */
  361. ret_buffer->length += sizeof(union acpi_object);
  362. }
  363. return_ACPI_STATUS(status);
  364. }
  365. /*******************************************************************************
  366. *
  367. * FUNCTION: acpi_ut_copy_esimple_to_isimple
  368. *
  369. * PARAMETERS: external_object - The external object to be converted
  370. * ret_internal_object - Where the internal object is returned
  371. *
  372. * RETURN: Status
  373. *
  374. * DESCRIPTION: This function copies an external object to an internal one.
  375. * NOTE: Pointers can be copied, we don't need to copy data.
  376. * (The pointers have to be valid in our address space no matter
  377. * what we do with them!)
  378. *
  379. ******************************************************************************/
  380. static acpi_status
  381. acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
  382. union acpi_operand_object **ret_internal_object)
  383. {
  384. union acpi_operand_object *internal_object;
  385. ACPI_FUNCTION_TRACE(ut_copy_esimple_to_isimple);
  386. /*
  387. * Simple types supported are: String, Buffer, Integer
  388. */
  389. switch (external_object->type) {
  390. case ACPI_TYPE_STRING:
  391. case ACPI_TYPE_BUFFER:
  392. case ACPI_TYPE_INTEGER:
  393. internal_object = acpi_ut_create_internal_object((u8)
  394. external_object->
  395. type);
  396. if (!internal_object) {
  397. return_ACPI_STATUS(AE_NO_MEMORY);
  398. }
  399. break;
  400. default:
  401. /* All other types are not supported */
  402. ACPI_ERROR((AE_INFO,
  403. "Unsupported object type, cannot convert to internal object: %s",
  404. acpi_ut_get_type_name(external_object->type)));
  405. return_ACPI_STATUS(AE_SUPPORT);
  406. }
  407. /* Must COPY string and buffer contents */
  408. switch (external_object->type) {
  409. case ACPI_TYPE_STRING:
  410. internal_object->string.pointer =
  411. ACPI_ALLOCATE_ZEROED((acpi_size) external_object->string.
  412. length + 1);
  413. if (!internal_object->string.pointer) {
  414. goto error_exit;
  415. }
  416. ACPI_MEMCPY(internal_object->string.pointer,
  417. external_object->string.pointer,
  418. external_object->string.length);
  419. internal_object->string.length = external_object->string.length;
  420. break;
  421. case ACPI_TYPE_BUFFER:
  422. internal_object->buffer.pointer =
  423. ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
  424. if (!internal_object->buffer.pointer) {
  425. goto error_exit;
  426. }
  427. ACPI_MEMCPY(internal_object->buffer.pointer,
  428. external_object->buffer.pointer,
  429. external_object->buffer.length);
  430. internal_object->buffer.length = external_object->buffer.length;
  431. /* Mark buffer data valid */
  432. internal_object->buffer.flags |= AOPOBJ_DATA_VALID;
  433. break;
  434. case ACPI_TYPE_INTEGER:
  435. internal_object->integer.value = external_object->integer.value;
  436. break;
  437. default:
  438. /* Other types can't get here */
  439. break;
  440. }
  441. *ret_internal_object = internal_object;
  442. return_ACPI_STATUS(AE_OK);
  443. error_exit:
  444. acpi_ut_remove_reference(internal_object);
  445. return_ACPI_STATUS(AE_NO_MEMORY);
  446. }
  447. /*******************************************************************************
  448. *
  449. * FUNCTION: acpi_ut_copy_epackage_to_ipackage
  450. *
  451. * PARAMETERS: external_object - The external object to be converted
  452. * internal_object - Where the internal object is returned
  453. *
  454. * RETURN: Status
  455. *
  456. * DESCRIPTION: Copy an external package object to an internal package.
  457. * Handles nested packages.
  458. *
  459. ******************************************************************************/
  460. static acpi_status
  461. acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
  462. union acpi_operand_object **internal_object)
  463. {
  464. acpi_status status = AE_OK;
  465. union acpi_operand_object *package_object;
  466. union acpi_operand_object **package_elements;
  467. acpi_native_uint i;
  468. ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
  469. /* Create the package object */
  470. package_object =
  471. acpi_ut_create_package_object(external_object->package.count);
  472. if (!package_object) {
  473. return_ACPI_STATUS(AE_NO_MEMORY);
  474. }
  475. package_elements = package_object->package.elements;
  476. /*
  477. * Recursive implementation. Probably ok, since nested external packages
  478. * as parameters should be very rare.
  479. */
  480. for (i = 0; i < external_object->package.count; i++) {
  481. status =
  482. acpi_ut_copy_eobject_to_iobject(&external_object->package.
  483. elements[i],
  484. &package_elements[i]);
  485. if (ACPI_FAILURE(status)) {
  486. /* Truncate package and delete it */
  487. package_object->package.count = (u32) i;
  488. package_elements[i] = NULL;
  489. acpi_ut_remove_reference(package_object);
  490. return_ACPI_STATUS(status);
  491. }
  492. }
  493. /* Mark package data valid */
  494. package_object->package.flags |= AOPOBJ_DATA_VALID;
  495. *internal_object = package_object;
  496. return_ACPI_STATUS(status);
  497. }
  498. /*******************************************************************************
  499. *
  500. * FUNCTION: acpi_ut_copy_eobject_to_iobject
  501. *
  502. * PARAMETERS: external_object - The external object to be converted
  503. * internal_object - Where the internal object is returned
  504. *
  505. * RETURN: Status - the status of the call
  506. *
  507. * DESCRIPTION: Converts an external object to an internal object.
  508. *
  509. ******************************************************************************/
  510. acpi_status
  511. acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
  512. union acpi_operand_object **internal_object)
  513. {
  514. acpi_status status;
  515. ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
  516. if (external_object->type == ACPI_TYPE_PACKAGE) {
  517. status =
  518. acpi_ut_copy_epackage_to_ipackage(external_object,
  519. internal_object);
  520. } else {
  521. /*
  522. * Build a simple object (no nested objects)
  523. */
  524. status =
  525. acpi_ut_copy_esimple_to_isimple(external_object,
  526. internal_object);
  527. }
  528. return_ACPI_STATUS(status);
  529. }
  530. /*******************************************************************************
  531. *
  532. * FUNCTION: acpi_ut_copy_simple_object
  533. *
  534. * PARAMETERS: source_desc - The internal object to be copied
  535. * dest_desc - New target object
  536. *
  537. * RETURN: Status
  538. *
  539. * DESCRIPTION: Simple copy of one internal object to another. Reference count
  540. * of the destination object is preserved.
  541. *
  542. ******************************************************************************/
  543. static acpi_status
  544. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  545. union acpi_operand_object *dest_desc)
  546. {
  547. u16 reference_count;
  548. union acpi_operand_object *next_object;
  549. /* Save fields from destination that we don't want to overwrite */
  550. reference_count = dest_desc->common.reference_count;
  551. next_object = dest_desc->common.next_object;
  552. /* Copy the entire source object over the destination object */
  553. ACPI_MEMCPY((char *)dest_desc, (char *)source_desc,
  554. sizeof(union acpi_operand_object));
  555. /* Restore the saved fields */
  556. dest_desc->common.reference_count = reference_count;
  557. dest_desc->common.next_object = next_object;
  558. /* New object is not static, regardless of source */
  559. dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  560. /* Handle the objects with extra data */
  561. switch (ACPI_GET_OBJECT_TYPE(dest_desc)) {
  562. case ACPI_TYPE_BUFFER:
  563. /*
  564. * Allocate and copy the actual buffer if and only if:
  565. * 1) There is a valid buffer pointer
  566. * 2) The buffer has a length > 0
  567. */
  568. if ((source_desc->buffer.pointer) &&
  569. (source_desc->buffer.length)) {
  570. dest_desc->buffer.pointer =
  571. ACPI_ALLOCATE(source_desc->buffer.length);
  572. if (!dest_desc->buffer.pointer) {
  573. return (AE_NO_MEMORY);
  574. }
  575. /* Copy the actual buffer data */
  576. ACPI_MEMCPY(dest_desc->buffer.pointer,
  577. source_desc->buffer.pointer,
  578. source_desc->buffer.length);
  579. }
  580. break;
  581. case ACPI_TYPE_STRING:
  582. /*
  583. * Allocate and copy the actual string if and only if:
  584. * 1) There is a valid string pointer
  585. * (Pointer to a NULL string is allowed)
  586. */
  587. if (source_desc->string.pointer) {
  588. dest_desc->string.pointer =
  589. ACPI_ALLOCATE((acpi_size) source_desc->string.
  590. length + 1);
  591. if (!dest_desc->string.pointer) {
  592. return (AE_NO_MEMORY);
  593. }
  594. /* Copy the actual string data */
  595. ACPI_MEMCPY(dest_desc->string.pointer,
  596. source_desc->string.pointer,
  597. (acpi_size) source_desc->string.length + 1);
  598. }
  599. break;
  600. case ACPI_TYPE_LOCAL_REFERENCE:
  601. /*
  602. * We copied the reference object, so we now must add a reference
  603. * to the object pointed to by the reference
  604. *
  605. * DDBHandle reference (from Load/load_table is a special reference,
  606. * it's Reference.Object is the table index, so does not need to
  607. * increase the reference count
  608. */
  609. if (source_desc->reference.opcode == AML_LOAD_OP) {
  610. break;
  611. }
  612. acpi_ut_add_reference(source_desc->reference.object);
  613. break;
  614. case ACPI_TYPE_REGION:
  615. /*
  616. * We copied the Region Handler, so we now must add a reference
  617. */
  618. if (dest_desc->region.handler) {
  619. acpi_ut_add_reference(dest_desc->region.handler);
  620. }
  621. break;
  622. default:
  623. /* Nothing to do for other simple objects */
  624. break;
  625. }
  626. return (AE_OK);
  627. }
  628. /*******************************************************************************
  629. *
  630. * FUNCTION: acpi_ut_copy_ielement_to_ielement
  631. *
  632. * PARAMETERS: acpi_pkg_callback
  633. *
  634. * RETURN: Status
  635. *
  636. * DESCRIPTION: Copy one package element to another package element
  637. *
  638. ******************************************************************************/
  639. static acpi_status
  640. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  641. union acpi_operand_object *source_object,
  642. union acpi_generic_state *state,
  643. void *context)
  644. {
  645. acpi_status status = AE_OK;
  646. u32 this_index;
  647. union acpi_operand_object **this_target_ptr;
  648. union acpi_operand_object *target_object;
  649. ACPI_FUNCTION_ENTRY();
  650. this_index = state->pkg.index;
  651. this_target_ptr = (union acpi_operand_object **)
  652. &state->pkg.dest_object->package.elements[this_index];
  653. switch (object_type) {
  654. case ACPI_COPY_TYPE_SIMPLE:
  655. /* A null source object indicates a (legal) null package element */
  656. if (source_object) {
  657. /*
  658. * This is a simple object, just copy it
  659. */
  660. target_object =
  661. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE
  662. (source_object));
  663. if (!target_object) {
  664. return (AE_NO_MEMORY);
  665. }
  666. status =
  667. acpi_ut_copy_simple_object(source_object,
  668. target_object);
  669. if (ACPI_FAILURE(status)) {
  670. goto error_exit;
  671. }
  672. *this_target_ptr = target_object;
  673. } else {
  674. /* Pass through a null element */
  675. *this_target_ptr = NULL;
  676. }
  677. break;
  678. case ACPI_COPY_TYPE_PACKAGE:
  679. /*
  680. * This object is a package - go down another nesting level
  681. * Create and build the package object
  682. */
  683. target_object =
  684. acpi_ut_create_package_object(source_object->package.count);
  685. if (!target_object) {
  686. return (AE_NO_MEMORY);
  687. }
  688. target_object->common.flags = source_object->common.flags;
  689. /* Pass the new package object back to the package walk routine */
  690. state->pkg.this_target_obj = target_object;
  691. /* Store the object pointer in the parent package object */
  692. *this_target_ptr = target_object;
  693. break;
  694. default:
  695. return (AE_BAD_PARAMETER);
  696. }
  697. return (status);
  698. error_exit:
  699. acpi_ut_remove_reference(target_object);
  700. return (status);
  701. }
  702. /*******************************************************************************
  703. *
  704. * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
  705. *
  706. * PARAMETERS: *source_obj - Pointer to the source package object
  707. * *dest_obj - Where the internal object is returned
  708. *
  709. * RETURN: Status - the status of the call
  710. *
  711. * DESCRIPTION: This function is called to copy an internal package object
  712. * into another internal package object.
  713. *
  714. ******************************************************************************/
  715. static acpi_status
  716. acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  717. union acpi_operand_object *dest_obj,
  718. struct acpi_walk_state *walk_state)
  719. {
  720. acpi_status status = AE_OK;
  721. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
  722. dest_obj->common.type = ACPI_GET_OBJECT_TYPE(source_obj);
  723. dest_obj->common.flags = source_obj->common.flags;
  724. dest_obj->package.count = source_obj->package.count;
  725. /*
  726. * Create the object array and walk the source package tree
  727. */
  728. dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
  729. source_obj->package.
  730. count +
  731. 1) * sizeof(void *));
  732. if (!dest_obj->package.elements) {
  733. ACPI_ERROR((AE_INFO, "Package allocation failure"));
  734. return_ACPI_STATUS(AE_NO_MEMORY);
  735. }
  736. /*
  737. * Copy the package element-by-element by walking the package "tree".
  738. * This handles nested packages of arbitrary depth.
  739. */
  740. status = acpi_ut_walk_package_tree(source_obj, dest_obj,
  741. acpi_ut_copy_ielement_to_ielement,
  742. walk_state);
  743. if (ACPI_FAILURE(status)) {
  744. /* On failure, delete the destination package object */
  745. acpi_ut_remove_reference(dest_obj);
  746. }
  747. return_ACPI_STATUS(status);
  748. }
  749. /*******************************************************************************
  750. *
  751. * FUNCTION: acpi_ut_copy_iobject_to_iobject
  752. *
  753. * PARAMETERS: walk_state - Current walk state
  754. * source_desc - The internal object to be copied
  755. * dest_desc - Where the copied object is returned
  756. *
  757. * RETURN: Status
  758. *
  759. * DESCRIPTION: Copy an internal object to a new internal object
  760. *
  761. ******************************************************************************/
  762. acpi_status
  763. acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
  764. union acpi_operand_object **dest_desc,
  765. struct acpi_walk_state *walk_state)
  766. {
  767. acpi_status status = AE_OK;
  768. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
  769. /* Create the top level object */
  770. *dest_desc =
  771. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE(source_desc));
  772. if (!*dest_desc) {
  773. return_ACPI_STATUS(AE_NO_MEMORY);
  774. }
  775. /* Copy the object and possible subobjects */
  776. if (ACPI_GET_OBJECT_TYPE(source_desc) == ACPI_TYPE_PACKAGE) {
  777. status =
  778. acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
  779. walk_state);
  780. } else {
  781. status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
  782. }
  783. return_ACPI_STATUS(status);
  784. }