utcopy.c 28 KB

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