kernel: Sync ACPICA with Intel's version 20140627.
[dragonfly.git] / sys / contrib / dev / acpica / source / os_specific / service_layers / oslibcfs.c
1 /******************************************************************************
2  *
3  * Module Name: oslibcfs - C library OSL for file IO
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 "acpi.h"
45 #include <stdio.h>
46 #include <stdarg.h>
47
48 #define _COMPONENT          ACPI_OS_SERVICES
49         ACPI_MODULE_NAME    ("oslibcfs")
50
51
52 /*******************************************************************************
53  *
54  * FUNCTION:    AcpiOsOpenFile
55  *
56  * PARAMETERS:  Path                - File path
57  *              Modes               - File operation type
58  *
59  * RETURN:      File descriptor.
60  *
61  * DESCRIPTION: Open a file for reading (ACPI_FILE_READING) or/and writing
62  *              (ACPI_FILE_WRITING).
63  *
64  ******************************************************************************/
65
66 ACPI_FILE
67 AcpiOsOpenFile (
68     const char              *Path,
69     UINT8                   Modes)
70 {
71     ACPI_FILE               File;
72     char                    ModesStr[4];
73     UINT32                  i = 0;
74
75     if (Modes & ACPI_FILE_READING)
76     {
77         ModesStr[i++] = 'r';
78     }
79     if (Modes & ACPI_FILE_WRITING)
80     {
81         ModesStr[i++] = 'w';
82     }
83     if (Modes & ACPI_FILE_BINARY)
84     {
85         ModesStr[i++] = 'b';
86     }
87     ModesStr[i++] = '\0';
88
89     File = fopen (Path, ModesStr);
90     if (!File)
91     {
92         perror ("Could not open file");
93     }
94
95     return (File);
96 }
97
98
99 /*******************************************************************************
100  *
101  * FUNCTION:    AcpiOsCloseFile
102  *
103  * PARAMETERS:  File                - File descriptor
104  *
105  * RETURN:      None.
106  *
107  * DESCRIPTION: Close a file.
108  *
109  ******************************************************************************/
110
111 void
112 AcpiOsCloseFile (
113     ACPI_FILE               File)
114 {
115     fclose (File);
116 }
117
118
119 /*******************************************************************************
120  *
121  * FUNCTION:    AcpiOsReadFile
122  *
123  * PARAMETERS:  File                - File descriptor
124  *              Buffer              - Data buffer
125  *              Size                - Data block size
126  *              Count               - Number of data blocks
127  *
128  * RETURN:      Size of successfully read buffer.
129  *
130  * DESCRIPTION: Read a file.
131  *
132  ******************************************************************************/
133
134 int
135 AcpiOsReadFile (
136     ACPI_FILE               File,
137     void                    *Buffer,
138     ACPI_SIZE               Size,
139     ACPI_SIZE               Count)
140 {
141     int                     Length;
142
143     Length = fread (Buffer, Size, Count, File);
144     if (Length < 0)
145     {
146         perror ("Error reading file");
147     }
148
149     return (Length);
150 }
151
152
153 /*******************************************************************************
154  *
155  * FUNCTION:    AcpiOsWriteFile
156  *
157  * PARAMETERS:  File                - File descriptor
158  *              Buffer              - Data buffer
159  *              Size                - Data block size
160  *              Count               - Number of data blocks
161  *
162  * RETURN:      Size of successfully written buffer.
163  *
164  * DESCRIPTION: Write a file.
165  *
166  ******************************************************************************/
167
168 int
169 AcpiOsWriteFile (
170     ACPI_FILE               File,
171     void                    *Buffer,
172     ACPI_SIZE               Size,
173     ACPI_SIZE               Count)
174 {
175     int                     Length;
176
177     Length = fwrite (Buffer, Size, Count, File);
178     if (Length < 0)
179     {
180         perror ("Error writing file");
181     }
182
183     return (Length);
184 }
185
186
187 /*******************************************************************************
188  *
189  * FUNCTION:    AcpiOsGetFileOffset
190  *
191  * PARAMETERS:  File                - File descriptor
192  *
193  * RETURN:      Size of current position.
194  *
195  * DESCRIPTION: Get current file offset.
196  *
197  ******************************************************************************/
198
199 long
200 AcpiOsGetFileOffset (
201     ACPI_FILE               File)
202 {
203     long                    Offset;
204
205     Offset = ftell (File);
206
207     return (Offset);
208 }
209
210
211 /*******************************************************************************
212  *
213  * FUNCTION:    AcpiOsSetFileOffset
214  *
215  * PARAMETERS:  File                - File descriptor
216  *              Offset              - File offset
217  *              From                - From begin/end of file
218  *
219  * RETURN:      Status
220  *
221  * DESCRIPTION: Set current file offset.
222  *
223  ******************************************************************************/
224
225 ACPI_STATUS
226 AcpiOsSetFileOffset (
227     ACPI_FILE               File,
228     long                    Offset,
229     UINT8                   From)
230 {
231     int                     Ret = 0;
232
233
234     if (From == ACPI_FILE_BEGIN)
235     {
236         Ret = fseek (File, Offset, SEEK_SET);
237     }
238     if (From == ACPI_FILE_END)
239     {
240         Ret = fseek (File, Offset, SEEK_END);
241     }
242
243     if (Ret < 0)
244     {
245         return (AE_ERROR);
246     }
247     else
248     {
249         return (AE_OK);
250     }
251 }