scoutos / prex-0.9.0 / usr / server / fs / fatfs / fatfs.h @ 03e9c04a
History | View | Annotate | Download (6.49 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2005-2007, Kohsuke Ohtani
|
3 |
* All rights reserved.
|
4 |
*
|
5 |
* Redistribution and use in source and binary forms, with or without
|
6 |
* modification, are permitted provided that the following conditions
|
7 |
* are met:
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
9 |
* notice, this list of conditions and the following disclaimer.
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
12 |
* documentation and/or other materials provided with the distribution.
|
13 |
* 3. Neither the name of the author nor the names of any co-contributors
|
14 |
* may be used to endorse or promote products derived from this software
|
15 |
* without specific prior written permission.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 |
* SUCH DAMAGE.
|
28 |
*/
|
29 |
|
30 |
#ifndef _FATFS_H
|
31 |
#define _FATFS_H
|
32 |
|
33 |
#include <sys/prex.h> |
34 |
#include <sys/cdefs.h> |
35 |
#include <sys/types.h> |
36 |
#include <sys/vnode.h> |
37 |
#include <sys/file.h> |
38 |
#include <sys/mount.h> |
39 |
#include <sys/syslog.h> |
40 |
#include <sys/buf.h> |
41 |
|
42 |
/* #define DEBUG_FATFS 1 */
|
43 |
|
44 |
#ifdef DEBUG_FATFS
|
45 |
#define DPRINTF(a) dprintf a
|
46 |
#define ASSERT(e) dassert(e)
|
47 |
#else
|
48 |
#define DPRINTF(a) do {} while (0) |
49 |
#define ASSERT(e)
|
50 |
#endif
|
51 |
|
52 |
|
53 |
#if CONFIG_FS_THREADS > 1 |
54 |
#define malloc(s) malloc_r(s)
|
55 |
#define free(p) free_r(p)
|
56 |
#else
|
57 |
#define mutex_init(m) do {} while (0) |
58 |
#define mutex_destroy(m) do {} while (0) |
59 |
#define mutex_lock(m) do {} while (0) |
60 |
#define mutex_unlock(m) do {} while (0) |
61 |
#define mutex_trylock(m) do {} while (0) |
62 |
#endif
|
63 |
|
64 |
|
65 |
#define SEC_SIZE 512 /* sector size */ |
66 |
#define SEC_INVAL 0xffffffff /* invalid sector */ |
67 |
|
68 |
/*
|
69 |
* Pre-defined cluster number
|
70 |
*/
|
71 |
#define CL_ROOT 0 /* cluster 0 means the root directory */ |
72 |
#define CL_FREE 0 /* cluster 0 also means the free cluster */ |
73 |
#define CL_FIRST 2 /* first legal cluster */ |
74 |
#define CL_LAST 0xfffffff5 /* last legal cluster */ |
75 |
#define CL_EOF 0xffffffff /* EOF cluster */ |
76 |
|
77 |
#define EOF_MASK 0xfffffff8 /* mask of eof */ |
78 |
|
79 |
#define FAT12_MASK 0x00000fff |
80 |
#define FAT16_MASK 0x0000ffff |
81 |
|
82 |
#if defined(__SUNPRO_C)
|
83 |
#pragma pack(1) |
84 |
#endif
|
85 |
|
86 |
/*
|
87 |
* BIOS parameter block
|
88 |
*/
|
89 |
struct fat_bpb {
|
90 |
uint16_t jmp_instruction; |
91 |
uint8_t nop_instruction; |
92 |
uint8_t oem_id[8];
|
93 |
uint16_t bytes_per_sector; |
94 |
uint8_t sectors_per_cluster; |
95 |
uint16_t reserved_sectors; |
96 |
uint8_t num_of_fats; |
97 |
uint16_t root_entries; |
98 |
uint16_t total_sectors; |
99 |
uint8_t media_descriptor; |
100 |
uint16_t sectors_per_fat; |
101 |
uint16_t sectors_per_track; |
102 |
uint16_t heads; |
103 |
uint32_t hidden_sectors; |
104 |
uint32_t big_total_sectors; |
105 |
uint8_t physical_drive; |
106 |
uint8_t reserved; |
107 |
uint8_t ext_boot_signature; |
108 |
uint32_t serial_no; |
109 |
uint8_t volume_id[11];
|
110 |
uint8_t file_sys_id[8];
|
111 |
} __packed; |
112 |
|
113 |
/*
|
114 |
* FAT directory entry
|
115 |
*/
|
116 |
struct fat_dirent {
|
117 |
uint8_t name[11];
|
118 |
uint8_t attr; |
119 |
uint8_t reserve[10];
|
120 |
uint16_t time; |
121 |
uint16_t date; |
122 |
uint16_t cluster; |
123 |
uint32_t size; |
124 |
} __packed; |
125 |
|
126 |
#if defined(__SUNPRO_C)
|
127 |
#pragma pack()
|
128 |
#endif
|
129 |
|
130 |
#define SLOT_EMPTY 0x00 |
131 |
#define SLOT_DELETED 0xe5 |
132 |
|
133 |
#define DIR_PER_SEC (SEC_SIZE / sizeof(struct fat_dirent)) |
134 |
|
135 |
/*
|
136 |
* FAT attribute for attr
|
137 |
*/
|
138 |
#define FA_RDONLY 0x01 |
139 |
#define FA_HIDDEN 0x02 |
140 |
#define FA_SYSTEM 0x04 |
141 |
#define FA_VOLID 0x08 |
142 |
#define FA_SUBDIR 0x10 |
143 |
#define FA_ARCH 0x20 |
144 |
#define FA_DEVICE 0x40 |
145 |
|
146 |
#define IS_DIR(de) (((de)->attr) & FA_SUBDIR)
|
147 |
#define IS_VOL(de) (((de)->attr) & FA_VOLID)
|
148 |
#define IS_FILE(de) (!IS_DIR(de) && !IS_VOL(de))
|
149 |
|
150 |
#define IS_DELETED(de) ((de)->name[0] == 0xe5) |
151 |
#define IS_EMPTY(de) ((de)->name[0] == 0) |
152 |
|
153 |
/*
|
154 |
* Mount data
|
155 |
*/
|
156 |
struct fatfsmount {
|
157 |
int fat_type; /* 12 or 16 */ |
158 |
u_long root_start; /* start sector for root directory */
|
159 |
u_long fat_start; /* start sector for fat entries */
|
160 |
u_long data_start; /* start sector for data */
|
161 |
u_long fat_eof; /* id of end cluster */
|
162 |
u_long sec_per_cl; /* sectors per cluster */
|
163 |
u_long cluster_size; /* cluster size */
|
164 |
u_long last_cluster; /* last cluser */
|
165 |
u_long fat_mask; /* mask for cluster# */
|
166 |
u_long free_scan; /* start cluster# to free search */
|
167 |
vnode_t root_vnode; /* vnode for root */
|
168 |
char *io_buf; /* local data buffer */ |
169 |
char *fat_buf; /* buffer for fat entry */ |
170 |
char *dir_buf; /* buffer for directory entry */ |
171 |
dev_t dev; /* mounted device */
|
172 |
#if CONFIG_FS_THREADS > 1 |
173 |
mutex_t lock; /* file system lock */
|
174 |
#endif
|
175 |
}; |
176 |
|
177 |
#define FAT12(fat) ((fat)->fat_type == 12) |
178 |
#define FAT16(fat) ((fat)->fat_type == 16) |
179 |
|
180 |
#define IS_EOFCL(fat, cl) \
|
181 |
(((cl) & EOF_MASK) == ((fat)->fat_mask & EOF_MASK)) |
182 |
|
183 |
/*
|
184 |
* File/directory node
|
185 |
*/
|
186 |
struct fatfs_node {
|
187 |
struct fat_dirent dirent; /* copy of directory entry */ |
188 |
u_long sector; /* sector# for directory entry */
|
189 |
u_long offset; /* offset of directory entry in sector */
|
190 |
}; |
191 |
|
192 |
extern struct vnops fatfs_vnops; |
193 |
|
194 |
/* Macro to convert cluster# to logical sector# */
|
195 |
#define cl_to_sec(fat, cl) \
|
196 |
(fat->data_start + (cl - 2) * fat->sec_per_cl)
|
197 |
|
198 |
__BEGIN_DECLS |
199 |
int fat_next_cluster(struct fatfsmount *fmp, u_long cl, u_long *next); |
200 |
int fat_set_cluster(struct fatfsmount *fmp, u_long cl, u_long next); |
201 |
int fat_alloc_cluster(struct fatfsmount *fmp, u_long scan_start, u_long *free); |
202 |
int fat_free_clusters(struct fatfsmount *fmp, u_long start); |
203 |
int fat_seek_cluster(struct fatfsmount *fmp, u_long start, u_long offset, |
204 |
u_long *cl); |
205 |
int fat_expand_file(struct fatfsmount *fmp, u_long cl, int size); |
206 |
int fat_expand_dir(struct fatfsmount *fmp, u_long cl, u_long *new_cl); |
207 |
|
208 |
void fat_convert_name(char *org, char *name); |
209 |
void fat_restore_name(char *org, char *name); |
210 |
int fat_valid_name(char *name); |
211 |
int fat_compare_name(char *n1, char *n2); |
212 |
void fat_mode_to_attr(mode_t mode, u_char *attr);
|
213 |
void fat_attr_to_mode(u_char attr, mode_t *mode);
|
214 |
|
215 |
int fatfs_lookup_node(vnode_t dvp, char *name, struct fatfs_node *node); |
216 |
int fatfs_get_node(vnode_t dvp, int index, struct fatfs_node *node); |
217 |
int fatfs_put_node(struct fatfsmount *fmp, struct fatfs_node *node); |
218 |
int fatfs_add_node(vnode_t dvp, struct fatfs_node *node); |
219 |
__END_DECLS |
220 |
|
221 |
#endif /* !_FATFS_H */ |