1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: exstorob - AML Interpreter object store support, store to object
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc *****************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acinterp.h"
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc
49433d6423SLionel Sambuc #define _COMPONENT ACPI_EXECUTER
50433d6423SLionel Sambuc ACPI_MODULE_NAME ("exstorob")
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc
53433d6423SLionel Sambuc /*******************************************************************************
54433d6423SLionel Sambuc *
55433d6423SLionel Sambuc * FUNCTION: AcpiExStoreBufferToBuffer
56433d6423SLionel Sambuc *
57433d6423SLionel Sambuc * PARAMETERS: SourceDesc - Source object to copy
58433d6423SLionel Sambuc * TargetDesc - Destination object of the copy
59433d6423SLionel Sambuc *
60433d6423SLionel Sambuc * RETURN: Status
61433d6423SLionel Sambuc *
62433d6423SLionel Sambuc * DESCRIPTION: Copy a buffer object to another buffer object.
63433d6423SLionel Sambuc *
64433d6423SLionel Sambuc ******************************************************************************/
65433d6423SLionel Sambuc
66433d6423SLionel Sambuc ACPI_STATUS
AcpiExStoreBufferToBuffer(ACPI_OPERAND_OBJECT * SourceDesc,ACPI_OPERAND_OBJECT * TargetDesc)67433d6423SLionel Sambuc AcpiExStoreBufferToBuffer (
68433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *SourceDesc,
69433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *TargetDesc)
70433d6423SLionel Sambuc {
71433d6423SLionel Sambuc UINT32 Length;
72433d6423SLionel Sambuc UINT8 *Buffer;
73433d6423SLionel Sambuc
74433d6423SLionel Sambuc
75433d6423SLionel Sambuc ACPI_FUNCTION_TRACE_PTR (ExStoreBufferToBuffer, SourceDesc);
76433d6423SLionel Sambuc
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc /* If Source and Target are the same, just return */
79433d6423SLionel Sambuc
80433d6423SLionel Sambuc if (SourceDesc == TargetDesc)
81433d6423SLionel Sambuc {
82433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
83433d6423SLionel Sambuc }
84433d6423SLionel Sambuc
85433d6423SLionel Sambuc /* We know that SourceDesc is a buffer by now */
86433d6423SLionel Sambuc
87433d6423SLionel Sambuc Buffer = ACPI_CAST_PTR (UINT8, SourceDesc->Buffer.Pointer);
88433d6423SLionel Sambuc Length = SourceDesc->Buffer.Length;
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc /*
91433d6423SLionel Sambuc * If target is a buffer of length zero or is a static buffer,
92433d6423SLionel Sambuc * allocate a new buffer of the proper length
93433d6423SLionel Sambuc */
94433d6423SLionel Sambuc if ((TargetDesc->Buffer.Length == 0) ||
95433d6423SLionel Sambuc (TargetDesc->Common.Flags & AOPOBJ_STATIC_POINTER))
96433d6423SLionel Sambuc {
97433d6423SLionel Sambuc TargetDesc->Buffer.Pointer = ACPI_ALLOCATE (Length);
98433d6423SLionel Sambuc if (!TargetDesc->Buffer.Pointer)
99433d6423SLionel Sambuc {
100433d6423SLionel Sambuc return_ACPI_STATUS (AE_NO_MEMORY);
101433d6423SLionel Sambuc }
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc TargetDesc->Buffer.Length = Length;
104433d6423SLionel Sambuc }
105433d6423SLionel Sambuc
106433d6423SLionel Sambuc /* Copy source buffer to target buffer */
107433d6423SLionel Sambuc
108433d6423SLionel Sambuc if (Length <= TargetDesc->Buffer.Length)
109433d6423SLionel Sambuc {
110433d6423SLionel Sambuc /* Clear existing buffer and copy in the new one */
111433d6423SLionel Sambuc
112433d6423SLionel Sambuc ACPI_MEMSET (TargetDesc->Buffer.Pointer, 0, TargetDesc->Buffer.Length);
113433d6423SLionel Sambuc ACPI_MEMCPY (TargetDesc->Buffer.Pointer, Buffer, Length);
114433d6423SLionel Sambuc
115433d6423SLionel Sambuc #ifdef ACPI_OBSOLETE_BEHAVIOR
116433d6423SLionel Sambuc /*
117433d6423SLionel Sambuc * NOTE: ACPI versions up to 3.0 specified that the buffer must be
118433d6423SLionel Sambuc * truncated if the string is smaller than the buffer. However, "other"
119433d6423SLionel Sambuc * implementations of ACPI never did this and thus became the defacto
120433d6423SLionel Sambuc * standard. ACPI 3.0A changes this behavior such that the buffer
121433d6423SLionel Sambuc * is no longer truncated.
122433d6423SLionel Sambuc */
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc /*
125433d6423SLionel Sambuc * OBSOLETE BEHAVIOR:
126433d6423SLionel Sambuc * If the original source was a string, we must truncate the buffer,
127433d6423SLionel Sambuc * according to the ACPI spec. Integer-to-Buffer and Buffer-to-Buffer
128433d6423SLionel Sambuc * copy must not truncate the original buffer.
129433d6423SLionel Sambuc */
130433d6423SLionel Sambuc if (OriginalSrcType == ACPI_TYPE_STRING)
131433d6423SLionel Sambuc {
132433d6423SLionel Sambuc /* Set the new length of the target */
133433d6423SLionel Sambuc
134433d6423SLionel Sambuc TargetDesc->Buffer.Length = Length;
135433d6423SLionel Sambuc }
136433d6423SLionel Sambuc #endif
137433d6423SLionel Sambuc }
138433d6423SLionel Sambuc else
139433d6423SLionel Sambuc {
140433d6423SLionel Sambuc /* Truncate the source, copy only what will fit */
141433d6423SLionel Sambuc
142433d6423SLionel Sambuc ACPI_MEMCPY (TargetDesc->Buffer.Pointer, Buffer,
143433d6423SLionel Sambuc TargetDesc->Buffer.Length);
144433d6423SLionel Sambuc
145433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
146433d6423SLionel Sambuc "Truncating source buffer from %X to %X\n",
147433d6423SLionel Sambuc Length, TargetDesc->Buffer.Length));
148433d6423SLionel Sambuc }
149433d6423SLionel Sambuc
150433d6423SLionel Sambuc /* Copy flags */
151433d6423SLionel Sambuc
152433d6423SLionel Sambuc TargetDesc->Buffer.Flags = SourceDesc->Buffer.Flags;
153433d6423SLionel Sambuc TargetDesc->Common.Flags &= ~AOPOBJ_STATIC_POINTER;
154433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
155433d6423SLionel Sambuc }
156433d6423SLionel Sambuc
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc /*******************************************************************************
159433d6423SLionel Sambuc *
160433d6423SLionel Sambuc * FUNCTION: AcpiExStoreStringToString
161433d6423SLionel Sambuc *
162433d6423SLionel Sambuc * PARAMETERS: SourceDesc - Source object to copy
163433d6423SLionel Sambuc * TargetDesc - Destination object of the copy
164433d6423SLionel Sambuc *
165433d6423SLionel Sambuc * RETURN: Status
166433d6423SLionel Sambuc *
167433d6423SLionel Sambuc * DESCRIPTION: Copy a String object to another String object
168433d6423SLionel Sambuc *
169433d6423SLionel Sambuc ******************************************************************************/
170433d6423SLionel Sambuc
171433d6423SLionel Sambuc ACPI_STATUS
AcpiExStoreStringToString(ACPI_OPERAND_OBJECT * SourceDesc,ACPI_OPERAND_OBJECT * TargetDesc)172433d6423SLionel Sambuc AcpiExStoreStringToString (
173433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *SourceDesc,
174433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *TargetDesc)
175433d6423SLionel Sambuc {
176433d6423SLionel Sambuc UINT32 Length;
177433d6423SLionel Sambuc UINT8 *Buffer;
178433d6423SLionel Sambuc
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc ACPI_FUNCTION_TRACE_PTR (ExStoreStringToString, SourceDesc);
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc /* If Source and Target are the same, just return */
184433d6423SLionel Sambuc
185433d6423SLionel Sambuc if (SourceDesc == TargetDesc)
186433d6423SLionel Sambuc {
187433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
188433d6423SLionel Sambuc }
189433d6423SLionel Sambuc
190433d6423SLionel Sambuc /* We know that SourceDesc is a string by now */
191433d6423SLionel Sambuc
192433d6423SLionel Sambuc Buffer = ACPI_CAST_PTR (UINT8, SourceDesc->String.Pointer);
193433d6423SLionel Sambuc Length = SourceDesc->String.Length;
194433d6423SLionel Sambuc
195433d6423SLionel Sambuc /*
196433d6423SLionel Sambuc * Replace existing string value if it will fit and the string
197433d6423SLionel Sambuc * pointer is not a static pointer (part of an ACPI table)
198433d6423SLionel Sambuc */
199433d6423SLionel Sambuc if ((Length < TargetDesc->String.Length) &&
200433d6423SLionel Sambuc (!(TargetDesc->Common.Flags & AOPOBJ_STATIC_POINTER)))
201433d6423SLionel Sambuc {
202433d6423SLionel Sambuc /*
203433d6423SLionel Sambuc * String will fit in existing non-static buffer.
204433d6423SLionel Sambuc * Clear old string and copy in the new one
205433d6423SLionel Sambuc */
206433d6423SLionel Sambuc ACPI_MEMSET (TargetDesc->String.Pointer, 0,
207433d6423SLionel Sambuc (ACPI_SIZE) TargetDesc->String.Length + 1);
208433d6423SLionel Sambuc ACPI_MEMCPY (TargetDesc->String.Pointer, Buffer, Length);
209433d6423SLionel Sambuc }
210433d6423SLionel Sambuc else
211433d6423SLionel Sambuc {
212433d6423SLionel Sambuc /*
213433d6423SLionel Sambuc * Free the current buffer, then allocate a new buffer
214433d6423SLionel Sambuc * large enough to hold the value
215433d6423SLionel Sambuc */
216433d6423SLionel Sambuc if (TargetDesc->String.Pointer &&
217433d6423SLionel Sambuc (!(TargetDesc->Common.Flags & AOPOBJ_STATIC_POINTER)))
218433d6423SLionel Sambuc {
219433d6423SLionel Sambuc /* Only free if not a pointer into the DSDT */
220433d6423SLionel Sambuc
221433d6423SLionel Sambuc ACPI_FREE (TargetDesc->String.Pointer);
222433d6423SLionel Sambuc }
223433d6423SLionel Sambuc
224433d6423SLionel Sambuc TargetDesc->String.Pointer = ACPI_ALLOCATE_ZEROED (
225433d6423SLionel Sambuc (ACPI_SIZE) Length + 1);
226433d6423SLionel Sambuc if (!TargetDesc->String.Pointer)
227433d6423SLionel Sambuc {
228433d6423SLionel Sambuc return_ACPI_STATUS (AE_NO_MEMORY);
229433d6423SLionel Sambuc }
230433d6423SLionel Sambuc
231433d6423SLionel Sambuc TargetDesc->Common.Flags &= ~AOPOBJ_STATIC_POINTER;
232433d6423SLionel Sambuc ACPI_MEMCPY (TargetDesc->String.Pointer, Buffer, Length);
233433d6423SLionel Sambuc }
234433d6423SLionel Sambuc
235433d6423SLionel Sambuc /* Set the new target length */
236433d6423SLionel Sambuc
237433d6423SLionel Sambuc TargetDesc->String.Length = Length;
238433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
239433d6423SLionel Sambuc }
240