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>
29 __FBSDID("$FreeBSD$");
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,
289 buf = (char *)buf + n;
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);
324 if (off < 0 || off > size) {
328 f->offset = (u_int)off;
337 dos_close(struct open_file *fd)
339 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
349 * Return some stat information on a file.
352 dos_stat(struct open_file *fd, struct stat *sb)
354 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
356 /* only important stuff */
357 sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444;
361 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
367 dos_readdir(struct open_file *fd, struct dirent *d)
369 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
373 u_int chk, i, x, xdn;
380 err = dos_read(fd, &dd, sizeof(dd), &res);
383 if (res == sizeof(dd))
385 if (dd.de.name[0] == 0)
388 /* Skip deleted entries */
389 if (dd.de.name[0] == 0xe5)
392 /* Check if directory entry is volume label */
393 if (dd.de.attr & FA_LABEL) {
395 * If volume label set, check if the current entry is
396 * extended entry (FA_XDE) for long file names.
398 if ((dd.de.attr & FA_MASK) == FA_XDE) {
400 * Read through all following extended entries
401 * to get the long file name. 0x40 marks the
402 * last entry containing part of long file name.
404 if (dd.xde.seq & 0x40)
406 else if (dd.xde.seq != xdn - 1 || dd.xde.chk != chk)
408 x = dd.xde.seq & ~0x40;
409 if (x < 1 || x > 20) {
413 cp_xdnm(fn, &dd.xde);
415 /* skip only volume label entries */
421 for (i = 0; i < 11; i++) {
422 x = ((x & 1) << 7) | (x >> 1);
436 d->d_fileno = (dd.de.clus[1] << 8) + dd.de.clus[0];
437 d->d_reclen = sizeof(*d);
438 d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG;
439 memcpy(d->d_name, fn, sizeof(d->d_name));
444 * Parse DOS boot sector
447 parsebs(DOS_FS *fs, DOS_BS *bs)
451 if ((bs->jmp[0] != 0x69 &&
452 bs->jmp[0] != 0xe9 &&
453 (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
454 bs->bpb.media < 0xf0)
456 if (cv2(bs->bpb.secsiz) != SECSIZ)
458 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
460 fs->bsize = secbyt(fs->spc);
461 fs->bshift = ffs(fs->bsize) - 1;
462 if ((fs->spf = cv2(bs->bpb.spf))) {
463 if (bs->bpb.fats != 2)
465 if (!(fs->dirents = cv2(bs->bpb.dirents)))
468 if (!(fs->spf = cv4(bs->bpb.lspf)))
470 if (!bs->bpb.fats || bs->bpb.fats > 16)
472 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
475 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
477 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats;
478 fs->lsndta = fs->lsndir + entsec(fs->dirents);
479 if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs)))
483 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
485 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
486 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
493 * Return directory entry from path
496 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
511 if (!(s = strchr(path, '/')))
513 if ((n = s - path) > 255)
515 memcpy(name, path, n);
518 if (!(de->attr & FA_DIR))
520 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
528 * Lookup path segment
531 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
533 static DOS_DIR dir[DEPSEC];
536 u_int nsec, lsec, xdn, chk, sec, ent, x;
540 for (ent = 0; ent < 2; ent++)
541 if (!strcasecmp(name, dotstr[ent])) {
545 if (!clus && fs->fatsz == 32)
547 nsec = !clus ? entsec(fs->dirents) : fs->spc;
553 else if (okclus(fs, clus))
554 lsec = blklsn(fs, clus);
557 for (sec = 0; sec < nsec; sec++) {
558 if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
560 for (ent = 0; ent < DEPSEC; ent++) {
561 if (!*dir[ent].de.name)
563 if (*dir[ent].de.name != 0xe5) {
564 if ((dir[ent].de.attr & FA_MASK) == FA_XDE) {
565 x = dir[ent].xde.seq;
566 if (x & 0x40 || (x + 1 == xdn &&
567 dir[ent].xde.chk == chk)) {
569 chk = dir[ent].xde.chk;
572 if (x >= 1 && x <= 20) {
573 cp_xdnm(lfn, &dir[ent].xde);
578 } else if (!(dir[ent].de.attr & FA_LABEL)) {
579 if ((ok = xdn == 1)) {
580 for (x = 0, i = 0; i < 11; i++)
581 x = ((((x & 1) << 7) | (x >> 1)) +
582 dir[ent].de.name[i]) & 0xff;
584 !strcasecmp(name, (const char *)lfn);
587 cp_sfn(sfn, &dir[ent].de);
588 ok = !strcasecmp(name, (const char *)sfn);
601 if ((err = fatget(fs, &clus)))
603 if (fatend(fs->fatsz, clus))
610 * Copy name from extended directory entry
613 cp_xdnm(u_char *lfn, DOS_XDE *xde)
619 {offsetof(DOS_XDE, name1), sizeof(xde->name1) / 2},
620 {offsetof(DOS_XDE, name2), sizeof(xde->name2) / 2},
621 {offsetof(DOS_XDE, name3), sizeof(xde->name3) / 2}
626 lfn += 13 * ((xde->seq & ~0x40) - 1);
627 for (n = 0; n < 3; n++)
628 for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
630 if ((c = cv2(p)) && (c < 32 || c > 127))
640 * Copy short filename
643 cp_sfn(u_char *sfn, DOS_DE *de)
649 if (*de->name != ' ') {
650 for (j = 7; de->name[j] == ' '; j--);
651 for (i = 0; i <= j; i++)
653 if (*de->ext != ' ') {
655 for (j = 2; de->ext[j] == ' '; j--);
656 for (i = 0; i <= j; i++)
666 * Return size of file in bytes
669 fsize(DOS_FS *fs, DOS_DE *de)
675 if (!(size = cv4(de->size)) && de->attr & FA_DIR) {
676 if (!(c = cv2(de->clus)))
677 size = fs->dirents * sizeof(DOS_DE);
679 if ((n = fatcnt(fs, c)) == -1)
681 size = blkbyt(fs, n);
688 * Count number of clusters in chain
691 fatcnt(DOS_FS *fs, u_int c)
695 for (n = 0; okclus(fs, c); n++)
698 return fatend(fs->fatsz, c) ? n : -1;
702 * Get next cluster in cluster chain
705 fatget(DOS_FS *fs, u_int *c)
711 err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
712 fs->fatsz != 32 ? 2 : 4);
715 x = fs->fatsz != 32 ? cv2(buf) : cv4(buf);
716 *c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
721 * Is cluster an end-of-chain marker?
724 fatend(u_int sz, u_int c)
726 return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
730 * Offset-based I/O primitive
733 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
740 if ((off = offset & (SECSIZ - 1))) {
742 if ((err = iobuf(fs, bytsec(offset))))
745 if ((n = SECSIZ - off) > nbyte)
747 memcpy(s, fs->buf + off, n);
751 n = nbyte & (SECSIZ - 1);
753 if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
759 if ((err = iobuf(fs, bytsec(offset))))
761 memcpy(s, fs->buf, n);
767 * Buffered sector-based I/O primitive
770 iobuf(DOS_FS *fs, u_int lsec)
774 if (fs->bufsec != lsec) {
775 if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
783 * Sector-based I/O primitive
786 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
791 if ((err = (fd->f_dev->dv_strategy)(fd->f_devdata, F_READ, lsec,
792 secbyt(nsec), buf, NULL)))