1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include <contrib/dev/acpica/compiler/dtcompiler.h>
48 #include <contrib/dev/acpica/include/actables.h>
50 #define _COMPONENT DT_COMPILER
51 ACPI_MODULE_NAME ("dtutils")
53 /* Local prototypes */
57 DT_SUBTABLE *Subtable,
62 /******************************************************************************
66 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
67 * MessageId - Index into global message buffer
68 * Op - Parse node where error happened
69 * ExtraMessage - additional error message
73 * DESCRIPTION: Common error interface for data table compiler
75 *****************************************************************************/
81 DT_FIELD *FieldObject,
85 /* Check if user wants to ignore this exception */
87 if (AslIsExceptionDisabled (Level, MessageId))
94 AslCommonError (Level, MessageId,
97 FieldObject->ByteOffset,
99 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
103 AslCommonError (Level, MessageId, 0,
104 0, 0, 0, 0, ExtraMessage);
109 /******************************************************************************
111 * FUNCTION: DtNameError
113 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
114 * MessageId - Index into global message buffer
115 * Op - Parse node where error happened
116 * ExtraMessage - additional error message
120 * DESCRIPTION: Error interface for named objects
122 *****************************************************************************/
128 DT_FIELD *FieldObject,
137 if (Gbl_WarningLevel < Level)
150 AslCommonError (Level, MessageId,
153 FieldObject->ByteOffset,
154 FieldObject->NameColumn,
155 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
159 AslCommonError (Level, MessageId, 0,
160 0, 0, 0, 0, ExtraMessage);
165 /*******************************************************************************
173 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
174 * compile or I/O errors
176 ******************************************************************************/
181 DT_FIELD *FieldObject,
185 DtError (ASL_ERROR, MessageId, FieldObject, ExtraMessage);
188 * TBD: remove this entire function, DtFatal
190 * We cannot abort the compiler on error, because we may be compiling a
191 * list of files. We must move on to the next file.
200 /******************************************************************************
202 * FUNCTION: DtStrtoul64
204 * PARAMETERS: String - Null terminated string
205 * ReturnInteger - Where the converted integer is returned
209 * DESCRIPTION: Simple conversion of a string hex integer constant to unsigned
210 * value. Assumes no leading "0x" for the constant.
212 * Portability note: The reason this function exists is because a 64-bit
213 * sscanf is not available in all environments.
215 *****************************************************************************/
220 UINT64 *ReturnInteger)
222 char *ThisChar = String;
224 UINT64 ReturnValue = 0;
228 /* Skip over any white space in the buffer */
230 while ((*ThisChar == ' ') || (*ThisChar == '\t'))
235 /* Skip leading zeros */
237 while ((*ThisChar) == '0')
242 /* Convert character-by-character */
246 if (ACPI_IS_DIGIT (*ThisChar))
248 /* Convert ASCII 0-9 to Decimal value */
250 ThisDigit = ((UINT8) *ThisChar) - '0';
254 ThisDigit = (UINT32) ACPI_TOUPPER (*ThisChar);
255 if (!ACPI_IS_XDIGIT ((char) ThisDigit))
259 return (AE_BAD_CHARACTER);
262 /* Convert ASCII Hex char (A-F) to value */
264 ThisDigit = (ThisDigit - 'A') + 10;
267 /* Insert the 4-bit hex digit */
270 ReturnValue += ThisDigit;
276 /* Value is too large (> 64 bits/8 bytes/16 hex digits) */
282 *ReturnInteger = ReturnValue;
287 /******************************************************************************
289 * FUNCTION: DtGetFileSize
291 * PARAMETERS: Handle - Open file handler
293 * RETURN: Current file size
295 * DESCRIPTION: Get the current size of a file. Seek to the EOF and get the
296 * offset. Seek back to the original location.
298 *****************************************************************************/
308 CurrentOffset = ftell (Handle);
309 fseek (Handle, 0, SEEK_END);
310 LastOffset = ftell (Handle);
311 fseek (Handle, CurrentOffset, SEEK_SET);
313 return ((UINT32) LastOffset);
317 /******************************************************************************
319 * FUNCTION: DtGetFieldValue
321 * PARAMETERS: Field - Current field list pointer
323 * RETURN: Field value
325 * DESCRIPTION: Get field value
327 *****************************************************************************/
338 return (Field->Value);
342 /******************************************************************************
344 * FUNCTION: DtGetFieldType
346 * PARAMETERS: Info - Data table info
350 * DESCRIPTION: Get field type
352 *****************************************************************************/
356 ACPI_DMTABLE_INFO *Info)
361 /* DT_FLAG means that this is the start of a block of flag bits */
362 /* TBD - we can make these a separate opcode later */
364 if (Info->Flags & DT_FLAG)
366 return (DT_FIELD_TYPE_FLAGS_INTEGER);
369 /* Type is based upon the opcode for this field in the info table */
371 switch (Info->Opcode)
381 case ACPI_DMT_FLAGS0:
382 case ACPI_DMT_FLAGS1:
383 case ACPI_DMT_FLAGS2:
384 case ACPI_DMT_FLAGS4:
386 Type = DT_FIELD_TYPE_FLAG;
393 case ACPI_DMT_STRING:
395 Type = DT_FIELD_TYPE_STRING;
398 case ACPI_DMT_BUFFER:
402 case ACPI_DMT_BUF128:
403 case ACPI_DMT_PCI_PATH:
405 Type = DT_FIELD_TYPE_BUFFER;
409 case ACPI_DMT_HESTNTFY:
411 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
414 case ACPI_DMT_UNICODE:
416 Type = DT_FIELD_TYPE_UNICODE;
421 Type = DT_FIELD_TYPE_UUID;
424 case ACPI_DMT_DEVICE_PATH:
426 Type = DT_FIELD_TYPE_DEVICE_PATH;
431 Type = DT_FIELD_TYPE_LABEL;
436 Type = DT_FIELD_TYPE_INTEGER;
444 /******************************************************************************
446 * FUNCTION: DtGetBufferLength
448 * PARAMETERS: Buffer - List of integers,
449 * for example "10 3A 4F 2E"
451 * RETURN: Count of integer
453 * DESCRIPTION: Get length of bytes needed to store the integers
455 *****************************************************************************/
461 UINT32 ByteLength = 0;
470 while (*Buffer == ' ')
479 return (++ByteLength);
483 /******************************************************************************
485 * FUNCTION: DtGetFieldLength
487 * PARAMETERS: Field - Current field
488 * Info - Data table info
490 * RETURN: Field length
492 * DESCRIPTION: Get length of bytes needed to compile the field
494 * Note: This function must remain in sync with AcpiDmDumpTable.
496 *****************************************************************************/
501 ACPI_DMTABLE_INFO *Info)
503 UINT32 ByteLength = 0;
507 /* Length is based upon the opcode for this field in the info table */
509 switch (Info->Opcode)
519 case ACPI_DMT_FLAGS0:
520 case ACPI_DMT_FLAGS1:
521 case ACPI_DMT_FLAGS2:
522 case ACPI_DMT_FLAGS4:
524 case ACPI_DMT_EXTRA_TEXT:
530 case ACPI_DMT_CHKSUM:
531 case ACPI_DMT_SPACEID:
532 case ACPI_DMT_ACCWIDTH:
538 case ACPI_DMT_HESTNTYP:
539 case ACPI_DMT_FADTPM:
540 case ACPI_DMT_EINJACT:
541 case ACPI_DMT_EINJINST:
542 case ACPI_DMT_ERSTACT:
543 case ACPI_DMT_ERSTINST:
548 case ACPI_DMT_UINT16:
551 case ACPI_DMT_PCI_PATH:
556 case ACPI_DMT_UINT24:
561 case ACPI_DMT_UINT32:
569 case ACPI_DMT_UINT40:
574 case ACPI_DMT_UINT48:
580 case ACPI_DMT_UINT56:
586 case ACPI_DMT_UINT64:
592 case ACPI_DMT_STRING:
594 Value = DtGetFieldValue (Field);
597 ByteLength = ACPI_STRLEN (Value) + 1;
600 { /* At this point, this is a fatal error */
602 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
603 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
610 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
613 case ACPI_DMT_HESTNTFY:
615 ByteLength = sizeof (ACPI_HEST_NOTIFY);
618 case ACPI_DMT_BUFFER:
620 Value = DtGetFieldValue (Field);
623 ByteLength = DtGetBufferLength (Value);
626 { /* At this point, this is a fatal error */
628 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
629 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
645 case ACPI_DMT_BUF128:
650 case ACPI_DMT_UNICODE:
652 Value = DtGetFieldValue (Field);
654 /* TBD: error if Value is NULL? (as below?) */
656 ByteLength = (ACPI_STRLEN (Value) + 1) * sizeof(UINT16);
661 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
669 /******************************************************************************
673 * PARAMETERS: DT_WALK_CALLBACK:
674 * Subtable - Subtable
676 * ReturnValue - Store the checksum of subtable
680 * DESCRIPTION: Get the checksum of subtable
682 *****************************************************************************/
686 DT_SUBTABLE *Subtable,
691 UINT8 *Sum = ReturnValue;
694 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
695 *Sum = (UINT8) (*Sum + Checksum);
699 /******************************************************************************
701 * FUNCTION: DtSetTableChecksum
703 * PARAMETERS: ChecksumPointer - Where to return the checksum
707 * DESCRIPTION: Set checksum of the whole data table into the checksum field
709 *****************************************************************************/
713 UINT8 *ChecksumPointer)
719 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
721 OldSum = *ChecksumPointer;
722 Checksum = (UINT8) (Checksum - OldSum);
724 /* Compute the final checksum */
726 Checksum = (UINT8) (0 - Checksum);
727 *ChecksumPointer = Checksum;
731 /******************************************************************************
733 * FUNCTION: DtSetTableLength
739 * DESCRIPTION: Walk the subtables and set all the length fields
741 *****************************************************************************/
747 DT_SUBTABLE *ParentTable;
748 DT_SUBTABLE *ChildTable;
751 ParentTable = Gbl_RootTable;
759 DtSetSubtableLength (ParentTable);
763 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
766 if (ChildTable->LengthField)
768 DtSetSubtableLength (ChildTable);
771 if (ChildTable->Child)
773 ParentTable = ChildTable;
778 ParentTable->TotalLength += ChildTable->TotalLength;
779 if (ParentTable->LengthField)
781 DtSetSubtableLength (ParentTable);
787 ChildTable = ParentTable;
789 if (ChildTable == Gbl_RootTable)
794 ParentTable = DtGetParentSubtable (ParentTable);
796 ParentTable->TotalLength += ChildTable->TotalLength;
797 if (ParentTable->LengthField)
799 DtSetSubtableLength (ParentTable);
806 /******************************************************************************
808 * FUNCTION: DtWalkTableTree
810 * PARAMETERS: StartTable - Subtable in the tree where walking begins
811 * UserFunction - Called during the walk
812 * Context - Passed to user function
813 * ReturnValue - The return value of UserFunction
817 * DESCRIPTION: Performs a depth-first walk of the subtable tree
819 *****************************************************************************/
823 DT_SUBTABLE *StartTable,
824 DT_WALK_CALLBACK UserFunction,
828 DT_SUBTABLE *ParentTable;
829 DT_SUBTABLE *ChildTable;
832 ParentTable = StartTable;
840 UserFunction (ParentTable, Context, ReturnValue);
844 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
847 UserFunction (ChildTable, Context, ReturnValue);
849 if (ChildTable->Child)
851 ParentTable = ChildTable;
857 ChildTable = ParentTable;
858 if (ChildTable == Gbl_RootTable)
863 ParentTable = DtGetParentSubtable (ParentTable);
865 if (ChildTable->Peer == StartTable)
874 /******************************************************************************
876 * FUNCTION: DtFreeFieldList
882 * DESCRIPTION: Free the field list
884 *****************************************************************************/
890 DT_FIELD *Field = Gbl_FieldList;
894 /* Walk and free entire field list */
898 NextField = Field->Next; /* Save link */
900 if (!(Field->Flags & DT_FIELD_NOT_ALLOCATED))
902 ACPI_FREE (Field->Name);
903 ACPI_FREE (Field->Value);