exconcat.c revision 1.1 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: exconcat - Concatenate-type AML operators
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2016, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpi.h"
45 1.1 christos #include "accommon.h"
46 1.1 christos #include "acinterp.h"
47 1.1 christos #include "amlresrc.h"
48 1.1 christos
49 1.1 christos
50 1.1 christos #define _COMPONENT ACPI_EXECUTER
51 1.1 christos ACPI_MODULE_NAME ("exconcat")
52 1.1 christos
53 1.1 christos /* Local Prototypes */
54 1.1 christos
55 1.1 christos static ACPI_STATUS
56 1.1 christos AcpiExConvertToObjectTypeString (
57 1.1 christos ACPI_OPERAND_OBJECT *ObjDesc,
58 1.1 christos ACPI_OPERAND_OBJECT **ResultDesc);
59 1.1 christos
60 1.1 christos
61 1.1 christos /*******************************************************************************
62 1.1 christos *
63 1.1 christos * FUNCTION: AcpiExDoConcatenate
64 1.1 christos *
65 1.1 christos * PARAMETERS: Operand0 - First source object
66 1.1 christos * Operand1 - Second source object
67 1.1 christos * ActualReturnDesc - Where to place the return object
68 1.1 christos * WalkState - Current walk state
69 1.1 christos *
70 1.1 christos * RETURN: Status
71 1.1 christos *
72 1.1 christos * DESCRIPTION: Concatenate two objects with the ACPI-defined conversion
73 1.1 christos * rules as necessary.
74 1.1 christos * NOTE:
75 1.1 christos * Per the ACPI spec (up to 6.1), Concatenate only supports Integer,
76 1.1 christos * String, and Buffer objects. However, we support all objects here
77 1.1 christos * as an extension. This improves the usefulness of both Concatenate
78 1.1 christos * and the Printf/Fprintf macros. The extension returns a string
79 1.1 christos * describing the object type for the other objects.
80 1.1 christos * 02/2016.
81 1.1 christos *
82 1.1 christos ******************************************************************************/
83 1.1 christos
84 1.1 christos ACPI_STATUS
85 1.1 christos AcpiExDoConcatenate (
86 1.1 christos ACPI_OPERAND_OBJECT *Operand0,
87 1.1 christos ACPI_OPERAND_OBJECT *Operand1,
88 1.1 christos ACPI_OPERAND_OBJECT **ActualReturnDesc,
89 1.1 christos ACPI_WALK_STATE *WalkState)
90 1.1 christos {
91 1.1 christos ACPI_OPERAND_OBJECT *LocalOperand0 = Operand0;
92 1.1 christos ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
93 1.1 christos ACPI_OPERAND_OBJECT *TempOperand1 = NULL;
94 1.1 christos ACPI_OPERAND_OBJECT *ReturnDesc;
95 1.1 christos char *Buffer;
96 1.1 christos ACPI_OBJECT_TYPE Operand0Type;
97 1.1 christos ACPI_OBJECT_TYPE Operand1Type;
98 1.1 christos ACPI_STATUS Status;
99 1.1 christos
100 1.1 christos
101 1.1 christos ACPI_FUNCTION_TRACE (ExDoConcatenate);
102 1.1 christos
103 1.1 christos
104 1.1 christos /* Operand 0 preprocessing */
105 1.1 christos
106 1.1 christos switch (Operand0->Common.Type)
107 1.1 christos {
108 1.1 christos case ACPI_TYPE_INTEGER:
109 1.1 christos case ACPI_TYPE_STRING:
110 1.1 christos case ACPI_TYPE_BUFFER:
111 1.1 christos
112 1.1 christos Operand0Type = Operand0->Common.Type;
113 1.1 christos break;
114 1.1 christos
115 1.1 christos default:
116 1.1 christos
117 1.1 christos /* For all other types, get the "object type" string */
118 1.1 christos
119 1.1 christos Status = AcpiExConvertToObjectTypeString (
120 1.1 christos Operand0, &LocalOperand0);
121 1.1 christos if (ACPI_FAILURE (Status))
122 1.1 christos {
123 1.1 christos goto Cleanup;
124 1.1 christos }
125 1.1 christos
126 1.1 christos Operand0Type = ACPI_TYPE_STRING;
127 1.1 christos break;
128 1.1 christos }
129 1.1 christos
130 1.1 christos /* Operand 1 preprocessing */
131 1.1 christos
132 1.1 christos switch (Operand1->Common.Type)
133 1.1 christos {
134 1.1 christos case ACPI_TYPE_INTEGER:
135 1.1 christos case ACPI_TYPE_STRING:
136 1.1 christos case ACPI_TYPE_BUFFER:
137 1.1 christos
138 1.1 christos Operand1Type = Operand1->Common.Type;
139 1.1 christos break;
140 1.1 christos
141 1.1 christos default:
142 1.1 christos
143 1.1 christos /* For all other types, get the "object type" string */
144 1.1 christos
145 1.1 christos Status = AcpiExConvertToObjectTypeString (
146 1.1 christos Operand1, &LocalOperand1);
147 1.1 christos if (ACPI_FAILURE (Status))
148 1.1 christos {
149 1.1 christos goto Cleanup;
150 1.1 christos }
151 1.1 christos
152 1.1 christos Operand1Type = ACPI_TYPE_STRING;
153 1.1 christos break;
154 1.1 christos }
155 1.1 christos
156 1.1 christos /*
157 1.1 christos * Convert the second operand if necessary. The first operand (0)
158 1.1 christos * determines the type of the second operand (1) (See the Data Types
159 1.1 christos * section of the ACPI specification). Both object types are
160 1.1 christos * guaranteed to be either Integer/String/Buffer by the operand
161 1.1 christos * resolution mechanism.
162 1.1 christos */
163 1.1 christos switch (Operand0Type)
164 1.1 christos {
165 1.1 christos case ACPI_TYPE_INTEGER:
166 1.1 christos
167 1.1 christos Status = AcpiExConvertToInteger (LocalOperand1, &TempOperand1, 16);
168 1.1 christos break;
169 1.1 christos
170 1.1 christos case ACPI_TYPE_BUFFER:
171 1.1 christos
172 1.1 christos Status = AcpiExConvertToBuffer (LocalOperand1, &TempOperand1);
173 1.1 christos break;
174 1.1 christos
175 1.1 christos case ACPI_TYPE_STRING:
176 1.1 christos
177 1.1 christos switch (Operand1Type)
178 1.1 christos {
179 1.1 christos case ACPI_TYPE_INTEGER:
180 1.1 christos case ACPI_TYPE_STRING:
181 1.1 christos case ACPI_TYPE_BUFFER:
182 1.1 christos
183 1.1 christos /* Other types have already been converted to string */
184 1.1 christos
185 1.1 christos Status = AcpiExConvertToString (
186 1.1 christos LocalOperand1, &TempOperand1, ACPI_IMPLICIT_CONVERT_HEX);
187 1.1 christos break;
188 1.1 christos
189 1.1 christos default:
190 1.1 christos
191 1.1 christos Status = AE_OK;
192 1.1 christos break;
193 1.1 christos }
194 1.1 christos break;
195 1.1 christos
196 1.1 christos default:
197 1.1 christos
198 1.1 christos ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
199 1.1 christos Operand0->Common.Type));
200 1.1 christos Status = AE_AML_INTERNAL;
201 1.1 christos }
202 1.1 christos
203 1.1 christos if (ACPI_FAILURE (Status))
204 1.1 christos {
205 1.1 christos goto Cleanup;
206 1.1 christos }
207 1.1 christos
208 1.1 christos /* Take care with any newly created operand objects */
209 1.1 christos
210 1.1 christos if ((LocalOperand1 != Operand1) &&
211 1.1 christos (LocalOperand1 != TempOperand1))
212 1.1 christos {
213 1.1 christos AcpiUtRemoveReference (LocalOperand1);
214 1.1 christos }
215 1.1 christos
216 1.1 christos LocalOperand1 = TempOperand1;
217 1.1 christos
218 1.1 christos /*
219 1.1 christos * Both operands are now known to be the same object type
220 1.1 christos * (Both are Integer, String, or Buffer), and we can now perform
221 1.1 christos * the concatenation.
222 1.1 christos *
223 1.1 christos * There are three cases to handle, as per the ACPI spec:
224 1.1 christos *
225 1.1 christos * 1) Two Integers concatenated to produce a new Buffer
226 1.1 christos * 2) Two Strings concatenated to produce a new String
227 1.1 christos * 3) Two Buffers concatenated to produce a new Buffer
228 1.1 christos */
229 1.1 christos switch (Operand0Type)
230 1.1 christos {
231 1.1 christos case ACPI_TYPE_INTEGER:
232 1.1 christos
233 1.1 christos /* Result of two Integers is a Buffer */
234 1.1 christos /* Need enough buffer space for two integers */
235 1.1 christos
236 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (
237 1.1 christos (ACPI_SIZE) ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
238 1.1 christos if (!ReturnDesc)
239 1.1 christos {
240 1.1 christos Status = AE_NO_MEMORY;
241 1.1 christos goto Cleanup;
242 1.1 christos }
243 1.1 christos
244 1.1 christos Buffer = (char *) ReturnDesc->Buffer.Pointer;
245 1.1 christos
246 1.1 christos /* Copy the first integer, LSB first */
247 1.1 christos
248 1.1 christos memcpy (Buffer, &Operand0->Integer.Value,
249 1.1 christos AcpiGbl_IntegerByteWidth);
250 1.1 christos
251 1.1 christos /* Copy the second integer (LSB first) after the first */
252 1.1 christos
253 1.1 christos memcpy (Buffer + AcpiGbl_IntegerByteWidth,
254 1.1 christos &LocalOperand1->Integer.Value, AcpiGbl_IntegerByteWidth);
255 1.1 christos break;
256 1.1 christos
257 1.1 christos case ACPI_TYPE_STRING:
258 1.1 christos
259 1.1 christos /* Result of two Strings is a String */
260 1.1 christos
261 1.1 christos ReturnDesc = AcpiUtCreateStringObject (
262 1.1 christos ((ACPI_SIZE) LocalOperand0->String.Length +
263 1.1 christos LocalOperand1->String.Length));
264 1.1 christos if (!ReturnDesc)
265 1.1 christos {
266 1.1 christos Status = AE_NO_MEMORY;
267 1.1 christos goto Cleanup;
268 1.1 christos }
269 1.1 christos
270 1.1 christos Buffer = ReturnDesc->String.Pointer;
271 1.1 christos
272 1.1 christos /* Concatenate the strings */
273 1.1 christos
274 1.1 christos strcpy (Buffer, LocalOperand0->String.Pointer);
275 1.1 christos strcat (Buffer, LocalOperand1->String.Pointer);
276 1.1 christos break;
277 1.1 christos
278 1.1 christos case ACPI_TYPE_BUFFER:
279 1.1 christos
280 1.1 christos /* Result of two Buffers is a Buffer */
281 1.1 christos
282 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (
283 1.1 christos ((ACPI_SIZE) Operand0->Buffer.Length +
284 1.1 christos LocalOperand1->Buffer.Length));
285 1.1 christos if (!ReturnDesc)
286 1.1 christos {
287 1.1 christos Status = AE_NO_MEMORY;
288 1.1 christos goto Cleanup;
289 1.1 christos }
290 1.1 christos
291 1.1 christos Buffer = (char *) ReturnDesc->Buffer.Pointer;
292 1.1 christos
293 1.1 christos /* Concatenate the buffers */
294 1.1 christos
295 1.1 christos memcpy (Buffer, Operand0->Buffer.Pointer,
296 1.1 christos Operand0->Buffer.Length);
297 1.1 christos memcpy (Buffer + Operand0->Buffer.Length,
298 1.1 christos LocalOperand1->Buffer.Pointer,
299 1.1 christos LocalOperand1->Buffer.Length);
300 1.1 christos break;
301 1.1 christos
302 1.1 christos default:
303 1.1 christos
304 1.1 christos /* Invalid object type, should not happen here */
305 1.1 christos
306 1.1 christos ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
307 1.1 christos Operand0->Common.Type));
308 1.1 christos Status = AE_AML_INTERNAL;
309 1.1 christos goto Cleanup;
310 1.1 christos }
311 1.1 christos
312 1.1 christos *ActualReturnDesc = ReturnDesc;
313 1.1 christos
314 1.1 christos Cleanup:
315 1.1 christos if (LocalOperand0 != Operand0)
316 1.1 christos {
317 1.1 christos AcpiUtRemoveReference (LocalOperand0);
318 1.1 christos }
319 1.1 christos
320 1.1 christos if (LocalOperand1 != Operand1)
321 1.1 christos {
322 1.1 christos AcpiUtRemoveReference (LocalOperand1);
323 1.1 christos }
324 1.1 christos
325 1.1 christos return_ACPI_STATUS (Status);
326 1.1 christos }
327 1.1 christos
328 1.1 christos
329 1.1 christos /*******************************************************************************
330 1.1 christos *
331 1.1 christos * FUNCTION: AcpiExConvertToObjectTypeString
332 1.1 christos *
333 1.1 christos * PARAMETERS: ObjDesc - Object to be converted
334 1.1 christos * ReturnDesc - Where to place the return object
335 1.1 christos *
336 1.1 christos * RETURN: Status
337 1.1 christos *
338 1.1 christos * DESCRIPTION: Convert an object of arbitrary type to a string object that
339 1.1 christos * contains the namestring for the object. Used for the
340 1.1 christos * concatenate operator.
341 1.1 christos *
342 1.1 christos ******************************************************************************/
343 1.1 christos
344 1.1 christos static ACPI_STATUS
345 1.1 christos AcpiExConvertToObjectTypeString (
346 1.1 christos ACPI_OPERAND_OBJECT *ObjDesc,
347 1.1 christos ACPI_OPERAND_OBJECT **ResultDesc)
348 1.1 christos {
349 1.1 christos ACPI_OPERAND_OBJECT *ReturnDesc;
350 1.1 christos const char *TypeString;
351 1.1 christos
352 1.1 christos
353 1.1 christos TypeString = AcpiUtGetTypeName (ObjDesc->Common.Type);
354 1.1 christos
355 1.1 christos ReturnDesc = AcpiUtCreateStringObject (
356 1.1 christos ((ACPI_SIZE) strlen (TypeString) + 9)); /* 9 For "[ Object]" */
357 1.1 christos if (!ReturnDesc)
358 1.1 christos {
359 1.1 christos return (AE_NO_MEMORY);
360 1.1 christos }
361 1.1 christos
362 1.1 christos strcpy (ReturnDesc->String.Pointer, "[");
363 1.1 christos strcat (ReturnDesc->String.Pointer, TypeString);
364 1.1 christos strcat (ReturnDesc->String.Pointer, " Object]");
365 1.1 christos
366 1.1 christos *ResultDesc = ReturnDesc;
367 1.1 christos return (AE_OK);
368 1.1 christos }
369 1.1 christos
370 1.1 christos
371 1.1 christos /*******************************************************************************
372 1.1 christos *
373 1.1 christos * FUNCTION: AcpiExConcatTemplate
374 1.1 christos *
375 1.1 christos * PARAMETERS: Operand0 - First source object
376 1.1 christos * Operand1 - Second source object
377 1.1 christos * ActualReturnDesc - Where to place the return object
378 1.1 christos * WalkState - Current walk state
379 1.1 christos *
380 1.1 christos * RETURN: Status
381 1.1 christos *
382 1.1 christos * DESCRIPTION: Concatenate two resource templates
383 1.1 christos *
384 1.1 christos ******************************************************************************/
385 1.1 christos
386 1.1 christos ACPI_STATUS
387 1.1 christos AcpiExConcatTemplate (
388 1.1 christos ACPI_OPERAND_OBJECT *Operand0,
389 1.1 christos ACPI_OPERAND_OBJECT *Operand1,
390 1.1 christos ACPI_OPERAND_OBJECT **ActualReturnDesc,
391 1.1 christos ACPI_WALK_STATE *WalkState)
392 1.1 christos {
393 1.1 christos ACPI_STATUS Status;
394 1.1 christos ACPI_OPERAND_OBJECT *ReturnDesc;
395 1.1 christos UINT8 *NewBuf;
396 1.1 christos UINT8 *EndTag;
397 1.1 christos ACPI_SIZE Length0;
398 1.1 christos ACPI_SIZE Length1;
399 1.1 christos ACPI_SIZE NewLength;
400 1.1 christos
401 1.1 christos
402 1.1 christos ACPI_FUNCTION_TRACE (ExConcatTemplate);
403 1.1 christos
404 1.1 christos
405 1.1 christos /*
406 1.1 christos * Find the EndTag descriptor in each resource template.
407 1.1 christos * Note1: returned pointers point TO the EndTag, not past it.
408 1.1 christos * Note2: zero-length buffers are allowed; treated like one EndTag
409 1.1 christos */
410 1.1 christos
411 1.1 christos /* Get the length of the first resource template */
412 1.1 christos
413 1.1 christos Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);
414 1.1 christos if (ACPI_FAILURE (Status))
415 1.1 christos {
416 1.1 christos return_ACPI_STATUS (Status);
417 1.1 christos }
418 1.1 christos
419 1.1 christos Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);
420 1.1 christos
421 1.1 christos /* Get the length of the second resource template */
422 1.1 christos
423 1.1 christos Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);
424 1.1 christos if (ACPI_FAILURE (Status))
425 1.1 christos {
426 1.1 christos return_ACPI_STATUS (Status);
427 1.1 christos }
428 1.1 christos
429 1.1 christos Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);
430 1.1 christos
431 1.1 christos /* Combine both lengths, minimum size will be 2 for EndTag */
432 1.1 christos
433 1.1 christos NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);
434 1.1 christos
435 1.1 christos /* Create a new buffer object for the result (with one EndTag) */
436 1.1 christos
437 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (NewLength);
438 1.1 christos if (!ReturnDesc)
439 1.1 christos {
440 1.1 christos return_ACPI_STATUS (AE_NO_MEMORY);
441 1.1 christos }
442 1.1 christos
443 1.1 christos /*
444 1.1 christos * Copy the templates to the new buffer, 0 first, then 1 follows. One
445 1.1 christos * EndTag descriptor is copied from Operand1.
446 1.1 christos */
447 1.1 christos NewBuf = ReturnDesc->Buffer.Pointer;
448 1.1 christos memcpy (NewBuf, Operand0->Buffer.Pointer, Length0);
449 1.1 christos memcpy (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);
450 1.1 christos
451 1.1 christos /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
452 1.1 christos
453 1.1 christos NewBuf[NewLength - 1] = 0;
454 1.1 christos NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
455 1.1 christos
456 1.1 christos /* Return the completed resource template */
457 1.1 christos
458 1.1 christos *ActualReturnDesc = ReturnDesc;
459 1.1 christos return_ACPI_STATUS (AE_OK);
460 1.1 christos }
461