exconcat.c revision 1.1.1.2 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.1.2 christos Status = AcpiExConvertToInteger (LocalOperand1, &TempOperand1,
168 1.1.1.2 christos ACPI_STRTOUL_BASE16);
169 1.1 christos break;
170 1.1 christos
171 1.1 christos case ACPI_TYPE_BUFFER:
172 1.1 christos
173 1.1 christos Status = AcpiExConvertToBuffer (LocalOperand1, &TempOperand1);
174 1.1 christos break;
175 1.1 christos
176 1.1 christos case ACPI_TYPE_STRING:
177 1.1 christos
178 1.1 christos switch (Operand1Type)
179 1.1 christos {
180 1.1 christos case ACPI_TYPE_INTEGER:
181 1.1 christos case ACPI_TYPE_STRING:
182 1.1 christos case ACPI_TYPE_BUFFER:
183 1.1 christos
184 1.1 christos /* Other types have already been converted to string */
185 1.1 christos
186 1.1 christos Status = AcpiExConvertToString (
187 1.1 christos LocalOperand1, &TempOperand1, ACPI_IMPLICIT_CONVERT_HEX);
188 1.1 christos break;
189 1.1 christos
190 1.1 christos default:
191 1.1 christos
192 1.1 christos Status = AE_OK;
193 1.1 christos break;
194 1.1 christos }
195 1.1 christos break;
196 1.1 christos
197 1.1 christos default:
198 1.1 christos
199 1.1 christos ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
200 1.1 christos Operand0->Common.Type));
201 1.1 christos Status = AE_AML_INTERNAL;
202 1.1 christos }
203 1.1 christos
204 1.1 christos if (ACPI_FAILURE (Status))
205 1.1 christos {
206 1.1 christos goto Cleanup;
207 1.1 christos }
208 1.1 christos
209 1.1 christos /* Take care with any newly created operand objects */
210 1.1 christos
211 1.1 christos if ((LocalOperand1 != Operand1) &&
212 1.1 christos (LocalOperand1 != TempOperand1))
213 1.1 christos {
214 1.1 christos AcpiUtRemoveReference (LocalOperand1);
215 1.1 christos }
216 1.1 christos
217 1.1 christos LocalOperand1 = TempOperand1;
218 1.1 christos
219 1.1 christos /*
220 1.1 christos * Both operands are now known to be the same object type
221 1.1 christos * (Both are Integer, String, or Buffer), and we can now perform
222 1.1 christos * the concatenation.
223 1.1 christos *
224 1.1 christos * There are three cases to handle, as per the ACPI spec:
225 1.1 christos *
226 1.1 christos * 1) Two Integers concatenated to produce a new Buffer
227 1.1 christos * 2) Two Strings concatenated to produce a new String
228 1.1 christos * 3) Two Buffers concatenated to produce a new Buffer
229 1.1 christos */
230 1.1 christos switch (Operand0Type)
231 1.1 christos {
232 1.1 christos case ACPI_TYPE_INTEGER:
233 1.1 christos
234 1.1 christos /* Result of two Integers is a Buffer */
235 1.1 christos /* Need enough buffer space for two integers */
236 1.1 christos
237 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (
238 1.1 christos (ACPI_SIZE) ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
239 1.1 christos if (!ReturnDesc)
240 1.1 christos {
241 1.1 christos Status = AE_NO_MEMORY;
242 1.1 christos goto Cleanup;
243 1.1 christos }
244 1.1 christos
245 1.1 christos Buffer = (char *) ReturnDesc->Buffer.Pointer;
246 1.1 christos
247 1.1 christos /* Copy the first integer, LSB first */
248 1.1 christos
249 1.1 christos memcpy (Buffer, &Operand0->Integer.Value,
250 1.1 christos AcpiGbl_IntegerByteWidth);
251 1.1 christos
252 1.1 christos /* Copy the second integer (LSB first) after the first */
253 1.1 christos
254 1.1 christos memcpy (Buffer + AcpiGbl_IntegerByteWidth,
255 1.1 christos &LocalOperand1->Integer.Value, AcpiGbl_IntegerByteWidth);
256 1.1 christos break;
257 1.1 christos
258 1.1 christos case ACPI_TYPE_STRING:
259 1.1 christos
260 1.1 christos /* Result of two Strings is a String */
261 1.1 christos
262 1.1 christos ReturnDesc = AcpiUtCreateStringObject (
263 1.1 christos ((ACPI_SIZE) LocalOperand0->String.Length +
264 1.1 christos LocalOperand1->String.Length));
265 1.1 christos if (!ReturnDesc)
266 1.1 christos {
267 1.1 christos Status = AE_NO_MEMORY;
268 1.1 christos goto Cleanup;
269 1.1 christos }
270 1.1 christos
271 1.1 christos Buffer = ReturnDesc->String.Pointer;
272 1.1 christos
273 1.1 christos /* Concatenate the strings */
274 1.1 christos
275 1.1 christos strcpy (Buffer, LocalOperand0->String.Pointer);
276 1.1 christos strcat (Buffer, LocalOperand1->String.Pointer);
277 1.1 christos break;
278 1.1 christos
279 1.1 christos case ACPI_TYPE_BUFFER:
280 1.1 christos
281 1.1 christos /* Result of two Buffers is a Buffer */
282 1.1 christos
283 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (
284 1.1 christos ((ACPI_SIZE) Operand0->Buffer.Length +
285 1.1 christos LocalOperand1->Buffer.Length));
286 1.1 christos if (!ReturnDesc)
287 1.1 christos {
288 1.1 christos Status = AE_NO_MEMORY;
289 1.1 christos goto Cleanup;
290 1.1 christos }
291 1.1 christos
292 1.1 christos Buffer = (char *) ReturnDesc->Buffer.Pointer;
293 1.1 christos
294 1.1 christos /* Concatenate the buffers */
295 1.1 christos
296 1.1 christos memcpy (Buffer, Operand0->Buffer.Pointer,
297 1.1 christos Operand0->Buffer.Length);
298 1.1 christos memcpy (Buffer + Operand0->Buffer.Length,
299 1.1 christos LocalOperand1->Buffer.Pointer,
300 1.1 christos LocalOperand1->Buffer.Length);
301 1.1 christos break;
302 1.1 christos
303 1.1 christos default:
304 1.1 christos
305 1.1 christos /* Invalid object type, should not happen here */
306 1.1 christos
307 1.1 christos ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
308 1.1 christos Operand0->Common.Type));
309 1.1 christos Status = AE_AML_INTERNAL;
310 1.1 christos goto Cleanup;
311 1.1 christos }
312 1.1 christos
313 1.1 christos *ActualReturnDesc = ReturnDesc;
314 1.1 christos
315 1.1 christos Cleanup:
316 1.1 christos if (LocalOperand0 != Operand0)
317 1.1 christos {
318 1.1 christos AcpiUtRemoveReference (LocalOperand0);
319 1.1 christos }
320 1.1 christos
321 1.1 christos if (LocalOperand1 != Operand1)
322 1.1 christos {
323 1.1 christos AcpiUtRemoveReference (LocalOperand1);
324 1.1 christos }
325 1.1 christos
326 1.1 christos return_ACPI_STATUS (Status);
327 1.1 christos }
328 1.1 christos
329 1.1 christos
330 1.1 christos /*******************************************************************************
331 1.1 christos *
332 1.1 christos * FUNCTION: AcpiExConvertToObjectTypeString
333 1.1 christos *
334 1.1 christos * PARAMETERS: ObjDesc - Object to be converted
335 1.1 christos * ReturnDesc - Where to place the return object
336 1.1 christos *
337 1.1 christos * RETURN: Status
338 1.1 christos *
339 1.1 christos * DESCRIPTION: Convert an object of arbitrary type to a string object that
340 1.1 christos * contains the namestring for the object. Used for the
341 1.1 christos * concatenate operator.
342 1.1 christos *
343 1.1 christos ******************************************************************************/
344 1.1 christos
345 1.1 christos static ACPI_STATUS
346 1.1 christos AcpiExConvertToObjectTypeString (
347 1.1 christos ACPI_OPERAND_OBJECT *ObjDesc,
348 1.1 christos ACPI_OPERAND_OBJECT **ResultDesc)
349 1.1 christos {
350 1.1 christos ACPI_OPERAND_OBJECT *ReturnDesc;
351 1.1 christos const char *TypeString;
352 1.1 christos
353 1.1 christos
354 1.1 christos TypeString = AcpiUtGetTypeName (ObjDesc->Common.Type);
355 1.1 christos
356 1.1 christos ReturnDesc = AcpiUtCreateStringObject (
357 1.1 christos ((ACPI_SIZE) strlen (TypeString) + 9)); /* 9 For "[ Object]" */
358 1.1 christos if (!ReturnDesc)
359 1.1 christos {
360 1.1 christos return (AE_NO_MEMORY);
361 1.1 christos }
362 1.1 christos
363 1.1 christos strcpy (ReturnDesc->String.Pointer, "[");
364 1.1 christos strcat (ReturnDesc->String.Pointer, TypeString);
365 1.1 christos strcat (ReturnDesc->String.Pointer, " Object]");
366 1.1 christos
367 1.1 christos *ResultDesc = ReturnDesc;
368 1.1 christos return (AE_OK);
369 1.1 christos }
370 1.1 christos
371 1.1 christos
372 1.1 christos /*******************************************************************************
373 1.1 christos *
374 1.1 christos * FUNCTION: AcpiExConcatTemplate
375 1.1 christos *
376 1.1 christos * PARAMETERS: Operand0 - First source object
377 1.1 christos * Operand1 - Second source object
378 1.1 christos * ActualReturnDesc - Where to place the return object
379 1.1 christos * WalkState - Current walk state
380 1.1 christos *
381 1.1 christos * RETURN: Status
382 1.1 christos *
383 1.1 christos * DESCRIPTION: Concatenate two resource templates
384 1.1 christos *
385 1.1 christos ******************************************************************************/
386 1.1 christos
387 1.1 christos ACPI_STATUS
388 1.1 christos AcpiExConcatTemplate (
389 1.1 christos ACPI_OPERAND_OBJECT *Operand0,
390 1.1 christos ACPI_OPERAND_OBJECT *Operand1,
391 1.1 christos ACPI_OPERAND_OBJECT **ActualReturnDesc,
392 1.1 christos ACPI_WALK_STATE *WalkState)
393 1.1 christos {
394 1.1 christos ACPI_STATUS Status;
395 1.1 christos ACPI_OPERAND_OBJECT *ReturnDesc;
396 1.1 christos UINT8 *NewBuf;
397 1.1 christos UINT8 *EndTag;
398 1.1 christos ACPI_SIZE Length0;
399 1.1 christos ACPI_SIZE Length1;
400 1.1 christos ACPI_SIZE NewLength;
401 1.1 christos
402 1.1 christos
403 1.1 christos ACPI_FUNCTION_TRACE (ExConcatTemplate);
404 1.1 christos
405 1.1 christos
406 1.1 christos /*
407 1.1 christos * Find the EndTag descriptor in each resource template.
408 1.1 christos * Note1: returned pointers point TO the EndTag, not past it.
409 1.1 christos * Note2: zero-length buffers are allowed; treated like one EndTag
410 1.1 christos */
411 1.1 christos
412 1.1 christos /* Get the length of the first resource template */
413 1.1 christos
414 1.1 christos Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);
415 1.1 christos if (ACPI_FAILURE (Status))
416 1.1 christos {
417 1.1 christos return_ACPI_STATUS (Status);
418 1.1 christos }
419 1.1 christos
420 1.1 christos Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);
421 1.1 christos
422 1.1 christos /* Get the length of the second resource template */
423 1.1 christos
424 1.1 christos Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);
425 1.1 christos if (ACPI_FAILURE (Status))
426 1.1 christos {
427 1.1 christos return_ACPI_STATUS (Status);
428 1.1 christos }
429 1.1 christos
430 1.1 christos Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);
431 1.1 christos
432 1.1 christos /* Combine both lengths, minimum size will be 2 for EndTag */
433 1.1 christos
434 1.1 christos NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);
435 1.1 christos
436 1.1 christos /* Create a new buffer object for the result (with one EndTag) */
437 1.1 christos
438 1.1 christos ReturnDesc = AcpiUtCreateBufferObject (NewLength);
439 1.1 christos if (!ReturnDesc)
440 1.1 christos {
441 1.1 christos return_ACPI_STATUS (AE_NO_MEMORY);
442 1.1 christos }
443 1.1 christos
444 1.1 christos /*
445 1.1 christos * Copy the templates to the new buffer, 0 first, then 1 follows. One
446 1.1 christos * EndTag descriptor is copied from Operand1.
447 1.1 christos */
448 1.1 christos NewBuf = ReturnDesc->Buffer.Pointer;
449 1.1 christos memcpy (NewBuf, Operand0->Buffer.Pointer, Length0);
450 1.1 christos memcpy (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);
451 1.1 christos
452 1.1 christos /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
453 1.1 christos
454 1.1 christos NewBuf[NewLength - 1] = 0;
455 1.1 christos NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
456 1.1 christos
457 1.1 christos /* Return the completed resource template */
458 1.1 christos
459 1.1 christos *ActualReturnDesc = ReturnDesc;
460 1.1 christos return_ACPI_STATUS (AE_OK);
461 1.1 christos }
462