utcopy.c 27 KB

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