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.
28 #include <sys/cdefs.h>
31 * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
35 #include <sys/types.h>
44 static int dos_open(const char *path
, struct open_file
*fd
);
45 static int dos_close(struct open_file
*fd
);
46 static int dos_read(struct open_file
*fd
, void *buf
, size_t size
, size_t *resid
);
47 static off_t
dos_seek(struct open_file
*fd
, off_t offset
, int whence
);
48 static int dos_stat(struct open_file
*fd
, struct stat
*sb
);
49 static int dos_readdir(struct open_file
*fd
, struct dirent
*d
);
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 */
67 #define FATBLKSZ 0x20000 /* size of block in the FAT cache buffer */
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 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 *, size_t);
145 static int ioget(struct open_file
*, daddr_t
, void *, size_t);
148 dos_read_fatblk(DOS_FS
*fs
, struct open_file
*fd
, u_int blknum
)
152 daddr_t offset_in_fat
, max_offset_in_fat
;
154 offset_in_fat
= ((daddr_t
)blknum
) * FATBLKSZ
;
155 max_offset_in_fat
= secbyt(fs
->spf
);
157 if (offset_in_fat
> max_offset_in_fat
)
158 offset_in_fat
= max_offset_in_fat
;
159 if (offset_in_fat
+ io_size
> max_offset_in_fat
)
160 io_size
= ((size_t)(max_offset_in_fat
- offset_in_fat
));
163 err
= ioget(fd
, fs
->lsnfat
+ bytsec(offset_in_fat
),
164 fs
->fatbuf
, io_size
);
166 fs
->fatbuf_blknum
= ((u_int
)(-1));
171 if (io_size
< FATBLKSZ
)
172 memset(fs
->fatbuf
+ io_size
, 0, FATBLKSZ
- io_size
);
174 fs
->fatbuf_blknum
= blknum
;
179 * Mount DOS filesystem
182 dos_mount(DOS_FS
*fs
, struct open_file
*fd
)
187 bzero(fs
, sizeof(DOS_FS
));
190 if ((buf
= malloc(secbyt(1))) == NULL
)
192 if ((err
= ioget(fs
->fd
, 0, buf
, secbyt(1))) ||
193 (err
= parsebs(fs
, (DOS_BS
*)buf
))) {
199 if ((fs
->fatbuf
= malloc(FATBLKSZ
)) == NULL
)
201 err
= dos_read_fatblk(fs
, fd
, 0);
208 fs
->root
.name
[0] = ' ';
209 if (fs
->fatsz
== 32) {
210 fs
->root
.clus
[0] = fs
->rdcl
& 0xff;
211 fs
->root
.clus
[1] = (fs
->rdcl
>> 8) & 0xff;
212 fs
->root
.dex
.h_clus
[0] = (fs
->rdcl
>> 16) & 0xff;
213 fs
->root
.dex
.h_clus
[1] = (fs
->rdcl
>> 24) & 0xff;
219 * Unmount mounted filesystem
222 dos_unmount(DOS_FS
*fs
)
235 dos_open(const char *path
, struct open_file
*fd
)
243 /* Allocate mount structure, associate with open */
244 if ((fs
= malloc(sizeof(DOS_FS
))) == NULL
)
246 if ((err
= dos_mount(fs
, fd
))) {
251 if ((err
= namede(fs
, path
, &de
))) {
256 clus
= stclus(fs
->fatsz
, de
);
257 size
= cv4(de
->size
);
259 if ((!(de
->attr
& FA_DIR
) && (!clus
!= !size
)) ||
260 ((de
->attr
& FA_DIR
) && size
) ||
261 (clus
&& !okclus(fs
, clus
))) {
265 if ((f
= malloc(sizeof(DOS_FILE
))) == NULL
) {
270 bzero(f
, sizeof(DOS_FILE
));
274 fd
->f_fsdata
= (void *)f
;
282 dos_read(struct open_file
*fd
, void *buf
, size_t nbyte
, size_t *resid
)
285 u_int nb
, off
, clus
, c
, cnt
, n
;
286 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
290 * as ioget() can be called *a lot*, use twiddle here.
291 * also 4 seems to be good value not to slow loading down too much:
292 * with 270MB file (~540k ioget() calls, twiddle can easily waste 4-5sec.
296 if ((size
= fsize(f
->fs
, &f
->de
)) == -1)
298 if (nb
> (n
= size
- f
->offset
))
301 if ((clus
= stclus(f
->fs
->fatsz
, &f
->de
)))
302 off
&= f
->fs
->bsize
- 1;
309 n
= bytblk(f
->fs
, f
->offset
);
313 if ((err
= fatget(f
->fs
, &c
)))
315 if (!okclus(f
->fs
, c
)) {
320 if (!clus
|| (n
= f
->fs
->bsize
- off
) > cnt
)
322 if ((err
= ioread(f
->fs
, (c
? blkoff(f
->fs
, c
) :
323 secbyt(f
->fs
->lsndir
)) + off
, buf
, n
)))
328 buf
= (char *)buf
+ n
;
333 *resid
= nbyte
- nb
+ cnt
;
338 * Reposition within file
341 dos_seek(struct open_file
*fd
, off_t offset
, int whence
)
345 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
347 size
= cv4(f
->de
.size
);
363 if (off
< 0 || off
> size
) {
367 f
->offset
= (u_int
)off
;
376 dos_close(struct open_file
*fd
)
378 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
388 * Return some stat information on a file.
391 dos_stat(struct open_file
*fd
, struct stat
*sb
)
393 DOS_FILE
*f
= (DOS_FILE
*)fd
->f_fsdata
;
395 /* only important stuff */
396 sb
->st_mode
= f
->de
.attr
& FA_DIR
? S_IFDIR
| 0555 : S_IFREG
| 0444;
400 if ((sb
->st_size
= fsize(f
->fs
, &f
->de
)) == -1)
406 dos_checksum(unsigned char *name
, unsigned char *ext
)
412 bcopy(ext
, buf
+8, 3);
414 for (i
= 0; i
< 11; i
++) {
415 x
= ((x
& 1) << 7) | (x
>> 1);
423 dos_readdir(struct open_file
*fd
, struct dirent
*d
)
425 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
436 err
= dos_read(fd
, &dd
, sizeof(dd
), &res
);
439 if (res
== sizeof(dd
))
441 if (dd
.de
.name
[0] == 0)
444 /* Skip deleted entries */
445 if (dd
.de
.name
[0] == 0xe5)
448 /* Check if directory entry is volume label */
449 if (dd
.de
.attr
& FA_LABEL
) {
451 * If volume label set, check if the current entry is
452 * extended entry (FA_XDE) for long file names.
454 if ((dd
.de
.attr
& FA_MASK
) == FA_XDE
) {
456 * Read through all following extended entries
457 * to get the long file name. 0x40 marks the
458 * last entry containing part of long file name.
460 if (dd
.xde
.seq
& 0x40)
462 else if (dd
.xde
.seq
!= xdn
- 1 || dd
.xde
.chk
!= chk
)
464 x
= dd
.xde
.seq
& ~0x40;
465 if (x
< 1 || x
> 20) {
469 cp_xdnm(fn
, &dd
.xde
);
471 /* skip only volume label entries */
476 x
= dos_checksum(dd
.de
.name
, dd
.de
.ext
);
487 d
->d_fileno
= (dd
.de
.clus
[1] << 8) + dd
.de
.clus
[0];
488 d
->d_reclen
= sizeof(*d
);
489 d
->d_type
= (dd
.de
.attr
& FA_DIR
) ? DT_DIR
: DT_REG
;
490 memcpy(d
->d_name
, fn
, sizeof(d
->d_name
));
495 * Parse DOS boot sector
498 parsebs(DOS_FS
*fs
, DOS_BS
*bs
)
502 if ((bs
->jmp
[0] != 0x69 &&
503 bs
->jmp
[0] != 0xe9 &&
504 (bs
->jmp
[0] != 0xeb || bs
->jmp
[2] != 0x90)) ||
505 bs
->bpb
.media
< 0xf0)
507 if (cv2(bs
->bpb
.secsiz
) != SECSIZ
)
509 if (!(fs
->spc
= bs
->bpb
.spc
) || fs
->spc
& (fs
->spc
- 1))
511 fs
->bsize
= secbyt(fs
->spc
);
512 fs
->bshift
= ffs(fs
->bsize
) - 1;
513 if ((fs
->spf
= cv2(bs
->bpb
.spf
))) {
514 if (bs
->bpb
.fats
!= 2)
516 if (!(fs
->dirents
= cv2(bs
->bpb
.dirents
)))
519 if (!(fs
->spf
= cv4(bs
->bpb
.lspf
)))
521 if (!bs
->bpb
.fats
|| bs
->bpb
.fats
> 16)
523 if ((fs
->rdcl
= cv4(bs
->bpb
.rdcl
)) < LOCLUS
)
526 if (!(fs
->lsnfat
= cv2(bs
->bpb
.ressec
)))
528 fs
->lsndir
= fs
->lsnfat
+ fs
->spf
* bs
->bpb
.fats
;
529 fs
->lsndta
= fs
->lsndir
+ entsec(fs
->dirents
);
530 if (!(sc
= cv2(bs
->bpb
.secs
)) && !(sc
= cv4(bs
->bpb
.lsecs
)))
534 if ((fs
->xclus
= secblk(fs
, sc
- fs
->lsndta
) + 1) < LOCLUS
)
536 fs
->fatsz
= fs
->dirents
? fs
->xclus
< 0xff6 ? 12 : 16 : 32;
537 sc
= (secbyt(fs
->spf
) << 1) / (fs
->fatsz
>> 2) - 1;
544 * Return directory entry from path
547 namede(DOS_FS
*fs
, const char *path
, DOS_DE
**dep
)
562 if (!(s
= strchr(path
, '/')))
564 if ((n
= s
- path
) > 255)
565 return (ENAMETOOLONG
);
566 memcpy(name
, path
, n
);
569 if (!(de
->attr
& FA_DIR
))
571 if ((err
= lookup(fs
, stclus(fs
->fatsz
, de
), name
, &de
)))
579 * Lookup path segment
582 lookup(DOS_FS
*fs
, u_int clus
, const char *name
, DOS_DE
**dep
)
584 static DOS_DIR dir
[DEPSEC
];
587 u_int nsec
, lsec
, xdn
, chk
, sec
, ent
, x
;
591 for (ent
= 0; ent
< 2; ent
++)
592 if (!strcasecmp(name
, dotstr
[ent
])) {
596 if (!clus
&& fs
->fatsz
== 32)
598 nsec
= !clus
? entsec(fs
->dirents
) : fs
->spc
;
604 else if (okclus(fs
, clus
))
605 lsec
= blklsn(fs
, clus
);
608 for (sec
= 0; sec
< nsec
; sec
++) {
609 if ((err
= ioget(fs
->fd
, lsec
+ sec
, dir
, secbyt(1))))
611 for (ent
= 0; ent
< DEPSEC
; ent
++) {
612 if (!*dir
[ent
].de
.name
)
614 if (*dir
[ent
].de
.name
!= 0xe5) {
615 if ((dir
[ent
].de
.attr
& FA_MASK
) == FA_XDE
) {
616 x
= dir
[ent
].xde
.seq
;
617 if (x
& 0x40 || (x
+ 1 == xdn
&&
618 dir
[ent
].xde
.chk
== chk
)) {
620 chk
= dir
[ent
].xde
.chk
;
623 if (x
>= 1 && x
<= 20) {
624 cp_xdnm(lfn
, &dir
[ent
].xde
);
629 } else if (!(dir
[ent
].de
.attr
& FA_LABEL
)) {
630 if ((ok
= xdn
== 1)) {
631 x
= dos_checksum(dir
[ent
].de
.name
, dir
[ent
].de
.ext
);
633 !strcasecmp(name
, (const char *)lfn
);
636 cp_sfn(sfn
, &dir
[ent
].de
);
637 ok
= !strcasecmp(name
, (const char *)sfn
);
650 if ((err
= fatget(fs
, &clus
)))
652 if (fatend(fs
->fatsz
, clus
))
659 * Copy name from extended directory entry
662 cp_xdnm(u_char
*lfn
, DOS_XDE
*xde
)
668 {offsetof(DOS_XDE
, name1
), sizeof(xde
->name1
) / 2},
669 {offsetof(DOS_XDE
, name2
), sizeof(xde
->name2
) / 2},
670 {offsetof(DOS_XDE
, name3
), sizeof(xde
->name3
) / 2}
675 lfn
+= 13 * ((xde
->seq
& ~0x40) - 1);
676 for (n
= 0; n
< 3; n
++)
677 for (p
= (u_char
*)xde
+ ix
[n
].off
, x
= ix
[n
].dim
; x
;
679 if ((c
= cv2(p
)) && (c
< 32 || c
> 127))
689 * Copy short filename
692 cp_sfn(u_char
*sfn
, DOS_DE
*de
)
698 if (*de
->name
!= ' ') {
699 for (j
= 7; de
->name
[j
] == ' '; j
--);
700 for (i
= 0; i
<= j
; i
++)
702 if (*de
->ext
!= ' ') {
704 for (j
= 2; de
->ext
[j
] == ' '; j
--);
705 for (i
= 0; i
<= j
; i
++)
715 * Return size of file in bytes
718 fsize(DOS_FS
*fs
, DOS_DE
*de
)
724 if (!(size
= cv4(de
->size
)) && de
->attr
& FA_DIR
) {
725 if (!(c
= cv2(de
->clus
)))
726 size
= fs
->dirents
* sizeof(DOS_DE
);
728 if ((n
= fatcnt(fs
, c
)) == -1)
730 size
= blkbyt(fs
, n
);
737 * Count number of clusters in chain
740 fatcnt(DOS_FS
*fs
, u_int c
)
744 for (n
= 0; okclus(fs
, c
); n
++)
747 return (fatend(fs
->fatsz
, c
) ? n
: -1);
751 * Get next cluster in cluster chain. Use in core fat cache unless
752 * the number of current 128K block in FAT has changed.
755 fatget(DOS_FS
*fs
, u_int
*c
)
757 u_int val_in
, val_out
, offset
, blknum
, nbyte
;
758 const u_char
*p_entry
;
761 /* check input value to prevent overflow in fatoff() */
763 if (val_in
& 0xf0000000)
766 /* ensure that current 128K FAT block is cached */
767 offset
= fatoff(fs
->fatsz
, val_in
);
768 nbyte
= fs
->fatsz
!= 32 ? 2 : 4;
769 if (offset
+ nbyte
> secbyt(fs
->spf
))
771 blknum
= offset
/ FATBLKSZ
;
773 if (offset
+ nbyte
> FATBLKSZ
)
775 if (blknum
!= fs
->fatbuf_blknum
) {
776 err
= dos_read_fatblk(fs
, fs
->fd
, blknum
);
780 p_entry
= fs
->fatbuf
+ offset
;
782 /* extract cluster number from FAT entry */
785 val_out
= cv4(p_entry
);
786 val_out
&= 0x0fffffff;
789 val_out
= cv2(p_entry
);
792 val_out
= cv2(p_entry
);
806 * Is cluster an end-of-chain marker?
809 fatend(u_int sz
, u_int c
)
811 return (c
> (sz
== 12 ? 0xff7U
: sz
== 16 ? 0xfff7U
: 0xffffff7));
815 * Offset-based I/O primitive
818 ioread(DOS_FS
*fs
, u_int offset
, void *buf
, size_t nbyte
)
823 u_char local_buf
[SECSIZ
];
826 if ((off
= offset
& (SECSIZ
- 1))) {
828 if ((n
= SECSIZ
- off
) > nbyte
)
830 if ((err
= ioget(fs
->fd
, bytsec(offset
), local_buf
, sizeof(local_buf
))))
832 memcpy(s
, local_buf
+ off
, n
);
837 n
= nbyte
& (SECSIZ
- 1);
839 if ((err
= ioget(fs
->fd
, bytsec(offset
), s
, nbyte
)))
845 if ((err
= ioget(fs
->fd
, bytsec(offset
), local_buf
, sizeof(local_buf
))))
847 memcpy(s
, local_buf
, n
);
853 * Sector-based I/O primitive
856 ioget(struct open_file
*fd
, daddr_t lsec
, void *buf
, size_t size
)
861 /* Make sure we get full read or error. */
863 rv
= (fd
->f_dev
->dv_strategy
)(fd
->f_devdata
, F_READ
, lsec
,
865 if ((rv
== 0) && (size
!= rsize
))