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>
44 typedef struct dos_mnt {
48 STAILQ_ENTRY(dos_mnt) dos_link;
51 typedef STAILQ_HEAD(dos_mnt_list, dos_mnt) dos_mnt_list_t;
52 static dos_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
54 static int dos_open(const char *path, struct open_file *fd);
55 static int dos_close(struct open_file *fd);
56 static int dos_read(struct open_file *fd, void *buf, size_t size, size_t *resid);
57 static off_t dos_seek(struct open_file *fd, off_t offset, int whence);
58 static int dos_stat(struct open_file *fd, struct stat *sb);
59 static int dos_readdir(struct open_file *fd, struct dirent *d);
60 static int dos_mount(const char *dev, const char *path, void **data);
61 static int dos_unmount(const char *dev, void *data);
63 struct fs_ops dosfs_fsops = {
66 .fo_close = dos_close,
68 .fo_write = null_write,
71 .fo_readdir = dos_readdir,
72 .fo_mount = dos_mount,
73 .fo_unmount = dos_unmount
76 #define SECSIZ 512 /* sector size */
77 #define SSHIFT 9 /* SECSIZ shift */
78 #define DEPSEC 16 /* directory entries per sector */
79 #define DSHIFT 4 /* DEPSEC shift */
80 #define LOCLUS 2 /* lowest cluster number */
81 #define FATBLKSZ 0x20000 /* size of block in the FAT cache buffer */
83 /* DOS "BIOS Parameter Block" */
85 u_char secsiz[2]; /* sector size */
86 u_char spc; /* sectors per cluster */
87 u_char ressec[2]; /* reserved sectors */
88 u_char fats; /* FATs */
89 u_char dirents[2]; /* root directory entries */
90 u_char secs[2]; /* total sectors */
91 u_char media; /* media descriptor */
92 u_char spf[2]; /* sectors per FAT */
93 u_char spt[2]; /* sectors per track */
94 u_char heads[2]; /* drive heads */
95 u_char hidsec[4]; /* hidden sectors */
96 u_char lsecs[4]; /* huge sectors */
97 u_char lspf[4]; /* huge sectors per FAT */
98 u_char xflg[2]; /* flags */
99 u_char vers[2]; /* filesystem version */
100 u_char rdcl[4]; /* root directory start cluster */
101 u_char infs[2]; /* filesystem info sector */
102 u_char bkbs[2]; /* backup boot sector */
105 /* Initial portion of DOS boot sector */
107 u_char jmp[3]; /* usually 80x86 'jmp' opcode */
108 u_char oem[8]; /* OEM name and version */
109 DOS_BPB bpb; /* BPB */
112 /* Supply missing "." and ".." root directory entries */
113 static const char *const dotstr[2] = {".", ".."};
114 static DOS_DE dot[2] = {
115 {". ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
116 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
117 {".. ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
118 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
121 /* The usual conversion macros to avoid multiplication and division */
122 #define bytsec(n) ((n) >> SSHIFT)
123 #define secbyt(s) ((s) << SSHIFT)
124 #define entsec(e) ((e) >> DSHIFT)
125 #define bytblk(fs, n) ((n) >> (fs)->bshift)
126 #define blkbyt(fs, b) ((b) << (fs)->bshift)
127 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
128 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
130 /* Convert cluster number to offset within filesystem */
131 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
133 /* Convert cluster number to logical sector number */
134 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
136 /* Convert cluster number to offset within FAT */
137 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
138 (sz) == 16 ? (c) << 1 : \
141 /* Does cluster number reference a valid data cluster? */
142 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
144 /* Get start cluster from directory entry */
145 #define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \
146 ((u_int)cv2((de)->dex.h_clus) << 16) | \
149 static int parsebs(DOS_FS *, DOS_BS *);
150 static int namede(DOS_FS *, const char *, DOS_DE **);
151 static int lookup(DOS_FS *, u_int, const char *, DOS_DE **);
152 static void cp_xdnm(u_char *, DOS_XDE *);
153 static void cp_sfn(u_char *, DOS_DE *);
154 static off_t fsize(DOS_FS *, DOS_DE *);
155 static int fatcnt(DOS_FS *, u_int);
156 static int fatget(DOS_FS *, u_int *);
157 static int fatend(u_int, u_int);
158 static int ioread(DOS_FS *, u_int, void *, size_t);
159 static int ioget(struct open_file *, daddr_t, void *, size_t);
162 dos_read_fatblk(DOS_FS *fs, struct open_file *fd, u_int blknum)
166 daddr_t offset_in_fat, max_offset_in_fat;
168 offset_in_fat = ((daddr_t)blknum) * FATBLKSZ;
169 max_offset_in_fat = secbyt(fs->spf);
171 if (offset_in_fat > max_offset_in_fat)
172 offset_in_fat = max_offset_in_fat;
173 if (offset_in_fat + io_size > max_offset_in_fat)
174 io_size = ((size_t)(max_offset_in_fat - offset_in_fat));
177 err = ioget(fd, fs->lsnfat + bytsec(offset_in_fat),
178 fs->fatbuf, io_size);
180 fs->fatbuf_blknum = ((u_int)(-1));
184 if (io_size < FATBLKSZ)
185 memset(fs->fatbuf + io_size, 0, FATBLKSZ - io_size);
187 fs->fatbuf_blknum = blknum;
192 * Mount DOS filesystem
195 dos_mount_impl(DOS_FS *fs, struct open_file *fd)
202 if ((buf = malloc(secbyt(1))) == NULL)
204 if ((err = ioget(fs->fd, 0, buf, secbyt(1))) ||
205 (err = parsebs(fs, (DOS_BS *)buf))) {
211 if ((fs->fatbuf = malloc(FATBLKSZ)) == NULL)
213 err = dos_read_fatblk(fs, fd, 0);
220 fs->root.name[0] = ' ';
221 if (fs->fatsz == 32) {
222 fs->root.clus[0] = fs->rdcl & 0xff;
223 fs->root.clus[1] = (fs->rdcl >> 8) & 0xff;
224 fs->root.dex.h_clus[0] = (fs->rdcl >> 16) & 0xff;
225 fs->root.dex.h_clus[1] = (fs->rdcl >> 24) & 0xff;
231 dos_mount(const char *dev, const char *path, void **data)
239 mnt = calloc(1, sizeof(*mnt));
243 mnt->dos_dev = strdup(dev);
244 if (mnt->dos_dev == NULL)
247 if (asprintf(&fs, "%s%s", dev, path) < 0)
250 mnt->dos_fd = open(fs, O_RDONLY);
252 if (mnt->dos_fd == -1)
255 f = fd2open_file(mnt->dos_fd);
256 if (strcmp(f->f_ops->fs_name, "dosfs") == 0) {
258 mnt->dos_fs = df->fs;
259 STAILQ_INSERT_TAIL(&mnt_list, mnt, dos_link);
267 if (mnt->dos_fd >= 0)
278 dos_unmount(const char *dev __unused, void *data)
280 dos_mnt_t *mnt = data;
282 STAILQ_REMOVE(&mnt_list, mnt, dos_mnt, dos_link);
290 * Unmount mounted filesystem
293 dos_unmount_impl(DOS_FS *fs)
306 dos_open(const char *path, struct open_file *fd)
316 dev = devformat((struct devdesc *)fd->f_devdata);
317 STAILQ_FOREACH(mnt, &mnt_list, dos_link) {
318 if (strcmp(dev, mnt->dos_dev) == 0)
323 /* Allocate mount structure, associate with open */
324 if ((fs = calloc(1, sizeof(DOS_FS))) == NULL)
326 if ((err = dos_mount_impl(fs, fd))) {
334 if ((err = namede(fs, path, &de))) {
336 dos_unmount_impl(fs);
340 clus = stclus(fs->fatsz, de);
341 size = cv4(de->size);
343 if ((!(de->attr & FA_DIR) && (!clus != !size)) ||
344 ((de->attr & FA_DIR) && size) ||
345 (clus && !okclus(fs, clus))) {
347 dos_unmount_impl(fs);
350 if ((f = calloc(1, sizeof(DOS_FILE))) == NULL) {
353 dos_unmount_impl(fs);
367 dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid)
370 u_int nb, off, clus, c, cnt, n;
371 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
375 * as ioget() can be called *a lot*, use twiddle here.
376 * also 4 seems to be good value not to slow loading down too much:
377 * with 270MB file (~540k ioget() calls, twiddle can easily waste
382 if ((size = fsize(f->fs, &f->de)) == -1)
384 if (nb > (n = size - f->offset))
387 if ((clus = stclus(f->fs->fatsz, &f->de)))
388 off &= f->fs->bsize - 1;
395 n = bytblk(f->fs, f->offset);
399 if ((err = fatget(f->fs, &c)))
401 if (!okclus(f->fs, c)) {
406 if (!clus || (n = f->fs->bsize - off) > cnt)
408 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
409 secbyt(f->fs->lsndir)) + off, buf, n)))
414 buf = (char *)buf + n;
419 *resid = nbyte - nb + cnt;
424 * Reposition within file
427 dos_seek(struct open_file *fd, off_t offset, int whence)
431 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
433 size = cv4(f->de.size);
449 if (off < 0 || off > size) {
453 f->offset = (u_int)off;
462 dos_close(struct open_file *fd)
464 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
469 dos_unmount_impl(fs);
474 * Return some stat information on a file.
477 dos_stat(struct open_file *fd, struct stat *sb)
479 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
481 /* only important stuff */
482 sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444;
486 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
492 dos_checksum(unsigned char *name, unsigned char *ext)
498 bcopy(ext, buf+8, 3);
500 for (i = 0; i < 11; i++) {
501 x = ((x & 1) << 7) | (x >> 1);
509 dos_readdir(struct open_file *fd, struct dirent *d)
511 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
522 err = dos_read(fd, &dd, sizeof(dd), &res);
525 if (res == sizeof(dd))
527 if (dd.de.name[0] == 0)
530 /* Skip deleted entries */
531 if (dd.de.name[0] == 0xe5)
534 /* Check if directory entry is volume label */
535 if (dd.de.attr & FA_LABEL) {
537 * If volume label set, check if the current entry is
538 * extended entry (FA_XDE) for long file names.
540 if ((dd.de.attr & FA_MASK) == FA_XDE) {
542 * Read through all following extended entries
543 * to get the long file name. 0x40 marks the
544 * last entry containing part of long file name.
546 if (dd.xde.seq & 0x40)
548 else if (dd.xde.seq != xdn - 1 ||
551 x = dd.xde.seq & ~0x40;
552 if (x < 1 || x > 20) {
556 cp_xdnm(fn, &dd.xde);
558 /* skip only volume label entries */
563 x = dos_checksum(dd.de.name, dd.de.ext);
574 d->d_fileno = (dd.de.clus[1] << 8) + dd.de.clus[0];
575 d->d_reclen = sizeof(*d);
576 d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG;
577 memcpy(d->d_name, fn, sizeof(d->d_name));
582 * Parse DOS boot sector
585 parsebs(DOS_FS *fs, DOS_BS *bs)
589 if ((bs->jmp[0] != 0x69 &&
590 bs->jmp[0] != 0xe9 &&
591 (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
592 bs->bpb.media < 0xf0)
594 if (cv2(bs->bpb.secsiz) != SECSIZ)
596 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
598 fs->bsize = secbyt(fs->spc);
599 fs->bshift = ffs(fs->bsize) - 1;
600 if ((fs->spf = cv2(bs->bpb.spf))) {
601 if (bs->bpb.fats != 2)
603 if (!(fs->dirents = cv2(bs->bpb.dirents)))
606 if (!(fs->spf = cv4(bs->bpb.lspf)))
608 if (!bs->bpb.fats || bs->bpb.fats > 16)
610 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
613 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
615 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats;
616 fs->lsndta = fs->lsndir + entsec(fs->dirents);
617 if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs)))
621 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
623 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
624 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
631 * Return directory entry from path
634 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
649 if (!(s = strchr(path, '/')))
651 if ((n = s - path) > 255)
652 return (ENAMETOOLONG);
653 memcpy(name, path, n);
656 if (!(de->attr & FA_DIR))
658 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
666 * Lookup path segment
669 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
671 static DOS_DIR dir[DEPSEC];
674 u_int nsec, lsec, xdn, chk, sec, ent, x;
678 for (ent = 0; ent < 2; ent++)
679 if (!strcasecmp(name, dotstr[ent])) {
683 if (!clus && fs->fatsz == 32)
685 nsec = !clus ? entsec(fs->dirents) : fs->spc;
691 else if (okclus(fs, clus))
692 lsec = blklsn(fs, clus);
695 for (sec = 0; sec < nsec; sec++) {
696 if ((err = ioget(fs->fd, lsec + sec, dir, secbyt(1))))
698 for (ent = 0; ent < DEPSEC; ent++) {
699 if (!*dir[ent].de.name)
701 if (*dir[ent].de.name != 0xe5) {
702 if ((dir[ent].de.attr & FA_MASK) ==
704 x = dir[ent].xde.seq;
705 if (x & 0x40 || (x + 1 == xdn &&
706 dir[ent].xde.chk == chk)) {
708 chk = dir[ent].xde.chk;
711 if (x >= 1 && x <= 20) {
712 cp_xdnm(lfn, &dir[ent].xde);
717 } else if (!(dir[ent].de.attr &
719 if ((ok = xdn == 1)) {
730 ok = !strcasecmp(name,
744 if ((err = fatget(fs, &clus)))
746 if (fatend(fs->fatsz, clus))
753 * Copy name from extended directory entry
756 cp_xdnm(u_char *lfn, DOS_XDE *xde)
762 {offsetof(DOS_XDE, name1), sizeof(xde->name1) / 2},
763 {offsetof(DOS_XDE, name2), sizeof(xde->name2) / 2},
764 {offsetof(DOS_XDE, name3), sizeof(xde->name3) / 2}
769 lfn += 13 * ((xde->seq & ~0x40) - 1);
770 for (n = 0; n < 3; n++)
771 for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
773 if ((c = cv2(p)) && (c < 32 || c > 127))
783 * Copy short filename
786 cp_sfn(u_char *sfn, DOS_DE *de)
792 if (*de->name != ' ') {
793 for (j = 7; de->name[j] == ' '; j--)
795 for (i = 0; i <= j; i++)
797 if (*de->ext != ' ') {
799 for (j = 2; de->ext[j] == ' '; j--)
801 for (i = 0; i <= j; i++)
811 * Return size of file in bytes
814 fsize(DOS_FS *fs, DOS_DE *de)
820 if (!(size = cv4(de->size)) && de->attr & FA_DIR) {
821 if (!(c = cv2(de->clus))) {
822 size = fs->dirents * sizeof(DOS_DE);
824 if ((n = fatcnt(fs, c)) == -1)
826 size = blkbyt(fs, n);
833 * Count number of clusters in chain
836 fatcnt(DOS_FS *fs, u_int c)
840 for (n = 0; okclus(fs, c); n++)
843 return (fatend(fs->fatsz, c) ? n : -1);
847 * Get next cluster in cluster chain. Use in core fat cache unless
848 * the number of current 128K block in FAT has changed.
851 fatget(DOS_FS *fs, u_int *c)
853 u_int val_in, val_out, offset, blknum, nbyte;
854 const u_char *p_entry;
857 /* check input value to prevent overflow in fatoff() */
859 if (val_in & 0xf0000000)
862 /* ensure that current 128K FAT block is cached */
863 offset = fatoff(fs->fatsz, val_in);
864 nbyte = fs->fatsz != 32 ? 2 : 4;
865 if (offset + nbyte > secbyt(fs->spf))
867 blknum = offset / FATBLKSZ;
869 if (offset + nbyte > FATBLKSZ)
871 if (blknum != fs->fatbuf_blknum) {
872 err = dos_read_fatblk(fs, fs->fd, blknum);
876 p_entry = fs->fatbuf + offset;
878 /* extract cluster number from FAT entry */
881 val_out = cv4(p_entry);
882 val_out &= 0x0fffffff;
885 val_out = cv2(p_entry);
888 val_out = cv2(p_entry);
902 * Is cluster an end-of-chain marker?
905 fatend(u_int sz, u_int c)
907 return (c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7));
911 * Offset-based I/O primitive
914 ioread(DOS_FS *fs, u_int offset, void *buf, size_t nbyte)
919 u_char local_buf[SECSIZ];
922 if ((off = offset & (SECSIZ - 1))) {
924 if ((n = SECSIZ - off) > nbyte)
926 err = ioget(fs->fd, bytsec(offset), local_buf,
930 memcpy(s, local_buf + off, n);
935 n = nbyte & (SECSIZ - 1);
937 if ((err = ioget(fs->fd, bytsec(offset), s, nbyte)))
943 err = ioget(fs->fd, bytsec(offset), local_buf,
947 memcpy(s, local_buf, n);
953 * Sector-based I/O primitive
956 ioget(struct open_file *fd, daddr_t lsec, void *buf, size_t size)
961 /* Make sure we get full read or error. */
963 rv = (fd->f_dev->dv_strategy)(fd->f_devdata, F_READ, lsec,
965 if ((rv == 0) && (size != rsize))