1167802Sjkim/******************************************************************************
2167802Sjkim *
3167802Sjkim * Module Name: adfile - Application-level disassembler file support routines
4167802Sjkim *
5167802Sjkim *****************************************************************************/
6167802Sjkim
7217365Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, Intel Corp.
9167802Sjkim * All rights reserved.
10167802Sjkim *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
25167802Sjkim *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
29167802Sjkim *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
43167802Sjkim
44281075Sdim#include <contrib/dev/acpica/compiler/aslcompiler.h>
45193529Sjkim#include <contrib/dev/acpica/include/acpi.h>
46193529Sjkim#include <contrib/dev/acpica/include/accommon.h>
47193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
48167802Sjkim
49167802Sjkim#include <stdio.h>
50167802Sjkim
51167802Sjkim
52167802Sjkim#define _COMPONENT          ACPI_TOOLS
53167802Sjkim        ACPI_MODULE_NAME    ("adfile")
54167802Sjkim
55193529Sjkim/* Local prototypes */
56167802Sjkim
57212761Sjkimstatic INT32
58193529SjkimAdWriteBuffer (
59193529Sjkim    char                    *Filename,
60193529Sjkim    char                    *Buffer,
61193529Sjkim    UINT32                  Length);
62193529Sjkim
63212761Sjkimstatic char                 FilenameBuf[20];
64167802Sjkim
65198237Sjkim
66167802Sjkim/******************************************************************************
67167802Sjkim *
68167802Sjkim * FUNCTION:    AfGenerateFilename
69167802Sjkim *
70198237Sjkim * PARAMETERS:  Prefix              - prefix string
71198237Sjkim *              TableId             - The table ID
72167802Sjkim *
73167802Sjkim * RETURN:      Pointer to the completed string
74167802Sjkim *
75167802Sjkim * DESCRIPTION: Build an output filename from an ACPI table ID string
76167802Sjkim *
77167802Sjkim ******************************************************************************/
78167802Sjkim
79167802Sjkimchar *
80167802SjkimAdGenerateFilename (
81167802Sjkim    char                    *Prefix,
82167802Sjkim    char                    *TableId)
83167802Sjkim{
84193529Sjkim    UINT32                  i;
85193529Sjkim    UINT32                  j;
86167802Sjkim
87167802Sjkim
88167802Sjkim    for (i = 0; Prefix[i]; i++)
89167802Sjkim    {
90167802Sjkim        FilenameBuf[i] = Prefix[i];
91167802Sjkim    }
92167802Sjkim
93167802Sjkim    FilenameBuf[i] = '_';
94167802Sjkim    i++;
95167802Sjkim
96167802Sjkim    for (j = 0; j < 8 && (TableId[j] != ' ') && (TableId[j] != 0); i++, j++)
97167802Sjkim    {
98167802Sjkim        FilenameBuf[i] = TableId[j];
99167802Sjkim    }
100167802Sjkim
101167802Sjkim    FilenameBuf[i] = 0;
102306536Sjkim    strcat (FilenameBuf, FILE_SUFFIX_BINARY_TABLE);
103241973Sjkim    return (FilenameBuf);
104167802Sjkim}
105167802Sjkim
106167802Sjkim
107167802Sjkim/******************************************************************************
108167802Sjkim *
109167802Sjkim * FUNCTION:    AfWriteBuffer
110167802Sjkim *
111198237Sjkim * PARAMETERS:  Filename            - name of file
112198237Sjkim *              Buffer              - data to write
113198237Sjkim *              Length              - length of data
114167802Sjkim *
115167802Sjkim * RETURN:      Actual number of bytes written
116167802Sjkim *
117167802Sjkim * DESCRIPTION: Open a file and write out a single buffer
118167802Sjkim *
119167802Sjkim ******************************************************************************/
120167802Sjkim
121212761Sjkimstatic INT32
122167802SjkimAdWriteBuffer (
123193529Sjkim    char                    *Filename,
124193529Sjkim    char                    *Buffer,
125193529Sjkim    UINT32                  Length)
126167802Sjkim{
127243347Sjkim    FILE                    *File;
128193529Sjkim    ACPI_SIZE               Actual;
129167802Sjkim
130167802Sjkim
131243347Sjkim    File = fopen (Filename, "wb");
132243347Sjkim    if (!File)
133167802Sjkim    {
134243347Sjkim        printf ("Could not open file %s\n", Filename);
135167802Sjkim        return (-1);
136167802Sjkim    }
137167802Sjkim
138243347Sjkim    Actual = fwrite (Buffer, 1, (size_t) Length, File);
139243347Sjkim    if (Actual != Length)
140243347Sjkim    {
141243347Sjkim        printf ("Could not write to file %s\n", Filename);
142243347Sjkim    }
143243347Sjkim
144243347Sjkim    fclose (File);
145193529Sjkim    return ((INT32) Actual);
146167802Sjkim}
147167802Sjkim
148167802Sjkim
149167802Sjkim/******************************************************************************
150167802Sjkim *
151167802Sjkim * FUNCTION:    AfWriteTable
152167802Sjkim *
153198237Sjkim * PARAMETERS:  Table               - pointer to the ACPI table
154198237Sjkim *              Length              - length of the table
155198237Sjkim *              TableName           - the table signature
156198237Sjkim *              OemTableID          - from the table header
157167802Sjkim *
158167802Sjkim * RETURN:      None
159167802Sjkim *
160167802Sjkim * DESCRIPTION: Dump the loaded tables to a file (or files)
161167802Sjkim *
162167802Sjkim ******************************************************************************/
163167802Sjkim
164167802Sjkimvoid
165167802SjkimAdWriteTable (
166167802Sjkim    ACPI_TABLE_HEADER       *Table,
167167802Sjkim    UINT32                  Length,
168167802Sjkim    char                    *TableName,
169167802Sjkim    char                    *OemTableId)
170167802Sjkim{
171167802Sjkim    char                    *Filename;
172167802Sjkim
173167802Sjkim
174167802Sjkim    Filename = AdGenerateFilename (TableName, OemTableId);
175167802Sjkim    AdWriteBuffer (Filename, (char *) Table, Length);
176167802Sjkim
177167802Sjkim    AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName, Filename);
178167802Sjkim}
179167802Sjkim
180167802Sjkim
181167802Sjkim/*******************************************************************************
182167802Sjkim *
183167802Sjkim * FUNCTION:    FlGenerateFilename
184167802Sjkim *
185167802Sjkim * PARAMETERS:  InputFilename       - Original ASL source filename
186167802Sjkim *              Suffix              - New extension.
187167802Sjkim *
188167802Sjkim * RETURN:      New filename containing the original base + the new suffix
189167802Sjkim *
190167802Sjkim * DESCRIPTION: Generate a new filename from the ASL source filename and a new
191241973Sjkim *              extension. Used to create the *.LST, *.TXT, etc. files.
192167802Sjkim *
193167802Sjkim ******************************************************************************/
194167802Sjkim
195167802Sjkimchar *
196167802SjkimFlGenerateFilename (
197167802Sjkim    char                    *InputFilename,
198167802Sjkim    char                    *Suffix)
199167802Sjkim{
200167802Sjkim    char                    *Position;
201167802Sjkim    char                    *NewFilename;
202281075Sdim    char                    *DirectoryPosition;
203167802Sjkim
204167802Sjkim
205167802Sjkim    /*
206281075Sdim     * Copy the original filename to a new buffer. Leave room for the worst
207281075Sdim     * case where we append the suffix, an added dot and the null terminator.
208167802Sjkim     */
209281075Sdim    NewFilename = UtStringCacheCalloc ((ACPI_SIZE)
210167802Sjkim        strlen (InputFilename) + strlen (Suffix) + 2);
211281075Sdim    if (!NewFilename)
212281075Sdim    {
213281075Sdim        return (NULL);
214281075Sdim    }
215281075Sdim
216167802Sjkim    strcpy (NewFilename, InputFilename);
217167802Sjkim
218167802Sjkim    /* Try to find the last dot in the filename */
219167802Sjkim
220281075Sdim    DirectoryPosition = strrchr (NewFilename, '/');
221167802Sjkim    Position = strrchr (NewFilename, '.');
222281075Sdim
223281075Sdim    if (Position && (Position > DirectoryPosition))
224167802Sjkim    {
225167802Sjkim        /* Tack on the new suffix */
226167802Sjkim
227167802Sjkim        Position++;
228167802Sjkim        *Position = 0;
229167802Sjkim        strcat (Position, Suffix);
230167802Sjkim    }
231167802Sjkim    else
232167802Sjkim    {
233167802Sjkim        /* No dot, add one and then the suffix */
234167802Sjkim
235167802Sjkim        strcat (NewFilename, ".");
236167802Sjkim        strcat (NewFilename, Suffix);
237167802Sjkim    }
238167802Sjkim
239241973Sjkim    return (NewFilename);
240167802Sjkim}
241167802Sjkim
242167802Sjkim
243167802Sjkim/*******************************************************************************
244167802Sjkim *
245167802Sjkim * FUNCTION:    FlStrdup
246167802Sjkim *
247167802Sjkim * DESCRIPTION: Local strdup function
248167802Sjkim *
249167802Sjkim ******************************************************************************/
250167802Sjkim
251167802Sjkimstatic char *
252167802SjkimFlStrdup (
253167802Sjkim    char                *String)
254167802Sjkim{
255167802Sjkim    char                *NewString;
256167802Sjkim
257167802Sjkim
258281075Sdim    NewString = UtStringCacheCalloc ((ACPI_SIZE) strlen (String) + 1);
259167802Sjkim    if (!NewString)
260167802Sjkim    {
261167802Sjkim        return (NULL);
262167802Sjkim    }
263167802Sjkim
264167802Sjkim    strcpy (NewString, String);
265167802Sjkim    return (NewString);
266167802Sjkim}
267167802Sjkim
268167802Sjkim
269167802Sjkim/*******************************************************************************
270167802Sjkim *
271167802Sjkim * FUNCTION:    FlSplitInputPathname
272167802Sjkim *
273167802Sjkim * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
274167802Sjkim *                                    compiled
275167802Sjkim *              OutDirectoryPath    - Where the directory path prefix is
276167802Sjkim *                                    returned
277167802Sjkim *              OutFilename         - Where the filename part is returned
278167802Sjkim *
279167802Sjkim * RETURN:      Status
280167802Sjkim *
281167802Sjkim * DESCRIPTION: Split the input path into a directory and filename part
282167802Sjkim *              1) Directory part used to open include files
283167802Sjkim *              2) Filename part used to generate output filenames
284167802Sjkim *
285167802Sjkim ******************************************************************************/
286167802Sjkim
287167802SjkimACPI_STATUS
288167802SjkimFlSplitInputPathname (
289167802Sjkim    char                    *InputPath,
290167802Sjkim    char                    **OutDirectoryPath,
291167802Sjkim    char                    **OutFilename)
292167802Sjkim{
293167802Sjkim    char                    *Substring;
294167802Sjkim    char                    *DirectoryPath;
295167802Sjkim    char                    *Filename;
296167802Sjkim
297167802Sjkim
298281687Sjkim    if (OutDirectoryPath)
299281687Sjkim    {
300281687Sjkim        *OutDirectoryPath = NULL;
301281687Sjkim    }
302167802Sjkim
303167802Sjkim    if (!InputPath)
304167802Sjkim    {
305167802Sjkim        return (AE_OK);
306167802Sjkim    }
307167802Sjkim
308167802Sjkim    /* Get the path to the input filename's directory */
309167802Sjkim
310167802Sjkim    DirectoryPath = FlStrdup (InputPath);
311167802Sjkim    if (!DirectoryPath)
312167802Sjkim    {
313167802Sjkim        return (AE_NO_MEMORY);
314167802Sjkim    }
315167802Sjkim
316235945Sjkim    /* Convert backslashes to slashes in the entire path */
317235945Sjkim
318235945Sjkim    UtConvertBackslashes (DirectoryPath);
319235945Sjkim
320235945Sjkim    /* Backup to last slash or colon */
321235945Sjkim
322235945Sjkim    Substring = strrchr (DirectoryPath, '/');
323167802Sjkim    if (!Substring)
324167802Sjkim    {
325235945Sjkim        Substring = strrchr (DirectoryPath, ':');
326167802Sjkim    }
327167802Sjkim
328235945Sjkim    /* Extract the simple filename */
329235945Sjkim
330167802Sjkim    if (!Substring)
331167802Sjkim    {
332235945Sjkim        Filename = FlStrdup (DirectoryPath);
333167802Sjkim        DirectoryPath[0] = 0;
334167802Sjkim    }
335167802Sjkim    else
336167802Sjkim    {
337167802Sjkim        Filename = FlStrdup (Substring + 1);
338167802Sjkim        *(Substring+1) = 0;
339167802Sjkim    }
340167802Sjkim
341167802Sjkim    if (!Filename)
342167802Sjkim    {
343167802Sjkim        return (AE_NO_MEMORY);
344167802Sjkim    }
345167802Sjkim
346281687Sjkim    if (OutDirectoryPath)
347281687Sjkim    {
348281687Sjkim        *OutDirectoryPath = DirectoryPath;
349281687Sjkim    }
350281075Sdim
351281075Sdim    if (OutFilename)
352281075Sdim    {
353281075Sdim        *OutFilename = Filename;
354281075Sdim        return (AE_OK);
355281075Sdim    }
356281075Sdim
357167802Sjkim    return (AE_OK);
358167802Sjkim}
359