]>
Commit | Line | Data |
---|---|---|
b00771f5 | 1 | /** @file\r |
2 | The EFI kernel's interpretation of a "file".\r | |
3 | \r | |
a9c12422 | 4 | Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r |
b00771f5 | 5 | This program and the accompanying materials are licensed and made available\r |
6 | under the terms and conditions of the BSD License which accompanies this\r | |
7 | distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | * Copyright (c) 1982, 1986, 1989, 1993\r | |
14 | * The Regents of the University of California. All rights reserved.\r | |
15 | *\r | |
16 | * Redistribution and use in source and binary forms, with or without\r | |
17 | * modification, are permitted provided that the following conditions\r | |
18 | * are met:\r | |
19 | * 1. Redistributions of source code must retain the above copyright\r | |
20 | * notice, this list of conditions and the following disclaimer.\r | |
21 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
22 | * notice, this list of conditions and the following disclaimer in the\r | |
23 | * documentation and/or other materials provided with the distribution.\r | |
24 | * 3. Neither the name of the University nor the names of its contributors\r | |
25 | * may be used to endorse or promote products derived from this software\r | |
26 | * without specific prior written permission.\r | |
27 | *\r | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
38 | * SUCH DAMAGE.\r | |
39 | *\r | |
40 | * file.h 8.3 (Berkeley) 1/9/95\r | |
41 | NetBSD: file.h,v 1.56 2006/05/14 21:38:18 elad Exp\r | |
42 | **/\r | |
43 | #ifndef _PIF_KFILE_H_\r | |
44 | #define _PIF_KFILE_H_\r | |
45 | \r | |
46 | #include <Uefi.h>\r | |
47 | #include <Protocol/SimpleTextOut.h>\r | |
48 | #include <Protocol/SimpleFileSystem.h>\r | |
49 | \r | |
50 | #include <wchar.h>\r | |
76beedc0 | 51 | #include <stdarg.h>\r |
b00771f5 | 52 | #include <sys/fcntl.h>\r |
53 | #include <sys/unistd.h>\r | |
54 | \r | |
55 | struct stat;\r | |
56 | struct fileops;\r | |
57 | struct _Device_Node;\r | |
58 | \r | |
59 | /* The number of "special" character stream devices.\r | |
60 | These include:\r | |
61 | stdin, stdout, stderr\r | |
62 | */\r | |
63 | #define NUM_SPECIAL 3\r | |
64 | \r | |
65 | /* Organization of the f_iflags member of the __filedes structure. */\r | |
66 | #define DTYPE_MASK 0x00000007 ///< Device Type\r | |
67 | #define DTYPE_VNODE 1 /* file */\r | |
68 | #define DTYPE_SOCKET 2 /* communications endpoint */\r | |
69 | #define DTYPE_PIPE 3 /* pipe */\r | |
70 | #define DTYPE_KQUEUE 4 /* event queue */\r | |
71 | #define DTYPE_MISC 5 /* misc file descriptor type */\r | |
72 | #define DTYPE_CRYPTO 6 /* crypto */\r | |
73 | #define DTYPE_NAMES "0", "file", "socket", "pipe", "kqueue", "misc", "crypto"\r | |
74 | \r | |
75 | #define FIF_WANTCLOSE 0x10000000 /* a close is waiting for usecount */\r | |
76 | #define FIF_DELCLOSE 0x20000000 /* Delete on close. */\r | |
77 | #define FIF_LARVAL 0x80000000 /* not fully constructed; don't use */\r | |
78 | \r | |
79 | /*\r | |
80 | This structure must be a multiple of 8 bytes in length.\r | |
81 | */\r | |
82 | struct __filedes {\r | |
f766dd76 | 83 | off_t f_offset; /* current position in file */\r |
b00771f5 | 84 | const struct fileops *f_ops;\r |
a9c12422 | 85 | \r |
86 | /* The devdata member has different meanings depending upon whether\r | |
87 | a block oriented or character oriented device is being accessed.\r | |
88 | For block devices, devdata holds an EFI handle to the open file or directory.\r | |
89 | For character devices, devdata points to the device's IIO structure,\r | |
90 | if it has one. It may be NULL indicating a non-interactive character\r | |
91 | device.\r | |
92 | */\r | |
b00771f5 | 93 | void *devdata; /* Device-specific data */\r |
a9c12422 | 94 | int Oflags; // From the open call, see fcntl.h\r |
b00771f5 | 95 | int Omode; // From the open call\r |
96 | int RefCount; // Reference count of opens\r | |
f766dd76 | 97 | UINT32 f_flag; /* see fcntl.h */\r |
98 | UINT32 f_iflags; // In use if non-zero\r | |
b00771f5 | 99 | UINT16 MyFD; // Which FD this is.\r |
100 | UINT16 Reserved_1; // Force this structure to be a multiple of 8-bytes in length\r | |
101 | };\r | |
102 | \r | |
103 | struct fileops {\r | |
104 | /* These functions must always be implemented. */\r | |
450ea6d5 DM |
105 | \r |
106 | /** Perform device specific operations for closing the device.\r | |
107 | It is the responsibility of this function to flush or discard\r | |
108 | buffer contents.\r | |
109 | **/\r | |
b00771f5 | 110 | int (EFIAPI *fo_close) (struct __filedes *filp);\r |
450ea6d5 | 111 | \r |
b00771f5 | 112 | ssize_t (EFIAPI *fo_read) (struct __filedes *filp, off_t *Offset, size_t Len, void *Buf);\r |
113 | ssize_t (EFIAPI *fo_write) (struct __filedes *filp, off_t *Offset, size_t Len, const void *Buf);\r | |
114 | \r | |
115 | /* Call the fnullop_* version of these functions if not implemented by the device. */\r | |
116 | int (EFIAPI *fo_fcntl) (struct __filedes *filp, UINT32 Cmd, void *p3, void *p4);\r | |
117 | short (EFIAPI *fo_poll) (struct __filedes *filp, short Events);\r | |
118 | int (EFIAPI *fo_flush) (struct __filedes *filp);\r | |
119 | \r | |
120 | /* Call the fbadop_* version of these functions if not implemented by the device. */\r | |
121 | int (EFIAPI *fo_stat) (struct __filedes *filp, struct stat *StatBuf, void *Buf);\r | |
76beedc0 | 122 | int (EFIAPI *fo_ioctl) (struct __filedes *filp, ULONGN Cmd, va_list argp);\r |
b00771f5 | 123 | int (EFIAPI *fo_delete) (struct __filedes *filp);\r |
124 | int (EFIAPI *fo_rmdir) (struct __filedes *filp);\r | |
125 | int (EFIAPI *fo_mkdir) (const char *path, __mode_t perms);\r | |
126 | int (EFIAPI *fo_rename) (const char *from, const char *to);\r | |
127 | \r | |
128 | /* Use a NULL if this function has not been implemented by the device. */\r | |
129 | off_t (EFIAPI *fo_lseek) (struct __filedes *filp, off_t, int);\r | |
130 | };\r | |
131 | \r | |
450ea6d5 | 132 | /* A generic instance structure which is valid\r |
b00771f5 | 133 | for all device instance structures.\r |
134 | \r | |
135 | All device instance structures MUST be a multiple of 8-bytes in length.\r | |
136 | */\r | |
137 | typedef struct {\r | |
138 | UINT32 Cookie; ///< Special value identifying this as a valid Instance\r | |
139 | UINT32 InstanceNum; ///< Which instance is this? Zero-based.\r | |
140 | EFI_HANDLE Dev; ///< Pointer to either Input or Output Protocol.\r | |
141 | struct _Device_Node *Parent; ///< Points to the parent Device Node.\r | |
142 | struct fileops Abstraction; ///< Pointers to functions implementing this device's abstraction.\r | |
143 | UINTN Reserved_1; // Force this to always be a multiple of 8-bytes in length\r | |
144 | } GenericInstance;\r | |
145 | \r | |
146 | /* Type of all Device-specific handler's open routines. */\r | |
147 | typedef\r | |
f766dd76 | 148 | int (EFIAPI *FO_OPEN) (struct _Device_Node *This, struct __filedes *FD,\r |
149 | int Instance, wchar_t *Path, wchar_t *MPath);\r | |
b00771f5 | 150 | \r |
151 | #define FILE_IS_USABLE(fp) (((fp)->f_iflags & \\r | |
152 | (FIF_WANTCLOSE|FIF_LARVAL)) == 0)\r | |
153 | \r | |
154 | #define FILE_SET_MATURE(fp) \\r | |
155 | do { \\r | |
156 | (fp)->f_iflags &= ~FIF_LARVAL; \\r | |
157 | } while (/*CONSTCOND*/0)\r | |
158 | \r | |
159 | /*\r | |
160 | * Flags for fo_read and fo_write.\r | |
161 | */\r | |
162 | #define FOF_UPDATE_OFFSET 0x01 /* update the file offset */\r | |
163 | \r | |
164 | __BEGIN_DECLS\r | |
165 | \r | |
166 | int fdcreate (CHAR16 *, UINT32, UINT32, BOOLEAN, VOID *, const struct fileops *);\r | |
167 | \r | |
168 | /* Commonly used fileops\r | |
169 | fnullop_* Does nothing and returns success.\r | |
170 | fbadop_* Does nothing and returns EPERM\r | |
171 | */\r | |
f766dd76 | 172 | int EFIAPI fnullop_fcntl (struct __filedes *filp, UINT32 Cmd, void *p3, void *p4);\r |
173 | short EFIAPI fnullop_poll (struct __filedes *filp, short Events);\r | |
174 | int EFIAPI fnullop_flush (struct __filedes *filp);\r | |
175 | \r | |
176 | int EFIAPI fbadop_stat (struct __filedes *filp, struct stat *StatBuf, void *Buf);\r | |
76beedc0 | 177 | int EFIAPI fbadop_ioctl (struct __filedes *filp, ULONGN Cmd, va_list argp);\r |
f766dd76 | 178 | int EFIAPI fbadop_delete (struct __filedes *filp);\r |
179 | int EFIAPI fbadop_rmdir (struct __filedes *filp);\r | |
180 | int EFIAPI fbadop_mkdir (const char *path, __mode_t perms);\r | |
181 | int EFIAPI fbadop_rename (const char *from, const char *to);\r | |
b00771f5 | 182 | \r |
183 | __END_DECLS\r | |
184 | \r | |
185 | /* From the original file... */\r | |
186 | #if 0\r | |
187 | \r | |
188 | //struct proc;\r | |
189 | //struct lwp;\r | |
190 | //struct uio;\r | |
191 | //struct iovec;\r | |
192 | //struct knote;\r | |
193 | \r | |
194 | //LIST_HEAD(filelist, file);\r | |
195 | //extern struct filelist filehead; /* head of list of open files */\r | |
196 | //extern int maxfiles; /* kernel limit on # of open files */\r | |
197 | //extern int nfiles; /* actual number of open files */\r | |
198 | \r | |
199 | //extern const struct fileops vnops; /* vnode operations for files */\r | |
200 | \r | |
201 | struct fileops {\r | |
202 | int (*fo_read) (struct file *, off_t *, struct uio *, kauth_cred_t, int);\r | |
203 | int (*fo_write) (struct file *, off_t *, struct uio *, kauth_cred_t, int);\r | |
204 | int (*fo_ioctl) (struct file *, u_long, void *, struct lwp *);\r | |
205 | int (*fo_fcntl) (struct file *, u_int, void *, struct lwp *);\r | |
206 | int (*fo_poll) (struct file *, int, struct lwp *);\r | |
207 | int (*fo_stat) (struct file *, struct stat *, struct lwp *);\r | |
208 | int (*fo_close) (struct file *, struct lwp *);\r | |
209 | };\r | |
210 | \r | |
211 | /*\r | |
212 | * Kernel descriptor table.\r | |
213 | * One entry for each open kernel vnode and socket.\r | |
214 | */\r | |
215 | struct file {\r | |
216 | LIST_ENTRY(file) f_list; /* list of active files */\r | |
217 | void *f_data; /* descriptor data, e.g. vnode/socket */\r | |
218 | const struct fileops *f_ops;\r | |
219 | void *f_DevDesc; /* Device Descriptor pointer */\r | |
220 | EFI_FILE_HANDLE FileHandle;\r | |
221 | EFI_HANDLE Handle;\r | |
222 | off_t f_offset; /* current position in file */\r | |
223 | int f_flag; /* see fcntl.h */\r | |
224 | UINT32 f_iflags; /* internal flags; FIF_* */\r | |
225 | int f_advice; /* access pattern hint; UVM_ADV_* */\r | |
226 | int f_type; /* descriptor type */\r | |
227 | int f_usecount; /* number active users */\r | |
228 | u_int f_count; /* reference count */\r | |
229 | u_int f_msgcount; /* references from message queue */\r | |
230 | // kauth_cred_t f_cred; /* creds associated with descriptor */\r | |
231 | struct simplelock f_slock;\r | |
232 | UINT16 MyFD; /* Which FD this is. */\r | |
233 | };\r | |
234 | \r | |
235 | #ifdef DIAGNOSTIC\r | |
236 | #define FILE_USE_CHECK(fp, str) \\r | |
237 | do { \\r | |
238 | if ((fp)->f_usecount < 0) \\r | |
239 | panic(str); \\r | |
240 | } while (/* CONSTCOND */ 0)\r | |
241 | #else\r | |
242 | #define FILE_USE_CHECK(fp, str) /* nothing */\r | |
243 | #endif\r | |
244 | \r | |
245 | /*\r | |
246 | * FILE_USE() must be called with the file lock held.\r | |
247 | * (Typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'\r | |
248 | * and fd_getfile() returns the file locked)\r | |
249 | *\r | |
250 | * fp is a pointer to a __filedes structure.\r | |
251 | */\r | |
252 | #define FILE_USE(fp) \\r | |
253 | do { \\r | |
254 | (fp)->f_usecount++; \\r | |
255 | FILE_USE_CHECK((fp), "f_usecount overflow"); \\r | |
256 | simple_unlock(&(fp)->f_slock); \\r | |
257 | } while (/* CONSTCOND */ 0)\r | |
258 | \r | |
259 | #define FILE_UNUSE_WLOCK(fp, l, havelock) \\r | |
260 | do { \\r | |
261 | if (!(havelock)) \\r | |
262 | simple_lock(&(fp)->f_slock); \\r | |
263 | if ((fp)->f_iflags & FIF_WANTCLOSE) { \\r | |
264 | simple_unlock(&(fp)->f_slock); \\r | |
265 | /* Will drop usecount */ \\r | |
266 | (void) closef((fp), (l)); \\r | |
267 | break; \\r | |
268 | } else { \\r | |
269 | (fp)->f_usecount--; \\r | |
270 | FILE_USE_CHECK((fp), "f_usecount underflow"); \\r | |
271 | } \\r | |
272 | simple_unlock(&(fp)->f_slock); \\r | |
273 | } while (/* CONSTCOND */ 0)\r | |
274 | \r | |
275 | #define FILE_UNUSE(fp, l) FILE_UNUSE_WLOCK(fp, l, 0)\r | |
276 | #define FILE_UNUSE_HAVELOCK(fp, l) FILE_UNUSE_WLOCK(fp, l, 1)\r | |
277 | \r | |
278 | __BEGIN_DECLS\r | |
279 | //int dofileread (struct lwp *, int, struct file *, void *, size_t, off_t *, int, register_t *);\r | |
280 | //int dofilewrite (struct lwp *, int, struct file *, const void *, size_t, off_t *, int, register_t *);\r | |
281 | \r | |
282 | //int dofilereadv (struct lwp *, int, struct file *, const struct iovec *, int, off_t *, int, register_t *);\r | |
283 | //int dofilewritev(struct lwp *, int, struct file *, const struct iovec *, int, off_t *, int, register_t *);\r | |
284 | \r | |
285 | //int fsetown (struct proc *, pid_t *, int, const void *);\r | |
286 | //int fgetown (struct proc *, pid_t, int, void *);\r | |
287 | //void fownsignal (pid_t, int, int, int, void *);\r | |
288 | \r | |
289 | //int fdclone (struct lwp *, struct file *, int, int, const struct fileops *, void *);\r | |
290 | \r | |
291 | /* Commonly used fileops\r | |
292 | fnullop_* Does nothing and returns success.\r | |
293 | fbadop_* Does nothing and returns EPERM\r | |
294 | */\r | |
295 | //int fnullop_fcntl (struct file *, u_int, void *, struct lwp *);\r | |
296 | //int fnullop_poll (struct file *, int, struct lwp *);\r | |
297 | //int fnullop_kqfilter(struct file *, struct knote *);\r | |
298 | //int fbadop_stat (struct file *, struct stat *, struct lwp *);\r | |
299 | //int fbadop_ioctl (struct file *, u_long, void *, struct lwp *);\r | |
300 | __END_DECLS\r | |
301 | \r | |
302 | #endif\r | |
303 | \r | |
304 | #endif /* _PIF_KFILE_H_ */\r |