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.
44 #include "aslcompiler.h"
45 #include "dtcompiler.h"
48 #define _COMPONENT DT_COMPILER
49 ACPI_MODULE_NAME ("dtio")
52 /* Local prototypes */
70 DT_SUBTABLE *Subtable,
83 DT_SUBTABLE *Subtable,
89 DT_SUBTABLE *Subtable,
94 /* States for DtGetNextLine */
96 #define DT_NORMAL_TEXT 0
97 #define DT_START_QUOTED_STRING 1
98 #define DT_START_COMMENT 2
99 #define DT_SLASH_ASTERISK_COMMENT 3
100 #define DT_SLASH_SLASH_COMMENT 4
101 #define DT_END_COMMENT 5
102 #define DT_MERGE_LINES 6
103 #define DT_ESCAPE_SEQUENCE 7
105 static UINT32 Gbl_NextLineOffset;
108 /******************************************************************************
112 * PARAMETERS: String - Current source code line to trim
114 * RETURN: Trimmed line. Must be freed by caller.
116 * DESCRIPTION: Trim left and right spaces
118 *****************************************************************************/
130 /* Skip lines that start with a space */
132 if (!ACPI_STRCMP (String, " "))
134 ReturnString = UtStringCacheCalloc (1);
135 return (ReturnString);
138 /* Setup pointers to start and end of input string */
141 End = String + ACPI_STRLEN (String) - 1;
143 /* Find first non-whitespace character */
145 while ((Start <= End) && ((*Start == ' ') || (*Start == '\t')))
150 /* Find last non-space character */
154 if (*End == '\r' || *End == '\n')
168 /* Remove any quotes around the string */
179 /* Create the trimmed return string */
181 Length = ACPI_PTR_DIFF (End, Start) + 1;
182 ReturnString = UtStringCacheCalloc (Length + 1);
183 if (ACPI_STRLEN (Start))
185 ACPI_STRNCPY (ReturnString, Start, Length);
188 ReturnString[Length] = 0;
189 return (ReturnString);
193 /******************************************************************************
195 * FUNCTION: DtLinkField
197 * PARAMETERS: Field - New field object to link
201 * DESCRIPTION: Link one field name and value to the list
203 *****************************************************************************/
213 Prev = Next = Gbl_FieldList;
227 Gbl_FieldList = Field;
232 /******************************************************************************
234 * FUNCTION: DtParseLine
236 * PARAMETERS: LineBuffer - Current source code line
237 * Line - Current line number in the source
238 * Offset - Current byte offset of the line
242 * DESCRIPTION: Parse one source line
244 *****************************************************************************/
263 BOOLEAN IsNullString = FALSE;
271 /* All lines after "Raw Table Data" are ingored */
273 if (strstr (LineBuffer, ACPI_RAW_TABLE_DATA_HEADER))
275 return (AE_NOT_FOUND);
278 Colon = strchr (LineBuffer, ':');
287 while (Start < Colon)
291 /* Found left bracket, go to the right bracket */
293 while (Start < Colon && *Start != ']')
298 else if (*Start != ' ')
307 * There are two column values. One for the field name,
308 * and one for the field value.
310 Column = ACPI_PTR_DIFF (Colon, LineBuffer) + 3;
311 NameColumn = ACPI_PTR_DIFF (Start, LineBuffer) + 1;
313 Length = ACPI_PTR_DIFF (End, Start);
315 TmpName = UtLocalCalloc (Length + 1);
316 ACPI_STRNCPY (TmpName, Start, Length);
317 Name = DtTrim (TmpName);
320 Start = End = (Colon + 1);
323 /* Found left quotation, go to the right quotation and break */
329 /* Check for an explicit null string */
335 while (*End && (*End != '"'))
345 * Special "comment" fields at line end, ignore them.
346 * Note: normal slash-slash and slash-asterisk comments are
347 * stripped already by the DtGetNextLine parser.
349 * TBD: Perhaps DtGetNextLine should parse the following type
360 Length = ACPI_PTR_DIFF (End, Start);
361 TmpValue = UtLocalCalloc (Length + 1);
363 ACPI_STRNCPY (TmpValue, Start, Length);
364 Value = DtTrim (TmpValue);
365 ACPI_FREE (TmpValue);
367 /* Create a new field object only if we have a valid value field */
369 if ((Value && *Value) || IsNullString)
371 Field = UtFieldCacheCalloc ();
373 Field->Value = Value;
375 Field->ByteOffset = Offset;
376 Field->NameColumn = NameColumn;
377 Field->Column = Column;
381 /* Else -- Ignore this field, it has no valid data */
387 /******************************************************************************
389 * FUNCTION: DtGetNextLine
391 * PARAMETERS: Handle - Open file handle for the source file
393 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
395 * DESCRIPTION: Get the next valid source line. Removes all comments.
396 * Ignores empty lines.
398 * Handles both slash-asterisk and slash-slash comments.
399 * Also, quoted strings, but no escapes within.
401 * Line is returned in Gbl_CurrentLineBuffer.
402 * Line number in original file is returned in Gbl_CurrentLineNumber.
404 *****************************************************************************/
410 BOOLEAN LineNotAllBlanks = FALSE;
411 UINT32 State = DT_NORMAL_TEXT;
412 UINT32 CurrentLineOffset;
420 * If line is too long, expand the line buffers. Also increases
421 * Gbl_LineBufferSize.
423 if (i >= Gbl_LineBufferSize)
425 UtExpandLineBuffers ();
433 case DT_START_QUOTED_STRING:
434 case DT_SLASH_ASTERISK_COMMENT:
436 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
444 /* Standalone EOF is OK */
452 * Received an EOF in the middle of a line. Terminate the
453 * line with a newline. The next call to this function will
454 * return a standalone EOF. Thus, the upper parsing software
455 * never has to deal with an EOF within a valid line (or
456 * the last line does not get tossed on the floor.)
459 State = DT_NORMAL_TEXT;
466 /* Normal text, insert char into line buffer */
468 Gbl_CurrentLineBuffer[i] = (char) c;
473 State = DT_START_COMMENT;
478 State = DT_START_QUOTED_STRING;
479 LineNotAllBlanks = TRUE;
485 * The continuation char MUST be last char on this line.
486 * Otherwise, it will be assumed to be a valid ASL char.
488 State = DT_MERGE_LINES;
493 CurrentLineOffset = Gbl_NextLineOffset;
494 Gbl_NextLineOffset = (UINT32) ftell (Handle);
495 Gbl_CurrentLineNumber++;
498 * Exit if line is complete. Ignore empty lines (only \n)
499 * or lines that contain nothing but blanks.
501 if ((i != 0) && LineNotAllBlanks)
503 if ((i + 1) >= Gbl_LineBufferSize)
505 UtExpandLineBuffers ();
508 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
509 return (CurrentLineOffset);
512 /* Toss this line and start a new one */
515 LineNotAllBlanks = FALSE;
522 LineNotAllBlanks = TRUE;
530 case DT_START_QUOTED_STRING:
532 /* Insert raw chars until end of quoted string */
534 Gbl_CurrentLineBuffer[i] = (char) c;
541 State = DT_NORMAL_TEXT;
546 State = DT_ESCAPE_SEQUENCE;
551 AcpiOsPrintf ("ERROR at line %u: Unterminated quoted string\n",
552 Gbl_CurrentLineNumber++);
553 State = DT_NORMAL_TEXT;
556 default: /* Get next character */
562 case DT_ESCAPE_SEQUENCE:
564 /* Just copy the escaped character. TBD: sufficient for table compiler? */
566 Gbl_CurrentLineBuffer[i] = (char) c;
568 State = DT_START_QUOTED_STRING;
571 case DT_START_COMMENT:
573 /* Open comment if this character is an asterisk or slash */
579 State = DT_SLASH_ASTERISK_COMMENT;
584 State = DT_SLASH_SLASH_COMMENT;
587 default: /* Not a comment */
589 i++; /* Save the preceding slash */
590 if (i >= Gbl_LineBufferSize)
592 UtExpandLineBuffers ();
595 Gbl_CurrentLineBuffer[i] = (char) c;
597 State = DT_NORMAL_TEXT;
602 case DT_SLASH_ASTERISK_COMMENT:
604 /* Ignore chars until an asterisk-slash is found */
610 Gbl_NextLineOffset = (UINT32) ftell (Handle);
611 Gbl_CurrentLineNumber++;
616 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 */
646 State = DT_NORMAL_TEXT;
651 CurrentLineOffset = Gbl_NextLineOffset;
652 Gbl_NextLineOffset = (UINT32) ftell (Handle);
653 Gbl_CurrentLineNumber++;
658 /* Consume all adjacent asterisks */
663 State = DT_SLASH_ASTERISK_COMMENT;
673 * This is not a continuation backslash, it is a normal
674 * normal ASL backslash - for example: Scope(\_SB_)
676 i++; /* Keep the backslash that is already in the buffer */
679 State = DT_NORMAL_TEXT;
684 * This is a continuation line -- a backlash followed
685 * immediately by a newline. Insert a space between the
686 * lines (overwrite the backslash)
688 Gbl_CurrentLineBuffer[i] = ' ';
691 /* Ignore newline, this will merge the lines */
693 CurrentLineOffset = Gbl_NextLineOffset;
694 Gbl_NextLineOffset = (UINT32) ftell (Handle);
695 Gbl_CurrentLineNumber++;
696 State = DT_NORMAL_TEXT;
702 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
709 /******************************************************************************
711 * FUNCTION: DtScanFile
713 * PARAMETERS: Handle - Open file handle for the source file
715 * RETURN: Pointer to start of the constructed parse tree.
717 * DESCRIPTION: Scan source file, link all field names and values
718 * to the global parse tree: Gbl_FieldList
720 *****************************************************************************/
730 ACPI_FUNCTION_NAME (DtScanFile);
733 /* Get the file size */
735 Gbl_InputByteCount = CmGetFileSize (Handle);
736 if (Gbl_InputByteCount == ACPI_UINT32_MAX)
741 Gbl_CurrentLineNumber = 0;
742 Gbl_CurrentLineOffset = 0;
743 Gbl_NextLineOffset = 0;
745 /* Scan line-by-line */
747 while ((Offset = DtGetNextLine (Handle)) != ASL_EOF)
749 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
750 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
752 Status = DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
753 if (Status == AE_NOT_FOUND)
759 /* Dump the parse tree if debug enabled */
761 DtDumpFieldList (Gbl_FieldList);
762 return (Gbl_FieldList);
770 /******************************************************************************
772 * FUNCTION: DtWriteBinary
774 * PARAMETERS: DT_WALK_CALLBACK
778 * DESCRIPTION: Write one subtable of a binary ACPI table
780 *****************************************************************************/
784 DT_SUBTABLE *Subtable,
789 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
793 /******************************************************************************
795 * FUNCTION: DtOutputBinary
801 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
803 *****************************************************************************/
807 DT_SUBTABLE *RootTable)
815 /* Walk the entire parse tree, emitting the binary data */
817 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
819 Gbl_TableLength = CmGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
820 if (Gbl_TableLength == ACPI_UINT32_MAX)
831 /******************************************************************************
833 * FUNCTION: DtDumpBuffer
835 * PARAMETERS: FileID - Where to write buffer data
836 * Buffer - Buffer to dump
837 * Offset - Offset in current table
838 * Length - Buffer Length
842 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
844 * TBD: merge dump buffer routines
846 *****************************************************************************/
860 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
861 Offset, Offset, Length);
868 FlPrintFile (FileId, "%24s", "");
871 /* Print 16 hex chars */
877 /* Dump fill spaces */
879 FlPrintFile (FileId, " ");
884 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
888 FlPrintFile (FileId, " ");
889 for (j = 0; j < 16; j++)
893 FlPrintFile (FileId, "\n\n");
897 BufChar = Buffer[(ACPI_SIZE) i + j];
898 if (ACPI_IS_PRINT (BufChar))
900 FlPrintFile (FileId, "%c", BufChar);
904 FlPrintFile (FileId, ".");
908 /* Done with that line. */
910 FlPrintFile (FileId, "\n");
914 FlPrintFile (FileId, "\n\n");
918 /******************************************************************************
920 * FUNCTION: DtDumpFieldList
922 * PARAMETERS: Field - Root field
926 * DESCRIPTION: Dump the entire field list
928 *****************************************************************************/
935 if (!Gbl_DebugFlag || !Field)
940 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
941 "LineNo ByteOff NameCol Column TableOff "
942 "Flags %32s : %s\n\n", "Name", "Value");
945 DbgPrint (ASL_DEBUG_OUTPUT,
946 "%.08X %.08X %.08X %.08X %.08X %.08X %32s : %s\n",
947 Field->Line, Field->ByteOffset, Field->NameColumn,
948 Field->Column, Field->TableOffset, Field->Flags,
949 Field->Name, Field->Value);
954 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
958 /******************************************************************************
960 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
962 * PARAMETERS: DT_WALK_CALLBACK
966 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
967 * Tree - dump a subtable tree formatted by depth indentation.
969 *****************************************************************************/
973 DT_SUBTABLE *Subtable,
978 DbgPrint (ASL_DEBUG_OUTPUT,
979 "[%.04X] %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
980 Subtable->Depth, Subtable->Length, Subtable->TotalLength,
981 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
982 Subtable->Parent, Subtable->Child, Subtable->Peer);
987 DT_SUBTABLE *Subtable,
992 DbgPrint (ASL_DEBUG_OUTPUT,
993 "[%.04X] %*s%08X (%.02X) - (%.02X)\n",
994 Subtable->Depth, (4 * Subtable->Depth), " ",
995 Subtable, Subtable->Length, Subtable->TotalLength);
999 /******************************************************************************
1001 * FUNCTION: DtDumpSubtableList
1007 * DESCRIPTION: Dump the raw list of subtables with information, and also
1008 * dump the subtable list in formatted tree format. Assists with
1009 * the development of new table code.
1011 *****************************************************************************/
1014 DtDumpSubtableList (
1018 if (!Gbl_DebugFlag || !Gbl_RootTable)
1023 DbgPrint (ASL_DEBUG_OUTPUT,
1025 "Depth Length TotalLen LenSize Flags "
1026 "This Parent Child Peer\n\n");
1027 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1029 DbgPrint (ASL_DEBUG_OUTPUT,
1030 "\nSubtable Tree: (Depth, Subtable, Length, TotalLength)\n\n");
1031 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1033 DbgPrint (ASL_DEBUG_OUTPUT, "\n");
1037 /******************************************************************************
1039 * FUNCTION: DtWriteFieldToListing
1041 * PARAMETERS: Buffer - Contains the compiled data
1042 * Field - Field node for the input line
1043 * Length - Length of the output data
1047 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1049 *****************************************************************************/
1052 DtWriteFieldToListing (
1060 if (!Gbl_ListingFlag || !Field)
1065 /* Dump the original source line */
1067 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1068 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1070 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1072 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1073 if (FileByte == '\n')
1079 /* Dump the line as parsed and represented internally */
1081 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1082 Field->Column-4, Field->Name, Field->Value);
1084 if (strlen (Field->Value) > 64)
1086 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1087 strlen (Field->Value));
1089 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1091 /* Dump the hex data that will be output for this field */
1093 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1097 /******************************************************************************
1099 * FUNCTION: DtWriteTableToListing
1105 * DESCRIPTION: Write the entire compiled table to the listing file
1108 *****************************************************************************/
1111 DtWriteTableToListing (
1117 if (!Gbl_ListingFlag)
1122 /* Read the entire table from the output file */
1124 Buffer = UtLocalCalloc (Gbl_TableLength);
1125 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1126 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1128 /* Dump the raw table data */
1130 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1132 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1133 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1134 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1136 AcpiOsRedirectOutput (stdout);