Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-for-linus-2.6
[sfrench/cifs-2.6.git] / drivers / acpi / executer / exstorob.c
1
2 /******************************************************************************
3  *
4  * Module Name: exstorob - AML Interpreter object store support, store to object
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2005, R. Byron Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48
49
50 #define _COMPONENT          ACPI_EXECUTER
51          ACPI_MODULE_NAME    ("exstorob")
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ex_store_buffer_to_buffer
57  *
58  * PARAMETERS:  source_desc         - Source object to copy
59  *              target_desc         - Destination object of the copy
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Copy a buffer object to another buffer object.
64  *
65  ******************************************************************************/
66
67 acpi_status
68 acpi_ex_store_buffer_to_buffer (
69         union acpi_operand_object       *source_desc,
70         union acpi_operand_object       *target_desc)
71 {
72         u32                             length;
73         u8                              *buffer;
74
75
76         ACPI_FUNCTION_TRACE_PTR ("ex_store_buffer_to_buffer", source_desc);
77
78
79         /* We know that source_desc is a buffer by now */
80
81         buffer = (u8 *) source_desc->buffer.pointer;
82         length = source_desc->buffer.length;
83
84         /*
85          * If target is a buffer of length zero or is a static buffer,
86          * allocate a new buffer of the proper length
87          */
88         if ((target_desc->buffer.length == 0) ||
89                 (target_desc->common.flags & AOPOBJ_STATIC_POINTER)) {
90                 target_desc->buffer.pointer = ACPI_MEM_ALLOCATE (length);
91                 if (!target_desc->buffer.pointer) {
92                         return_ACPI_STATUS (AE_NO_MEMORY);
93                 }
94
95                 target_desc->buffer.length = length;
96         }
97
98         /* Copy source buffer to target buffer */
99
100         if (length <= target_desc->buffer.length) {
101                 /* Clear existing buffer and copy in the new one */
102
103                 ACPI_MEMSET (target_desc->buffer.pointer, 0, target_desc->buffer.length);
104                 ACPI_MEMCPY (target_desc->buffer.pointer, buffer, length);
105
106 #ifdef ACPI_OBSOLETE_BEHAVIOR
107                 /*
108                  * NOTE: ACPI versions up to 3.0 specified that the buffer must be
109                  * truncated if the string is smaller than the buffer.  However, "other"
110                  * implementations of ACPI never did this and thus became the defacto
111                  * standard. ACPi 3.0_a changes this behavior such that the buffer
112                  * is no longer truncated.
113                  */
114
115                 /*
116                  * OBSOLETE BEHAVIOR:
117                  * If the original source was a string, we must truncate the buffer,
118                  * according to the ACPI spec.  Integer-to-Buffer and Buffer-to-Buffer
119                  * copy must not truncate the original buffer.
120                  */
121                 if (original_src_type == ACPI_TYPE_STRING) {
122                         /* Set the new length of the target */
123
124                         target_desc->buffer.length = length;
125                 }
126 #endif
127         }
128         else {
129                 /* Truncate the source, copy only what will fit */
130
131                 ACPI_MEMCPY (target_desc->buffer.pointer, buffer,
132                         target_desc->buffer.length);
133
134                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
135                         "Truncating source buffer from %X to %X\n",
136                         length, target_desc->buffer.length));
137         }
138
139         /* Copy flags */
140
141         target_desc->buffer.flags = source_desc->buffer.flags;
142         target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
143         return_ACPI_STATUS (AE_OK);
144 }
145
146
147 /*******************************************************************************
148  *
149  * FUNCTION:    acpi_ex_store_string_to_string
150  *
151  * PARAMETERS:  source_desc         - Source object to copy
152  *              target_desc         - Destination object of the copy
153  *
154  * RETURN:      Status
155  *
156  * DESCRIPTION: Copy a String object to another String object
157  *
158  ******************************************************************************/
159
160 acpi_status
161 acpi_ex_store_string_to_string (
162         union acpi_operand_object       *source_desc,
163         union acpi_operand_object       *target_desc)
164 {
165         u32                             length;
166         u8                              *buffer;
167
168
169         ACPI_FUNCTION_TRACE_PTR ("ex_store_string_to_string", source_desc);
170
171
172         /* We know that source_desc is a string by now */
173
174         buffer = (u8 *) source_desc->string.pointer;
175         length = source_desc->string.length;
176
177         /*
178          * Replace existing string value if it will fit and the string
179          * pointer is not a static pointer (part of an ACPI table)
180          */
181         if ((length < target_desc->string.length) &&
182            (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
183                 /*
184                  * String will fit in existing non-static buffer.
185                  * Clear old string and copy in the new one
186                  */
187                 ACPI_MEMSET (target_desc->string.pointer, 0,
188                         (acpi_size) target_desc->string.length + 1);
189                 ACPI_MEMCPY (target_desc->string.pointer, buffer, length);
190         }
191         else {
192                 /*
193                  * Free the current buffer, then allocate a new buffer
194                  * large enough to hold the value
195                  */
196                 if (target_desc->string.pointer &&
197                    (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
198                         /* Only free if not a pointer into the DSDT */
199
200                         ACPI_MEM_FREE (target_desc->string.pointer);
201                 }
202
203                 target_desc->string.pointer = ACPI_MEM_CALLOCATE (
204                                    (acpi_size) length + 1);
205                 if (!target_desc->string.pointer) {
206                         return_ACPI_STATUS (AE_NO_MEMORY);
207                 }
208
209                 target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
210                 ACPI_MEMCPY (target_desc->string.pointer, buffer, length);
211         }
212
213         /* Set the new target length */
214
215         target_desc->string.length = length;
216         return_ACPI_STATUS (AE_OK);
217 }
218
219