1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for data table compiler
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 (!strcmp (String, " "))
134 ReturnString = UtStringCacheCalloc (1);
135 return (ReturnString);
138 /* Setup pointers to start and end of input string */
141 End = String + 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);
185 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 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
361 Length = ACPI_PTR_DIFF (End, Start);
362 TmpValue = UtLocalCalloc (Length + 1);
364 strncpy (TmpValue, Start, Length);
365 Value = DtTrim (TmpValue);
366 ACPI_FREE (TmpValue);
368 /* Create a new field object only if we have a valid value field */
370 if ((Value && *Value) || IsNullString)
372 Field = UtFieldCacheCalloc ();
374 Field->Value = Value;
376 Field->ByteOffset = Offset;
377 Field->NameColumn = NameColumn;
378 Field->Column = Column;
379 Field->StringLength = Length;
383 /* Else -- Ignore this field, it has no valid data */
389 /******************************************************************************
391 * FUNCTION: DtGetNextLine
393 * PARAMETERS: Handle - Open file handle for the source file
395 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
397 * DESCRIPTION: Get the next valid source line. Removes all comments.
398 * Ignores empty lines.
400 * Handles both slash-asterisk and slash-slash comments.
401 * Also, quoted strings, but no escapes within.
403 * Line is returned in Gbl_CurrentLineBuffer.
404 * Line number in original file is returned in Gbl_CurrentLineNumber.
406 *****************************************************************************/
413 BOOLEAN LineNotAllBlanks = FALSE;
414 UINT32 State = DT_NORMAL_TEXT;
415 UINT32 CurrentLineOffset;
420 memset (Gbl_CurrentLineBuffer, 0, Gbl_LineBufferSize);
424 * If line is too long, expand the line buffers. Also increases
425 * Gbl_LineBufferSize.
427 if (i >= Gbl_LineBufferSize)
429 UtExpandLineBuffers ();
437 case DT_START_QUOTED_STRING:
438 case DT_SLASH_ASTERISK_COMMENT:
440 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
448 /* Standalone EOF is OK */
456 * Received an EOF in the middle of a line. Terminate the
457 * line with a newline. The next call to this function will
458 * return a standalone EOF. Thus, the upper parsing software
459 * never has to deal with an EOF within a valid line (or
460 * the last line does not get tossed on the floor.)
463 State = DT_NORMAL_TEXT;
470 /* Normal text, insert char into line buffer */
472 Gbl_CurrentLineBuffer[i] = (char) c;
477 State = DT_START_COMMENT;
482 State = DT_START_QUOTED_STRING;
483 LineNotAllBlanks = TRUE;
489 * The continuation char MUST be last char on this line.
490 * Otherwise, it will be assumed to be a valid ASL char.
492 State = DT_MERGE_LINES;
497 CurrentLineOffset = Gbl_NextLineOffset;
498 Gbl_NextLineOffset = (UINT32) ftell (Handle);
499 Gbl_CurrentLineNumber++;
502 * Exit if line is complete. Ignore empty lines (only \n)
503 * or lines that contain nothing but blanks.
505 if ((i != 0) && LineNotAllBlanks)
507 if ((i + 1) >= Gbl_LineBufferSize)
509 UtExpandLineBuffers ();
512 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
513 return (CurrentLineOffset);
516 /* Toss this line and start a new one */
519 LineNotAllBlanks = FALSE;
526 LineNotAllBlanks = TRUE;
534 case DT_START_QUOTED_STRING:
536 /* Insert raw chars until end of quoted string */
538 Gbl_CurrentLineBuffer[i] = (char) c;
545 State = DT_NORMAL_TEXT;
550 State = DT_ESCAPE_SEQUENCE;
555 if (!(Flags & DT_ALLOW_MULTILINE_QUOTES))
558 "ERROR at line %u: Unterminated quoted string\n",
559 Gbl_CurrentLineNumber++);
560 State = DT_NORMAL_TEXT;
564 default: /* Get next character */
570 case DT_ESCAPE_SEQUENCE:
572 /* Just copy the escaped character. TBD: sufficient for table compiler? */
574 Gbl_CurrentLineBuffer[i] = (char) c;
576 State = DT_START_QUOTED_STRING;
579 case DT_START_COMMENT:
581 /* Open comment if this character is an asterisk or slash */
587 State = DT_SLASH_ASTERISK_COMMENT;
592 State = DT_SLASH_SLASH_COMMENT;
595 default: /* Not a comment */
597 i++; /* Save the preceding slash */
598 if (i >= Gbl_LineBufferSize)
600 UtExpandLineBuffers ();
603 Gbl_CurrentLineBuffer[i] = (char) c;
605 State = DT_NORMAL_TEXT;
610 case DT_SLASH_ASTERISK_COMMENT:
612 /* Ignore chars until an asterisk-slash is found */
618 Gbl_NextLineOffset = (UINT32) ftell (Handle);
619 Gbl_CurrentLineNumber++;
624 State = DT_END_COMMENT;
633 case DT_SLASH_SLASH_COMMENT:
635 /* Ignore chars until end-of-line */
639 /* We will exit via the NORMAL_TEXT path */
642 State = DT_NORMAL_TEXT;
648 /* End comment if this char is a slash */
654 State = DT_NORMAL_TEXT;
659 CurrentLineOffset = Gbl_NextLineOffset;
660 Gbl_NextLineOffset = (UINT32) ftell (Handle);
661 Gbl_CurrentLineNumber++;
666 /* Consume all adjacent asterisks */
671 State = DT_SLASH_ASTERISK_COMMENT;
681 * This is not a continuation backslash, it is a normal
682 * normal ASL backslash - for example: Scope(\_SB_)
684 i++; /* Keep the backslash that is already in the buffer */
687 State = DT_NORMAL_TEXT;
692 * This is a continuation line -- a backlash followed
693 * immediately by a newline. Insert a space between the
694 * lines (overwrite the backslash)
696 Gbl_CurrentLineBuffer[i] = ' ';
699 /* Ignore newline, this will merge the lines */
701 CurrentLineOffset = Gbl_NextLineOffset;
702 Gbl_NextLineOffset = (UINT32) ftell (Handle);
703 Gbl_CurrentLineNumber++;
704 State = DT_NORMAL_TEXT;
710 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
717 /******************************************************************************
719 * FUNCTION: DtScanFile
721 * PARAMETERS: Handle - Open file handle for the source file
723 * RETURN: Pointer to start of the constructed parse tree.
725 * DESCRIPTION: Scan source file, link all field names and values
726 * to the global parse tree: Gbl_FieldList
728 *****************************************************************************/
738 ACPI_FUNCTION_NAME (DtScanFile);
741 /* Get the file size */
743 Gbl_InputByteCount = CmGetFileSize (Handle);
744 if (Gbl_InputByteCount == ACPI_UINT32_MAX)
749 Gbl_CurrentLineNumber = 0;
750 Gbl_CurrentLineOffset = 0;
751 Gbl_NextLineOffset = 0;
753 /* Scan line-by-line */
755 while ((Offset = DtGetNextLine (Handle, 0)) != ASL_EOF)
757 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
758 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
760 Status = DtParseLine (Gbl_CurrentLineBuffer,
761 Gbl_CurrentLineNumber, Offset);
762 if (Status == AE_NOT_FOUND)
768 /* Dump the parse tree if debug enabled */
770 DtDumpFieldList (Gbl_FieldList);
771 return (Gbl_FieldList);
779 /******************************************************************************
781 * FUNCTION: DtWriteBinary
783 * PARAMETERS: DT_WALK_CALLBACK
787 * DESCRIPTION: Write one subtable of a binary ACPI table
789 *****************************************************************************/
793 DT_SUBTABLE *Subtable,
798 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
802 /******************************************************************************
804 * FUNCTION: DtOutputBinary
810 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
812 *****************************************************************************/
816 DT_SUBTABLE *RootTable)
824 /* Walk the entire parse tree, emitting the binary data */
826 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
828 Gbl_TableLength = CmGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
829 if (Gbl_TableLength == ACPI_UINT32_MAX)
840 /******************************************************************************
842 * FUNCTION: DtDumpBuffer
844 * PARAMETERS: FileID - Where to write buffer data
845 * Buffer - Buffer to dump
846 * Offset - Offset in current table
847 * Length - Buffer Length
851 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
853 * TBD: merge dump buffer routines
855 *****************************************************************************/
869 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
870 Offset, Offset, Length);
877 FlPrintFile (FileId, "%24s", "");
880 /* Print 16 hex chars */
886 /* Dump fill spaces */
888 FlPrintFile (FileId, " ");
893 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
897 FlPrintFile (FileId, " ");
898 for (j = 0; j < 16; j++)
902 FlPrintFile (FileId, "\n\n");
906 BufChar = Buffer[(ACPI_SIZE) i + j];
907 if (isprint (BufChar))
909 FlPrintFile (FileId, "%c", BufChar);
913 FlPrintFile (FileId, ".");
917 /* Done with that line. */
919 FlPrintFile (FileId, "\n");
923 FlPrintFile (FileId, "\n\n");
927 /******************************************************************************
929 * FUNCTION: DtDumpFieldList
931 * PARAMETERS: Field - Root field
935 * DESCRIPTION: Dump the entire field list
937 *****************************************************************************/
944 if (!Gbl_DebugFlag || !Field)
949 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
950 "LineNo ByteOff NameCol Column TableOff "
951 "Flags %32s : %s\n\n", "Name", "Value");
955 DbgPrint (ASL_DEBUG_OUTPUT,
956 "%.08X %.08X %.08X %.08X %.08X %2.2X %32s : %s\n",
957 Field->Line, Field->ByteOffset, Field->NameColumn,
958 Field->Column, Field->TableOffset, Field->Flags,
959 Field->Name, Field->Value);
964 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
968 /******************************************************************************
970 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
972 * PARAMETERS: DT_WALK_CALLBACK
976 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
977 * Tree - dump a subtable tree formatted by depth indentation.
979 *****************************************************************************/
983 DT_SUBTABLE *Subtable,
988 DbgPrint (ASL_DEBUG_OUTPUT,
989 "[%.04X] %24s %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
990 Subtable->Depth, Subtable->Name, Subtable->Length, Subtable->TotalLength,
991 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
992 Subtable->Parent, Subtable->Child, Subtable->Peer);
997 DT_SUBTABLE *Subtable,
1002 DbgPrint (ASL_DEBUG_OUTPUT,
1003 "[%.04X] %24s %*s%08X (%.02X) - (%.02X)\n",
1004 Subtable->Depth, Subtable->Name, (4 * Subtable->Depth), " ",
1005 Subtable, Subtable->Length, Subtable->TotalLength);
1009 /******************************************************************************
1011 * FUNCTION: DtDumpSubtableList
1017 * DESCRIPTION: Dump the raw list of subtables with information, and also
1018 * dump the subtable list in formatted tree format. Assists with
1019 * the development of new table code.
1021 *****************************************************************************/
1024 DtDumpSubtableList (
1028 if (!Gbl_DebugFlag || !Gbl_RootTable)
1033 DbgPrint (ASL_DEBUG_OUTPUT,
1035 "Depth Name Length TotalLen LenSize Flags "
1036 "This Parent Child Peer\n\n");
1037 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1039 DbgPrint (ASL_DEBUG_OUTPUT,
1040 "\nSubtable Tree: (Depth, Name, Subtable, Length, TotalLength)\n\n");
1041 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1043 DbgPrint (ASL_DEBUG_OUTPUT, "\n");
1047 /******************************************************************************
1049 * FUNCTION: DtWriteFieldToListing
1051 * PARAMETERS: Buffer - Contains the compiled data
1052 * Field - Field node for the input line
1053 * Length - Length of the output data
1057 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1059 *****************************************************************************/
1062 DtWriteFieldToListing (
1070 if (!Gbl_ListingFlag || !Field)
1075 /* Dump the original source line */
1077 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1078 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1080 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1082 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1083 if (FileByte == '\n')
1089 /* Dump the line as parsed and represented internally */
1091 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1092 Field->Column-4, Field->Name, Field->Value);
1094 if (strlen (Field->Value) > 64)
1096 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1097 strlen (Field->Value));
1100 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1102 /* Dump the hex data that will be output for this field */
1104 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1108 /******************************************************************************
1110 * FUNCTION: DtWriteTableToListing
1116 * DESCRIPTION: Write the entire compiled table to the listing file
1119 *****************************************************************************/
1122 DtWriteTableToListing (
1128 if (!Gbl_ListingFlag)
1133 /* Read the entire table from the output file */
1135 Buffer = UtLocalCalloc (Gbl_TableLength);
1136 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1137 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1139 /* Dump the raw table data */
1141 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1143 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1144 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1145 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1147 AcpiOsRedirectOutput (stdout);