dosfs.c revision 1.14 1 /* $NetBSD: dosfs.c,v 1.14 2008/03/25 21:23:50 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 #define strcasecmp(s1, s2) dos_strcasecmp(s1, s2)
154 static int
155 strcasecmp(const char *s1, const char *s2)
156 {
157 char c1, c2;
158 #define TO_UPPER(c) ((c) >= 'a' && (c) <= 'z' ? (c) - ('a' - 'A') : (c))
159 for (;;) {
160 c1 = *s1++;
161 c2 = *s2++;
162 if (TO_UPPER(c1) != TO_UPPER(c2))
163 return 1;
164 if (c1 == 0)
165 return 0;
166 }
167 #undef TO_UPPER
168 }
169
170 /*
171 * Mount DOS filesystem
172 */
173 static int
174 dos_mount(DOS_FS *fs, struct open_file *fd)
175 {
176 int err;
177
178 (void)memset(fs, 0, sizeof(DOS_FS));
179 fs->fd = fd;
180 if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
181 (err = ioget(fs->fd, 0, fs->buf, 1)) ||
182 (err = parsebs(fs, (DOS_BS *)fs->buf))) {
183 (void) dosunmount(fs);
184 return err;
185 }
186 return 0;
187 }
188
189 #ifndef LIBSA_NO_FS_CLOSE
190 /*
191 * Unmount mounted filesystem
192 */
193 static int
194 dos_unmount(DOS_FS *fs)
195 {
196 int err;
197
198 if (fs->links)
199 return EBUSY;
200 if ((err = dosunmount(fs)))
201 return err;
202 return 0;
203 }
204 #endif
205
206 /*
207 * Common code shared by dos_mount() and dos_unmount()
208 */
209 static int
210 dosunmount(DOS_FS *fs)
211 {
212 if (fs->buf)
213 dealloc(fs->buf, SECSIZ);
214 dealloc(fs, sizeof(DOS_FS));
215 return 0;
216 }
217
218 /*
219 * Open DOS file
220 */
221 int
222 dosfs_open(const char *path, struct open_file *fd)
223 {
224 const struct direntry *de;
225 DOS_FILE *f;
226 DOS_FS *fs;
227 u_int size, clus;
228 int err = 0;
229
230 /* Allocate mount structure, associate with open */
231 fs = alloc(sizeof(DOS_FS));
232
233 if ((err = dos_mount(fs, fd)))
234 goto out;
235
236 if ((err = namede(fs, path, &de)))
237 goto out;
238
239 clus = stclus(fs->fatsz, de);
240 size = getulong(de->deFileSize);
241
242 if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
243 ((de->deAttributes & ATTR_DIRECTORY) && size) ||
244 (clus && !okclus(fs, clus))) {
245 err = EINVAL;
246 goto out;
247 }
248
249 f = alloc(sizeof(DOS_FILE));
250 #ifdef BOOTXX
251 /* due to __internal_memset_ causing all sorts of register spillage
252 (and being completely unoptimized for zeroing small amounts of
253 memory), if we hand-initialize the remaining members of f to zero,
254 the code size drops 68 bytes. This makes no sense, admittedly. */
255 f->offset = 0;
256 f->c = 0;
257 #else
258 (void)memset(f, 0, sizeof(DOS_FILE));
259 #endif
260 f->fs = fs;
261 fs->links++;
262 f->de = *de;
263 fd->f_fsdata = (void *)f;
264
265 out:
266 return err;
267 }
268
269 /*
270 * Read from file
271 */
272 int
273 dosfs_read(struct open_file *fd, void *vbuf, size_t nbyte, size_t *resid)
274 {
275 off_t size;
276 u_int8_t *buf = vbuf;
277 u_int nb, off, clus, c, cnt, n;
278 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
279 int err = 0;
280
281 nb = (u_int) nbyte;
282 if ((size = fsize(f->fs, &f->de)) == -1)
283 return EINVAL;
284 if (nb > (n = size - f->offset))
285 nb = n;
286 off = f->offset;
287 if ((clus = stclus(f->fs->fatsz, &f->de)))
288 off &= f->fs->bsize - 1;
289 c = f->c;
290 cnt = nb;
291 while (cnt) {
292 n = 0;
293 if (!c) {
294 if ((c = clus))
295 n = bytblk(f->fs, f->offset);
296 } else if (!off) {
297 n++;
298 }
299 while (n--) {
300 if ((err = fatget(f->fs, &c)))
301 goto out;
302 if (!okclus(f->fs, c)) {
303 err = EINVAL;
304 goto out;
305 }
306 }
307 if (!clus || (n = f->fs->bsize - off) > cnt)
308 n = cnt;
309 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
310 secbyt(f->fs->lsndir)) + off,
311 buf, n)))
312 goto out;
313 f->offset += n;
314 f->c = c;
315 off = 0;
316 buf += n;
317 cnt -= n;
318 }
319 out:
320 if (resid)
321 *resid = nbyte - nb + cnt;
322 return err;
323 }
324
325 #ifndef LIBSA_NO_FS_WRITE
326 /*
327 * Not implemented.
328 */
329 int
330 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
331 {
332
333 return EROFS;
334 }
335 #endif /* !LIBSA_NO_FS_WRITE */
336
337 #ifndef LIBSA_NO_FS_SEEK
338 /*
339 * Reposition within file
340 */
341 off_t
342 dosfs_seek(struct open_file *fd, off_t offset, int whence)
343 {
344 off_t off;
345 u_int size;
346 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
347
348 size = getulong(f->de.deFileSize);
349 switch (whence) {
350 case SEEK_SET:
351 off = 0;
352 break;
353 case SEEK_CUR:
354 off = f->offset;
355 break;
356 case SEEK_END:
357 off = size;
358 break;
359 default:
360 return -1;
361 }
362 off += offset;
363 if (off < 0 || off > size)
364 return -1;
365 f->offset = (u_int) off;
366 f->c = 0;
367 return off;
368 }
369 #endif /* !LIBSA_NO_FS_SEEK */
370
371 #ifndef LIBSA_NO_FS_CLOSE
372 /*
373 * Close open file
374 */
375 int
376 dosfs_close(struct open_file *fd)
377 {
378 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
379 DOS_FS *fs = f->fs;
380
381 f->fs->links--;
382 dealloc(f, sizeof(DOS_FILE));
383 dos_unmount(fs);
384 return 0;
385 }
386 #endif /* !LIBSA_NO_FS_CLOSE */
387
388 /*
389 * Return some stat information on a file.
390 */
391 int
392 dosfs_stat(struct open_file *fd, struct stat *sb)
393 {
394 DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
395
396 /* only important stuff */
397 sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
398 (S_IFDIR | 0555) : (S_IFREG | 0444);
399 sb->st_nlink = 1;
400 sb->st_uid = 0;
401 sb->st_gid = 0;
402 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
403 return EINVAL;
404 return 0;
405 }
406
407 /*
408 * Parse DOS boot sector
409 */
410 static int
411 parsebs(DOS_FS *fs, DOS_BS *bs)
412 {
413 u_int sc;
414
415 if ((bs->jmp[0] != 0x69 &&
416 bs->jmp[0] != 0xe9 &&
417 (bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
418 bs->bpb.bpbMedia < 0xf0)
419 return EINVAL;
420 if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
421 return EINVAL;
422 if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
423 return EINVAL;
424 fs->bsize = secbyt(fs->spc);
425 fs->bshift = ffs(fs->bsize) - 1;
426 if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
427 if (bs->bpb.bpbFATs != 2)
428 return EINVAL;
429 if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
430 return EINVAL;
431 } else {
432 if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
433 return EINVAL;
434 if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
435 return EINVAL;
436 if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
437 return EINVAL;
438 }
439 if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
440 return EINVAL;
441 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
442 fs->lsndta = fs->lsndir + entsec(fs->dirents);
443 if (!(sc = getushort(bs->bpb.bpbSectors)) &&
444 !(sc = getulong(bs->bpb.bpbHugeSectors)))
445 return EINVAL;
446 if (fs->lsndta > sc)
447 return EINVAL;
448 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
449 return EINVAL;
450 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
451 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
452 if (fs->xclus > sc)
453 fs->xclus = sc;
454 return 0;
455 }
456
457 /*
458 * Return directory entry from path
459 */
460 static int
461 namede(DOS_FS *fs, const char *path, const struct direntry **dep)
462 {
463 char name[256];
464 const struct direntry *de;
465 char *s;
466 size_t n;
467 int err;
468
469 err = 0;
470 de = dot;
471 if (*path == '/')
472 path++;
473 while (*path) {
474 if (!(s = strchr(path, '/')))
475 s = strchr(path, 0);
476 if ((n = s - path) > 255)
477 return ENAMETOOLONG;
478 memcpy(name, path, n);
479 name[n] = 0;
480 path = s;
481 if (!(de->deAttributes & ATTR_DIRECTORY))
482 return ENOTDIR;
483 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
484 return err;
485 if (*path == '/')
486 path++;
487 }
488 *dep = de;
489 return 0;
490 }
491
492 /*
493 * Lookup path segment
494 */
495 static int
496 lookup(DOS_FS *fs, u_int clus, const char *name, const struct direntry **dep)
497 {
498 static DOS_DIR *dir = NULL;
499 u_char lfn[261];
500 u_char sfn[13];
501 u_int nsec, lsec, xdn, chk, sec, ent, x;
502 int err = 0, ok, i;
503
504 if (!clus)
505 for (ent = 0; ent < 2; ent++)
506 if (!strcasecmp(name, dotstr[ent])) {
507 *dep = dot + ent;
508 return 0;
509 }
510
511 if (dir == NULL) {
512 dir = alloc(sizeof(DOS_DIR) * DEPSEC);
513 if (dir == NULL)
514 return ENOMEM;
515 }
516
517 if (!clus && fs->fatsz == 32)
518 clus = fs->rdcl;
519 nsec = !clus ? entsec(fs->dirents) : fs->spc;
520 lsec = 0;
521 xdn = chk = 0;
522 for (;;) {
523 if (!clus && !lsec)
524 lsec = fs->lsndir;
525 else if (okclus(fs, clus))
526 lsec = blklsn(fs, clus);
527 else {
528 err = EINVAL;
529 goto out;
530 }
531 for (sec = 0; sec < nsec; sec++) {
532 if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
533 goto out;
534 for (ent = 0; ent < DEPSEC; ent++) {
535 if (!*dir[ent].de.deName) {
536 err = ENOENT;
537 goto out;
538 }
539 if (*dir[ent].de.deName != 0xe5) {
540 if (dir[ent].de.deAttributes ==
541 ATTR_WIN95) {
542 x = dir[ent].xde.weCnt;
543 if (x & WIN_LAST ||
544 (x + 1 == xdn &&
545 dir[ent].xde.weChksum ==
546 chk)) {
547 if (x & WIN_LAST) {
548 chk = dir[ent].xde.weChksum;
549 x &= WIN_CNT;
550 }
551 if (x >= 1 && x <= 20) {
552 cp_xdnm(lfn, &dir[ent].xde);
553 xdn = x;
554 continue;
555 }
556 }
557 } else if (!(dir[ent].de.deAttributes &
558 ATTR_VOLUME)) {
559 if ((ok = xdn == 1)) {
560 for (x = 0, i = 0;
561 i < 11; i++)
562 x = ((((x & 1) << 7) | (x >> 1)) +
563 dir[ent].de.deName[i]) & 0xff;
564 ok = chk == x &&
565 !strcasecmp(name, (const char *)lfn);
566 }
567 if (!ok) {
568 cp_sfn(sfn, &dir[ent].de);
569 ok = !strcasecmp(name, (const char *)sfn);
570 }
571 if (ok) {
572 *dep = &dir[ent].de;
573 goto out2;
574 }
575 }
576 }
577 xdn = 0;
578 }
579 }
580 if (!clus)
581 break;
582 if ((err = fatget(fs, &clus)))
583 goto out;
584 if (fatend(fs->fatsz, clus))
585 break;
586 }
587 err = ENOENT;
588 out:
589 dealloc(dir, sizeof(DOS_DIR) * DEPSEC);
590 dir = NULL;
591 out2:
592 return err;
593 }
594
595 /*
596 * Copy name from extended directory entry
597 */
598 static void
599 cp_xdnm(u_char *lfn, struct winentry *xde)
600 {
601 static const struct {
602 u_int off;
603 u_int dim;
604 } ix[3] = {
605 { offsetof(struct winentry, wePart1),
606 sizeof(xde->wePart1) / 2 },
607 { offsetof(struct winentry, wePart2),
608 sizeof(xde->wePart2) / 2 },
609 { offsetof(struct winentry, wePart3),
610 sizeof(xde->wePart3) / 2 }
611 };
612 u_char *p;
613 u_int n, x, c;
614
615 lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
616 for (n = 0; n < 3; n++)
617 for (p = (u_char *)xde + ix[n].off, x = ix[n].dim; x;
618 p += 2, x--) {
619 if ((c = getushort(p)) && (c < 32 || c > 127))
620 c = '?';
621 if (!(*lfn++ = c))
622 return;
623 }
624 if (xde->weCnt & WIN_LAST)
625 *lfn = 0;
626 }
627
628 /*
629 * Copy short filename
630 */
631 static void
632 cp_sfn(u_char *sfn, struct direntry *de)
633 {
634 u_char *p;
635 int j, i;
636
637 p = sfn;
638 if (*de->deName != ' ') {
639 for (j = 7; de->deName[j] == ' '; j--);
640 for (i = 0; i <= j; i++)
641 *p++ = de->deName[i];
642 if (*de->deExtension != ' ') {
643 *p++ = '.';
644 for (j = 2; de->deExtension[j] == ' '; j--);
645 for (i = 0; i <= j; i++)
646 *p++ = de->deExtension[i];
647 }
648 }
649 *p = 0;
650 if (*sfn == 5)
651 *sfn = 0xe5;
652 }
653
654 /*
655 * Return size of file in bytes
656 */
657 static off_t
658 fsize(DOS_FS *fs, struct direntry *de)
659 {
660 u_long size;
661 u_int c;
662 int n;
663
664 if (!(size = getulong(de->deFileSize)) &&
665 de->deAttributes & ATTR_DIRECTORY) {
666 if (!(c = getushort(de->deStartCluster))) {
667 size = fs->dirents * sizeof(struct direntry);
668 } else {
669 if ((n = fatcnt(fs, c)) == -1)
670 return n;
671 size = blkbyt(fs, n);
672 }
673 }
674 return size;
675 }
676
677 /*
678 * Count number of clusters in chain
679 */
680 static int
681 fatcnt(DOS_FS *fs, u_int c)
682 {
683 int n;
684
685 for (n = 0; okclus(fs, c); n++)
686 if (fatget(fs, &c))
687 return -1;
688 return fatend(fs->fatsz, c) ? n : -1;
689 }
690
691 /*
692 * Get next cluster in cluster chain
693 */
694 static int
695 fatget(DOS_FS *fs, u_int *c)
696 {
697 u_char buf[4];
698 u_int x;
699 int err;
700
701 err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
702 fs->fatsz != 32 ? 2 : 4);
703 if (err)
704 return err;
705 x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
706 *c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
707 return 0;
708 }
709
710 /*
711 * Is cluster an end-of-chain marker?
712 */
713 static int
714 fatend(u_int sz, u_int c)
715 {
716 return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
717 }
718
719 /*
720 * Offset-based I/O primitive
721 */
722 static int
723 ioread(DOS_FS *fs, u_int offset, void *buf, u_int nbyte)
724 {
725 char *s;
726 u_int off, n;
727 int err;
728
729 s = buf;
730 if ((off = offset & (SECSIZ - 1))) {
731 offset -= off;
732 if ((err = iobuf(fs, bytsec(offset))))
733 return err;
734 offset += SECSIZ;
735 if ((n = SECSIZ - off) > nbyte)
736 n = nbyte;
737 memcpy(s, fs->buf + off, n);
738 s += n;
739 nbyte -= n;
740 }
741 n = nbyte & (SECSIZ - 1);
742 if (nbyte -= n) {
743 if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
744 return err;
745 offset += nbyte;
746 s += nbyte;
747 }
748 if (n) {
749 if ((err = iobuf(fs, bytsec(offset))))
750 return err;
751 memcpy(s, fs->buf, n);
752 }
753 return 0;
754 }
755
756 /*
757 * Buffered sector-based I/O primitive
758 */
759 static int
760 iobuf(DOS_FS *fs, u_int lsec)
761 {
762 int err;
763
764 if (fs->bufsec != lsec) {
765 if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
766 return err;
767 fs->bufsec = lsec;
768 }
769 return 0;
770 }
771
772 /*
773 * Sector-based I/O primitive
774 */
775 static int
776 ioget(struct open_file *fd, u_int lsec, void *buf, u_int nsec)
777 {
778 size_t rsize;
779 int err;
780
781 #ifndef LIBSA_NO_TWIDDLE
782 twiddle();
783 #endif
784 err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
785 secbyt(nsec), buf, &rsize);
786 return err;
787 }
788