1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for data table compiler
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
46 #include <contrib/dev/acpica/include/acapps.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;
378 Field->StringLength = Length;
382 /* Else -- Ignore this field, it has no valid data */
388 /******************************************************************************
390 * FUNCTION: DtGetNextLine
392 * PARAMETERS: Handle - Open file handle for the source file
394 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
396 * DESCRIPTION: Get the next valid source line. Removes all comments.
397 * Ignores empty lines.
399 * Handles both slash-asterisk and slash-slash comments.
400 * Also, quoted strings, but no escapes within.
402 * Line is returned in Gbl_CurrentLineBuffer.
403 * Line number in original file is returned in Gbl_CurrentLineNumber.
405 *****************************************************************************/
411 BOOLEAN LineNotAllBlanks = FALSE;
412 UINT32 State = DT_NORMAL_TEXT;
413 UINT32 CurrentLineOffset;
418 ACPI_MEMSET (Gbl_CurrentLineBuffer, 0, Gbl_LineBufferSize);
422 * If line is too long, expand the line buffers. Also increases
423 * Gbl_LineBufferSize.
425 if (i >= Gbl_LineBufferSize)
427 UtExpandLineBuffers ();
435 case DT_START_QUOTED_STRING:
436 case DT_SLASH_ASTERISK_COMMENT:
438 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
446 /* Standalone EOF is OK */
454 * Received an EOF in the middle of a line. Terminate the
455 * line with a newline. The next call to this function will
456 * return a standalone EOF. Thus, the upper parsing software
457 * never has to deal with an EOF within a valid line (or
458 * the last line does not get tossed on the floor.)
461 State = DT_NORMAL_TEXT;
468 /* Normal text, insert char into line buffer */
470 Gbl_CurrentLineBuffer[i] = (char) c;
475 State = DT_START_COMMENT;
480 State = DT_START_QUOTED_STRING;
481 LineNotAllBlanks = TRUE;
487 * The continuation char MUST be last char on this line.
488 * Otherwise, it will be assumed to be a valid ASL char.
490 State = DT_MERGE_LINES;
495 CurrentLineOffset = Gbl_NextLineOffset;
496 Gbl_NextLineOffset = (UINT32) ftell (Handle);
497 Gbl_CurrentLineNumber++;
500 * Exit if line is complete. Ignore empty lines (only \n)
501 * or lines that contain nothing but blanks.
503 if ((i != 0) && LineNotAllBlanks)
505 if ((i + 1) >= Gbl_LineBufferSize)
507 UtExpandLineBuffers ();
510 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
511 return (CurrentLineOffset);
514 /* Toss this line and start a new one */
517 LineNotAllBlanks = FALSE;
524 LineNotAllBlanks = TRUE;
532 case DT_START_QUOTED_STRING:
534 /* Insert raw chars until end of quoted string */
536 Gbl_CurrentLineBuffer[i] = (char) c;
543 State = DT_NORMAL_TEXT;
548 State = DT_ESCAPE_SEQUENCE;
553 AcpiOsPrintf ("ERROR at line %u: Unterminated quoted string\n",
554 Gbl_CurrentLineNumber++);
555 State = DT_NORMAL_TEXT;
558 default: /* Get next character */
564 case DT_ESCAPE_SEQUENCE:
566 /* Just copy the escaped character. TBD: sufficient for table compiler? */
568 Gbl_CurrentLineBuffer[i] = (char) c;
570 State = DT_START_QUOTED_STRING;
573 case DT_START_COMMENT:
575 /* Open comment if this character is an asterisk or slash */
581 State = DT_SLASH_ASTERISK_COMMENT;
586 State = DT_SLASH_SLASH_COMMENT;
589 default: /* Not a comment */
591 i++; /* Save the preceding slash */
592 if (i >= Gbl_LineBufferSize)
594 UtExpandLineBuffers ();
597 Gbl_CurrentLineBuffer[i] = (char) c;
599 State = DT_NORMAL_TEXT;
604 case DT_SLASH_ASTERISK_COMMENT:
606 /* Ignore chars until an asterisk-slash is found */
612 Gbl_NextLineOffset = (UINT32) ftell (Handle);
613 Gbl_CurrentLineNumber++;
618 State = DT_END_COMMENT;
627 case DT_SLASH_SLASH_COMMENT:
629 /* Ignore chars until end-of-line */
633 /* We will exit via the NORMAL_TEXT path */
636 State = DT_NORMAL_TEXT;
642 /* End comment if this char is a slash */
648 State = DT_NORMAL_TEXT;
653 CurrentLineOffset = Gbl_NextLineOffset;
654 Gbl_NextLineOffset = (UINT32) ftell (Handle);
655 Gbl_CurrentLineNumber++;
660 /* Consume all adjacent asterisks */
665 State = DT_SLASH_ASTERISK_COMMENT;
675 * This is not a continuation backslash, it is a normal
676 * normal ASL backslash - for example: Scope(\_SB_)
678 i++; /* Keep the backslash that is already in the buffer */
681 State = DT_NORMAL_TEXT;
686 * This is a continuation line -- a backlash followed
687 * immediately by a newline. Insert a space between the
688 * lines (overwrite the backslash)
690 Gbl_CurrentLineBuffer[i] = ' ';
693 /* Ignore newline, this will merge the lines */
695 CurrentLineOffset = Gbl_NextLineOffset;
696 Gbl_NextLineOffset = (UINT32) ftell (Handle);
697 Gbl_CurrentLineNumber++;
698 State = DT_NORMAL_TEXT;
704 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
711 /******************************************************************************
713 * FUNCTION: DtScanFile
715 * PARAMETERS: Handle - Open file handle for the source file
717 * RETURN: Pointer to start of the constructed parse tree.
719 * DESCRIPTION: Scan source file, link all field names and values
720 * to the global parse tree: Gbl_FieldList
722 *****************************************************************************/
732 ACPI_FUNCTION_NAME (DtScanFile);
735 /* Get the file size */
737 Gbl_InputByteCount = CmGetFileSize (Handle);
738 if (Gbl_InputByteCount == ACPI_UINT32_MAX)
743 Gbl_CurrentLineNumber = 0;
744 Gbl_CurrentLineOffset = 0;
745 Gbl_NextLineOffset = 0;
747 /* Scan line-by-line */
749 while ((Offset = DtGetNextLine (Handle)) != ASL_EOF)
751 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
752 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
754 Status = DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
755 if (Status == AE_NOT_FOUND)
761 /* Dump the parse tree if debug enabled */
763 DtDumpFieldList (Gbl_FieldList);
764 return (Gbl_FieldList);
772 /******************************************************************************
774 * FUNCTION: DtWriteBinary
776 * PARAMETERS: DT_WALK_CALLBACK
780 * DESCRIPTION: Write one subtable of a binary ACPI table
782 *****************************************************************************/
786 DT_SUBTABLE *Subtable,
791 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
795 /******************************************************************************
797 * FUNCTION: DtOutputBinary
803 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
805 *****************************************************************************/
809 DT_SUBTABLE *RootTable)
817 /* Walk the entire parse tree, emitting the binary data */
819 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
821 Gbl_TableLength = CmGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
822 if (Gbl_TableLength == ACPI_UINT32_MAX)
833 /******************************************************************************
835 * FUNCTION: DtDumpBuffer
837 * PARAMETERS: FileID - Where to write buffer data
838 * Buffer - Buffer to dump
839 * Offset - Offset in current table
840 * Length - Buffer Length
844 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
846 * TBD: merge dump buffer routines
848 *****************************************************************************/
862 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
863 Offset, Offset, Length);
870 FlPrintFile (FileId, "%24s", "");
873 /* Print 16 hex chars */
879 /* Dump fill spaces */
881 FlPrintFile (FileId, " ");
886 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
890 FlPrintFile (FileId, " ");
891 for (j = 0; j < 16; j++)
895 FlPrintFile (FileId, "\n\n");
899 BufChar = Buffer[(ACPI_SIZE) i + j];
900 if (ACPI_IS_PRINT (BufChar))
902 FlPrintFile (FileId, "%c", BufChar);
906 FlPrintFile (FileId, ".");
910 /* Done with that line. */
912 FlPrintFile (FileId, "\n");
916 FlPrintFile (FileId, "\n\n");
920 /******************************************************************************
922 * FUNCTION: DtDumpFieldList
924 * PARAMETERS: Field - Root field
928 * DESCRIPTION: Dump the entire field list
930 *****************************************************************************/
937 if (!Gbl_DebugFlag || !Field)
942 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
943 "LineNo ByteOff NameCol Column TableOff "
944 "Flags %32s : %s\n\n", "Name", "Value");
947 DbgPrint (ASL_DEBUG_OUTPUT,
948 "%.08X %.08X %.08X %.08X %.08X %2.2X %32s : %s\n",
949 Field->Line, Field->ByteOffset, Field->NameColumn,
950 Field->Column, Field->TableOffset, Field->Flags,
951 Field->Name, Field->Value);
956 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
960 /******************************************************************************
962 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
964 * PARAMETERS: DT_WALK_CALLBACK
968 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
969 * Tree - dump a subtable tree formatted by depth indentation.
971 *****************************************************************************/
975 DT_SUBTABLE *Subtable,
980 DbgPrint (ASL_DEBUG_OUTPUT,
981 "[%.04X] %24s %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
982 Subtable->Depth, Subtable->Name, Subtable->Length, Subtable->TotalLength,
983 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
984 Subtable->Parent, Subtable->Child, Subtable->Peer);
989 DT_SUBTABLE *Subtable,
994 DbgPrint (ASL_DEBUG_OUTPUT,
995 "[%.04X] %24s %*s%08X (%.02X) - (%.02X)\n",
996 Subtable->Depth, Subtable->Name, (4 * Subtable->Depth), " ",
997 Subtable, Subtable->Length, Subtable->TotalLength);
1001 /******************************************************************************
1003 * FUNCTION: DtDumpSubtableList
1009 * DESCRIPTION: Dump the raw list of subtables with information, and also
1010 * dump the subtable list in formatted tree format. Assists with
1011 * the development of new table code.
1013 *****************************************************************************/
1016 DtDumpSubtableList (
1020 if (!Gbl_DebugFlag || !Gbl_RootTable)
1025 DbgPrint (ASL_DEBUG_OUTPUT,
1027 "Depth Name Length TotalLen LenSize Flags "
1028 "This Parent Child Peer\n\n");
1029 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1031 DbgPrint (ASL_DEBUG_OUTPUT,
1032 "\nSubtable Tree: (Depth, Name, Subtable, Length, TotalLength)\n\n");
1033 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1035 DbgPrint (ASL_DEBUG_OUTPUT, "\n");
1039 /******************************************************************************
1041 * FUNCTION: DtWriteFieldToListing
1043 * PARAMETERS: Buffer - Contains the compiled data
1044 * Field - Field node for the input line
1045 * Length - Length of the output data
1049 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1051 *****************************************************************************/
1054 DtWriteFieldToListing (
1062 if (!Gbl_ListingFlag || !Field)
1067 /* Dump the original source line */
1069 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1070 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1072 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1074 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1075 if (FileByte == '\n')
1081 /* Dump the line as parsed and represented internally */
1083 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1084 Field->Column-4, Field->Name, Field->Value);
1086 if (strlen (Field->Value) > 64)
1088 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1089 strlen (Field->Value));
1091 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1093 /* Dump the hex data that will be output for this field */
1095 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1099 /******************************************************************************
1101 * FUNCTION: DtWriteTableToListing
1107 * DESCRIPTION: Write the entire compiled table to the listing file
1110 *****************************************************************************/
1113 DtWriteTableToListing (
1119 if (!Gbl_ListingFlag)
1124 /* Read the entire table from the output file */
1126 Buffer = UtLocalCalloc (Gbl_TableLength);
1127 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1128 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1130 /* Dump the raw table data */
1132 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1134 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1135 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1136 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1138 AcpiOsRedirectOutput (stdout);