utcopy.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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/acnamesp.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. /* This is an object reference. */
  146. switch (internal_object->reference.class) {
  147. case ACPI_REFCLASS_NAME:
  148. /*
  149. * For namepath, return the object handle ("reference")
  150. * We are referring to the namespace node
  151. */
  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. default:
  158. /* All other reference types are unsupported */
  159. return_ACPI_STATUS(AE_TYPE);
  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. case ACPI_TYPE_LOCAL_REFERENCE:
  394. internal_object = acpi_ut_create_internal_object((u8)
  395. external_object->
  396. type);
  397. if (!internal_object) {
  398. return_ACPI_STATUS(AE_NO_MEMORY);
  399. }
  400. break;
  401. case ACPI_TYPE_ANY: /* This is the case for a NULL object */
  402. *ret_internal_object = NULL;
  403. return_ACPI_STATUS(AE_OK);
  404. default:
  405. /* All other types are not supported */
  406. ACPI_ERROR((AE_INFO,
  407. "Unsupported object type, cannot convert to internal object: %s",
  408. acpi_ut_get_type_name(external_object->type)));
  409. return_ACPI_STATUS(AE_SUPPORT);
  410. }
  411. /* Must COPY string and buffer contents */
  412. switch (external_object->type) {
  413. case ACPI_TYPE_STRING:
  414. internal_object->string.pointer =
  415. ACPI_ALLOCATE_ZEROED((acpi_size) external_object->string.
  416. length + 1);
  417. if (!internal_object->string.pointer) {
  418. goto error_exit;
  419. }
  420. ACPI_MEMCPY(internal_object->string.pointer,
  421. external_object->string.pointer,
  422. external_object->string.length);
  423. internal_object->string.length = external_object->string.length;
  424. break;
  425. case ACPI_TYPE_BUFFER:
  426. internal_object->buffer.pointer =
  427. ACPI_ALLOCATE_ZEROED(external_object->buffer.length);
  428. if (!internal_object->buffer.pointer) {
  429. goto error_exit;
  430. }
  431. ACPI_MEMCPY(internal_object->buffer.pointer,
  432. external_object->buffer.pointer,
  433. external_object->buffer.length);
  434. internal_object->buffer.length = external_object->buffer.length;
  435. /* Mark buffer data valid */
  436. internal_object->buffer.flags |= AOPOBJ_DATA_VALID;
  437. break;
  438. case ACPI_TYPE_INTEGER:
  439. internal_object->integer.value = external_object->integer.value;
  440. break;
  441. case ACPI_TYPE_LOCAL_REFERENCE:
  442. /* TBD: should validate incoming handle */
  443. internal_object->reference.class = ACPI_REFCLASS_NAME;
  444. internal_object->reference.node =
  445. external_object->reference.handle;
  446. break;
  447. default:
  448. /* Other types can't get here */
  449. break;
  450. }
  451. *ret_internal_object = internal_object;
  452. return_ACPI_STATUS(AE_OK);
  453. error_exit:
  454. acpi_ut_remove_reference(internal_object);
  455. return_ACPI_STATUS(AE_NO_MEMORY);
  456. }
  457. /*******************************************************************************
  458. *
  459. * FUNCTION: acpi_ut_copy_epackage_to_ipackage
  460. *
  461. * PARAMETERS: external_object - The external object to be converted
  462. * internal_object - Where the internal object is returned
  463. *
  464. * RETURN: Status
  465. *
  466. * DESCRIPTION: Copy an external package object to an internal package.
  467. * Handles nested packages.
  468. *
  469. ******************************************************************************/
  470. static acpi_status
  471. acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
  472. union acpi_operand_object **internal_object)
  473. {
  474. acpi_status status = AE_OK;
  475. union acpi_operand_object *package_object;
  476. union acpi_operand_object **package_elements;
  477. u32 i;
  478. ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
  479. /* Create the package object */
  480. package_object =
  481. acpi_ut_create_package_object(external_object->package.count);
  482. if (!package_object) {
  483. return_ACPI_STATUS(AE_NO_MEMORY);
  484. }
  485. package_elements = package_object->package.elements;
  486. /*
  487. * Recursive implementation. Probably ok, since nested external packages
  488. * as parameters should be very rare.
  489. */
  490. for (i = 0; i < external_object->package.count; i++) {
  491. status =
  492. acpi_ut_copy_eobject_to_iobject(&external_object->package.
  493. elements[i],
  494. &package_elements[i]);
  495. if (ACPI_FAILURE(status)) {
  496. /* Truncate package and delete it */
  497. package_object->package.count = i;
  498. package_elements[i] = NULL;
  499. acpi_ut_remove_reference(package_object);
  500. return_ACPI_STATUS(status);
  501. }
  502. }
  503. /* Mark package data valid */
  504. package_object->package.flags |= AOPOBJ_DATA_VALID;
  505. *internal_object = package_object;
  506. return_ACPI_STATUS(status);
  507. }
  508. /*******************************************************************************
  509. *
  510. * FUNCTION: acpi_ut_copy_eobject_to_iobject
  511. *
  512. * PARAMETERS: external_object - The external object to be converted
  513. * internal_object - Where the internal object is returned
  514. *
  515. * RETURN: Status - the status of the call
  516. *
  517. * DESCRIPTION: Converts an external object to an internal object.
  518. *
  519. ******************************************************************************/
  520. acpi_status
  521. acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
  522. union acpi_operand_object **internal_object)
  523. {
  524. acpi_status status;
  525. ACPI_FUNCTION_TRACE(ut_copy_eobject_to_iobject);
  526. if (external_object->type == ACPI_TYPE_PACKAGE) {
  527. status =
  528. acpi_ut_copy_epackage_to_ipackage(external_object,
  529. internal_object);
  530. } else {
  531. /*
  532. * Build a simple object (no nested objects)
  533. */
  534. status =
  535. acpi_ut_copy_esimple_to_isimple(external_object,
  536. internal_object);
  537. }
  538. return_ACPI_STATUS(status);
  539. }
  540. /*******************************************************************************
  541. *
  542. * FUNCTION: acpi_ut_copy_simple_object
  543. *
  544. * PARAMETERS: source_desc - The internal object to be copied
  545. * dest_desc - New target object
  546. *
  547. * RETURN: Status
  548. *
  549. * DESCRIPTION: Simple copy of one internal object to another. Reference count
  550. * of the destination object is preserved.
  551. *
  552. ******************************************************************************/
  553. static acpi_status
  554. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  555. union acpi_operand_object *dest_desc)
  556. {
  557. u16 reference_count;
  558. union acpi_operand_object *next_object;
  559. /* Save fields from destination that we don't want to overwrite */
  560. reference_count = dest_desc->common.reference_count;
  561. next_object = dest_desc->common.next_object;
  562. /* Copy the entire source object over the destination object */
  563. ACPI_MEMCPY((char *)dest_desc, (char *)source_desc,
  564. sizeof(union acpi_operand_object));
  565. /* Restore the saved fields */
  566. dest_desc->common.reference_count = reference_count;
  567. dest_desc->common.next_object = next_object;
  568. /* New object is not static, regardless of source */
  569. dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  570. /* Handle the objects with extra data */
  571. switch (ACPI_GET_OBJECT_TYPE(dest_desc)) {
  572. case ACPI_TYPE_BUFFER:
  573. /*
  574. * Allocate and copy the actual buffer if and only if:
  575. * 1) There is a valid buffer pointer
  576. * 2) The buffer has a length > 0
  577. */
  578. if ((source_desc->buffer.pointer) &&
  579. (source_desc->buffer.length)) {
  580. dest_desc->buffer.pointer =
  581. ACPI_ALLOCATE(source_desc->buffer.length);
  582. if (!dest_desc->buffer.pointer) {
  583. return (AE_NO_MEMORY);
  584. }
  585. /* Copy the actual buffer data */
  586. ACPI_MEMCPY(dest_desc->buffer.pointer,
  587. source_desc->buffer.pointer,
  588. source_desc->buffer.length);
  589. }
  590. break;
  591. case ACPI_TYPE_STRING:
  592. /*
  593. * Allocate and copy the actual string if and only if:
  594. * 1) There is a valid string pointer
  595. * (Pointer to a NULL string is allowed)
  596. */
  597. if (source_desc->string.pointer) {
  598. dest_desc->string.pointer =
  599. ACPI_ALLOCATE((acpi_size) source_desc->string.
  600. length + 1);
  601. if (!dest_desc->string.pointer) {
  602. return (AE_NO_MEMORY);
  603. }
  604. /* Copy the actual string data */
  605. ACPI_MEMCPY(dest_desc->string.pointer,
  606. source_desc->string.pointer,
  607. (acpi_size) source_desc->string.length + 1);
  608. }
  609. break;
  610. case ACPI_TYPE_LOCAL_REFERENCE:
  611. /*
  612. * We copied the reference object, so we now must add a reference
  613. * to the object pointed to by the reference
  614. *
  615. * DDBHandle reference (from Load/load_table) is a special reference,
  616. * it does not have a Reference.Object, so does not need to
  617. * increase the reference count
  618. */
  619. if (source_desc->reference.class == ACPI_REFCLASS_TABLE) {
  620. break;
  621. }
  622. acpi_ut_add_reference(source_desc->reference.object);
  623. break;
  624. case ACPI_TYPE_REGION:
  625. /*
  626. * We copied the Region Handler, so we now must add a reference
  627. */
  628. if (dest_desc->region.handler) {
  629. acpi_ut_add_reference(dest_desc->region.handler);
  630. }
  631. break;
  632. default:
  633. /* Nothing to do for other simple objects */
  634. break;
  635. }
  636. return (AE_OK);
  637. }
  638. /*******************************************************************************
  639. *
  640. * FUNCTION: acpi_ut_copy_ielement_to_ielement
  641. *
  642. * PARAMETERS: acpi_pkg_callback
  643. *
  644. * RETURN: Status
  645. *
  646. * DESCRIPTION: Copy one package element to another package element
  647. *
  648. ******************************************************************************/
  649. static acpi_status
  650. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  651. union acpi_operand_object *source_object,
  652. union acpi_generic_state *state,
  653. void *context)
  654. {
  655. acpi_status status = AE_OK;
  656. u32 this_index;
  657. union acpi_operand_object **this_target_ptr;
  658. union acpi_operand_object *target_object;
  659. ACPI_FUNCTION_ENTRY();
  660. this_index = state->pkg.index;
  661. this_target_ptr = (union acpi_operand_object **)
  662. &state->pkg.dest_object->package.elements[this_index];
  663. switch (object_type) {
  664. case ACPI_COPY_TYPE_SIMPLE:
  665. /* A null source object indicates a (legal) null package element */
  666. if (source_object) {
  667. /*
  668. * This is a simple object, just copy it
  669. */
  670. target_object =
  671. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE
  672. (source_object));
  673. if (!target_object) {
  674. return (AE_NO_MEMORY);
  675. }
  676. status =
  677. acpi_ut_copy_simple_object(source_object,
  678. target_object);
  679. if (ACPI_FAILURE(status)) {
  680. goto error_exit;
  681. }
  682. *this_target_ptr = target_object;
  683. } else {
  684. /* Pass through a null element */
  685. *this_target_ptr = NULL;
  686. }
  687. break;
  688. case ACPI_COPY_TYPE_PACKAGE:
  689. /*
  690. * This object is a package - go down another nesting level
  691. * Create and build the package object
  692. */
  693. target_object =
  694. acpi_ut_create_package_object(source_object->package.count);
  695. if (!target_object) {
  696. return (AE_NO_MEMORY);
  697. }
  698. target_object->common.flags = source_object->common.flags;
  699. /* Pass the new package object back to the package walk routine */
  700. state->pkg.this_target_obj = target_object;
  701. /* Store the object pointer in the parent package object */
  702. *this_target_ptr = target_object;
  703. break;
  704. default:
  705. return (AE_BAD_PARAMETER);
  706. }
  707. return (status);
  708. error_exit:
  709. acpi_ut_remove_reference(target_object);
  710. return (status);
  711. }
  712. /*******************************************************************************
  713. *
  714. * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
  715. *
  716. * PARAMETERS: *source_obj - Pointer to the source package object
  717. * *dest_obj - Where the internal object is returned
  718. *
  719. * RETURN: Status - the status of the call
  720. *
  721. * DESCRIPTION: This function is called to copy an internal package object
  722. * into another internal package object.
  723. *
  724. ******************************************************************************/
  725. static acpi_status
  726. acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  727. union acpi_operand_object *dest_obj,
  728. struct acpi_walk_state *walk_state)
  729. {
  730. acpi_status status = AE_OK;
  731. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
  732. dest_obj->common.type = ACPI_GET_OBJECT_TYPE(source_obj);
  733. dest_obj->common.flags = source_obj->common.flags;
  734. dest_obj->package.count = source_obj->package.count;
  735. /*
  736. * Create the object array and walk the source package tree
  737. */
  738. dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
  739. source_obj->package.
  740. count +
  741. 1) * sizeof(void *));
  742. if (!dest_obj->package.elements) {
  743. ACPI_ERROR((AE_INFO, "Package allocation failure"));
  744. return_ACPI_STATUS(AE_NO_MEMORY);
  745. }
  746. /*
  747. * Copy the package element-by-element by walking the package "tree".
  748. * This handles nested packages of arbitrary depth.
  749. */
  750. status = acpi_ut_walk_package_tree(source_obj, dest_obj,
  751. acpi_ut_copy_ielement_to_ielement,
  752. walk_state);
  753. if (ACPI_FAILURE(status)) {
  754. /* On failure, delete the destination package object */
  755. acpi_ut_remove_reference(dest_obj);
  756. }
  757. return_ACPI_STATUS(status);
  758. }
  759. /*******************************************************************************
  760. *
  761. * FUNCTION: acpi_ut_copy_iobject_to_iobject
  762. *
  763. * PARAMETERS: walk_state - Current walk state
  764. * source_desc - The internal object to be copied
  765. * dest_desc - Where the copied object is returned
  766. *
  767. * RETURN: Status
  768. *
  769. * DESCRIPTION: Copy an internal object to a new internal object
  770. *
  771. ******************************************************************************/
  772. acpi_status
  773. acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
  774. union acpi_operand_object **dest_desc,
  775. struct acpi_walk_state *walk_state)
  776. {
  777. acpi_status status = AE_OK;
  778. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
  779. /* Create the top level object */
  780. *dest_desc =
  781. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE(source_desc));
  782. if (!*dest_desc) {
  783. return_ACPI_STATUS(AE_NO_MEMORY);
  784. }
  785. /* Copy the object and possible subobjects */
  786. if (ACPI_GET_OBJECT_TYPE(source_desc) == ACPI_TYPE_PACKAGE) {
  787. status =
  788. acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
  789. walk_state);
  790. } else {
  791. status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
  792. }
  793. return_ACPI_STATUS(status);
  794. }