Merge branch 'master' into pathbased
[profile/ivi/syslinux.git] / core / include / fs.h
1 #ifndef FS_H
2 #define FS_H
3
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <com32.h>
8 #include <stdio.h>
9 #include "core.h"
10 #include "disk.h"
11
12 /*
13  * Maximum number of open files.  This is *currently* constrained by the
14  * fact that PXE needs to be able to fit all its packet buffers into a
15  * 64K segment; this should be fixed by moving the packet buffers to high
16  * memory.
17  */
18 #define MAX_OPEN_LG2    5
19 #define MAX_OPEN        (1 << MAX_OPEN_LG2)
20
21 #define FILENAME_MAX_LG2 8
22 #define FILENAME_MAX     (1 << FILENAME_MAX_LG2)
23
24 #define CURRENTDIR_MAX  FILENAME_MAX
25
26 #define BLOCK_SIZE(fs)   ((fs)->block_size)
27 #define BLOCK_SHIFT(fs)  ((fs)->block_shift)
28 #define SECTOR_SIZE(fs)  ((fs)->sector_size)
29 #define SECTOR_SHIFT(fs) ((fs)->sector_shift)
30
31 struct fs_info {
32     const struct fs_ops *fs_ops;
33     struct device *fs_dev;
34     void *fs_info;             /* The fs-specific information */
35     int sector_shift, sector_size;
36     int block_shift, block_size;
37     struct inode *cwd;                  /* Current directory */
38     char cwd_name[CURRENTDIR_MAX];      /* Current directory by name */
39 };
40
41 extern struct fs_info *this_fs;
42
43 struct dirent;                  /* Directory entry structure */
44 struct file;
45 enum fs_flags {
46     FS_NODEV   = 1 << 0,
47     FS_USEMEM  = 1 << 1,        /* If we need a malloc routine, set it */
48     FS_THISIND = 1 << 2,        /* Set cwd based on config file location */
49 };
50
51 struct fs_ops {
52     /* in fact, we use fs_ops structure to find the right fs */
53     const char *fs_name;
54     enum fs_flags fs_flags;
55     
56     int      (*fs_init)(struct fs_info *);
57     void     (*searchdir)(const char *, struct file *);
58     uint32_t (*getfssec)(struct file *, char *, int, bool *);
59     void     (*close_file)(struct file *);
60     void     (*mangle_name)(char *, const char *);
61     char *   (*unmangle_name)(char *, const char *);
62     size_t   (*realpath)(struct fs_info *, char *, const char *, size_t);
63     int      (*chdir)(struct fs_info *, const char *);
64     int      (*load_config)(void);
65
66     struct inode * (*iget_root)(struct fs_info *);
67     struct inode * (*iget)(char *, struct inode *);
68     char * (*follow_symlink)(struct inode *, const char *);
69
70     /* the _dir_ stuff */
71     struct dirent * (*readdir)(struct file *);
72 };
73
74 enum inode_mode {I_FILE, I_DIR, I_SYMLINK};
75
76 /* 
77  * The inode structure, including the detail file information 
78  */
79 struct inode {
80     struct fs_info *fs;  /* The filesystem this inode is associated with */
81     int          mode;   /* FILE , DIR or SYMLINK */
82     uint32_t     size;
83     uint32_t     ino;    /* Inode number */
84     uint32_t     atime;  /* Access time */
85     uint32_t     mtime;  /* Modify time */
86     uint32_t     ctime;  /* Create time */
87     uint32_t     dtime;  /* Delete time */
88     int          blocks; /* How many blocks the file take */
89     uint32_t     flags;
90     uint32_t     file_acl;
91     char         pvt[0]; /* Private filesystem data */
92 };
93
94 struct open_file_t;
95
96 struct file {
97     struct fs_info *fs;
98     uint32_t file_len;
99     union {
100         /* For the new universal-path_lookup */
101         struct {
102             struct inode *inode;        /* The file-specific information */
103             uint32_t offset;            /* for next read */
104         };
105
106         /* For the old searhdir method */
107         struct {
108             struct open_file_t *open_file;/* The fs-specific open file struct */
109         };
110     };
111 };
112
113
114 enum dev_type {CHS, EDD};
115
116 /*
117  * Struct device contains:
118  *     the pointer points to the disk structure,
119  *     the cache stuff.
120  */
121 struct device {
122     struct disk *disk;
123
124     /* the cache stuff */
125     char* cache_data;
126     void* cache_head;
127     uint16_t cache_block_size;
128     uint16_t cache_entries;
129     uint32_t cache_size;
130 };
131
132 /*
133  * Our definition of "not whitespace"
134  */
135 static inline bool not_whitespace(char c)
136 {
137   return (unsigned char)c > ' ';
138 }
139
140 /*
141  * Inode allocator/deallocator
142  */
143 struct inode *alloc_inode(struct fs_info *fs, uint32_t ino, size_t data);
144 static inline void free_inode(struct inode * inode)
145 {
146     free(inode);
147 }
148
149 static inline void malloc_error(char *obj)
150 {
151         printf("Out of memory: can't allocate memory for %s\n", obj);
152         kaboom();
153 }
154
155 /*
156  * File handle conversion functions
157  */
158 extern struct file files[];
159 static inline uint16_t file_to_handle(struct file *file)
160 {
161     return file ? (file - files)+1 : 0;
162 }
163 static inline struct file *handle_to_file(uint16_t handle)
164 {
165     return handle ? &files[handle-1] : NULL;
166 }
167
168 /* fs.c */
169 void pm_mangle_name(com32sys_t *);
170 void pm_unmangle_name(com32sys_t *);
171 void pm_searchdir(com32sys_t *);
172 void mangle_name(char *, const char *);
173 char *unmangle_name(char *, const char *);
174 int searchdir(const char *name);
175 void _close_file(struct file *);
176
177 /* chdir.c */
178 void pm_realpath(com32sys_t *regs);
179 size_t realpath(char *dst, const char *src, size_t bufsize);
180 int chdir(const char *src);
181
182 /*
183  * Generic functions that filesystem drivers may choose to use
184  */
185
186 /* mangle.c */
187 void generic_mangle_name(char *, const char *);
188 #define generic_unmangle_name stpcpy
189
190 /* loadconfig.c */
191 int generic_load_config(void);
192
193 #endif /* FS_H */