1 /******************************************************************************
3 * Module Name: dtio.c - File I/O support for data table compiler
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include <contrib/dev/acpica/compiler/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)
292 /* Found left bracket, go to the right bracket */
294 while (Start < Colon && *Start != ']')
299 else if (*Start != ' ')
308 * There are two column values. One for the field name,
309 * and one for the field value.
311 Column = ACPI_PTR_DIFF (Colon, LineBuffer) + 3;
312 NameColumn = ACPI_PTR_DIFF (Start, LineBuffer) + 1;
314 Length = ACPI_PTR_DIFF (End, Start);
316 TmpName = UtLocalCalloc (Length + 1);
317 ACPI_STRNCPY (TmpName, Start, Length);
318 Name = DtTrim (TmpName);
321 Start = End = (Colon + 1);
324 /* Found left quotation, go to the right quotation and break */
330 /* Check for an explicit null string */
336 while (*End && (*End != '"'))
346 * Special "comment" fields at line end, ignore them.
347 * Note: normal slash-slash and slash-asterisk comments are
348 * stripped already by the DtGetNextLine parser.
350 * TBD: Perhaps DtGetNextLine should parse the following type
361 Length = ACPI_PTR_DIFF (End, Start);
362 TmpValue = UtLocalCalloc (Length + 1);
364 ACPI_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 = UtLocalCalloc (sizeof (DT_FIELD));
374 Field->Value = Value;
376 Field->ByteOffset = Offset;
377 Field->NameColumn = NameColumn;
378 Field->Column = Column;
382 else /* Ignore this field, it has no valid data */
392 /******************************************************************************
394 * FUNCTION: DtGetNextLine
396 * PARAMETERS: Handle - Open file handle for the source file
398 * RETURN: Filled line buffer and offset of start-of-line (ASL_EOF on EOF)
400 * DESCRIPTION: Get the next valid source line. Removes all comments.
401 * Ignores empty lines.
403 * Handles both slash-asterisk and slash-slash comments.
404 * Also, quoted strings, but no escapes within.
406 * Line is returned in Gbl_CurrentLineBuffer.
407 * Line number in original file is returned in Gbl_CurrentLineNumber.
409 *****************************************************************************/
415 BOOLEAN LineNotAllBlanks = FALSE;
416 UINT32 State = DT_NORMAL_TEXT;
417 UINT32 CurrentLineOffset;
425 * If line is too long, expand the line buffers. Also increases
426 * Gbl_LineBufferSize.
428 if (i >= Gbl_LineBufferSize)
430 UtExpandLineBuffers ();
438 case DT_START_QUOTED_STRING:
439 case DT_SLASH_ASTERISK_COMMENT:
441 AcpiOsPrintf ("**** EOF within comment/string %u\n", State);
449 /* Standalone EOF is OK */
457 * Received an EOF in the middle of a line. Terminate the
458 * line with a newline. The next call to this function will
459 * return a standalone EOF. Thus, the upper parsing software
460 * never has to deal with an EOF within a valid line (or
461 * the last line does not get tossed on the floor.)
464 State = DT_NORMAL_TEXT;
471 /* Normal text, insert char into line buffer */
473 Gbl_CurrentLineBuffer[i] = (char) c;
478 State = DT_START_COMMENT;
483 State = DT_START_QUOTED_STRING;
484 LineNotAllBlanks = TRUE;
490 * The continuation char MUST be last char on this line.
491 * Otherwise, it will be assumed to be a valid ASL char.
493 State = DT_MERGE_LINES;
498 CurrentLineOffset = Gbl_NextLineOffset;
499 Gbl_NextLineOffset = (UINT32) ftell (Handle);
500 Gbl_CurrentLineNumber++;
503 * Exit if line is complete. Ignore empty lines (only \n)
504 * or lines that contain nothing but blanks.
506 if ((i != 0) && LineNotAllBlanks)
508 if ((i + 1) >= Gbl_LineBufferSize)
510 UtExpandLineBuffers ();
513 Gbl_CurrentLineBuffer[i+1] = 0; /* Terminate string */
514 return (CurrentLineOffset);
517 /* Toss this line and start a new one */
520 LineNotAllBlanks = FALSE;
527 LineNotAllBlanks = TRUE;
535 case DT_START_QUOTED_STRING:
537 /* Insert raw chars until end of quoted string */
539 Gbl_CurrentLineBuffer[i] = (char) c;
546 State = DT_NORMAL_TEXT;
551 State = DT_ESCAPE_SEQUENCE;
556 AcpiOsPrintf ("ERROR at line %u: Unterminated quoted string\n",
557 Gbl_CurrentLineNumber++);
558 State = DT_NORMAL_TEXT;
561 default: /* Get next character */
567 case DT_ESCAPE_SEQUENCE:
569 /* Just copy the escaped character. TBD: sufficient for table compiler? */
571 Gbl_CurrentLineBuffer[i] = (char) c;
573 State = DT_START_QUOTED_STRING;
576 case DT_START_COMMENT:
578 /* Open comment if this character is an asterisk or slash */
584 State = DT_SLASH_ASTERISK_COMMENT;
589 State = DT_SLASH_SLASH_COMMENT;
592 default: /* Not a comment */
594 i++; /* Save the preceding slash */
595 if (i >= Gbl_LineBufferSize)
597 UtExpandLineBuffers ();
600 Gbl_CurrentLineBuffer[i] = (char) c;
602 State = DT_NORMAL_TEXT;
607 case DT_SLASH_ASTERISK_COMMENT:
609 /* Ignore chars until an asterisk-slash is found */
615 Gbl_NextLineOffset = (UINT32) ftell (Handle);
616 Gbl_CurrentLineNumber++;
621 State = DT_END_COMMENT;
630 case DT_SLASH_SLASH_COMMENT:
632 /* Ignore chars until end-of-line */
636 /* We will exit via the NORMAL_TEXT path */
639 State = DT_NORMAL_TEXT;
645 /* End comment if this char is a slash */
651 State = DT_NORMAL_TEXT;
656 CurrentLineOffset = Gbl_NextLineOffset;
657 Gbl_NextLineOffset = (UINT32) ftell (Handle);
658 Gbl_CurrentLineNumber++;
663 /* Consume all adjacent asterisks */
668 State = DT_SLASH_ASTERISK_COMMENT;
678 * This is not a continuation backslash, it is a normal
679 * normal ASL backslash - for example: Scope(\_SB_)
681 i++; /* Keep the backslash that is already in the buffer */
684 State = DT_NORMAL_TEXT;
689 * This is a continuation line -- a backlash followed
690 * immediately by a newline. Insert a space between the
691 * lines (overwrite the backslash)
693 Gbl_CurrentLineBuffer[i] = ' ';
696 /* Ignore newline, this will merge the lines */
698 CurrentLineOffset = Gbl_NextLineOffset;
699 Gbl_NextLineOffset = (UINT32) ftell (Handle);
700 Gbl_CurrentLineNumber++;
701 State = DT_NORMAL_TEXT;
707 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, "Unknown input state");
714 /******************************************************************************
716 * FUNCTION: DtScanFile
718 * PARAMETERS: Handle - Open file handle for the source file
720 * RETURN: Pointer to start of the constructed parse tree.
722 * DESCRIPTION: Scan source file, link all field names and values
723 * to the global parse tree: Gbl_FieldList
725 *****************************************************************************/
735 ACPI_FUNCTION_NAME (DtScanFile);
738 /* Get the file size */
740 Gbl_InputByteCount = DtGetFileSize (Handle);
742 Gbl_CurrentLineNumber = 0;
743 Gbl_CurrentLineOffset = 0;
744 Gbl_NextLineOffset = 0;
746 /* Scan line-by-line */
748 while ((Offset = DtGetNextLine (Handle)) != ASL_EOF)
750 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Line %2.2u/%4.4X - %s",
751 Gbl_CurrentLineNumber, Offset, Gbl_CurrentLineBuffer));
753 Status = DtParseLine (Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber, Offset);
754 if (Status == AE_NOT_FOUND)
760 /* Dump the parse tree if debug enabled */
762 DtDumpFieldList (Gbl_FieldList);
763 return (Gbl_FieldList);
771 /******************************************************************************
773 * FUNCTION: DtWriteBinary
775 * PARAMETERS: DT_WALK_CALLBACK
779 * DESCRIPTION: Write one subtable of a binary ACPI table
781 *****************************************************************************/
785 DT_SUBTABLE *Subtable,
790 FlWriteFile (ASL_FILE_AML_OUTPUT, Subtable->Buffer, Subtable->Length);
794 /******************************************************************************
796 * FUNCTION: DtOutputBinary
802 * DESCRIPTION: Write entire binary ACPI table (result of compilation)
804 *****************************************************************************/
808 DT_SUBTABLE *RootTable)
816 /* Walk the entire parse tree, emitting the binary data */
818 DtWalkTableTree (RootTable, DtWriteBinary, NULL, NULL);
819 Gbl_TableLength = DtGetFileSize (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
827 /******************************************************************************
829 * FUNCTION: DtDumpBuffer
831 * PARAMETERS: FileID - Where to write buffer data
832 * Buffer - Buffer to dump
833 * Offset - Offset in current table
834 * Length - Buffer Length
838 * DESCRIPTION: Another copy of DumpBuffer routine (unfortunately).
840 * TBD: merge dump buffer routines
842 *****************************************************************************/
856 FlPrintFile (FileId, "Output: [%3.3Xh %4.4d %3d] ",
857 Offset, Offset, Length);
864 FlPrintFile (FileId, "%24s", "");
867 /* Print 16 hex chars */
873 /* Dump fill spaces */
875 FlPrintFile (FileId, " ");
880 FlPrintFile (FileId, "%02X ", Buffer[i+j]);
884 FlPrintFile (FileId, " ");
885 for (j = 0; j < 16; j++)
889 FlPrintFile (FileId, "\n\n");
893 BufChar = Buffer[(ACPI_SIZE) i + j];
894 if (ACPI_IS_PRINT (BufChar))
896 FlPrintFile (FileId, "%c", BufChar);
900 FlPrintFile (FileId, ".");
904 /* Done with that line. */
906 FlPrintFile (FileId, "\n");
910 FlPrintFile (FileId, "\n\n");
914 /******************************************************************************
916 * FUNCTION: DtDumpFieldList
918 * PARAMETERS: Field - Root field
922 * DESCRIPTION: Dump the entire field list
924 *****************************************************************************/
931 if (!Gbl_DebugFlag || !Field)
936 DbgPrint (ASL_DEBUG_OUTPUT, "\nField List:\n"
937 "LineNo ByteOff NameCol Column TableOff "
938 "Flags %32s : %s\n\n", "Name", "Value");
941 DbgPrint (ASL_DEBUG_OUTPUT,
942 "%.08X %.08X %.08X %.08X %.08X %.08X %32s : %s\n",
943 Field->Line, Field->ByteOffset, Field->NameColumn,
944 Field->Column, Field->TableOffset, Field->Flags,
945 Field->Name, Field->Value);
950 DbgPrint (ASL_DEBUG_OUTPUT, "\n\n");
954 /******************************************************************************
956 * FUNCTION: DtDumpSubtableInfo, DtDumpSubtableTree
958 * PARAMETERS: DT_WALK_CALLBACK
962 * DESCRIPTION: Info - dump a subtable tree entry with extra information.
963 * Tree - dump a subtable tree formatted by depth indentation.
965 *****************************************************************************/
969 DT_SUBTABLE *Subtable,
974 DbgPrint (ASL_DEBUG_OUTPUT,
975 "[%.04X] %.08X %.08X %.08X %.08X %.08X %p %p %p\n",
976 Subtable->Depth, Subtable->Length, Subtable->TotalLength,
977 Subtable->SizeOfLengthField, Subtable->Flags, Subtable,
978 Subtable->Parent, Subtable->Child, Subtable->Peer);
983 DT_SUBTABLE *Subtable,
988 DbgPrint (ASL_DEBUG_OUTPUT,
989 "[%.04X] %*s%08X (%.02X) - (%.02X)\n",
990 Subtable->Depth, (4 * Subtable->Depth), " ",
991 Subtable, Subtable->Length, Subtable->TotalLength);
995 /******************************************************************************
997 * FUNCTION: DtDumpSubtableList
1003 * DESCRIPTION: Dump the raw list of subtables with information, and also
1004 * dump the subtable list in formatted tree format. Assists with
1005 * the development of new table code.
1007 *****************************************************************************/
1010 DtDumpSubtableList (
1014 if (!Gbl_DebugFlag || !Gbl_RootTable)
1019 DbgPrint (ASL_DEBUG_OUTPUT,
1021 "Depth Length TotalLen LenSize Flags "
1022 "This Parent Child Peer\n\n");
1023 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableInfo, NULL, NULL);
1025 DbgPrint (ASL_DEBUG_OUTPUT,
1026 "\nSubtable Tree: (Depth, Subtable, Length, TotalLength)\n\n");
1027 DtWalkTableTree (Gbl_RootTable, DtDumpSubtableTree, NULL, NULL);
1031 /******************************************************************************
1033 * FUNCTION: DtWriteFieldToListing
1035 * PARAMETERS: Buffer - Contains the compiled data
1036 * Field - Field node for the input line
1037 * Length - Length of the output data
1041 * DESCRIPTION: Write one field to the listing file (if listing is enabled).
1043 *****************************************************************************/
1046 DtWriteFieldToListing (
1054 if (!Gbl_ListingFlag || !Field)
1059 /* Dump the original source line */
1061 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Input: ");
1062 FlSeekFile (ASL_FILE_INPUT, Field->ByteOffset);
1064 while (FlReadFile (ASL_FILE_INPUT, &FileByte, 1) == AE_OK)
1066 FlWriteFile (ASL_FILE_LISTING_OUTPUT, &FileByte, 1);
1067 if (FileByte == '\n')
1073 /* Dump the line as parsed and represented internally */
1075 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "Parsed: %*s : %.64s",
1076 Field->Column-4, Field->Name, Field->Value);
1078 if (strlen (Field->Value) > 64)
1080 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "...Additional data, length 0x%X\n",
1081 strlen (Field->Value));
1083 FlPrintFile (ASL_FILE_LISTING_OUTPUT, "\n");
1085 /* Dump the hex data that will be output for this field */
1087 DtDumpBuffer (ASL_FILE_LISTING_OUTPUT, Buffer, Field->TableOffset, Length);
1091 /******************************************************************************
1093 * FUNCTION: DtWriteTableToListing
1099 * DESCRIPTION: Write the entire compiled table to the listing file
1102 *****************************************************************************/
1105 DtWriteTableToListing (
1111 if (!Gbl_ListingFlag)
1116 /* Read the entire table from the output file */
1118 Buffer = UtLocalCalloc (Gbl_TableLength);
1119 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1120 FlReadFile (ASL_FILE_AML_OUTPUT, Buffer, Gbl_TableLength);
1122 /* Dump the raw table data */
1124 AcpiOsRedirectOutput (Gbl_Files[ASL_FILE_LISTING_OUTPUT].Handle);
1126 AcpiOsPrintf ("\n%s: Length %d (0x%X)\n\n",
1127 ACPI_RAW_TABLE_DATA_HEADER, Gbl_TableLength, Gbl_TableLength);
1128 AcpiUtDumpBuffer (Buffer, Gbl_TableLength, DB_BYTE_DISPLAY, 0);
1130 AcpiOsRedirectOutput (stdout);