Al Stone b61377
From f0d66bb3979a2bcba84027cb2d09f5de76fec9a4 Mon Sep 17 00:00:00 2001
Al Stone b61377
From: Sami Mujawar <sami.mujawar@arm.com>
Al Stone b61377
Date: Fri, 21 Apr 2017 17:07:03 +0100
Al Stone b61377
Subject: [PATCH] Add support for hex AML C header file generation
Al Stone b61377
Al Stone b61377
The iAsl compiler provides a list of options to support
Al Stone b61377
'Firmware Support - C Text Output'.
Al Stone b61377
Al Stone b61377
One of the options, '-tc' supports generation of AML hex
Al Stone b61377
tables in C. This command generates a character array that
Al Stone b61377
represents the AML hex table data.
Al Stone b61377
        e.g. unsigned char AmlCode[] = {
Al Stone b61377
               // HEX AML data
Al Stone b61377
               ...
Al Stone b61377
               };
Al Stone b61377
Al Stone b61377
However, there is no option to change the name of the
Al Stone b61377
generated character array (AmlCode) in the output hex file.
Al Stone b61377
This limits the ability to reference more than one table from
Al Stone b61377
the firmware code.
Al Stone b61377
Al Stone b61377
Also, the generated output does not have header file include
Al Stone b61377
guards. This is ideally desired if the hex file is to be
Al Stone b61377
included from a C source file.
Al Stone b61377
Al Stone b61377
A solution to this is to modify the Hex C output such that
Al Stone b61377
the name of the array is derived from the input file name,
Al Stone b61377
and to add header file include guards.
Al Stone b61377
Al Stone b61377
To address the above, without breaking existing implementations,
Al Stone b61377
a new option '-th' is introduced in the list of supported
Al Stone b61377
'Firmware Support - C Text Output' commands. This option
Al Stone b61377
generates an AML hex table output in a C header file suitable
Al Stone b61377
for including from a C source file.
Al Stone b61377
Al Stone b61377
The '-th' option:
Al Stone b61377
1. Uses the input file name as a prefix for the generated
Al Stone b61377
   C array name.
Al Stone b61377
   e.g. 'unsigned char DadtAmlCode[]'
Al Stone b61377
Al Stone b61377
2. Adds a header file include guard.
Al Stone b61377
   e.g.
Al Stone b61377
        #ifndef DSDT_HEX_
Al Stone b61377
        #define DSDT_HEX_
Al Stone b61377
        ...
Al Stone b61377
Al Stone b61377
        unsigned char DadtAmlCode[] = {
Al Stone b61377
                // HEX AML data
Al Stone b61377
                ...
Al Stone b61377
        };
Al Stone b61377
Al Stone b61377
        #endif // DSDT_HEX_
Al Stone b61377
Al Stone b61377
  Where Dadt.aml is the input file name.
Al Stone b61377
Al Stone b61377
  Note: The header file include guard uses '<inputfilename>_HEX_'
Al Stone b61377
        format instead of '<inputfilename>_H_' to avoid possible
Al Stone b61377
        collision with an existing header guard.
Al Stone b61377
Al Stone b61377
Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Al Stone b61377
Signed-off-by: Evan Lloyd <evan.lloyd@arm.com>
Al Stone b61377
Al Stone b61377
Github-Location: https://github.com/acpica/acpica/pull/368/commits/f0d66bb3979a2bcba84027cb2d09f5de76fec9a4
Al Stone b61377
Al Stone b61377
---
Al Stone b61377
 source/compiler/aslcompile.c |  12 +++-
Al Stone b61377
 source/compiler/aslglobal.h  |   1 +
Al Stone b61377
 source/compiler/aslhelp.c    |   1 +
Al Stone b61377
 source/compiler/aslhex.c     | 154 +++++++++++++++++++++++++++++++++++++++++++
Al Stone b61377
 source/compiler/asloptions.c |   5 ++
Al Stone b61377
 source/include/acapps.h      |   6 ++
Al Stone b61377
 6 files changed, 176 insertions(+), 3 deletions(-)
Al Stone b61377
Al Stone b61377
Index: acpica-unix2-20180209/source/compiler/aslcompile.c
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/compiler/aslcompile.c
Al Stone b61377
+++ acpica-unix2-20180209/source/compiler/aslcompile.c
Al Stone b61377
@@ -436,7 +436,8 @@ AslCompilerSignon (
Al Stone b61377
             Prefix = "; ";
Al Stone b61377
         }
Al Stone b61377
         else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
