xref: /netbsd-src/sys/lib/libsa/dosfs.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: dosfs.c,v 1.10 2006/01/25 18:27:23 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1996, 1998 Robert Nordier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
32  * also supports VFAT.
33  */
34 
35 /*
36  * XXX DOES NOT SUPPORT:
37  *
38  *	LIBSA_FS_SINGLECOMPONENT
39  */
40 
41 #include <sys/param.h>
42 
43 #include <fs/msdosfs/bpb.h>
44 #include <fs/msdosfs/direntry.h>
45 
46 #ifdef _STANDALONE
47 #include <lib/libkern/libkern.h>
48 #else
49 #include <string.h>
50 #include <stddef.h>
51 #endif
52 
53 #include "stand.h"
54 #include "dosfs.h"
55 
56 #define SECSIZ  512		/* sector size */
57 #define SSHIFT    9		/* SECSIZ shift */
58 #define DEPSEC   16		/* directory entries per sector */
59 #define DSHIFT    4		/* DEPSEC shift */
60 #define LOCLUS    2		/* lowest cluster number */
61 
62 typedef union {
63 	struct direntry de;	/* standard directory entry */
64 	struct winentry xde;	/* extended directory entry */
65 } DOS_DIR;
66 
67 typedef struct {
68 	struct open_file *fd;	/* file descriptor */
69 	u_char *buf;		/* buffer */
70 	u_int   bufsec;		/* buffered sector */
71 	u_int   links;		/* active links to structure */
72 	u_int   spc;		/* sectors per cluster */
73 	u_int   bsize;		/* cluster size in bytes */
74 	u_int   bshift;		/* cluster conversion shift */
75 	u_int   dirents;	/* root directory entries */
76 	u_int   spf;		/* sectors per fat */
77 	u_int   rdcl;		/* root directory start cluster */
78 	u_int   lsnfat;		/* start of fat */
79 	u_int   lsndir;		/* start of root dir */
80 	u_int   lsndta;		/* start of data area */
81 	u_int   fatsz;		/* FAT entry size */
82 	u_int   xclus;		/* maximum cluster number */
83 } DOS_FS;
84 
85 typedef struct {
86 	DOS_FS *fs;		/* associated filesystem */
87 	struct direntry de;	/* directory entry */
88 	u_int   offset;		/* current offset */
89 	u_int   c;		/* last cluster read */
90 } DOS_FILE;
91 
92 /* Initial portion of DOS boot sector */
93 typedef struct {
94 	u_char  jmp[3];		/* usually 80x86 'jmp' opcode */
95 	u_char  oem[8];		/* OEM name and version */
96 	struct byte_bpb710 bpb;	/* BPB */
97 } DOS_BS;
98 
99 /* Supply missing "." and ".." root directory entries */
100 static const char *const dotstr[2] = {".", ".."};
101 static const struct direntry dot[2] = {
102 	{".       ", "   ", ATTR_DIRECTORY,
103 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
104 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
105 
106 	{"..      ", "   ", ATTR_DIRECTORY,
107 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
108 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
109 };
110 
111 /* The usual conversion macros to avoid multiplication and division */
112 #define bytsec(n)      ((n) >> SSHIFT)
113 #define secbyt(s)      ((s) << SSHIFT)
114 #define entsec(e)      ((e) >> DSHIFT)
115 #define bytblk(fs, n)  ((n) >> (fs)->bshift)
116 #define blkbyt(fs, b)  ((b) << (fs)->bshift)
117 #define secblk(fs, s)  ((s) >> ((fs)->bshift - SSHIFT))
118 #define blksec(fs, b)  ((b) << ((fs)->bshift - SSHIFT))
119 
120 /* Convert cluster number to offset within filesystem */
121 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
122 
123 /* Convert cluster number to logical sector number */
124 #define blklsn(fs, b)  ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
125 
126 /* Convert cluster number to offset within FAT */
127 #define fatoff(sz, c)  ((sz) == 12 ? (c) + ((c) >> 1) :  \
128                         (sz) == 16 ? (c) << 1 :          \
129 			(c) << 2)
130 
131 /* Does cluster number reference a valid data cluster? */
132 #define okclus(fs, c)  ((c) >= LOCLUS && (c) <= (fs)->xclus)
133 
134 /* Get start cluster from directory entry */
135 #define stclus(sz, de)  ((sz) != 32 ? (u_int)getushort((de)->deStartCluster) : \
136                          ((u_int)getushort((de)->deHighClust) << 16) |  \
137 			 (u_int)getushort((de)->deStartCluster))
138 
139 static int dosunmount(DOS_FS *);
140 static int parsebs(DOS_FS *, DOS_BS *);
141 static int namede(DOS_FS *, const char *, const struct direntry **);
142 static int lookup(DOS_FS *, u_int, const char *, const struct direntry **);
143 static void cp_xdnm(u_char *, struct winentry *);
144 static void cp_sfn(u_char *, struct direntry *);
145 static off_t fsize(DOS_FS *, struct direntry *);
146 static int fatcnt(DOS_FS *, u_int);
147 static int fatget(DOS_FS *, u_int *);
148 static int fatend(u_int, u_int);
149 static int ioread(DOS_FS *, u_int, void *, u_int);
150 static int iobuf(DOS_FS *, u_int);
151 static int ioget(struct open_file *, u_int, void *, u_int);
152 
153 /*
154  * Mount DOS filesystem
155  */
156 static int
157 dos_mount(DOS_FS * fs, struct open_file * fd)
158 {
159 	int     err;
160 
161 	bzero(fs, sizeof(DOS_FS));
162 	fs->fd = fd;
163 	if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
164 	    (err = ioget(fs->fd, 0, fs->buf, 1)) ||
165 	    (err = parsebs(fs, (DOS_BS *) fs->buf))) {
166 		(void) dosunmount(fs);
167 		return (err);
168 	}
169 	return 0;
170 }
171 
172 #ifndef LIBSA_NO_FS_CLOSE
173 /*
174  * Unmount mounted filesystem
175  */
176 static int
177 dos_unmount(DOS_FS * fs)
178 {
179 	int     err;
180 
181 	if (fs->links)
182 		return (EBUSY);
183 	if ((err = dosunmount(fs)))
184 		return (err);
185 	return 0;
186 }
187 #endif
188 
189 /*
190  * Common code shared by dos_mount() and dos_unmount()
191  */
192 static int
193 dosunmount(DOS_FS * fs)
194 {
195 	if (fs->buf)
196 		dealloc(fs->buf, SECSIZ);
197 	dealloc(fs, sizeof(DOS_FS));
198 	return (0);
199 }
200 
201 /*
202  * Open DOS file
203  */
204 int
205 dosfs_open(const char *path, struct open_file *fd)
206 {
207 	const struct direntry *de;
208 	DOS_FILE *f;
209 	DOS_FS *fs;
210 	u_int   size, clus;
211 	int     err = 0;
212 
213 	/* Allocate mount structure, associate with open */
214 	fs = alloc(sizeof(DOS_FS));
215 
216 	if ((err = dos_mount(fs, fd)))
217 		goto out;
218 
219 	if ((err = namede(fs, path, &de)))
220 		goto out;
221 
222 	clus = stclus(fs->fatsz, de);
223 	size = getulong(de->deFileSize);
224 
225 	if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
226 	    ((de->deAttributes & ATTR_DIRECTORY) && size) ||
227 	    (clus && !okclus(fs, clus))) {
228 		err = EINVAL;
229 		goto out;
230 	}
231 	f = alloc(sizeof(DOS_FILE));
232 	bzero(f, sizeof(DOS_FILE));
233 	f->fs = fs;
234 	fs->links++;
235 	f->de = *de;
236 	fd->f_fsdata = (void *) f;
237 
238 out:
239 	return (err);
240 }
241 
242 /*
243  * Read from file
244  */
245 int
246 dosfs_read(struct open_file * fd, void *vbuf, size_t nbyte, size_t * resid)
247 {
248 	off_t   size;
249 	u_int8_t *buf = vbuf;
250 	u_int   nb, off, clus, c, cnt, n;
251 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
252 	int     err = 0;
253 
254 	nb = (u_int) nbyte;
255 	if ((size = fsize(f->fs, &f->de)) == -1)
256 		return EINVAL;
257 	if (nb > (n = size - f->offset))
258 		nb = n;
259 	off = f->offset;
260 	if ((clus = stclus(f->fs->fatsz, &f->de)))
261 		off &= f->fs->bsize - 1;
262 	c = f->c;
263 	cnt = nb;
264 	while (cnt) {
265 		n = 0;
266 		if (!c) {
267 			if ((c = clus))
268 				n = bytblk(f->fs, f->offset);
269 		} else if (!off)
270 			n++;
271 		while (n--) {
272 			if ((err = fatget(f->fs, &c)))
273 				goto out;
274 			if (!okclus(f->fs, c)) {
275 				err = EINVAL;
276 				goto out;
277 			}
278 		}
279 		if (!clus || (n = f->fs->bsize - off) > cnt)
280 			n = cnt;
281 		if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
282 				secbyt(f->fs->lsndir)) + off,
283 			    buf, n)))
284 			goto out;
285 		f->offset += n;
286 		f->c = c;
287 		off = 0;
288 		buf += n;
289 		cnt -= n;
290 	}
291 out:
292 	if (resid)
293 		*resid = nbyte - nb + cnt;
294 	return (err);
295 }
296 
297 #ifndef LIBSA_NO_FS_WRITE
298 /*
299  * Not implemented.
300  */
301 int
302 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
303 {
304 
305 	return (EROFS);
306 }
307 #endif /* !LIBSA_NO_FS_WRITE */
308 
309 #ifndef LIBSA_NO_FS_SEEK
310 /*
311  * Reposition within file
312  */
313 off_t
314 dosfs_seek(struct open_file * fd, off_t offset, int whence)
315 {
316 	off_t   off;
317 	u_int   size;
318 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
319 
320 	size = getulong(f->de.deFileSize);
321 	switch (whence) {
322 	case SEEK_SET:
323 		off = 0;
324 		break;
325 	case SEEK_CUR:
326 		off = f->offset;
327 		break;
328 	case SEEK_END:
329 		off = size;
330 		break;
331 	default:
332 		return (-1);
333 	}
334 	off += offset;
335 	if (off < 0 || off > size)
336 		return (-1);
337 	f->offset = (u_int) off;
338 	f->c = 0;
339 	return (off);
340 }
341 #endif /* !LIBSA_NO_FS_SEEK */
342 
343 #ifndef LIBSA_NO_FS_CLOSE
344 /*
345  * Close open file
346  */
347 int
348 dosfs_close(struct open_file * fd)
349 {
350 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
351 	DOS_FS *fs = f->fs;
352 
353 	f->fs->links--;
354 	dealloc(f, sizeof(DOS_FILE));
355 	dos_unmount(fs);
356 	return 0;
357 }
358 #endif /* !LIBSA_NO_FS_CLOSE */
359 
360 /*
361  * Return some stat information on a file.
362  */
363 int
364 dosfs_stat(struct open_file * fd, struct stat * sb)
365 {
366 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
367 
368 	/* only important stuff */
369 	sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
370 	    (S_IFDIR | 0555) : (S_IFREG | 0444);
371 	sb->st_nlink = 1;
372 	sb->st_uid = 0;
373 	sb->st_gid = 0;
374 	if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
375 		return EINVAL;
376 	return (0);
377 }
378 
379 /*
380  * Parse DOS boot sector
381  */
382 static int
383 parsebs(DOS_FS * fs, DOS_BS * bs)
384 {
385 	u_int   sc;
386 
387 	if ((bs->jmp[0] != 0x69 &&
388 		bs->jmp[0] != 0xe9 &&
389 		(bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
390 	    bs->bpb.bpbMedia < 0xf0)
391 		return EINVAL;
392 	if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
393 		return EINVAL;
394 	if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
395 		return EINVAL;
396 	fs->bsize = secbyt(fs->spc);
397 	fs->bshift = ffs(fs->bsize) - 1;
398 	if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
399 		if (bs->bpb.bpbFATs != 2)
400 			return EINVAL;
401 		if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
402 			return EINVAL;
403 	} else {
404 		if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
405 			return EINVAL;
406 		if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
407 			return EINVAL;
408 		if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
409 			return EINVAL;
410 	}
411 	if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
412 		return EINVAL;
413 	fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
414 	fs->lsndta = fs->lsndir + entsec(fs->dirents);
415 	if (!(sc = getushort(bs->bpb.bpbSectors)) &&
416 	    !(sc = getulong(bs->bpb.bpbHugeSectors)))
417 		return EINVAL;
418 	if (fs->lsndta > sc)
419 		return EINVAL;
420 	if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
421 		return EINVAL;
422 	fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
423 	sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
424 	if (fs->xclus > sc)
425 		fs->xclus = sc;
426 	return 0;
427 }
428 
429 /*
430  * Return directory entry from path
431  */
432 static int
433 namede(DOS_FS * fs, const char *path, const struct direntry ** dep)
434 {
435 	char    name[256];
436 	const struct direntry *de;
437 	char   *s;
438 	size_t  n;
439 	int     err;
440 
441 	err = 0;
442 	de = dot;
443 	if (*path == '/')
444 		path++;
445 	while (*path) {
446 		if (!(s = strchr(path, '/')))
447 			s = strchr(path, 0);
448 		if ((n = s - path) > 255)
449 			return ENAMETOOLONG;
450 		memcpy(name, path, n);
451 		name[n] = 0;
452 		path = s;
453 		if (!(de->deAttributes & ATTR_DIRECTORY))
454 			return ENOTDIR;
455 		if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
456 			return err;
457 		if (*path == '/')
458 			path++;
459 	}
460 	*dep = de;
461 	return 0;
462 }
463 
464 /*
465  * Lookup path segment
466  */
467 static int
468 lookup(DOS_FS * fs, u_int clus, const char *name, const struct direntry ** dep)
469 {
470 	static DOS_DIR *dir = NULL;
471 	u_char  lfn[261];
472 	u_char  sfn[13];
473 	u_int   nsec, lsec, xdn, chk, sec, ent, x;
474 	int     err = 0, ok, i;
475 
476 	if (!clus)
477 		for (ent = 0; ent < 2; ent++)
478 			if (!strcasecmp(name, dotstr[ent])) {
479 				*dep = dot + ent;
480 				return 0;
481 			}
482 
483 	if (dir == NULL) {
484 		dir = alloc(sizeof(DOS_DIR) * DEPSEC);
485 		if (dir == NULL)
486 			return (ENOMEM);
487 	}
488 
489 	if (!clus && fs->fatsz == 32)
490 		clus = fs->rdcl;
491 	nsec = !clus ? entsec(fs->dirents) : fs->spc;
492 	lsec = 0;
493 	xdn = chk = 0;
494 	for (;;) {
495 		if (!clus && !lsec)
496 			lsec = fs->lsndir;
497 		else if (okclus(fs, clus))
498 			lsec = blklsn(fs, clus);
499 		else {
500 			err = EINVAL;
501 			goto out;
502 		}
503 		for (sec = 0; sec < nsec; sec++) {
504 			if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
505 				goto out;
506 			for (ent = 0; ent < DEPSEC; ent++) {
507 				if (!*dir[ent].de.deName) {
508 					err = ENOENT;
509 					goto out;
510 				}
511 				if (*dir[ent].de.deName != 0xe5) {
512 					if (dir[ent].de.deAttributes ==
513 					    ATTR_WIN95) {
514 						x = dir[ent].xde.weCnt;
515 						if (x & WIN_LAST ||
516 						    (x + 1 == xdn &&
517 						     dir[ent].xde.weChksum ==
518 						     chk)) {
519 							if (x & WIN_LAST) {
520 								chk = dir[ent].xde.weChksum;
521 								x &= WIN_CNT;
522 							}
523 							if (x >= 1 && x <= 20) {
524 								cp_xdnm(lfn, &dir[ent].xde);
525 								xdn = x;
526 								continue;
527 							}
528 						}
529 					} else if (!(dir[ent].de.deAttributes &
530 						     ATTR_VOLUME)) {
531 						if ((ok = xdn == 1)) {
532 							for (x = 0, i = 0;
533 							     i < 11; i++)
534 								x = ((((x & 1) << 7) | (x >> 1)) +
535 								    dir[ent].de.deName[i]) & 0xff;
536 							ok = chk == x &&
537 							    !strcasecmp(name, (const char *) lfn);
538 						}
539 						if (!ok) {
540 							cp_sfn(sfn, &dir[ent].de);
541 							ok = !strcasecmp(name, (const char *) sfn);
542 						}
543 						if (ok) {
544 							*dep = &dir[ent].de;
545 							goto out2;
546 						}
547 					}
548 				}
549 				xdn = 0;
550 			}
551 		}
552 		if (!clus)
553 			break;
554 		if ((err = fatget(fs, &clus)))
555 			goto out;
556 		if (fatend(fs->fatsz, clus))
557 			break;
558 	}
559 	err = ENOENT;
560  out:
561 	dealloc(dir, sizeof(DOS_DIR) * DEPSEC);
562 	dir = NULL;
563  out2:
564 	return (err);
565 }
566 
567 /*
568  * Copy name from extended directory entry
569  */
570 static void
571 cp_xdnm(u_char * lfn, struct winentry * xde)
572 {
573 	static const struct {
574 		u_int   off;
575 		u_int   dim;
576 	} ix[3] = {
577 		{ offsetof(struct winentry, wePart1),
578 		    sizeof(xde->wePart1) / 2 },
579 		{ offsetof(struct winentry, wePart2),
580 		    sizeof(xde->wePart2) / 2 },
581 		{ offsetof(struct winentry, wePart3),
582 		    sizeof(xde->wePart3) / 2 }
583 	};
584 	u_char *p;
585 	u_int   n, x, c;
586 
587 	lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
588 	for (n = 0; n < 3; n++)
589 		for (p = (u_char *) xde + ix[n].off, x = ix[n].dim; x;
590 		    p += 2, x--) {
591 			if ((c = getushort(p)) && (c < 32 || c > 127))
592 				c = '?';
593 			if (!(*lfn++ = c))
594 				return;
595 		}
596 	if (xde->weCnt & WIN_LAST)
597 		*lfn = 0;
598 }
599 
600 /*
601  * Copy short filename
602  */
603 static void
604 cp_sfn(u_char * sfn, struct direntry * de)
605 {
606 	u_char *p;
607 	int     j, i;
608 
609 	p = sfn;
610 	if (*de->deName != ' ') {
611 		for (j = 7; de->deName[j] == ' '; j--);
612 		for (i = 0; i <= j; i++)
613 			*p++ = de->deName[i];
614 		if (*de->deExtension != ' ') {
615 			*p++ = '.';
616 			for (j = 2; de->deExtension[j] == ' '; j--);
617 			for (i = 0; i <= j; i++)
618 				*p++ = de->deExtension[i];
619 		}
620 	}
621 	*p = 0;
622 	if (*sfn == 5)
623 		*sfn = 0xe5;
624 }
625 
626 /*
627  * Return size of file in bytes
628  */
629 static  off_t
630 fsize(DOS_FS * fs, struct direntry * de)
631 {
632 	u_long  size;
633 	u_int   c;
634 	int     n;
635 
636 	if (!(size = getulong(de->deFileSize)) &&
637 	    de->deAttributes & ATTR_DIRECTORY) {
638 		if (!(c = getushort(de->deStartCluster)))
639 			size = fs->dirents * sizeof(struct direntry);
640 		else {
641 			if ((n = fatcnt(fs, c)) == -1)
642 				return n;
643 			size = blkbyt(fs, n);
644 		}
645 	}
646 	return size;
647 }
648 
649 /*
650  * Count number of clusters in chain
651  */
652 static int
653 fatcnt(DOS_FS * fs, u_int c)
654 {
655 	int     n;
656 
657 	for (n = 0; okclus(fs, c); n++)
658 		if (fatget(fs, &c))
659 			return -1;
660 	return fatend(fs->fatsz, c) ? n : -1;
661 }
662 
663 /*
664  * Get next cluster in cluster chain
665  */
666 static int
667 fatget(DOS_FS * fs, u_int * c)
668 {
669 	u_char  buf[4];
670 	u_int   x;
671 	int     err;
672 
673 	err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
674 	    fs->fatsz != 32 ? 2 : 4);
675 	if (err)
676 		return err;
677 	x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
678 	*c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
679 	return 0;
680 }
681 
682 /*
683  * Is cluster an end-of-chain marker?
684  */
685 static int
686 fatend(u_int sz, u_int c)
687 {
688 	return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
689 }
690 
691 /*
692  * Offset-based I/O primitive
693  */
694 static int
695 ioread(DOS_FS * fs, u_int offset, void *buf, u_int nbyte)
696 {
697 	char   *s;
698 	u_int   off, n;
699 	int     err;
700 
701 	s = buf;
702 	if ((off = offset & (SECSIZ - 1))) {
703 		offset -= off;
704 		if ((err = iobuf(fs, bytsec(offset))))
705 			return err;
706 		offset += SECSIZ;
707 		if ((n = SECSIZ - off) > nbyte)
708 			n = nbyte;
709 		memcpy(s, fs->buf + off, n);
710 		s += n;
711 		nbyte -= n;
712 	}
713 	n = nbyte & (SECSIZ - 1);
714 	if (nbyte -= n) {
715 		if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
716 			return err;
717 		offset += nbyte;
718 		s += nbyte;
719 	}
720 	if (n) {
721 		if ((err = iobuf(fs, bytsec(offset))))
722 			return err;
723 		memcpy(s, fs->buf, n);
724 	}
725 	return 0;
726 }
727 
728 /*
729  * Buffered sector-based I/O primitive
730  */
731 static int
732 iobuf(DOS_FS * fs, u_int lsec)
733 {
734 	int     err;
735 
736 	if (fs->bufsec != lsec) {
737 		if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
738 			return err;
739 		fs->bufsec = lsec;
740 	}
741 	return 0;
742 }
743 
744 /*
745  * Sector-based I/O primitive
746  */
747 static int
748 ioget(struct open_file * fd, u_int lsec, void *buf, u_int nsec)
749 {
750 	size_t rsize;
751 	int err;
752 
753 #ifndef LIBSA_NO_TWIDDLE
754 	twiddle();
755 #endif
756 	err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
757 	    secbyt(nsec), buf, &rsize);
758 	return (err);
759 }
760