xref: /freebsd/sys/contrib/dev/acpica/compiler/dttemplate.c (revision f5f7c05209ca2c3748fd8b27c5e80ffad49120eb)
1 /******************************************************************************
2  *
3  * Module Name: dttemplate - ACPI table template generation
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/acapps.h>
46 #include <contrib/dev/acpica/compiler/dtcompiler.h>
47 #include <contrib/dev/acpica/compiler/dttemplate.h> /* Contains the hex ACPI table templates */
48 
49 #define _COMPONENT          DT_COMPILER
50         ACPI_MODULE_NAME    ("dttemplate")
51 
52 
53 /* Local prototypes */
54 
55 static BOOLEAN
56 AcpiUtIsSpecialTable (
57     char                    *Signature);
58 
59 static ACPI_STATUS
60 DtCreateOneTemplate (
61     char                    *Signature,
62     ACPI_DMTABLE_DATA       *TableData);
63 
64 static ACPI_STATUS
65 DtCreateAllTemplates (
66     void);
67 
68 
69 /*******************************************************************************
70  *
71  * FUNCTION:    AcpiUtIsSpecialTable
72  *
73  * PARAMETERS:  Signature           - ACPI table signature
74  *
75  * RETURN:      TRUE if signature is a special ACPI table
76  *
77  * DESCRIPTION: Check for valid ACPI tables that are not in the main ACPI
78  *              table data structure (AcpiDmTableData).
79  *
80  ******************************************************************************/
81 
82 static BOOLEAN
83 AcpiUtIsSpecialTable (
84     char                    *Signature)
85 {
86 
87     if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT) ||
88         ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT) ||
89         ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS) ||
90         ACPI_COMPARE_NAME (Signature, ACPI_RSDP_NAME))
91     {
92         return (TRUE);
93     }
94 
95     return (FALSE);
96 }
97 
98 
99 /*******************************************************************************
100  *
101  * FUNCTION:    DtCreateTemplates
102  *
103  * PARAMETERS:  Signature           - ACPI table signature
104  *
105  * RETURN:      Status
106  *
107  * DESCRIPTION: Create one or more template files.
108  *
109  ******************************************************************************/
110 
111 ACPI_STATUS
112 DtCreateTemplates (
113     char                    *Signature)
114 {
115     ACPI_DMTABLE_DATA       *TableData;
116     ACPI_STATUS             Status;
117 
118 
119     AslInitializeGlobals ();
120     AcpiUtStrupr (Signature);
121 
122     /* Create all known templates if requested */
123 
124     if (!ACPI_STRNCMP (Signature, "ALL", 3) ||
125         !ACPI_STRCMP (Signature, "*"))
126     {
127         Status = DtCreateAllTemplates ();
128         return (Status);
129     }
130 
131     /*
132      * Validate signature and get the template data:
133      *  1) Signature must be 4 characters
134      *  2) Signature must be a recognized ACPI table
135      *  3) There must be a template associated with the signature
136      */
137     if (strlen (Signature) != ACPI_NAME_SIZE)
138     {
139         fprintf (stderr, "%s, Invalid ACPI table signature\n", Signature);
140         return (AE_ERROR);
141     }
142 
143     /*
144      * Some slack for the two strange tables whose name is different than
145      * their signatures: MADT->APIC and FADT->FACP.
146      */
147     if (!strcmp (Signature, "MADT"))
148     {
149         Signature = "APIC";
150     }
151     else if (!strcmp (Signature, "FADT"))
152     {
153         Signature = "FACP";
154     }
155 
156     TableData = AcpiDmGetTableData (Signature);
157     if (TableData)
158     {
159         if (!TableData->Template)
160         {
161             fprintf (stderr, "%4.4s, No template available\n", Signature);
162             return (AE_ERROR);
163         }
164     }
165     else if (!AcpiUtIsSpecialTable (Signature))
166     {
167         fprintf (stderr,
168             "%4.4s, Unrecognized ACPI table signature\n", Signature);
169         return (AE_ERROR);
170     }
171 
172     Status = AdInitialize ();
173     if (ACPI_FAILURE (Status))
174     {
175         return (Status);
176     }
177 
178     Status = DtCreateOneTemplate (Signature, TableData);
179     return (Status);
180 }
181 
182 
183 /*******************************************************************************
184  *
185  * FUNCTION:    DtCreateAllTemplates
186  *
187  * PARAMETERS:  None
188  *
189  * RETURN:      Status
190  *
191  * DESCRIPTION: Create all currently defined template files
192  *
193  ******************************************************************************/
194 
195 static ACPI_STATUS
196 DtCreateAllTemplates (
197     void)
198 {
199     ACPI_DMTABLE_DATA       *TableData;
200     ACPI_STATUS             Status;
201 
202 
203     Status = AdInitialize ();
204     if (ACPI_FAILURE (Status))
205     {
206         return (Status);
207     }
208 
209     fprintf (stderr, "Creating all supported Template files\n");
210 
211     /* Walk entire ACPI table data structure */
212 
213     for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
214     {
215         /* If table has a template, create the template file */
216 
217         if (TableData->Template)
218         {
219             Status = DtCreateOneTemplate (TableData->Signature,
220                         TableData);
221             if (ACPI_FAILURE (Status))
222             {
223                 return (Status);
224             }
225         }
226     }
227 
228     /*
229      * Create the special ACPI tables:
230      * 1) DSDT/SSDT are AML tables, not data tables
231      * 2) FACS and RSDP have non-standard headers
232      */
233     Status = DtCreateOneTemplate (ACPI_SIG_DSDT, NULL);
234     if (ACPI_FAILURE (Status))
235     {
236         return (Status);
237     }
238 
239     Status = DtCreateOneTemplate (ACPI_SIG_SSDT, NULL);
240     if (ACPI_FAILURE (Status))
241     {
242         return (Status);
243     }
244 
245     Status = DtCreateOneTemplate (ACPI_SIG_FACS, NULL);
246     if (ACPI_FAILURE (Status))
247     {
248         return (Status);
249     }
250 
251     Status = DtCreateOneTemplate (ACPI_RSDP_NAME, NULL);
252     if (ACPI_FAILURE (Status))
253     {
254         return (Status);
255     }
256 
257     return (AE_OK);
258 }
259 
260 
261 /*******************************************************************************
262  *
263  * FUNCTION:    DtCreateOneTemplate
264  *
265  * PARAMETERS:  Signature           - ACPI signature, NULL terminated.
266  *              TableData           - Entry in ACPI table data structure.
267  *                                    NULL if a special ACPI table.
268  *
269  * RETURN:      Status
270  *
271  * DESCRIPTION: Create one template source file for the requested ACPI table.
272  *
273  ******************************************************************************/
274 
275 static ACPI_STATUS
276 DtCreateOneTemplate (
277     char                    *Signature,
278     ACPI_DMTABLE_DATA       *TableData)
279 {
280     char                    *DisasmFilename;
281     FILE                    *File;
282     ACPI_STATUS             Status = AE_OK;
283     ACPI_SIZE               Actual;
284 
285 
286     /* New file will have a .asl suffix */
287 
288     DisasmFilename = FlGenerateFilename (
289         Signature, FILE_SUFFIX_ASL_CODE);
290     if (!DisasmFilename)
291     {
292         fprintf (stderr, "Could not generate output filename\n");
293         return (AE_ERROR);
294     }
295 
296     /* Probably should prompt to overwrite the file */
297 
298     AcpiUtStrlwr (DisasmFilename);
299     File = fopen (DisasmFilename, "w+");
300     if (!File)
301     {
302         fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
303         return (AE_ERROR);
304     }
305 
306     /* Emit the common file header */
307 
308     AcpiOsRedirectOutput (File);
309 
310     AcpiOsPrintf ("/*\n");
311     AcpiOsPrintf (ACPI_COMMON_HEADER ("iASL Compiler/Disassembler", " * "));
312 
313     AcpiOsPrintf (" * Template for [%4.4s] ACPI Table\n",
314         Signature);
315 
316     /* Dump the actual ACPI table */
317 
318     if (TableData)
319     {
320         /* Normal case, tables that appear in AcpiDmTableData */
321 
322         if (Gbl_VerboseTemplates)
323         {
324             AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]"
325                 "  FieldName : HexFieldValue\n */\n\n");
326         }
327         else
328         {
329             AcpiOsPrintf (" * Format: [ByteLength]"
330                 "  FieldName : HexFieldValue\n */\n\n");
331         }
332 
333         AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
334             TableData->Template));
335     }
336     else
337     {
338         /* Special ACPI tables - DSDT, SSDT, FADT, RSDP */
339 
340         AcpiOsPrintf (" */\n\n");
341         if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT))
342         {
343             Actual = fwrite (TemplateDsdt, 1, sizeof (TemplateDsdt) -1, File);
344             if (Actual != sizeof (TemplateDsdt) -1)
345             {
346                 fprintf (stderr,
347                     "Could not write to output file %s\n", DisasmFilename);
348                 Status = AE_ERROR;
349                 goto Cleanup;
350             }
351         }
352         else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT))
353         {
354             Actual = fwrite (TemplateSsdt, 1, sizeof (TemplateSsdt) -1, File);
355             if (Actual != sizeof (TemplateSsdt) -1)
356             {
357                 fprintf (stderr,
358                     "Could not write to output file %s\n", DisasmFilename);
359                 Status = AE_ERROR;
360                 goto Cleanup;
361             }
362         }
363         else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS)) /* FADT */
364         {
365             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
366                 TemplateFacs));
367         }
368         else if (ACPI_COMPARE_NAME (Signature, ACPI_RSDP_NAME))
369         {
370             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
371                 TemplateRsdp));
372         }
373         else
374         {
375             fprintf (stderr,
376                 "%4.4s, Unrecognized ACPI table signature\n", Signature);
377             Status = AE_ERROR;
378             goto Cleanup;
379         }
380     }
381 
382     fprintf (stderr,
383         "Created ACPI table template for [%4.4s], written to \"%s\"\n",
384         Signature, DisasmFilename);
385 
386 Cleanup:
387     fclose (File);
388     AcpiOsRedirectOutput (stdout);
389     ACPI_FREE (DisasmFilename);
390     return (Status);
391 }
392