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