1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
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 "aslcompiler.h"
45 #include "dtcompiler.h"
48 #define _COMPONENT DT_COMPILER
49 ACPI_MODULE_NAME ("dtutils")
51 /* Local prototypes */
55 DT_SUBTABLE *Subtable,
60 /******************************************************************************
64 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
65 * MessageId - Index into global message buffer
66 * Op - Parse node where error happened
67 * ExtraMessage - additional error message
71 * DESCRIPTION: Common error interface for data table compiler
73 *****************************************************************************/
79 DT_FIELD *FieldObject,
83 /* Check if user wants to ignore this exception */
85 if (AslIsExceptionDisabled (Level, MessageId))
92 AslCommonError (Level, MessageId,
95 FieldObject->ByteOffset,
97 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
101 AslCommonError (Level, MessageId, 0,
102 0, 0, 0, 0, ExtraMessage);
107 /******************************************************************************
109 * FUNCTION: DtNameError
111 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
112 * MessageId - Index into global message buffer
113 * Op - Parse node where error happened
114 * ExtraMessage - additional error message
118 * DESCRIPTION: Error interface for named objects
120 *****************************************************************************/
126 DT_FIELD *FieldObject,
135 if (Gbl_WarningLevel < Level)
148 AslCommonError (Level, MessageId,
151 FieldObject->ByteOffset,
152 FieldObject->NameColumn,
153 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
157 AslCommonError (Level, MessageId, 0,
158 0, 0, 0, 0, ExtraMessage);
163 /*******************************************************************************
171 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
172 * compile or I/O errors
174 ******************************************************************************/
179 DT_FIELD *FieldObject,
183 DtError (ASL_ERROR, MessageId, FieldObject, ExtraMessage);
186 * TBD: remove this entire function, DtFatal
188 * We cannot abort the compiler on error, because we may be compiling a
189 * list of files. We must move on to the next file.
198 /******************************************************************************
200 * FUNCTION: DtStrtoul64
202 * PARAMETERS: String - Null terminated string
203 * ReturnInteger - Where the converted integer is returned
207 * DESCRIPTION: Simple conversion of a string hex integer constant to unsigned
208 * value. Assumes no leading "0x" for the constant.
210 * Portability note: The reason this function exists is because a 64-bit
211 * sscanf is not available in all environments.
213 *****************************************************************************/
218 UINT64 *ReturnInteger)
220 char *ThisChar = String;
222 UINT64 ReturnValue = 0;
226 /* Skip over any white space in the buffer */
228 while ((*ThisChar == ' ') || (*ThisChar == '\t'))
233 /* Skip leading zeros */
235 while ((*ThisChar) == '0')
240 /* Convert character-by-character */
244 if (isdigit ((int) *ThisChar))
246 /* Convert ASCII 0-9 to Decimal value */
248 ThisDigit = ((UINT8) *ThisChar) - '0';
252 ThisDigit = (UINT32) toupper ((int) *ThisChar);
253 if (!isxdigit ((int) ThisDigit))
257 return (AE_BAD_CHARACTER);
260 /* Convert ASCII Hex char (A-F) to value */
262 ThisDigit = (ThisDigit - 'A') + 10;
265 /* Insert the 4-bit hex digit */
268 ReturnValue += ThisDigit;
274 /* Value is too large (> 64 bits/8 bytes/16 hex digits) */
280 *ReturnInteger = ReturnValue;
285 /******************************************************************************
287 * FUNCTION: DtGetFieldValue
289 * PARAMETERS: Field - Current field list pointer
291 * RETURN: Field value
293 * DESCRIPTION: Get field value
295 *****************************************************************************/
306 return (Field->Value);
310 /******************************************************************************
312 * FUNCTION: DtGetFieldType
314 * PARAMETERS: Info - Data table info
318 * DESCRIPTION: Get field type
320 *****************************************************************************/
324 ACPI_DMTABLE_INFO *Info)
329 /* DT_FLAG means that this is the start of a block of flag bits */
330 /* TBD - we can make these a separate opcode later */
332 if (Info->Flags & DT_FLAG)
334 return (DT_FIELD_TYPE_FLAGS_INTEGER);
337 /* Type is based upon the opcode for this field in the info table */
339 switch (Info->Opcode)
349 case ACPI_DMT_FLAGS0:
350 case ACPI_DMT_FLAGS1:
351 case ACPI_DMT_FLAGS2:
352 case ACPI_DMT_FLAGS4:
354 Type = DT_FIELD_TYPE_FLAG;
361 case ACPI_DMT_STRING:
363 Type = DT_FIELD_TYPE_STRING;
366 case ACPI_DMT_BUFFER:
367 case ACPI_DMT_RAW_BUFFER:
372 case ACPI_DMT_BUF128:
373 case ACPI_DMT_PCI_PATH:
375 Type = DT_FIELD_TYPE_BUFFER;
379 case ACPI_DMT_HESTNTFY:
380 case ACPI_DMT_IORTMEM:
382 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
385 case ACPI_DMT_UNICODE:
387 Type = DT_FIELD_TYPE_UNICODE;
392 Type = DT_FIELD_TYPE_UUID;
395 case ACPI_DMT_DEVICE_PATH:
397 Type = DT_FIELD_TYPE_DEVICE_PATH;
402 Type = DT_FIELD_TYPE_LABEL;
407 Type = DT_FIELD_TYPE_INTEGER;
415 /******************************************************************************
417 * FUNCTION: DtGetBufferLength
419 * PARAMETERS: Buffer - List of integers,
420 * for example "10 3A 4F 2E"
422 * RETURN: Count of integer
424 * DESCRIPTION: Get length of bytes needed to store the integers
426 *****************************************************************************/
432 UINT32 ByteLength = 0;
441 while (*Buffer == ' ')
450 return (++ByteLength);
454 /******************************************************************************
456 * FUNCTION: DtGetFieldLength
458 * PARAMETERS: Field - Current field
459 * Info - Data table info
461 * RETURN: Field length
463 * DESCRIPTION: Get length of bytes needed to compile the field
465 * Note: This function must remain in sync with AcpiDmDumpTable.
467 *****************************************************************************/
472 ACPI_DMTABLE_INFO *Info)
474 UINT32 ByteLength = 0;
478 /* Length is based upon the opcode for this field in the info table */
480 switch (Info->Opcode)
490 case ACPI_DMT_FLAGS0:
491 case ACPI_DMT_FLAGS1:
492 case ACPI_DMT_FLAGS2:
493 case ACPI_DMT_FLAGS4:
495 case ACPI_DMT_EXTRA_TEXT:
501 case ACPI_DMT_CHKSUM:
502 case ACPI_DMT_SPACEID:
503 case ACPI_DMT_ACCWIDTH:
511 case ACPI_DMT_HESTNTYP:
512 case ACPI_DMT_FADTPM:
513 case ACPI_DMT_EINJACT:
514 case ACPI_DMT_EINJINST:
515 case ACPI_DMT_ERSTACT:
516 case ACPI_DMT_ERSTINST:
517 case ACPI_DMT_DMAR_SCOPE:
522 case ACPI_DMT_UINT16:
526 case ACPI_DMT_PCI_PATH:
531 case ACPI_DMT_UINT24:
536 case ACPI_DMT_UINT32:
544 case ACPI_DMT_UINT40:
549 case ACPI_DMT_UINT48:
555 case ACPI_DMT_UINT56:
561 case ACPI_DMT_UINT64:
567 case ACPI_DMT_STRING:
569 Value = DtGetFieldValue (Field);
572 ByteLength = strlen (Value) + 1;
575 { /* At this point, this is a fatal error */
577 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
578 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
585 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
588 case ACPI_DMT_HESTNTFY:
590 ByteLength = sizeof (ACPI_HEST_NOTIFY);
593 case ACPI_DMT_IORTMEM:
595 ByteLength = sizeof (ACPI_IORT_MEMORY_ACCESS);
598 case ACPI_DMT_BUFFER:
599 case ACPI_DMT_RAW_BUFFER:
601 Value = DtGetFieldValue (Field);
604 ByteLength = DtGetBufferLength (Value);
607 { /* At this point, this is a fatal error */
609 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
610 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
631 case ACPI_DMT_BUF128:
636 case ACPI_DMT_UNICODE:
638 Value = DtGetFieldValue (Field);
640 /* TBD: error if Value is NULL? (as below?) */
642 ByteLength = (strlen (Value) + 1) * sizeof(UINT16);
647 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
655 /******************************************************************************
659 * PARAMETERS: DT_WALK_CALLBACK:
660 * Subtable - Subtable
662 * ReturnValue - Store the checksum of subtable
666 * DESCRIPTION: Get the checksum of subtable
668 *****************************************************************************/
672 DT_SUBTABLE *Subtable,
677 UINT8 *Sum = ReturnValue;
680 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
681 *Sum = (UINT8) (*Sum + Checksum);
685 /******************************************************************************
687 * FUNCTION: DtSetTableChecksum
689 * PARAMETERS: ChecksumPointer - Where to return the checksum
693 * DESCRIPTION: Set checksum of the whole data table into the checksum field
695 *****************************************************************************/
699 UINT8 *ChecksumPointer)
705 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
707 OldSum = *ChecksumPointer;
708 Checksum = (UINT8) (Checksum - OldSum);
710 /* Compute the final checksum */
712 Checksum = (UINT8) (0 - Checksum);
713 *ChecksumPointer = Checksum;
717 /******************************************************************************
719 * FUNCTION: DtSetTableLength
725 * DESCRIPTION: Walk the subtables and set all the length fields
727 *****************************************************************************/
733 DT_SUBTABLE *ParentTable;
734 DT_SUBTABLE *ChildTable;
737 ParentTable = Gbl_RootTable;
745 DtSetSubtableLength (ParentTable);
749 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
752 if (ChildTable->LengthField)
754 DtSetSubtableLength (ChildTable);
757 if (ChildTable->Child)
759 ParentTable = ChildTable;
764 ParentTable->TotalLength += ChildTable->TotalLength;
765 if (ParentTable->LengthField)
767 DtSetSubtableLength (ParentTable);
773 ChildTable = ParentTable;
775 if (ChildTable == Gbl_RootTable)
780 ParentTable = DtGetParentSubtable (ParentTable);
782 ParentTable->TotalLength += ChildTable->TotalLength;
783 if (ParentTable->LengthField)
785 DtSetSubtableLength (ParentTable);
792 /******************************************************************************
794 * FUNCTION: DtWalkTableTree
796 * PARAMETERS: StartTable - Subtable in the tree where walking begins
797 * UserFunction - Called during the walk
798 * Context - Passed to user function
799 * ReturnValue - The return value of UserFunction
803 * DESCRIPTION: Performs a depth-first walk of the subtable tree
805 *****************************************************************************/
809 DT_SUBTABLE *StartTable,
810 DT_WALK_CALLBACK UserFunction,
814 DT_SUBTABLE *ParentTable;
815 DT_SUBTABLE *ChildTable;
818 ParentTable = StartTable;
826 UserFunction (ParentTable, Context, ReturnValue);
830 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
833 UserFunction (ChildTable, Context, ReturnValue);
835 if (ChildTable->Child)
837 ParentTable = ChildTable;
843 ChildTable = ParentTable;
844 if (ChildTable == Gbl_RootTable)
849 ParentTable = DtGetParentSubtable (ParentTable);
851 if (ChildTable->Peer == StartTable)
860 /*******************************************************************************
862 * FUNCTION: UtSubtableCacheCalloc
866 * RETURN: Pointer to the buffer. Aborts on allocation failure
868 * DESCRIPTION: Allocate a subtable object buffer. Bypass the local
869 * dynamic memory manager for performance reasons (This has a
870 * major impact on the speed of the compiler.)
872 ******************************************************************************/
875 UtSubtableCacheCalloc (
878 ASL_CACHE_INFO *Cache;
881 if (Gbl_SubtableCacheNext >= Gbl_SubtableCacheLast)
883 /* Allocate a new buffer */
885 Cache = UtLocalCalloc (sizeof (Cache->Next) +
886 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE));
888 /* Link new cache buffer to head of list */
890 Cache->Next = Gbl_SubtableCacheList;
891 Gbl_SubtableCacheList = Cache;
893 /* Setup cache management pointers */
895 Gbl_SubtableCacheNext = ACPI_CAST_PTR (DT_SUBTABLE, Cache->Buffer);
896 Gbl_SubtableCacheLast = Gbl_SubtableCacheNext + ASL_SUBTABLE_CACHE_SIZE;
900 return (Gbl_SubtableCacheNext++);
904 /*******************************************************************************
906 * FUNCTION: UtFieldCacheCalloc
910 * RETURN: Pointer to the buffer. Aborts on allocation failure
912 * DESCRIPTION: Allocate a field object buffer. Bypass the local
913 * dynamic memory manager for performance reasons (This has a
914 * major impact on the speed of the compiler.)
916 ******************************************************************************/
922 ASL_CACHE_INFO *Cache;
925 if (Gbl_FieldCacheNext >= Gbl_FieldCacheLast)
927 /* Allocate a new buffer */
929 Cache = UtLocalCalloc (sizeof (Cache->Next) +
930 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE));
932 /* Link new cache buffer to head of list */
934 Cache->Next = Gbl_FieldCacheList;
935 Gbl_FieldCacheList = Cache;
937 /* Setup cache management pointers */
939 Gbl_FieldCacheNext = ACPI_CAST_PTR (DT_FIELD, Cache->Buffer);
940 Gbl_FieldCacheLast = Gbl_FieldCacheNext + ASL_FIELD_CACHE_SIZE;
944 return (Gbl_FieldCacheNext++);
948 /*******************************************************************************
950 * FUNCTION: DtDeleteCaches
956 * DESCRIPTION: Delete all local cache buffer blocks
958 ******************************************************************************/
965 ASL_CACHE_INFO *Next;
971 while (Gbl_FieldCacheList)
973 Next = Gbl_FieldCacheList->Next;
974 ACPI_FREE (Gbl_FieldCacheList);
975 Gbl_FieldCacheList = Next;
979 DbgPrint (ASL_DEBUG_OUTPUT,
980 "%u Fields, Buffer size: %u fields (%u bytes), %u Buffers\n",
981 Gbl_FieldCount, ASL_FIELD_CACHE_SIZE,
982 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE), BufferCount);
985 Gbl_FieldCacheNext = NULL;
986 Gbl_FieldCacheLast = NULL;
991 while (Gbl_SubtableCacheList)
993 Next = Gbl_SubtableCacheList->Next;
994 ACPI_FREE (Gbl_SubtableCacheList);
995 Gbl_SubtableCacheList = Next;
999 DbgPrint (ASL_DEBUG_OUTPUT,
1000 "%u Subtables, Buffer size: %u subtables (%u bytes), %u Buffers\n",
1001 Gbl_SubtableCount, ASL_SUBTABLE_CACHE_SIZE,
1002 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE), BufferCount);
1004 Gbl_SubtableCount = 0;
1005 Gbl_SubtableCacheNext = NULL;
1006 Gbl_SubtableCacheLast = NULL;