1 /******************************************************************************
3 * Module Name: dtfield.c - Code generation for individual source fields
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 *****************************************************************************/
104 case DT_FIELD_TYPE_INTEGER:
106 DtCompileInteger (Buffer, Field, ByteLength, Flags);
109 case DT_FIELD_TYPE_STRING:
111 DtCompileString (Buffer, Field, ByteLength);
114 case DT_FIELD_TYPE_UUID:
116 Status = DtCompileUuid (Buffer, Field, ByteLength);
117 if (ACPI_SUCCESS (Status))
124 case DT_FIELD_TYPE_BUFFER:
126 DtCompileBuffer (Buffer, Field->Value, Field, ByteLength);
129 case DT_FIELD_TYPE_UNICODE:
131 DtCompileUnicode (Buffer, Field, ByteLength);
134 case DT_FIELD_TYPE_DEVICE_PATH:
140 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid field type");
146 /******************************************************************************
148 * FUNCTION: DtCompileString
150 * PARAMETERS: Buffer - Output buffer
151 * Field - String to be copied to buffer
152 * ByteLength - Maximum length of string
156 * DESCRIPTION: Copy string to the buffer
158 *****************************************************************************/
169 Length = strlen (Field->Value);
171 /* Check if the string is too long for the field */
173 if (Length > ByteLength)
175 sprintf (MsgBuffer, "Maximum %u characters", ByteLength);
176 DtError (ASL_ERROR, ASL_MSG_STRING_LENGTH, Field, MsgBuffer);
180 memcpy (Buffer, Field->Value, Length);
184 /******************************************************************************
186 * FUNCTION: DtCompileUnicode
188 * PARAMETERS: Buffer - Output buffer
189 * Field - String to be copied to buffer
190 * ByteLength - Maximum length of string
194 * DESCRIPTION: Convert ASCII string to Unicode string
196 * Note: The Unicode string is 16 bits per character, no leading signature,
197 * with a 16-bit terminating NULL.
199 *****************************************************************************/
210 UINT16 *UnicodeString;
213 AsciiString = Field->Value;
214 UnicodeString = (UINT16 *) Buffer;
215 Count = strlen (AsciiString) + 1;
217 /* Convert to Unicode string (including null terminator) */
219 for (i = 0; i < Count; i++)
221 UnicodeString[i] = (UINT16) AsciiString[i];
226 /*******************************************************************************
228 * FUNCTION: DtCompileUuid
230 * PARAMETERS: Buffer - Output buffer
231 * Field - String to be copied to buffer
232 * ByteLength - Maximum length of string
236 * DESCRIPTION: Convert UUID string to 16-byte buffer
238 ******************************************************************************/
250 InString = Field->Value;
252 Status = AuValidateUuid (InString);
253 if (ACPI_FAILURE (Status))
255 sprintf (MsgBuffer, "%s", Field->Value);
256 DtNameError (ASL_ERROR, ASL_MSG_INVALID_UUID, Field, MsgBuffer);
260 AcpiUtConvertStringToUuid (InString, Buffer);
267 /******************************************************************************
269 * FUNCTION: DtCompileInteger
271 * PARAMETERS: Buffer - Output buffer
272 * Field - Field obj with Integer to be compiled
273 * ByteLength - Byte length of the integer
274 * Flags - Additional compile info
278 * DESCRIPTION: Compile an integer. Supports integer expressions with C-style
281 *****************************************************************************/
295 /* Output buffer byte length must be in range 1-8 */
297 if ((ByteLength > 8) || (ByteLength == 0))
299 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field,
300 "Invalid internal Byte length");
304 /* Resolve integer expression to a single integer value */
306 Status = DtResolveIntegerExpression (Field, &Value);
307 if (ACPI_FAILURE (Status))
313 * Ensure that reserved fields are set properly. Note: uses
314 * the DT_NON_ZERO flag to indicate that the reserved value
315 * must be exactly one. Otherwise, the value must be zero.
316 * This is sufficient for now.
319 /* TBD: Should use a flag rather than compare "Reserved" */
321 if (!strcmp (Field->Name, "Reserved"))
323 if (Flags & DT_NON_ZERO)
327 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
328 "Must be one, setting to one");
334 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
335 "Must be zero, setting to zero");
340 /* Check if the value must be non-zero */
342 else if ((Flags & DT_NON_ZERO) && (Value == 0))
344 DtError (ASL_ERROR, ASL_MSG_ZERO_VALUE, Field, NULL);
348 * Generate the maximum value for the data type (ByteLength)
349 * Note: construct chosen for maximum portability
351 MaxValue = ((UINT64) (-1)) >> (64 - (ByteLength * 8));
353 /* Validate that the input value is within range of the target */
355 if (Value > MaxValue)
357 sprintf (MsgBuffer, "%8.8X%8.8X - max %u bytes",
358 ACPI_FORMAT_UINT64 (Value), ByteLength);
359 DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, MsgBuffer);
362 memcpy (Buffer, &Value, ByteLength);
367 /******************************************************************************
369 * FUNCTION: DtNormalizeBuffer
371 * PARAMETERS: Buffer - Input buffer
372 * Count - Output the count of hex number in
375 * RETURN: The normalized buffer, freed by caller
377 * DESCRIPTION: [1A,2B,3C,4D] or 1A, 2B, 3C, 4D will be normalized
380 *****************************************************************************/
389 UINT32 BufferCount = 0;
390 BOOLEAN Separator = TRUE;
394 NewBuffer = UtLocalCalloc (strlen (Buffer) + 1);
395 TmpBuffer = NewBuffer;
397 while ((c = *Buffer++))
401 /* Valid separators */
415 /* Insert blank as the standard separator */
431 *Count = BufferCount + 1;
436 /******************************************************************************
438 * FUNCTION: DtCompileBuffer
440 * PARAMETERS: Buffer - Output buffer
441 * StringValue - Integer list to be compiled
442 * Field - Current field object
443 * ByteLength - Byte length of the integer list
445 * RETURN: Count of remaining data in the input list
447 * DESCRIPTION: Compile and pack an integer list, for example
448 * "AA 1F 20 3B" ==> Buffer[] = {0xAA,0x1F,0x20,0x3B}
450 *****************************************************************************/
466 /* Allow several different types of value separators */
468 StringValue = DtNormalizeBuffer (StringValue, &Count);
471 for (i = 0; i < Count; i++)
473 /* Each element of StringValue is three chars */
475 Hex[0] = StringValue[(3 * i)];
476 Hex[1] = StringValue[(3 * i) + 1];
478 /* Convert one hex byte */
481 Status = DtStrtoul64 (Hex, &Value);
482 if (ACPI_FAILURE (Status))
484 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, MsgBuffer);
488 Buffer[i] = (UINT8) Value;
492 ACPI_FREE (StringValue);
493 return (ByteLength - Count);
497 /******************************************************************************
499 * FUNCTION: DtCompileFlag
501 * PARAMETERS: Buffer - Output buffer
502 * Field - Field to be compiled
507 * DESCRIPTION: Compile a flag
509 *****************************************************************************/
515 ACPI_DMTABLE_INFO *Info)
518 UINT32 BitLength = 1;
519 UINT8 BitPosition = 0;
523 Status = DtStrtoul64 (Field->Value, &Value);
524 if (ACPI_FAILURE (Status))
526 DtError (ASL_ERROR, ASL_MSG_INVALID_HEX_INTEGER, Field, NULL);
529 switch (Info->Opcode)
540 BitPosition = Info->Opcode;
544 case ACPI_DMT_FLAGS0:
551 case ACPI_DMT_FLAGS1:
558 case ACPI_DMT_FLAGS2:
564 case ACPI_DMT_FLAGS4:
572 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid flag opcode");
576 /* Check range of the input flag value */
578 if (Value >= ((UINT64) 1 << BitLength))
580 sprintf (MsgBuffer, "Maximum %u bit", BitLength);
581 DtError (ASL_ERROR, ASL_MSG_FLAG_VALUE, Field, MsgBuffer);
585 *Buffer |= (UINT8) (Value << BitPosition);