1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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 "aslcompiler.h"
47 #include "dtcompiler.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,
89 if (Gbl_WarningLevel < Level)
101 AslCommonError (Level, MessageId,
104 FieldObject->ByteOffset,
106 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
110 AslCommonError (Level, MessageId, 0,
111 0, 0, 0, 0, ExtraMessage);
116 /******************************************************************************
118 * FUNCTION: DtNameError
120 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
121 * MessageId - Index into global message buffer
122 * Op - Parse node where error happened
123 * ExtraMessage - additional error message
127 * DESCRIPTION: Error interface for named objects
129 *****************************************************************************/
135 DT_FIELD *FieldObject,
143 if (Gbl_WarningLevel < Level)
155 AslCommonError (Level, MessageId,
158 FieldObject->ByteOffset,
159 FieldObject->NameColumn,
160 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
164 AslCommonError (Level, MessageId, 0,
165 0, 0, 0, 0, ExtraMessage);
170 /*******************************************************************************
178 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
179 * compile or I/O errors
181 ******************************************************************************/
186 DT_FIELD *FieldObject,
190 DtError (ASL_ERROR, MessageId, FieldObject, ExtraMessage);
193 * TBD: remove this entire function, DtFatal
195 * We cannot abort the compiler on error, because we may be compiling a
196 * list of files. We must move on to the next file.
205 /******************************************************************************
207 * FUNCTION: DtStrtoul64
209 * PARAMETERS: String - Null terminated string
210 * ReturnInteger - Where the converted integer is returned
214 * DESCRIPTION: Simple conversion of a string hex integer constant to unsigned
215 * value. Assumes no leading "0x" for the constant.
217 * Portability note: The reason this function exists is because a 64-bit
218 * sscanf is not available in all environments.
220 *****************************************************************************/
225 UINT64 *ReturnInteger)
227 char *ThisChar = String;
229 UINT64 ReturnValue = 0;
233 /* Skip over any white space in the buffer */
235 while ((*ThisChar == ' ') || (*ThisChar == '\t'))
240 /* Skip leading zeros */
242 while ((*ThisChar) == '0')
247 /* Convert character-by-character */
251 if (ACPI_IS_DIGIT (*ThisChar))
253 /* Convert ASCII 0-9 to Decimal value */
255 ThisDigit = ((UINT8) *ThisChar) - '0';
259 ThisDigit = (UINT32) ACPI_TOUPPER (*ThisChar);
260 if (!ACPI_IS_XDIGIT ((char) ThisDigit))
264 return (AE_BAD_CHARACTER);
267 /* Convert ASCII Hex char (A-F) to value */
269 ThisDigit = (ThisDigit - 'A') + 10;
272 /* Insert the 4-bit hex digit */
275 ReturnValue += ThisDigit;
281 /* Value is too large (> 64 bits/8 bytes/16 hex digits) */
287 *ReturnInteger = ReturnValue;
292 /******************************************************************************
294 * FUNCTION: DtGetFileSize
296 * PARAMETERS: Handle - Open file handler
298 * RETURN: Current file size
300 * DESCRIPTION: Get the current size of a file. Seek to the EOF and get the
301 * offset. Seek back to the original location.
303 *****************************************************************************/
313 CurrentOffset = ftell (Handle);
314 fseek (Handle, 0, SEEK_END);
315 LastOffset = ftell (Handle);
316 fseek (Handle, CurrentOffset, SEEK_SET);
318 return ((UINT32) LastOffset);
322 /******************************************************************************
324 * FUNCTION: DtGetFieldValue
326 * PARAMETERS: Field - Current field list pointer
328 * RETURN: Field value
330 * DESCRIPTION: Get field value
332 *****************************************************************************/
343 return (Field->Value);
347 /******************************************************************************
349 * FUNCTION: DtGetFieldType
351 * PARAMETERS: Info - Data table info
355 * DESCRIPTION: Get field type
357 *****************************************************************************/
361 ACPI_DMTABLE_INFO *Info)
366 /* DT_FLAG means that this is the start of a block of flag bits */
367 /* TBD - we can make these a separate opcode later */
369 if (Info->Flags & DT_FLAG)
371 return (DT_FIELD_TYPE_FLAGS_INTEGER);
374 /* Type is based upon the opcode for this field in the info table */
376 switch (Info->Opcode)
386 case ACPI_DMT_FLAGS0:
387 case ACPI_DMT_FLAGS1:
388 case ACPI_DMT_FLAGS2:
389 case ACPI_DMT_FLAGS4:
390 Type = DT_FIELD_TYPE_FLAG;
397 case ACPI_DMT_STRING:
398 Type = DT_FIELD_TYPE_STRING;
401 case ACPI_DMT_BUFFER:
404 case ACPI_DMT_BUF128:
405 case ACPI_DMT_PCI_PATH:
406 Type = DT_FIELD_TYPE_BUFFER;
410 case ACPI_DMT_HESTNTFY:
411 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
414 case ACPI_DMT_UNICODE:
415 Type = DT_FIELD_TYPE_UNICODE;
419 Type = DT_FIELD_TYPE_UUID;
422 case ACPI_DMT_DEVICE_PATH:
423 Type = DT_FIELD_TYPE_DEVICE_PATH;
427 Type = DT_FIELD_TYPE_LABEL;
431 Type = DT_FIELD_TYPE_INTEGER;
439 /******************************************************************************
441 * FUNCTION: DtGetBufferLength
443 * PARAMETERS: Buffer - List of integers,
444 * for example "10 3A 4F 2E"
446 * RETURN: Count of integer
448 * DESCRIPTION: Get length of bytes needed to store the integers
450 *****************************************************************************/
456 UINT32 ByteLength = 0;
465 while (*Buffer == ' ')
474 return (++ByteLength);
478 /******************************************************************************
480 * FUNCTION: DtGetFieldLength
482 * PARAMETERS: Field - Current field
483 * Info - Data table info
485 * RETURN: Field length
487 * DESCRIPTION: Get length of bytes needed to compile the field
489 * Note: This function must remain in sync with AcpiDmDumpTable.
491 *****************************************************************************/
496 ACPI_DMTABLE_INFO *Info)
498 UINT32 ByteLength = 0;
502 /* Length is based upon the opcode for this field in the info table */
504 switch (Info->Opcode)
514 case ACPI_DMT_FLAGS0:
515 case ACPI_DMT_FLAGS1:
516 case ACPI_DMT_FLAGS2:
517 case ACPI_DMT_FLAGS4:
519 case ACPI_DMT_EXTRA_TEXT:
524 case ACPI_DMT_CHKSUM:
525 case ACPI_DMT_SPACEID:
526 case ACPI_DMT_ACCWIDTH:
532 case ACPI_DMT_HESTNTYP:
533 case ACPI_DMT_FADTPM:
534 case ACPI_DMT_EINJACT:
535 case ACPI_DMT_EINJINST:
536 case ACPI_DMT_ERSTACT:
537 case ACPI_DMT_ERSTINST:
541 case ACPI_DMT_UINT16:
544 case ACPI_DMT_PCI_PATH:
548 case ACPI_DMT_UINT24:
552 case ACPI_DMT_UINT32:
559 case ACPI_DMT_UINT40:
563 case ACPI_DMT_UINT48:
568 case ACPI_DMT_UINT56:
573 case ACPI_DMT_UINT64:
578 case ACPI_DMT_STRING:
579 Value = DtGetFieldValue (Field);
582 ByteLength = ACPI_STRLEN (Value) + 1;
585 { /* At this point, this is a fatal error */
587 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
588 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
594 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
597 case ACPI_DMT_HESTNTFY:
598 ByteLength = sizeof (ACPI_HEST_NOTIFY);
601 case ACPI_DMT_BUFFER:
602 Value = DtGetFieldValue (Field);
605 ByteLength = DtGetBufferLength (Value);
608 { /* At this point, this is a fatal error */
610 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
611 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
621 case ACPI_DMT_BUF128:
625 case ACPI_DMT_UNICODE:
626 Value = DtGetFieldValue (Field);
628 /* TBD: error if Value is NULL? (as below?) */
630 ByteLength = (ACPI_STRLEN (Value) + 1) * sizeof(UINT16);
634 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
642 /******************************************************************************
646 * PARAMETERS: DT_WALK_CALLBACK:
647 * Subtable - Subtable
649 * ReturnValue - Store the checksum of subtable
653 * DESCRIPTION: Get the checksum of subtable
655 *****************************************************************************/
659 DT_SUBTABLE *Subtable,
664 UINT8 *Sum = ReturnValue;
667 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
668 *Sum = (UINT8) (*Sum + Checksum);
672 /******************************************************************************
674 * FUNCTION: DtSetTableChecksum
676 * PARAMETERS: ChecksumPointer - Where to return the checksum
680 * DESCRIPTION: Set checksum of the whole data table into the checksum field
682 *****************************************************************************/
686 UINT8 *ChecksumPointer)
692 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
694 OldSum = *ChecksumPointer;
695 Checksum = (UINT8) (Checksum - OldSum);
697 /* Compute the final checksum */
699 Checksum = (UINT8) (0 - Checksum);
700 *ChecksumPointer = Checksum;
704 /******************************************************************************
706 * FUNCTION: DtSetTableLength
712 * DESCRIPTION: Walk the subtables and set all the length fields
714 *****************************************************************************/
720 DT_SUBTABLE *ParentTable;
721 DT_SUBTABLE *ChildTable;
724 ParentTable = Gbl_RootTable;
732 DtSetSubtableLength (ParentTable);
736 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
739 if (ChildTable->LengthField)
741 DtSetSubtableLength (ChildTable);
744 if (ChildTable->Child)
746 ParentTable = ChildTable;
751 ParentTable->TotalLength += ChildTable->TotalLength;
752 if (ParentTable->LengthField)
754 DtSetSubtableLength (ParentTable);
760 ChildTable = ParentTable;
762 if (ChildTable == Gbl_RootTable)
767 ParentTable = DtGetParentSubtable (ParentTable);
769 ParentTable->TotalLength += ChildTable->TotalLength;
770 if (ParentTable->LengthField)
772 DtSetSubtableLength (ParentTable);
779 /******************************************************************************
781 * FUNCTION: DtWalkTableTree
783 * PARAMETERS: StartTable - Subtable in the tree where walking begins
784 * UserFunction - Called during the walk
785 * Context - Passed to user function
786 * ReturnValue - The return value of UserFunction
790 * DESCRIPTION: Performs a depth-first walk of the subtable tree
792 *****************************************************************************/
796 DT_SUBTABLE *StartTable,
797 DT_WALK_CALLBACK UserFunction,
801 DT_SUBTABLE *ParentTable;
802 DT_SUBTABLE *ChildTable;
805 ParentTable = StartTable;
813 UserFunction (ParentTable, Context, ReturnValue);
817 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
820 UserFunction (ChildTable, Context, ReturnValue);
822 if (ChildTable->Child)
824 ParentTable = ChildTable;
830 ChildTable = ParentTable;
831 if (ChildTable == Gbl_RootTable)
836 ParentTable = DtGetParentSubtable (ParentTable);
838 if (ChildTable->Peer == StartTable)
847 /******************************************************************************
849 * FUNCTION: DtFreeFieldList
855 * DESCRIPTION: Free the field list
857 *****************************************************************************/
863 DT_FIELD *Field = Gbl_FieldList;
867 /* Walk and free entire field list */
871 NextField = Field->Next; /* Save link */
873 if (!(Field->Flags & DT_FIELD_NOT_ALLOCATED))
875 ACPI_FREE (Field->Name);
876 ACPI_FREE (Field->Value);