Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / dttemplate.c
blob73708558d0ee68d3757863d714210d90c7cbf596
1 /******************************************************************************
3 * Module Name: dttemplate - ACPI table template generation
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include "aslcompiler.h"
45 #include "acapps.h"
46 #include "dtcompiler.h"
47 #include "dttemplate.h" /* Contains the hex ACPI table templates */
49 #define _COMPONENT DT_COMPILER
50 ACPI_MODULE_NAME ("dttemplate")
53 /* Local prototypes */
55 static BOOLEAN
56 AcpiUtIsSpecialTable (
57 char *Signature);
59 static ACPI_STATUS
60 DtCreateOneTemplate (
61 char *Signature,
62 ACPI_DMTABLE_DATA *TableData);
64 static ACPI_STATUS
65 DtCreateAllTemplates (
66 void);
69 /*******************************************************************************
71 * FUNCTION: AcpiUtIsSpecialTable
73 * PARAMETERS: Signature - ACPI table signature
75 * RETURN: TRUE if signature is a special ACPI table
77 * DESCRIPTION: Check for valid ACPI tables that are not in the main ACPI
78 * table data structure (AcpiDmTableData).
80 ******************************************************************************/
82 static BOOLEAN
83 AcpiUtIsSpecialTable (
84 char *Signature)
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))
92 return (TRUE);
95 return (FALSE);
99 /*******************************************************************************
101 * FUNCTION: DtCreateTemplates
103 * PARAMETERS: Signature - ACPI table signature
105 * RETURN: Status
107 * DESCRIPTION: Create one or more template files.
109 ******************************************************************************/
111 ACPI_STATUS
112 DtCreateTemplates (
113 char *Signature)
115 ACPI_DMTABLE_DATA *TableData;
116 ACPI_STATUS Status;
119 AslInitializeGlobals ();
121 /* Default (no signature) is DSDT */
123 if (!Signature)
125 Signature = "DSDT";
126 goto GetTemplate;
129 AcpiUtStrupr (Signature);
130 if (!ACPI_STRCMP (Signature, "ALL") ||
131 !ACPI_STRCMP (Signature, "*"))
133 /* Create all available/known templates */
135 Status = DtCreateAllTemplates ();
136 return (Status);
140 * Validate signature and get the template data:
141 * 1) Signature must be 4 characters
142 * 2) Signature must be a recognized ACPI table
143 * 3) There must be a template associated with the signature
145 if (strlen (Signature) != ACPI_NAME_SIZE)
147 fprintf (stderr,
148 "%s: Invalid ACPI table signature (length must be 4 characters)\n",
149 Signature);
150 return (AE_ERROR);
154 * Some slack for the two strange tables whose name is different than
155 * their signatures: MADT->APIC and FADT->FACP.
157 if (!strcmp (Signature, "MADT"))
159 Signature = "APIC";
161 else if (!strcmp (Signature, "FADT"))
163 Signature = "FACP";
166 GetTemplate:
167 TableData = AcpiDmGetTableData (Signature);
168 if (TableData)
170 if (!TableData->Template)
172 fprintf (stderr, "%4.4s: No template available\n", Signature);
173 return (AE_ERROR);
176 else if (!AcpiUtIsSpecialTable (Signature))
178 fprintf (stderr,
179 "%4.4s: Unrecognized ACPI table signature\n", Signature);
180 return (AE_ERROR);
183 Status = AdInitialize ();
184 if (ACPI_FAILURE (Status))
186 return (Status);
189 Status = DtCreateOneTemplate (Signature, TableData);
190 return (Status);
194 /*******************************************************************************
196 * FUNCTION: DtCreateAllTemplates
198 * PARAMETERS: None
200 * RETURN: Status
202 * DESCRIPTION: Create all currently defined template files
204 ******************************************************************************/
206 static ACPI_STATUS
207 DtCreateAllTemplates (
208 void)
210 ACPI_DMTABLE_DATA *TableData;
211 ACPI_STATUS Status;
214 Status = AdInitialize ();
215 if (ACPI_FAILURE (Status))
217 return (Status);
220 fprintf (stderr, "Creating all supported Template files\n");
222 /* Walk entire ACPI table data structure */
224 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
226 /* If table has a template, create the template file */
228 if (TableData->Template)
230 Status = DtCreateOneTemplate (TableData->Signature,
231 TableData);
232 if (ACPI_FAILURE (Status))
234 return (Status);
240 * Create the special ACPI tables:
241 * 1) DSDT/SSDT are AML tables, not data tables
242 * 2) FACS and RSDP have non-standard headers
244 Status = DtCreateOneTemplate (ACPI_SIG_DSDT, NULL);
245 if (ACPI_FAILURE (Status))
247 return (Status);
250 Status = DtCreateOneTemplate (ACPI_SIG_SSDT, NULL);
251 if (ACPI_FAILURE (Status))
253 return (Status);
256 Status = DtCreateOneTemplate (ACPI_SIG_FACS, NULL);
257 if (ACPI_FAILURE (Status))
259 return (Status);
262 Status = DtCreateOneTemplate (ACPI_RSDP_NAME, NULL);
263 if (ACPI_FAILURE (Status))
265 return (Status);
268 return (AE_OK);
272 /*******************************************************************************
274 * FUNCTION: DtCreateOneTemplate
276 * PARAMETERS: Signature - ACPI signature, NULL terminated.
277 * TableData - Entry in ACPI table data structure.
278 * NULL if a special ACPI table.
280 * RETURN: Status
282 * DESCRIPTION: Create one template source file for the requested ACPI table.
284 ******************************************************************************/
286 static ACPI_STATUS
287 DtCreateOneTemplate (
288 char *Signature,
289 ACPI_DMTABLE_DATA *TableData)
291 char *DisasmFilename;
292 FILE *File;
293 ACPI_STATUS Status = AE_OK;
294 ACPI_SIZE Actual;
297 /* New file will have a .asl suffix */
299 DisasmFilename = FlGenerateFilename (
300 Signature, FILE_SUFFIX_ASL_CODE);
301 if (!DisasmFilename)
303 fprintf (stderr, "Could not generate output filename\n");
304 return (AE_ERROR);
307 /* Probably should prompt to overwrite the file */
309 AcpiUtStrlwr (DisasmFilename);
310 File = fopen (DisasmFilename, "w+");
311 if (!File)
313 fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
314 return (AE_ERROR);
317 /* Emit the common file header */
319 AcpiOsRedirectOutput (File);
321 AcpiOsPrintf ("/*\n");
322 AcpiOsPrintf (ACPI_COMMON_HEADER ("iASL Compiler/Disassembler", " * "));
324 AcpiOsPrintf (" * Template for [%4.4s] ACPI Table\n",
325 Signature);
327 /* Dump the actual ACPI table */
329 if (TableData)
331 /* Normal case, tables that appear in AcpiDmTableData */
333 if (Gbl_VerboseTemplates)
335 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]"
336 " FieldName : HexFieldValue\n */\n\n");
338 else
340 AcpiOsPrintf (" * Format: [ByteLength]"
341 " FieldName : HexFieldValue\n */\n\n");
344 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
345 TableData->Template));
347 else
349 /* Special ACPI tables - DSDT, SSDT, FADT, RSDP */
351 AcpiOsPrintf (" */\n\n");
352 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT))
354 Actual = fwrite (TemplateDsdt, 1, sizeof (TemplateDsdt) -1, File);
355 if (Actual != sizeof (TemplateDsdt) -1)
357 fprintf (stderr,
358 "Could not write to output file %s\n", DisasmFilename);
359 Status = AE_ERROR;
360 goto Cleanup;
363 else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT))
365 Actual = fwrite (TemplateSsdt, 1, sizeof (TemplateSsdt) -1, File);
366 if (Actual != sizeof (TemplateSsdt) -1)
368 fprintf (stderr,
369 "Could not write to output file %s\n", DisasmFilename);
370 Status = AE_ERROR;
371 goto Cleanup;
374 else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS)) /* FADT */
376 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
377 TemplateFacs));
379 else if (ACPI_COMPARE_NAME (Signature, ACPI_RSDP_NAME))
381 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
382 TemplateRsdp));
384 else
386 fprintf (stderr,
387 "%4.4s, Unrecognized ACPI table signature\n", Signature);
388 Status = AE_ERROR;
389 goto Cleanup;
393 fprintf (stderr,
394 "Created ACPI table template for [%4.4s], written to \"%s\"\n",
395 Signature, DisasmFilename);
397 Cleanup:
398 fclose (File);
399 AcpiOsRedirectOutput (stdout);
400 ACPI_FREE (DisasmFilename);
401 return (Status);