xref: /freebsd/sys/contrib/dev/acpica/compiler/dttemplate.c (revision a009b7dcabdc27aa3fdde1f2c6dd08d4fe5a3170)
1a88e22b7SJung-uk Kim /******************************************************************************
2a88e22b7SJung-uk Kim  *
3a88e22b7SJung-uk Kim  * Module Name: dttemplate - ACPI table template generation
4a88e22b7SJung-uk Kim  *
5a88e22b7SJung-uk Kim  *****************************************************************************/
6a88e22b7SJung-uk Kim 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
11e5e1f58aSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2019, Intel Corp.
12a88e22b7SJung-uk Kim  * All rights reserved.
13a88e22b7SJung-uk Kim  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-uk Kim  *
119d244b227SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120d244b227SJung-uk Kim  * modification, are permitted provided that the following conditions
121d244b227SJung-uk Kim  * are met:
122d244b227SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123d244b227SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124d244b227SJung-uk Kim  *    without modification.
125d244b227SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126d244b227SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127d244b227SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128d244b227SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129d244b227SJung-uk Kim  *    binary redistribution.
130d244b227SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131d244b227SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132d244b227SJung-uk Kim  *    from this software without specific prior written permission.
133a88e22b7SJung-uk Kim  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147d244b227SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148d244b227SJung-uk Kim  * Software Foundation.
149a88e22b7SJung-uk Kim  *
1500d84335fSJung-uk Kim  *****************************************************************************/
151a88e22b7SJung-uk Kim 
152a88e22b7SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
153a88e22b7SJung-uk Kim #include <contrib/dev/acpica/include/acapps.h>
154a88e22b7SJung-uk Kim #include <contrib/dev/acpica/compiler/dttemplate.h> /* Contains the hex ACPI table templates */
155a88e22b7SJung-uk Kim 
156a88e22b7SJung-uk Kim #define _COMPONENT          DT_COMPILER
157a88e22b7SJung-uk Kim         ACPI_MODULE_NAME    ("dttemplate")
158a88e22b7SJung-uk Kim 
159a88e22b7SJung-uk Kim 
160a88e22b7SJung-uk Kim /* Local prototypes */
161a88e22b7SJung-uk Kim 
162a88e22b7SJung-uk Kim static BOOLEAN
163a88e22b7SJung-uk Kim AcpiUtIsSpecialTable (
164a88e22b7SJung-uk Kim     char                    *Signature);
165a88e22b7SJung-uk Kim 
166a88e22b7SJung-uk Kim static ACPI_STATUS
167f8146b88SJung-uk Kim DtCreateOneTemplateFile (
168f8146b88SJung-uk Kim     char                    *Signature,
169f8146b88SJung-uk Kim     UINT32                  TableCount);
170f8146b88SJung-uk Kim 
171f8146b88SJung-uk Kim static ACPI_STATUS
172a88e22b7SJung-uk Kim DtCreateOneTemplate (
173a88e22b7SJung-uk Kim     char                    *Signature,
174f8146b88SJung-uk Kim     UINT32                  TableCount,
175a371a5fdSJung-uk Kim     const ACPI_DMTABLE_DATA *TableData);
176a88e22b7SJung-uk Kim 
177a88e22b7SJung-uk Kim static ACPI_STATUS
178a88e22b7SJung-uk Kim DtCreateAllTemplates (
179a88e22b7SJung-uk Kim     void);
180a88e22b7SJung-uk Kim 
181f8146b88SJung-uk Kim static int
182f8146b88SJung-uk Kim DtEmitDefinitionBlock (
183f8146b88SJung-uk Kim     FILE                    *File,
184f8146b88SJung-uk Kim     char                    *Filename,
185f8146b88SJung-uk Kim     char                    *Signature,
186f8146b88SJung-uk Kim     UINT32                  Instance);
187f8146b88SJung-uk Kim 
188a88e22b7SJung-uk Kim 
189a88e22b7SJung-uk Kim /*******************************************************************************
190a88e22b7SJung-uk Kim  *
191a88e22b7SJung-uk Kim  * FUNCTION:    AcpiUtIsSpecialTable
192a88e22b7SJung-uk Kim  *
193a88e22b7SJung-uk Kim  * PARAMETERS:  Signature           - ACPI table signature
194a88e22b7SJung-uk Kim  *
195a88e22b7SJung-uk Kim  * RETURN:      TRUE if signature is a special ACPI table
196a88e22b7SJung-uk Kim  *
197a88e22b7SJung-uk Kim  * DESCRIPTION: Check for valid ACPI tables that are not in the main ACPI
198a88e22b7SJung-uk Kim  *              table data structure (AcpiDmTableData).
199a88e22b7SJung-uk Kim  *
200a88e22b7SJung-uk Kim  ******************************************************************************/
201a88e22b7SJung-uk Kim 
202a88e22b7SJung-uk Kim static BOOLEAN
203a88e22b7SJung-uk Kim AcpiUtIsSpecialTable (
204a88e22b7SJung-uk Kim     char                    *Signature)
205a88e22b7SJung-uk Kim {
206a88e22b7SJung-uk Kim 
207278f0de6SJung-uk Kim     if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT) ||
208278f0de6SJung-uk Kim         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_OSDT) ||
209278f0de6SJung-uk Kim         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_SSDT) ||
210278f0de6SJung-uk Kim         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS) ||
211278f0de6SJung-uk Kim         ACPI_COMPARE_NAMESEG (Signature, ACPI_RSDP_NAME))
212a88e22b7SJung-uk Kim     {
213a88e22b7SJung-uk Kim         return (TRUE);
214a88e22b7SJung-uk Kim     }
215a88e22b7SJung-uk Kim 
216a88e22b7SJung-uk Kim     return (FALSE);
217a88e22b7SJung-uk Kim }
218a88e22b7SJung-uk Kim 
219a88e22b7SJung-uk Kim 
220a88e22b7SJung-uk Kim /*******************************************************************************
221a88e22b7SJung-uk Kim  *
222a88e22b7SJung-uk Kim  * FUNCTION:    DtCreateTemplates
223a88e22b7SJung-uk Kim  *
224f8146b88SJung-uk Kim  * PARAMETERS:  argv                - Standard command line arguments
225a88e22b7SJung-uk Kim  *
226a88e22b7SJung-uk Kim  * RETURN:      Status
227a88e22b7SJung-uk Kim  *
228a88e22b7SJung-uk Kim  * DESCRIPTION: Create one or more template files.
229a88e22b7SJung-uk Kim  *
230a88e22b7SJung-uk Kim  ******************************************************************************/
231a88e22b7SJung-uk Kim 
232a88e22b7SJung-uk Kim ACPI_STATUS
233a88e22b7SJung-uk Kim DtCreateTemplates (
234f8146b88SJung-uk Kim     char                    **argv)
235a88e22b7SJung-uk Kim {
236f8146b88SJung-uk Kim     char                    *Signature;
237f8146b88SJung-uk Kim     char                    *End;
238f8146b88SJung-uk Kim     unsigned long           TableCount;
239f8146b88SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
240a88e22b7SJung-uk Kim 
241a88e22b7SJung-uk Kim 
242a88e22b7SJung-uk Kim     AslInitializeGlobals ();
243bf6fac21SJung-uk Kim 
244f8146b88SJung-uk Kim     Status = AdInitialize ();
245f8146b88SJung-uk Kim     if (ACPI_FAILURE (Status))
246bf6fac21SJung-uk Kim     {
247f8146b88SJung-uk Kim         return (Status);
248bf6fac21SJung-uk Kim     }
249bf6fac21SJung-uk Kim 
250f8146b88SJung-uk Kim     /*
251f8146b88SJung-uk Kim      * Special cases for DSDT, ALL, and '*'
252f8146b88SJung-uk Kim      */
253f8146b88SJung-uk Kim 
254f8146b88SJung-uk Kim     /* Default (no signature option) is DSDT */
255f8146b88SJung-uk Kim 
256f8146b88SJung-uk Kim     if (AcpiGbl_Optind < 3)
257f8146b88SJung-uk Kim     {
258f8146b88SJung-uk Kim         Status = DtCreateOneTemplateFile (ACPI_SIG_DSDT, 0);
259f8146b88SJung-uk Kim         goto Exit;
260f8146b88SJung-uk Kim     }
261f8146b88SJung-uk Kim 
262f8146b88SJung-uk Kim     AcpiGbl_Optind--;
263f8146b88SJung-uk Kim     Signature = argv[AcpiGbl_Optind];
264a88e22b7SJung-uk Kim     AcpiUtStrupr (Signature);
265f8146b88SJung-uk Kim 
266f8146b88SJung-uk Kim     /*
267f8146b88SJung-uk Kim      * Multiple SSDT support (-T <ssdt count>)
268f8146b88SJung-uk Kim      */
269f8146b88SJung-uk Kim     TableCount = strtoul (Signature, &End, 0);
270f8146b88SJung-uk Kim     if (Signature != End)
271f8146b88SJung-uk Kim     {
272f8146b88SJung-uk Kim         /* The count is used for table ID and method name - max is 254(+1) */
273f8146b88SJung-uk Kim 
274f8146b88SJung-uk Kim         if (TableCount > 254)
275f8146b88SJung-uk Kim         {
276f8146b88SJung-uk Kim             fprintf (stderr, "%u SSDTs requested, maximum is 254\n",
277f8146b88SJung-uk Kim                 (unsigned int) TableCount);
278f8146b88SJung-uk Kim 
279f8146b88SJung-uk Kim             Status = AE_LIMIT;
280f8146b88SJung-uk Kim             goto Exit;
281f8146b88SJung-uk Kim         }
282f8146b88SJung-uk Kim 
283f8146b88SJung-uk Kim         Status = DtCreateOneTemplateFile (ACPI_SIG_DSDT, TableCount);
284f8146b88SJung-uk Kim         goto Exit;
285f8146b88SJung-uk Kim     }
286f8146b88SJung-uk Kim 
287f8146b88SJung-uk Kim     if (!strcmp (Signature, "ALL"))
288a88e22b7SJung-uk Kim     {
289bf6fac21SJung-uk Kim         /* Create all available/known templates */
290bf6fac21SJung-uk Kim 
291a88e22b7SJung-uk Kim         Status = DtCreateAllTemplates ();
292f8146b88SJung-uk Kim         goto Exit;
293f8146b88SJung-uk Kim     }
294f8146b88SJung-uk Kim 
295f8146b88SJung-uk Kim     /*
296f8146b88SJung-uk Kim      * Normal case: Create template for each signature
297f8146b88SJung-uk Kim      */
298f8146b88SJung-uk Kim     while (argv[AcpiGbl_Optind])
299f8146b88SJung-uk Kim     {
300f8146b88SJung-uk Kim         Signature = argv[AcpiGbl_Optind];
301f8146b88SJung-uk Kim         AcpiUtStrupr (Signature);
302f8146b88SJung-uk Kim 
303f8146b88SJung-uk Kim         Status = DtCreateOneTemplateFile (Signature, 0);
304f8146b88SJung-uk Kim         if (ACPI_FAILURE (Status))
305f8146b88SJung-uk Kim         {
306f8146b88SJung-uk Kim             goto Exit;
307f8146b88SJung-uk Kim         }
308f8146b88SJung-uk Kim 
309f8146b88SJung-uk Kim         AcpiGbl_Optind++;
310f8146b88SJung-uk Kim     }
311f8146b88SJung-uk Kim 
312f8146b88SJung-uk Kim 
313f8146b88SJung-uk Kim Exit:
314f8146b88SJung-uk Kim     /* Shutdown ACPICA subsystem */
315f8146b88SJung-uk Kim 
316f8146b88SJung-uk Kim     (void) AcpiTerminate ();
31767d9aa44SJung-uk Kim     UtDeleteLocalCaches ();
318a88e22b7SJung-uk Kim     return (Status);
319a88e22b7SJung-uk Kim }
320a88e22b7SJung-uk Kim 
321f8146b88SJung-uk Kim 
322f8146b88SJung-uk Kim /*******************************************************************************
323f8146b88SJung-uk Kim  *
324f8146b88SJung-uk Kim  * FUNCTION:    DtCreateOneTemplateFile
325f8146b88SJung-uk Kim  *
326f8146b88SJung-uk Kim  * PARAMETERS:  Signature           - ACPI table signature
327f8146b88SJung-uk Kim  *
328f8146b88SJung-uk Kim  * RETURN:      Status
329f8146b88SJung-uk Kim  *
330f8146b88SJung-uk Kim  * DESCRIPTION: Create one template file of the requested signature.
331f8146b88SJung-uk Kim  *
332f8146b88SJung-uk Kim  ******************************************************************************/
333f8146b88SJung-uk Kim 
334f8146b88SJung-uk Kim static ACPI_STATUS
335f8146b88SJung-uk Kim DtCreateOneTemplateFile (
336f8146b88SJung-uk Kim     char                    *Signature,
337f8146b88SJung-uk Kim     UINT32                  TableCount)
338f8146b88SJung-uk Kim {
339f8146b88SJung-uk Kim     const ACPI_DMTABLE_DATA *TableData;
340f8146b88SJung-uk Kim     ACPI_STATUS             Status;
341f8146b88SJung-uk Kim 
342f8146b88SJung-uk Kim 
343a88e22b7SJung-uk Kim     /*
344a88e22b7SJung-uk Kim      * Validate signature and get the template data:
345a88e22b7SJung-uk Kim      *  1) Signature must be 4 characters
346a88e22b7SJung-uk Kim      *  2) Signature must be a recognized ACPI table
347a88e22b7SJung-uk Kim      *  3) There must be a template associated with the signature
348a88e22b7SJung-uk Kim      */
349278f0de6SJung-uk Kim     if (strlen (Signature) != ACPI_NAMESEG_SIZE)
350a88e22b7SJung-uk Kim     {
351bf6fac21SJung-uk Kim         fprintf (stderr,
352f8146b88SJung-uk Kim             "%s: Invalid ACPI table signature "
353f8146b88SJung-uk Kim             "(length must be 4 characters)\n", Signature);
354a88e22b7SJung-uk Kim         return (AE_ERROR);
355a88e22b7SJung-uk Kim     }
356a88e22b7SJung-uk Kim 
357a88e22b7SJung-uk Kim     /*
358a88e22b7SJung-uk Kim      * Some slack for the two strange tables whose name is different than
359a88e22b7SJung-uk Kim      * their signatures: MADT->APIC and FADT->FACP.
360a88e22b7SJung-uk Kim      */
361a88e22b7SJung-uk Kim     if (!strcmp (Signature, "MADT"))
362a88e22b7SJung-uk Kim     {
363a88e22b7SJung-uk Kim         Signature = "APIC";
364a88e22b7SJung-uk Kim     }
365a88e22b7SJung-uk Kim     else if (!strcmp (Signature, "FADT"))
366a88e22b7SJung-uk Kim     {
367a88e22b7SJung-uk Kim         Signature = "FACP";
368a88e22b7SJung-uk Kim     }
369a88e22b7SJung-uk Kim 
370f8146b88SJung-uk Kim     /* TableData will point to the template */
371f8146b88SJung-uk Kim 
372a88e22b7SJung-uk Kim     TableData = AcpiDmGetTableData (Signature);
373a88e22b7SJung-uk Kim     if (TableData)
374a88e22b7SJung-uk Kim     {
375a88e22b7SJung-uk Kim         if (!TableData->Template)
376a88e22b7SJung-uk Kim         {
377bf6fac21SJung-uk Kim             fprintf (stderr, "%4.4s: No template available\n", Signature);
378a88e22b7SJung-uk Kim             return (AE_ERROR);
379a88e22b7SJung-uk Kim         }
380a88e22b7SJung-uk Kim     }
381a88e22b7SJung-uk Kim     else if (!AcpiUtIsSpecialTable (Signature))
382a88e22b7SJung-uk Kim     {
383a88e22b7SJung-uk Kim         fprintf (stderr,
384bf6fac21SJung-uk Kim             "%4.4s: Unrecognized ACPI table signature\n", Signature);
385a88e22b7SJung-uk Kim         return (AE_ERROR);
386a88e22b7SJung-uk Kim     }
387a88e22b7SJung-uk Kim 
388f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (Signature, TableCount, TableData);
389a88e22b7SJung-uk Kim     return (Status);
390a88e22b7SJung-uk Kim }
391a88e22b7SJung-uk Kim 
392a88e22b7SJung-uk Kim 
393a88e22b7SJung-uk Kim /*******************************************************************************
394a88e22b7SJung-uk Kim  *
395a88e22b7SJung-uk Kim  * FUNCTION:    DtCreateAllTemplates
396a88e22b7SJung-uk Kim  *
397a88e22b7SJung-uk Kim  * PARAMETERS:  None
398a88e22b7SJung-uk Kim  *
399a88e22b7SJung-uk Kim  * RETURN:      Status
400a88e22b7SJung-uk Kim  *
401a88e22b7SJung-uk Kim  * DESCRIPTION: Create all currently defined template files
402a88e22b7SJung-uk Kim  *
403a88e22b7SJung-uk Kim  ******************************************************************************/
404a88e22b7SJung-uk Kim 
405a88e22b7SJung-uk Kim static ACPI_STATUS
406a88e22b7SJung-uk Kim DtCreateAllTemplates (
407a88e22b7SJung-uk Kim     void)
408a88e22b7SJung-uk Kim {
409a371a5fdSJung-uk Kim     const ACPI_DMTABLE_DATA *TableData;
410a88e22b7SJung-uk Kim     ACPI_STATUS             Status;
411a88e22b7SJung-uk Kim 
412a88e22b7SJung-uk Kim 
413a88e22b7SJung-uk Kim     fprintf (stderr, "Creating all supported Template files\n");
414a88e22b7SJung-uk Kim 
415a88e22b7SJung-uk Kim     /* Walk entire ACPI table data structure */
416a88e22b7SJung-uk Kim 
417a88e22b7SJung-uk Kim     for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
418a88e22b7SJung-uk Kim     {
419a88e22b7SJung-uk Kim         /* If table has a template, create the template file */
420a88e22b7SJung-uk Kim 
421a88e22b7SJung-uk Kim         if (TableData->Template)
422a88e22b7SJung-uk Kim         {
423a88e22b7SJung-uk Kim             Status = DtCreateOneTemplate (TableData->Signature,
424f8146b88SJung-uk Kim                 0, TableData);
425a88e22b7SJung-uk Kim             if (ACPI_FAILURE (Status))
426a88e22b7SJung-uk Kim             {
427a88e22b7SJung-uk Kim                 return (Status);
428a88e22b7SJung-uk Kim             }
429a88e22b7SJung-uk Kim         }
430a88e22b7SJung-uk Kim     }
431a88e22b7SJung-uk Kim 
432a88e22b7SJung-uk Kim     /*
433f38b0f21SJung-uk Kim      * Create the special ACPI tables:
434a88e22b7SJung-uk Kim      * 1) DSDT/SSDT are AML tables, not data tables
435a88e22b7SJung-uk Kim      * 2) FACS and RSDP have non-standard headers
436a88e22b7SJung-uk Kim      */
437f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (ACPI_SIG_DSDT, 0, NULL);
438a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
439a88e22b7SJung-uk Kim     {
440a88e22b7SJung-uk Kim         return (Status);
441a88e22b7SJung-uk Kim     }
442a88e22b7SJung-uk Kim 
443f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (ACPI_SIG_SSDT, 0, NULL);
444a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
445a88e22b7SJung-uk Kim     {
446a88e22b7SJung-uk Kim         return (Status);
447a88e22b7SJung-uk Kim     }
448a88e22b7SJung-uk Kim 
449f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (ACPI_SIG_OSDT, 0, NULL);
450a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
451a88e22b7SJung-uk Kim     {
452a88e22b7SJung-uk Kim         return (Status);
453a88e22b7SJung-uk Kim     }
454a88e22b7SJung-uk Kim 
455f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (ACPI_SIG_FACS, 0, NULL);
456f8146b88SJung-uk Kim     if (ACPI_FAILURE (Status))
457f8146b88SJung-uk Kim     {
458f8146b88SJung-uk Kim         return (Status);
459f8146b88SJung-uk Kim     }
460f8146b88SJung-uk Kim 
461f8146b88SJung-uk Kim     Status = DtCreateOneTemplate (ACPI_RSDP_NAME, 0, NULL);
462a88e22b7SJung-uk Kim     if (ACPI_FAILURE (Status))
463a88e22b7SJung-uk Kim     {
464a88e22b7SJung-uk Kim         return (Status);
465a88e22b7SJung-uk Kim     }
466a88e22b7SJung-uk Kim 
467a88e22b7SJung-uk Kim     return (AE_OK);
468a88e22b7SJung-uk Kim }
469a88e22b7SJung-uk Kim 
470a88e22b7SJung-uk Kim 
471a88e22b7SJung-uk Kim /*******************************************************************************
472a88e22b7SJung-uk Kim  *
473a88e22b7SJung-uk Kim  * FUNCTION:    DtCreateOneTemplate
474a88e22b7SJung-uk Kim  *
475a88e22b7SJung-uk Kim  * PARAMETERS:  Signature           - ACPI signature, NULL terminated.
476f8146b88SJung-uk Kim  *              TableCount          - Used for SSDTs in same file as DSDT
477a88e22b7SJung-uk Kim  *              TableData           - Entry in ACPI table data structure.
478a88e22b7SJung-uk Kim  *                                    NULL if a special ACPI table.
479a88e22b7SJung-uk Kim  *
480a88e22b7SJung-uk Kim  * RETURN:      Status
481a88e22b7SJung-uk Kim  *
482a88e22b7SJung-uk Kim  * DESCRIPTION: Create one template source file for the requested ACPI table.
483a88e22b7SJung-uk Kim  *
484a88e22b7SJung-uk Kim  ******************************************************************************/
485a88e22b7SJung-uk Kim 
486a88e22b7SJung-uk Kim static ACPI_STATUS
487a88e22b7SJung-uk Kim DtCreateOneTemplate (
488a88e22b7SJung-uk Kim     char                    *Signature,
489f8146b88SJung-uk Kim     UINT32                  TableCount,
490a371a5fdSJung-uk Kim     const ACPI_DMTABLE_DATA  *TableData)
491a88e22b7SJung-uk Kim {
492a88e22b7SJung-uk Kim     char                    *DisasmFilename;
493a88e22b7SJung-uk Kim     FILE                    *File;
494a88e22b7SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
495f8146b88SJung-uk Kim     int                     Actual;
496f8146b88SJung-uk Kim     UINT32                  i;
497a88e22b7SJung-uk Kim 
498a88e22b7SJung-uk Kim 
499a88e22b7SJung-uk Kim     /* New file will have a .asl suffix */
500a88e22b7SJung-uk Kim 
501a88e22b7SJung-uk Kim     DisasmFilename = FlGenerateFilename (
502a88e22b7SJung-uk Kim         Signature, FILE_SUFFIX_ASL_CODE);
503a88e22b7SJung-uk Kim     if (!DisasmFilename)
504a88e22b7SJung-uk Kim     {
505a88e22b7SJung-uk Kim         fprintf (stderr, "Could not generate output filename\n");
506a88e22b7SJung-uk Kim         return (AE_ERROR);
507a88e22b7SJung-uk Kim     }
508a88e22b7SJung-uk Kim 
509a88e22b7SJung-uk Kim     AcpiUtStrlwr (DisasmFilename);
510f8146b88SJung-uk Kim     if (!UtQueryForOverwrite (DisasmFilename))
511f8146b88SJung-uk Kim     {
512*a009b7dcSJung-uk Kim         return (AE_OK);
513f8146b88SJung-uk Kim     }
514f8146b88SJung-uk Kim 
515a88e22b7SJung-uk Kim     File = fopen (DisasmFilename, "w+");
516a88e22b7SJung-uk Kim     if (!File)
517a88e22b7SJung-uk Kim     {
518f8146b88SJung-uk Kim         fprintf (stderr, "Could not open output file %s\n",
519f8146b88SJung-uk Kim             DisasmFilename);
520a88e22b7SJung-uk Kim         return (AE_ERROR);
521a88e22b7SJung-uk Kim     }
522a88e22b7SJung-uk Kim 
523a88e22b7SJung-uk Kim     /* Emit the common file header */
524a88e22b7SJung-uk Kim 
525a88e22b7SJung-uk Kim     AcpiOsRedirectOutput (File);
526a88e22b7SJung-uk Kim 
5273c1812acSJung-uk Kim     AcpiOsPrintf ("/*\n");
5283c1812acSJung-uk Kim     AcpiOsPrintf (ACPI_COMMON_HEADER ("iASL Compiler/Disassembler", " * "));
5293c1812acSJung-uk Kim 
530f8146b88SJung-uk Kim     if (TableCount == 0)
531f8146b88SJung-uk Kim     {
5325ef50723SJung-uk Kim         AcpiOsPrintf (" * Template for [%4.4s] ACPI Table",
533a88e22b7SJung-uk Kim             Signature);
534f8146b88SJung-uk Kim     }
535f8146b88SJung-uk Kim     else
536f8146b88SJung-uk Kim     {
537f8146b88SJung-uk Kim         AcpiOsPrintf (" * Template for [%4.4s] and %u [SSDT] ACPI Tables",
538f8146b88SJung-uk Kim             Signature, TableCount);
539f8146b88SJung-uk Kim     }
540a88e22b7SJung-uk Kim 
541a88e22b7SJung-uk Kim     /* Dump the actual ACPI table */
542a88e22b7SJung-uk Kim 
543a88e22b7SJung-uk Kim     if (TableData)
544a88e22b7SJung-uk Kim     {
545a88e22b7SJung-uk Kim         /* Normal case, tables that appear in AcpiDmTableData */
546a88e22b7SJung-uk Kim 
5475ef50723SJung-uk Kim         AcpiOsPrintf (" (static data table)\n");
5485ef50723SJung-uk Kim 
5496f1f1a63SJung-uk Kim         if (AslGbl_VerboseTemplates)
550a88e22b7SJung-uk Kim         {
551a88e22b7SJung-uk Kim             AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]"
552a88e22b7SJung-uk Kim                 "  FieldName : HexFieldValue\n */\n\n");
553a88e22b7SJung-uk Kim         }
554a88e22b7SJung-uk Kim         else
555a88e22b7SJung-uk Kim         {
556a88e22b7SJung-uk Kim             AcpiOsPrintf (" * Format: [ByteLength]"
5575ef50723SJung-uk Kim                 "  FieldName : HexFieldValue\n */\n");
558a88e22b7SJung-uk Kim         }
559a88e22b7SJung-uk Kim 
560a88e22b7SJung-uk Kim         AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
561a88e22b7SJung-uk Kim             TableData->Template));
562a88e22b7SJung-uk Kim     }
563a88e22b7SJung-uk Kim     else
564a88e22b7SJung-uk Kim     {
565f8146b88SJung-uk Kim         /* Special ACPI tables - DSDT, SSDT, OSDT, FACS, RSDP */
566a88e22b7SJung-uk Kim 
5675ef50723SJung-uk Kim         AcpiOsPrintf (" (AML byte code table)\n");
5685ef50723SJung-uk Kim         AcpiOsPrintf (" */\n");
569f8146b88SJung-uk Kim 
570278f0de6SJung-uk Kim         if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT))
571a88e22b7SJung-uk Kim         {
572f8146b88SJung-uk Kim             Actual = DtEmitDefinitionBlock (
573f8146b88SJung-uk Kim                 File, DisasmFilename, ACPI_SIG_DSDT, 1);
574f8146b88SJung-uk Kim             if (Actual < 0)
575ed17e06eSJung-uk Kim             {
576f8146b88SJung-uk Kim                 Status = AE_ERROR;
577f8146b88SJung-uk Kim                 goto Cleanup;
578f8146b88SJung-uk Kim             }
579f8146b88SJung-uk Kim 
580f8146b88SJung-uk Kim             /* Emit any requested SSDTs into the same file */
581f8146b88SJung-uk Kim 
582f8146b88SJung-uk Kim             for (i = 1; i <= TableCount; i++)
583f8146b88SJung-uk Kim             {
584f8146b88SJung-uk Kim                 Actual = DtEmitDefinitionBlock (
585f8146b88SJung-uk Kim                     File, DisasmFilename, ACPI_SIG_SSDT, i + 1);
586f8146b88SJung-uk Kim                 if (Actual < 0)
587f8146b88SJung-uk Kim                 {
588ed17e06eSJung-uk Kim                     Status = AE_ERROR;
589ed17e06eSJung-uk Kim                     goto Cleanup;
590ed17e06eSJung-uk Kim                 }
591a88e22b7SJung-uk Kim             }
592f8146b88SJung-uk Kim         }
593278f0de6SJung-uk Kim         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_SSDT))
594a88e22b7SJung-uk Kim         {
595f8146b88SJung-uk Kim             Actual = DtEmitDefinitionBlock (
596f8146b88SJung-uk Kim                 File, DisasmFilename, ACPI_SIG_SSDT, 1);
597f8146b88SJung-uk Kim             if (Actual < 0)
598ed17e06eSJung-uk Kim             {
599ed17e06eSJung-uk Kim                 Status = AE_ERROR;
600ed17e06eSJung-uk Kim                 goto Cleanup;
601a88e22b7SJung-uk Kim             }
602ed17e06eSJung-uk Kim         }
603278f0de6SJung-uk Kim         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_OSDT))
6045ef50723SJung-uk Kim         {
605f8146b88SJung-uk Kim             Actual = DtEmitDefinitionBlock (
606f8146b88SJung-uk Kim                 File, DisasmFilename, ACPI_SIG_OSDT, 1);
607f8146b88SJung-uk Kim             if (Actual < 0)
6085ef50723SJung-uk Kim             {
6095ef50723SJung-uk Kim                 Status = AE_ERROR;
6105ef50723SJung-uk Kim                 goto Cleanup;
6115ef50723SJung-uk Kim             }
6125ef50723SJung-uk Kim         }
613278f0de6SJung-uk Kim         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS))
614a88e22b7SJung-uk Kim         {
615a88e22b7SJung-uk Kim             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
616a88e22b7SJung-uk Kim                 TemplateFacs));
617a88e22b7SJung-uk Kim         }
618278f0de6SJung-uk Kim         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_RSDP_NAME))
619a88e22b7SJung-uk Kim         {
620a88e22b7SJung-uk Kim             AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
621a88e22b7SJung-uk Kim                 TemplateRsdp));
622a88e22b7SJung-uk Kim         }
623a88e22b7SJung-uk Kim         else
624a88e22b7SJung-uk Kim         {
625a88e22b7SJung-uk Kim             fprintf (stderr,
626a88e22b7SJung-uk Kim                 "%4.4s, Unrecognized ACPI table signature\n", Signature);
627ed17e06eSJung-uk Kim             Status = AE_ERROR;
628ed17e06eSJung-uk Kim             goto Cleanup;
629a88e22b7SJung-uk Kim         }
630a88e22b7SJung-uk Kim     }
631a88e22b7SJung-uk Kim 
632f8146b88SJung-uk Kim     if (TableCount == 0)
633f8146b88SJung-uk Kim     {
634a88e22b7SJung-uk Kim         fprintf (stderr,
635f8146b88SJung-uk Kim             "Created ACPI table template for [%4.4s], "
636f8146b88SJung-uk Kim             "written to \"%s\"\n",
637a88e22b7SJung-uk Kim             Signature, DisasmFilename);
638f8146b88SJung-uk Kim     }
639f8146b88SJung-uk Kim     else
640f8146b88SJung-uk Kim     {
641f8146b88SJung-uk Kim         fprintf (stderr,
642f8146b88SJung-uk Kim             "Created ACPI table templates for [%4.4s] "
643f8146b88SJung-uk Kim             "and %u [SSDT], written to \"%s\"\n",
644f8146b88SJung-uk Kim             Signature, TableCount, DisasmFilename);
645f8146b88SJung-uk Kim     }
646a88e22b7SJung-uk Kim 
647ed17e06eSJung-uk Kim Cleanup:
648a88e22b7SJung-uk Kim     fclose (File);
649a88e22b7SJung-uk Kim     AcpiOsRedirectOutput (stdout);
650a88e22b7SJung-uk Kim     return (Status);
651a88e22b7SJung-uk Kim }
652f8146b88SJung-uk Kim 
653f8146b88SJung-uk Kim 
654f8146b88SJung-uk Kim /*******************************************************************************
655f8146b88SJung-uk Kim  *
656f8146b88SJung-uk Kim  * FUNCTION:    DtEmitDefinitionBlock
657f8146b88SJung-uk Kim  *
658f8146b88SJung-uk Kim  * PARAMETERS:  File                - An open file for the block
659f8146b88SJung-uk Kim  *              Filename            - Filename for same, for error msg(s)
660f8146b88SJung-uk Kim  *              Signature           - ACPI signature for the block
661f8146b88SJung-uk Kim  *              Instance            - Used for multiple SSDTs in the same file
662f8146b88SJung-uk Kim  *
663f8146b88SJung-uk Kim  * RETURN:      Status from fprintf
664f8146b88SJung-uk Kim  *
665f8146b88SJung-uk Kim  * DESCRIPTION: Emit the raw ASL for a complete Definition Block (DSDT or SSDT)
666f8146b88SJung-uk Kim  *
667f8146b88SJung-uk Kim  * Note: The AMLFileName parameter for DefinitionBlock is left as a NULL
668f8146b88SJung-uk Kim  * string. This allows the compiler to create the output AML filename from
669f8146b88SJung-uk Kim  * the input filename.
670f8146b88SJung-uk Kim  *
671f8146b88SJung-uk Kim  ******************************************************************************/
672f8146b88SJung-uk Kim 
673f8146b88SJung-uk Kim static int
674f8146b88SJung-uk Kim DtEmitDefinitionBlock (
675f8146b88SJung-uk Kim     FILE                    *File,
676f8146b88SJung-uk Kim     char                    *Filename,
677f8146b88SJung-uk Kim     char                    *Signature,
678f8146b88SJung-uk Kim     UINT32                  Instance)
679f8146b88SJung-uk Kim {
680f8146b88SJung-uk Kim     int                     Status;
681f8146b88SJung-uk Kim 
682f8146b88SJung-uk Kim 
683f8146b88SJung-uk Kim     Status = fprintf (File,
684f8146b88SJung-uk Kim         "DefinitionBlock (\"\", \"%4.4s\", 2, \"Intel\", \"_%4.4s_%.2X\", 0x00000001)\n"
685f8146b88SJung-uk Kim         "{\n"
686f8146b88SJung-uk Kim         "    Method (%2.2s%.2X)\n"
687f8146b88SJung-uk Kim         "    {\n"
688f8146b88SJung-uk Kim         "    }\n"
689f8146b88SJung-uk Kim         "}\n\n",
690f8146b88SJung-uk Kim         Signature, Signature, Instance, Signature, Instance);
691f8146b88SJung-uk Kim 
692f8146b88SJung-uk Kim     if (Status < 0)
693f8146b88SJung-uk Kim     {
694f8146b88SJung-uk Kim         fprintf (stderr,
695f8146b88SJung-uk Kim             "Could not write %4.4s to output file %s\n",
696f8146b88SJung-uk Kim             Signature, Filename);
697f8146b88SJung-uk Kim     }
698f8146b88SJung-uk Kim 
699f8146b88SJung-uk Kim     return (Status);
700f8146b88SJung-uk Kim }
701