dtcompile.c revision 281687
1/******************************************************************************
2 *
3 * Module Name: dtcompile.c - Front-end for data table compiler
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2015, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44#define _DECLARE_DT_GLOBALS
45
46#include <contrib/dev/acpica/compiler/aslcompiler.h>
47#include <contrib/dev/acpica/compiler/dtcompiler.h>
48
49#define _COMPONENT          DT_COMPILER
50        ACPI_MODULE_NAME    ("dtcompile")
51
52static char                 VersionString[9];
53
54
55/* Local prototypes */
56
57static ACPI_STATUS
58DtInitialize (
59    void);
60
61static ACPI_STATUS
62DtCompileDataTable (
63    DT_FIELD                **Field);
64
65static void
66DtInsertCompilerIds (
67    DT_FIELD                *FieldList);
68
69
70/******************************************************************************
71 *
72 * FUNCTION:    DtDoCompile
73 *
74 * PARAMETERS:  None
75 *
76 * RETURN:      Status
77 *
78 * DESCRIPTION: Main entry point for the data table compiler.
79 *
80 * Note: Assumes Gbl_Files[ASL_FILE_INPUT] is initialized and the file is
81 *          open at seek offset zero.
82 *
83 *****************************************************************************/
84
85ACPI_STATUS
86DtDoCompile (
87    void)
88{
89    ACPI_STATUS             Status;
90    UINT8                   Event;
91    DT_FIELD                *FieldList;
92
93
94    /* Initialize globals */
95
96    Status = DtInitialize ();
97    if (ACPI_FAILURE (Status))
98    {
99        printf ("Error during compiler initialization, 0x%X\n", Status);
100        return (Status);
101    }
102
103    /* Preprocessor */
104
105    if (Gbl_PreprocessFlag)
106    {
107        /* Preprocessor */
108
109        Event = UtBeginEvent ("Preprocess input file");
110        PrDoPreprocess ();
111        UtEndEvent (Event);
112
113        if (Gbl_PreprocessOnly)
114        {
115            return (AE_OK);
116        }
117    }
118
119    /*
120     * Scan the input file (file is already open) and
121     * build the parse tree
122     */
123    Event = UtBeginEvent ("Scan and parse input file");
124    FieldList = DtScanFile (Gbl_Files[ASL_FILE_INPUT].Handle);
125    UtEndEvent (Event);
126
127    /* Did the parse tree get successfully constructed? */
128
129    if (!FieldList)
130    {
131        /* TBD: temporary error message. Msgs should come from function above */
132
133        DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
134            "Input file does not appear to be an ASL or data table source file");
135
136        Status = AE_ERROR;
137        goto CleanupAndExit;
138    }
139
140    Event = UtBeginEvent ("Compile parse tree");
141
142    /*
143     * Compile the parse tree
144     */
145    Status = DtCompileDataTable (&FieldList);
146    UtEndEvent (Event);
147
148    if (ACPI_FAILURE (Status))
149    {
150        /* TBD: temporary error message. Msgs should come from function above */
151
152        DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
153            "Could not compile input file");
154
155        goto CleanupAndExit;
156    }
157
158    /* Create/open the binary output file */
159
160    Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
161    Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
162    if (ACPI_FAILURE (Status))
163    {
164        goto CleanupAndExit;
165    }
166
167    /* Write the binary, then the optional hex file */
168
169    DtOutputBinary (Gbl_RootTable);
170    HxDoHexOutput ();
171    DtWriteTableToListing ();
172
173CleanupAndExit:
174
175    AcpiUtDeleteCaches ();
176    DtDeleteCaches ();
177    CmCleanupAndExit ();
178    return (Status);
179}
180
181
182/******************************************************************************
183 *
184 * FUNCTION:    DtInitialize
185 *
186 * PARAMETERS:  None
187 *
188 * RETURN:      Status
189 *
190 * DESCRIPTION: Initialize data table compiler globals. Enables multiple
191 *              compiles per invocation.
192 *
193 *****************************************************************************/
194
195static ACPI_STATUS
196DtInitialize (
197    void)
198{
199    ACPI_STATUS             Status;
200
201
202    Status = AcpiOsInitialize ();
203    if (ACPI_FAILURE (Status))
204    {
205        return (Status);
206    }
207
208    Status = AcpiUtInitGlobals ();
209    if (ACPI_FAILURE (Status))
210    {
211        return (Status);
212    }
213
214    Gbl_FieldList = NULL;
215    Gbl_RootTable = NULL;
216    Gbl_SubtableStack = NULL;
217
218    sprintf (VersionString, "%X", (UINT32) ACPI_CA_VERSION);
219    return (AE_OK);
220}
221
222
223/******************************************************************************
224 *
225 * FUNCTION:    DtInsertCompilerIds
226 *
227 * PARAMETERS:  FieldList           - Current field list pointer
228 *
229 * RETURN:      None
230 *
231 * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
232 *              the original ACPI table header.
233 *
234 *****************************************************************************/
235
236static void
237DtInsertCompilerIds (
238    DT_FIELD                *FieldList)
239{
240    DT_FIELD                *Next;
241    UINT32                  i;
242
243
244    /*
245     * Don't insert current compiler ID if requested. Used for compiler
246     * debug/validation only.
247     */
248    if (Gbl_UseOriginalCompilerId)
249    {
250        return;
251    }
252
253    /* Walk to the Compiler fields at the end of the header */
254
255    Next = FieldList;
256    for (i = 0; i < 7; i++)
257    {
258        Next = Next->Next;
259    }
260
261    Next->Value = ASL_CREATOR_ID;
262    Next->Flags = DT_FIELD_NOT_ALLOCATED;
263
264    Next = Next->Next;
265    Next->Value = VersionString;
266    Next->Flags = DT_FIELD_NOT_ALLOCATED;
267}
268
269
270/******************************************************************************
271 *
272 * FUNCTION:    DtCompileDataTable
273 *
274 * PARAMETERS:  FieldList           - Current field list pointer
275 *
276 * RETURN:      Status
277 *
278 * DESCRIPTION: Entry point to compile one data table
279 *
280 *****************************************************************************/
281
282static ACPI_STATUS
283DtCompileDataTable (
284    DT_FIELD                **FieldList)
285{
286    ACPI_DMTABLE_DATA       *TableData;
287    DT_SUBTABLE             *Subtable;
288    char                    *Signature;
289    ACPI_TABLE_HEADER       *AcpiTableHeader;
290    ACPI_STATUS             Status;
291    DT_FIELD                *RootField = *FieldList;
292
293
294    /* Verify that we at least have a table signature and save it */
295
296    Signature = DtGetFieldValue (*FieldList);
297    if (!Signature)
298    {
299        sprintf (MsgBuffer, "Expected \"%s\"", "Signature");
300        DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
301            *FieldList, MsgBuffer);
302        return (AE_ERROR);
303    }
304
305    Gbl_Signature = UtStringCacheCalloc (ACPI_STRLEN (Signature) + 1);
306    strcpy (Gbl_Signature, Signature);
307
308    /*
309     * Handle tables that don't use the common ACPI table header structure.
310     * Currently, these are the FACS and RSDP. Also check for an OEMx table,
311     * these tables have user-defined contents.
312     */
313    if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
314    {
315        Status = DtCompileFacs (FieldList);
316        if (ACPI_FAILURE (Status))
317        {
318            return (Status);
319        }
320
321        DtSetTableLength ();
322        return (Status);
323    }
324    else if (ACPI_VALIDATE_RSDP_SIG (Signature))
325    {
326        Status = DtCompileRsdp (FieldList);
327        return (Status);
328    }
329    else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_S3PT))
330    {
331        Status = DtCompileS3pt (FieldList);
332        if (ACPI_FAILURE (Status))
333        {
334            return (Status);
335        }
336
337        DtSetTableLength ();
338        return (Status);
339    }
340
341    /*
342     * All other tables must use the common ACPI table header. Insert the
343     * current iASL IDs (name, version), and compile the header now.
344     */
345    DtInsertCompilerIds (*FieldList);
346
347    Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
348                &Gbl_RootTable, TRUE);
349    if (ACPI_FAILURE (Status))
350    {
351        return (Status);
352    }
353
354    DtPushSubtable (Gbl_RootTable);
355
356    /* Validate the signature via the ACPI table list */
357
358    TableData = AcpiDmGetTableData (Signature);
359    if (!TableData || Gbl_CompileGeneric)
360    {
361        DtCompileGeneric ((void **) FieldList);
362        goto FinishHeader;
363    }
364
365    /* Dispatch to per-table compile */
366
367    if (TableData->CmTableHandler)
368    {
369        /* Complex table, has a handler */
370
371        Status = TableData->CmTableHandler ((void **) FieldList);
372        if (ACPI_FAILURE (Status))
373        {
374            return (Status);
375        }
376    }
377    else if (TableData->TableInfo)
378    {
379        /* Simple table, just walk the info table */
380
381        Subtable = NULL;
382        Status = DtCompileTable (FieldList, TableData->TableInfo,
383                    &Subtable, TRUE);
384        if (ACPI_FAILURE (Status))
385        {
386            return (Status);
387        }
388
389        DtInsertSubtable (Gbl_RootTable, Subtable);
390        DtPopSubtable ();
391    }
392    else
393    {
394        DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
395            "Missing table dispatch info");
396        return (AE_ERROR);
397    }
398
399FinishHeader:
400
401    /* Set the final table length and then the checksum */
402
403    DtSetTableLength ();
404    AcpiTableHeader = ACPI_CAST_PTR (
405        ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
406    DtSetTableChecksum (&AcpiTableHeader->Checksum);
407
408    DtDumpFieldList (RootField);
409    DtDumpSubtableList ();
410    return (AE_OK);
411}
412
413
414/******************************************************************************
415 *
416 * FUNCTION:    DtCompileTable
417 *
418 * PARAMETERS:  Field               - Current field list pointer
419 *              Info                - Info table for this ACPI table
420 *              RetSubtable         - Compile result of table
421 *              Required            - If this subtable must exist
422 *
423 * RETURN:      Status
424 *
425 * DESCRIPTION: Compile a subtable
426 *
427 *****************************************************************************/
428
429ACPI_STATUS
430DtCompileTable (
431    DT_FIELD                **Field,
432    ACPI_DMTABLE_INFO       *Info,
433    DT_SUBTABLE             **RetSubtable,
434    BOOLEAN                 Required)
435{
436    DT_FIELD                *LocalField;
437    UINT32                  Length;
438    DT_SUBTABLE             *Subtable;
439    DT_SUBTABLE             *InlineSubtable;
440    UINT32                  FieldLength = 0;
441    UINT8                   FieldType;
442    UINT8                   *Buffer;
443    UINT8                   *FlagBuffer = NULL;
444    char                    *String;
445    UINT32                  CurrentFlagByteOffset = 0;
446    ACPI_STATUS             Status;
447
448
449    if (!Field || !*Field)
450    {
451        return (AE_BAD_PARAMETER);
452    }
453
454    /* Ignore optional subtable if name does not match */
455
456    if ((Info->Flags & DT_OPTIONAL) &&
457        ACPI_STRCMP ((*Field)->Name, Info->Name))
458    {
459        *RetSubtable = NULL;
460        return (AE_OK);
461    }
462
463    Length = DtGetSubtableLength (*Field, Info);
464    if (Length == ASL_EOF)
465    {
466        return (AE_ERROR);
467    }
468
469    Subtable = UtSubtableCacheCalloc ();
470
471    if (Length > 0)
472    {
473        String = UtStringCacheCalloc (Length);
474        Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
475    }
476
477    Subtable->Length = Length;
478    Subtable->TotalLength = Length;
479    Buffer = Subtable->Buffer;
480
481    LocalField = *Field;
482
483    /*
484     * Main loop walks the info table for this ACPI table or subtable
485     */
486    for (; Info->Name; Info++)
487    {
488        if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
489        {
490            continue;
491        }
492
493        if (!LocalField)
494        {
495            sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
496                Info->Name);
497            DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
498            Status = AE_BAD_DATA;
499            goto Error;
500        }
501
502        /* Maintain table offsets */
503
504        LocalField->TableOffset = Gbl_CurrentTableOffset;
505        FieldLength = DtGetFieldLength (LocalField, Info);
506        Gbl_CurrentTableOffset += FieldLength;
507
508        FieldType = DtGetFieldType (Info);
509        Gbl_InputFieldCount++;
510
511        switch (FieldType)
512        {
513        case DT_FIELD_TYPE_FLAGS_INTEGER:
514            /*
515             * Start of the definition of a flags field.
516             * This master flags integer starts at value zero, in preparation
517             * to compile and insert the flag fields from the individual bits
518             */
519            LocalField = LocalField->Next;
520            *Field = LocalField;
521
522            FlagBuffer = Buffer;
523            CurrentFlagByteOffset = Info->Offset;
524            break;
525
526        case DT_FIELD_TYPE_FLAG:
527
528            /* Individual Flag field, can be multiple bits */
529
530            if (FlagBuffer)
531            {
532                /*
533                 * We must increment the FlagBuffer when we have crossed
534                 * into the next flags byte within the flags field
535                 * of type DT_FIELD_TYPE_FLAGS_INTEGER.
536                 */
537                FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
538                CurrentFlagByteOffset = Info->Offset;
539
540                DtCompileFlag (FlagBuffer, LocalField, Info);
541            }
542            else
543            {
544                /* TBD - this is an internal error */
545            }
546
547            LocalField = LocalField->Next;
548            *Field = LocalField;
549            break;
550
551        case DT_FIELD_TYPE_INLINE_SUBTABLE:
552            /*
553             * Recursion (one level max): compile GAS (Generic Address)
554             * or Notify in-line subtable
555             */
556            *Field = LocalField;
557
558            if (Info->Opcode == ACPI_DMT_GAS)
559            {
560                Status = DtCompileTable (Field, AcpiDmTableInfoGas,
561                    &InlineSubtable, TRUE);
562            }
563            else
564            {
565                Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
566                    &InlineSubtable, TRUE);
567            }
568
569            if (ACPI_FAILURE (Status))
570            {
571                goto Error;
572            }
573
574            DtSetSubtableLength (InlineSubtable);
575
576            ACPI_MEMCPY (Buffer, InlineSubtable->Buffer, FieldLength);
577            LocalField = *Field;
578            break;
579
580        case DT_FIELD_TYPE_LABEL:
581
582            DtWriteFieldToListing (Buffer, LocalField, 0);
583            LocalField = LocalField->Next;
584            break;
585
586        default:
587
588            /* Normal case for most field types (Integer, String, etc.) */
589
590            DtCompileOneField (Buffer, LocalField,
591                FieldLength, FieldType, Info->Flags);
592
593            DtWriteFieldToListing (Buffer, LocalField, FieldLength);
594            LocalField = LocalField->Next;
595
596            if (Info->Flags & DT_LENGTH)
597            {
598                /* Field is an Integer that will contain a subtable length */
599
600                Subtable->LengthField = Buffer;
601                Subtable->SizeOfLengthField = FieldLength;
602            }
603
604            break;
605        }
606
607        Buffer += FieldLength;
608    }
609
610    *Field = LocalField;
611    *RetSubtable = Subtable;
612    return (AE_OK);
613
614Error:
615    ACPI_FREE (Subtable->Buffer);
616    ACPI_FREE (Subtable);
617    return (Status);
618}
619