apfiles.c revision 1.2.4.2 1 1.2.4.2 rmind /******************************************************************************
2 1.2.4.2 rmind *
3 1.2.4.2 rmind * Module Name: apfiles - File-related functions for acpidump utility
4 1.2.4.2 rmind *
5 1.2.4.2 rmind *****************************************************************************/
6 1.2.4.2 rmind
7 1.2.4.2 rmind /*
8 1.2.4.2 rmind * Copyright (C) 2000 - 2013, Intel Corp.
9 1.2.4.2 rmind * All rights reserved.
10 1.2.4.2 rmind *
11 1.2.4.2 rmind * Redistribution and use in source and binary forms, with or without
12 1.2.4.2 rmind * modification, are permitted provided that the following conditions
13 1.2.4.2 rmind * are met:
14 1.2.4.2 rmind * 1. Redistributions of source code must retain the above copyright
15 1.2.4.2 rmind * notice, this list of conditions, and the following disclaimer,
16 1.2.4.2 rmind * without modification.
17 1.2.4.2 rmind * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.2.4.2 rmind * substantially similar to the "NO WARRANTY" disclaimer below
19 1.2.4.2 rmind * ("Disclaimer") and any redistribution must be conditioned upon
20 1.2.4.2 rmind * including a substantially similar Disclaimer requirement for further
21 1.2.4.2 rmind * binary redistribution.
22 1.2.4.2 rmind * 3. Neither the names of the above-listed copyright holders nor the names
23 1.2.4.2 rmind * of any contributors may be used to endorse or promote products derived
24 1.2.4.2 rmind * from this software without specific prior written permission.
25 1.2.4.2 rmind *
26 1.2.4.2 rmind * Alternatively, this software may be distributed under the terms of the
27 1.2.4.2 rmind * GNU General Public License ("GPL") version 2 as published by the Free
28 1.2.4.2 rmind * Software Foundation.
29 1.2.4.2 rmind *
30 1.2.4.2 rmind * NO WARRANTY
31 1.2.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.2.4.2 rmind * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.2.4.2 rmind * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.2.4.2 rmind * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.2.4.2 rmind * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.2.4.2 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.2.4.2 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.2.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.2.4.2 rmind * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.2.4.2 rmind * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.2.4.2 rmind * POSSIBILITY OF SUCH DAMAGES.
42 1.2.4.2 rmind */
43 1.2.4.2 rmind
44 1.2.4.2 rmind #include "acpidump.h"
45 1.2.4.2 rmind #include "acapps.h"
46 1.2.4.2 rmind
47 1.2.4.2 rmind
48 1.2.4.2 rmind /******************************************************************************
49 1.2.4.2 rmind *
50 1.2.4.2 rmind * FUNCTION: ApOpenOutputFile
51 1.2.4.2 rmind *
52 1.2.4.2 rmind * PARAMETERS: Pathname - Output filename
53 1.2.4.2 rmind *
54 1.2.4.2 rmind * RETURN: Open file handle
55 1.2.4.2 rmind *
56 1.2.4.2 rmind * DESCRIPTION: Open a text output file for acpidump. Checks if file already
57 1.2.4.2 rmind * exists.
58 1.2.4.2 rmind *
59 1.2.4.2 rmind ******************************************************************************/
60 1.2.4.2 rmind
61 1.2.4.2 rmind int
62 1.2.4.2 rmind ApOpenOutputFile (
63 1.2.4.2 rmind char *Pathname)
64 1.2.4.2 rmind {
65 1.2.4.2 rmind struct stat StatInfo;
66 1.2.4.2 rmind FILE *File;
67 1.2.4.2 rmind
68 1.2.4.2 rmind
69 1.2.4.2 rmind /* If file exists, prompt for overwrite */
70 1.2.4.2 rmind
71 1.2.4.2 rmind if (!stat (Pathname, &StatInfo))
72 1.2.4.2 rmind {
73 1.2.4.2 rmind fprintf (stderr, "Target path already exists, overwrite? [y|n] ");
74 1.2.4.2 rmind
75 1.2.4.2 rmind if (getchar () != 'y')
76 1.2.4.2 rmind {
77 1.2.4.2 rmind return (-1);
78 1.2.4.2 rmind }
79 1.2.4.2 rmind }
80 1.2.4.2 rmind
81 1.2.4.2 rmind /* Point stdout to the file */
82 1.2.4.2 rmind
83 1.2.4.2 rmind File = freopen (Pathname, "w", stdout);
84 1.2.4.2 rmind if (!File)
85 1.2.4.2 rmind {
86 1.2.4.2 rmind perror ("Could not open output file");
87 1.2.4.2 rmind return (-1);
88 1.2.4.2 rmind }
89 1.2.4.2 rmind
90 1.2.4.2 rmind /* Save the file and path */
91 1.2.4.2 rmind
92 1.2.4.2 rmind Gbl_OutputFile = File;
93 1.2.4.2 rmind Gbl_OutputFilename = Pathname;
94 1.2.4.2 rmind return (0);
95 1.2.4.2 rmind }
96 1.2.4.2 rmind
97 1.2.4.2 rmind
98 1.2.4.2 rmind /******************************************************************************
99 1.2.4.2 rmind *
100 1.2.4.2 rmind * FUNCTION: ApWriteToBinaryFile
101 1.2.4.2 rmind *
102 1.2.4.2 rmind * PARAMETERS: Table - ACPI table to be written
103 1.2.4.2 rmind * Instance - ACPI table instance no. to be written
104 1.2.4.2 rmind *
105 1.2.4.2 rmind * RETURN: Status
106 1.2.4.2 rmind *
107 1.2.4.2 rmind * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
108 1.2.4.2 rmind * filename from the table signature.
109 1.2.4.2 rmind *
110 1.2.4.2 rmind ******************************************************************************/
111 1.2.4.2 rmind
112 1.2.4.2 rmind int
113 1.2.4.2 rmind ApWriteToBinaryFile (
114 1.2.4.2 rmind ACPI_TABLE_HEADER *Table,
115 1.2.4.2 rmind UINT32 Instance)
116 1.2.4.2 rmind {
117 1.2.4.2 rmind char Filename[ACPI_NAME_SIZE + 16];
118 1.2.4.2 rmind char InstanceStr [16];
119 1.2.4.2 rmind FILE *File;
120 1.2.4.2 rmind size_t Actual;
121 1.2.4.2 rmind UINT32 TableLength;
122 1.2.4.2 rmind
123 1.2.4.2 rmind
124 1.2.4.2 rmind /* Obtain table length */
125 1.2.4.2 rmind
126 1.2.4.2 rmind TableLength = ApGetTableLength (Table);
127 1.2.4.2 rmind
128 1.2.4.2 rmind /* Construct lower-case filename from the table local signature */
129 1.2.4.2 rmind
130 1.2.4.2 rmind if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
131 1.2.4.2 rmind {
132 1.2.4.2 rmind ACPI_MOVE_NAME (Filename, AP_DUMP_SIG_RSDP);
133 1.2.4.2 rmind }
134 1.2.4.2 rmind else
135 1.2.4.2 rmind {
136 1.2.4.2 rmind ACPI_MOVE_NAME (Filename, Table->Signature);
137 1.2.4.2 rmind }
138 1.2.4.2 rmind Filename[0] = (char) ACPI_TOLOWER (Filename[0]);
139 1.2.4.2 rmind Filename[1] = (char) ACPI_TOLOWER (Filename[1]);
140 1.2.4.2 rmind Filename[2] = (char) ACPI_TOLOWER (Filename[2]);
141 1.2.4.2 rmind Filename[3] = (char) ACPI_TOLOWER (Filename[3]);
142 1.2.4.2 rmind Filename[ACPI_NAME_SIZE] = 0;
143 1.2.4.2 rmind
144 1.2.4.2 rmind /* Handle multiple SSDTs - create different filenames for each */
145 1.2.4.2 rmind
146 1.2.4.2 rmind if (Instance > 0)
147 1.2.4.2 rmind {
148 1.2.4.2 rmind snprintf (InstanceStr, sizeof(InstanceStr), "%u", Instance);
149 1.2.4.2 rmind strcat (Filename, InstanceStr);
150 1.2.4.2 rmind }
151 1.2.4.2 rmind
152 1.2.4.2 rmind strcat (Filename, ACPI_TABLE_FILE_SUFFIX);
153 1.2.4.2 rmind
154 1.2.4.2 rmind if (Gbl_VerboseMode)
155 1.2.4.2 rmind {
156 1.2.4.2 rmind fprintf (stderr,
157 1.2.4.2 rmind "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
158 1.2.4.2 rmind Table->Signature, Filename, Table->Length, Table->Length);
159 1.2.4.2 rmind }
160 1.2.4.2 rmind
161 1.2.4.2 rmind /* Open the file and dump the entire table in binary mode */
162 1.2.4.2 rmind
163 1.2.4.2 rmind File = fopen (Filename, "wb");
164 1.2.4.2 rmind if (!File)
165 1.2.4.2 rmind {
166 1.2.4.2 rmind perror ("Could not open output file");
167 1.2.4.2 rmind return (-1);
168 1.2.4.2 rmind }
169 1.2.4.2 rmind
170 1.2.4.2 rmind Actual = fwrite (Table, 1, TableLength, File);
171 1.2.4.2 rmind if (Actual != TableLength)
172 1.2.4.2 rmind {
173 1.2.4.2 rmind perror ("Error writing binary output file");
174 1.2.4.2 rmind fclose (File);
175 1.2.4.2 rmind return (-1);
176 1.2.4.2 rmind }
177 1.2.4.2 rmind
178 1.2.4.2 rmind fclose (File);
179 1.2.4.2 rmind return (0);
180 1.2.4.2 rmind }
181 1.2.4.2 rmind
182 1.2.4.2 rmind
183 1.2.4.2 rmind /******************************************************************************
184 1.2.4.2 rmind *
185 1.2.4.2 rmind * FUNCTION: ApGetTableFromFile
186 1.2.4.2 rmind *
187 1.2.4.2 rmind * PARAMETERS: Pathname - File containing the binary ACPI table
188 1.2.4.2 rmind * OutFileSize - Where the file size is returned
189 1.2.4.2 rmind *
190 1.2.4.2 rmind * RETURN: Buffer containing the ACPI table. NULL on error.
191 1.2.4.2 rmind *
192 1.2.4.2 rmind * DESCRIPTION: Open a file and read it entirely into a new buffer
193 1.2.4.2 rmind *
194 1.2.4.2 rmind ******************************************************************************/
195 1.2.4.2 rmind
196 1.2.4.2 rmind ACPI_TABLE_HEADER *
197 1.2.4.2 rmind ApGetTableFromFile (
198 1.2.4.2 rmind char *Pathname,
199 1.2.4.2 rmind UINT32 *OutFileSize)
200 1.2.4.2 rmind {
201 1.2.4.2 rmind ACPI_TABLE_HEADER *Buffer = NULL;
202 1.2.4.2 rmind FILE *File;
203 1.2.4.2 rmind UINT32 FileSize;
204 1.2.4.2 rmind size_t Actual;
205 1.2.4.2 rmind
206 1.2.4.2 rmind
207 1.2.4.2 rmind /* Must use binary mode */
208 1.2.4.2 rmind
209 1.2.4.2 rmind File = fopen (Pathname, "rb");
210 1.2.4.2 rmind if (!File)
211 1.2.4.2 rmind {
212 1.2.4.2 rmind perror ("Could not open input file");
213 1.2.4.2 rmind return (NULL);
214 1.2.4.2 rmind }
215 1.2.4.2 rmind
216 1.2.4.2 rmind /* Need file size to allocate a buffer */
217 1.2.4.2 rmind
218 1.2.4.2 rmind FileSize = ApGetFileSize (File);
219 1.2.4.2 rmind if (!FileSize)
220 1.2.4.2 rmind {
221 1.2.4.2 rmind fprintf (stderr,
222 1.2.4.2 rmind "Could not get input file size: %s\n", Pathname);
223 1.2.4.2 rmind goto Cleanup;
224 1.2.4.2 rmind }
225 1.2.4.2 rmind
226 1.2.4.2 rmind /* Allocate a buffer for the entire file */
227 1.2.4.2 rmind
228 1.2.4.2 rmind Buffer = calloc (1, FileSize);
229 1.2.4.2 rmind if (!Buffer)
230 1.2.4.2 rmind {
231 1.2.4.2 rmind fprintf (stderr,
232 1.2.4.2 rmind "Could not allocate file buffer of size: %u\n", FileSize);
233 1.2.4.2 rmind goto Cleanup;
234 1.2.4.2 rmind }
235 1.2.4.2 rmind
236 1.2.4.2 rmind /* Read the entire file */
237 1.2.4.2 rmind
238 1.2.4.2 rmind Actual = fread (Buffer, 1, FileSize, File);
239 1.2.4.2 rmind if (Actual != FileSize)
240 1.2.4.2 rmind {
241 1.2.4.2 rmind fprintf (stderr,
242 1.2.4.2 rmind "Could not read input file: %s\n", Pathname);
243 1.2.4.2 rmind free (Buffer);
244 1.2.4.2 rmind Buffer = NULL;
245 1.2.4.2 rmind goto Cleanup;
246 1.2.4.2 rmind }
247 1.2.4.2 rmind
248 1.2.4.2 rmind *OutFileSize = FileSize;
249 1.2.4.2 rmind
250 1.2.4.2 rmind Cleanup:
251 1.2.4.2 rmind fclose (File);
252 1.2.4.2 rmind return (Buffer);
253 1.2.4.2 rmind }
254 1.2.4.2 rmind
255 1.2.4.2 rmind
256 1.2.4.2 rmind /******************************************************************************
257 1.2.4.2 rmind *
258 1.2.4.2 rmind * FUNCTION: ApGetFileSize
259 1.2.4.2 rmind *
260 1.2.4.2 rmind * PARAMETERS: File - Open file descriptor
261 1.2.4.2 rmind *
262 1.2.4.2 rmind * RETURN: File size in bytes
263 1.2.4.2 rmind *
264 1.2.4.2 rmind * DESCRIPTION: Get the size of an open file
265 1.2.4.2 rmind *
266 1.2.4.2 rmind ******************************************************************************/
267 1.2.4.2 rmind
268 1.2.4.2 rmind UINT32
269 1.2.4.2 rmind ApGetFileSize (
270 1.2.4.2 rmind FILE *File)
271 1.2.4.2 rmind {
272 1.2.4.2 rmind UINT32 FileSize;
273 1.2.4.2 rmind long Offset;
274 1.2.4.2 rmind
275 1.2.4.2 rmind
276 1.2.4.2 rmind Offset = ftell (File);
277 1.2.4.2 rmind if (fseek (File, 0, SEEK_END))
278 1.2.4.2 rmind {
279 1.2.4.2 rmind return (0);
280 1.2.4.2 rmind }
281 1.2.4.2 rmind
282 1.2.4.2 rmind /* Get size and restore file pointer */
283 1.2.4.2 rmind
284 1.2.4.2 rmind FileSize = (UINT32) ftell (File);
285 1.2.4.2 rmind if (fseek (File, Offset, SEEK_SET))
286 1.2.4.2 rmind {
287 1.2.4.2 rmind return (0);
288 1.2.4.2 rmind }
289 1.2.4.2 rmind
290 1.2.4.2 rmind return (FileSize);
291 1.2.4.2 rmind }
292