1 /******************************************************************************
3 * Module Name: apfiles - File-related functions for acpidump utility
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.
48 /******************************************************************************
50 * FUNCTION: ApOpenOutputFile
52 * PARAMETERS: Pathname - Output filename
54 * RETURN: Open file handle
56 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
59 ******************************************************************************/
69 /* If file exists, prompt for overwrite */
71 if (!stat (Pathname
, &StatInfo
))
73 fprintf (stderr
, "Target path already exists, overwrite? [y|n] ");
75 if (getchar () != 'y')
81 /* Point stdout to the file */
83 File
= freopen (Pathname
, "w", stdout
);
86 perror ("Could not open output file");
90 /* Save the file and path */
92 Gbl_OutputFile
= File
;
93 Gbl_OutputFilename
= Pathname
;
98 /******************************************************************************
100 * FUNCTION: ApWriteToBinaryFile
102 * PARAMETERS: Table - ACPI table to be written
103 * Instance - ACPI table instance no. to be written
107 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
108 * filename from the table signature.
110 ******************************************************************************/
113 ApWriteToBinaryFile (
114 ACPI_TABLE_HEADER
*Table
,
117 char Filename
[ACPI_NAME_SIZE
+ 16];
118 char InstanceStr
[16];
124 /* Obtain table length */
126 TableLength
= ApGetTableLength (Table
);
128 /* Construct lower-case filename from the table local signature */
130 if (ACPI_VALIDATE_RSDP_SIG (Table
->Signature
))
132 ACPI_MOVE_NAME (Filename
, AP_DUMP_SIG_RSDP
);
136 ACPI_MOVE_NAME (Filename
, Table
->Signature
);
138 Filename
[0] = (char) ACPI_TOLOWER (Filename
[0]);
139 Filename
[1] = (char) ACPI_TOLOWER (Filename
[1]);
140 Filename
[2] = (char) ACPI_TOLOWER (Filename
[2]);
141 Filename
[3] = (char) ACPI_TOLOWER (Filename
[3]);
142 Filename
[ACPI_NAME_SIZE
] = 0;
144 /* Handle multiple SSDTs - create different filenames for each */
148 sprintf (InstanceStr
, "%u", Instance
);
149 strcat (Filename
, InstanceStr
);
152 strcat (Filename
, ACPI_TABLE_FILE_SUFFIX
);
157 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
158 Table
->Signature
, Filename
, Table
->Length
, Table
->Length
);
161 /* Open the file and dump the entire table in binary mode */
163 File
= fopen (Filename
, "wb");
166 perror ("Could not open output file");
170 Actual
= fwrite (Table
, 1, TableLength
, File
);
171 if (Actual
!= TableLength
)
173 perror ("Error writing binary output file");
183 /******************************************************************************
185 * FUNCTION: ApGetTableFromFile
187 * PARAMETERS: Pathname - File containing the binary ACPI table
188 * OutFileSize - Where the file size is returned
190 * RETURN: Buffer containing the ACPI table. NULL on error.
192 * DESCRIPTION: Open a file and read it entirely into a new buffer
194 ******************************************************************************/
201 ACPI_TABLE_HEADER
*Buffer
= NULL
;
207 /* Must use binary mode */
209 File
= fopen (Pathname
, "rb");
212 perror ("Could not open input file");
216 /* Need file size to allocate a buffer */
218 FileSize
= ApGetFileSize (File
);
222 "Could not get input file size: %s\n", Pathname
);
226 /* Allocate a buffer for the entire file */
228 Buffer
= calloc (1, FileSize
);
232 "Could not allocate file buffer of size: %u\n", FileSize
);
236 /* Read the entire file */
238 Actual
= fread (Buffer
, 1, FileSize
, File
);
239 if (Actual
!= FileSize
)
242 "Could not read input file: %s\n", Pathname
);
248 *OutFileSize
= FileSize
;
256 /******************************************************************************
258 * FUNCTION: ApGetFileSize
260 * PARAMETERS: File - Open file descriptor
262 * RETURN: File size in bytes
264 * DESCRIPTION: Get the size of an open file
266 ******************************************************************************/
276 Offset
= ftell (File
);
277 if (fseek (File
, 0, SEEK_END
))
282 /* Get size and restore file pointer */
284 FileSize
= (UINT32
) ftell (File
);
285 if (fseek (File
, Offset
, SEEK_SET
))