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