utcopy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. return_ACPI_STATUS(AE_SUPPORT);
  181. }
  182. return_ACPI_STATUS(status);
  183. }
  184. /*******************************************************************************
  185. *
  186. * FUNCTION: acpi_ut_copy_ielement_to_eelement
  187. *
  188. * PARAMETERS: acpi_pkg_callback
  189. *
  190. * RETURN: Status
  191. *
  192. * DESCRIPTION: Copy one package element to another package element
  193. *
  194. ******************************************************************************/
  195. static acpi_status
  196. acpi_ut_copy_ielement_to_eelement(u8 object_type,
  197. union acpi_operand_object *source_object,
  198. union acpi_generic_state *state,
  199. void *context)
  200. {
  201. acpi_status status = AE_OK;
  202. struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
  203. acpi_size object_space;
  204. u32 this_index;
  205. union acpi_object *target_object;
  206. ACPI_FUNCTION_ENTRY();
  207. this_index = state->pkg.index;
  208. target_object = (union acpi_object *)
  209. &((union acpi_object *)(state->pkg.dest_object))->package.
  210. elements[this_index];
  211. switch (object_type) {
  212. case ACPI_COPY_TYPE_SIMPLE:
  213. /*
  214. * This is a simple or null object
  215. */
  216. status = acpi_ut_copy_isimple_to_esimple(source_object,
  217. target_object,
  218. info->free_space,
  219. &object_space);
  220. if (ACPI_FAILURE(status)) {
  221. return (status);
  222. }
  223. break;
  224. case ACPI_COPY_TYPE_PACKAGE:
  225. /*
  226. * Build the package object
  227. */
  228. target_object->type = ACPI_TYPE_PACKAGE;
  229. target_object->package.count = source_object->package.count;
  230. target_object->package.elements =
  231. ACPI_CAST_PTR(union acpi_object, info->free_space);
  232. /*
  233. * Pass the new package object back to the package walk routine
  234. */
  235. state->pkg.this_target_obj = target_object;
  236. /*
  237. * Save space for the array of objects (Package elements)
  238. * update the buffer length counter
  239. */
  240. object_space = ACPI_ROUND_UP_TO_NATIVE_WORD((acpi_size)
  241. target_object->
  242. package.count *
  243. sizeof(union
  244. acpi_object));
  245. break;
  246. default:
  247. return (AE_BAD_PARAMETER);
  248. }
  249. info->free_space += object_space;
  250. info->length += object_space;
  251. return (status);
  252. }
  253. /*******************************************************************************
  254. *
  255. * FUNCTION: acpi_ut_copy_ipackage_to_epackage
  256. *
  257. * PARAMETERS: internal_object - Pointer to the object we are returning
  258. * Buffer - Where the object is returned
  259. * space_used - Where the object length is returned
  260. *
  261. * RETURN: Status
  262. *
  263. * DESCRIPTION: This function is called to place a package object in a user
  264. * buffer. A package object by definition contains other objects.
  265. *
  266. * The buffer is assumed to have sufficient space for the object.
  267. * The caller must have verified the buffer length needed using the
  268. * acpi_ut_get_object_size function before calling this function.
  269. *
  270. ******************************************************************************/
  271. static acpi_status
  272. acpi_ut_copy_ipackage_to_epackage(union acpi_operand_object *internal_object,
  273. u8 * buffer, acpi_size * space_used)
  274. {
  275. union acpi_object *external_object;
  276. acpi_status status;
  277. struct acpi_pkg_info info;
  278. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_epackage);
  279. /*
  280. * First package at head of the buffer
  281. */
  282. external_object = ACPI_CAST_PTR(union acpi_object, buffer);
  283. /*
  284. * Free space begins right after the first package
  285. */
  286. info.length = ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  287. info.free_space =
  288. buffer + ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  289. info.object_space = 0;
  290. info.num_packages = 1;
  291. external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
  292. external_object->package.count = internal_object->package.count;
  293. external_object->package.elements = ACPI_CAST_PTR(union acpi_object,
  294. info.free_space);
  295. /*
  296. * Leave room for an array of ACPI_OBJECTS in the buffer
  297. * and move the free space past it
  298. */
  299. info.length += (acpi_size) external_object->package.count *
  300. ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  301. info.free_space += external_object->package.count *
  302. ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object));
  303. status = acpi_ut_walk_package_tree(internal_object, external_object,
  304. acpi_ut_copy_ielement_to_eelement,
  305. &info);
  306. *space_used = info.length;
  307. return_ACPI_STATUS(status);
  308. }
  309. /*******************************************************************************
  310. *
  311. * FUNCTION: acpi_ut_copy_iobject_to_eobject
  312. *
  313. * PARAMETERS: internal_object - The internal object to be converted
  314. * buffer_ptr - Where the object is returned
  315. *
  316. * RETURN: Status
  317. *
  318. * DESCRIPTION: This function is called to build an API object to be returned to
  319. * the caller.
  320. *
  321. ******************************************************************************/
  322. acpi_status
  323. acpi_ut_copy_iobject_to_eobject(union acpi_operand_object *internal_object,
  324. struct acpi_buffer *ret_buffer)
  325. {
  326. acpi_status status;
  327. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_eobject);
  328. if (ACPI_GET_OBJECT_TYPE(internal_object) == ACPI_TYPE_PACKAGE) {
  329. /*
  330. * Package object: Copy all subobjects (including
  331. * nested packages)
  332. */
  333. status = acpi_ut_copy_ipackage_to_epackage(internal_object,
  334. ret_buffer->pointer,
  335. &ret_buffer->length);
  336. } else {
  337. /*
  338. * Build a simple object (no nested objects)
  339. */
  340. status = acpi_ut_copy_isimple_to_esimple(internal_object,
  341. ACPI_CAST_PTR(union
  342. acpi_object,
  343. ret_buffer->
  344. pointer),
  345. ACPI_ADD_PTR(u8,
  346. ret_buffer->
  347. pointer,
  348. ACPI_ROUND_UP_TO_NATIVE_WORD
  349. (sizeof
  350. (union
  351. acpi_object))),
  352. &ret_buffer->length);
  353. /*
  354. * build simple does not include the object size in the length
  355. * so we add it in here
  356. */
  357. ret_buffer->length += sizeof(union acpi_object);
  358. }
  359. return_ACPI_STATUS(status);
  360. }
  361. /*******************************************************************************
  362. *
  363. * FUNCTION: acpi_ut_copy_esimple_to_isimple
  364. *
  365. * PARAMETERS: external_object - The external object to be converted
  366. * ret_internal_object - Where the internal object is returned
  367. *
  368. * RETURN: Status
  369. *
  370. * DESCRIPTION: This function copies an external object to an internal one.
  371. * NOTE: Pointers can be copied, we don't need to copy data.
  372. * (The pointers have to be valid in our address space no matter
  373. * what we do with them!)
  374. *
  375. ******************************************************************************/
  376. static acpi_status
  377. acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
  378. union acpi_operand_object **ret_internal_object)
  379. {
  380. union acpi_operand_object *internal_object;
  381. ACPI_FUNCTION_TRACE(ut_copy_esimple_to_isimple);
  382. /*
  383. * Simple types supported are: String, Buffer, Integer
  384. */
  385. switch (external_object->type) {
  386. case ACPI_TYPE_STRING:
  387. case ACPI_TYPE_BUFFER:
  388. case ACPI_TYPE_INTEGER:
  389. internal_object = acpi_ut_create_internal_object((u8)
  390. external_object->
  391. type);
  392. if (!internal_object) {
  393. return_ACPI_STATUS(AE_NO_MEMORY);
  394. }
  395. break;
  396. default:
  397. /* All other types are not supported */
  398. return_ACPI_STATUS(AE_SUPPORT);
  399. }
  400. /* Must COPY string and buffer contents */
  401. switch (external_object->type) {
  402. case ACPI_TYPE_STRING:
  403. internal_object->string.pointer =
  404. ACPI_ALLOCATE_ZEROED((acpi_size) external_object->string.
  405. length + 1);
  406. if (!internal_object->string.pointer) {
  407. goto error_exit;
  408. }
  409. ACPI_MEMCPY(internal_object->string.pointer,
  410. external_object->string.pointer,
  411. external_object->string.length);
  412. internal_object->string.length = external_object->string.length;
  413. break;
  414. case ACPI_TYPE_BUFFER:
  415. internal_object->buffer.pointer =
  416. ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
  417. if (!internal_object->buffer.pointer) {
  418. goto error_exit;
  419. }
  420. ACPI_MEMCPY(internal_object->buffer.pointer,
  421. external_object->buffer.pointer,
  422. external_object->buffer.length);
  423. internal_object->buffer.length = external_object->buffer.length;
  424. break;
  425. case ACPI_TYPE_INTEGER:
  426. internal_object->integer.value = external_object->integer.value;
  427. break;
  428. default:
  429. /* Other types can't get here */
  430. break;
  431. }
  432. *ret_internal_object = internal_object;
  433. return_ACPI_STATUS(AE_OK);
  434. error_exit:
  435. acpi_ut_remove_reference(internal_object);
  436. return_ACPI_STATUS(AE_NO_MEMORY);
  437. }
  438. /*******************************************************************************
  439. *
  440. * FUNCTION: acpi_ut_copy_epackage_to_ipackage
  441. *
  442. * PARAMETERS: external_object - The external object to be converted
  443. * internal_object - Where the internal object is returned
  444. *
  445. * RETURN: Status
  446. *
  447. * DESCRIPTION: Copy an external package object to an internal package.
  448. * Handles nested packages.
  449. *
  450. ******************************************************************************/
  451. static acpi_status
  452. acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
  453. union acpi_operand_object **internal_object)
  454. {
  455. acpi_status status = AE_OK;
  456. union acpi_operand_object *package_object;
  457. union acpi_operand_object **package_elements;
  458. acpi_native_uint i;
  459. ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
  460. /* Create the package object */
  461. package_object =
  462. acpi_ut_create_package_object(external_object->package.count);
  463. if (!package_object) {
  464. return_ACPI_STATUS(AE_NO_MEMORY);
  465. }
  466. package_elements = package_object->package.elements;
  467. /*
  468. * Recursive implementation. Probably ok, since nested external packages
  469. * as parameters should be very rare.
  470. */
  471. for (i = 0; i < external_object->package.count; i++) {
  472. status =
  473. acpi_ut_copy_eobject_to_iobject(&external_object->package.
  474. elements[i],
  475. &package_elements[i]);
  476. if (ACPI_FAILURE(status)) {
  477. /* Truncate package and delete it */
  478. package_object->package.count = i;
  479. package_elements[i] = NULL;
  480. acpi_ut_remove_reference(package_object);
  481. return_ACPI_STATUS(status);
  482. }
  483. }
  484. *internal_object = package_object;
  485. return_ACPI_STATUS(status);
  486. }
  487. /*******************************************************************************
  488. *
  489. * FUNCTION: acpi_ut_copy_eobject_to_iobject
  490. *
  491. * PARAMETERS: external_object - The external object to be converted
  492. * internal_object - Where the internal object is returned
  493. *
  494. * RETURN: Status - the status of the call
  495. *
  496. * DESCRIPTION: Converts an external object to an internal object.
  497. *
  498. ******************************************************************************/
  499. acpi_status
  500. acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
  501. union acpi_operand_object **internal_object)
  502. {
  503. acpi_status status;
  504. ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
  505. if (external_object->type == ACPI_TYPE_PACKAGE) {
  506. status =
  507. acpi_ut_copy_epackage_to_ipackage(external_object,
  508. internal_object);
  509. } else {
  510. /*
  511. * Build a simple object (no nested objects)
  512. */
  513. status =
  514. acpi_ut_copy_esimple_to_isimple(external_object,
  515. internal_object);
  516. }
  517. return_ACPI_STATUS(status);
  518. }
  519. /*******************************************************************************
  520. *
  521. * FUNCTION: acpi_ut_copy_simple_object
  522. *
  523. * PARAMETERS: source_desc - The internal object to be copied
  524. * dest_desc - New target object
  525. *
  526. * RETURN: Status
  527. *
  528. * DESCRIPTION: Simple copy of one internal object to another. Reference count
  529. * of the destination object is preserved.
  530. *
  531. ******************************************************************************/
  532. static acpi_status
  533. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  534. union acpi_operand_object *dest_desc)
  535. {
  536. u16 reference_count;
  537. union acpi_operand_object *next_object;
  538. /* Save fields from destination that we don't want to overwrite */
  539. reference_count = dest_desc->common.reference_count;
  540. next_object = dest_desc->common.next_object;
  541. /* Copy the entire source object over the destination object */
  542. ACPI_MEMCPY((char *)dest_desc, (char *)source_desc,
  543. sizeof(union acpi_operand_object));
  544. /* Restore the saved fields */
  545. dest_desc->common.reference_count = reference_count;
  546. dest_desc->common.next_object = next_object;
  547. /* New object is not static, regardless of source */
  548. dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  549. /* Handle the objects with extra data */
  550. switch (ACPI_GET_OBJECT_TYPE(dest_desc)) {
  551. case ACPI_TYPE_BUFFER:
  552. /*
  553. * Allocate and copy the actual buffer if and only if:
  554. * 1) There is a valid buffer pointer
  555. * 2) The buffer has a length > 0
  556. */
  557. if ((source_desc->buffer.pointer) &&
  558. (source_desc->buffer.length)) {
  559. dest_desc->buffer.pointer =
  560. ACPI_ALLOCATE(source_desc->buffer.length);
  561. if (!dest_desc->buffer.pointer) {
  562. return (AE_NO_MEMORY);
  563. }
  564. /* Copy the actual buffer data */
  565. ACPI_MEMCPY(dest_desc->buffer.pointer,
  566. source_desc->buffer.pointer,
  567. source_desc->buffer.length);
  568. }
  569. break;
  570. case ACPI_TYPE_STRING:
  571. /*
  572. * Allocate and copy the actual string if and only if:
  573. * 1) There is a valid string pointer
  574. * (Pointer to a NULL string is allowed)
  575. */
  576. if (source_desc->string.pointer) {
  577. dest_desc->string.pointer =
  578. ACPI_ALLOCATE((acpi_size) source_desc->string.
  579. length + 1);
  580. if (!dest_desc->string.pointer) {
  581. return (AE_NO_MEMORY);
  582. }
  583. /* Copy the actual string data */
  584. ACPI_MEMCPY(dest_desc->string.pointer,
  585. source_desc->string.pointer,
  586. (acpi_size) source_desc->string.length + 1);
  587. }
  588. break;
  589. case ACPI_TYPE_LOCAL_REFERENCE:
  590. /*
  591. * We copied the reference object, so we now must add a reference
  592. * to the object pointed to by the reference
  593. */
  594. acpi_ut_add_reference(source_desc->reference.object);
  595. break;
  596. case ACPI_TYPE_REGION:
  597. /*
  598. * We copied the Region Handler, so we now must add a reference
  599. */
  600. if (dest_desc->region.handler) {
  601. acpi_ut_add_reference(dest_desc->region.handler);
  602. }
  603. break;
  604. default:
  605. /* Nothing to do for other simple objects */
  606. break;
  607. }
  608. return (AE_OK);
  609. }
  610. /*******************************************************************************
  611. *
  612. * FUNCTION: acpi_ut_copy_ielement_to_ielement
  613. *
  614. * PARAMETERS: acpi_pkg_callback
  615. *
  616. * RETURN: Status
  617. *
  618. * DESCRIPTION: Copy one package element to another package element
  619. *
  620. ******************************************************************************/
  621. static acpi_status
  622. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  623. union acpi_operand_object *source_object,
  624. union acpi_generic_state *state,
  625. void *context)
  626. {
  627. acpi_status status = AE_OK;
  628. u32 this_index;
  629. union acpi_operand_object **this_target_ptr;
  630. union acpi_operand_object *target_object;
  631. ACPI_FUNCTION_ENTRY();
  632. this_index = state->pkg.index;
  633. this_target_ptr = (union acpi_operand_object **)
  634. &state->pkg.dest_object->package.elements[this_index];
  635. switch (object_type) {
  636. case ACPI_COPY_TYPE_SIMPLE:
  637. /* A null source object indicates a (legal) null package element */
  638. if (source_object) {
  639. /*
  640. * This is a simple object, just copy it
  641. */
  642. target_object =
  643. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE
  644. (source_object));
  645. if (!target_object) {
  646. return (AE_NO_MEMORY);
  647. }
  648. status =
  649. acpi_ut_copy_simple_object(source_object,
  650. target_object);
  651. if (ACPI_FAILURE(status)) {
  652. goto error_exit;
  653. }
  654. *this_target_ptr = target_object;
  655. } else {
  656. /* Pass through a null element */
  657. *this_target_ptr = NULL;
  658. }
  659. break;
  660. case ACPI_COPY_TYPE_PACKAGE:
  661. /*
  662. * This object is a package - go down another nesting level
  663. * Create and build the package object
  664. */
  665. target_object =
  666. acpi_ut_create_package_object(source_object->package.count);
  667. if (!target_object) {
  668. return (AE_NO_MEMORY);
  669. }
  670. target_object->common.flags = source_object->common.flags;
  671. /* Pass the new package object back to the package walk routine */
  672. state->pkg.this_target_obj = target_object;
  673. /* Store the object pointer in the parent package object */
  674. *this_target_ptr = target_object;
  675. break;
  676. default:
  677. return (AE_BAD_PARAMETER);
  678. }
  679. return (status);
  680. error_exit:
  681. acpi_ut_remove_reference(target_object);
  682. return (status);
  683. }
  684. /*******************************************************************************
  685. *
  686. * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
  687. *
  688. * PARAMETERS: *source_obj - Pointer to the source package object
  689. * *dest_obj - Where the internal object is returned
  690. *
  691. * RETURN: Status - the status of the call
  692. *
  693. * DESCRIPTION: This function is called to copy an internal package object
  694. * into another internal package object.
  695. *
  696. ******************************************************************************/
  697. static acpi_status
  698. acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  699. union acpi_operand_object *dest_obj,
  700. struct acpi_walk_state *walk_state)
  701. {
  702. acpi_status status = AE_OK;
  703. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
  704. dest_obj->common.type = ACPI_GET_OBJECT_TYPE(source_obj);
  705. dest_obj->common.flags = source_obj->common.flags;
  706. dest_obj->package.count = source_obj->package.count;
  707. /*
  708. * Create the object array and walk the source package tree
  709. */
  710. dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
  711. source_obj->package.
  712. count +
  713. 1) * sizeof(void *));
  714. if (!dest_obj->package.elements) {
  715. ACPI_ERROR((AE_INFO, "Package allocation failure"));
  716. return_ACPI_STATUS(AE_NO_MEMORY);
  717. }
  718. /*
  719. * Copy the package element-by-element by walking the package "tree".
  720. * This handles nested packages of arbitrary depth.
  721. */
  722. status = acpi_ut_walk_package_tree(source_obj, dest_obj,
  723. acpi_ut_copy_ielement_to_ielement,
  724. walk_state);
  725. if (ACPI_FAILURE(status)) {
  726. /* On failure, delete the destination package object */
  727. acpi_ut_remove_reference(dest_obj);
  728. }
  729. return_ACPI_STATUS(status);
  730. }
  731. /*******************************************************************************
  732. *
  733. * FUNCTION: acpi_ut_copy_iobject_to_iobject
  734. *
  735. * PARAMETERS: walk_state - Current walk state
  736. * source_desc - The internal object to be copied
  737. * dest_desc - Where the copied object is returned
  738. *
  739. * RETURN: Status
  740. *
  741. * DESCRIPTION: Copy an internal object to a new internal object
  742. *
  743. ******************************************************************************/
  744. acpi_status
  745. acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
  746. union acpi_operand_object **dest_desc,
  747. struct acpi_walk_state *walk_state)
  748. {
  749. acpi_status status = AE_OK;
  750. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
  751. /* Create the top level object */
  752. *dest_desc =
  753. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE(source_desc));
  754. if (!*dest_desc) {
  755. return_ACPI_STATUS(AE_NO_MEMORY);
  756. }
  757. /* Copy the object and possible subobjects */
  758. if (ACPI_GET_OBJECT_TYPE(source_desc) == ACPI_TYPE_PACKAGE) {
  759. status =
  760. acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
  761. walk_state);
  762. } else {
  763. status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
  764. }
  765. return_ACPI_STATUS(status);
  766. }