1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
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>
46 #include <contrib/dev/acpica/include/actables.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:
367 case ACPI_DMT_RAW_BUFFER:
371 case ACPI_DMT_BUF128:
372 case ACPI_DMT_PCI_PATH:
374 Type = DT_FIELD_TYPE_BUFFER;
378 case ACPI_DMT_HESTNTFY:
379 case ACPI_DMT_IORTMEM:
381 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
384 case ACPI_DMT_UNICODE:
386 Type = DT_FIELD_TYPE_UNICODE;
391 Type = DT_FIELD_TYPE_UUID;
394 case ACPI_DMT_DEVICE_PATH:
396 Type = DT_FIELD_TYPE_DEVICE_PATH;
401 Type = DT_FIELD_TYPE_LABEL;
406 Type = DT_FIELD_TYPE_INTEGER;
414 /******************************************************************************
416 * FUNCTION: DtGetBufferLength
418 * PARAMETERS: Buffer - List of integers,
419 * for example "10 3A 4F 2E"
421 * RETURN: Count of integer
423 * DESCRIPTION: Get length of bytes needed to store the integers
425 *****************************************************************************/
431 UINT32 ByteLength = 0;
440 while (*Buffer == ' ')
449 return (++ByteLength);
453 /******************************************************************************
455 * FUNCTION: DtGetFieldLength
457 * PARAMETERS: Field - Current field
458 * Info - Data table info
460 * RETURN: Field length
462 * DESCRIPTION: Get length of bytes needed to compile the field
464 * Note: This function must remain in sync with AcpiDmDumpTable.
466 *****************************************************************************/
471 ACPI_DMTABLE_INFO *Info)
473 UINT32 ByteLength = 0;
477 /* Length is based upon the opcode for this field in the info table */
479 switch (Info->Opcode)
489 case ACPI_DMT_FLAGS0:
490 case ACPI_DMT_FLAGS1:
491 case ACPI_DMT_FLAGS2:
492 case ACPI_DMT_FLAGS4:
494 case ACPI_DMT_EXTRA_TEXT:
500 case ACPI_DMT_CHKSUM:
501 case ACPI_DMT_SPACEID:
502 case ACPI_DMT_ACCWIDTH:
510 case ACPI_DMT_HESTNTYP:
511 case ACPI_DMT_FADTPM:
512 case ACPI_DMT_EINJACT:
513 case ACPI_DMT_EINJINST:
514 case ACPI_DMT_ERSTACT:
515 case ACPI_DMT_ERSTINST:
516 case ACPI_DMT_DMAR_SCOPE:
521 case ACPI_DMT_UINT16:
525 case ACPI_DMT_PCI_PATH:
530 case ACPI_DMT_UINT24:
535 case ACPI_DMT_UINT32:
543 case ACPI_DMT_UINT40:
548 case ACPI_DMT_UINT48:
554 case ACPI_DMT_UINT56:
560 case ACPI_DMT_UINT64:
566 case ACPI_DMT_STRING:
568 Value = DtGetFieldValue (Field);
571 ByteLength = ACPI_STRLEN (Value) + 1;
574 { /* At this point, this is a fatal error */
576 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
577 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
584 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
587 case ACPI_DMT_HESTNTFY:
589 ByteLength = sizeof (ACPI_HEST_NOTIFY);
592 case ACPI_DMT_IORTMEM:
594 ByteLength = sizeof (ACPI_IORT_MEMORY_ACCESS);
597 case ACPI_DMT_BUFFER:
598 case ACPI_DMT_RAW_BUFFER:
600 Value = DtGetFieldValue (Field);
603 ByteLength = DtGetBufferLength (Value);
606 { /* At this point, this is a fatal error */
608 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
609 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
625 case ACPI_DMT_BUF128:
630 case ACPI_DMT_UNICODE:
632 Value = DtGetFieldValue (Field);
634 /* TBD: error if Value is NULL? (as below?) */
636 ByteLength = (ACPI_STRLEN (Value) + 1) * sizeof(UINT16);
641 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
649 /******************************************************************************
653 * PARAMETERS: DT_WALK_CALLBACK:
654 * Subtable - Subtable
656 * ReturnValue - Store the checksum of subtable
660 * DESCRIPTION: Get the checksum of subtable
662 *****************************************************************************/
666 DT_SUBTABLE *Subtable,
671 UINT8 *Sum = ReturnValue;
674 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
675 *Sum = (UINT8) (*Sum + Checksum);
679 /******************************************************************************
681 * FUNCTION: DtSetTableChecksum
683 * PARAMETERS: ChecksumPointer - Where to return the checksum
687 * DESCRIPTION: Set checksum of the whole data table into the checksum field
689 *****************************************************************************/
693 UINT8 *ChecksumPointer)
699 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
701 OldSum = *ChecksumPointer;
702 Checksum = (UINT8) (Checksum - OldSum);
704 /* Compute the final checksum */
706 Checksum = (UINT8) (0 - Checksum);
707 *ChecksumPointer = Checksum;
711 /******************************************************************************
713 * FUNCTION: DtSetTableLength
719 * DESCRIPTION: Walk the subtables and set all the length fields
721 *****************************************************************************/
727 DT_SUBTABLE *ParentTable;
728 DT_SUBTABLE *ChildTable;
731 ParentTable = Gbl_RootTable;
739 DtSetSubtableLength (ParentTable);
743 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
746 if (ChildTable->LengthField)
748 DtSetSubtableLength (ChildTable);
751 if (ChildTable->Child)
753 ParentTable = ChildTable;
758 ParentTable->TotalLength += ChildTable->TotalLength;
759 if (ParentTable->LengthField)
761 DtSetSubtableLength (ParentTable);
767 ChildTable = ParentTable;
769 if (ChildTable == Gbl_RootTable)
774 ParentTable = DtGetParentSubtable (ParentTable);
776 ParentTable->TotalLength += ChildTable->TotalLength;
777 if (ParentTable->LengthField)
779 DtSetSubtableLength (ParentTable);
786 /******************************************************************************
788 * FUNCTION: DtWalkTableTree
790 * PARAMETERS: StartTable - Subtable in the tree where walking begins
791 * UserFunction - Called during the walk
792 * Context - Passed to user function
793 * ReturnValue - The return value of UserFunction
797 * DESCRIPTION: Performs a depth-first walk of the subtable tree
799 *****************************************************************************/
803 DT_SUBTABLE *StartTable,
804 DT_WALK_CALLBACK UserFunction,
808 DT_SUBTABLE *ParentTable;
809 DT_SUBTABLE *ChildTable;
812 ParentTable = StartTable;
820 UserFunction (ParentTable, Context, ReturnValue);
824 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
827 UserFunction (ChildTable, Context, ReturnValue);
829 if (ChildTable->Child)
831 ParentTable = ChildTable;
837 ChildTable = ParentTable;
838 if (ChildTable == Gbl_RootTable)
843 ParentTable = DtGetParentSubtable (ParentTable);
845 if (ChildTable->Peer == StartTable)
854 /*******************************************************************************
856 * FUNCTION: UtSubtableCacheCalloc
860 * RETURN: Pointer to the buffer. Aborts on allocation failure
862 * DESCRIPTION: Allocate a subtable object buffer. Bypass the local
863 * dynamic memory manager for performance reasons (This has a
864 * major impact on the speed of the compiler.)
866 ******************************************************************************/
869 UtSubtableCacheCalloc (
872 ASL_CACHE_INFO *Cache;
875 if (Gbl_SubtableCacheNext >= Gbl_SubtableCacheLast)
877 /* Allocate a new buffer */
879 Cache = UtLocalCalloc (sizeof (Cache->Next) +
880 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE));
882 /* Link new cache buffer to head of list */
884 Cache->Next = Gbl_SubtableCacheList;
885 Gbl_SubtableCacheList = Cache;
887 /* Setup cache management pointers */
889 Gbl_SubtableCacheNext = ACPI_CAST_PTR (DT_SUBTABLE, Cache->Buffer);
890 Gbl_SubtableCacheLast = Gbl_SubtableCacheNext + ASL_SUBTABLE_CACHE_SIZE;
894 return (Gbl_SubtableCacheNext++);
898 /*******************************************************************************
900 * FUNCTION: UtFieldCacheCalloc
904 * RETURN: Pointer to the buffer. Aborts on allocation failure
906 * DESCRIPTION: Allocate a field object buffer. Bypass the local
907 * dynamic memory manager for performance reasons (This has a
908 * major impact on the speed of the compiler.)
910 ******************************************************************************/
916 ASL_CACHE_INFO *Cache;
919 if (Gbl_FieldCacheNext >= Gbl_FieldCacheLast)
921 /* Allocate a new buffer */
923 Cache = UtLocalCalloc (sizeof (Cache->Next) +
924 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE));
926 /* Link new cache buffer to head of list */
928 Cache->Next = Gbl_FieldCacheList;
929 Gbl_FieldCacheList = Cache;
931 /* Setup cache management pointers */
933 Gbl_FieldCacheNext = ACPI_CAST_PTR (DT_FIELD, Cache->Buffer);
934 Gbl_FieldCacheLast = Gbl_FieldCacheNext + ASL_FIELD_CACHE_SIZE;
938 return (Gbl_FieldCacheNext++);
942 /*******************************************************************************
944 * FUNCTION: DtDeleteCaches
950 * DESCRIPTION: Delete all local cache buffer blocks
952 ******************************************************************************/
959 ASL_CACHE_INFO *Next;
965 while (Gbl_FieldCacheList)
967 Next = Gbl_FieldCacheList->Next;
968 ACPI_FREE (Gbl_FieldCacheList);
969 Gbl_FieldCacheList = Next;
973 DbgPrint (ASL_DEBUG_OUTPUT,
974 "%u Fields, Buffer size: %u fields (%u bytes), %u Buffers\n",
975 Gbl_FieldCount, ASL_FIELD_CACHE_SIZE,
976 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE), BufferCount);
979 Gbl_FieldCacheNext = NULL;
980 Gbl_FieldCacheLast = NULL;
985 while (Gbl_SubtableCacheList)
987 Next = Gbl_SubtableCacheList->Next;
988 ACPI_FREE (Gbl_SubtableCacheList);
989 Gbl_SubtableCacheList = Next;
993 DbgPrint (ASL_DEBUG_OUTPUT,
994 "%u Subtables, Buffer size: %u subtables (%u bytes), %u Buffers\n",
995 Gbl_SubtableCount, ASL_SUBTABLE_CACHE_SIZE,
996 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE), BufferCount);
998 Gbl_SubtableCount = 0;
999 Gbl_SubtableCacheNext = NULL;
1000 Gbl_SubtableCacheLast = NULL;