utcopy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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(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. (union acpi_object *)
  339. ret_buffer->pointer,
  340. ((u8 *) ret_buffer->
  341. pointer +
  342. ACPI_ROUND_UP_TO_NATIVE_WORD
  343. (sizeof
  344. (union
  345. acpi_object))),
  346. &ret_buffer->length);
  347. /*
  348. * build simple does not include the object size in the length
  349. * so we add it in here
  350. */
  351. ret_buffer->length += sizeof(union acpi_object);
  352. }
  353. return_ACPI_STATUS(status);
  354. }
  355. /*******************************************************************************
  356. *
  357. * FUNCTION: acpi_ut_copy_esimple_to_isimple
  358. *
  359. * PARAMETERS: external_object - The external object to be converted
  360. * ret_internal_object - Where the internal object is returned
  361. *
  362. * RETURN: Status
  363. *
  364. * DESCRIPTION: This function copies an external object to an internal one.
  365. * NOTE: Pointers can be copied, we don't need to copy data.
  366. * (The pointers have to be valid in our address space no matter
  367. * what we do with them!)
  368. *
  369. ******************************************************************************/
  370. static acpi_status
  371. acpi_ut_copy_esimple_to_isimple(union acpi_object *external_object,
  372. union acpi_operand_object **ret_internal_object)
  373. {
  374. union acpi_operand_object *internal_object;
  375. ACPI_FUNCTION_TRACE("ut_copy_esimple_to_isimple");
  376. /*
  377. * Simple types supported are: String, Buffer, Integer
  378. */
  379. switch (external_object->type) {
  380. case ACPI_TYPE_STRING:
  381. case ACPI_TYPE_BUFFER:
  382. case ACPI_TYPE_INTEGER:
  383. internal_object = acpi_ut_create_internal_object((u8)
  384. external_object->
  385. type);
  386. if (!internal_object) {
  387. return_ACPI_STATUS(AE_NO_MEMORY);
  388. }
  389. break;
  390. default:
  391. /* All other types are not supported */
  392. return_ACPI_STATUS(AE_SUPPORT);
  393. }
  394. /* Must COPY string and buffer contents */
  395. switch (external_object->type) {
  396. case ACPI_TYPE_STRING:
  397. internal_object->string.pointer =
  398. ACPI_MEM_CALLOCATE((acpi_size) external_object->string.
  399. length + 1);
  400. if (!internal_object->string.pointer) {
  401. goto error_exit;
  402. }
  403. ACPI_MEMCPY(internal_object->string.pointer,
  404. external_object->string.pointer,
  405. external_object->string.length);
  406. internal_object->string.length = external_object->string.length;
  407. break;
  408. case ACPI_TYPE_BUFFER:
  409. internal_object->buffer.pointer =
  410. ACPI_MEM_CALLOCATE(external_object->buffer.length);
  411. if (!internal_object->buffer.pointer) {
  412. goto error_exit;
  413. }
  414. ACPI_MEMCPY(internal_object->buffer.pointer,
  415. external_object->buffer.pointer,
  416. external_object->buffer.length);
  417. internal_object->buffer.length = external_object->buffer.length;
  418. break;
  419. case ACPI_TYPE_INTEGER:
  420. internal_object->integer.value = external_object->integer.value;
  421. break;
  422. default:
  423. /* Other types can't get here */
  424. break;
  425. }
  426. *ret_internal_object = internal_object;
  427. return_ACPI_STATUS(AE_OK);
  428. error_exit:
  429. acpi_ut_remove_reference(internal_object);
  430. return_ACPI_STATUS(AE_NO_MEMORY);
  431. }
  432. #ifdef ACPI_FUTURE_IMPLEMENTATION
  433. /* Code to convert packages that are parameters to control methods */
  434. /*******************************************************************************
  435. *
  436. * FUNCTION: acpi_ut_copy_epackage_to_ipackage
  437. *
  438. * PARAMETERS: *internal_object - Pointer to the object we are returning
  439. * *Buffer - Where the object is returned
  440. * *space_used - Where the length of the object is returned
  441. *
  442. * RETURN: Status
  443. *
  444. * DESCRIPTION: This function is called to place a package object in a user
  445. * buffer. A package object by definition contains other objects.
  446. *
  447. * The buffer is assumed to have sufficient space for the object.
  448. * The caller must have verified the buffer length needed using the
  449. * acpi_ut_get_object_size function before calling this function.
  450. *
  451. ******************************************************************************/
  452. static acpi_status
  453. acpi_ut_copy_epackage_to_ipackage(union acpi_operand_object *internal_object,
  454. u8 * buffer, u32 * space_used)
  455. {
  456. u8 *free_space;
  457. union acpi_object *external_object;
  458. u32 length = 0;
  459. u32 this_index;
  460. u32 object_space = 0;
  461. union acpi_operand_object *this_internal_obj;
  462. union acpi_object *this_external_obj;
  463. ACPI_FUNCTION_TRACE("ut_copy_epackage_to_ipackage");
  464. /*
  465. * First package at head of the buffer
  466. */
  467. external_object = (union acpi_object *)buffer;
  468. /*
  469. * Free space begins right after the first package
  470. */
  471. free_space = buffer + sizeof(union acpi_object);
  472. external_object->type = ACPI_GET_OBJECT_TYPE(internal_object);
  473. external_object->package.count = internal_object->package.count;
  474. external_object->package.elements = (union acpi_object *)free_space;
  475. /*
  476. * Build an array of ACPI_OBJECTS in the buffer
  477. * and move the free space past it
  478. */
  479. free_space +=
  480. external_object->package.count * sizeof(union acpi_object);
  481. /* Call walk_package */
  482. }
  483. #endif /* Future implementation */
  484. /*******************************************************************************
  485. *
  486. * FUNCTION: acpi_ut_copy_eobject_to_iobject
  487. *
  488. * PARAMETERS: *internal_object - The external object to be converted
  489. * *buffer_ptr - Where the internal object is returned
  490. *
  491. * RETURN: Status - the status of the call
  492. *
  493. * DESCRIPTION: Converts an external object to an internal object.
  494. *
  495. ******************************************************************************/
  496. acpi_status
  497. acpi_ut_copy_eobject_to_iobject(union acpi_object *external_object,
  498. union acpi_operand_object **internal_object)
  499. {
  500. acpi_status status;
  501. ACPI_FUNCTION_TRACE("ut_copy_eobject_to_iobject");
  502. if (external_object->type == ACPI_TYPE_PACKAGE) {
  503. /*
  504. * Packages as external input to control methods are not supported,
  505. */
  506. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  507. "Packages as parameters not implemented!\n"));
  508. return_ACPI_STATUS(AE_NOT_IMPLEMENTED);
  509. }
  510. else {
  511. /*
  512. * Build a simple object (no nested objects)
  513. */
  514. status =
  515. acpi_ut_copy_esimple_to_isimple(external_object,
  516. internal_object);
  517. }
  518. return_ACPI_STATUS(status);
  519. }
  520. /*******************************************************************************
  521. *
  522. * FUNCTION: acpi_ut_copy_simple_object
  523. *
  524. * PARAMETERS: source_desc - The internal object to be copied
  525. * dest_desc - New target object
  526. *
  527. * RETURN: Status
  528. *
  529. * DESCRIPTION: Simple copy of one internal object to another. Reference count
  530. * of the destination object is preserved.
  531. *
  532. ******************************************************************************/
  533. static acpi_status
  534. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  535. union acpi_operand_object *dest_desc)
  536. {
  537. u16 reference_count;
  538. union acpi_operand_object *next_object;
  539. /* Save fields from destination that we don't want to overwrite */
  540. reference_count = dest_desc->common.reference_count;
  541. next_object = dest_desc->common.next_object;
  542. /* Copy the entire source object over the destination object */
  543. ACPI_MEMCPY((char *)dest_desc, (char *)source_desc,
  544. sizeof(union acpi_operand_object));
  545. /* Restore the saved fields */
  546. dest_desc->common.reference_count = reference_count;
  547. dest_desc->common.next_object = next_object;
  548. /* New object is not static, regardless of source */
  549. dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  550. /* Handle the objects with extra data */
  551. switch (ACPI_GET_OBJECT_TYPE(dest_desc)) {
  552. case ACPI_TYPE_BUFFER:
  553. /*
  554. * Allocate and copy the actual buffer if and only if:
  555. * 1) There is a valid buffer pointer
  556. * 2) The buffer has a length > 0
  557. */
  558. if ((source_desc->buffer.pointer) &&
  559. (source_desc->buffer.length)) {
  560. dest_desc->buffer.pointer =
  561. ACPI_MEM_ALLOCATE(source_desc->buffer.length);
  562. if (!dest_desc->buffer.pointer) {
  563. return (AE_NO_MEMORY);
  564. }
  565. /* Copy the actual buffer data */
  566. ACPI_MEMCPY(dest_desc->buffer.pointer,
  567. source_desc->buffer.pointer,
  568. source_desc->buffer.length);
  569. }
  570. break;
  571. case ACPI_TYPE_STRING:
  572. /*
  573. * Allocate and copy the actual string if and only if:
  574. * 1) There is a valid string pointer
  575. * (Pointer to a NULL string is allowed)
  576. */
  577. if (source_desc->string.pointer) {
  578. dest_desc->string.pointer =
  579. ACPI_MEM_ALLOCATE((acpi_size) source_desc->string.
  580. length + 1);
  581. if (!dest_desc->string.pointer) {
  582. return (AE_NO_MEMORY);
  583. }
  584. /* Copy the actual string data */
  585. ACPI_MEMCPY(dest_desc->string.pointer,
  586. source_desc->string.pointer,
  587. (acpi_size) source_desc->string.length + 1);
  588. }
  589. break;
  590. case ACPI_TYPE_LOCAL_REFERENCE:
  591. /*
  592. * We copied the reference object, so we now must add a reference
  593. * to the object pointed to by the reference
  594. */
  595. acpi_ut_add_reference(source_desc->reference.object);
  596. break;
  597. default:
  598. /* Nothing to do for other simple objects */
  599. break;
  600. }
  601. return (AE_OK);
  602. }
  603. /*******************************************************************************
  604. *
  605. * FUNCTION: acpi_ut_copy_ielement_to_ielement
  606. *
  607. * PARAMETERS: acpi_pkg_callback
  608. *
  609. * RETURN: Status
  610. *
  611. * DESCRIPTION: Copy one package element to another package element
  612. *
  613. ******************************************************************************/
  614. static acpi_status
  615. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  616. union acpi_operand_object *source_object,
  617. union acpi_generic_state *state,
  618. void *context)
  619. {
  620. acpi_status status = AE_OK;
  621. u32 this_index;
  622. union acpi_operand_object **this_target_ptr;
  623. union acpi_operand_object *target_object;
  624. ACPI_FUNCTION_ENTRY();
  625. this_index = state->pkg.index;
  626. this_target_ptr = (union acpi_operand_object **)
  627. &state->pkg.dest_object->package.elements[this_index];
  628. switch (object_type) {
  629. case ACPI_COPY_TYPE_SIMPLE:
  630. /* A null source object indicates a (legal) null package element */
  631. if (source_object) {
  632. /*
  633. * This is a simple object, just copy it
  634. */
  635. target_object =
  636. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE
  637. (source_object));
  638. if (!target_object) {
  639. return (AE_NO_MEMORY);
  640. }
  641. status =
  642. acpi_ut_copy_simple_object(source_object,
  643. target_object);
  644. if (ACPI_FAILURE(status)) {
  645. goto error_exit;
  646. }
  647. *this_target_ptr = target_object;
  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 =
  659. acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
  660. if (!target_object) {
  661. return (AE_NO_MEMORY);
  662. }
  663. target_object->package.count = source_object->package.count;
  664. target_object->common.flags = source_object->common.flags;
  665. /*
  666. * Create the object array
  667. */
  668. target_object->package.elements =
  669. ACPI_MEM_CALLOCATE(((acpi_size) source_object->package.
  670. count + 1) * sizeof(void *));
  671. if (!target_object->package.elements) {
  672. status = AE_NO_MEMORY;
  673. goto error_exit;
  674. }
  675. /*
  676. * Pass the new package object back to the package walk routine
  677. */
  678. state->pkg.this_target_obj = target_object;
  679. /*
  680. * Store the object pointer in the parent package object
  681. */
  682. *this_target_ptr = target_object;
  683. break;
  684. default:
  685. return (AE_BAD_PARAMETER);
  686. }
  687. return (status);
  688. error_exit:
  689. acpi_ut_remove_reference(target_object);
  690. return (status);
  691. }
  692. /*******************************************************************************
  693. *
  694. * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
  695. *
  696. * PARAMETERS: *source_obj - Pointer to the source package object
  697. * *dest_obj - Where the internal object is returned
  698. *
  699. * RETURN: Status - the status of the call
  700. *
  701. * DESCRIPTION: This function is called to copy an internal package object
  702. * into another internal package object.
  703. *
  704. ******************************************************************************/
  705. static acpi_status
  706. acpi_ut_copy_ipackage_to_ipackage(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(((acpi_size)
  719. source_obj->package.
  720. count +
  721. 1) * sizeof(void *));
  722. if (!dest_obj->package.elements) {
  723. ACPI_REPORT_ERROR(("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,
  732. walk_state);
  733. if (ACPI_FAILURE(status)) {
  734. /* On failure, delete the destination package object */
  735. acpi_ut_remove_reference(dest_obj);
  736. }
  737. return_ACPI_STATUS(status);
  738. }
  739. /*******************************************************************************
  740. *
  741. * FUNCTION: acpi_ut_copy_iobject_to_iobject
  742. *
  743. * PARAMETERS: walk_state - Current walk state
  744. * source_desc - The internal object to be copied
  745. * dest_desc - Where the copied object is returned
  746. *
  747. * RETURN: Status
  748. *
  749. * DESCRIPTION: Copy an internal object to a new internal object
  750. *
  751. ******************************************************************************/
  752. acpi_status
  753. acpi_ut_copy_iobject_to_iobject(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 =
  761. acpi_ut_create_internal_object(ACPI_GET_OBJECT_TYPE(source_desc));
  762. if (!*dest_desc) {
  763. return_ACPI_STATUS(AE_NO_MEMORY);
  764. }
  765. /* Copy the object and possible subobjects */
  766. if (ACPI_GET_OBJECT_TYPE(source_desc) == ACPI_TYPE_PACKAGE) {
  767. status =
  768. acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
  769. walk_state);
  770. } else {
  771. status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
  772. }
  773. return_ACPI_STATUS(status);
  774. }