utcopy.c 27 KB

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