1 /******************************************************************************
3 * Module Name: dtfield.c - Code generation for individual source fields
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
47 #define _COMPONENT DT_COMPILER
48 ACPI_MODULE_NAME ("dtfield")
51 /* Local prototypes */
77 /******************************************************************************
79 * FUNCTION: DtCompileOneField
81 * PARAMETERS: Buffer - Output buffer
82 * Field - Field to be compiled
83 * ByteLength - Byte length of the field
88 * DESCRIPTION: Compile a field value to binary
90 *****************************************************************************/
105 case DT_FIELD_TYPE_INTEGER:
107 DtCompileInteger (Buffer, Field, ByteLength, Flags);
110 case DT_FIELD_TYPE_STRING:
112 DtCompileString (Buffer, Field, ByteLength);
115 case DT_FIELD_TYPE_UUID:
117 Status = DtCompileUuid (Buffer, Field, ByteLength);
118 if (ACPI_SUCCESS (Status))
125 case DT_FIELD_TYPE_BUFFER:
127 DtCompileBuffer (Buffer, Field->Value, Field, ByteLength);
130 case DT_FIELD_TYPE_UNICODE:
132 DtCompileUnicode (Buffer, Field, ByteLength);
135 case DT_FIELD_TYPE_DEVICE_PATH:
141 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid field type");
147 /******************************************************************************
149 * FUNCTION: DtCompileString
151 * PARAMETERS: Buffer - Output buffer
152 * Field - String to be copied to buffer
153 * ByteLength - Maximum length of string
157 * DESCRIPTION: Copy string to the buffer
159 *****************************************************************************/
170 Length = strlen (Field->Value);
172 /* Check if the string is too long for the field */
174 if (Length > ByteLength)
176 sprintf (MsgBuffer, "Maximum %u characters", ByteLength);
177 DtError (ASL_ERROR, ASL_MSG_STRING_LENGTH, Field, MsgBuffer);
181 memcpy (Buffer, Field->Value, Length);
185 /******************************************************************************
187 * FUNCTION: DtCompileUnicode
189 * PARAMETERS: Buffer - Output buffer
190 * Field - String to be copied to buffer
191 * ByteLength - Maximum length of string
195 * DESCRIPTION: Convert ASCII string to Unicode string
197 * Note: The Unicode string is 16 bits per character, no leading signature,
198 * with a 16-bit terminating NULL.
200 *****************************************************************************/
211 UINT16 *UnicodeString;
214 AsciiString = Field->Value;
215 UnicodeString = (UINT16 *) Buffer;
216 Count = strlen (AsciiString) + 1;
218 /* Convert to Unicode string (including null terminator) */
220 for (i = 0; i < Count; i++)
222 UnicodeString[i] = (UINT16) AsciiString[i];
227 /*******************************************************************************
229 * FUNCTION: DtCompileUuid
231 * PARAMETERS: Buffer - Output buffer
232 * Field - String to be copied to buffer
233 * ByteLength - Maximum length of string
237 * DESCRIPTION: Convert UUID string to 16-byte buffer
239 ******************************************************************************/
251 InString = Field->Value;
253 Status = AuValidateUuid (InString);
254 if (ACPI_FAILURE (Status))
256 sprintf (MsgBuffer, "%s", Field->Value);
257 DtNameError (ASL_ERROR, ASL_MSG_INVALID_UUID, Field, MsgBuffer);
261 AcpiUtConvertStringToUuid (InString, Buffer);
268 /******************************************************************************
270 * FUNCTION: DtCompileInteger
272 * PARAMETERS: Buffer - Output buffer
273 * Field - Field obj with Integer to be compiled
274 * ByteLength - Byte length of the integer
275 * Flags - Additional compile info
279 * DESCRIPTION: Compile an integer. Supports integer expressions with C-style
282 *****************************************************************************/
296 /* Output buffer byte length must be in range 1-8 */
298 if ((ByteLength > 8) || (ByteLength == 0))
300 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field,
301 "Invalid internal Byte length");
305 /* Resolve integer expression to a single integer value */
307 Status = DtResolveIntegerExpression (Field, &Value);
308 if (ACPI_FAILURE (Status))
314 * Ensure that reserved fields are set properly. Note: uses
315 * the DT_NON_ZERO flag to indicate that the reserved value
316 * must be exactly one. Otherwise, the value must be zero.
317 * This is sufficient for now.
320 /* TBD: Should use a flag rather than compare "Reserved" */
322 if (!strcmp (Field->Name, "Reserved"))
324 if (Flags & DT_NON_ZERO)
328 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
329 "Must be one, setting to one");
335 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
336 "Must be zero, setting to zero");
341 /* Check if the value must be non-zero */
343 else if ((Flags & DT_NON_ZERO) && (Value == 0))
345 DtError (ASL_ERROR, ASL_MSG_ZERO_VALUE, Field, NULL);
349 * Generate the maximum value for the data type (ByteLength)
350 * Note: construct chosen for maximum portability
352 MaxValue = ((UINT64) (-1)) >> (64 - (ByteLength * 8));
354 /* Validate that the input value is within range of the target */
356 if (Value > MaxValue)
358 sprintf (MsgBuffer, "%8.8X%8.8X - max %u bytes",
359 ACPI_FORMAT_UINT64 (Value), ByteLength);
360 DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, MsgBuffer);
363 memcpy (Buffer, &Value, ByteLength);
368 /******************************************************************************
370 * FUNCTION: DtNormalizeBuffer
372 * PARAMETERS: Buffer - Input buffer
373 * Count - Output the count of hex numbers in
376 * RETURN: The normalized buffer, must be freed by caller
378 * DESCRIPTION: [1A,2B,3C,4D] or 1A, 2B, 3C, 4D will be normalized
381 *****************************************************************************/
390 UINT32 BufferCount = 0;
391 BOOLEAN Separator = TRUE;
395 NewBuffer = UtLocalCalloc (strlen (Buffer) + 1);
396 TmpBuffer = NewBuffer;
398 while ((c = *Buffer++))
402 /* Valid separators */
416 /* Insert blank as the standard separator */
432 *Count = BufferCount + 1;
437 /******************************************************************************
439 * FUNCTION: DtCompileBuffer
441 * PARAMETERS: Buffer - Output buffer
442 * StringValue - Integer list to be compiled
443 * Field - Current field object
444 * ByteLength - Byte length of the integer list
446 * RETURN: Count of remaining data in the input list
448 * DESCRIPTION: Compile and pack an integer list, for example
449 * "AA 1F 20 3B" ==> Buffer[] = {0xAA,0x1F,0x20,0x3B}
451 *****************************************************************************/
466 /* Allow several different types of value separators */
468 StringValue = DtNormalizeBuffer (StringValue, &Count);
469 Substring = StringValue;
471 /* Each element of StringValue is now three chars (2 hex + 1 space) */
473 for (i = 0; i < Count; i++, Substring += 3)
475 /* Check for byte value too long */
477 if (*(&Substring[2]) &&
478 (*(&Substring[2]) != ' '))
480 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
484 /* Convert two ASCII characters to one hex byte */
486 Status = AcpiUtAsciiToHexByte (Substring, &Buffer[i]);
487 if (ACPI_FAILURE (Status))
489 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
495 ACPI_FREE (StringValue);
496 return (ByteLength - Count);
500 /******************************************************************************
502 * FUNCTION: DtCompileFlag
504 * PARAMETERS: Buffer - Output buffer
505 * Field - Field to be compiled
510 * DESCRIPTION: Compile a flag field. Handles flags up to 64 bits.
512 *****************************************************************************/
518 ACPI_DMTABLE_INFO *Info)
521 UINT32 BitLength = 1;
522 UINT8 BitPosition = 0;
526 Status = AcpiUtStrtoul64 (Field->Value,
527 (ACPI_STRTOUL_64BIT | ACPI_STRTOUL_BASE16), &Value);
528 if (ACPI_FAILURE (Status))
530 DtError (ASL_ERROR, ASL_MSG_INVALID_HEX_INTEGER, Field, NULL);
533 switch (Info->Opcode)
544 BitPosition = Info->Opcode;
548 case ACPI_DMT_FLAGS0:
555 case ACPI_DMT_FLAGS1:
562 case ACPI_DMT_FLAGS2:
568 case ACPI_DMT_FLAGS4:
576 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid flag opcode");
580 /* Check range of the input flag value */
582 if (Value >= ((UINT64) 1 << BitLength))
584 sprintf (MsgBuffer, "Maximum %u bit", BitLength);
585 DtError (ASL_ERROR, ASL_MSG_FLAG_VALUE, Field, MsgBuffer);
589 *Buffer |= (UINT8) (Value << BitPosition);