2 * Copyright (c) 1996, 1998 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/lib/libstand/dosfs.c,v 1.4.2.1 2000/05/04 13:47:49 ps Exp $
28 * $DragonFly: src/lib/libstand/dosfs.c,v 1.2 2003/06/17 04:26:51 dillon Exp $
32 * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
36 #include <sys/types.h>
45 static int dos_open(const char *path
, struct open_file
*fd
);
46 static int dos_close(struct open_file
*fd
);
47 static int dos_read(struct open_file
*fd
, void *buf
, size_t size
, size_t *resid
);
48 static off_t
dos_seek(struct open_file
*fd
, off_t offset
, int whence
);
49 static int dos_stat(struct open_file
*fd
, struct stat
*sb
);
51 struct fs_ops dosfs_fsops
= {
62 #define SECSIZ 512 /* sector size */
63 #define SSHIFT 9 /* SECSIZ shift */
64 #define DEPSEC 16 /* directory entries per sector */
65 #define DSHIFT 4 /* DEPSEC shift */
66 #define LOCLUS 2 /* lowest cluster number */
68 /* DOS "BIOS Parameter Block" */
70 u_char secsiz
[2]; /* sector size */
71 u_char spc
; /* sectors per cluster */
72 u_char ressec
[2]; /* reserved sectors */
73 u_char fats
; /* FATs */
74 u_char dirents
[2]; /* root directory entries */
75 u_char secs
[2]; /* total sectors */
76 u_char media
; /* media descriptor */
77 u_char spf
[2]; /* sectors per FAT */
78 u_char spt
[2]; /* sectors per track */
79 u_char heads
[2]; /* drive heads */
80 u_char hidsec
[4]; /* hidden sectors */
81 u_char lsecs
[4]; /* huge sectors */
82 u_char lspf
[4]; /* huge sectors per FAT */
83 u_char xflg
[2]; /* flags */
84 u_char vers
[2]; /* filesystem version */
85 u_char rdcl
[4]; /* root directory start cluster */
86 u_char infs
[2]; /* filesystem info sector */
87 u_char bkbs
[2]; /* backup boot sector */
90 /* Initial portion of DOS boot sector */
92 u_char jmp
[3]; /* usually 80x86 'jmp' opcode */
93 u_char oem
[8]; /* OEM name and version */
94 DOS_BPB bpb
; /* BPB */
97 /* Supply missing "." and ".." root directory entries */
98 static const char *const dotstr
[2] = {".", ".."};
99 static DOS_DE dot
[2] = {
100 {". ", " ", FA_DIR
, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
101 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
102 {".. ", " ", FA_DIR
, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
103 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
106 /* The usual conversion macros to avoid multiplication and division */
107 #define bytsec(n) ((n) >> SSHIFT)
108 #define secbyt(s) ((s) << SSHIFT)
109 #define entsec(e) ((e) >> DSHIFT)
110 #define bytblk(fs, n) ((n) >> (fs)->bshift)
111 #define blkbyt(fs, b) ((b) << (fs)->bshift)
112 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
113 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
115 /* Convert cluster number to offset within filesystem */
116 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
118 /* Convert cluster number to logical sector number */
119 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
121 /* Convert cluster number to offset within FAT */
122 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
123 (sz) == 16 ? (c) << 1 : \
126 /* Does cluster number reference a valid data cluster? */
127 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
129 /* Get start cluster from directory entry */
130 #define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \
131 ((u_int)cv2((de)->dex.h_clus) << 16) | \
134 static int dosunmount(DOS_FS
*);
135 static int parsebs(DOS_FS
*, DOS_BS
*);
136 static int namede(DOS_FS
*, const char *, DOS_DE
**);
137 static int lookup(DOS_FS
*, u_int
, const char *, DOS_DE
**);
138 static void cp_xdnm(u_char
*, DOS_XDE
*);
139 static void cp_sfn(u_char
*, DOS_DE
*);
140 static off_t
fsize(DOS_FS
*, DOS_DE
*);
141 static int fatcnt(DOS_FS
*, u_int
);
142 static int fatget(DOS_FS
*, u_int
*);
143 static int fatend(u_int
, u_int
);
144 static int ioread(DOS_FS
*, u_int
, void *, u_int
);
145 static int iobuf(DOS_FS
*, u_int
);
146 static int ioget(struct open_file
*, u_int
, void *, u_int
);
149 * Mount DOS filesystem
152 dos_mount(DOS_FS
*fs
, struct open_file
*fd
)
156 bzero(fs
, sizeof(DOS_FS
));
158 if ((err
= !(fs
->buf
= malloc(SECSIZ
)) ? errno
: 0) ||
159 (err
= ioget(fs
->fd
, 0, fs
->buf
, 1)) ||
160 (err
= parsebs(fs
, (DOS_BS
*)fs
->buf
))) {
161 (void)dosunmount(fs
);
168 * Unmount mounted filesystem
171 dos_unmount(DOS_FS
*fs
)
177 if ((err
= dosunmount(fs
)))
183 * Common code shared by dos_mount() and dos_unmount()
186 dosunmount(DOS_FS
*fs
)
198 dos_open(const char *path
, struct open_file
*fd
)
206 /* Allocate mount structure, associate with open */
207 fs
= malloc(sizeof(DOS_FS
));
209 if ((err
= dos_mount(fs
, fd
)))
212 if ((err
= namede(fs
, path
, &de
)))
215 clus
= stclus(fs
->fatsz
, de
);
216 size
= cv4(de
->size
);
218 if ((!(de
->attr
& FA_DIR
) && (!clus
!= !size
)) ||
219 ((de
->attr
& FA_DIR
) && size
) ||
220 (clus
&& !okclus(fs
, clus
))) {
224 f
= malloc(sizeof(DOS_FILE
));
225 bzero(f
, sizeof(DOS_FILE
));
229 fd
->f_fsdata
= (void *)f
;
239 dos_read(struct open_file
*fd
, void *buf
, size_t nbyte
, size_t *resid
)
242 u_int nb
, off
, clus
, c
, cnt
, n
;
243 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
247 if ((size
= fsize(f
->fs
, &f
->de
)) == -1)
249 if (nb
> (n
= size
- f
->offset
))
252 if ((clus
= stclus(f
->fs
->fatsz
, &f
->de
)))
253 off
&= f
->fs
->bsize
- 1;
260 n
= bytblk(f
->fs
, f
->offset
);
264 if ((err
= fatget(f
->fs
, &c
)))
266 if (!okclus(f
->fs
, c
)) {
271 if (!clus
|| (n
= f
->fs
->bsize
- off
) > cnt
)
273 if ((err
= ioread(f
->fs
, (c
? blkoff(f
->fs
, c
) :
274 secbyt(f
->fs
->lsndir
)) + off
,
285 *resid
= nbyte
- nb
+ cnt
;
290 * Reposition within file
293 dos_seek(struct open_file
*fd
, off_t offset
, int whence
)
297 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
299 size
= cv4(f
->de
.size
);
314 if (off
< 0 || off
> size
)
316 f
->offset
= (u_int
)off
;
325 dos_close(struct open_file
*fd
)
327 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
342 * Return some stat information on a file.
345 dos_stat(struct open_file
*fd
, struct stat
*sb
)
347 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
349 /* only important stuff */
350 sb
->st_mode
= f
->de
.attr
& FA_DIR
? S_IFDIR
| 0555 : S_IFREG
| 0444;
354 if ((sb
->st_size
= fsize(f
->fs
, &f
->de
)) == -1)
360 * Parse DOS boot sector
363 parsebs(DOS_FS
*fs
, DOS_BS
*bs
)
367 if ((bs
->jmp
[0] != 0x69 &&
368 bs
->jmp
[0] != 0xe9 &&
369 (bs
->jmp
[0] != 0xeb || bs
->jmp
[2] != 0x90)) ||
370 bs
->bpb
.media
< 0xf0)
372 if (cv2(bs
->bpb
.secsiz
) != SECSIZ
)
374 if (!(fs
->spc
= bs
->bpb
.spc
) || fs
->spc
& (fs
->spc
- 1))
376 fs
->bsize
= secbyt(fs
->spc
);
377 fs
->bshift
= ffs(fs
->bsize
) - 1;
378 if ((fs
->spf
= cv2(bs
->bpb
.spf
))) {
379 if (bs
->bpb
.fats
!= 2)
381 if (!(fs
->dirents
= cv2(bs
->bpb
.dirents
)))
384 if (!(fs
->spf
= cv4(bs
->bpb
.lspf
)))
386 if (!bs
->bpb
.fats
|| bs
->bpb
.fats
> 16)
388 if ((fs
->rdcl
= cv4(bs
->bpb
.rdcl
)) < LOCLUS
)
391 if (!(fs
->lsnfat
= cv2(bs
->bpb
.ressec
)))
393 fs
->lsndir
= fs
->lsnfat
+ fs
->spf
* bs
->bpb
.fats
;
394 fs
->lsndta
= fs
->lsndir
+ entsec(fs
->dirents
);
395 if (!(sc
= cv2(bs
->bpb
.secs
)) && !(sc
= cv4(bs
->bpb
.lsecs
)))
399 if ((fs
->xclus
= secblk(fs
, sc
- fs
->lsndta
) + 1) < LOCLUS
)
401 fs
->fatsz
= fs
->dirents
? fs
->xclus
< 0xff6 ? 12 : 16 : 32;
402 sc
= (secbyt(fs
->spf
) << 1) / (fs
->fatsz
>> 2) - 1;
409 * Return directory entry from path
412 namede(DOS_FS
*fs
, const char *path
, DOS_DE
**dep
)
425 if (!(s
= strchr(path
, '/')))
427 if ((n
= s
- path
) > 255)
429 memcpy(name
, path
, n
);
432 if (!(de
->attr
& FA_DIR
))
434 if ((err
= lookup(fs
, stclus(fs
->fatsz
, de
), name
, &de
)))
444 * Lookup path segment
447 lookup(DOS_FS
*fs
, u_int clus
, const char *name
, DOS_DE
**dep
)
449 static DOS_DIR dir
[DEPSEC
];
452 u_int nsec
, lsec
, xdn
, chk
, sec
, ent
, x
;
456 for (ent
= 0; ent
< 2; ent
++)
457 if (!strcasecmp(name
, dotstr
[ent
])) {
461 if (!clus
&& fs
->fatsz
== 32)
463 nsec
= !clus
? entsec(fs
->dirents
) : fs
->spc
;
469 else if (okclus(fs
, clus
))
470 lsec
= blklsn(fs
, clus
);
473 for (sec
= 0; sec
< nsec
; sec
++) {
474 if ((err
= ioget(fs
->fd
, lsec
+ sec
, dir
, 1)))
476 for (ent
= 0; ent
< DEPSEC
; ent
++) {
477 if (!*dir
[ent
].de
.name
)
479 if (*dir
[ent
].de
.name
!= 0xe5) {
480 if ((dir
[ent
].de
.attr
& FA_MASK
) == FA_XDE
) {
481 x
= dir
[ent
].xde
.seq
;
482 if (x
& 0x40 || (x
+ 1 == xdn
&&
483 dir
[ent
].xde
.chk
== chk
)) {
485 chk
= dir
[ent
].xde
.chk
;
488 if (x
>= 1 && x
<= 20) {
489 cp_xdnm(lfn
, &dir
[ent
].xde
);
494 } else if (!(dir
[ent
].de
.attr
& FA_LABEL
)) {
495 if ((ok
= xdn
== 1)) {
496 for (x
= 0, i
= 0; i
< 11; i
++)
497 x
= ((((x
& 1) << 7) | (x
>> 1)) +
498 dir
[ent
].de
.name
[i
]) & 0xff;
500 !strcasecmp(name
, (const char *)lfn
);
503 cp_sfn(sfn
, &dir
[ent
].de
);
504 ok
= !strcasecmp(name
, (const char *)sfn
);
517 if ((err
= fatget(fs
, &clus
)))
519 if (fatend(fs
->fatsz
, clus
))
526 * Copy name from extended directory entry
529 cp_xdnm(u_char
*lfn
, DOS_XDE
*xde
)
535 {offsetof(DOS_XDE
, name1
), sizeof(xde
->name1
) / 2},
536 {offsetof(DOS_XDE
, name2
), sizeof(xde
->name2
) / 2},
537 {offsetof(DOS_XDE
, name3
), sizeof(xde
->name3
) / 2}
542 lfn
+= 13 * ((xde
->seq
& ~0x40) - 1);
543 for (n
= 0; n
< 3; n
++)
544 for (p
= (u_char
*)xde
+ ix
[n
].off
, x
= ix
[n
].dim
; x
;
546 if ((c
= cv2(p
)) && (c
< 32 || c
> 127))
556 * Copy short filename
559 cp_sfn(u_char
*sfn
, DOS_DE
*de
)
565 if (*de
->name
!= ' ') {
566 for (j
= 7; de
->name
[j
] == ' '; j
--);
567 for (i
= 0; i
<= j
; i
++)
569 if (*de
->ext
!= ' ') {
571 for (j
= 2; de
->ext
[j
] == ' '; j
--);
572 for (i
= 0; i
<= j
; i
++)
582 * Return size of file in bytes
585 fsize(DOS_FS
*fs
, DOS_DE
*de
)
591 if (!(size
= cv4(de
->size
)) && de
->attr
& FA_DIR
) {
592 if (!(c
= cv2(de
->clus
)))
593 size
= fs
->dirents
* sizeof(DOS_DE
);
595 if ((n
= fatcnt(fs
, c
)) == -1)
597 size
= blkbyt(fs
, n
);
604 * Count number of clusters in chain
607 fatcnt(DOS_FS
*fs
, u_int c
)
611 for (n
= 0; okclus(fs
, c
); n
++)
614 return fatend(fs
->fatsz
, c
) ? n
: -1;
618 * Get next cluster in cluster chain
621 fatget(DOS_FS
*fs
, u_int
*c
)
627 err
= ioread(fs
, secbyt(fs
->lsnfat
) + fatoff(fs
->fatsz
, *c
), buf
,
628 fs
->fatsz
!= 32 ? 2 : 4);
631 x
= fs
->fatsz
!= 32 ? cv2(buf
) : cv4(buf
);
632 *c
= fs
->fatsz
== 12 ? *c
& 1 ? x
>> 4 : x
& 0xfff : x
;
637 * Is cluster an end-of-chain marker?
640 fatend(u_int sz
, u_int c
)
642 return c
> (sz
== 12 ? 0xff7U
: sz
== 16 ? 0xfff7U
: 0xffffff7);
646 * Offset-based I/O primitive
649 ioread(DOS_FS
*fs
, u_int offset
, void *buf
, u_int nbyte
)
656 if ((off
= offset
& (SECSIZ
- 1))) {
658 if ((err
= iobuf(fs
, bytsec(offset
))))
661 if ((n
= SECSIZ
- off
) > nbyte
)
663 memcpy(s
, fs
->buf
+ off
, n
);
667 n
= nbyte
& (SECSIZ
- 1);
669 if ((err
= ioget(fs
->fd
, bytsec(offset
), s
, bytsec(nbyte
))))
675 if ((err
= iobuf(fs
, bytsec(offset
))))
677 memcpy(s
, fs
->buf
, n
);
683 * Buffered sector-based I/O primitive
686 iobuf(DOS_FS
*fs
, u_int lsec
)
690 if (fs
->bufsec
!= lsec
) {
691 if ((err
= ioget(fs
->fd
, lsec
, fs
->buf
, 1)))
699 * Sector-based I/O primitive
702 ioget(struct open_file
*fd
, u_int lsec
, void *buf
, u_int nsec
)
706 if ((err
= (fd
->f_dev
->dv_strategy
)(fd
->f_devdata
, F_READ
, lsec
,
707 secbyt(nsec
), buf
, NULL
)))