xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/dttemplate.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /******************************************************************************
2  *
3  * Module Name: dttemplate - ACPI table template generation
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2011, 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 "aslcompiler.h"
45 #include "acapps.h"
46 #include "dtcompiler.h"
47 #include "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     {
126         Status = DtCreateAllTemplates ();
127         return (Status);
128     }
129 
130     /*
131      * Validate signature and get the template data:
132      *  1) Signature must be 4 characters
133      *  2) Signature must be a recognized ACPI table
134      *  3) There must be a template associated with the signature
135      */
136     if (strlen (Signature) != ACPI_NAME_SIZE)
137     {
138         fprintf (stderr, "%s, Invalid ACPI table signature\n", Signature);
139         return (AE_ERROR);
140     }
141 
142     /*
143      * Some slack for the two strange tables whose name is different than
144      * their signatures: MADT->APIC and FADT->FACP.
145      */
146     if (!strcmp (Signature, "MADT"))
147     {
148         Signature = "APIC";
149     }
150     else if (!strcmp (Signature, "FADT"))
151     {
152         Signature = "FACP";
153     }
154 
155     TableData = AcpiDmGetTableData (Signature);
156     if (TableData)
157     {
158         if (!TableData->Template)
159         {
160             fprintf (stderr, "%4.4s, No template available\n", Signature);
161             return (AE_ERROR);
162         }
163     }
164     else if (!AcpiUtIsSpecialTable (Signature))
165     {
166         fprintf (stderr,
167             "%4.4s, Unrecognized ACPI table signature\n", Signature);
168         return (AE_ERROR);
169     }
170 
171     Status = AdInitialize ();
172     if (ACPI_FAILURE (Status))
173     {
174         return (Status);
175     }
176 
177     Status = DtCreateOneTemplate (Signature, TableData);
178     return (Status);
179 }
180 
181 
182 /*******************************************************************************
183  *
184  * FUNCTION:    DtCreateAllTemplates
185  *
186  * PARAMETERS:  None
187  *
188  * RETURN:      Status
189  *
190  * DESCRIPTION: Create all currently defined template files
191  *
192  ******************************************************************************/
193 
194 static ACPI_STATUS
195 DtCreateAllTemplates (
196     void)
197 {
198     ACPI_DMTABLE_DATA       *TableData;
199     ACPI_STATUS             Status;
200 
201 
202     Status = AdInitialize ();
203     if (ACPI_FAILURE (Status))
204     {
205         return (Status);
206     }
207 
208     fprintf (stderr, "Creating all supported Template files\n");
209 
210     /* Walk entire ACPI table data structure */
211 
212     for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
213     {
214         /* If table has a template, create the template file */
215 
216         if (TableData->Template)
217         {
218             Status = DtCreateOneTemplate (TableData->Signature,
219                         TableData);
220             if (ACPI_FAILURE (Status))
221             {
222                 return (Status);
223             }
224         }
225     }
226 
227     /*
228      * Create the "special ACPI tables:
229      * 1) DSDT/SSDT are AML tables, not data tables
230      * 2) FACS and RSDP have non-standard headers
231      */
232     Status = DtCreateOneTemplate (ACPI_SIG_DSDT, NULL);
233     if (ACPI_FAILURE (Status))
234     {
235         return (Status);
236     }
237 
238     Status = DtCreateOneTemplate (ACPI_SIG_SSDT, NULL);
239     if (ACPI_FAILURE (Status))
240     {
241         return (Status);
242     }
243 
244     Status = DtCreateOneTemplate (ACPI_SIG_FACS, NULL);
245     if (ACPI_FAILURE (Status))
246     {
247         return (Status);
248     }
249 
250     Status = DtCreateOneTemplate (ACPI_RSDP_NAME, NULL);
251     if (ACPI_FAILURE (Status))
252     {
253         return (Status);
254     }
255 
256     return (AE_OK);
257 }
258 
259 
260 /*******************************************************************************
261  *
262  * FUNCTION:    DtCreateOneTemplate
263  *
264  * PARAMETERS:  Signature           - ACPI signature, NULL terminated.
265  *              TableData           - Entry in ACPI table data structure.
266  *                                    NULL if a special ACPI table.
267  *
268  * RETURN:      Status
269  *
270  * DESCRIPTION: Create one template source file for the requested ACPI table.
271  *
272  ******************************************************************************/
273 
274 static ACPI_STATUS
275 DtCreateOneTemplate (
276     char                    *Signature,
277     ACPI_DMTABLE_DATA       *TableData)
278 {
279     char                    *DisasmFilename;
280     FILE                    *File;
281     ACPI_STATUS             Status = AE_OK;
282 
283 
284     /* New file will have a .asl suffix */
285 
286     DisasmFilename = FlGenerateFilename (
287         Signature, FILE_SUFFIX_ASL_CODE);
288     if (!DisasmFilename)
289     {
290         fprintf (stderr, "Could not generate output filename\n");
291         return (AE_ERROR);
292     }
293 
294     /* Probably should prompt to overwrite the file */
295 
296     AcpiUtStrlwr (DisasmFilename);
297     File = fopen (DisasmFilename, "w+");
298     if (!File)
299     {
300         fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
301         return (AE_ERROR);
302     }
303 
304     /* Emit the common file header */
305 
306     AcpiOsRedirectOutput (File);
307 
308     AcpiOsPrintf ("/*\n");
309     AcpiOsPrintf (ACPI_COMMON_HEADER ("iASL Compiler/Disassembler", " * "));
310 
311     AcpiOsPrintf (" * Template for [%4.4s] ACPI Table\n",
312         Signature);
313 
314     /* Dump the actual ACPI table */
315 
316     if (TableData)
317     {
318         /* Normal case, tables that appear in AcpiDmTableData */
319 
320         if (Gbl_VerboseTemplates)
321         {
322             AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]"
323                 "  FieldName : HexFieldValue\n */\n\n");
324         }
325         else
326         {
327             AcpiOsPrintf (" * Format: [ByteLength]"
328                 "  FieldName : HexFieldValue\n */\n\n");
329         }
330 
331         AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
332             TableData->Template));
333     }
334     else
335     {
336         /* Special ACPI tables - DSDT, SSDT, FACS, RSDP */
337 
338         AcpiOsPrintf (" */\n\n");
339         if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT))
340         {
341             fwrite (TemplateDsdt, sizeof (TemplateDsdt) -1, 1, File);
342         }
343         else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT))
344         {
345             fwrite (TemplateSsdt, sizeof (TemplateSsdt) -1, 1, File);
346         }
347         else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
348         {
349             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
350                 TemplateFacs));
351         }
352         else if (ACPI_COMPARE_NAME (Signature, ACPI_RSDP_NAME))
353         {
354             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
355                 TemplateRsdp));
356         }
357         else
358         {
359             fprintf (stderr,
360                 "%4.4s, Unrecognized ACPI table signature\n", Signature);
361             return (AE_ERROR);
362         }
363     }
364 
365     fprintf (stderr,
366         "Created ACPI table template for [%4.4s], written to \"%s\"\n",
367         Signature, DisasmFilename);
368 
369     fclose (File);
370     AcpiOsRedirectOutput (stdout);
371     ACPI_FREE (DisasmFilename);
372     return (Status);
373 }
374