fs: fix cwd setting for FAT/iso9660/extfs
[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 BLOCK_SIZE(fs)   ((fs)->block_size)
25 #define BLOCK_SHIFT(fs)  ((fs)->block_shift)
26 #define SECTOR_SIZE(fs)  ((fs)->sector_size)
27 #define SECTOR_SHIFT(fs) ((fs)->sector_shift)
28
29 struct fs_info {
30     const struct fs_ops *fs_ops;
31     struct device *fs_dev;
32     void *fs_info;             /* The fs-specific information */
33     int sector_shift, sector_size;
34     int block_shift, block_size;
35 };
36
37 extern struct fs_info *this_fs;
38
39 struct dirent;                  /* Directory entry structure */
40 struct file;
41 enum fs_flags {
42     FS_NODEV   = 1 << 0,
43     FS_USEMEM  = 1 << 1,        /* If we need a malloc routine, set it */
44     FS_THISIND = 1 << 2,        /* Set cwd based on config file location */
45 };
46
47 struct fs_ops {
48     /* in fact, we use fs_ops structure to find the right fs */
49     const char *fs_name;
50     enum fs_flags fs_flags;
51     
52     int      (*fs_init)(struct fs_info *);
53     void     (*searchdir)(char *, struct file *);
54     uint32_t (*getfssec)(struct file *, char *, int, bool *);
55     void     (*close_file)(struct file *);
56     void     (*mangle_name)(char *, const char *);
57     char *   (*unmangle_name)(char *, const char *);
58     int      (*load_config)(void);
59
60     struct inode * (*iget_root)(struct fs_info *);
61     struct inode * (*iget_current)(struct fs_info *);
62     struct inode * (*iget)(char *, struct inode *);
63     char * (*follow_symlink)(struct inode *, const char *);
64
65     /* the _dir_ stuff */
66     struct dirent * (*readdir)(struct file *);
67 };
68
69 enum inode_mode {I_FILE, I_DIR, I_SYMLINK};
70
71 /* 
72  * The inode structure, including the detail file information 
73  */
74 struct inode {
75     struct fs_info *fs;  /* The filesystem this inode is associated with */
76     int          mode;   /* FILE , DIR or SYMLINK */
77     uint32_t     size;
78     uint32_t     ino;    /* Inode number */
79     uint32_t     atime;  /* Access time */
80     uint32_t     mtime;  /* Modify time */
81     uint32_t     ctime;  /* Create time */
82     uint32_t     dtime;  /* Delete time */
83     int          blocks; /* How many blocks the file take */
84     uint32_t     flags;
85     uint32_t     file_acl;
86     char         pvt[0]; /* Private filesystem data */
87 };
88
89 struct open_file_t;
90
91 struct file {
92     struct fs_info *fs;
93     union {
94         /* For the new universal-path_lookup */
95         struct {
96             struct inode *inode;        /* The file-specific information */
97             uint32_t offset;            /* for next read */
98         };
99
100         /* For the old searhdir method */
101         struct {
102             struct open_file_t *open_file;/* The fs-specific open file struct */
103             uint32_t file_len;
104         };
105     };
106 };
107
108
109 enum dev_type {CHS, EDD};
110
111 /*
112  * Generic functions that filesystem drivers may choose to use
113  */
114 void generic_mangle_name(char *, const char *);
115 #define generic_unmangle_name stpcpy
116
117 /*
118  * Struct device contains:
119  *     the pointer points to the disk structure,
120  *     the cache stuff.
121  */
122 struct device {
123     struct disk *disk;
124
125     /* the cache stuff */
126     char* cache_data;
127     void* cache_head;
128     uint16_t cache_block_size;
129     uint16_t cache_entries;
130     uint32_t cache_size;
131 };
132
133 /*
134  * Our definition of "not whitespace"
135  */
136 static inline bool not_whitespace(char c)
137 {
138   return (unsigned char)c > ' ';
139 }
140
141 /*
142  * Inode allocator/deallocator
143  */
144 struct inode *alloc_inode(struct fs_info *fs, uint32_t ino, size_t data);
145 static inline void free_inode(struct inode * inode)
146 {
147     free(inode);
148 }
149
150 static inline void malloc_error(char *obj)
151 {
152         printf("Out of memory: can't allocate memory for %s\n", obj);
153         kaboom();
154 }
155
156 /* 
157  * functions
158  */
159 void mangle_name(com32sys_t *);
160 void searchdir(com32sys_t *);
161 void _close_file(struct file *);
162 inline uint16_t file_to_handle(struct file *);
163 inline struct file *handle_to_file(uint16_t);
164
165 #endif /* FS_H */