utcopy.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /******************************************************************************
  2. *
  3. * Module Name: utcopy - Internal to external object translation utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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 *)&((union acpi_object *)
  214. (state->pkg.dest_object))->
  215. package.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 = buffer +
  293. 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 =
  299. ACPI_CAST_PTR(union acpi_object, 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
  489. * packages 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 = acpi_ut_copy_esimple_to_isimple(external_object,
  536. internal_object);
  537. }
  538. return_ACPI_STATUS(status);
  539. }
  540. /*******************************************************************************
  541. *
  542. * FUNCTION: acpi_ut_copy_simple_object
  543. *
  544. * PARAMETERS: source_desc - The internal object to be copied
  545. * dest_desc - New target object
  546. *
  547. * RETURN: Status
  548. *
  549. * DESCRIPTION: Simple copy of one internal object to another. Reference count
  550. * of the destination object is preserved.
  551. *
  552. ******************************************************************************/
  553. static acpi_status
  554. acpi_ut_copy_simple_object(union acpi_operand_object *source_desc,
  555. union acpi_operand_object *dest_desc)
  556. {
  557. u16 reference_count;
  558. union acpi_operand_object *next_object;
  559. acpi_status status;
  560. acpi_size copy_size;
  561. /* Save fields from destination that we don't want to overwrite */
  562. reference_count = dest_desc->common.reference_count;
  563. next_object = dest_desc->common.next_object;
  564. /*
  565. * Copy the entire source object over the destination object.
  566. * Note: Source can be either an operand object or namespace node.
  567. */
  568. copy_size = sizeof(union acpi_operand_object);
  569. if (ACPI_GET_DESCRIPTOR_TYPE(source_desc) == ACPI_DESC_TYPE_NAMED) {
  570. copy_size = sizeof(struct acpi_namespace_node);
  571. }
  572. memcpy(ACPI_CAST_PTR(char, dest_desc),
  573. ACPI_CAST_PTR(char, source_desc), copy_size);
  574. /* Restore the saved fields */
  575. dest_desc->common.reference_count = reference_count;
  576. dest_desc->common.next_object = next_object;
  577. /* New object is not static, regardless of source */
  578. dest_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  579. /* Handle the objects with extra data */
  580. switch (dest_desc->common.type) {
  581. case ACPI_TYPE_BUFFER:
  582. /*
  583. * Allocate and copy the actual buffer if and only if:
  584. * 1) There is a valid buffer pointer
  585. * 2) The buffer has a length > 0
  586. */
  587. if ((source_desc->buffer.pointer) &&
  588. (source_desc->buffer.length)) {
  589. dest_desc->buffer.pointer =
  590. ACPI_ALLOCATE(source_desc->buffer.length);
  591. if (!dest_desc->buffer.pointer) {
  592. return (AE_NO_MEMORY);
  593. }
  594. /* Copy the actual buffer data */
  595. memcpy(dest_desc->buffer.pointer,
  596. source_desc->buffer.pointer,
  597. source_desc->buffer.length);
  598. }
  599. break;
  600. case ACPI_TYPE_STRING:
  601. /*
  602. * Allocate and copy the actual string if and only if:
  603. * 1) There is a valid string pointer
  604. * (Pointer to a NULL string is allowed)
  605. */
  606. if (source_desc->string.pointer) {
  607. dest_desc->string.pointer =
  608. ACPI_ALLOCATE((acpi_size)source_desc->string.
  609. length + 1);
  610. if (!dest_desc->string.pointer) {
  611. return (AE_NO_MEMORY);
  612. }
  613. /* Copy the actual string data */
  614. memcpy(dest_desc->string.pointer,
  615. source_desc->string.pointer,
  616. (acpi_size)source_desc->string.length + 1);
  617. }
  618. break;
  619. case ACPI_TYPE_LOCAL_REFERENCE:
  620. /*
  621. * We copied the reference object, so we now must add a reference
  622. * to the object pointed to by the reference
  623. *
  624. * DDBHandle reference (from Load/load_table) is a special reference,
  625. * it does not have a Reference.Object, so does not need to
  626. * increase the reference count
  627. */
  628. if (source_desc->reference.class == ACPI_REFCLASS_TABLE) {
  629. break;
  630. }
  631. acpi_ut_add_reference(source_desc->reference.object);
  632. break;
  633. case ACPI_TYPE_REGION:
  634. /*
  635. * We copied the Region Handler, so we now must add a reference
  636. */
  637. if (dest_desc->region.handler) {
  638. acpi_ut_add_reference(dest_desc->region.handler);
  639. }
  640. break;
  641. /*
  642. * For Mutex and Event objects, we cannot simply copy the underlying
  643. * OS object. We must create a new one.
  644. */
  645. case ACPI_TYPE_MUTEX:
  646. status = acpi_os_create_mutex(&dest_desc->mutex.os_mutex);
  647. if (ACPI_FAILURE(status)) {
  648. return (status);
  649. }
  650. break;
  651. case ACPI_TYPE_EVENT:
  652. status = acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0,
  653. &dest_desc->event.
  654. os_semaphore);
  655. if (ACPI_FAILURE(status)) {
  656. return (status);
  657. }
  658. break;
  659. default:
  660. /* Nothing to do for other simple objects */
  661. break;
  662. }
  663. return (AE_OK);
  664. }
  665. /*******************************************************************************
  666. *
  667. * FUNCTION: acpi_ut_copy_ielement_to_ielement
  668. *
  669. * PARAMETERS: acpi_pkg_callback
  670. *
  671. * RETURN: Status
  672. *
  673. * DESCRIPTION: Copy one package element to another package element
  674. *
  675. ******************************************************************************/
  676. static acpi_status
  677. acpi_ut_copy_ielement_to_ielement(u8 object_type,
  678. union acpi_operand_object *source_object,
  679. union acpi_generic_state *state,
  680. void *context)
  681. {
  682. acpi_status status = AE_OK;
  683. u32 this_index;
  684. union acpi_operand_object **this_target_ptr;
  685. union acpi_operand_object *target_object;
  686. ACPI_FUNCTION_ENTRY();
  687. this_index = state->pkg.index;
  688. this_target_ptr = (union acpi_operand_object **)
  689. &state->pkg.dest_object->package.elements[this_index];
  690. switch (object_type) {
  691. case ACPI_COPY_TYPE_SIMPLE:
  692. /* A null source object indicates a (legal) null package element */
  693. if (source_object) {
  694. /*
  695. * This is a simple object, just copy it
  696. */
  697. target_object =
  698. acpi_ut_create_internal_object(source_object->
  699. common.type);
  700. if (!target_object) {
  701. return (AE_NO_MEMORY);
  702. }
  703. status =
  704. acpi_ut_copy_simple_object(source_object,
  705. target_object);
  706. if (ACPI_FAILURE(status)) {
  707. goto error_exit;
  708. }
  709. *this_target_ptr = target_object;
  710. } else {
  711. /* Pass through a null element */
  712. *this_target_ptr = NULL;
  713. }
  714. break;
  715. case ACPI_COPY_TYPE_PACKAGE:
  716. /*
  717. * This object is a package - go down another nesting level
  718. * Create and build the package object
  719. */
  720. target_object =
  721. acpi_ut_create_package_object(source_object->package.count);
  722. if (!target_object) {
  723. return (AE_NO_MEMORY);
  724. }
  725. target_object->common.flags = source_object->common.flags;
  726. /* Pass the new package object back to the package walk routine */
  727. state->pkg.this_target_obj = target_object;
  728. /* Store the object pointer in the parent package object */
  729. *this_target_ptr = target_object;
  730. break;
  731. default:
  732. return (AE_BAD_PARAMETER);
  733. }
  734. return (status);
  735. error_exit:
  736. acpi_ut_remove_reference(target_object);
  737. return (status);
  738. }
  739. /*******************************************************************************
  740. *
  741. * FUNCTION: acpi_ut_copy_ipackage_to_ipackage
  742. *
  743. * PARAMETERS: source_obj - Pointer to the source package object
  744. * dest_obj - Where the internal object is returned
  745. * walk_state - Current Walk state descriptor
  746. *
  747. * RETURN: Status
  748. *
  749. * DESCRIPTION: This function is called to copy an internal package object
  750. * into another internal package object.
  751. *
  752. ******************************************************************************/
  753. static acpi_status
  754. acpi_ut_copy_ipackage_to_ipackage(union acpi_operand_object *source_obj,
  755. union acpi_operand_object *dest_obj,
  756. struct acpi_walk_state *walk_state)
  757. {
  758. acpi_status status = AE_OK;
  759. ACPI_FUNCTION_TRACE(ut_copy_ipackage_to_ipackage);
  760. dest_obj->common.type = source_obj->common.type;
  761. dest_obj->common.flags = source_obj->common.flags;
  762. dest_obj->package.count = source_obj->package.count;
  763. /*
  764. * Create the object array and walk the source package tree
  765. */
  766. dest_obj->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
  767. source_obj->package.
  768. count +
  769. 1) * sizeof(void *));
  770. if (!dest_obj->package.elements) {
  771. ACPI_ERROR((AE_INFO, "Package allocation failure"));
  772. return_ACPI_STATUS(AE_NO_MEMORY);
  773. }
  774. /*
  775. * Copy the package element-by-element by walking the package "tree".
  776. * This handles nested packages of arbitrary depth.
  777. */
  778. status = acpi_ut_walk_package_tree(source_obj, dest_obj,
  779. acpi_ut_copy_ielement_to_ielement,
  780. walk_state);
  781. if (ACPI_FAILURE(status)) {
  782. /* On failure, delete the destination package object */
  783. acpi_ut_remove_reference(dest_obj);
  784. }
  785. return_ACPI_STATUS(status);
  786. }
  787. /*******************************************************************************
  788. *
  789. * FUNCTION: acpi_ut_copy_iobject_to_iobject
  790. *
  791. * PARAMETERS: source_desc - The internal object to be copied
  792. * dest_desc - Where the copied object is returned
  793. * walk_state - Current walk state
  794. *
  795. * RETURN: Status
  796. *
  797. * DESCRIPTION: Copy an internal object to a new internal object
  798. *
  799. ******************************************************************************/
  800. acpi_status
  801. acpi_ut_copy_iobject_to_iobject(union acpi_operand_object *source_desc,
  802. union acpi_operand_object **dest_desc,
  803. struct acpi_walk_state *walk_state)
  804. {
  805. acpi_status status = AE_OK;
  806. ACPI_FUNCTION_TRACE(ut_copy_iobject_to_iobject);
  807. /* Create the top level object */
  808. *dest_desc = acpi_ut_create_internal_object(source_desc->common.type);
  809. if (!*dest_desc) {
  810. return_ACPI_STATUS(AE_NO_MEMORY);
  811. }
  812. /* Copy the object and possible subobjects */
  813. if (source_desc->common.type == ACPI_TYPE_PACKAGE) {
  814. status =
  815. acpi_ut_copy_ipackage_to_ipackage(source_desc, *dest_desc,
  816. walk_state);
  817. } else {
  818. status = acpi_ut_copy_simple_object(source_desc, *dest_desc);
  819. }
  820. /* Delete the allocated object if copy failed */
  821. if (ACPI_FAILURE(status)) {
  822. acpi_ut_remove_reference(*dest_desc);
  823. }
  824. return_ACPI_STATUS(status);
  825. }