1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for 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 ("dtio")
54 /* Local prototypes */
72 DT_SUBTABLE *Subtable,
85 DT_SUBTABLE *Subtable,
91 DT_SUBTABLE *Subtable,
96 /* States for DtGetNextLine */
98 #define DT_NORMAL_TEXT 0
99 #define DT_START_QUOTED_STRING 1
100 #define DT_START_COMMENT 2
101 #define DT_SLASH_ASTERISK_COMMENT 3
102 #define DT_SLASH_SLASH_COMMENT 4
103 #define DT_END_COMMENT 5
104 #define DT_MERGE_LINES 6
105 #define DT_ESCAPE_SEQUENCE 7
107 static UINT32 Gbl_NextLineOffset;
110 /******************************************************************************
114 * PARAMETERS: String - Current source code line to trim
116 * RETURN: Trimmed line. Must be freed by caller.
118 * DESCRIPTION: Trim left and right spaces
120 *****************************************************************************/
132 /* Skip lines that start with a space */
134 if (!ACPI_STRCMP (String, " "))
136 ReturnString = UtLocalCalloc (1);
137 return (ReturnString);
140 /* Setup pointers to start and end of input string */
143 End = String + ACPI_STRLEN (String) - 1;
145 /* Find first non-whitespace character */
147 while ((Start <= End) && ((*Start == ' ') || (*Start == '\t')))
152 /* Find last non-space character */
156 if (*End == '\r' || *End == '\n')
170 /* Remove any quotes around the string */
181 /* Create the trimmed return string */
183 Length = ACPI_PTR_DIFF (End, Start) + 1;
184 ReturnString = UtLocalCalloc (Length + 1);
185 if (ACPI_STRLEN (Start))
187 ACPI_STRNCPY (ReturnString, Start, Length);
190 ReturnString[Length] = 0;
191 return (ReturnString);
195 /******************************************************************************
197 * FUNCTION: DtLinkField
199 * PARAMETERS: Field - New field object to link
203 * DESCRIPTION: Link one field name and value to the list
205 *****************************************************************************/
215 Prev = Next = Gbl_FieldList;
229 Gbl_FieldList = Field;
234 /******************************************************************************
236 * FUNCTION: DtParseLine
238 * PARAMETERS: LineBuffer - Current source code line
239 * Line - Current line number in the source
240 * Offset - Current byte offset of the line
244 * DESCRIPTION: Parse one source line
246 *****************************************************************************/
265 BOOLEAN IsNullString = FALSE;
273 /* All lines after "Raw Table Data" are ingored */
275 if (strstr (LineBuffer, ACPI_RAW_TABLE_DATA_HEADER))
277 return (AE_NOT_FOUND);
280 Colon = strchr (LineBuffer, ':');
289 while (Start < Colon)
293 /* Found left bracket, go to the right bracket */
295 while (Start < Colon && *Start != ']')
300 else if (*Start != ' ')
309 * There are two column values. One for the field name,
310 * and one for the field value.
312 Column = ACPI_PTR_DIFF (Colon, LineBuffer) + 3;
313 NameColumn = ACPI_PTR_DIFF (Start, LineBuffer) + 1;
315 Length = ACPI_PTR_DIFF (End, Start);
317 TmpName = UtLocalCalloc (Length + 1);
318 ACPI_STRNCPY (TmpName, Start, Length);
319 Name = DtTrim (TmpName);
322 Start = End = (Colon + 1);
325 /* Found left quotation, go to the right quotation and break */
331 /* Check for an explicit null string */
337 while (*End && (*End != '"'))
347 * Special "comment" fields at line end, ignore them.
348 * Note: normal slash-slash and slash-asterisk comments are
349 * stripped already by the DtGetNextLine parser.
351 * TBD: Perhaps DtGetNextLine should parse the following type
362 Length = ACPI_PTR_DIFF (End, Start);
363 TmpValue = UtLocalCalloc (Length + 1);
365 ACPI_STRNCPY (TmpValue, Start, Length);
366 Value = DtTrim (TmpValue);
367 ACPI_FREE (TmpValue);
369 /* Create a new field object only if we have a valid value field */
371 if ((Value && *Value) || IsNullString)
373 Field = UtLocalCalloc (sizeof (DT_FIELD));
375 Field->Value = Value;
377 Field->ByteOffset = Offset;
378 Field->NameColumn = NameColumn;
379 Field->Column = Column;
383 else /* Ignore this field, it has no valid data */
393 /******************************************************************************
395 * FUNCTION: DtGetNextLine
397 * PARAMETERS: Handle - Open file handle for the source file
399 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
401 * DESCRIPTION: Get the next valid source line. Removes all comments.
402 * Ignores empty lines.
404 * Handles both slash-asterisk and slash-slash comments.
405 * Also, quoted strings, but no escapes within.
407 * Line is returned in Gbl_CurrentLineBuffer.
408 * Line number in original file is returned in Gbl_CurrentLineNumber.
410 *****************************************************************************/
416 BOOLEAN LineNotAllBlanks = FALSE;
417 UINT32 State = DT_NORMAL_TEXT;
418 UINT32 CurrentLineOffset;
426 * If line is too long, expand the line buffers. Also increases
427 * Gbl_LineBufferSize.
429 if (i >= Gbl_LineBufferSize)
431 UtExpandLineBuffers ();
439 case DT_START_QUOTED_STRING:
440 case DT_SLASH_ASTERISK_COMMENT:
442 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
450 /* Standalone EOF is OK */
458 * Received an EOF in the middle of a line. Terminate the
459 * line with a newline. The next call to this function will
460 * return a standalone EOF. Thus, the upper parsing software
461 * never has to deal with an EOF within a valid line (or
462 * the last line does not get tossed on the floor.)
465 State = DT_NORMAL_TEXT;
472 /* Normal text, insert char into line buffer */
474 Gbl_CurrentLineBuffer[i] = (char) c;
479 State = DT_START_COMMENT;
484 State = DT_START_QUOTED_STRING;
485 LineNotAllBlanks = TRUE;
491 * The continuation char MUST be last char on this line.
492 * Otherwise, it will be assumed to be a valid ASL char.
494 State = DT_MERGE_LINES;
499 CurrentLineOffset = Gbl_NextLineOffset;
500 Gbl_NextLineOffset = (UINT32) ftell (Handle);
501 Gbl_CurrentLineNumber++;
504 * Exit if line is complete. Ignore empty lines (only \n)
505 * or lines that contain nothing but blanks.
507 if ((i != 0) && LineNotAllBlanks)
509 if ((i + 1) >= Gbl_LineBufferSize)
511 UtExpandLineBuffers ();
514 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
515 return (CurrentLineOffset);
518 /* Toss this line and start a new one */
521 LineNotAllBlanks = FALSE;
528 LineNotAllBlanks = TRUE;
536 case DT_START_QUOTED_STRING:
538 /* Insert raw chars until end of quoted string */
540 Gbl_CurrentLineBuffer[i] = (char) c;
547 State = DT_NORMAL_TEXT;
552 State = DT_ESCAPE_SEQUENCE;
557 AcpiOsPrintf ("ERROR at line %u: Unterminated quoted string\n",
558 Gbl_CurrentLineNumber++);
559 State = DT_NORMAL_TEXT;
562 default: /* Get next character */
568 case DT_ESCAPE_SEQUENCE:
570 /* Just copy the escaped character. TBD: sufficient for table compiler? */
572 Gbl_CurrentLineBuffer[i] = (char) c;
574 State = DT_START_QUOTED_STRING;
577 case DT_START_COMMENT:
579 /* Open comment if this character is an asterisk or slash */
585 State = DT_SLASH_ASTERISK_COMMENT;
590 State = DT_SLASH_SLASH_COMMENT;
593 default: /* Not a comment */
595 i++; /* Save the preceding slash */
596 if (i >= Gbl_LineBufferSize)
598 UtExpandLineBuffers ();
601 Gbl_CurrentLineBuffer[i] = (char) c;
603 State = DT_NORMAL_TEXT;
608 case DT_SLASH_ASTERISK_COMMENT:
610 /* Ignore chars until an asterisk-slash is found */
616 Gbl_NextLineOffset = (UINT32) ftell (Handle);
617 Gbl_CurrentLineNumber++;
622 State = DT_END_COMMENT;
631 case DT_SLASH_SLASH_COMMENT:
633 /* Ignore chars until end-of-line */
637 /* We will exit via the NORMAL_TEXT path */
640 State = DT_NORMAL_TEXT;
646 /* End comment if this char is a slash */
652 State = DT_NORMAL_TEXT;
657 CurrentLineOffset = Gbl_NextLineOffset;
658 Gbl_NextLineOffset = (UINT32) ftell (Handle);
659 Gbl_CurrentLineNumber++;
664 /* Consume all adjacent asterisks */
669 State = DT_SLASH_ASTERISK_COMMENT;
679 * This is not a continuation backslash, it is a normal
680 * normal ASL backslash - for example: Scope(\_SB_)
682 i++; /* Keep the backslash that is already in the buffer */
685 State = DT_NORMAL_TEXT;
690 * This is a continuation line -- a backlash followed
691 * immediately by a newline. Insert a space between the
692 * lines (overwrite the backslash)
694 Gbl_CurrentLineBuffer[i] = ' ';
697 /* Ignore newline, this will merge the lines */
699 CurrentLineOffset = Gbl_NextLineOffset;
700 Gbl_NextLineOffset = (UINT32) ftell (Handle);
701 Gbl_CurrentLineNumber++;
702 State = DT_NORMAL_TEXT;
708 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
715 /******************************************************************************
717 * FUNCTION: DtScanFile
719 * PARAMETERS: Handle - Open file handle for the source file
721 * RETURN: Pointer to start of the constructed parse tree.
723 * DESCRIPTION: Scan source file, link all field names and values
724 * to the global parse tree: Gbl_FieldList
726 *****************************************************************************/
736 ACPI_FUNCTION_NAME (DtScanFile);
739 /* Get the file size */
741 Gbl_InputByteCount = CmGetFileSize (Handle);
742 if (Gbl_InputByteCount == ACPI_UINT32_MAX)
747 Gbl_CurrentLineNumber = 0;
748 Gbl_CurrentLineOffset = 0;
749 Gbl_NextLineOffset = 0;
751 /* Scan line-by-line */
753 while ((Offset = DtGetNextLine (Handle)) != ASL_EOF)
755 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
756 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
758 Status = DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
759 if (Status == AE_NOT_FOUND)
765 /* Dump the parse tree if debug enabled */
767 DtDumpFieldList (Gbl_FieldList);
768 return (Gbl_FieldList);
776 /******************************************************************************
778 * FUNCTION: DtWriteBinary
780 * PARAMETERS: DT_WALK_CALLBACK
784 * DESCRIPTION: Write one subtable of a binary ACPI table
786 *****************************************************************************/
790 DT_SUBTABLE *Subtable,
795 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
799 /******************************************************************************
801 * FUNCTION: DtOutputBinary
807 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
809 *****************************************************************************/
813 DT_SUBTABLE *RootTable)
821 /* Walk the entire parse tree, emitting the binary data */
823 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
825 Gbl_TableLength = CmGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
826 if (Gbl_TableLength == ACPI_UINT32_MAX)
837 /******************************************************************************
839 * FUNCTION: DtDumpBuffer
841 * PARAMETERS: FileID - Where to write buffer data
842 * Buffer - Buffer to dump
843 * Offset - Offset in current table
844 * Length - Buffer Length
848 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
850 * TBD: merge dump buffer routines
852 *****************************************************************************/
866 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
867 Offset, Offset, Length);
874 FlPrintFile (FileId, "%24s", "");
877 /* Print 16 hex chars */
883 /* Dump fill spaces */
885 FlPrintFile (FileId, " ");
890 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
894 FlPrintFile (FileId, " ");
895 for (j = 0; j < 16; j++)
899 FlPrintFile (FileId, "\n\n");
903 BufChar = Buffer[(ACPI_SIZE) i + j];
904 if (ACPI_IS_PRINT (BufChar))
906 FlPrintFile (FileId, "%c", BufChar);
910 FlPrintFile (FileId, ".");
914 /* Done with that line. */
916 FlPrintFile (FileId, "\n");
920 FlPrintFile (FileId, "\n\n");
924 /******************************************************************************
926 * FUNCTION: DtDumpFieldList
928 * PARAMETERS: Field - Root field
932 * DESCRIPTION: Dump the entire field list
934 *****************************************************************************/
941 if (!Gbl_DebugFlag || !Field)
946 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
947 "LineNo ByteOff NameCol Column TableOff "
948 "Flags %32s : %s\n\n", "Name", "Value");
951 DbgPrint (ASL_DEBUG_OUTPUT,
952 "%.08X %.08X %.08X %.08X %.08X %.08X %32s : %s\n",
953 Field->Line, Field->ByteOffset, Field->NameColumn,
954 Field->Column, Field->TableOffset, Field->Flags,
955 Field->Name, Field->Value);
960 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
964 /******************************************************************************
966 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
968 * PARAMETERS: DT_WALK_CALLBACK
972 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
973 * Tree - dump a subtable tree formatted by depth indentation.
975 *****************************************************************************/
979 DT_SUBTABLE *Subtable,
984 DbgPrint (ASL_DEBUG_OUTPUT,
985 "[%.04X] %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
986 Subtable->Depth, Subtable->Length, Subtable->TotalLength,
987 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
988 Subtable->Parent, Subtable->Child, Subtable->Peer);
993 DT_SUBTABLE *Subtable,
998 DbgPrint (ASL_DEBUG_OUTPUT,
999 "[%.04X] %*s%08X (%.02X) - (%.02X)\n",
1000 Subtable->Depth, (4 * Subtable->Depth), " ",
1001 Subtable, Subtable->Length, Subtable->TotalLength);
1005 /******************************************************************************
1007 * FUNCTION: DtDumpSubtableList
1013 * DESCRIPTION: Dump the raw list of subtables with information, and also
1014 * dump the subtable list in formatted tree format. Assists with
1015 * the development of new table code.
1017 *****************************************************************************/
1020 DtDumpSubtableList (
1024 if (!Gbl_DebugFlag || !Gbl_RootTable)
1029 DbgPrint (ASL_DEBUG_OUTPUT,
1031 "Depth Length TotalLen LenSize Flags "
1032 "This Parent Child Peer\n\n");
1033 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1035 DbgPrint (ASL_DEBUG_OUTPUT,
1036 "\nSubtable Tree: (Depth, Subtable, Length, TotalLength)\n\n");
1037 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1041 /******************************************************************************
1043 * FUNCTION: DtWriteFieldToListing
1045 * PARAMETERS: Buffer - Contains the compiled data
1046 * Field - Field node for the input line
1047 * Length - Length of the output data
1051 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1053 *****************************************************************************/
1056 DtWriteFieldToListing (
1064 if (!Gbl_ListingFlag || !Field)
1069 /* Dump the original source line */
1071 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1072 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1074 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1076 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1077 if (FileByte == '\n')
1083 /* Dump the line as parsed and represented internally */
1085 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1086 Field->Column-4, Field->Name, Field->Value);
1088 if (strlen (Field->Value) > 64)
1090 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1091 strlen (Field->Value));
1093 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1095 /* Dump the hex data that will be output for this field */
1097 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1101 /******************************************************************************
1103 * FUNCTION: DtWriteTableToListing
1109 * DESCRIPTION: Write the entire compiled table to the listing file
1112 *****************************************************************************/
1115 DtWriteTableToListing (
1121 if (!Gbl_ListingFlag)
1126 /* Read the entire table from the output file */
1128 Buffer = UtLocalCalloc (Gbl_TableLength);
1129 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1130 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1132 /* Dump the raw table data */
1134 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1136 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1137 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1138 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1140 AcpiOsRedirectOutput (stdout);