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
);
50 static int dos_readdir(struct open_file
*fd
, struct dirent
*d
);
52 struct fs_ops dosfs_fsops
= {
63 #define SECSIZ 512 /* sector size */
64 #define SSHIFT 9 /* SECSIZ shift */
65 #define DEPSEC 16 /* directory entries per sector */
66 #define DSHIFT 4 /* DEPSEC shift */
67 #define LOCLUS 2 /* lowest cluster number */
69 /* DOS "BIOS Parameter Block" */
71 u_char secsiz
[2]; /* sector size */
72 u_char spc
; /* sectors per cluster */
73 u_char ressec
[2]; /* reserved sectors */
74 u_char fats
; /* FATs */
75 u_char dirents
[2]; /* root directory entries */
76 u_char secs
[2]; /* total sectors */
77 u_char media
; /* media descriptor */
78 u_char spf
[2]; /* sectors per FAT */
79 u_char spt
[2]; /* sectors per track */
80 u_char heads
[2]; /* drive heads */
81 u_char hidsec
[4]; /* hidden sectors */
82 u_char lsecs
[4]; /* huge sectors */
83 u_char lspf
[4]; /* huge sectors per FAT */
84 u_char xflg
[2]; /* flags */
85 u_char vers
[2]; /* filesystem version */
86 u_char rdcl
[4]; /* root directory start cluster */
87 u_char infs
[2]; /* filesystem info sector */
88 u_char bkbs
[2]; /* backup boot sector */
91 /* Initial portion of DOS boot sector */
93 u_char jmp
[3]; /* usually 80x86 'jmp' opcode */
94 u_char oem
[8]; /* OEM name and version */
95 DOS_BPB bpb
; /* BPB */
98 /* Supply missing "." and ".." root directory entries */
99 static const char *const dotstr
[2] = {".", ".."};
100 static DOS_DE dot
[2] = {
101 {". ", " ", FA_DIR
, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
102 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
103 {".. ", " ", FA_DIR
, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
104 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
107 /* The usual conversion macros to avoid multiplication and division */
108 #define bytsec(n) ((n) >> SSHIFT)
109 #define secbyt(s) ((s) << SSHIFT)
110 #define entsec(e) ((e) >> DSHIFT)
111 #define bytblk(fs, n) ((n) >> (fs)->bshift)
112 #define blkbyt(fs, b) ((b) << (fs)->bshift)
113 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
114 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
116 /* Convert cluster number to offset within filesystem */
117 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
119 /* Convert cluster number to logical sector number */
120 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
122 /* Convert cluster number to offset within FAT */
123 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
124 (sz) == 16 ? (c) << 1 : \
127 /* Does cluster number reference a valid data cluster? */
128 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
130 /* Get start cluster from directory entry */
131 #define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \
132 ((u_int)cv2((de)->dex.h_clus) << 16) | \
135 static int dosunmount(DOS_FS
*);
136 static int parsebs(DOS_FS
*, DOS_BS
*);
137 static int namede(DOS_FS
*, const char *, DOS_DE
**);
138 static int lookup(DOS_FS
*, u_int
, const char *, DOS_DE
**);
139 static void cp_xdnm(u_char
*, DOS_XDE
*);
140 static void cp_sfn(u_char
*, DOS_DE
*);
141 static off_t
fsize(DOS_FS
*, DOS_DE
*);
142 static int fatcnt(DOS_FS
*, u_int
);
143 static int fatget(DOS_FS
*, u_int
*);
144 static int fatend(u_int
, u_int
);
145 static int ioread(DOS_FS
*, u_int
, void *, u_int
);
146 static int iobuf(DOS_FS
*, u_int
);
147 static int ioget(struct open_file
*, u_int
, void *, u_int
);
150 * Mount DOS filesystem
153 dos_mount(DOS_FS
*fs
, struct open_file
*fd
)
157 bzero(fs
, sizeof(DOS_FS
));
159 if ((err
= !(fs
->buf
= malloc(SECSIZ
)) ? errno
: 0) ||
160 (err
= ioget(fs
->fd
, 0, fs
->buf
, 1)) ||
161 (err
= parsebs(fs
, (DOS_BS
*)fs
->buf
))) {
162 (void)dosunmount(fs
);
166 fs
->root
.name
[0] = ' ';
167 if (fs
->fatsz
== 32) {
168 fs
->root
.clus
[0] = fs
->rdcl
& 0xff;
169 fs
->root
.clus
[1] = (fs
->rdcl
>> 8) & 0xff;
170 fs
->root
.dex
.h_clus
[0] = (fs
->rdcl
>> 16) & 0xff;
171 fs
->root
.dex
.h_clus
[1] = (fs
->rdcl
>> 24) & 0xff;
177 * Unmount mounted filesystem
180 dos_unmount(DOS_FS
*fs
)
186 if ((err
= dosunmount(fs
)))
192 * Common code shared by dos_mount() and dos_unmount()
195 dosunmount(DOS_FS
*fs
)
207 dos_open(const char *path
, struct open_file
*fd
)
215 /* Allocate mount structure, associate with open */
216 fs
= malloc(sizeof(DOS_FS
));
218 if ((err
= dos_mount(fs
, fd
)))
221 if ((err
= namede(fs
, path
, &de
)))
224 clus
= stclus(fs
->fatsz
, de
);
225 size
= cv4(de
->size
);
227 if ((!(de
->attr
& FA_DIR
) && (!clus
!= !size
)) ||
228 ((de
->attr
& FA_DIR
) && size
) ||
229 (clus
&& !okclus(fs
, clus
))) {
233 f
= malloc(sizeof(DOS_FILE
));
234 bzero(f
, sizeof(DOS_FILE
));
238 fd
->f_fsdata
= (void *)f
;
248 dos_read(struct open_file
*fd
, void *buf
, size_t nbyte
, size_t *resid
)
251 u_int nb
, off
, clus
, c
, cnt
, n
;
252 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
256 if ((size
= fsize(f
->fs
, &f
->de
)) == -1)
258 if (nb
> (n
= size
- f
->offset
))
261 if ((clus
= stclus(f
->fs
->fatsz
, &f
->de
)))
262 off
&= f
->fs
->bsize
- 1;
269 n
= bytblk(f
->fs
, f
->offset
);
273 if ((err
= fatget(f
->fs
, &c
)))
275 if (!okclus(f
->fs
, c
)) {
280 if (!clus
|| (n
= f
->fs
->bsize
- off
) > cnt
)
282 if ((err
= ioread(f
->fs
, (c
? blkoff(f
->fs
, c
) :
283 secbyt(f
->fs
->lsndir
)) + off
,
294 *resid
= nbyte
- nb
+ cnt
;
299 * Reposition within file
302 dos_seek(struct open_file
*fd
, off_t offset
, int whence
)
306 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
308 size
= cv4(f
->de
.size
);
323 if (off
< 0 || off
> size
)
325 f
->offset
= (u_int
)off
;
334 dos_close(struct open_file
*fd
)
336 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
351 * Return some stat information on a file.
354 dos_stat(struct open_file
*fd
, struct stat
*sb
)
356 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
358 /* only important stuff */
359 sb
->st_mode
= f
->de
.attr
& FA_DIR
? S_IFDIR
| 0555 : S_IFREG
| 0444;
363 if ((sb
->st_size
= fsize(f
->fs
, &f
->de
)) == -1)
369 dos_readdir(struct open_file
*fd
, struct dirent
*d
)
371 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
375 u_int chk
, i
, x
, xdn
;
382 err
= dos_read(fd
, &dd
, sizeof(dd
), &res
);
385 if (res
== sizeof(dd
))
387 if (dd
.de
.name
[0] == 0)
390 /* Skip deleted entries */
391 if (dd
.de
.name
[0] == 0xe5)
394 /* Check if directory entry is volume label */
395 if (dd
.de
.attr
& FA_LABEL
) {
397 * If volume label set, check if the current entry is
398 * extended entry (FA_XDE) for long file names.
400 if ((dd
.de
.attr
& FA_MASK
) == FA_XDE
) {
402 * Read through all following extended entries
403 * to get the long file name. 0x40 marks the
404 * last entry containing part of long file name.
406 if (dd
.xde
.seq
& 0x40)
408 else if (dd
.xde
.seq
!= xdn
- 1 || dd
.xde
.chk
!= chk
)
410 x
= dd
.xde
.seq
& ~0x40;
411 if (x
< 1 || x
> 20) {
415 cp_xdnm(fn
, &dd
.xde
);
417 /* skip only volume label entries */
423 for (i
= 0; i
< 11; i
++) {
424 x
= ((x
& 1) << 7) | (x
>> 1);
425 x
+= (i
< 8) ? dd
.de
.name
[i
] : dd
.de
.ext
[i
-8];
438 d
->d_fileno
= (dd
.de
.clus
[1] << 8) + dd
.de
.clus
[0];
439 d
->d_type
= (dd
.de
.attr
& FA_DIR
) ? DT_DIR
: DT_REG
;
440 memcpy(d
->d_name
, fn
, sizeof(d
->d_name
));
445 * Parse DOS boot sector
448 parsebs(DOS_FS
*fs
, DOS_BS
*bs
)
452 if ((bs
->jmp
[0] != 0x69 &&
453 bs
->jmp
[0] != 0xe9 &&
454 (bs
->jmp
[0] != 0xeb || bs
->jmp
[2] != 0x90)) ||
455 bs
->bpb
.media
< 0xf0)
457 if (cv2(bs
->bpb
.secsiz
) != SECSIZ
)
459 if (!(fs
->spc
= bs
->bpb
.spc
) || fs
->spc
& (fs
->spc
- 1))
461 fs
->bsize
= secbyt(fs
->spc
);
462 fs
->bshift
= ffs(fs
->bsize
) - 1;
463 if ((fs
->spf
= cv2(bs
->bpb
.spf
))) {
464 if (bs
->bpb
.fats
!= 2)
466 if (!(fs
->dirents
= cv2(bs
->bpb
.dirents
)))
469 if (!(fs
->spf
= cv4(bs
->bpb
.lspf
)))
471 if (!bs
->bpb
.fats
|| bs
->bpb
.fats
> 16)
473 if ((fs
->rdcl
= cv4(bs
->bpb
.rdcl
)) < LOCLUS
)
476 if (!(fs
->lsnfat
= cv2(bs
->bpb
.ressec
)))
478 fs
->lsndir
= fs
->lsnfat
+ fs
->spf
* bs
->bpb
.fats
;
479 fs
->lsndta
= fs
->lsndir
+ entsec(fs
->dirents
);
480 if (!(sc
= cv2(bs
->bpb
.secs
)) && !(sc
= cv4(bs
->bpb
.lsecs
)))
484 if ((fs
->xclus
= secblk(fs
, sc
- fs
->lsndta
) + 1) < LOCLUS
)
486 fs
->fatsz
= fs
->dirents
? fs
->xclus
< 0xff6 ? 12 : 16 : 32;
487 sc
= (secbyt(fs
->spf
) << 1) / (fs
->fatsz
>> 2) - 1;
494 * Return directory entry from path
497 namede(DOS_FS
*fs
, const char *path
, DOS_DE
**dep
)
512 if (!(s
= strchr(path
, '/')))
514 if ((n
= s
- path
) > 255)
516 memcpy(name
, path
, n
);
519 if (!(de
->attr
& FA_DIR
))
521 if ((err
= lookup(fs
, stclus(fs
->fatsz
, de
), name
, &de
)))
529 * Lookup path segment
532 lookup(DOS_FS
*fs
, u_int clus
, const char *name
, DOS_DE
**dep
)
534 static DOS_DIR dir
[DEPSEC
];
537 u_int nsec
, lsec
, xdn
, chk
, sec
, ent
, x
;
541 for (ent
= 0; ent
< 2; ent
++)
542 if (!strcasecmp(name
, dotstr
[ent
])) {
546 if (!clus
&& fs
->fatsz
== 32)
548 nsec
= !clus
? entsec(fs
->dirents
) : fs
->spc
;
554 else if (okclus(fs
, clus
))
555 lsec
= blklsn(fs
, clus
);
558 for (sec
= 0; sec
< nsec
; sec
++) {
559 if ((err
= ioget(fs
->fd
, lsec
+ sec
, dir
, 1)))
561 for (ent
= 0; ent
< DEPSEC
; ent
++) {
562 if (!*dir
[ent
].de
.name
)
564 if (*dir
[ent
].de
.name
!= 0xe5) {
565 if ((dir
[ent
].de
.attr
& FA_MASK
) == FA_XDE
) {
566 x
= dir
[ent
].xde
.seq
;
567 if (x
& 0x40 || (x
+ 1 == xdn
&&
568 dir
[ent
].xde
.chk
== chk
)) {
570 chk
= dir
[ent
].xde
.chk
;
573 if (x
>= 1 && x
<= 20) {
574 cp_xdnm(lfn
, &dir
[ent
].xde
);
579 } else if (!(dir
[ent
].de
.attr
& FA_LABEL
)) {
580 if ((ok
= xdn
== 1)) {
581 for (x
= 0, i
= 0; i
< 11; i
++)
582 x
= ((((x
& 1) << 7) | (x
>> 1)) +
583 ((i
< 8) ? dir
[ent
].de
.name
[i
] :
584 dir
[ent
].de
.ext
[i
- 8])
587 !strcasecmp(name
, (const char *)lfn
);
590 cp_sfn(sfn
, &dir
[ent
].de
);
591 ok
= !strcasecmp(name
, (const char *)sfn
);
604 if ((err
= fatget(fs
, &clus
)))
606 if (fatend(fs
->fatsz
, clus
))
613 * Copy name from extended directory entry
616 cp_xdnm(u_char
*lfn
, DOS_XDE
*xde
)
622 {offsetof(DOS_XDE
, name1
), sizeof(xde
->name1
) / 2},
623 {offsetof(DOS_XDE
, name2
), sizeof(xde
->name2
) / 2},
624 {offsetof(DOS_XDE
, name3
), sizeof(xde
->name3
) / 2}
629 lfn
+= 13 * ((xde
->seq
& ~0x40) - 1);
630 for (n
= 0; n
< 3; n
++)
631 for (p
= (u_char
*)xde
+ ix
[n
].off
, x
= ix
[n
].dim
; x
;
633 if ((c
= cv2(p
)) && (c
< 32 || c
> 127))
643 * Copy short filename
646 cp_sfn(u_char
*sfn
, DOS_DE
*de
)
652 if (*de
->name
!= ' ') {
653 for (j
= 7; de
->name
[j
] == ' '; j
--);
654 for (i
= 0; i
<= j
; i
++)
656 if (*de
->ext
!= ' ') {
658 for (j
= 2; de
->ext
[j
] == ' '; j
--);
659 for (i
= 0; i
<= j
; i
++)
669 * Return size of file in bytes
672 fsize(DOS_FS
*fs
, DOS_DE
*de
)
678 if (!(size
= cv4(de
->size
)) && de
->attr
& FA_DIR
) {
679 if (!(c
= cv2(de
->clus
)))
680 size
= fs
->dirents
* sizeof(DOS_DE
);
682 if ((n
= fatcnt(fs
, c
)) == -1)
684 size
= blkbyt(fs
, n
);
691 * Count number of clusters in chain
694 fatcnt(DOS_FS
*fs
, u_int c
)
698 for (n
= 0; okclus(fs
, c
); n
++)
701 return fatend(fs
->fatsz
, c
) ? n
: -1;
705 * Get next cluster in cluster chain
708 fatget(DOS_FS
*fs
, u_int
*c
)
714 err
= ioread(fs
, secbyt(fs
->lsnfat
) + fatoff(fs
->fatsz
, *c
), buf
,
715 fs
->fatsz
!= 32 ? 2 : 4);
718 x
= fs
->fatsz
!= 32 ? cv2(buf
) : cv4(buf
);
719 *c
= fs
->fatsz
== 12 ? *c
& 1 ? x
>> 4 : x
& 0xfff : x
;
724 * Is cluster an end-of-chain marker?
727 fatend(u_int sz
, u_int c
)
729 return c
> (sz
== 12 ? 0xff7U
: sz
== 16 ? 0xfff7U
: 0xffffff7);
733 * Offset-based I/O primitive
736 ioread(DOS_FS
*fs
, u_int offset
, void *buf
, u_int nbyte
)
743 if ((off
= offset
& (SECSIZ
- 1))) {
745 if ((err
= iobuf(fs
, bytsec(offset
))))
748 if ((n
= SECSIZ
- off
) > nbyte
)
750 memcpy(s
, fs
->buf
+ off
, n
);
754 n
= nbyte
& (SECSIZ
- 1);
756 if ((err
= ioget(fs
->fd
, bytsec(offset
), s
, bytsec(nbyte
))))
762 if ((err
= iobuf(fs
, bytsec(offset
))))
764 memcpy(s
, fs
->buf
, n
);
770 * Buffered sector-based I/O primitive
773 iobuf(DOS_FS
*fs
, u_int lsec
)
777 if (fs
->bufsec
!= lsec
) {
778 if ((err
= ioget(fs
->fd
, lsec
, fs
->buf
, 1)))
786 * Sector-based I/O primitive
789 ioget(struct open_file
*fd
, u_int lsec
, void *buf
, u_int nsec
)
794 if ((err
= (fd
->f_dev
->dv_strategy
)(fd
->f_devdata
, F_READ
, lsec
,
795 secbyt(nsec
), buf
, NULL
)))