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