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.
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,
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: DtGetFieldValue
291 * PARAMETERS: Field - Current field list pointer
293 * RETURN: Field value
295 * DESCRIPTION: Get field value
297 *****************************************************************************/
308 return (Field->Value);
312 /******************************************************************************
314 * FUNCTION: DtGetFieldType
316 * PARAMETERS: Info - Data table info
320 * DESCRIPTION: Get field type
322 *****************************************************************************/
326 ACPI_DMTABLE_INFO *Info)
331 /* DT_FLAG means that this is the start of a block of flag bits */
332 /* TBD - we can make these a separate opcode later */
334 if (Info->Flags & DT_FLAG)
336 return (DT_FIELD_TYPE_FLAGS_INTEGER);
339 /* Type is based upon the opcode for this field in the info table */
341 switch (Info->Opcode)
351 case ACPI_DMT_FLAGS0:
352 case ACPI_DMT_FLAGS1:
353 case ACPI_DMT_FLAGS2:
354 case ACPI_DMT_FLAGS4:
356 Type = DT_FIELD_TYPE_FLAG;
363 case ACPI_DMT_STRING:
365 Type = DT_FIELD_TYPE_STRING;
368 case ACPI_DMT_BUFFER:
372 case ACPI_DMT_BUF128:
373 case ACPI_DMT_PCI_PATH:
375 Type = DT_FIELD_TYPE_BUFFER;
379 case ACPI_DMT_HESTNTFY:
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:
509 case ACPI_DMT_HESTNTYP:
510 case ACPI_DMT_FADTPM:
511 case ACPI_DMT_EINJACT:
512 case ACPI_DMT_EINJINST:
513 case ACPI_DMT_ERSTACT:
514 case ACPI_DMT_ERSTINST:
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: DtFreeFieldList
854 * DESCRIPTION: Free the field list
856 *****************************************************************************/
862 DT_FIELD *Field = Gbl_FieldList;
866 /* Walk and free entire field list */
870 NextField = Field->Next; /* Save link */
872 if (!(Field->Flags & DT_FIELD_NOT_ALLOCATED))
874 ACPI_FREE (Field->Name);
875 ACPI_FREE (Field->Value);