utcopy.c 28 KB

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