Home | History | Annotate | Line # | Download | only in common
adfile.c revision 1.1.1.14
      1       1.1    jruoho /******************************************************************************
      2       1.1    jruoho  *
      3       1.1    jruoho  * Module Name: adfile - Application-level disassembler file support routines
      4       1.1    jruoho  *
      5       1.1    jruoho  *****************************************************************************/
      6       1.1    jruoho 
      7   1.1.1.2    jruoho /*
      8  1.1.1.14  christos  * Copyright (C) 2000 - 2021, Intel Corp.
      9       1.1    jruoho  * All rights reserved.
     10       1.1    jruoho  *
     11   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13   1.1.1.2    jruoho  * are met:
     14   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16   1.1.1.2    jruoho  *    without modification.
     17   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21   1.1.1.2    jruoho  *    binary redistribution.
     22   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24   1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25   1.1.1.2    jruoho  *
     26   1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27   1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.1.1.2    jruoho  * Software Foundation.
     29   1.1.1.2    jruoho  *
     30   1.1.1.2    jruoho  * NO WARRANTY
     31   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.14  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42   1.1.1.2    jruoho  */
     43       1.1    jruoho 
     44   1.1.1.4  christos #include "aslcompiler.h"
     45       1.1    jruoho #include "acpi.h"
     46       1.1    jruoho #include "accommon.h"
     47       1.1    jruoho #include "acapps.h"
     48       1.1    jruoho 
     49       1.1    jruoho #include <stdio.h>
     50       1.1    jruoho 
     51       1.1    jruoho 
     52       1.1    jruoho #define _COMPONENT          ACPI_TOOLS
     53       1.1    jruoho         ACPI_MODULE_NAME    ("adfile")
     54       1.1    jruoho 
     55       1.1    jruoho /* Local prototypes */
     56       1.1    jruoho 
     57   1.1.1.2    jruoho static INT32
     58       1.1    jruoho AdWriteBuffer (
     59       1.1    jruoho     char                    *Filename,
     60       1.1    jruoho     char                    *Buffer,
     61       1.1    jruoho     UINT32                  Length);
     62       1.1    jruoho 
     63   1.1.1.2    jruoho static char                 FilenameBuf[20];
     64       1.1    jruoho 
     65       1.1    jruoho 
     66       1.1    jruoho /******************************************************************************
     67       1.1    jruoho  *
     68       1.1    jruoho  * FUNCTION:    AfGenerateFilename
     69       1.1    jruoho  *
     70       1.1    jruoho  * PARAMETERS:  Prefix              - prefix string
     71       1.1    jruoho  *              TableId             - The table ID
     72       1.1    jruoho  *
     73       1.1    jruoho  * RETURN:      Pointer to the completed string
     74       1.1    jruoho  *
     75       1.1    jruoho  * DESCRIPTION: Build an output filename from an ACPI table ID string
     76       1.1    jruoho  *
     77       1.1    jruoho  ******************************************************************************/
     78       1.1    jruoho 
     79       1.1    jruoho char *
     80       1.1    jruoho AdGenerateFilename (
     81       1.1    jruoho     char                    *Prefix,
     82       1.1    jruoho     char                    *TableId)
     83       1.1    jruoho {
     84       1.1    jruoho     UINT32                  i;
     85       1.1    jruoho     UINT32                  j;
     86       1.1    jruoho 
     87       1.1    jruoho 
     88       1.1    jruoho     for (i = 0; Prefix[i]; i++)
     89       1.1    jruoho     {
     90       1.1    jruoho         FilenameBuf[i] = Prefix[i];
     91       1.1    jruoho     }
     92       1.1    jruoho 
     93       1.1    jruoho     FilenameBuf[i] = '_';
     94       1.1    jruoho     i++;
     95       1.1    jruoho 
     96       1.1    jruoho     for (j = 0; j < 8 && (TableId[j] != ' ') && (TableId[j] != 0); i++, j++)
     97       1.1    jruoho     {
     98       1.1    jruoho         FilenameBuf[i] = TableId[j];
     99       1.1    jruoho     }
    100       1.1    jruoho 
    101       1.1    jruoho     FilenameBuf[i] = 0;
    102   1.1.1.6  christos     strcat (FilenameBuf, FILE_SUFFIX_BINARY_TABLE);
    103   1.1.1.3  christos     return (FilenameBuf);
    104       1.1    jruoho }
    105       1.1    jruoho 
    106       1.1    jruoho 
    107       1.1    jruoho /******************************************************************************
    108       1.1    jruoho  *
    109       1.1    jruoho  * FUNCTION:    AfWriteBuffer
    110       1.1    jruoho  *
    111       1.1    jruoho  * PARAMETERS:  Filename            - name of file
    112       1.1    jruoho  *              Buffer              - data to write
    113       1.1    jruoho  *              Length              - length of data
    114       1.1    jruoho  *
    115       1.1    jruoho  * RETURN:      Actual number of bytes written
    116       1.1    jruoho  *
    117       1.1    jruoho  * DESCRIPTION: Open a file and write out a single buffer
    118       1.1    jruoho  *
    119       1.1    jruoho  ******************************************************************************/
    120       1.1    jruoho 
    121   1.1.1.2    jruoho static INT32
    122       1.1    jruoho AdWriteBuffer (
    123       1.1    jruoho     char                    *Filename,
    124       1.1    jruoho     char                    *Buffer,
    125       1.1    jruoho     UINT32                  Length)
    126       1.1    jruoho {
    127   1.1.1.3  christos     FILE                    *File;
    128       1.1    jruoho     ACPI_SIZE               Actual;
    129       1.1    jruoho 
    130       1.1    jruoho 
    131   1.1.1.3  christos     File = fopen (Filename, "wb");
    132   1.1.1.3  christos     if (!File)
    133       1.1    jruoho     {
    134   1.1.1.3  christos         printf ("Could not open file %s\n", Filename);
    135       1.1    jruoho         return (-1);
    136       1.1    jruoho     }
    137       1.1    jruoho 
    138   1.1.1.3  christos     Actual = fwrite (Buffer, 1, (size_t) Length, File);
    139   1.1.1.3  christos     if (Actual != Length)
    140   1.1.1.3  christos     {
    141   1.1.1.3  christos         printf ("Could not write to file %s\n", Filename);
    142   1.1.1.3  christos     }
    143   1.1.1.3  christos 
    144   1.1.1.3  christos     fclose (File);
    145       1.1    jruoho     return ((INT32) Actual);
    146       1.1    jruoho }
    147       1.1    jruoho 
    148       1.1    jruoho 
    149       1.1    jruoho /******************************************************************************
    150       1.1    jruoho  *
    151       1.1    jruoho  * FUNCTION:    AfWriteTable
    152       1.1    jruoho  *
    153       1.1    jruoho  * PARAMETERS:  Table               - pointer to the ACPI table
    154       1.1    jruoho  *              Length              - length of the table
    155       1.1    jruoho  *              TableName           - the table signature
    156       1.1    jruoho  *              OemTableID          - from the table header
    157       1.1    jruoho  *
    158       1.1    jruoho  * RETURN:      None
    159       1.1    jruoho  *
    160       1.1    jruoho  * DESCRIPTION: Dump the loaded tables to a file (or files)
    161       1.1    jruoho  *
    162       1.1    jruoho  ******************************************************************************/
    163       1.1    jruoho 
    164       1.1    jruoho void
    165       1.1    jruoho AdWriteTable (
    166       1.1    jruoho     ACPI_TABLE_HEADER       *Table,
    167       1.1    jruoho     UINT32                  Length,
    168       1.1    jruoho     char                    *TableName,
    169       1.1    jruoho     char                    *OemTableId)
    170       1.1    jruoho {
    171       1.1    jruoho     char                    *Filename;
    172       1.1    jruoho 
    173       1.1    jruoho 
    174       1.1    jruoho     Filename = AdGenerateFilename (TableName, OemTableId);
    175       1.1    jruoho     AdWriteBuffer (Filename, (char *) Table, Length);
    176       1.1    jruoho 
    177       1.1    jruoho     AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName, Filename);
    178       1.1    jruoho }
    179       1.1    jruoho 
    180       1.1    jruoho 
    181       1.1    jruoho /*******************************************************************************
    182       1.1    jruoho  *
    183       1.1    jruoho  * FUNCTION:    FlGenerateFilename
    184       1.1    jruoho  *
    185       1.1    jruoho  * PARAMETERS:  InputFilename       - Original ASL source filename
    186       1.1    jruoho  *              Suffix              - New extension.
    187       1.1    jruoho  *
    188       1.1    jruoho  * RETURN:      New filename containing the original base + the new suffix
    189       1.1    jruoho  *
    190       1.1    jruoho  * DESCRIPTION: Generate a new filename from the ASL source filename and a new
    191   1.1.1.3  christos  *              extension. Used to create the *.LST, *.TXT, etc. files.
    192       1.1    jruoho  *
    193       1.1    jruoho  ******************************************************************************/
    194       1.1    jruoho 
    195       1.1    jruoho char *
    196       1.1    jruoho FlGenerateFilename (
    197       1.1    jruoho     char                    *InputFilename,
    198       1.1    jruoho     char                    *Suffix)
    199       1.1    jruoho {
    200       1.1    jruoho     char                    *Position;
    201       1.1    jruoho     char                    *NewFilename;
    202   1.1.1.3  christos     char                    *DirectoryPosition;
    203       1.1    jruoho 
    204       1.1    jruoho 
    205       1.1    jruoho     /*
    206   1.1.1.4  christos      * Copy the original filename to a new buffer. Leave room for the worst
    207   1.1.1.4  christos      * case where we append the suffix, an added dot and the null terminator.
    208       1.1    jruoho      */
    209   1.1.1.9  christos     NewFilename = UtLocalCacheCalloc ((ACPI_SIZE)
    210       1.1    jruoho         strlen (InputFilename) + strlen (Suffix) + 2);
    211       1.1    jruoho     strcpy (NewFilename, InputFilename);
    212       1.1    jruoho 
    213       1.1    jruoho     /* Try to find the last dot in the filename */
    214       1.1    jruoho 
    215   1.1.1.3  christos     DirectoryPosition = strrchr (NewFilename, '/');
    216       1.1    jruoho     Position = strrchr (NewFilename, '.');
    217   1.1.1.3  christos 
    218   1.1.1.3  christos     if (Position && (Position > DirectoryPosition))
    219       1.1    jruoho     {
    220       1.1    jruoho         /* Tack on the new suffix */
    221       1.1    jruoho 
    222       1.1    jruoho         Position++;
    223       1.1    jruoho         *Position = 0;
    224       1.1    jruoho         strcat (Position, Suffix);
    225       1.1    jruoho     }
    226       1.1    jruoho     else
    227       1.1    jruoho     {
    228       1.1    jruoho         /* No dot, add one and then the suffix */
    229       1.1    jruoho 
    230       1.1    jruoho         strcat (NewFilename, ".");
    231       1.1    jruoho         strcat (NewFilename, Suffix);
    232       1.1    jruoho     }
    233       1.1    jruoho 
    234   1.1.1.3  christos     return (NewFilename);
    235       1.1    jruoho }
    236       1.1    jruoho 
    237       1.1    jruoho 
    238       1.1    jruoho /*******************************************************************************
    239       1.1    jruoho  *
    240       1.1    jruoho  * FUNCTION:    FlStrdup
    241       1.1    jruoho  *
    242       1.1    jruoho  * DESCRIPTION: Local strdup function
    243       1.1    jruoho  *
    244       1.1    jruoho  ******************************************************************************/
    245       1.1    jruoho 
    246       1.1    jruoho static char *
    247       1.1    jruoho FlStrdup (
    248       1.1    jruoho     char                *String)
    249       1.1    jruoho {
    250       1.1    jruoho     char                *NewString;
    251       1.1    jruoho 
    252       1.1    jruoho 
    253   1.1.1.9  christos     NewString = UtLocalCacheCalloc ((ACPI_SIZE) strlen (String) + 1);
    254       1.1    jruoho     strcpy (NewString, String);
    255       1.1    jruoho     return (NewString);
    256       1.1    jruoho }
    257       1.1    jruoho 
    258       1.1    jruoho 
    259       1.1    jruoho /*******************************************************************************
    260       1.1    jruoho  *
    261       1.1    jruoho  * FUNCTION:    FlSplitInputPathname
    262       1.1    jruoho  *
    263       1.1    jruoho  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
    264       1.1    jruoho  *                                    compiled
    265       1.1    jruoho  *              OutDirectoryPath    - Where the directory path prefix is
    266       1.1    jruoho  *                                    returned
    267       1.1    jruoho  *              OutFilename         - Where the filename part is returned
    268       1.1    jruoho  *
    269       1.1    jruoho  * RETURN:      Status
    270       1.1    jruoho  *
    271       1.1    jruoho  * DESCRIPTION: Split the input path into a directory and filename part
    272       1.1    jruoho  *              1) Directory part used to open include files
    273       1.1    jruoho  *              2) Filename part used to generate output filenames
    274       1.1    jruoho  *
    275       1.1    jruoho  ******************************************************************************/
    276       1.1    jruoho 
    277       1.1    jruoho ACPI_STATUS
    278       1.1    jruoho FlSplitInputPathname (
    279       1.1    jruoho     char                    *InputPath,
    280       1.1    jruoho     char                    **OutDirectoryPath,
    281       1.1    jruoho     char                    **OutFilename)
    282       1.1    jruoho {
    283       1.1    jruoho     char                    *Substring;
    284       1.1    jruoho     char                    *DirectoryPath;
    285       1.1    jruoho     char                    *Filename;
    286       1.1    jruoho 
    287       1.1    jruoho 
    288   1.1.1.5  christos     if (OutDirectoryPath)
    289   1.1.1.5  christos     {
    290   1.1.1.5  christos         *OutDirectoryPath = NULL;
    291   1.1.1.5  christos     }
    292       1.1    jruoho 
    293       1.1    jruoho     if (!InputPath)
    294       1.1    jruoho     {
    295       1.1    jruoho         return (AE_OK);
    296       1.1    jruoho     }
    297       1.1    jruoho 
    298       1.1    jruoho     /* Get the path to the input filename's directory */
    299       1.1    jruoho 
    300       1.1    jruoho     DirectoryPath = FlStrdup (InputPath);
    301       1.1    jruoho     if (!DirectoryPath)
    302       1.1    jruoho     {
    303       1.1    jruoho         return (AE_NO_MEMORY);
    304       1.1    jruoho     }
    305       1.1    jruoho 
    306   1.1.1.3  christos     /* Convert backslashes to slashes in the entire path */
    307   1.1.1.3  christos 
    308   1.1.1.3  christos     UtConvertBackslashes (DirectoryPath);
    309   1.1.1.3  christos 
    310   1.1.1.3  christos     /* Backup to last slash or colon */
    311   1.1.1.3  christos 
    312   1.1.1.3  christos     Substring = strrchr (DirectoryPath, '/');
    313       1.1    jruoho     if (!Substring)
    314       1.1    jruoho     {
    315   1.1.1.3  christos         Substring = strrchr (DirectoryPath, ':');
    316       1.1    jruoho     }
    317       1.1    jruoho 
    318   1.1.1.3  christos     /* Extract the simple filename */
    319   1.1.1.3  christos 
    320       1.1    jruoho     if (!Substring)
    321       1.1    jruoho     {
    322   1.1.1.3  christos         Filename = FlStrdup (DirectoryPath);
    323       1.1    jruoho         DirectoryPath[0] = 0;
    324       1.1    jruoho     }
    325       1.1    jruoho     else
    326       1.1    jruoho     {
    327       1.1    jruoho         Filename = FlStrdup (Substring + 1);
    328       1.1    jruoho         *(Substring+1) = 0;
    329       1.1    jruoho     }
    330       1.1    jruoho 
    331       1.1    jruoho     if (!Filename)
    332       1.1    jruoho     {
    333       1.1    jruoho         return (AE_NO_MEMORY);
    334       1.1    jruoho     }
    335       1.1    jruoho 
    336   1.1.1.5  christos     if (OutDirectoryPath)
    337   1.1.1.5  christos     {
    338   1.1.1.5  christos         *OutDirectoryPath = DirectoryPath;
    339   1.1.1.5  christos     }
    340       1.1    jruoho 
    341   1.1.1.3  christos     if (OutFilename)
    342   1.1.1.3  christos     {
    343   1.1.1.3  christos         *OutFilename = Filename;
    344   1.1.1.3  christos         return (AE_OK);
    345   1.1.1.3  christos     }
    346   1.1.1.3  christos 
    347       1.1    jruoho     return (AE_OK);
    348       1.1    jruoho }
    349  1.1.1.11  christos 
    350  1.1.1.11  christos 
    351  1.1.1.11  christos /*******************************************************************************
    352  1.1.1.11  christos  *
    353  1.1.1.11  christos  * FUNCTION:    FlGetFileBasename
    354  1.1.1.11  christos  *
    355  1.1.1.11  christos  * PARAMETERS:  FilePathname            - File path to be split
    356  1.1.1.11  christos  *
    357  1.1.1.11  christos  * RETURN:      The extracted base name of the file, in upper case
    358  1.1.1.11  christos  *
    359  1.1.1.11  christos  * DESCRIPTION: Extract the file base name (the file name with no extension)
    360  1.1.1.11  christos  *              from the input pathname.
    361  1.1.1.11  christos  *
    362  1.1.1.11  christos  *              Note: Any backslashes in the pathname should be previously
    363  1.1.1.11  christos  *              converted to forward slashes before calling this function.
    364  1.1.1.11  christos  *
    365  1.1.1.11  christos  ******************************************************************************/
    366  1.1.1.11  christos 
    367  1.1.1.11  christos char *
    368  1.1.1.11  christos FlGetFileBasename (
    369  1.1.1.11  christos     char                    *FilePathname)
    370  1.1.1.11  christos {
    371  1.1.1.11  christos     char                    *FileBasename;
    372  1.1.1.11  christos     char                    *Substring;
    373  1.1.1.11  christos 
    374  1.1.1.11  christos 
    375  1.1.1.11  christos     /* Backup to last slash or colon */
    376  1.1.1.11  christos 
    377  1.1.1.11  christos     Substring = strrchr (FilePathname, '/');
    378  1.1.1.11  christos     if (!Substring)
    379  1.1.1.11  christos     {
    380  1.1.1.11  christos         Substring = strrchr (FilePathname, ':');
    381  1.1.1.11  christos     }
    382  1.1.1.11  christos 
    383  1.1.1.11  christos     /* Extract the full filename (base + extension) */
    384  1.1.1.11  christos 
    385  1.1.1.11  christos     if (Substring)
    386  1.1.1.11  christos     {
    387  1.1.1.11  christos         FileBasename = FlStrdup (Substring + 1);
    388  1.1.1.11  christos     }
    389  1.1.1.11  christos     else
    390  1.1.1.11  christos     {
    391  1.1.1.11  christos         FileBasename = FlStrdup (FilePathname);
    392  1.1.1.11  christos     }
    393  1.1.1.11  christos 
    394  1.1.1.11  christos     /* Remove the filename extension if present */
    395  1.1.1.11  christos 
    396  1.1.1.11  christos     Substring = strchr (FileBasename, '.');
    397  1.1.1.11  christos     if (Substring)
    398  1.1.1.11  christos     {
    399  1.1.1.11  christos         *Substring = 0;
    400  1.1.1.11  christos     }
    401  1.1.1.11  christos 
    402  1.1.1.11  christos     AcpiUtStrupr (FileBasename);
    403  1.1.1.11  christos     return (FileBasename);
    404  1.1.1.11  christos }
    405