Al Stone b61377
-                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
Al Stone b61377
+                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL) ||
Al Stone b61377
+                 (Gbl_HexOutputFlag == HEX_OUTPUT_H))
Al Stone b61377
         {
Al Stone b61377
             FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
Al Stone b61377
             Prefix = " * ";
Al Stone b61377
@@ -471,7 +472,11 @@ AslCompilerSignon (
Al Stone b61377
     /* Compiler signon with copyright */
Al Stone b61377
 
Al Stone b61377
     FlPrintFile (FileId, "%s\n", Prefix);
Al Stone b61377
-    FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
Al Stone b61377
+    if (Gbl_HexOutputFlag == HEX_OUTPUT_H) {
Al Stone b61377
+        FlPrintFile (FileId, ACPI_COMMON_HEADER_NO_COPYRIGHT (UtilityName, Prefix));
Al Stone b61377
+    } else {
Al Stone b61377
+        FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
Al Stone b61377
+    }
Al Stone b61377
 }
Al Stone b61377
 
Al Stone b61377
 
Al Stone b61377
@@ -513,7 +518,8 @@ AslCompilerFileHeader (
Al Stone b61377
             Prefix = "; ";
Al Stone b61377
         }
Al Stone b61377
         else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
Al Stone b61377
-                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
Al Stone b61377
+                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL) ||
Al Stone b61377
+                 (Gbl_HexOutputFlag == HEX_OUTPUT_H))
Al Stone b61377
         {
Al Stone b61377
             Prefix = " * ";
Al Stone b61377
         }
Al Stone b61377
Index: acpica-unix2-20180209/source/compiler/aslglobal.h
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/compiler/aslglobal.h
Al Stone b61377
+++ acpica-unix2-20180209/source/compiler/aslglobal.h
Al Stone b61377
@@ -220,6 +220,7 @@ ASL_EXTERN BOOLEAN                  ASL_
Al Stone b61377
 #define HEX_OUTPUT_C                1
Al Stone b61377
 #define HEX_OUTPUT_ASM              2
Al Stone b61377
 #define HEX_OUTPUT_ASL              3
Al Stone b61377
+#define HEX_OUTPUT_H                4
Al Stone b61377
 
Al Stone b61377
 ASL_EXTERN BOOLEAN                  ASL_INIT_GLOBAL (Gbl_HexOutputFlag, HEX_OUTPUT_NONE);
Al Stone b61377
 
