1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
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.
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 (ACPI_IS_DIGIT (*ThisChar))
246 /* Convert ASCII 0-9 to Decimal value */
248 ThisDigit = ((UINT8) *ThisChar) - '0';
252 ThisDigit = (UINT32) ACPI_TOUPPER (*ThisChar);
253 if (!ACPI_IS_XDIGIT ((char) 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:
370 case ACPI_DMT_BUF128:
371 case ACPI_DMT_PCI_PATH:
373 Type = DT_FIELD_TYPE_BUFFER;
377 case ACPI_DMT_HESTNTFY:
379 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
382 case ACPI_DMT_UNICODE:
384 Type = DT_FIELD_TYPE_UNICODE;
389 Type = DT_FIELD_TYPE_UUID;
392 case ACPI_DMT_DEVICE_PATH:
394 Type = DT_FIELD_TYPE_DEVICE_PATH;
399 Type = DT_FIELD_TYPE_LABEL;
404 Type = DT_FIELD_TYPE_INTEGER;
412 /******************************************************************************
414 * FUNCTION: DtGetBufferLength
416 * PARAMETERS: Buffer - List of integers,
417 * for example "10 3A 4F 2E"
419 * RETURN: Count of integer
421 * DESCRIPTION: Get length of bytes needed to store the integers
423 *****************************************************************************/
429 UINT32 ByteLength = 0;
438 while (*Buffer == ' ')
447 return (++ByteLength);
451 /******************************************************************************
453 * FUNCTION: DtGetFieldLength
455 * PARAMETERS: Field - Current field
456 * Info - Data table info
458 * RETURN: Field length
460 * DESCRIPTION: Get length of bytes needed to compile the field
462 * Note: This function must remain in sync with AcpiDmDumpTable.
464 *****************************************************************************/
469 ACPI_DMTABLE_INFO *Info)
471 UINT32 ByteLength = 0;
475 /* Length is based upon the opcode for this field in the info table */
477 switch (Info->Opcode)
487 case ACPI_DMT_FLAGS0:
488 case ACPI_DMT_FLAGS1:
489 case ACPI_DMT_FLAGS2:
490 case ACPI_DMT_FLAGS4:
492 case ACPI_DMT_EXTRA_TEXT:
498 case ACPI_DMT_CHKSUM:
499 case ACPI_DMT_SPACEID:
500 case ACPI_DMT_ACCWIDTH:
508 case ACPI_DMT_HESTNTYP:
509 case ACPI_DMT_FADTPM:
510 case ACPI_DMT_EINJACT:
511 case ACPI_DMT_EINJINST:
512 case ACPI_DMT_ERSTACT:
513 case ACPI_DMT_ERSTINST:
514 case ACPI_DMT_DMAR_SCOPE:
519 case ACPI_DMT_UINT16:
522 case ACPI_DMT_PCI_PATH:
527 case ACPI_DMT_UINT24:
532 case ACPI_DMT_UINT32:
541 case ACPI_DMT_UINT40:
546 case ACPI_DMT_UINT48:
552 case ACPI_DMT_UINT56:
558 case ACPI_DMT_UINT64:
564 case ACPI_DMT_STRING:
566 Value = DtGetFieldValue (Field);
569 ByteLength = ACPI_STRLEN (Value) + 1;
572 { /* At this point, this is a fatal error */
574 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
575 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
582 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
585 case ACPI_DMT_HESTNTFY:
587 ByteLength = sizeof (ACPI_HEST_NOTIFY);
590 case ACPI_DMT_BUFFER:
592 Value = DtGetFieldValue (Field);
595 ByteLength = DtGetBufferLength (Value);
598 { /* At this point, this is a fatal error */
600 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
601 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
617 case ACPI_DMT_BUF128:
622 case ACPI_DMT_UNICODE:
624 Value = DtGetFieldValue (Field);
626 /* TBD: error if Value is NULL? (as below?) */
628 ByteLength = (ACPI_STRLEN (Value) + 1) * sizeof(UINT16);
633 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
641 /******************************************************************************
645 * PARAMETERS: DT_WALK_CALLBACK:
646 * Subtable - Subtable
648 * ReturnValue - Store the checksum of subtable
652 * DESCRIPTION: Get the checksum of subtable
654 *****************************************************************************/
658 DT_SUBTABLE *Subtable,
663 UINT8 *Sum = ReturnValue;
666 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
667 *Sum = (UINT8) (*Sum + Checksum);
671 /******************************************************************************
673 * FUNCTION: DtSetTableChecksum
675 * PARAMETERS: ChecksumPointer - Where to return the checksum
679 * DESCRIPTION: Set checksum of the whole data table into the checksum field
681 *****************************************************************************/
685 UINT8 *ChecksumPointer)
691 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
693 OldSum = *ChecksumPointer;
694 Checksum = (UINT8) (Checksum - OldSum);
696 /* Compute the final checksum */
698 Checksum = (UINT8) (0 - Checksum);
699 *ChecksumPointer = Checksum;
703 /******************************************************************************
705 * FUNCTION: DtSetTableLength
711 * DESCRIPTION: Walk the subtables and set all the length fields
713 *****************************************************************************/
719 DT_SUBTABLE *ParentTable;
720 DT_SUBTABLE *ChildTable;
723 ParentTable = Gbl_RootTable;
731 DtSetSubtableLength (ParentTable);
735 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
738 if (ChildTable->LengthField)
740 DtSetSubtableLength (ChildTable);
743 if (ChildTable->Child)
745 ParentTable = ChildTable;
750 ParentTable->TotalLength += ChildTable->TotalLength;
751 if (ParentTable->LengthField)
753 DtSetSubtableLength (ParentTable);
759 ChildTable = ParentTable;
761 if (ChildTable == Gbl_RootTable)
766 ParentTable = DtGetParentSubtable (ParentTable);
768 ParentTable->TotalLength += ChildTable->TotalLength;
769 if (ParentTable->LengthField)
771 DtSetSubtableLength (ParentTable);
778 /******************************************************************************
780 * FUNCTION: DtWalkTableTree
782 * PARAMETERS: StartTable - Subtable in the tree where walking begins
783 * UserFunction - Called during the walk
784 * Context - Passed to user function
785 * ReturnValue - The return value of UserFunction
789 * DESCRIPTION: Performs a depth-first walk of the subtable tree
791 *****************************************************************************/
795 DT_SUBTABLE *StartTable,
796 DT_WALK_CALLBACK UserFunction,
800 DT_SUBTABLE *ParentTable;
801 DT_SUBTABLE *ChildTable;
804 ParentTable = StartTable;
812 UserFunction (ParentTable, Context, ReturnValue);
816 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
819 UserFunction (ChildTable, Context, ReturnValue);
821 if (ChildTable->Child)
823 ParentTable = ChildTable;
829 ChildTable = ParentTable;
830 if (ChildTable == Gbl_RootTable)
835 ParentTable = DtGetParentSubtable (ParentTable);
837 if (ChildTable->Peer == StartTable)
846 /*******************************************************************************
848 * FUNCTION: UtSubtableCacheCalloc
852 * RETURN: Pointer to the buffer. Aborts on allocation failure
854 * DESCRIPTION: Allocate a subtable object buffer. Bypass the local
855 * dynamic memory manager for performance reasons (This has a
856 * major impact on the speed of the compiler.)
858 ******************************************************************************/
861 UtSubtableCacheCalloc (
864 ASL_CACHE_INFO *Cache;
867 if (Gbl_SubtableCacheNext >= Gbl_SubtableCacheLast)
869 /* Allocate a new buffer */
871 Cache = UtLocalCalloc (sizeof (Cache->Next) +
872 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE));
874 /* Link new cache buffer to head of list */
876 Cache->Next = Gbl_SubtableCacheList;
877 Gbl_SubtableCacheList = Cache;
879 /* Setup cache management pointers */
881 Gbl_SubtableCacheNext = ACPI_CAST_PTR (DT_SUBTABLE, Cache->Buffer);
882 Gbl_SubtableCacheLast = Gbl_SubtableCacheNext + ASL_SUBTABLE_CACHE_SIZE;
886 return (Gbl_SubtableCacheNext++);
890 /*******************************************************************************
892 * FUNCTION: UtFieldCacheCalloc
896 * RETURN: Pointer to the buffer. Aborts on allocation failure
898 * DESCRIPTION: Allocate a field object buffer. Bypass the local
899 * dynamic memory manager for performance reasons (This has a
900 * major impact on the speed of the compiler.)
902 ******************************************************************************/
908 ASL_CACHE_INFO *Cache;
911 if (Gbl_FieldCacheNext >= Gbl_FieldCacheLast)
913 /* Allocate a new buffer */
915 Cache = UtLocalCalloc (sizeof (Cache->Next) +
916 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE));
918 /* Link new cache buffer to head of list */
920 Cache->Next = Gbl_FieldCacheList;
921 Gbl_FieldCacheList = Cache;
923 /* Setup cache management pointers */
925 Gbl_FieldCacheNext = ACPI_CAST_PTR (DT_FIELD, Cache->Buffer);
926 Gbl_FieldCacheLast = Gbl_FieldCacheNext + ASL_FIELD_CACHE_SIZE;
930 return (Gbl_FieldCacheNext++);
934 /*******************************************************************************
936 * FUNCTION: DtDeleteCaches
942 * DESCRIPTION: Delete all local cache buffer blocks
944 ******************************************************************************/
951 ASL_CACHE_INFO *Next;
957 while (Gbl_FieldCacheList)
959 Next = Gbl_FieldCacheList->Next;
960 ACPI_FREE (Gbl_FieldCacheList);
961 Gbl_FieldCacheList = Next;
965 DbgPrint (ASL_DEBUG_OUTPUT,
966 "%u Fields, Buffer size: %u fields (%u bytes), %u Buffers\n",
967 Gbl_FieldCount, ASL_FIELD_CACHE_SIZE,
968 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE), BufferCount);
971 Gbl_FieldCacheNext = NULL;
972 Gbl_FieldCacheLast = NULL;
977 while (Gbl_SubtableCacheList)
979 Next = Gbl_SubtableCacheList->Next;
980 ACPI_FREE (Gbl_SubtableCacheList);
981 Gbl_SubtableCacheList = Next;
985 DbgPrint (ASL_DEBUG_OUTPUT,
986 "%u Subtables, Buffer size: %u subtables (%u bytes), %u Buffers\n",
987 Gbl_SubtableCount, ASL_SUBTABLE_CACHE_SIZE,
988 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE), BufferCount);
990 Gbl_SubtableCount = 0;
991 Gbl_SubtableCacheNext = NULL;
992 Gbl_SubtableCacheLast = NULL;