1 /*
2 * Copyright (c) 1996, 1998 Robert Nordier
3 * All rights reserved.
4 *
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.
14 *
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.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32 * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
33 * also supports VFAT.
34 */
35
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stddef.h>
39
40 #include "stand.h"
41
42 #include "dosfs.h"
43
44
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);
51
52 struct fs_ops dosfs_fsops = {
53 "dosfs",
54 dos_open,
55 dos_close,
56 dos_read,
57 null_write,
58 dos_seek,
59 dos_stat,
60 dos_readdir
61 };
62
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 */
68
69 /* DOS "BIOS Parameter Block" */
70 typedef struct {
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 */
89 } DOS_BPB;
90
91 /* Initial portion of DOS boot sector */
92 typedef struct {
93 u_char jmp[3]; /* usually 80x86 'jmp' opcode */
94 u_char oem[8]; /* OEM name and version */
95 DOS_BPB bpb; /* BPB */
96 } DOS_BS;
97
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}}
105 };
106
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))
115
116 /* Convert cluster number to offset within filesystem */
117 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
118
119 /* Convert cluster number to logical sector number */
120 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
121
122 /* Convert cluster number to offset within FAT */
123 #define fatoff(sz, c) ((sz) == 12 ? (c) + ((c) >> 1) : \
124 (sz) == 16 ? (c) << 1 : \
125 (c) << 2)
126
127 /* Does cluster number reference a valid data cluster? */
128 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
129
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) | \
133 cv2((de)->clus))
134
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);
148
149 /*
150 * Mount DOS filesystem
151 */
152 static int
dos_mount(DOS_FS * fs,struct open_file * fd)153 dos_mount(DOS_FS *fs, struct open_file *fd)
154 {
155 int err;
156
157 bzero(fs, sizeof(DOS_FS));
158 fs->fd = fd;
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);
163 return(err);
164 }
165 fs->root = dot[0];
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;
172 }
173 return 0;
174 }
175
176 /*
177 * Unmount mounted filesystem
178 */
179 static int
dos_unmount(DOS_FS * fs)180 dos_unmount(DOS_FS *fs)
181 {
182 int err;
183
184 if (fs->links)
185 return(EBUSY);
186 if ((err = dosunmount(fs)))
187 return(err);
188 return 0;
189 }
190
191 /*
192 * Common code shared by dos_mount() and dos_unmount()
193 */
194 static int
dosunmount(DOS_FS * fs)195 dosunmount(DOS_FS *fs)
196 {
197 if (fs->buf)
198 free(fs->buf);
199 free(fs);
200 return(0);
201 }
202
203 /*
204 * Open DOS file
205 */
206 static int
dos_open(const char * path,struct open_file * fd)207 dos_open(const char *path, struct open_file *fd)
208 {
209 DOS_DE *de;
210 DOS_FILE *f;
211 DOS_FS *fs;
212 u_int size, clus;
213 int err = 0;
214
215 /* Allocate mount structure, associate with open */
216 fs = malloc(sizeof(DOS_FS));
217
218 if ((err = dos_mount(fs, fd)))
219 goto out;
220
221 if ((err = namede(fs, path, &de)))
222 goto out;
223
224 clus = stclus(fs->fatsz, de);
225 size = cv4(de->size);
226
227 if ((!(de->attr & FA_DIR) && (!clus != !size)) ||
228 ((de->attr & FA_DIR) && size) ||
229 (clus && !okclus(fs, clus))) {
230 err = EINVAL;
231 goto out;
232 }
233 f = malloc(sizeof(DOS_FILE));
234 bzero(f, sizeof(DOS_FILE));
235 f->fs = fs;
236 fs->links++;
237 f->de = *de;
238 fd->f_fsdata = (void *)f;
239
240 out:
241 return(err);
242 }
243
244 /*
245 * Read from file
246 */
247 static int
dos_read(struct open_file * fd,void * buf,size_t nbyte,size_t * resid)248 dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid)
249 {
250 off_t size;
251 u_int nb, off, clus, c, cnt, n;
252 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
253 int err = 0;
254
255 nb = (u_int)nbyte;
256 if ((size = fsize(f->fs, &f->de)) == -1)
257 return EINVAL;
258 if (nb > (n = size - f->offset))
259 nb = n;
260 off = f->offset;
261 if ((clus = stclus(f->fs->fatsz, &f->de)))
262 off &= f->fs->bsize - 1;
263 c = f->c;
264 cnt = nb;
265 while (cnt) {
266 n = 0;
267 if (!c) {
268 if ((c = clus))
269 n = bytblk(f->fs, f->offset);
270 } else if (!off)
271 n++;
272 while (n--) {
273 if ((err = fatget(f->fs, &c)))
274 goto out;
275 if (!okclus(f->fs, c)) {
276 err = EINVAL;
277 goto out;
278 }
279 }
280 if (!clus || (n = f->fs->bsize - off) > cnt)
281 n = cnt;
282 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
283 secbyt(f->fs->lsndir)) + off,
284 buf, n)))
285 goto out;
286 f->offset += n;
287 f->c = c;
288 off = 0;
289 buf = (char *)buf + n;
290 cnt -= n;
291 }
292 out:
293 if (resid)
294 *resid = nbyte - nb + cnt;
295 return(err);
296 }
297
298 /*
299 * Reposition within file
300 */
301 static off_t
dos_seek(struct open_file * fd,off_t offset,int whence)302 dos_seek(struct open_file *fd, off_t offset, int whence)
303 {
304 off_t off;
305 u_int size;
306 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
307
308 size = cv4(f->de.size);
309 switch (whence) {
310 case SEEK_SET:
311 off = 0;
312 break;
313 case SEEK_CUR:
314 off = f->offset;
315 break;
316 case SEEK_END:
317 off = size;
318 break;
319 default:
320 errno = EINVAL;
321 return(-1);
322 }
323 off += offset;
324 if (off < 0 || off > size) {
325 errno = EINVAL;
326 return(-1);
327 }
328 f->offset = (u_int)off;
329 f->c = 0;
330 return(off);
331 }
332
333 /*
334 * Close open file
335 */
336 static int
dos_close(struct open_file * fd)337 dos_close(struct open_file *fd)
338 {
339 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
340 DOS_FS *fs = f->fs;
341
342 f->fs->links--;
343 free(f);
344 dos_unmount(fs);
345 return 0;
346 }
347
348 /*
349 * Return some stat information on a file.
350 */
351 static int
dos_stat(struct open_file * fd,struct stat * sb)352 dos_stat(struct open_file *fd, struct stat *sb)
353 {
354 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
355
356 /* only important stuff */
357 sb->st_mode = f->de.attr & FA_DIR ? S_IFDIR | 0555 : S_IFREG | 0444;
358 sb->st_nlink = 1;
359 sb->st_uid = 0;
360 sb->st_gid = 0;
361 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
362 return EINVAL;
363 return (0);
364 }
365
366 static int
dos_readdir(struct open_file * fd,struct dirent * d)367 dos_readdir(struct open_file *fd, struct dirent *d)
368 {
369 /* DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; */
370 u_char fn[261];
371 DOS_DIR dd;
372 size_t res;
373 u_int chk, i, x, xdn;
374 int err;
375
376 x = chk = 0;
377 while (1) {
378 xdn = x;
379 x = 0;
380 err = dos_read(fd, &dd, sizeof(dd), &res);
381 if (err)
382 return (err);
383 if (res == sizeof(dd))
384 return (ENOENT);
385 if (dd.de.name[0] == 0)
386 return (ENOENT);
387
388 /* Skip deleted entries */
389 if (dd.de.name[0] == 0xe5)
390 continue;
391
392 /* Check if directory entry is volume label */
393 if (dd.de.attr & FA_LABEL) {
394 /*
395 * If volume label set, check if the current entry is
396 * extended entry (FA_XDE) for long file names.
397 */
398 if ((dd.de.attr & FA_MASK) == FA_XDE) {
399 /*
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.
403 */
404 if (dd.xde.seq & 0x40)
405 chk = dd.xde.chk;
406 else if (dd.xde.seq != xdn - 1 || dd.xde.chk != chk)
407 continue;
408 x = dd.xde.seq & ~0x40;
409 if (x < 1 || x > 20) {
410 x = 0;
411 continue;
412 }
413 cp_xdnm(fn, &dd.xde);
414 } else {
415 /* skip only volume label entries */
416 continue;
417 }
418 } else {
419 if (xdn == 1) {
420 x = 0;
421 for (i = 0; i < 11; i++) {
422 x = ((x & 1) << 7) | (x >> 1);
423 x += dd.de.name[i];
424 x &= 0xff;
425 }
426 if (x == chk)
427 break;
428 } else {
429 cp_sfn(fn, &dd.de);
430 break;
431 }
432 x = 0;
433 }
434 }
435
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));
440 return(0);
441 }
442
443 /*
444 * Parse DOS boot sector
445 */
446 static int
parsebs(DOS_FS * fs,DOS_BS * bs)447 parsebs(DOS_FS *fs, DOS_BS *bs)
448 {
449 u_int sc;
450
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)
455 return EINVAL;
456 if (cv2(bs->bpb.secsiz) != SECSIZ)
457 return EINVAL;
458 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
459 return EINVAL;
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)
464 return EINVAL;
465 if (!(fs->dirents = cv2(bs->bpb.dirents)))
466 return EINVAL;
467 } else {
468 if (!(fs->spf = cv4(bs->bpb.lspf)))
469 return EINVAL;
470 if (!bs->bpb.fats || bs->bpb.fats > 16)
471 return EINVAL;
472 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
473 return EINVAL;
474 }
475 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
476 return EINVAL;
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)))
480 return EINVAL;
481 if (fs->lsndta > sc)
482 return EINVAL;
483 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
484 return EINVAL;
485 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
486 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
487 if (fs->xclus > sc)
488 fs->xclus = sc;
489 return 0;
490 }
491
492 /*
493 * Return directory entry from path
494 */
495 static int
namede(DOS_FS * fs,const char * path,DOS_DE ** dep)496 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
497 {
498 char name[256];
499 DOS_DE *de;
500 char *s;
501 size_t n;
502 int err;
503
504 err = 0;
505 de = &fs->root;
506 while (*path) {
507 while (*path == '/')
508 path++;
509 if (*path == '\0')
510 break;
511 if (!(s = strchr(path, '/')))
512 s = strchr(path, 0);
513 if ((n = s - path) > 255)
514 return ENAMETOOLONG;
515 memcpy(name, path, n);
516 name[n] = 0;
517 path = s;
518 if (!(de->attr & FA_DIR))
519 return ENOTDIR;
520 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
521 return err;
522 }
523 *dep = de;
524 return 0;
525 }
526
527 /*
528 * Lookup path segment
529 */
530 static int
lookup(DOS_FS * fs,u_int clus,const char * name,DOS_DE ** dep)531 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
532 {
533 static DOS_DIR dir[DEPSEC];
534 u_char lfn[261];
535 u_char sfn[13];
536 u_int nsec, lsec, xdn, chk, sec, ent, x;
537 int err, ok, i;
538
539 if (!clus)
540 for (ent = 0; ent < 2; ent++)
541 if (!strcasecmp(name, dotstr[ent])) {
542 *dep = dot + ent;
543 return 0;
544 }
545 if (!clus && fs->fatsz == 32)
546 clus = fs->rdcl;
547 nsec = !clus ? entsec(fs->dirents) : fs->spc;
548 lsec = 0;
549 xdn = chk = 0;
550 for (;;) {
551 if (!clus && !lsec)
552 lsec = fs->lsndir;
553 else if (okclus(fs, clus))
554 lsec = blklsn(fs, clus);
555 else
556 return EINVAL;
557 for (sec = 0; sec < nsec; sec++) {
558 if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
559 return err;
560 for (ent = 0; ent < DEPSEC; ent++) {
561 if (!*dir[ent].de.name)
562 return ENOENT;
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)) {
568 if (x & 0x40) {
569 chk = dir[ent].xde.chk;
570 x &= ~0x40;
571 }
572 if (x >= 1 && x <= 20) {
573 cp_xdnm(lfn, &dir[ent].xde);
574 xdn = x;
575 continue;
576 }
577 }
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;
583 ok = chk == x &&
584 !strcasecmp(name, (const char *)lfn);
585 }
586 if (!ok) {
587 cp_sfn(sfn, &dir[ent].de);
588 ok = !strcasecmp(name, (const char *)sfn);
589 }
590 if (ok) {
591 *dep = &dir[ent].de;
592 return 0;
593 }
594 }
595 }
596 xdn = 0;
597 }
598 }
599 if (!clus)
600 break;
601 if ((err = fatget(fs, &clus)))
602 return err;
603 if (fatend(fs->fatsz, clus))
604 break;
605 }
606 return ENOENT;
607 }
608
609 /*
610 * Copy name from extended directory entry
611 */
612 static void
cp_xdnm(u_char * lfn,DOS_XDE * xde)613 cp_xdnm(u_char *lfn, DOS_XDE *xde)
614 {
615 static struct {
616 u_int off;
617 u_int dim;
618 } ix[3] = {
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}
622 };
623 u_char *p;
624 u_int n, x, c;
625
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;
629 p += 2, x--) {
630 if ((c = cv2(p)) && (c < 32 || c > 127))
631 c = '?';
632 if (!(*lfn++ = c))
633 return;
634 }
635 if (xde->seq & 0x40)
636 *lfn = 0;
637 }
638
639 /*
640 * Copy short filename
641 */
642 static void
cp_sfn(u_char * sfn,DOS_DE * de)643 cp_sfn(u_char *sfn, DOS_DE *de)
644 {
645 u_char *p;
646 int j, i;
647
648 p = sfn;
649 if (*de->name != ' ') {
650 for (j = 7; de->name[j] == ' '; j--);
651 for (i = 0; i <= j; i++)
652 *p++ = de->name[i];
653 if (*de->ext != ' ') {
654 *p++ = '.';
655 for (j = 2; de->ext[j] == ' '; j--);
656 for (i = 0; i <= j; i++)
657 *p++ = de->ext[i];
658 }
659 }
660 *p = 0;
661 if (*sfn == 5)
662 *sfn = 0xe5;
663 }
664
665 /*
666 * Return size of file in bytes
667 */
668 static off_t
fsize(DOS_FS * fs,DOS_DE * de)669 fsize(DOS_FS *fs, DOS_DE *de)
670 {
671 u_long size;
672 u_int c;
673 int n;
674
675 if (!(size = cv4(de->size)) && de->attr & FA_DIR) {
676 if (!(c = cv2(de->clus)))
677 size = fs->dirents * sizeof(DOS_DE);
678 else {
679 if ((n = fatcnt(fs, c)) == -1)
680 return n;
681 size = blkbyt(fs, n);
682 }
683 }
684 return size;
685 }
686
687 /*
688 * Count number of clusters in chain
689 */
690 static int
fatcnt(DOS_FS * fs,u_int c)691 fatcnt(DOS_FS *fs, u_int c)
692 {
693 int n;
694
695 for (n = 0; okclus(fs, c); n++)
696 if (fatget(fs, &c))
697 return -1;
698 return fatend(fs->fatsz, c) ? n : -1;
699 }
700
701 /*
702 * Get next cluster in cluster chain
703 */
704 static int
fatget(DOS_FS * fs,u_int * c)705 fatget(DOS_FS *fs, u_int *c)
706 {
707 u_char buf[4];
708 u_int x;
709 int err;
710
711 err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
712 fs->fatsz != 32 ? 2 : 4);
713 if (err)
714 return err;
715 x = fs->fatsz != 32 ? cv2(buf) : cv4(buf);
716 *c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
717 return 0;
718 }
719
720 /*
721 * Is cluster an end-of-chain marker?
722 */
723 static int
fatend(u_int sz,u_int c)724 fatend(u_int sz, u_int c)
725 {
726 return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
727 }
728
729 /*
730 * Offset-based I/O primitive
731 */
732 static int
ioread(DOS_FS * fs,u_int offset,void * buf,u_int nbyte)733 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
734 {
735 char *s;
736 u_int off, n;
737 int err;
738
739 s = buf;
740 if ((off = offset & (SECSIZ - 1))) {
741 offset -= off;
742 if ((err = iobuf(fs, bytsec(offset))))
743 return err;
744 offset += SECSIZ;
745 if ((n = SECSIZ - off) > nbyte)
746 n = nbyte;
747 memcpy(s, fs->buf + off, n);
748 s += n;
749 nbyte -= n;
750 }
751 n = nbyte & (SECSIZ - 1);
752 if (nbyte -= n) {
753 if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
754 return err;
755 offset += nbyte;
756 s += nbyte;
757 }
758 if (n) {
759 if ((err = iobuf(fs, bytsec(offset))))
760 return err;
761 memcpy(s, fs->buf, n);
762 }
763 return 0;
764 }
765
766 /*
767 * Buffered sector-based I/O primitive
768 */
769 static int
iobuf(DOS_FS * fs,u_int lsec)770 iobuf(DOS_FS *fs, u_int lsec)
771 {
772 int err;
773
774 if (fs->bufsec != lsec) {
775 if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
776 return err;
777 fs->bufsec = lsec;
778 }
779 return 0;
780 }
781
782 /*
783 * Sector-based I/O primitive
784 */
785 static int
ioget(struct open_file * fd,u_int lsec,void * buf,u_int nsec)786 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
787 {
788 int err;
789
790 twiddle(1);
791 if ((err = (fd->f_dev->dv_strategy)(fd->f_devdata, F_READ, lsec,
792 secbyt(nsec), buf, NULL)))
793 return(err);
794 return(0);
795 }
796