1 /******************************************************************************
3 * Module Name: dtfield.c - Code generation for individual source fields
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 #include "aslcompiler.h"
47 #include "dtcompiler.h"
49 #define _COMPONENT DT_COMPILER
50 ACPI_MODULE_NAME ("dtfield")
53 /* Local prototypes */
79 /******************************************************************************
81 * FUNCTION: DtCompileOneField
83 * PARAMETERS: Buffer - Output buffer
84 * Field - Field to be compiled
85 * ByteLength - Byte length of the field
90 * DESCRIPTION: Compile a field value to binary
92 *****************************************************************************/
106 case DT_FIELD_TYPE_INTEGER:
108 DtCompileInteger (Buffer, Field, ByteLength, Flags);
111 case DT_FIELD_TYPE_STRING:
113 DtCompileString (Buffer, Field, ByteLength);
116 case DT_FIELD_TYPE_UUID:
118 Status = DtCompileUuid (Buffer, Field, ByteLength);
119 if (ACPI_SUCCESS (Status))
126 case DT_FIELD_TYPE_BUFFER:
128 DtCompileBuffer (Buffer, Field->Value, Field, ByteLength);
131 case DT_FIELD_TYPE_UNICODE:
133 DtCompileUnicode (Buffer, Field, ByteLength);
136 case DT_FIELD_TYPE_DEVICE_PATH:
142 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid field type");
148 /******************************************************************************
150 * FUNCTION: DtCompileString
152 * PARAMETERS: Buffer - Output buffer
153 * Field - String to be copied to buffer
154 * ByteLength - Maximum length of string
158 * DESCRIPTION: Copy string to the buffer
160 *****************************************************************************/
171 Length = ACPI_STRLEN (Field->Value);
173 /* Check if the string is too long for the field */
175 if (Length > ByteLength)
177 sprintf (MsgBuffer, "Maximum %u characters", ByteLength);
178 DtError (ASL_ERROR, ASL_MSG_STRING_LENGTH, Field, MsgBuffer);
182 ACPI_MEMCPY (Buffer, Field->Value, Length);
186 /******************************************************************************
188 * FUNCTION: DtCompileUnicode
190 * PARAMETERS: Buffer - Output buffer
191 * Field - String to be copied to buffer
192 * ByteLength - Maximum length of string
196 * DESCRIPTION: Convert ASCII string to Unicode string
198 * Note: The Unicode string is 16 bits per character, no leading signature,
199 * with a 16-bit terminating NULL.
201 *****************************************************************************/
212 UINT16 *UnicodeString;
215 AsciiString = Field->Value;
216 UnicodeString = (UINT16 *) Buffer;
217 Count = ACPI_STRLEN (AsciiString) + 1;
219 /* Convert to Unicode string (including null terminator) */
221 for (i = 0; i < Count; i++)
223 UnicodeString[i] = (UINT16) AsciiString[i];
228 /*******************************************************************************
230 * FUNCTION: DtCompileUuid
232 * PARAMETERS: Buffer - Output buffer
233 * Field - String to be copied to buffer
234 * ByteLength - Maximum length of string
238 * DESCRIPTION: Convert UUID string to 16-byte buffer
240 ******************************************************************************/
252 InString = Field->Value;
254 Status = AuValidateUuid (InString);
255 if (ACPI_FAILURE (Status))
257 sprintf (MsgBuffer, "%s", Field->Value);
258 DtNameError (ASL_ERROR, ASL_MSG_INVALID_UUID, Field, MsgBuffer);
262 AcpiUtConvertStringToUuid (InString, Buffer);
269 /******************************************************************************
271 * FUNCTION: DtCompileInteger
273 * PARAMETERS: Buffer - Output buffer
274 * Field - Field obj with Integer to be compiled
275 * ByteLength - Byte length of the integer
276 * Flags - Additional compile info
280 * DESCRIPTION: Compile an integer. Supports integer expressions with C-style
283 *****************************************************************************/
297 /* Output buffer byte length must be in range 1-8 */
299 if ((ByteLength > 8) || (ByteLength == 0))
301 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field,
302 "Invalid internal Byte length");
306 /* Resolve integer expression to a single integer value */
308 Status = DtResolveIntegerExpression (Field, &Value);
309 if (ACPI_FAILURE (Status))
315 * Ensure that reserved fields are set properly. Note: uses
316 * the DT_NON_ZERO flag to indicate that the reserved value
317 * must be exactly one. Otherwise, the value must be zero.
318 * This is sufficient for now.
321 /* TBD: Should use a flag rather than compare "Reserved" */
323 if (!ACPI_STRCMP (Field->Name, "Reserved"))
325 if (Flags & DT_NON_ZERO)
329 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
330 "Must be one, setting to one");
336 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
337 "Must be zero, setting to zero");
342 /* Check if the value must be non-zero */
344 else if ((Flags & DT_NON_ZERO) && (Value == 0))
346 DtError (ASL_ERROR, ASL_MSG_ZERO_VALUE, Field, NULL);
350 * Generate the maximum value for the data type (ByteLength)
351 * Note: construct chosen for maximum portability
353 MaxValue = ((UINT64) (-1)) >> (64 - (ByteLength * 8));
355 /* Validate that the input value is within range of the target */
357 if (Value > MaxValue)
359 sprintf (MsgBuffer, "%8.8X%8.8X - max %u bytes",
360 ACPI_FORMAT_UINT64 (Value), ByteLength);
361 DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, MsgBuffer);
364 ACPI_MEMCPY (Buffer, &Value, ByteLength);
369 /******************************************************************************
371 * FUNCTION: DtNormalizeBuffer
373 * PARAMETERS: Buffer - Input buffer
374 * Count - Output the count of hex number in
377 * RETURN: The normalized buffer, freed by caller
379 * DESCRIPTION: [1A,2B,3C,4D] or 1A, 2B, 3C, 4D will be normalized
382 *****************************************************************************/
391 UINT32 BufferCount = 0;
392 BOOLEAN Separator = TRUE;
396 NewBuffer = UtLocalCalloc (ACPI_STRLEN (Buffer) + 1);
397 TmpBuffer = NewBuffer;
399 while ((c = *Buffer++))
403 /* Valid separators */
417 /* Insert blank as the standard separator */
433 *Count = BufferCount + 1;
438 /******************************************************************************
440 * FUNCTION: DtCompileBuffer
442 * PARAMETERS: Buffer - Output buffer
443 * StringValue - Integer list to be compiled
444 * Field - Current field object
445 * ByteLength - Byte length of the integer list
447 * RETURN: Count of remaining data in the input list
449 * DESCRIPTION: Compile and pack an integer list, for example
450 * "AA 1F 20 3B" ==> Buffer[] = {0xAA,0x1F,0x20,0x3B}
452 *****************************************************************************/
468 /* Allow several different types of value separators */
470 StringValue = DtNormalizeBuffer (StringValue, &Count);
473 for (i = 0; i < Count; i++)
475 /* Each element of StringValue is three chars */
477 Hex[0] = StringValue[(3 * i)];
478 Hex[1] = StringValue[(3 * i) + 1];
480 /* Convert one hex byte */
483 Status = DtStrtoul64 (Hex, &Value);
484 if (ACPI_FAILURE (Status))
486 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, MsgBuffer);
490 Buffer[i] = (UINT8) Value;
494 ACPI_FREE (StringValue);
495 return (ByteLength - Count);
499 /******************************************************************************
501 * FUNCTION: DtCompileFlag
503 * PARAMETERS: Buffer - Output buffer
504 * Field - Field to be compiled
509 * DESCRIPTION: Compile a flag
511 *****************************************************************************/
517 ACPI_DMTABLE_INFO *Info)
520 UINT32 BitLength = 1;
521 UINT8 BitPosition = 0;
525 Status = DtStrtoul64 (Field->Value, &Value);
526 if (ACPI_FAILURE (Status))
528 DtError (ASL_ERROR, ASL_MSG_INVALID_HEX_INTEGER, Field, NULL);
531 switch (Info->Opcode)
542 BitPosition = Info->Opcode;
546 case ACPI_DMT_FLAGS0:
553 case ACPI_DMT_FLAGS1:
560 case ACPI_DMT_FLAGS2:
566 case ACPI_DMT_FLAGS4:
574 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid flag opcode");
578 /* Check range of the input flag value */
580 if (Value >= ((UINT64) 1 << BitLength))
582 sprintf (MsgBuffer, "Maximum %u bit", BitLength);
583 DtError (ASL_ERROR, ASL_MSG_FLAG_VALUE, Field, MsgBuffer);
587 *Buffer |= (UINT8) (Value << BitPosition);