]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLibPrivateInternalFiles/Include/kfile.h
Add device abstraction code for the UEFI Console and UEFI Shell-based file systems.
[mirror_edk2.git] / StdLibPrivateInternalFiles / Include / kfile.h
diff --git a/StdLibPrivateInternalFiles/Include/kfile.h b/StdLibPrivateInternalFiles/Include/kfile.h
new file mode 100644 (file)
index 0000000..845b7d9
--- /dev/null
@@ -0,0 +1,288 @@
+/** @file\r
+  The EFI kernel's interpretation of a "file".\r
+\r
+  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+ * Copyright (c) 1982, 1986, 1989, 1993\r
+ *  The Regents of the University of California.  All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ *    notice, this list of conditions and the following disclaimer in the\r
+ *    documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the University nor the names of its contributors\r
+ *    may be used to endorse or promote products derived from this software\r
+ *    without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+ * SUCH DAMAGE.\r
+ *\r
+ *  file.h  8.3 (Berkeley) 1/9/95\r
+    NetBSD: file.h,v 1.56 2006/05/14 21:38:18 elad Exp\r
+**/\r
+#ifndef _PIF_KFILE_H_\r
+#define _PIF_KFILE_H_\r
+\r
+#include  <Uefi.h>\r
+#include  <Protocol/SimpleTextOut.h>\r
+#include  <Protocol/SimpleFileSystem.h>\r
+\r
+#include  <wchar.h>\r
+#include  <sys/fcntl.h>\r
+#include  <sys/unistd.h>\r
+\r
+struct stat;\r
+struct fileops;\r
+struct _Device_Node;\r
+\r
+/* The number of "special" character stream devices.\r
+   These include:\r
+    stdin, stdout, stderr\r
+*/\r
+#define NUM_SPECIAL   3\r
+\r
+/* Organization of the f_iflags member of the __filedes structure. */\r
+#define DTYPE_MASK      0x00000007    ///< Device Type\r
+#define DTYPE_VNODE             1     /* file */\r
+#define DTYPE_SOCKET            2     /* communications endpoint */\r
+#define DTYPE_PIPE              3     /* pipe */\r
+#define DTYPE_KQUEUE            4     /* event queue */\r
+#define DTYPE_MISC              5     /* misc file descriptor type */\r
+#define DTYPE_CRYPTO            6     /* crypto */\r
+#define DTYPE_NAMES   "0", "file", "socket", "pipe", "kqueue", "misc", "crypto"\r
+\r
+#define FIF_WANTCLOSE   0x10000000  /* a close is waiting for usecount */\r
+#define FIF_DELCLOSE    0x20000000  /* Delete on close. */\r
+#define FIF_LARVAL      0x80000000  /* not fully constructed; don't use */\r
+\r
+/*\r
+    This structure must be a multiple of 8 bytes in length.\r
+*/\r
+struct __filedes {\r
+  const struct fileops   *f_ops;\r
+  void                   *devdata;      /* Device-specific data */\r
+  off_t                   f_offset;     /* current position in file */\r
+  UINT32                  f_flag;       /* see fcntl.h */\r
+  UINT32                  f_iflags;     // In use if non-zero\r
+  int                     Oflags;       // From the open call\r
+  int                     Omode;        // From the open call\r
+  int                     RefCount;     // Reference count of opens\r
+  UINT16                  MyFD;         // Which FD this is.\r
+  UINT16                  Reserved_1;   // Force this structure to be a multiple of 8-bytes in length\r
+};\r
+\r
+struct fileops {\r
+  /* These functions must always be implemented. */\r
+  int     (EFIAPI *fo_close)    (struct __filedes *filp);\r
+  ssize_t (EFIAPI *fo_read)     (struct __filedes *filp, off_t *Offset, size_t Len, void *Buf);\r
+  ssize_t (EFIAPI *fo_write)    (struct __filedes *filp, off_t *Offset, size_t Len, const void *Buf);\r
+\r
+  /* Call the fnullop_* version of these functions if not implemented by the device. */\r
+  int     (EFIAPI *fo_fcntl)    (struct __filedes *filp, UINT32 Cmd, void *p3, void *p4);\r
+  short   (EFIAPI *fo_poll)     (struct __filedes *filp, short Events);\r
+  int     (EFIAPI *fo_flush)    (struct __filedes *filp);\r
+\r
+  /* Call the fbadop_* version of these functions if not implemented by the device. */\r
+  int     (EFIAPI *fo_stat)     (struct __filedes *filp, struct stat *StatBuf, void *Buf);\r
+  int     (EFIAPI *fo_ioctl)    (struct __filedes *filp, ULONGN Cmd, void *argp);\r
+  int     (EFIAPI *fo_delete)   (struct __filedes *filp);\r
+  int     (EFIAPI *fo_rmdir)    (struct __filedes *filp);\r
+  int     (EFIAPI *fo_mkdir)    (const char *path, __mode_t perms);\r
+  int     (EFIAPI *fo_rename)   (const char *from, const char *to);\r
+\r
+  /* Use a NULL if this function has not been implemented by the device. */\r
+  off_t   (EFIAPI *fo_lseek)    (struct __filedes *filp, off_t, int);\r
+};\r
+\r
+/*  A generic instance structure which is valid for\r
+    for all device instance structures.\r
+\r
+    All device instance structures MUST be a multiple of 8-bytes in length.\r
+*/\r
+typedef struct {\r
+  UINT32                      Cookie;       ///< Special value identifying this as a valid Instance\r
+  UINT32                      InstanceNum;  ///< Which instance is this?  Zero-based.\r
+  EFI_HANDLE                  Dev;          ///< Pointer to either Input or Output Protocol.\r
+  struct _Device_Node        *Parent;       ///< Points to the parent Device Node.\r
+  struct fileops              Abstraction;  ///< Pointers to functions implementing this device's abstraction.\r
+  UINTN                       Reserved_1;   // Force this to always be a multiple of 8-bytes in length\r
+} GenericInstance;\r
+\r
+/* Type of all Device-specific handler's open routines. */\r
+typedef\r
+  int     (EFIAPI *FO_OPEN)    (struct __filedes *FD, void *IP, wchar_t *Path, wchar_t *Flags);\r
+\r
+#define FILE_IS_USABLE(fp)  (((fp)->f_iflags &      \\r
+          (FIF_WANTCLOSE|FIF_LARVAL)) == 0)\r
+\r
+#define FILE_SET_MATURE(fp)       \\r
+do {                              \\r
+  (fp)->f_iflags &= ~FIF_LARVAL;  \\r
+} while (/*CONSTCOND*/0)\r
+\r
+/*\r
+ * Flags for fo_read and fo_write.\r
+ */\r
+#define FOF_UPDATE_OFFSET 0x01      /* update the file offset */\r
+\r
+__BEGIN_DECLS\r
+\r
+int   fdcreate    (CHAR16 *, UINT32, UINT32, BOOLEAN, VOID *, const struct fileops *);\r
+\r
+/* Commonly used fileops\r
+      fnullop_*   Does nothing and returns success.\r
+      fbadop_*    Does nothing and returns EPERM\r
+*/\r
+int     fnullop_fcntl (struct __filedes *filp, UINT32 Cmd, void *p3, void *p4);\r
+short   fnullop_poll  (struct __filedes *filp, short Events);\r
+int     fnullop_flush (struct __filedes *filp);\r
+\r
+int     fbadop_stat   (struct __filedes *filp, struct stat *StatBuf, void *Buf);\r
+int     fbadop_ioctl  (struct __filedes *filp, ULONGN Cmd, void *argp);\r
+int     fbadop_delete (struct __filedes *filp);\r
+int     fbadop_rmdir  (struct __filedes *filp);\r
+int     fbadop_mkdir  (const char *path, __mode_t perms);\r
+int     fbadop_rename (const char *from, const char *to);\r
+\r
+__END_DECLS\r
+\r
+/* From the original file... */\r
+#if 0\r
+\r
+//struct proc;\r
+//struct lwp;\r
+//struct uio;\r
+//struct iovec;\r
+//struct knote;\r
+\r
+//LIST_HEAD(filelist, file);\r
+//extern struct filelist  filehead;   /* head of list of open files */\r
+//extern int              maxfiles;   /* kernel limit on # of open files */\r
+//extern int              nfiles;     /* actual number of open files */\r
+\r
+//extern const struct fileops vnops;  /* vnode operations for files */\r
+\r
+struct fileops {\r
+  int (*fo_read)      (struct file *, off_t *, struct uio *, kauth_cred_t, int);\r
+  int (*fo_write)     (struct file *, off_t *, struct uio *, kauth_cred_t, int);\r
+  int (*fo_ioctl)     (struct file *, u_long, void *, struct lwp *);\r
+  int (*fo_fcntl)     (struct file *, u_int, void *, struct lwp *);\r
+  int (*fo_poll)      (struct file *, int, struct lwp *);\r
+  int (*fo_stat)      (struct file *, struct stat *, struct lwp *);\r
+  int (*fo_close)     (struct file *, struct lwp *);\r
+};\r
+\r
+/*\r
+ * Kernel descriptor table.\r
+ * One entry for each open kernel vnode and socket.\r
+ */\r
+struct file {\r
+  LIST_ENTRY(file)        f_list;     /* list of active files */\r
+  void                   *f_data;     /* descriptor data, e.g. vnode/socket */\r
+  const struct fileops   *f_ops;\r
+  void                   *f_DevDesc;  /* Device Descriptor pointer */\r
+  EFI_FILE_HANDLE         FileHandle;\r
+  EFI_HANDLE              Handle;\r
+  off_t                   f_offset;   /* current position in file */\r
+  int                     f_flag;     /* see fcntl.h */\r
+  UINT32                  f_iflags;   /* internal flags; FIF_* */\r
+  int                     f_advice;   /* access pattern hint; UVM_ADV_* */\r
+  int                     f_type;     /* descriptor type */\r
+  int                     f_usecount; /* number active users */\r
+  u_int                   f_count;    /* reference count */\r
+  u_int                   f_msgcount; /* references from message queue */\r
+//  kauth_cred_t            f_cred;     /* creds associated with descriptor */\r
+  struct simplelock       f_slock;\r
+  UINT16                  MyFD;       /* Which FD this is. */\r
+};\r
+\r
+#ifdef DIAGNOSTIC\r
+#define FILE_USE_CHECK(fp, str)   \\r
+  do {                              \\r
+  if ((fp)->f_usecount < 0)       \\r
+    panic(str);                   \\r
+} while (/* CONSTCOND */ 0)\r
+#else\r
+#define FILE_USE_CHECK(fp, str)   /* nothing */\r
+#endif\r
+\r
+  /*\r
+   * FILE_USE() must be called with the file lock held.\r
+   * (Typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'\r
+   * and fd_getfile() returns the file locked)\r
+   *\r
+   * fp is a pointer to a __filedes structure.\r
+   */\r
+#define FILE_USE(fp)                                \\r
+    do {                                                \\r
+    (fp)->f_usecount++;                               \\r
+    FILE_USE_CHECK((fp), "f_usecount overflow");      \\r
+    simple_unlock(&(fp)->f_slock);                    \\r
+  } while (/* CONSTCOND */ 0)\r
+\r
+#define FILE_UNUSE_WLOCK(fp, l, havelock)           \\r
+      do {                                                \\r
+      if (!(havelock))                                  \\r
+        simple_lock(&(fp)->f_slock);                    \\r
+        if ((fp)->f_iflags & FIF_WANTCLOSE) {             \\r
+        simple_unlock(&(fp)->f_slock);                  \\r
+        /* Will drop usecount */                        \\r
+        (void) closef((fp), (l));                       \\r
+        break;                                          \\r
+        } else {                                          \\r
+        (fp)->f_usecount--;                             \\r
+        FILE_USE_CHECK((fp), "f_usecount underflow");   \\r
+      }                                                 \\r
+      simple_unlock(&(fp)->f_slock);                    \\r
+    } while (/* CONSTCOND */ 0)\r
+\r
+#define FILE_UNUSE(fp, l)           FILE_UNUSE_WLOCK(fp, l, 0)\r
+#define FILE_UNUSE_HAVELOCK(fp, l)  FILE_UNUSE_WLOCK(fp, l, 1)\r
+\r
+__BEGIN_DECLS\r
+//int   dofileread  (struct lwp *, int, struct file *, void *, size_t, off_t *, int, register_t *);\r
+//int   dofilewrite (struct lwp *, int, struct file *, const void *, size_t, off_t *, int, register_t *);\r
+\r
+//int   dofilereadv (struct lwp *, int, struct file *, const struct iovec *, int, off_t *, int, register_t *);\r
+//int   dofilewritev(struct lwp *, int, struct file *, const struct iovec *, int, off_t *, int, register_t *);\r
+\r
+//int   fsetown     (struct proc *, pid_t *, int, const void *);\r
+//int   fgetown     (struct proc *, pid_t, int, void *);\r
+//void  fownsignal  (pid_t, int, int, int, void *);\r
+\r
+//int   fdclone     (struct lwp *, struct file *, int, int, const struct fileops *, void *);\r
+\r
+/* Commonly used fileops\r
+      fnullop_*   Does nothing and returns success.\r
+      fbadop_*    Does nothing and returns EPERM\r
+*/\r
+//int   fnullop_fcntl   (struct file *, u_int, void *, struct lwp *);\r
+//int   fnullop_poll    (struct file *, int, struct lwp *);\r
+//int   fnullop_kqfilter(struct file *, struct knote *);\r
+//int   fbadop_stat     (struct file *, struct stat *, struct lwp *);\r
+//int   fbadop_ioctl    (struct file *, u_long, void *, struct lwp *);\r
+__END_DECLS\r
+\r
+#endif\r
+\r
+#endif /* _PIF_KFILE_H_ */\r