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