1 /******************************************************************************
3 * Module Name: apfiles - File-related functions for acpidump utility
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 */
55 /******************************************************************************
57 * FUNCTION: ApIsExistingFile
59 * PARAMETERS: Pathname - Output filename
61 * RETURN: 0 on success
63 * DESCRIPTION: Query for file overwrite if it already exists.
65 ******************************************************************************/
75 if (!stat (Pathname
, &StatInfo
))
77 AcpiLogError ("Target path already exists, overwrite? [y|n] ");
79 if (getchar () != 'y')
90 /******************************************************************************
92 * FUNCTION: ApOpenOutputFile
94 * PARAMETERS: Pathname - Output filename
96 * RETURN: Open file handle
98 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
101 ******************************************************************************/
110 /* If file exists, prompt for overwrite */
112 if (ApIsExistingFile (Pathname
) != 0)
117 /* Point stdout to the file */
119 File
= AcpiOsOpenFile (Pathname
, ACPI_FILE_WRITING
);
122 AcpiLogError ("Could not open output file: %s\n", Pathname
);
126 /* Save the file and path */
128 Gbl_OutputFile
= File
;
129 Gbl_OutputFilename
= Pathname
;
134 /******************************************************************************
136 * FUNCTION: ApWriteToBinaryFile
138 * PARAMETERS: Table - ACPI table to be written
139 * Instance - ACPI table instance no. to be written
143 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
144 * filename from the table signature.
146 ******************************************************************************/
149 ApWriteToBinaryFile (
150 ACPI_TABLE_HEADER
*Table
,
153 char Filename
[ACPI_NAME_SIZE
+ 16];
154 char InstanceStr
[16];
160 /* Obtain table length */
162 TableLength
= ApGetTableLength (Table
);
164 /* Construct lower-case filename from the table local signature */
166 if (ACPI_VALIDATE_RSDP_SIG (Table
->Signature
))
168 ACPI_MOVE_NAME (Filename
, ACPI_RSDP_NAME
);
172 ACPI_MOVE_NAME (Filename
, Table
->Signature
);
175 Filename
[0] = (char) tolower ((int) Filename
[0]);
176 Filename
[1] = (char) tolower ((int) Filename
[1]);
177 Filename
[2] = (char) tolower ((int) Filename
[2]);
178 Filename
[3] = (char) tolower ((int) Filename
[3]);
179 Filename
[ACPI_NAME_SIZE
] = 0;
181 /* Handle multiple SSDTs - create different filenames for each */
185 AcpiUtSnprintf (InstanceStr
, sizeof (InstanceStr
), "%u", Instance
);
186 strcat (Filename
, InstanceStr
);
189 strcat (Filename
, FILE_SUFFIX_BINARY_TABLE
);
194 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
195 Table
->Signature
, Filename
, Table
->Length
, Table
->Length
);
198 /* Open the file and dump the entire table in binary mode */
200 File
= AcpiOsOpenFile (Filename
,
201 ACPI_FILE_WRITING
| ACPI_FILE_BINARY
);
204 AcpiLogError ("Could not open output file: %s\n", Filename
);
208 Actual
= AcpiOsWriteFile (File
, Table
, 1, TableLength
);
209 if (Actual
!= TableLength
)
211 AcpiLogError ("Error writing binary output file: %s\n", Filename
);
212 AcpiOsCloseFile (File
);
216 AcpiOsCloseFile (File
);
221 /******************************************************************************
223 * FUNCTION: ApGetTableFromFile
225 * PARAMETERS: Pathname - File containing the binary ACPI table
226 * OutFileSize - Where the file size is returned
228 * RETURN: Buffer containing the ACPI table. NULL on error.
230 * DESCRIPTION: Open a file and read it entirely into a new buffer
232 ******************************************************************************/
239 ACPI_TABLE_HEADER
*Buffer
= NULL
;
245 /* Must use binary mode */
247 File
= AcpiOsOpenFile (Pathname
, ACPI_FILE_READING
| ACPI_FILE_BINARY
);
250 AcpiLogError ("Could not open input file: %s\n", Pathname
);
254 /* Need file size to allocate a buffer */
256 FileSize
= CmGetFileSize (File
);
257 if (FileSize
== ACPI_UINT32_MAX
)
260 "Could not get input file size: %s\n", Pathname
);
264 /* Allocate a buffer for the entire file */
266 Buffer
= ACPI_ALLOCATE_ZEROED (FileSize
);
270 "Could not allocate file buffer of size: %u\n", FileSize
);
274 /* Read the entire file */
276 Actual
= AcpiOsReadFile (File
, Buffer
, 1, FileSize
);
277 if (Actual
!= FileSize
)
280 "Could not read input file: %s\n", Pathname
);
286 *OutFileSize
= FileSize
;
289 AcpiOsCloseFile (File
);