1 /******************************************************************************
3 * Module Name: adfile - Application-level disassembler file support routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
52 #define _COMPONENT ACPI_TOOLS
53 ACPI_MODULE_NAME ("adfile")
55 /* Local prototypes */
63 static char FilenameBuf
[20];
66 /******************************************************************************
68 * FUNCTION: AfGenerateFilename
70 * PARAMETERS: Prefix - prefix string
71 * TableId - The table ID
73 * RETURN: Pointer to the completed string
75 * DESCRIPTION: Build an output filename from an ACPI table ID string
77 ******************************************************************************/
88 for (i
= 0; Prefix
[i
]; i
++)
90 FilenameBuf
[i
] = Prefix
[i
];
96 for (j
= 0; j
< 8 && (TableId
[j
] != ' ') && (TableId
[j
] != 0); i
++, j
++)
98 FilenameBuf
[i
] = TableId
[j
];
102 strcat (FilenameBuf
, ACPI_TABLE_FILE_SUFFIX
);
103 return (FilenameBuf
);
107 /******************************************************************************
109 * FUNCTION: AfWriteBuffer
111 * PARAMETERS: Filename - name of file
112 * Buffer - data to write
113 * Length - length of data
115 * RETURN: Actual number of bytes written
117 * DESCRIPTION: Open a file and write out a single buffer
119 ******************************************************************************/
131 File
= fopen (Filename
, "wb");
134 printf ("Could not open file %s\n", Filename
);
138 Actual
= fwrite (Buffer
, 1, (size_t) Length
, File
);
139 if (Actual
!= Length
)
141 printf ("Could not write to file %s\n", Filename
);
145 return ((INT32
) Actual
);
149 /******************************************************************************
151 * FUNCTION: AfWriteTable
153 * PARAMETERS: Table - pointer to the ACPI table
154 * Length - length of the table
155 * TableName - the table signature
156 * OemTableID - from the table header
160 * DESCRIPTION: Dump the loaded tables to a file (or files)
162 ******************************************************************************/
166 ACPI_TABLE_HEADER
*Table
,
174 Filename
= AdGenerateFilename (TableName
, OemTableId
);
175 AdWriteBuffer (Filename
, (char *) Table
, Length
);
177 AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName
, Filename
);
181 /*******************************************************************************
183 * FUNCTION: FlGenerateFilename
185 * PARAMETERS: InputFilename - Original ASL source filename
186 * Suffix - New extension.
188 * RETURN: New filename containing the original base + the new suffix
190 * DESCRIPTION: Generate a new filename from the ASL source filename and a new
191 * extension. Used to create the *.LST, *.TXT, etc. files.
193 ******************************************************************************/
205 * Copy the original filename to a new buffer. Leave room for the worst case
206 * where we append the suffix, an added dot and the null terminator.
208 NewFilename
= ACPI_ALLOCATE_ZEROED ((ACPI_SIZE
)
209 strlen (InputFilename
) + strlen (Suffix
) + 2);
210 strcpy (NewFilename
, InputFilename
);
212 /* Try to find the last dot in the filename */
214 Position
= strrchr (NewFilename
, '.');
217 /* Tack on the new suffix */
221 strcat (Position
, Suffix
);
225 /* No dot, add one and then the suffix */
227 strcat (NewFilename
, ".");
228 strcat (NewFilename
, Suffix
);
231 return (NewFilename
);
235 /*******************************************************************************
239 * DESCRIPTION: Local strdup function
241 ******************************************************************************/
250 NewString
= ACPI_ALLOCATE ((ACPI_SIZE
) strlen (String
) + 1);
256 strcpy (NewString
, String
);
261 /*******************************************************************************
263 * FUNCTION: FlSplitInputPathname
265 * PARAMETERS: InputFilename - The user-specified ASL source file to be
267 * OutDirectoryPath - Where the directory path prefix is
269 * OutFilename - Where the filename part is returned
273 * DESCRIPTION: Split the input path into a directory and filename part
274 * 1) Directory part used to open include files
275 * 2) Filename part used to generate output filenames
277 ******************************************************************************/
280 FlSplitInputPathname (
282 char **OutDirectoryPath
,
290 *OutDirectoryPath
= NULL
;
297 /* Get the path to the input filename's directory */
299 DirectoryPath
= FlStrdup (InputPath
);
302 return (AE_NO_MEMORY
);
305 /* Convert backslashes to slashes in the entire path */
307 UtConvertBackslashes (DirectoryPath
);
309 /* Backup to last slash or colon */
311 Substring
= strrchr (DirectoryPath
, '/');
314 Substring
= strrchr (DirectoryPath
, ':');
317 /* Extract the simple filename */
321 Filename
= FlStrdup (DirectoryPath
);
322 DirectoryPath
[0] = 0;
326 Filename
= FlStrdup (Substring
+ 1);
332 ACPI_FREE (DirectoryPath
);
333 return (AE_NO_MEMORY
);
336 *OutDirectoryPath
= DirectoryPath
;
340 *OutFilename
= Filename
;
344 ACPI_FREE (Filename
);