1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for 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"
49 #define _COMPONENT DT_COMPILER
50 ACPI_MODULE_NAME ("dtio")
53 /* Local prototypes */
71 DT_SUBTABLE *Subtable,
84 DT_SUBTABLE *Subtable,
90 DT_SUBTABLE *Subtable,
95 /* States for DtGetNextLine */
97 #define DT_NORMAL_TEXT 0
98 #define DT_START_QUOTED_STRING 1
99 #define DT_START_COMMENT 2
100 #define DT_SLASH_ASTERISK_COMMENT 3
101 #define DT_SLASH_SLASH_COMMENT 4
102 #define DT_END_COMMENT 5
103 #define DT_MERGE_LINES 6
104 #define DT_ESCAPE_SEQUENCE 7
106 static UINT32 Gbl_NextLineOffset;
109 /******************************************************************************
113 * PARAMETERS: String - Current source code line to trim
115 * RETURN: Trimmed line. Must be freed by caller.
117 * DESCRIPTION: Trim left and right spaces
119 *****************************************************************************/
131 /* Skip lines that start with a space */
133 if (!ACPI_STRCMP (String, " "))
135 ReturnString = UtLocalCalloc (1);
136 return (ReturnString);
139 /* Setup pointers to start and end of input string */
142 End = String + ACPI_STRLEN (String) - 1;
144 /* Find first non-whitespace character */
146 while ((Start <= End) && ((*Start == ' ') || (*Start == '\t')))
151 /* Find last non-space character */
155 if (*End == '\r' || *End == '\n')
169 /* Remove any quotes around the string */
180 /* Create the trimmed return string */
182 Length = ACPI_PTR_DIFF (End, Start) + 1;
183 ReturnString = UtLocalCalloc (Length + 1);
184 if (ACPI_STRLEN (Start))
186 ACPI_STRNCPY (ReturnString, Start, Length);
189 ReturnString[Length] = 0;
190 return (ReturnString);
194 /******************************************************************************
196 * FUNCTION: DtLinkField
198 * PARAMETERS: Field - New field object to link
202 * DESCRIPTION: Link one field name and value to the list
204 *****************************************************************************/
214 Prev = Next = Gbl_FieldList;
228 Gbl_FieldList = Field;
233 /******************************************************************************
235 * FUNCTION: DtParseLine
237 * PARAMETERS: LineBuffer - Current source code line
238 * Line - Current line number in the source
239 * Offset - Current byte offset of the line
243 * DESCRIPTION: Parse one source line
245 *****************************************************************************/
264 BOOLEAN IsNullString = FALSE;
272 /* All lines after "Raw Table Data" are ingored */
274 if (strstr (LineBuffer, ACPI_RAW_TABLE_DATA_HEADER))
276 return (AE_NOT_FOUND);
279 Colon = strchr (LineBuffer, ':');
288 while (Start < Colon)
296 /* Found left bracket, go to the right bracket */
300 while (Start < Colon && *Start != ']')
318 * There are two column values. One for the field name,
319 * and one for the field value.
321 Column = ACPI_PTR_DIFF (Colon, LineBuffer) + 3;
322 NameColumn = ACPI_PTR_DIFF (Start, LineBuffer) + 1;
324 Length = ACPI_PTR_DIFF (End, Start);
326 TmpName = UtLocalCalloc (Length + 1);
327 ACPI_STRNCPY (TmpName, Start, Length);
328 Name = DtTrim (TmpName);
331 Start = End = (Colon + 1);
334 /* Found left quotation, go to the right quotation and break */
340 /* Check for an explicit null string */
346 while (*End && (*End != '"'))
356 * Special "comment" fields at line end, ignore them.
357 * Note: normal slash-slash and slash-asterisk comments are
358 * stripped already by the DtGetNextLine parser.
360 * TBD: Perhaps DtGetNextLine should parse the following type
371 Length = ACPI_PTR_DIFF (End, Start);
372 TmpValue = UtLocalCalloc (Length + 1);
374 ACPI_STRNCPY (TmpValue, Start, Length);
375 Value = DtTrim (TmpValue);
376 ACPI_FREE (TmpValue);
378 /* Create a new field object only if we have a valid value field */
380 if ((Value && *Value) || IsNullString)
382 Field = UtLocalCalloc (sizeof (DT_FIELD));
384 Field->Value = Value;
386 Field->ByteOffset = Offset;
387 Field->NameColumn = NameColumn;
388 Field->Column = Column;
392 else /* Ignore this field, it has no valid data */
402 /******************************************************************************
404 * FUNCTION: DtGetNextLine
406 * PARAMETERS: Handle - Open file handle for the source file
408 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
410 * DESCRIPTION: Get the next valid source line. Removes all comments.
411 * Ignores empty lines.
413 * Handles both slash-asterisk and slash-slash comments.
414 * Also, quoted strings, but no escapes within.
416 * Line is returned in Gbl_CurrentLineBuffer.
417 * Line number in original file is returned in Gbl_CurrentLineNumber.
419 *****************************************************************************/
425 BOOLEAN LineNotAllBlanks = FALSE;
426 UINT32 State = DT_NORMAL_TEXT;
427 UINT32 CurrentLineOffset;
435 * If line is too long, expand the line buffers. Also increases
436 * Gbl_LineBufferSize.
438 if (i >= Gbl_LineBufferSize)
440 UtExpandLineBuffers ();
443 c = (char) getc (Handle);
448 case DT_START_QUOTED_STRING:
449 case DT_SLASH_ASTERISK_COMMENT:
451 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
458 /* Standalone EOF is OK */
466 * Received an EOF in the middle of a line. Terminate the
467 * line with a newline. The next call to this function will
468 * return a standalone EOF. Thus, the upper parsing software
469 * never has to deal with an EOF within a valid line (or
470 * the last line does not get tossed on the floor.)
473 State = DT_NORMAL_TEXT;
480 /* Normal text, insert char into line buffer */
482 Gbl_CurrentLineBuffer[i] = c;
486 State = DT_START_COMMENT;
490 State = DT_START_QUOTED_STRING;
491 LineNotAllBlanks = TRUE;
497 * The continuation char MUST be last char on this line.
498 * Otherwise, it will be assumed to be a valid ASL char.
500 State = DT_MERGE_LINES;
504 CurrentLineOffset = Gbl_NextLineOffset;
505 Gbl_NextLineOffset = (UINT32) ftell (Handle);
506 Gbl_CurrentLineNumber++;
509 * Exit if line is complete. Ignore empty lines (only \n)
510 * or lines that contain nothing but blanks.
512 if ((i != 0) && LineNotAllBlanks)
514 if ((i + 1) >= Gbl_LineBufferSize)
516 UtExpandLineBuffers ();
519 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
520 return (CurrentLineOffset);
523 /* Toss this line and start a new one */
526 LineNotAllBlanks = FALSE;
532 LineNotAllBlanks = TRUE;
540 case DT_START_QUOTED_STRING:
542 /* Insert raw chars until end of quoted string */
544 Gbl_CurrentLineBuffer[i] = c;
550 State = DT_NORMAL_TEXT;
554 State = DT_ESCAPE_SEQUENCE;
558 AcpiOsPrintf ("ERROR at line %u: Unterminated quoted string\n",
559 Gbl_CurrentLineNumber++);
560 State = DT_NORMAL_TEXT;
563 default: /* Get next character */
568 case DT_ESCAPE_SEQUENCE:
570 /* Just copy the escaped character. TBD: sufficient for table compiler? */
572 Gbl_CurrentLineBuffer[i] = c;
574 State = DT_START_QUOTED_STRING;
577 case DT_START_COMMENT:
579 /* Open comment if this character is an asterisk or slash */
584 State = DT_SLASH_ASTERISK_COMMENT;
588 State = DT_SLASH_SLASH_COMMENT;
591 default: /* Not a comment */
592 i++; /* Save the preceding slash */
593 if (i >= Gbl_LineBufferSize)
595 UtExpandLineBuffers ();
598 Gbl_CurrentLineBuffer[i] = c;
600 State = DT_NORMAL_TEXT;
605 case DT_SLASH_ASTERISK_COMMENT:
607 /* Ignore chars until an asterisk-slash is found */
612 Gbl_NextLineOffset = (UINT32) ftell (Handle);
613 Gbl_CurrentLineNumber++;
617 State = DT_END_COMMENT;
625 case DT_SLASH_SLASH_COMMENT:
627 /* Ignore chars until end-of-line */
631 /* We will exit via the NORMAL_TEXT path */
634 State = DT_NORMAL_TEXT;
640 /* End comment if this char is a slash */
645 State = DT_NORMAL_TEXT;
649 CurrentLineOffset = Gbl_NextLineOffset;
650 Gbl_NextLineOffset = (UINT32) ftell (Handle);
651 Gbl_CurrentLineNumber++;
655 /* Consume all adjacent asterisks */
659 State = DT_SLASH_ASTERISK_COMMENT;
669 * This is not a continuation backslash, it is a normal
670 * normal ASL backslash - for example: Scope(\_SB_)
672 i++; /* Keep the backslash that is already in the buffer */
675 State = DT_NORMAL_TEXT;
680 * This is a continuation line -- a backlash followed
681 * immediately by a newline. Insert a space between the
682 * lines (overwrite the backslash)
684 Gbl_CurrentLineBuffer[i] = ' ';
687 /* Ignore newline, this will merge the lines */
689 CurrentLineOffset = Gbl_NextLineOffset;
690 Gbl_NextLineOffset = (UINT32) ftell (Handle);
691 Gbl_CurrentLineNumber++;
692 State = DT_NORMAL_TEXT;
697 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
704 /******************************************************************************
706 * FUNCTION: DtScanFile
708 * PARAMETERS: Handle - Open file handle for the source file
710 * RETURN: Pointer to start of the constructed parse tree.
712 * DESCRIPTION: Scan source file, link all field names and values
713 * to the global parse tree: Gbl_FieldList
715 *****************************************************************************/
725 ACPI_FUNCTION_NAME (DtScanFile);
728 /* Get the file size */
730 Gbl_InputByteCount = DtGetFileSize (Handle);
732 Gbl_CurrentLineNumber = 0;
733 Gbl_CurrentLineOffset = 0;
734 Gbl_NextLineOffset = 0;
736 /* Scan line-by-line */
738 while ((Offset = DtGetNextLine (Handle)) != ASL_EOF)
740 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
741 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
743 Status = DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
744 if (Status == AE_NOT_FOUND)
750 /* Dump the parse tree if debug enabled */
752 DtDumpFieldList (Gbl_FieldList);
753 return (Gbl_FieldList);
761 /******************************************************************************
763 * FUNCTION: DtWriteBinary
765 * PARAMETERS: DT_WALK_CALLBACK
769 * DESCRIPTION: Write one subtable of a binary ACPI table
771 *****************************************************************************/
775 DT_SUBTABLE *Subtable,
780 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
784 /******************************************************************************
786 * FUNCTION: DtOutputBinary
792 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
794 *****************************************************************************/
798 DT_SUBTABLE *RootTable)
806 /* Walk the entire parse tree, emitting the binary data */
808 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
809 Gbl_TableLength = DtGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
817 /******************************************************************************
819 * FUNCTION: DtDumpBuffer
821 * PARAMETERS: FileID - Where to write buffer data
822 * Buffer - Buffer to dump
823 * Offset - Offset in current table
824 * Length - Buffer Length
828 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
830 * TBD: merge dump buffer routines
832 *****************************************************************************/
846 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
847 Offset, Offset, Length);
854 FlPrintFile (FileId, "%24s", "");
857 /* Print 16 hex chars */
863 /* Dump fill spaces */
865 FlPrintFile (FileId, " ");
870 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
874 FlPrintFile (FileId, " ");
875 for (j = 0; j < 16; j++)
879 FlPrintFile (FileId, "\n\n");
883 BufChar = Buffer[(ACPI_SIZE) i + j];
884 if (ACPI_IS_PRINT (BufChar))
886 FlPrintFile (FileId, "%c", BufChar);
890 FlPrintFile (FileId, ".");
894 /* Done with that line. */
896 FlPrintFile (FileId, "\n");
900 FlPrintFile (FileId, "\n\n");
904 /******************************************************************************
906 * FUNCTION: DtDumpFieldList
908 * PARAMETERS: Field - Root field
912 * DESCRIPTION: Dump the entire field list
914 *****************************************************************************/
921 if (!Gbl_DebugFlag || !Field)
926 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
927 "LineNo ByteOff NameCol Column TableOff "
928 "Flags %32s : %s\n\n", "Name", "Value");
931 DbgPrint (ASL_DEBUG_OUTPUT,
932 "%.08X %.08X %.08X %.08X %.08X %.08X %32s : %s\n",
933 Field->Line, Field->ByteOffset, Field->NameColumn,
934 Field->Column, Field->TableOffset, Field->Flags,
935 Field->Name, Field->Value);
940 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
944 /******************************************************************************
946 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
948 * PARAMETERS: DT_WALK_CALLBACK
952 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
953 * Tree - dump a subtable tree formatted by depth indentation.
955 *****************************************************************************/
959 DT_SUBTABLE *Subtable,
964 DbgPrint (ASL_DEBUG_OUTPUT,
965 "[%.04X] %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
966 Subtable->Depth, Subtable->Length, Subtable->TotalLength,
967 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
968 Subtable->Parent, Subtable->Child, Subtable->Peer);
973 DT_SUBTABLE *Subtable,
978 DbgPrint (ASL_DEBUG_OUTPUT,
979 "[%.04X] %*s%08X (%.02X) - (%.02X)\n",
980 Subtable->Depth, (4 * Subtable->Depth), " ",
981 Subtable, Subtable->Length, Subtable->TotalLength);
985 /******************************************************************************
987 * FUNCTION: DtDumpSubtableList
993 * DESCRIPTION: Dump the raw list of subtables with information, and also
994 * dump the subtable list in formatted tree format. Assists with
995 * the development of new table code.
997 *****************************************************************************/
1000 DtDumpSubtableList (
1004 if (!Gbl_DebugFlag || !Gbl_RootTable)
1009 DbgPrint (ASL_DEBUG_OUTPUT,
1011 "Depth Length TotalLen LenSize Flags "
1012 "This Parent Child Peer\n\n");
1013 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1015 DbgPrint (ASL_DEBUG_OUTPUT,
1016 "\nSubtable Tree: (Depth, Subtable, Length, TotalLength)\n\n");
1017 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1021 /******************************************************************************
1023 * FUNCTION: DtWriteFieldToListing
1025 * PARAMETERS: Buffer - Contains the compiled data
1026 * Field - Field node for the input line
1027 * Length - Length of the output data
1031 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1033 *****************************************************************************/
1036 DtWriteFieldToListing (
1044 if (!Gbl_ListingFlag || !Field)
1049 /* Dump the original source line */
1051 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1052 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1054 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1056 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1057 if (FileByte == '\n')
1063 /* Dump the line as parsed and represented internally */
1065 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1066 Field->Column-4, Field->Name, Field->Value);
1068 if (strlen (Field->Value) > 64)
1070 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1071 strlen (Field->Value));
1073 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1075 /* Dump the hex data that will be output for this field */
1077 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1081 /******************************************************************************
1083 * FUNCTION: DtWriteTableToListing
1089 * DESCRIPTION: Write the entire compiled table to the listing file
1092 *****************************************************************************/
1095 DtWriteTableToListing (
1101 if (!Gbl_ListingFlag)
1106 /* Read the entire table from the output file */
1108 Buffer = UtLocalCalloc (Gbl_TableLength);
1109 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1110 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1112 /* Dump the raw table data */
1114 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1116 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1117 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1118 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1120 AcpiOsRedirectOutput (stdout);