adfile.c revision 1.1.1.7 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.7 christos * Copyright (C) 2000 - 2017, 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.2 jruoho * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY 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.4 christos NewFilename = UtStringCacheCalloc ((ACPI_SIZE)
210 1.1 jruoho strlen (InputFilename) + strlen (Suffix) + 2);
211 1.1.1.4 christos if (!NewFilename)
212 1.1.1.4 christos {
213 1.1.1.4 christos return (NULL);
214 1.1.1.4 christos }
215 1.1.1.4 christos
216 1.1 jruoho strcpy (NewFilename, InputFilename);
217 1.1 jruoho
218 1.1 jruoho /* Try to find the last dot in the filename */
219 1.1 jruoho
220 1.1.1.3 christos DirectoryPosition = strrchr (NewFilename, '/');
221 1.1 jruoho Position = strrchr (NewFilename, '.');
222 1.1.1.3 christos
223 1.1.1.3 christos if (Position && (Position > DirectoryPosition))
224 1.1 jruoho {
225 1.1 jruoho /* Tack on the new suffix */
226 1.1 jruoho
227 1.1 jruoho Position++;
228 1.1 jruoho *Position = 0;
229 1.1 jruoho strcat (Position, Suffix);
230 1.1 jruoho }
231 1.1 jruoho else
232 1.1 jruoho {
233 1.1 jruoho /* No dot, add one and then the suffix */
234 1.1 jruoho
235 1.1 jruoho strcat (NewFilename, ".");
236 1.1 jruoho strcat (NewFilename, Suffix);
237 1.1 jruoho }
238 1.1 jruoho
239 1.1.1.3 christos return (NewFilename);
240 1.1 jruoho }
241 1.1 jruoho
242 1.1 jruoho
243 1.1 jruoho /*******************************************************************************
244 1.1 jruoho *
245 1.1 jruoho * FUNCTION: FlStrdup
246 1.1 jruoho *
247 1.1 jruoho * DESCRIPTION: Local strdup function
248 1.1 jruoho *
249 1.1 jruoho ******************************************************************************/
250 1.1 jruoho
251 1.1 jruoho static char *
252 1.1 jruoho FlStrdup (
253 1.1 jruoho char *String)
254 1.1 jruoho {
255 1.1 jruoho char *NewString;
256 1.1 jruoho
257 1.1 jruoho
258 1.1.1.4 christos NewString = UtStringCacheCalloc ((ACPI_SIZE) strlen (String) + 1);
259 1.1 jruoho if (!NewString)
260 1.1 jruoho {
261 1.1 jruoho return (NULL);
262 1.1 jruoho }
263 1.1 jruoho
264 1.1 jruoho strcpy (NewString, String);
265 1.1 jruoho return (NewString);
266 1.1 jruoho }
267 1.1 jruoho
268 1.1 jruoho
269 1.1 jruoho /*******************************************************************************
270 1.1 jruoho *
271 1.1 jruoho * FUNCTION: FlSplitInputPathname
272 1.1 jruoho *
273 1.1 jruoho * PARAMETERS: InputFilename - The user-specified ASL source file to be
274 1.1 jruoho * compiled
275 1.1 jruoho * OutDirectoryPath - Where the directory path prefix is
276 1.1 jruoho * returned
277 1.1 jruoho * OutFilename - Where the filename part is returned
278 1.1 jruoho *
279 1.1 jruoho * RETURN: Status
280 1.1 jruoho *
281 1.1 jruoho * DESCRIPTION: Split the input path into a directory and filename part
282 1.1 jruoho * 1) Directory part used to open include files
283 1.1 jruoho * 2) Filename part used to generate output filenames
284 1.1 jruoho *
285 1.1 jruoho ******************************************************************************/
286 1.1 jruoho
287 1.1 jruoho ACPI_STATUS
288 1.1 jruoho FlSplitInputPathname (
289 1.1 jruoho char *InputPath,
290 1.1 jruoho char **OutDirectoryPath,
291 1.1 jruoho char **OutFilename)
292 1.1 jruoho {
293 1.1 jruoho char *Substring;
294 1.1 jruoho char *DirectoryPath;
295 1.1 jruoho char *Filename;
296 1.1 jruoho
297 1.1 jruoho
298 1.1.1.5 christos if (OutDirectoryPath)
299 1.1.1.5 christos {
300 1.1.1.5 christos *OutDirectoryPath = NULL;
301 1.1.1.5 christos }
302 1.1 jruoho
303 1.1 jruoho if (!InputPath)
304 1.1 jruoho {
305 1.1 jruoho return (AE_OK);
306 1.1 jruoho }
307 1.1 jruoho
308 1.1 jruoho /* Get the path to the input filename's directory */
309 1.1 jruoho
310 1.1 jruoho DirectoryPath = FlStrdup (InputPath);
311 1.1 jruoho if (!DirectoryPath)
312 1.1 jruoho {
313 1.1 jruoho return (AE_NO_MEMORY);
314 1.1 jruoho }
315 1.1 jruoho
316 1.1.1.3 christos /* Convert backslashes to slashes in the entire path */
317 1.1.1.3 christos
318 1.1.1.3 christos UtConvertBackslashes (DirectoryPath);
319 1.1.1.3 christos
320 1.1.1.3 christos /* Backup to last slash or colon */
321 1.1.1.3 christos
322 1.1.1.3 christos Substring = strrchr (DirectoryPath, '/');
323 1.1 jruoho if (!Substring)
324 1.1 jruoho {
325 1.1.1.3 christos Substring = strrchr (DirectoryPath, ':');
326 1.1 jruoho }
327 1.1 jruoho
328 1.1.1.3 christos /* Extract the simple filename */
329 1.1.1.3 christos
330 1.1 jruoho if (!Substring)
331 1.1 jruoho {
332 1.1.1.3 christos Filename = FlStrdup (DirectoryPath);
333 1.1 jruoho DirectoryPath[0] = 0;
334 1.1 jruoho }
335 1.1 jruoho else
336 1.1 jruoho {
337 1.1 jruoho Filename = FlStrdup (Substring + 1);
338 1.1 jruoho *(Substring+1) = 0;
339 1.1 jruoho }
340 1.1 jruoho
341 1.1 jruoho if (!Filename)
342 1.1 jruoho {
343 1.1 jruoho return (AE_NO_MEMORY);
344 1.1 jruoho }
345 1.1 jruoho
346 1.1.1.5 christos if (OutDirectoryPath)
347 1.1.1.5 christos {
348 1.1.1.5 christos *OutDirectoryPath = DirectoryPath;
349 1.1.1.5 christos }
350 1.1 jruoho
351 1.1.1.3 christos if (OutFilename)
352 1.1.1.3 christos {
353 1.1.1.3 christos *OutFilename = Filename;
354 1.1.1.3 christos return (AE_OK);
355 1.1.1.3 christos }
356 1.1.1.3 christos
357 1.1 jruoho return (AE_OK);
358 1.1 jruoho }
359