adds fkvm_inject_virq syscall: magic changes
[freebsd-src/fkvm-freebsd.git] / lib / libstand / dosfs.c
blob161d3300837619322b73df136f8ffadfa279437b
1 /*
2 * Copyright (c) 1996, 1998 Robert Nordier
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
13 * distribution.
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,
33 * also supports VFAT.
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stddef.h>
40 #include "stand.h"
42 #include "dosfs.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);
51 struct fs_ops dosfs_fsops = {
52 "dosfs",
53 dos_open,
54 dos_close,
55 dos_read,
56 null_write,
57 dos_seek,
58 dos_stat,
59 null_readdir
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 */
68 /* DOS "BIOS Parameter Block" */
69 typedef struct {
70 u_char secsiz[2]; /* sector size */
71 u_char spc; /* sectors per cluster */
72 u_char ressec[2]; /* reserved sectors */
73 u_char fats; /* FATs */
74 u_char dirents[2]; /* root directory entries */
75 u_char secs[2]; /* total sectors */
76 u_char media; /* media descriptor */
77 u_char spf[2]; /* sectors per FAT */
78 u_char spt[2]; /* sectors per track */
79 u_char heads[2]; /* drive heads */
80 u_char hidsec[4]; /* hidden sectors */
81 u_char lsecs[4]; /* huge sectors */
82 u_char lspf[4]; /* huge sectors per FAT */
83 u_char xflg[2]; /* flags */
84 u_char vers[2]; /* filesystem version */
85 u_char rdcl[4]; /* root directory start cluster */
86 u_char infs[2]; /* filesystem info sector */
87 u_char bkbs[2]; /* backup boot sector */
88 } DOS_BPB;
90 /* Initial portion of DOS boot sector */
91 typedef struct {
92 u_char jmp[3]; /* usually 80x86 'jmp' opcode */
93 u_char oem[8]; /* OEM name and version */
94 DOS_BPB bpb; /* BPB */
95 } DOS_BS;
97 /* Supply missing "." and ".." root directory entries */
98 static const char *const dotstr[2] = {".", ".."};
99 static DOS_DE dot[2] = {
100 {". ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
101 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
102 {".. ", " ", FA_DIR, {0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
103 {0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
106 /* The usual conversion macros to avoid multiplication and division */
107 #define bytsec(n) ((n) >> SSHIFT)
108 #define secbyt(s) ((s) << SSHIFT)
109 #define entsec(e) ((e) >> DSHIFT)
110 #define bytblk(fs, n) ((n) >> (fs)->bshift)
111 #define blkbyt(fs, b) ((b) << (fs)->bshift)
112 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
113 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
115 /* Convert cluster number to offset within filesystem */
116 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
118 /* Convert cluster number to logical sector number */
119 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
121 /* Convert cluster number to offset within FAT */
122 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
123 (sz) == 16 ? (c) << 1 : \
124 (c) << 2)
126 /* Does cluster number reference a valid data cluster? */
127 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
129 /* Get start cluster from directory entry */
130 #define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \
131 ((u_int)cv2((de)->dex.h_clus) << 16) | \
132 cv2((de)->clus))
134 static int dosunmount(DOS_FS *);
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 *, u_int);
145 static int iobuf(DOS_FS *, u_int);
146 static int ioget(struct open_file *, u_int, void *, u_int);
149 * Mount DOS filesystem
151 static int
152 dos_mount(DOS_FS *fs, struct open_file *fd)
154 int err;
156 bzero(fs, sizeof(DOS_FS));
157 fs->fd = fd;
158 if ((err = !(fs->buf = malloc(SECSIZ)) ? errno : 0) ||
159 (err = ioget(fs->fd, 0, fs->buf, 1)) ||
160 (err = parsebs(fs, (DOS_BS *)fs->buf))) {
161 (void)dosunmount(fs);
162 return(err);
164 return 0;
168 * Unmount mounted filesystem
170 static int
171 dos_unmount(DOS_FS *fs)
173 int err;
175 if (fs->links)
176 return(EBUSY);
177 if ((err = dosunmount(fs)))
178 return(err);
179 return 0;
183 * Common code shared by dos_mount() and dos_unmount()
185 static int
186 dosunmount(DOS_FS *fs)
188 if (fs->buf)
189 free(fs->buf);
190 free(fs);
191 return(0);
195 * Open DOS file
197 static int
198 dos_open(const char *path, struct open_file *fd)
200 DOS_DE *de;
201 DOS_FILE *f;
202 DOS_FS *fs;
203 u_int size, clus;
204 int err = 0;
206 /* Allocate mount structure, associate with open */
207 fs = malloc(sizeof(DOS_FS));
209 if ((err = dos_mount(fs, fd)))
210 goto out;
212 if ((err = namede(fs, path, &de)))
213 goto out;
215 clus = stclus(fs->fatsz, de);
216 size = cv4(de->size);
218 if ((!(de->attr & FA_DIR) && (!clus != !size)) ||
219 ((de->attr & FA_DIR) && size) ||
220 (clus && !okclus(fs, clus))) {
221 err = EINVAL;
222 goto out;
224 f = malloc(sizeof(DOS_FILE));
225 bzero(f, sizeof(DOS_FILE));
226 f->fs = fs;
227 fs->links++;
228 f->de = *de;
229 fd->f_fsdata = (void *)f;
231 out:
232 return(err);
236 * Read from file
238 static int
239 dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid)
241 off_t size;
242 u_int nb, off, clus, c, cnt, n;
243 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
244 int err = 0;
246 nb = (u_int)nbyte;
247 if ((size = fsize(f->fs, &f->de)) == -1)
248 return EINVAL;
249 if (nb > (n = size - f->offset))
250 nb = n;
251 off = f->offset;
252 if ((clus = stclus(f->fs->fatsz, &f->de)))
253 off &= f->fs->bsize - 1;
254 c = f->c;
255 cnt = nb;
256 while (cnt) {
257 n = 0;
258 if (!c) {
259 if ((c = clus))
260 n = bytblk(f->fs, f->offset);
261 } else if (!off)
262 n++;
263 while (n--) {
264 if ((err = fatget(f->fs, &c)))
265 goto out;
266 if (!okclus(f->fs, c)) {
267 err = EINVAL;
268 goto out;
271 if (!clus || (n = f->fs->bsize - off) > cnt)
272 n = cnt;
273 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
274 secbyt(f->fs->lsndir)) + off,
275 buf, n)))
276 goto out;
277 f->offset += n;
278 f->c = c;
279 off = 0;
280 buf = (char *)buf + n;
281 cnt -= n;
283 out:
284 if (resid)
285 *resid = nbyte - nb + cnt;
286 return(err);
290 * Reposition within file
292 static off_t
293 dos_seek(struct open_file *fd, off_t offset, int whence)
295 off_t off;
296 u_int size;
297 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
299 size = cv4(f->de.size);
300 switch (whence) {
301 case SEEK_SET:
302 off = 0;
303 break;
304 case SEEK_CUR:
305 off = f->offset;
306 break;
307 case SEEK_END:
308 off = size;
309 break;
310 default:
311 errno = EINVAL;
312 return(-1);
314 off += offset;
315 if (off < 0 || off > size) {
316 errno = EINVAL;
317 return(-1);
319 f->offset = (u_int)off;
320 f->c = 0;
321 return(off);
325 * Close open file
327 static int
328 dos_close(struct open_file *fd)
330 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
331 DOS_FS *fs = f->fs;
333 f->fs->links--;
334 free(f);
335 dos_unmount(fs);
336 return 0;
340 * Return some stat information on a file.
342 static int
343 dos_stat(struct open_file *fd, struct stat *sb)
345 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
347 /* only important stuff */
348 sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444;
349 sb->st_nlink = 1;
350 sb->st_uid = 0;
351 sb->st_gid = 0;
352 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
353 return EINVAL;
354 return (0);
358 * Parse DOS boot sector
360 static int
361 parsebs(DOS_FS *fs, DOS_BS *bs)
363 u_int sc;
365 if ((bs->jmp[0] != 0x69 &&
366 bs->jmp[0] != 0xe9 &&
367 (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
368 bs->bpb.media < 0xf0)
369 return EINVAL;
370 if (cv2(bs->bpb.secsiz) != SECSIZ)
371 return EINVAL;
372 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
373 return EINVAL;
374 fs->bsize = secbyt(fs->spc);
375 fs->bshift = ffs(fs->bsize) - 1;
376 if ((fs->spf = cv2(bs->bpb.spf))) {
377 if (bs->bpb.fats != 2)
378 return EINVAL;
379 if (!(fs->dirents = cv2(bs->bpb.dirents)))
380 return EINVAL;
381 } else {
382 if (!(fs->spf = cv4(bs->bpb.lspf)))
383 return EINVAL;
384 if (!bs->bpb.fats || bs->bpb.fats > 16)
385 return EINVAL;
386 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
387 return EINVAL;
389 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
390 return EINVAL;
391 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats;
392 fs->lsndta = fs->lsndir + entsec(fs->dirents);
393 if (!(sc = cv2(bs->bpb.secs)) && !(sc = cv4(bs->bpb.lsecs)))
394 return EINVAL;
395 if (fs->lsndta > sc)
396 return EINVAL;
397 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
398 return EINVAL;
399 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
400 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
401 if (fs->xclus > sc)
402 fs->xclus = sc;
403 return 0;
407 * Return directory entry from path
409 static int
410 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
412 char name[256];
413 DOS_DE *de;
414 char *s;
415 size_t n;
416 int err;
418 err = 0;
419 de = dot;
420 if (*path == '/')
421 path++;
422 while (*path) {
423 if (!(s = strchr(path, '/')))
424 s = strchr(path, 0);
425 if ((n = s - path) > 255)
426 return ENAMETOOLONG;
427 memcpy(name, path, n);
428 name[n] = 0;
429 path = s;
430 if (!(de->attr & FA_DIR))
431 return ENOTDIR;
432 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
433 return err;
434 if (*path == '/')
435 path++;
437 *dep = de;
438 return 0;
442 * Lookup path segment
444 static int
445 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
447 static DOS_DIR dir[DEPSEC];
448 u_char lfn[261];
449 u_char sfn[13];
450 u_int nsec, lsec, xdn, chk, sec, ent, x;
451 int err, ok, i;
453 if (!clus)
454 for (ent = 0; ent < 2; ent++)
455 if (!strcasecmp(name, dotstr[ent])) {
456 *dep = dot + ent;
457 return 0;
459 if (!clus && fs->fatsz == 32)
460 clus = fs->rdcl;
461 nsec = !clus ? entsec(fs->dirents) : fs->spc;
462 lsec = 0;
463 xdn = chk = 0;
464 for (;;) {
465 if (!clus && !lsec)
466 lsec = fs->lsndir;
467 else if (okclus(fs, clus))
468 lsec = blklsn(fs, clus);
469 else
470 return EINVAL;
471 for (sec = 0; sec < nsec; sec++) {
472 if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
473 return err;
474 for (ent = 0; ent < DEPSEC; ent++) {
475 if (!*dir[ent].de.name)
476 return ENOENT;
477 if (*dir[ent].de.name != 0xe5) {
478 if ((dir[ent].de.attr & FA_MASK) == FA_XDE) {
479 x = dir[ent].xde.seq;
480 if (x & 0x40 || (x + 1 == xdn &&
481 dir[ent].xde.chk == chk)) {
482 if (x & 0x40) {
483 chk = dir[ent].xde.chk;
484 x &= ~0x40;
486 if (x >= 1 && x <= 20) {
487 cp_xdnm(lfn, &dir[ent].xde);
488 xdn = x;
489 continue;
492 } else if (!(dir[ent].de.attr & FA_LABEL)) {
493 if ((ok = xdn == 1)) {
494 for (x = 0, i = 0; i < 11; i++)
495 x = ((((x & 1) << 7) | (x >> 1)) +
496 dir[ent].de.name[i]) & 0xff;
497 ok = chk == x &&
498 !strcasecmp(name, (const char *)lfn);
500 if (!ok) {
501 cp_sfn(sfn, &dir[ent].de);
502 ok = !strcasecmp(name, (const char *)sfn);
504 if (ok) {
505 *dep = &dir[ent].de;
506 return 0;
510 xdn = 0;
513 if (!clus)
514 break;
515 if ((err = fatget(fs, &clus)))
516 return err;
517 if (fatend(fs->fatsz, clus))
518 break;
520 return ENOENT;
524 * Copy name from extended directory entry
526 static void
527 cp_xdnm(u_char *lfn, DOS_XDE *xde)
529 static struct {
530 u_int off;
531 u_int dim;
532 } ix[3] = {
533 {offsetof(DOS_XDE, name1), sizeof(xde->name1) / 2},
534 {offsetof(DOS_XDE, name2), sizeof(xde->name2) / 2},
535 {offsetof(DOS_XDE, name3), sizeof(xde->name3) / 2}
537 u_char *p;
538 u_int n, x, c;
540 lfn += 13 * ((xde->seq & ~0x40) - 1);
541 for (n = 0; n < 3; n++)
542 for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
543 p += 2, x--) {
544 if ((c = cv2(p)) && (c < 32 || c > 127))
545 c = '?';
546 if (!(*lfn++ = c))
547 return;
549 if (xde->seq & 0x40)
550 *lfn = 0;
554 * Copy short filename
556 static void
557 cp_sfn(u_char *sfn, DOS_DE *de)
559 u_char *p;
560 int j, i;
562 p = sfn;
563 if (*de->name != ' ') {
564 for (j = 7; de->name[j] == ' '; j--);
565 for (i = 0; i <= j; i++)
566 *p++ = de->name[i];
567 if (*de->ext != ' ') {
568 *p++ = '.';
569 for (j = 2; de->ext[j] == ' '; j--);
570 for (i = 0; i <= j; i++)
571 *p++ = de->ext[i];
574 *p = 0;
575 if (*sfn == 5)
576 *sfn = 0xe5;
580 * Return size of file in bytes
582 static off_t
583 fsize(DOS_FS *fs, DOS_DE *de)
585 u_long size;
586 u_int c;
587 int n;
589 if (!(size = cv4(de->size)) && de->attr & FA_DIR) {
590 if (!(c = cv2(de->clus)))
591 size = fs->dirents * sizeof(DOS_DE);
592 else {
593 if ((n = fatcnt(fs, c)) == -1)
594 return n;
595 size = blkbyt(fs, n);
598 return size;
602 * Count number of clusters in chain
604 static int
605 fatcnt(DOS_FS *fs, u_int c)
607 int n;
609 for (n = 0; okclus(fs, c); n++)
610 if (fatget(fs, &c))
611 return -1;
612 return fatend(fs->fatsz, c) ? n : -1;
616 * Get next cluster in cluster chain
618 static int
619 fatget(DOS_FS *fs, u_int *c)
621 u_char buf[4];
622 u_int x;
623 int err;
625 err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
626 fs->fatsz != 32 ? 2 : 4);
627 if (err)
628 return err;
629 x = fs->fatsz != 32 ? cv2(buf) : cv4(buf);
630 *c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
631 return 0;
635 * Is cluster an end-of-chain marker?
637 static int
638 fatend(u_int sz, u_int c)
640 return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
644 * Offset-based I/O primitive
646 static int
647 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
649 char *s;
650 u_int off, n;
651 int err;
653 s = buf;
654 if ((off = offset & (SECSIZ - 1))) {
655 offset -= off;
656 if ((err = iobuf(fs, bytsec(offset))))
657 return err;
658 offset += SECSIZ;
659 if ((n = SECSIZ - off) > nbyte)
660 n = nbyte;
661 memcpy(s, fs->buf + off, n);
662 s += n;
663 nbyte -= n;
665 n = nbyte & (SECSIZ - 1);
666 if (nbyte -= n) {
667 if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
668 return err;
669 offset += nbyte;
670 s += nbyte;
672 if (n) {
673 if ((err = iobuf(fs, bytsec(offset))))
674 return err;
675 memcpy(s, fs->buf, n);
677 return 0;
681 * Buffered sector-based I/O primitive
683 static int
684 iobuf(DOS_FS *fs, u_int lsec)
686 int err;
688 if (fs->bufsec != lsec) {
689 if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
690 return err;
691 fs->bufsec = lsec;
693 return 0;
697 * Sector-based I/O primitive
699 static int
700 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
702 int err;
704 if ((err = (fd->f_dev->dv_strategy)(fd->f_devdata, F_READ, lsec,
705 secbyt(nsec), buf, NULL)))
706 return(err);
707 return(0);