Loading...
1
2/******************************************************************************
3 *
4 * Module Name: exstorob - AML Interpreter object store support, store to object
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2011, Intel Corp.
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#include <acpi/acpi.h>
46#include "accommon.h"
47#include "acinterp.h"
48
49#define _COMPONENT ACPI_EXECUTER
50ACPI_MODULE_NAME("exstorob")
51
52/*******************************************************************************
53 *
54 * FUNCTION: acpi_ex_store_buffer_to_buffer
55 *
56 * PARAMETERS: source_desc - Source object to copy
57 * target_desc - Destination object of the copy
58 *
59 * RETURN: Status
60 *
61 * DESCRIPTION: Copy a buffer object to another buffer object.
62 *
63 ******************************************************************************/
64acpi_status
65acpi_ex_store_buffer_to_buffer(union acpi_operand_object *source_desc,
66 union acpi_operand_object *target_desc)
67{
68 u32 length;
69 u8 *buffer;
70
71 ACPI_FUNCTION_TRACE_PTR(ex_store_buffer_to_buffer, source_desc);
72
73 /* If Source and Target are the same, just return */
74
75 if (source_desc == target_desc) {
76 return_ACPI_STATUS(AE_OK);
77 }
78
79 /* We know that source_desc is a buffer by now */
80
81 buffer = ACPI_CAST_PTR(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_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
102 /* Clear existing buffer and copy in the new one */
103
104 ACPI_MEMSET(target_desc->buffer.pointer, 0,
105 target_desc->buffer.length);
106 ACPI_MEMCPY(target_desc->buffer.pointer, buffer, length);
107
108#ifdef ACPI_OBSOLETE_BEHAVIOR
109 /*
110 * NOTE: ACPI versions up to 3.0 specified that the buffer must be
111 * truncated if the string is smaller than the buffer. However, "other"
112 * implementations of ACPI never did this and thus became the defacto
113 * standard. ACPI 3.0_a changes this behavior such that the buffer
114 * is no longer truncated.
115 */
116
117 /*
118 * OBSOLETE BEHAVIOR:
119 * If the original source was a string, we must truncate the buffer,
120 * according to the ACPI spec. Integer-to-Buffer and Buffer-to-Buffer
121 * copy must not truncate the original buffer.
122 */
123 if (original_src_type == ACPI_TYPE_STRING) {
124
125 /* Set the new length of the target */
126
127 target_desc->buffer.length = length;
128 }
129#endif
130 } else {
131 /* Truncate the source, copy only what will fit */
132
133 ACPI_MEMCPY(target_desc->buffer.pointer, buffer,
134 target_desc->buffer.length);
135
136 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
137 "Truncating source buffer from %X to %X\n",
138 length, target_desc->buffer.length));
139 }
140
141 /* Copy flags */
142
143 target_desc->buffer.flags = source_desc->buffer.flags;
144 target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
145 return_ACPI_STATUS(AE_OK);
146}
147
148/*******************************************************************************
149 *
150 * FUNCTION: acpi_ex_store_string_to_string
151 *
152 * PARAMETERS: source_desc - Source object to copy
153 * target_desc - Destination object of the copy
154 *
155 * RETURN: Status
156 *
157 * DESCRIPTION: Copy a String object to another String object
158 *
159 ******************************************************************************/
160
161acpi_status
162acpi_ex_store_string_to_string(union acpi_operand_object *source_desc,
163 union acpi_operand_object *target_desc)
164{
165 u32 length;
166 u8 *buffer;
167
168 ACPI_FUNCTION_TRACE_PTR(ex_store_string_to_string, source_desc);
169
170 /* If Source and Target are the same, just return */
171
172 if (source_desc == target_desc) {
173 return_ACPI_STATUS(AE_OK);
174 }
175
176 /* We know that source_desc is a string by now */
177
178 buffer = ACPI_CAST_PTR(u8, source_desc->string.pointer);
179 length = source_desc->string.length;
180
181 /*
182 * Replace existing string value if it will fit and the string
183 * pointer is not a static pointer (part of an ACPI table)
184 */
185 if ((length < target_desc->string.length) &&
186 (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
187 /*
188 * String will fit in existing non-static buffer.
189 * Clear old string and copy in the new one
190 */
191 ACPI_MEMSET(target_desc->string.pointer, 0,
192 (acpi_size) target_desc->string.length + 1);
193 ACPI_MEMCPY(target_desc->string.pointer, buffer, length);
194 } else {
195 /*
196 * Free the current buffer, then allocate a new buffer
197 * large enough to hold the value
198 */
199 if (target_desc->string.pointer &&
200 (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
201
202 /* Only free if not a pointer into the DSDT */
203
204 ACPI_FREE(target_desc->string.pointer);
205 }
206
207 target_desc->string.pointer = ACPI_ALLOCATE_ZEROED((acpi_size)
208 length + 1);
209 if (!target_desc->string.pointer) {
210 return_ACPI_STATUS(AE_NO_MEMORY);
211 }
212
213 target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
214 ACPI_MEMCPY(target_desc->string.pointer, buffer, length);
215 }
216
217 /* Set the new target length */
218
219 target_desc->string.length = length;
220 return_ACPI_STATUS(AE_OK);
221}
1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2/******************************************************************************
3 *
4 * Module Name: exstorob - AML object store support, store to object
5 *
6 * Copyright (C) 2000 - 2019, Intel Corp.
7 *
8 *****************************************************************************/
9
10#include <acpi/acpi.h>
11#include "accommon.h"
12#include "acinterp.h"
13
14#define _COMPONENT ACPI_EXECUTER
15ACPI_MODULE_NAME("exstorob")
16
17/*******************************************************************************
18 *
19 * FUNCTION: acpi_ex_store_buffer_to_buffer
20 *
21 * PARAMETERS: source_desc - Source object to copy
22 * target_desc - Destination object of the copy
23 *
24 * RETURN: Status
25 *
26 * DESCRIPTION: Copy a buffer object to another buffer object.
27 *
28 ******************************************************************************/
29acpi_status
30acpi_ex_store_buffer_to_buffer(union acpi_operand_object *source_desc,
31 union acpi_operand_object *target_desc)
32{
33 u32 length;
34 u8 *buffer;
35
36 ACPI_FUNCTION_TRACE_PTR(ex_store_buffer_to_buffer, source_desc);
37
38 /* If Source and Target are the same, just return */
39
40 if (source_desc == target_desc) {
41 return_ACPI_STATUS(AE_OK);
42 }
43
44 /* We know that source_desc is a buffer by now */
45
46 buffer = ACPI_CAST_PTR(u8, source_desc->buffer.pointer);
47 length = source_desc->buffer.length;
48
49 /*
50 * If target is a buffer of length zero or is a static buffer,
51 * allocate a new buffer of the proper length
52 */
53 if ((target_desc->buffer.length == 0) ||
54 (target_desc->common.flags & AOPOBJ_STATIC_POINTER)) {
55 target_desc->buffer.pointer = ACPI_ALLOCATE(length);
56 if (!target_desc->buffer.pointer) {
57 return_ACPI_STATUS(AE_NO_MEMORY);
58 }
59
60 target_desc->buffer.length = length;
61 }
62
63 /* Copy source buffer to target buffer */
64
65 if (length <= target_desc->buffer.length) {
66
67 /* Clear existing buffer and copy in the new one */
68
69 memset(target_desc->buffer.pointer, 0,
70 target_desc->buffer.length);
71 memcpy(target_desc->buffer.pointer, buffer, length);
72
73#ifdef ACPI_OBSOLETE_BEHAVIOR
74 /*
75 * NOTE: ACPI versions up to 3.0 specified that the buffer must be
76 * truncated if the string is smaller than the buffer. However, "other"
77 * implementations of ACPI never did this and thus became the defacto
78 * standard. ACPI 3.0A changes this behavior such that the buffer
79 * is no longer truncated.
80 */
81
82 /*
83 * OBSOLETE BEHAVIOR:
84 * If the original source was a string, we must truncate the buffer,
85 * according to the ACPI spec. Integer-to-Buffer and Buffer-to-Buffer
86 * copy must not truncate the original buffer.
87 */
88 if (original_src_type == ACPI_TYPE_STRING) {
89
90 /* Set the new length of the target */
91
92 target_desc->buffer.length = length;
93 }
94#endif
95 } else {
96 /* Truncate the source, copy only what will fit */
97
98 memcpy(target_desc->buffer.pointer, buffer,
99 target_desc->buffer.length);
100
101 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102 "Truncating source buffer from %X to %X\n",
103 length, target_desc->buffer.length));
104 }
105
106 /* Copy flags */
107
108 target_desc->buffer.flags = source_desc->buffer.flags;
109 target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
110 return_ACPI_STATUS(AE_OK);
111}
112
113/*******************************************************************************
114 *
115 * FUNCTION: acpi_ex_store_string_to_string
116 *
117 * PARAMETERS: source_desc - Source object to copy
118 * target_desc - Destination object of the copy
119 *
120 * RETURN: Status
121 *
122 * DESCRIPTION: Copy a String object to another String object
123 *
124 ******************************************************************************/
125
126acpi_status
127acpi_ex_store_string_to_string(union acpi_operand_object *source_desc,
128 union acpi_operand_object *target_desc)
129{
130 u32 length;
131 u8 *buffer;
132
133 ACPI_FUNCTION_TRACE_PTR(ex_store_string_to_string, source_desc);
134
135 /* If Source and Target are the same, just return */
136
137 if (source_desc == target_desc) {
138 return_ACPI_STATUS(AE_OK);
139 }
140
141 /* We know that source_desc is a string by now */
142
143 buffer = ACPI_CAST_PTR(u8, source_desc->string.pointer);
144 length = source_desc->string.length;
145
146 /*
147 * Replace existing string value if it will fit and the string
148 * pointer is not a static pointer (part of an ACPI table)
149 */
150 if ((length < target_desc->string.length) &&
151 (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
152 /*
153 * String will fit in existing non-static buffer.
154 * Clear old string and copy in the new one
155 */
156 memset(target_desc->string.pointer, 0,
157 (acpi_size)target_desc->string.length + 1);
158 memcpy(target_desc->string.pointer, buffer, length);
159 } else {
160 /*
161 * Free the current buffer, then allocate a new buffer
162 * large enough to hold the value
163 */
164 if (target_desc->string.pointer &&
165 (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
166
167 /* Only free if not a pointer into the DSDT */
168
169 ACPI_FREE(target_desc->string.pointer);
170 }
171
172 target_desc->string.pointer =
173 ACPI_ALLOCATE_ZEROED((acpi_size)length + 1);
174
175 if (!target_desc->string.pointer) {
176 return_ACPI_STATUS(AE_NO_MEMORY);
177 }
178
179 target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
180 memcpy(target_desc->string.pointer, buffer, length);
181 }
182
183 /* Set the new target length */
184
185 target_desc->string.length = length;
186 return_ACPI_STATUS(AE_OK);
187}