Al Stone b61377
Index: acpica-unix2-20180209/source/compiler/aslhelp.c
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/compiler/aslhelp.c
Al Stone b61377
+++ acpica-unix2-20180209/source/compiler/aslhelp.c
Al Stone b61377
@@ -121,6 +121,7 @@ Usage (
Al Stone b61377
 
Al Stone b61377
     printf ("\nFirmware Support - C Text Output:\n");
Al Stone b61377
     ACPI_OPTION ("-tc",             "Create hex AML table in C (*.hex)");
Al Stone b61377
+    ACPI_OPTION ("-th",             "Create hex AML table in C header (*.hex)");
Al Stone b61377
     ACPI_OPTION ("-sc",             "Create named hex AML arrays in C (*.c)");
Al Stone b61377
     ACPI_OPTION ("-ic",             "Create include file in C for -sc symbols (*.h)");
Al Stone b61377
     ACPI_OPTION ("-so",             "Create namespace AML offset table in C (*.offset.h)");
Al Stone b61377
Index: acpica-unix2-20180209/source/compiler/aslhex.c
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/compiler/aslhex.c
Al Stone b61377
+++ acpica-unix2-20180209/source/compiler/aslhex.c
Al Stone b61377
@@ -46,6 +46,10 @@
Al Stone b61377
 #define _COMPONENT          ACPI_COMPILER
Al Stone b61377
         ACPI_MODULE_NAME    ("ashex")
Al Stone b61377
 
Al Stone b61377
+#ifndef PATH_MAX
Al Stone b61377
+#define PATH_MAX 256
Al Stone b61377
+#endif
Al Stone b61377
+
Al Stone b61377
 /*
Al Stone b61377
  * This module emits ASCII hex output files in either C, ASM, or ASL format
Al Stone b61377
  */
Al Stone b61377
@@ -64,6 +68,10 @@ static void
Al Stone b61377
 HxDoHexOutputAsm (
Al Stone b61377
     void);
Al Stone b61377
 
Al Stone b61377
+static void
Al Stone b61377
+HxDoHexOutputH (
Al Stone b61377
+    void);
Al Stone b61377
+
Al Stone b61377
 static UINT32
Al Stone b61377
 HxReadAmlOutputFile (
Al Stone b61377
     UINT8                   *Buffer);
Al Stone b61377
@@ -104,6 +112,11 @@ HxDoHexOutput (
Al Stone b61377
         HxDoHexOutputAsl ();
Al Stone b61377
         break;
Al Stone b61377
 
Al Stone b61377
+    case HEX_OUTPUT_H:
Al Stone b61377
+
Al Stone b61377
+        HxDoHexOutputH ();
Al Stone b61377
+        break;
Al Stone b61377
+
Al Stone b61377
     default:
Al Stone b61377
 
Al Stone b61377
         /* No other output types supported */
Al Stone b61377
@@ -233,6 +246,147 @@ HxDoHexOutputC (
Al Stone b61377
 }
Al Stone b61377
 
Al Stone b61377
 
Al Stone b61377
+/*******************************************************************************
Al Stone b61377
+*
Al Stone b61377
+* FUNCTION:    HxDoHexOutputH
Al Stone b61377
+*
Al Stone b61377
+* PARAMETERS:  None
Al Stone b61377
+*
Al Stone b61377
+* RETURN:      None
Al Stone b61377
+*
Al Stone b61377
+* DESCRIPTION: Create the hex output file. This is the same data as the AML
Al Stone b61377
+*              output file, but formatted into hex/ASCII bytes suitable for
Al Stone b61377
+*              inclusion into a C source file.
Al Stone b61377
+*
Al Stone b61377
+******************************************************************************/
Al Stone b61377
+
Al Stone b61377
+static void
Al Stone b61377
+HxDoHexOutputH (
Al Stone b61377
+    void)
Al Stone b61377
+{
Al Stone b61377
+    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
Al Stone b61377
+    UINT32                  LineLength;
Al Stone b61377
+    UINT32                  Offset = 0;
Al Stone b61377
+    UINT32                  AmlFileSize;
Al Stone b61377
+    UINT32                  i;
Al Stone b61377
+    UINT32                  FileNamePrefixLength;
Al Stone b61377
+    char                    *DotPosition;
Al Stone b61377
+    char                    *InputFileName;
Al Stone b61377
+    char                    *NamePosition;
Al Stone b61377
+    char                    *FileNameEndPosition;
Al Stone b61377
+    char                    FileNamePrefix[PATH_MAX];
Al Stone b61377
+    char                    FileGuard[PATH_MAX];
Al Stone b61377
+
Al Stone b61377
+    /* Get AML size, seek back to start */
Al Stone b61377
+
Al Stone b61377
+    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
Al Stone b61377
+    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
Al Stone b61377
+
Al Stone b61377
+    /*
Al Stone b61377
+     * Find the filename and use it as a prefix for
Al Stone b61377
+     * the AML code array and the header file guard
Al Stone b61377
+     */
Al Stone b61377
+    InputFileName = Gbl_Files[ASL_FILE_INPUT].Filename;
Al Stone b61377
+    FileNameEndPosition = InputFileName + strlen (InputFileName);
Al Stone b61377
+
Al Stone b61377
+    NamePosition = strrchr (InputFileName, '/');
Al Stone b61377
+    if (NamePosition == NULL) {
Al Stone b61377
+        /* '/' not found. This means the input file name
Al Stone b61377
+         * does not contain the path and is in the current
Al Stone b61377
+         * directory.
Al Stone b61377
+         */
Al Stone b61377
+        NamePosition = InputFileName;
Al Stone b61377
+    } else {
Al Stone b61377
+        /* '/' was found. So move to the next character
Al Stone b61377
+         * which is the name of the input file.
Al Stone b61377
+         */
Al Stone b61377
+        NamePosition++;
Al Stone b61377
+    }
Al Stone b61377
+
Al Stone b61377
+    /* Get the file name without the extension to use
Al Stone b61377
+     * as a prefix.
Al Stone b61377
+     */
Al Stone b61377
+    DotPosition = strrchr (NamePosition, '.');
Al Stone b61377
+    if (DotPosition) {
Al Stone b61377
+        /* No dot or suffix */
Al Stone b61377
+        FileNameEndPosition = DotPosition;
Al Stone b61377
+    }
Al Stone b61377
+
Al Stone b61377
+    FileNamePrefixLength = FileNameEndPosition - NamePosition;
Al Stone b61377
+    if (FileNamePrefixLength > (PATH_MAX - 1)) {
Al Stone b61377
+        AslError (ASL_ERROR, ASL_MSG_STRING_LENGTH, NULL, ": Input file name too long.");
Al Stone b61377
+        return;
Al Stone b61377
+    }
Al Stone b61377
+
Al Stone b61377
+    strncpy (FileNamePrefix, NamePosition, FileNamePrefixLength);
Al Stone b61377
+    /* NULL terminate the string */
Al Stone b61377
+    FileNamePrefix[FileNamePrefixLength] = '\0';
Al Stone b61377
+
Al Stone b61377
+    if (strrchr (FileNamePrefix, ' ')) {
Al Stone b61377
+        AslError (ASL_ERROR, ASL_MSG_SYNTAX, NULL, "Input file name has space.");
Al Stone b61377
+        return;
Al Stone b61377
+    }
Al Stone b61377
+
Al Stone b61377
+    strcpy (FileGuard, FileNamePrefix);
Al Stone b61377
+    AcpiUtStrupr (FileGuard);
Al Stone b61377
+
Al Stone b61377
+    /* Generate the hex output. */
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C header code output\n");
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
Al Stone b61377
+        AmlFileSize);
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "#ifndef %s_HEX_\n", FileGuard);
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "#define %s_HEX_\n", FileGuard);
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char %sAmlCode[] =\n{\n", FileNamePrefix);
Al Stone b61377
+
Al Stone b61377
+    while (Offset < AmlFileSize) {
Al Stone b61377
+        /* Read enough bytes needed for one output line */
Al Stone b61377
+
Al Stone b61377
+        LineLength = HxReadAmlOutputFile (FileData);
Al Stone b61377
+        if (!LineLength) {
Al Stone b61377
+            break;
Al Stone b61377
+        }
Al Stone b61377
+
Al Stone b61377
+        FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
Al Stone b61377
+
Al Stone b61377
+        for (i = 0; i < LineLength; i++) {
Al Stone b61377
+            /*
Al Stone b61377
+            * Print each hex byte.
Al Stone b61377
+            * Add a comma until the very last byte of the AML file
Al Stone b61377
+            * (Some C compilers complain about a trailing comma)
Al Stone b61377
+            */
Al Stone b61377
+            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
Al Stone b61377
+            if ((Offset + i + 1) < AmlFileSize) {
Al Stone b61377
+                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
Al Stone b61377
+            } else {
Al Stone b61377
+                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
Al Stone b61377
+            }
Al Stone b61377
+        }
Al Stone b61377
+
Al Stone b61377
+        /* Add fill spaces if needed for last line */
Al Stone b61377
+
Al Stone b61377
+        if (LineLength < HEX_TABLE_LINE_SIZE) {
Al Stone b61377
+            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
Al Stone b61377
+                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
Al Stone b61377
+        }
Al Stone b61377
+
Al Stone b61377
+        /* Emit the offset and ASCII dump for the entire line */
Al Stone b61377
+
Al Stone b61377
+        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
Al Stone b61377
+        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
Al Stone b61377
+
Al Stone b61377
+        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
Al Stone b61377
+            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
Al Stone b61377
+
Al Stone b61377
+        Offset += LineLength;
Al Stone b61377
+    }
Al Stone b61377
+
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
Al Stone b61377
+    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n#endif // %s_HEX_\n", FileGuard);
Al Stone b61377
+}
Al Stone b61377
+
Al Stone b61377
+
Al Stone b61377
 /*******************************************************************************
Al Stone b61377
  *
Al Stone b61377
  * FUNCTION:    HxDoHexOutputAsl
Al Stone b61377
Index: acpica-unix2-20180209/source/compiler/asloptions.c
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/compiler/asloptions.c
Al Stone b61377
+++ acpica-unix2-20180209/source/compiler/asloptions.c
Al Stone b61377
@@ -734,6 +734,11 @@ AslDoOptions (
Al Stone b61377
             Gbl_HexOutputFlag = HEX_OUTPUT_ASL;
Al Stone b61377
             break;
Al Stone b61377
 
Al Stone b61377
+        case 'h':
Al Stone b61377
+
Al Stone b61377
+            Gbl_HexOutputFlag = HEX_OUTPUT_H;
Al Stone b61377
+            break;
Al Stone b61377
+
Al Stone b61377
         default:
Al Stone b61377
 
Al Stone b61377
             printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
Al Stone b61377
Index: acpica-unix2-20180209/source/include/acapps.h
Al Stone b61377
===================================================================
Al Stone b61377
--- acpica-unix2-20180209.orig/source/include/acapps.h
Al Stone b61377
+++ acpica-unix2-20180209/source/include/acapps.h
Al Stone b61377
@@ -80,6 +80,12 @@
Al Stone b61377
     Prefix, ACPICA_COPYRIGHT, \
Al Stone b61377
     Prefix
Al Stone b61377
 
Al Stone b61377
+#define ACPI_COMMON_HEADER_NO_COPYRIGHT(UtilityName, Prefix) \
Al Stone b61377
+    "%s%s\n%s%s version %8.8X%s\n%s\n", \
Al Stone b61377
+    Prefix, ACPICA_NAME, \
Al Stone b61377
+    Prefix, UtilityName, ((UINT32) ACPI_CA_VERSION), ACPI_WIDTH, \
Al Stone b61377
+    Prefix
Al Stone b61377
+
Al Stone b61377
 #define ACPI_COMMON_BUILD_TIME \
Al Stone b61377
     "Build date/time: %s %s\n", __DATE__, __TIME__
Al Stone b61377