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