Debian Codegraph @

source/compiler/dttemplate.c

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Module Name: dttemplate - ACPI table template generation
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2026, 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 MERCHANTABILITY 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 "dttemplate.h" /* Contains the hex ACPI table templates */
47
48 #define _COMPONENT DT_COMPILER
49 ACPI_MODULE_NAME ("dttemplate")
50
51
52 /* Local prototypes */
53
54 static BOOLEAN
55 AcpiUtIsSpecialTable (
56 char *Signature);
57
58 static ACPI_STATUS
59 DtCreateOneTemplateFile (
60 char *Signature,
61 UINT32 TableCount);
62
63 static ACPI_STATUS
64 DtCreateOneTemplate (
65 char *Signature,
66 UINT32 TableCount,
67 const ACPI_DMTABLE_DATA *TableData);
68
69 static ACPI_STATUS
70 DtCreateAllTemplates (
71 void);
72
73 static int
74 DtEmitDefinitionBlock (
75 FILE *File,
76 char *Filename,
77 char *Signature,
78 UINT32 Instance);
79
80
81 /*******************************************************************************
82 *
83 * FUNCTION: AcpiUtIsSpecialTable
84 *
85 * PARAMETERS: Signature - ACPI table signature
86 *
87 * RETURN: TRUE if signature is a special ACPI table
88 *
89 * DESCRIPTION: Check for valid ACPI tables that are not in the main ACPI
90 * table data structure (AcpiDmTableData).
91 *
92 ******************************************************************************/
93
94 static BOOLEAN
95 AcpiUtIsSpecialTable (
96 char *Signature)
97 {
98
99 if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT) ||
100 ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_OSDT) ||
101 ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_SSDT) ||
102 ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS) ||
103 ACPI_COMPARE_NAMESEG (Signature, ACPI_RSDP_NAME))
104 {
105 return (TRUE);
106 }
107
108 return (FALSE);
109 }
110
111
112 /*******************************************************************************
113 *
114 * FUNCTION: DtCreateTemplates
115 *
116 * PARAMETERS: argv - Standard command line arguments
117 *
118 * RETURN: Status
119 *
120 * DESCRIPTION: Create one or more template files.
121 *
122 ******************************************************************************/
123
124 ACPI_STATUS
125 DtCreateTemplates (
126 char **argv)
127 {
128 char *Signature;
129 char *End;
130 unsigned long TableCount;
131 ACPI_STATUS Status = AE_OK;
132
133
134 AslInitializeGlobals ();
135
136 Status = AdInitialize ();
137 if (ACPI_FAILURE (Status))
138 {
139 return (Status);
140 }
141
142 /*
143 * Special cases for DSDT, ALL, and '*'
144 */
145
146 /* Default (no signature option) is DSDT */
147
148 if (AcpiGbl_Optind < 3)
149 {
150 fprintf (stdout, "Creating default template: [DSDT]\n");
151 Status = DtCreateOneTemplateFile (ACPI_SIG_DSDT, 0);
152 goto Exit;
153 }
154
155 AcpiGbl_Optind--;
156 Signature = argv[AcpiGbl_Optind];
157 AcpiUtStrupr (Signature);
158
159 /*
160 * Multiple SSDT support (-T <ssdt count>)
161 */
162 TableCount = strtoul (Signature, &End, 0);
163 if (Signature != End)
164 {
165 /* The count is used for table ID and method name - max is 254(+1) */
166
167 if (TableCount > 254)
168 {
169 fprintf (stderr, "%u SSDTs requested, maximum is 254\n",
170 (unsigned int) TableCount);
171
172 Status = AE_LIMIT;
173 goto Exit;
174 }
175
176 Status = DtCreateOneTemplateFile (ACPI_SIG_DSDT, TableCount);
177 goto Exit;
178 }
179
180 if (!strcmp (Signature, "ALL"))
181 {
182 /* Create all available/known templates */
183
184 Status = DtCreateAllTemplates ();
185 goto Exit;
186 }
187
188 /*
189 * Normal case: Create template for each signature
190 */
191 while (argv[AcpiGbl_Optind])
192 {
193 Signature = argv[AcpiGbl_Optind];
194 AcpiUtStrupr (Signature);
195
196 Status = DtCreateOneTemplateFile (Signature, 0);
197 if (ACPI_FAILURE (Status))
198 {
199 goto Exit;
200 }
201
202 AcpiGbl_Optind++;
203 }
204
205
206 Exit:
207 /* Shutdown ACPICA subsystem */
208
209 (void) AcpiTerminate ();
210 UtDeleteLocalCaches ();
211 return (Status);
212 }
213
214
215 /*******************************************************************************
216 *
217 * FUNCTION: DtCreateOneTemplateFile
218 *
219 * PARAMETERS: Signature - ACPI table signature
220 *
221 * RETURN: Status
222 *
223 * DESCRIPTION: Create one template file of the requested signature.
224 *
225 ******************************************************************************/
226
227 static ACPI_STATUS
228 DtCreateOneTemplateFile (
229 char *Signature,
230 UINT32 TableCount)
231 {
232 const ACPI_DMTABLE_DATA *TableData;
233 ACPI_STATUS Status;
234
235
236 /*
237 * Validate signature and get the template data:
238 * 1) Signature must be 4 characters
239 * 2) Signature must be a recognized ACPI table
240 * 3) There must be a template associated with the signature
241 */
242 if (strlen (Signature) != ACPI_NAMESEG_SIZE)
243 {
244 fprintf (stderr,
245 "%s: Invalid ACPI table signature "
246 "(length must be 4 characters)\n", Signature);
247 return (AE_ERROR);
248 }
249
250 /*
251 * Some slack for the two strange tables whose name is different than
252 * their signatures: MADT->APIC and FADT->FACP.
253 */
254 if (!strcmp (Signature, "MADT"))
255 {
256 Signature = "APIC";
257 }
258 else if (!strcmp (Signature, "FADT"))
259 {
260 Signature = "FACP";
261 }
262
263 /* TableData will point to the template */
264
265 TableData = AcpiDmGetTableData (Signature);
266 if (TableData)
267 {
268 if (!TableData->Template)
269 {
270 fprintf (stderr, "%4.4s: No template available\n", Signature);
271 return (AE_ERROR);
272 }
273 }
274 else if (!AcpiUtIsSpecialTable (Signature))
275 {
276 fprintf (stderr,
277 "%4.4s: Unrecognized ACPI table signature\n", Signature);
278 return (AE_ERROR);
279 }
280
281 Status = DtCreateOneTemplate (Signature, TableCount, TableData);
282 return (Status);
283 }
284
285
286 /*******************************************************************************
287 *
288 * FUNCTION: DtCreateAllTemplates
289 *
290 * PARAMETERS: None
291 *
292 * RETURN: Status
293 *
294 * DESCRIPTION: Create all currently defined template files
295 *
296 ******************************************************************************/
297
298 static ACPI_STATUS
299 DtCreateAllTemplates (
300 void)
301 {
302 const ACPI_DMTABLE_DATA *TableData;
303 ACPI_STATUS Status;
304
305
306 fprintf (stdout, "Creating all supported Template files\n");
307
308 /* Walk entire ACPI table data structure */
309
310 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
311 {
312 /* If table has a template, create the template file */
313
314 if (TableData->Template)
315 {
316 if (ACPI_COMPARE_NAMESEG (TableData->Signature, ACPI_SIG_CDAT))
317 /* Special handling of CDAT */
318 Status = DtCreateOneTemplate (TableData->Signature,
319 0, NULL);
320 else
321 Status = DtCreateOneTemplate (TableData->Signature,
322 0, TableData);
323 if (ACPI_FAILURE (Status))
324 {
325 return (Status);
326 }
327 }
328 }
329
330 /*
331 * Create the special ACPI tables:
332 * 1) DSDT/SSDT are AML tables, not data tables
333 * 2) FACS and RSDP have non-standard headers
334 */
335 Status = DtCreateOneTemplate (ACPI_SIG_DSDT, 0, NULL);
336 if (ACPI_FAILURE (Status))
337 {
338 return (Status);
339 }
340
341 Status = DtCreateOneTemplate (ACPI_SIG_SSDT, 0, NULL);
342 if (ACPI_FAILURE (Status))
343 {
344 return (Status);
345 }
346
347 Status = DtCreateOneTemplate (ACPI_SIG_OSDT, 0, NULL);
348 if (ACPI_FAILURE (Status))
349 {
350 return (Status);
351 }
352
353 Status = DtCreateOneTemplate (ACPI_SIG_FACS, 0, NULL);
354 if (ACPI_FAILURE (Status))
355 {
356 return (Status);
357 }
358
359 Status = DtCreateOneTemplate (ACPI_RSDP_NAME, 0, NULL);
360 if (ACPI_FAILURE (Status))
361 {
362 return (Status);
363 }
364
365 return (AE_OK);
366 }
367
368
369 /*******************************************************************************
370 *
371 * FUNCTION: DtCreateOneTemplate
372 *
373 * PARAMETERS: Signature - ACPI signature, NULL terminated.
374 * TableCount - Used for SSDTs in same file as DSDT
375 * TableData - Entry in ACPI table data structure.
376 * NULL if a special ACPI table.
377 *
378 * RETURN: Status
379 *
380 * DESCRIPTION: Create one template source file for the requested ACPI table.
381 *
382 ******************************************************************************/
383
384 static ACPI_STATUS
385 DtCreateOneTemplate (
386 char *Signature,
387 UINT32 TableCount,
388 const ACPI_DMTABLE_DATA *TableData)
389 {
390 char *DisasmFilename;
391 FILE *File;
392 ACPI_STATUS Status = AE_OK;
393 int Actual;
394 UINT32 i;
395
396
397 /* New file will have a .asl suffix */
398
399 DisasmFilename = FlGenerateFilename (
400 Signature, FILE_SUFFIX_ASL_CODE);
401 if (!DisasmFilename)
402 {
403 fprintf (stderr, "Could not generate output filename\n");
404 return (AE_ERROR);
405 }
406
407 AcpiUtStrlwr (DisasmFilename);
408 if (!UtQueryForOverwrite (DisasmFilename))
409 {
410 return (AE_OK);
411 }
412
413 File = fopen (DisasmFilename, "w+");
414 if (!File)
415 {
416 fprintf (stderr, "Could not open output file %s\n",
417 DisasmFilename);
418 return (AE_ERROR);
419 }
420
421 /* Emit the common file header */
422
423 AcpiOsRedirectOutput (File);
424
425 AcpiOsPrintf ("/*\n");
426 AcpiOsPrintf (ACPI_COMMON_HEADER ("iASL Compiler/Disassembler", " * "));
427
428 if (TableCount == 0)
429 {
430 AcpiOsPrintf (" * Template for [%4.4s] ACPI Table",
431 Signature);
432 }
433 else
434 {
435 AcpiOsPrintf (" * Template for [%4.4s] and %u [SSDT] ACPI Tables",
436 Signature, TableCount);
437 }
438
439 /* Dump the actual ACPI table */
440
441 if (TableData)
442 {
443 /* Normal case, tables that appear in AcpiDmTableData */
444
445 AcpiOsPrintf (" (static data table)\n");
446
447 if (AslGbl_VerboseTemplates)
448 {
449 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]"
450 " FieldName : HexFieldValue\n */\n\n");
451 }
452 else
453 {
454 AcpiOsPrintf (" * Format: [ByteLength]"
455 " FieldName : HexFieldValue\n */\n");
456 }
457
458 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
459 TableData->Template));
460 }
461 else
462 {
463 /* Special ACPI tables - DSDT, SSDT, OSDT, FACS, RSDP, CDAT */
464
465 AcpiOsPrintf (" (AML byte code table)\n");
466 AcpiOsPrintf (" */\n");
467
468 if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT))
469 {
470 Actual = DtEmitDefinitionBlock (
471 File, DisasmFilename, ACPI_SIG_DSDT, 1);
472 if (Actual < 0)
473 {
474 Status = AE_ERROR;
475 goto Cleanup;
476 }
477
478 /* Emit any requested SSDTs into the same file */
479
480 for (i = 1; i <= TableCount; i++)
481 {
482 Actual = DtEmitDefinitionBlock (
483 File, DisasmFilename, ACPI_SIG_SSDT, i + 1);
484 if (Actual < 0)
485 {
486 Status = AE_ERROR;
487 goto Cleanup;
488 }
489 }
490 }
491 else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_SSDT))
492 {
493 Actual = DtEmitDefinitionBlock (
494 File, DisasmFilename, ACPI_SIG_SSDT, 1);
495 if (Actual < 0)
496 {
497 Status = AE_ERROR;
498 goto Cleanup;
499 }
500 }
501 else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_OSDT))
502 {
503 Actual = DtEmitDefinitionBlock (
504 File, DisasmFilename, ACPI_SIG_OSDT, 1);
505 if (Actual < 0)
506 {
507 Status = AE_ERROR;
508 goto Cleanup;
509 }
510 }
511 else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS))
512 {
513 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
514 TemplateFacs));
515 }
516 else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_RSDP_NAME))
517 {
518 AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
519 TemplateRsdp));
520 }
521 else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_CDAT))
522 {
523 AcpiDmDumpCdat (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
524 TemplateCdat));
525 }
526 else
527 {
528 fprintf (stderr,
529 "%4.4s, Unrecognized ACPI table signature\n", Signature);
530 Status = AE_ERROR;
531 goto Cleanup;
532 }
533 }
534
535 if (TableCount == 0)
536 {
537 fprintf (stdout,
538 "Created ACPI table template for [%4.4s], "
539 "written to \"%s\"\n",
540 Signature, DisasmFilename);
541 }
542 else
543 {
544 fprintf (stdout,
545 "Created ACPI table templates for [%4.4s] "
546 "and %u [SSDT] in same file, written to \"%s\"\n",
547 Signature, TableCount, DisasmFilename);
548 }
549
550 Cleanup:
551 fclose (File);
552 AcpiOsRedirectOutput (stdout);
553 return (Status);
554 }
555
556
557 /*******************************************************************************
558 *
559 * FUNCTION: DtEmitDefinitionBlock
560 *
561 * PARAMETERS: File - An open file for the block
562 * Filename - Filename for same, for error msg(s)
563 * Signature - ACPI signature for the block
564 * Instance - Used for multiple SSDTs in the same file
565 *
566 * RETURN: Status from fprintf
567 *
568 * DESCRIPTION: Emit the raw ASL for a complete Definition Block (DSDT or SSDT)
569 *
570 * Note: The AMLFileName parameter for DefinitionBlock is left as a NULL
571 * string. This allows the compiler to create the output AML filename from
572 * the input filename.
573 *
574 ******************************************************************************/
575
576 static int
577 DtEmitDefinitionBlock (
578 FILE *File,
579 char *Filename,
580 char *Signature,
581 UINT32 Instance)
582 {
583 int Status;
584
585
586 Status = fprintf (File,
587 "DefinitionBlock (\"\", \"%4.4s\", 2, \"Intel\", \"_%4.4s_%.2X\", 0x00000001)\n"
588 "{\n"
589 " Method (%2.2s%.2X)\n"
590 " {\n"
591 " }\n"
592 "}\n\n",
593 Signature, Signature, Instance, Signature, Instance);
594
595 if (Status < 0)
596 {
597 fprintf (stderr,
598 "Could not write %4.4s to output file %s\n",
599 Signature, Filename);
600 }
601
602 return (Status);
603 }