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.
29 * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
33 #include <sys/types.h>
41 typedef struct dos_mnt {
45 STAILQ_ENTRY(dos_mnt) dos_link;
48 typedef STAILQ_HEAD(dos_mnt_list, dos_mnt) dos_mnt_list_t;
49 static dos_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
51 static int dos_open(const char *path, struct open_file *fd);
52 static int dos_close(struct open_file *fd);
53 static int dos_read(struct open_file *fd, void *buf, size_t size, size_t *resid);
54 static off_t dos_seek(struct open_file *fd, off_t offset, int whence);
55 static int dos_stat(struct open_file *fd, struct stat *sb);
56 static int dos_readdir(struct open_file *fd, struct dirent *d);
57 static int dos_mount(const char *dev, const char *path, void **data);
58 static int dos_unmount(const char *dev, void *data);
60 struct fs_ops dosfs_fsops = {
63 .fo_close = dos_close,
65 .fo_write = null_write,
68 .fo_readdir = dos_readdir,
69 .fo_mount = dos_mount,
70 .fo_unmount = dos_unmount
73 #define SECSIZ 512 /* sector size */
74 #define SSHIFT 9 /* SECSIZ shift */
75 #define DEPSEC 16 /* directory entries per sector */
76 #define DSHIFT 4 /* DEPSEC shift */
77 #define LOCLUS 2 /* lowest cluster number */
78 #define FATBLKSZ 0x20000 /* size of block in the FAT cache buffer */
80 /* DOS "BIOS Parameter Block" */
82 u_char secsiz[2]; /* sector size */
83 u_char spc; /* sectors per cluster */
84 u_char ressec[2]; /* reserved sectors */
85 u_char fats; /* FATs */
86 u_char dirents[2]; /* root directory entries */
87 u_char secs[2]; /* total sectors */
88 u_char media; /* media descriptor */
89 u_char spf[2]; /* sectors per FAT */
90 u_char spt[2]; /* sectors per track */
91 u_char heads[2]; /* drive heads */
92 u_char hidsec[4]; /* hidden sectors */
93 u_char lsecs[4]; /* huge sectors */
94 u_char lspf[4]; /* huge sectors per FAT */
95 u_char xflg[2]; /* flags */
96 u_char vers[2]; /* filesystem version */
97 u_char rdcl[4]; /* root directory start cluster */
98 u_char infs[2]; /* filesystem info sector */
99 u_char bkbs[2]; /* backup boot sector */
102 /* Initial portion of DOS boot sector */
104 u_char jmp[3]; /* usually 80x86 'jmp' opcode */
105 u_char oem[8]; /* OEM name and version */
106 DOS_BPB bpb; /* BPB */
109 /* Supply missing "." and ".." root directory entries */
110 static const char *const dotstr[2] = {".", ".."};
111 static DOS_DE dot[2] = {
112 {". ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
113 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
114 {".. ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
115 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
118 /* The usual conversion macros to avoid multiplication and division */
119 #define bytsec(n) ((n) >> SSHIFT)
120 #define secbyt(s) ((s) << SSHIFT)
121 #define entsec(e) ((e) >> DSHIFT)
122 #define bytblk(fs, n) ((n) >> (fs)->bshift)
123 #define blkbyt(fs, b) ((b) << (fs)->bshift)
124 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
125 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
127 /* Convert cluster number to offset within filesystem */
128 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
130 /* Convert cluster number to logical sector number */
131 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
133 /* Convert cluster number to offset within FAT */
134 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
135 (sz) == 16 ? (c) << 1 : \
138 /* Does cluster number reference a valid data cluster? */
139 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
141 /* Get start cluster from directory entry */
142 #define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \
143 ((u_int)cv2((de)->dex.h_clus) << 16) | \
146 static int parsebs(DOS_FS *, DOS_BS *);
147 static int namede(DOS_FS *, const char *, DOS_DE **);
148 static int lookup(DOS_FS *, u_int, const char *, DOS_DE **);
149 static void cp_xdnm(u_char *, DOS_XDE *);
150 static void cp_sfn(u_char *, DOS_DE *);
151 static off_t fsize(DOS_FS *, DOS_DE *);
152 static int fatcnt(DOS_FS *, u_int);
153 static int fatget(DOS_FS *, u_int *);
154 static int fatend(u_int, u_int);
155 static int ioread(DOS_FS *, u_int, void *, size_t);
156 static int ioget(struct open_file *, daddr_t, void *, size_t);
159 dos_read_fatblk(DOS_FS *fs, struct open_file *fd, u_int blknum)
163 daddr_t offset_in_fat, max_offset_in_fat;
165 offset_in_fat = ((daddr_t)blknum) * FATBLKSZ;
166 max_offset_in_fat = secbyt(fs->spf);
168 if (offset_in_fat > max_offset_in_fat)
169 offset_in_fat = max_offset_in_fat;
170 if (offset_in_fat + io_size > max_offset_in_fat)
171 io_size = ((size_t)(max_offset_in_fat - offset_in_fat));
174 err = ioget(fd, fs->lsnfat + bytsec(offset_in_fat),
175 fs->fatbuf, io_size);
177 fs->fatbuf_blknum = ((u_int)(-1));
181 if (io_size < FATBLKSZ)
182 memset(fs->fatbuf + io_size, 0, FATBLKSZ - io_size);
184 fs->fatbuf_blknum = blknum;
189 * Mount DOS filesystem
192 dos_mount_impl(DOS_FS *fs, struct open_file *fd)
199 if ((buf = malloc(secbyt(1))) == NULL)
201 if ((err = ioget(fs->fd, 0, buf, secbyt(1))) ||
202 (err = parsebs(fs, (DOS_BS *)buf))) {
208 if ((fs->fatbuf = malloc(FATBLKSZ)) == NULL)
210 err = dos_read_fatblk(fs, fd, 0);
217 fs->root.name[0] = ' ';
218 if (fs->fatsz == 32) {
219 fs->root.clus[0] = fs->rdcl & 0xff;
220 fs->root.clus[1] = (fs->rdcl >> 8) & 0xff;
221 fs->root.dex.h_clus[0] = (fs->rdcl >> 16) & 0xff;
222 fs->root.dex.h_clus[1] = (fs->rdcl >> 24) & 0xff;
228 dos_mount(const char *dev, const char *path, void **data)
236 mnt = calloc(1, sizeof(*mnt));
240 mnt->dos_dev = strdup(dev);
241 if (mnt->dos_dev == NULL)
244 if (asprintf(&fs, "%s%s", dev, path) < 0)
247 mnt->dos_fd = open(fs, O_RDONLY);
249 if (mnt->dos_fd == -1)
252 f = fd2open_file(mnt->dos_fd);
253 if (strcmp(f->f_ops->fs_name, "dosfs") == 0) {
255 mnt->dos_fs = df->fs;
256 STAILQ_INSERT_TAIL(&mnt_list, mnt, dos_link);
264 if (mnt->dos_fd >= 0)
275 dos_unmount(const char *dev __unused, void *data)
277 dos_mnt_t *mnt = data;
279 STAILQ_REMOVE(&mnt_list, mnt, dos_mnt, dos_link);
287 * Unmount mounted filesystem
290 dos_unmount_impl(DOS_FS *fs)
303 dos_open(const char *path, struct open_file *fd)
313 dev = devformat((struct devdesc *)fd->f_devdata);
314 STAILQ_FOREACH(mnt, &mnt_list, dos_link) {
315 if (strcmp(dev, mnt->dos_dev) == 0)
320 /* Allocate mount structure, associate with open */
321 if ((fs = calloc(1, sizeof(DOS_FS))) == NULL)
323 if ((err = dos_mount_impl(fs, fd))) {
331 if ((err = namede(fs, path, &de))) {
333 dos_unmount_impl(fs);
337 clus = stclus(fs->fatsz, de);
338 size = cv4(de->size);
340 if ((!(de->attr & FA_DIR) && (!clus != !size)) ||
341 ((de->attr & FA_DIR) && size) ||
342 (clus && !okclus(fs, clus))) {
344 dos_unmount_impl(fs);
347 if ((f = calloc(1, sizeof(DOS_FILE))) == NULL) {
350 dos_unmount_impl(fs);
364 dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid)
367 u_int nb, off, clus, c, cnt, n;
368 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
372 * as ioget() can be called *a lot*, use twiddle here.
373 * also 4 seems to be good value not to slow loading down too much:
374 * with 270MB file (~540k ioget() calls, twiddle can easily waste
379 if ((size = fsize(f->fs, &f->de)) == -1)
381 if (nb > (n = size - f->offset))
384 if ((clus = stclus(f->fs->fatsz, &f->de)))
385 off &= f->fs->bsize - 1;
392 n = bytblk(f->fs, f->offset);
396 if ((err = fatget(f->fs, &c)))
398 if (!okclus(f->fs, c)) {
403 if (!clus || (n = f->fs->bsize - off) > cnt)
405 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
406 secbyt(f->fs->lsndir)) + off, buf, n)))
411 buf = (char *)buf + n;
416 *resid = nbyte - nb + cnt;
421 * Reposition within file
424 dos_seek(struct open_file *fd, off_t offset, int whence)
428 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
430 size = cv4(f->de.size);
446 if (off < 0 || off > size) {
450 f->offset = (u_int)off;
459 dos_close(struct open_file *fd)
461 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
466 dos_unmount_impl(fs);
471 * Return some stat information on a file.
474 dos_stat(struct open_file *fd, struct stat *sb)
476 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
478 /* only important stuff */
479 sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444;
483 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
489 dos_checksum(unsigned char *name, unsigned char *ext)
495 bcopy(ext, buf+8, 3);
497 for (i = 0; i < 11; i++) {
498 x = ((x & 1) << 7) | (x >> 1);
506 dos_readdir(struct open_file *fd, struct dirent *d)
508 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
519 err = dos_read(fd, &dd, sizeof(dd), &res);
522 if (res == sizeof(dd))
524 if (dd.de.name[0] == 0)
527 /* Skip deleted entries */
528 if (dd.de.name[0] == 0xe5)
531 /* Check if directory entry is volume label */
532 if (dd.de.attr & FA_LABEL) {
534 * If volume label set, check if the current entry is
535 * extended entry (FA_XDE) for long file names.
537 if ((dd.de.attr & FA_MASK) == FA_XDE) {
539 * Read through all following extended entries
540 * to get the long file name. 0x40 marks the
541 * last entry containing part of long file name.
543 if (dd.xde.seq & 0x40)
545 else if (dd.xde.seq != xdn - 1 ||
548 x = dd.xde.seq & ~0x40;
549 if (x < 1 || x > 20) {
553 cp_xdnm(fn, &dd.xde);
555 /* skip only volume label entries */
560 x = dos_checksum(dd.de.name, dd.de.ext);
571 d->d_fileno = (dd.de.clus[1] << 8) + dd.de.clus[0];
572 d->d_reclen = sizeof(*d);
573 d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG;
574 memcpy(d->d_name, fn, sizeof(d->d_name));
579 * Parse DOS boot sector
582 parsebs(DOS_FS *fs, DOS_BS *bs)
586 if ((bs->jmp[0] != 0x69 &&
587 bs->jmp[0] != 0xe9 &&
588 (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
589 bs->bpb.media < 0xf0)
591 if (cv2(bs->bpb.secsiz) != SECSIZ)
593 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
595 fs->bsize = secbyt(fs->spc);
596 fs->bshift = ffs(fs->bsize) - 1;
597 if ((fs->spf = cv2(bs->bpb.spf))) {
598 if (bs->bpb.fats != 2)
600 if (!(fs->dirents = cv2(bs->bpb.dirents)))
603 if (!(fs->spf = cv4(bs->bpb.lspf)))
605 if (!bs->bpb.fats || bs->bpb.fats > 16)
607 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
610 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
612 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats;
613 fs->lsndta = fs->lsndir + entsec(fs->dirents);
614 if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs)))
618 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
620 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
621 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
628 * Return directory entry from path
631 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
646 if (!(s = strchr(path, '/')))
648 if ((n = s - path) > 255)
649 return (ENAMETOOLONG);
650 memcpy(name, path, n);
653 if (!(de->attr & FA_DIR))
655 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
663 * Lookup path segment
666 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
668 static DOS_DIR dir[DEPSEC];
671 u_int nsec, lsec, xdn, chk, sec, ent, x;
675 for (ent = 0; ent < 2; ent++)
676 if (!strcasecmp(name, dotstr[ent])) {
680 if (!clus && fs->fatsz == 32)
682 nsec = !clus ? entsec(fs->dirents) : fs->spc;
688 else if (okclus(fs, clus))
689 lsec = blklsn(fs, clus);
692 for (sec = 0; sec < nsec; sec++) {
693 if ((err = ioget(fs->fd, lsec + sec, dir, secbyt(1))))
695 for (ent = 0; ent < DEPSEC; ent++) {
696 if (!*dir[ent].de.name)
698 if (*dir[ent].de.name != 0xe5) {
699 if ((dir[ent].de.attr & FA_MASK) ==
701 x = dir[ent].xde.seq;
702 if (x & 0x40 || (x + 1 == xdn &&
703 dir[ent].xde.chk == chk)) {
705 chk = dir[ent].xde.chk;
708 if (x >= 1 && x <= 20) {
709 cp_xdnm(lfn, &dir[ent].xde);
714 } else if (!(dir[ent].de.attr &
716 if ((ok = xdn == 1)) {
727 ok = !strcasecmp(name,
741 if ((err = fatget(fs, &clus)))
743 if (fatend(fs->fatsz, clus))
750 * Copy name from extended directory entry
753 cp_xdnm(u_char *lfn, DOS_XDE *xde)
759 {offsetof(DOS_XDE, name1), sizeof(xde->name1) / 2},
760 {offsetof(DOS_XDE, name2), sizeof(xde->name2) / 2},
761 {offsetof(DOS_XDE, name3), sizeof(xde->name3) / 2}
766 lfn += 13 * ((xde->seq & ~0x40) - 1);
767 for (n = 0; n < 3; n++)
768 for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
770 if ((c = cv2(p)) && (c < 32 || c > 127))
780 * Copy short filename
783 cp_sfn(u_char *sfn, DOS_DE *de)
789 if (*de->name != ' ') {
790 for (j = 7; de->name[j] == ' '; j--)
792 for (i = 0; i <= j; i++)
794 if (*de->ext != ' ') {
796 for (j = 2; de->ext[j] == ' '; j--)
798 for (i = 0; i <= j; i++)
808 * Return size of file in bytes
811 fsize(DOS_FS *fs, DOS_DE *de)
817 if (!(size = cv4(de->size)) && de->attr & FA_DIR) {
818 if (!(c = cv2(de->clus))) {
819 size = fs->dirents * sizeof(DOS_DE);
821 if ((n = fatcnt(fs, c)) == -1)
823 size = blkbyt(fs, n);
830 * Count number of clusters in chain
833 fatcnt(DOS_FS *fs, u_int c)
837 for (n = 0; okclus(fs, c); n++)
840 return (fatend(fs->fatsz, c) ? n : -1);
844 * Get next cluster in cluster chain. Use in core fat cache unless
845 * the number of current 128K block in FAT has changed.
848 fatget(DOS_FS *fs, u_int *c)
850 u_int val_in, val_out, offset, blknum, nbyte;
851 const u_char *p_entry;
854 /* check input value to prevent overflow in fatoff() */
856 if (val_in & 0xf0000000)
859 /* ensure that current 128K FAT block is cached */
860 offset = fatoff(fs->fatsz, val_in);
861 nbyte = fs->fatsz != 32 ? 2 : 4;
862 if (offset + nbyte > secbyt(fs->spf))
864 blknum = offset / FATBLKSZ;
866 if (offset + nbyte > FATBLKSZ)
868 if (blknum != fs->fatbuf_blknum) {
869 err = dos_read_fatblk(fs, fs->fd, blknum);
873 p_entry = fs->fatbuf + offset;
875 /* extract cluster number from FAT entry */
878 val_out = cv4(p_entry);
879 val_out &= 0x0fffffff;
882 val_out = cv2(p_entry);
885 val_out = cv2(p_entry);
899 * Is cluster an end-of-chain marker?
902 fatend(u_int sz, u_int c)
904 return (c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7));
908 * Offset-based I/O primitive
911 ioread(DOS_FS *fs, u_int offset, void *buf, size_t nbyte)
916 u_char local_buf[SECSIZ];
919 if ((off = offset & (SECSIZ - 1))) {
921 if ((n = SECSIZ - off) > nbyte)
923 err = ioget(fs->fd, bytsec(offset), local_buf,
927 memcpy(s, local_buf + off, n);
932 n = nbyte & (SECSIZ - 1);
934 if ((err = ioget(fs->fd, bytsec(offset), s, nbyte)))
940 err = ioget(fs->fd, bytsec(offset), local_buf,
944 memcpy(s, local_buf, n);
950 * Sector-based I/O primitive
953 ioget(struct open_file *fd, daddr_t lsec, void *buf, size_t size)
958 /* Make sure we get full read or error. */
960 rv = (fd->f_dev->dv_strategy)(fd->f_devdata, F_READ, lsec,
962 if ((rv == 0) && (size != rsize))