]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/tools/acpidump/apfiles.c
Import ACPICA 20130517.
[FreeBSD/FreeBSD.git] / source / tools / acpidump / apfiles.c
1 /******************************************************************************
2  *
3  * Module Name: apfiles - File-related functions for acpidump utility
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43
44 #include "acpidump.h"
45 #include "acapps.h"
46
47
48 /******************************************************************************
49  *
50  * FUNCTION:    ApOpenOutputFile
51  *
52  * PARAMETERS:  Pathname            - Output filename
53  *
54  * RETURN:      Open file handle
55  *
56  * DESCRIPTION: Open a text output file for acpidump. Checks if file already
57  *              exists.
58  *
59  ******************************************************************************/
60
61 int
62 ApOpenOutputFile (
63     char                    *Pathname)
64 {
65     struct stat             StatInfo;
66     FILE                    *File;
67
68
69     /* If file exists, prompt for overwrite */
70
71     if (!stat (Pathname, &StatInfo))
72     {
73         fprintf (stderr, "Target path already exists, overwrite? [y|n] ");
74
75         if (getchar () != 'y')
76         {
77             return (-1);
78         }
79     }
80
81     /* Point stdout to the file */
82
83     File = freopen (Pathname, "w", stdout);
84     if (!File)
85     {
86         perror ("Could not open output file");
87         return (-1);
88     }
89
90     /* Save the file and path */
91
92     Gbl_OutputFile = File;
93     Gbl_OutputFilename = Pathname;
94     return (0);
95 }
96
97
98 /******************************************************************************
99  *
100  * FUNCTION:    ApWriteToBinaryFile
101  *
102  * PARAMETERS:  Table               - ACPI table to be written
103  *
104  * RETURN:      Status
105  *
106  * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
107  *              filename from the table signature.
108  *
109  ******************************************************************************/
110
111 int
112 ApWriteToBinaryFile (
113     ACPI_TABLE_HEADER       *Table)
114 {
115     char                    Filename[ACPI_NAME_SIZE + 16];
116     char                    SsdtInstance [16];
117     FILE                    *File;
118     size_t                  Actual;
119
120
121     /* Construct lower-case filename from the table signature */
122
123     Filename[0] = (char) ACPI_TOLOWER (Table->Signature[0]);
124     Filename[1] = (char) ACPI_TOLOWER (Table->Signature[1]);
125     Filename[2] = (char) ACPI_TOLOWER (Table->Signature[2]);
126     Filename[3] = (char) ACPI_TOLOWER (Table->Signature[3]);
127     Filename[ACPI_NAME_SIZE] = 0;
128
129     /* Handle multiple SSDTs - create different filenames for each */
130
131     if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
132     {
133         sprintf (SsdtInstance, "%u", Gbl_SsdtCount);
134         strcat (Filename, SsdtInstance);
135         Gbl_SsdtCount++;
136     }
137
138     strcat (Filename, ACPI_TABLE_FILE_SUFFIX);
139
140     if (Gbl_VerboseMode)
141     {
142         fprintf (stderr,
143             "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
144             Table->Signature, Filename, Table->Length, Table->Length);
145     }
146
147     /* Open the file and dump the entire table in binary mode */
148
149     File = fopen (Filename, "wb");
150     if (!File)
151     {
152         perror ("Could not open output file");
153         return (-1);
154     }
155
156     Actual = fwrite (Table, 1, Table->Length, File);
157     if (Actual != Table->Length)
158     {
159         perror ("Error writing binary output file");
160         fclose (File);
161         return (-1);
162     }
163
164     fclose (File);
165     return (0);
166 }
167
168
169 /******************************************************************************
170  *
171  * FUNCTION:    ApGetTableFromFile
172  *
173  * PARAMETERS:  Pathname            - File containing the binary ACPI table
174  *              OutFileSize         - Where the file size is returned
175  *
176  * RETURN:      Buffer containing the ACPI table. NULL on error.
177  *
178  * DESCRIPTION: Open a file and read it entirely into a new buffer
179  *
180  ******************************************************************************/
181
182 ACPI_TABLE_HEADER *
183 ApGetTableFromFile (
184     char                    *Pathname,
185     UINT32                  *OutFileSize)
186 {
187     ACPI_TABLE_HEADER       *Buffer = NULL;
188     FILE                    *File;
189     UINT32                  FileSize;
190     size_t                  Actual;
191
192
193     /* Must use binary mode */
194
195     File = fopen (Pathname, "rb");
196     if (!File)
197     {
198         perror ("Could not open input file");
199         return (NULL);
200     }
201
202     /* Need file size to allocate a buffer */
203
204     FileSize = ApGetFileSize (File);
205     if (!FileSize)
206     {
207         fprintf (stderr,
208             "Could not get input file size: %s\n", Pathname);
209         goto Cleanup;
210     }
211
212     /* Allocate a buffer for the entire file */
213
214     Buffer = calloc (1, FileSize);
215     if (!Buffer)
216     {
217         fprintf (stderr,
218             "Could not allocate file buffer of size: %u\n", FileSize);
219         goto Cleanup;
220     }
221
222     /* Read the entire file */
223
224     Actual = fread (Buffer, 1, FileSize, File);
225     if (Actual != FileSize)
226     {
227         fprintf (stderr,
228             "Could not read input file: %s\n", Pathname);
229         free (Buffer);
230         Buffer = NULL;
231         goto Cleanup;
232     }
233
234     *OutFileSize = FileSize;
235
236 Cleanup:
237     fclose (File);
238     return (Buffer);
239 }
240
241
242 /******************************************************************************
243  *
244  * FUNCTION:    ApGetFileSize
245  *
246  * PARAMETERS:  File                - Open file descriptor
247  *
248  * RETURN:      File size in bytes
249  *
250  * DESCRIPTION: Get the size of an open file
251  *
252  ******************************************************************************/
253
254 UINT32
255 ApGetFileSize (
256     FILE                    *File)
257 {
258     UINT32                  FileSize;
259     long                    Offset;
260
261
262     Offset = ftell (File);
263     if (fseek (File, 0, SEEK_END))
264     {
265         return (0);
266     }
267
268     /* Get size and restore file pointer */
269
270     FileSize = (UINT32) ftell (File);
271     if (fseek (File, Offset, SEEK_SET))
272     {
273         return (0);
274     }
275
276     return (FileSize);
277 }