1 /******************************************************************************
3 * Module Name: apfiles - File-related functions for acpidump utility
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
48 /* Local prototypes */
63 if (!stat (Pathname, &StatInfo))
65 AcpiLogError ("Target path already exists, overwrite? [y|n] ");
67 if (getchar () != 'y')
78 /******************************************************************************
80 * FUNCTION: ApOpenOutputFile
82 * PARAMETERS: Pathname - Output filename
84 * RETURN: Open file handle
86 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
89 ******************************************************************************/
98 /* If file exists, prompt for overwrite */
100 if (ApIsExistingFile (Pathname) != 0)
105 /* Point stdout to the file */
107 File = AcpiOsOpenFile (Pathname, ACPI_FILE_WRITING);
110 AcpiLogError ("Could not open output file: %s\n", Pathname);
114 /* Save the file and path */
116 Gbl_OutputFile = File;
117 Gbl_OutputFilename = Pathname;
122 /******************************************************************************
124 * FUNCTION: ApWriteToBinaryFile
126 * PARAMETERS: Table - ACPI table to be written
127 * Instance - ACPI table instance no. to be written
131 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
132 * filename from the table signature.
134 ******************************************************************************/
137 ApWriteToBinaryFile (
138 ACPI_TABLE_HEADER *Table,
141 char Filename[ACPI_NAME_SIZE + 16];
142 char InstanceStr [16];
148 /* Obtain table length */
150 TableLength = ApGetTableLength (Table);
152 /* Construct lower-case filename from the table local signature */
154 if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
156 ACPI_MOVE_NAME (Filename, ACPI_RSDP_NAME);
160 ACPI_MOVE_NAME (Filename, Table->Signature);
162 Filename[0] = (char) tolower ((int) Filename[0]);
163 Filename[1] = (char) tolower ((int) Filename[1]);
164 Filename[2] = (char) tolower ((int) Filename[2]);
165 Filename[3] = (char) tolower ((int) Filename[3]);
166 Filename[ACPI_NAME_SIZE] = 0;
168 /* Handle multiple SSDTs - create different filenames for each */
172 AcpiUtSnprintf (InstanceStr, sizeof (InstanceStr), "%u", Instance);
173 strcat (Filename, InstanceStr);
176 strcat (Filename, ACPI_TABLE_FILE_SUFFIX);
181 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
182 Table->Signature, Filename, Table->Length, Table->Length);
185 /* Open the file and dump the entire table in binary mode */
187 File = AcpiOsOpenFile (Filename,
188 ACPI_FILE_WRITING | ACPI_FILE_BINARY);
191 AcpiLogError ("Could not open output file: %s\n", Filename);
195 Actual = AcpiOsWriteFile (File, Table, 1, TableLength);
196 if (Actual != TableLength)
198 AcpiLogError ("Error writing binary output file: %s\n", Filename);
199 AcpiOsCloseFile (File);
203 AcpiOsCloseFile (File);
208 /******************************************************************************
210 * FUNCTION: ApGetTableFromFile
212 * PARAMETERS: Pathname - File containing the binary ACPI table
213 * OutFileSize - Where the file size is returned
215 * RETURN: Buffer containing the ACPI table. NULL on error.
217 * DESCRIPTION: Open a file and read it entirely into a new buffer
219 ******************************************************************************/
226 ACPI_TABLE_HEADER *Buffer = NULL;
232 /* Must use binary mode */
234 File = AcpiOsOpenFile (Pathname, ACPI_FILE_READING | ACPI_FILE_BINARY);
237 AcpiLogError ("Could not open input file: %s\n", Pathname);
241 /* Need file size to allocate a buffer */
243 FileSize = CmGetFileSize (File);
244 if (FileSize == ACPI_UINT32_MAX)
247 "Could not get input file size: %s\n", Pathname);
251 /* Allocate a buffer for the entire file */
253 Buffer = ACPI_ALLOCATE_ZEROED (FileSize);
257 "Could not allocate file buffer of size: %u\n", FileSize);
261 /* Read the entire file */
263 Actual = AcpiOsReadFile (File, Buffer, 1, FileSize);
264 if (Actual != FileSize)
267 "Could not read input file: %s\n", Pathname);
273 *OutFileSize = FileSize;
276 AcpiOsCloseFile (File);