1 /* $NetBSD: dosfs.c,v 1.4 2000/11/09 01:55:31 matt 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 <msdosfs/bpb.h> 44 #include <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 ? getushort((de)->deStartCluster) : \ 136 ((u_int)getushort((de)->deHighClust) << 16) | \ 137 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 free(fs->buf, SECSIZ); 197 free(fs, sizeof(DOS_FS)); 198 return (0); 199 } 200 201 /* 202 * Open DOS file 203 */ 204 int 205 dosfs_open(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 free(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 DOS_DIR *dir; 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 dir = alloc(sizeof(DOS_DIR) * DEPSEC); 484 485 if (!clus && fs->fatsz == 32) 486 clus = fs->rdcl; 487 nsec = !clus ? entsec(fs->dirents) : fs->spc; 488 lsec = 0; 489 xdn = chk = 0; 490 for (;;) { 491 if (!clus && !lsec) 492 lsec = fs->lsndir; 493 else if (okclus(fs, clus)) 494 lsec = blklsn(fs, clus); 495 else { 496 err = EINVAL; 497 goto out; 498 } 499 for (sec = 0; sec < nsec; sec++) { 500 if ((err = ioget(fs->fd, lsec + sec, dir, 1))) 501 goto out; 502 for (ent = 0; ent < DEPSEC; ent++) { 503 if (!*dir[ent].de.deName) { 504 err = ENOENT; 505 goto out; 506 } 507 if (*dir[ent].de.deName != 0xe5) { 508 if (dir[ent].de.deAttributes == 509 ATTR_WIN95) { 510 x = dir[ent].xde.weCnt; 511 if (x & WIN_LAST || 512 (x + 1 == xdn && 513 dir[ent].xde.weChksum == 514 chk)) { 515 if (x & WIN_LAST) { 516 chk = dir[ent].xde.weChksum; 517 x &= WIN_CNT; 518 } 519 if (x >= 1 && x <= 20) { 520 cp_xdnm(lfn, &dir[ent].xde); 521 xdn = x; 522 continue; 523 } 524 } 525 } else if (!(dir[ent].de.deAttributes & 526 ATTR_VOLUME)) { 527 if ((ok = xdn == 1)) { 528 for (x = 0, i = 0; 529 i < 11; i++) 530 x = ((((x & 1) << 7) | (x >> 1)) + 531 dir[ent].de.deName[i]) & 0xff; 532 ok = chk == x && 533 !strcasecmp(name, (const char *) lfn); 534 } 535 if (!ok) { 536 cp_sfn(sfn, &dir[ent].de); 537 ok = !strcasecmp(name, (const char *) sfn); 538 } 539 if (ok) { 540 *dep = &dir[ent].de; 541 goto out; 542 } 543 } 544 } 545 xdn = 0; 546 } 547 } 548 if (!clus) 549 break; 550 if ((err = fatget(fs, &clus))) 551 goto out; 552 if (fatend(fs->fatsz, clus)) 553 break; 554 } 555 err = ENOENT; 556 out: 557 free(dir, sizeof(DOS_DIR) * DEPSEC); 558 return (err); 559 } 560 561 /* 562 * Copy name from extended directory entry 563 */ 564 static void 565 cp_xdnm(u_char * lfn, struct winentry * xde) 566 { 567 static const struct { 568 u_int off; 569 u_int dim; 570 } ix[3] = { 571 { offsetof(struct winentry, wePart1), 572 sizeof(xde->wePart1) / 2 }, 573 { offsetof(struct winentry, wePart2), 574 sizeof(xde->wePart2) / 2 }, 575 { offsetof(struct winentry, wePart3), 576 sizeof(xde->wePart3) / 2 } 577 }; 578 u_char *p; 579 u_int n, x, c; 580 581 lfn += 13 * ((xde->weCnt & WIN_CNT) - 1); 582 for (n = 0; n < 3; n++) 583 for (p = (u_char *) xde + ix[n].off, x = ix[n].dim; x; 584 p += 2, x--) { 585 if ((c = getushort(p)) && (c < 32 || c > 127)) 586 c = '?'; 587 if (!(*lfn++ = c)) 588 return; 589 } 590 if (xde->weCnt & WIN_LAST) 591 *lfn = 0; 592 } 593 594 /* 595 * Copy short filename 596 */ 597 static void 598 cp_sfn(u_char * sfn, struct direntry * de) 599 { 600 u_char *p; 601 int j, i; 602 603 p = sfn; 604 if (*de->deName != ' ') { 605 for (j = 7; de->deName[j] == ' '; j--); 606 for (i = 0; i <= j; i++) 607 *p++ = de->deName[i]; 608 if (*de->deExtension != ' ') { 609 *p++ = '.'; 610 for (j = 2; de->deExtension[j] == ' '; j--); 611 for (i = 0; i <= j; i++) 612 *p++ = de->deExtension[i]; 613 } 614 } 615 *p = 0; 616 if (*sfn == 5) 617 *sfn = 0xe5; 618 } 619 620 /* 621 * Return size of file in bytes 622 */ 623 static off_t 624 fsize(DOS_FS * fs, struct direntry * de) 625 { 626 u_long size; 627 u_int c; 628 int n; 629 630 if (!(size = getulong(de->deFileSize)) && 631 de->deAttributes & ATTR_DIRECTORY) { 632 if (!(c = getushort(de->deStartCluster))) 633 size = fs->dirents * sizeof(struct direntry); 634 else { 635 if ((n = fatcnt(fs, c)) == -1) 636 return n; 637 size = blkbyt(fs, n); 638 } 639 } 640 return size; 641 } 642 643 /* 644 * Count number of clusters in chain 645 */ 646 static int 647 fatcnt(DOS_FS * fs, u_int c) 648 { 649 int n; 650 651 for (n = 0; okclus(fs, c); n++) 652 if (fatget(fs, &c)) 653 return -1; 654 return fatend(fs->fatsz, c) ? n : -1; 655 } 656 657 /* 658 * Get next cluster in cluster chain 659 */ 660 static int 661 fatget(DOS_FS * fs, u_int * c) 662 { 663 u_char buf[4]; 664 u_int x; 665 int err; 666 667 err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf, 668 fs->fatsz != 32 ? 2 : 4); 669 if (err) 670 return err; 671 x = fs->fatsz != 32 ? getushort(buf) : getulong(buf); 672 *c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x; 673 return 0; 674 } 675 676 /* 677 * Is cluster an end-of-chain marker? 678 */ 679 static int 680 fatend(u_int sz, u_int c) 681 { 682 return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7); 683 } 684 685 /* 686 * Offset-based I/O primitive 687 */ 688 static int 689 ioread(DOS_FS * fs, u_int offset, void *buf, u_int nbyte) 690 { 691 char *s; 692 u_int off, n; 693 int err; 694 695 s = buf; 696 if ((off = offset & (SECSIZ - 1))) { 697 offset -= off; 698 if ((err = iobuf(fs, bytsec(offset)))) 699 return err; 700 offset += SECSIZ; 701 if ((n = SECSIZ - off) > nbyte) 702 n = nbyte; 703 memcpy(s, fs->buf + off, n); 704 s += n; 705 nbyte -= n; 706 } 707 n = nbyte & (SECSIZ - 1); 708 if (nbyte -= n) { 709 if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte)))) 710 return err; 711 offset += nbyte; 712 s += nbyte; 713 } 714 if (n) { 715 if ((err = iobuf(fs, bytsec(offset)))) 716 return err; 717 memcpy(s, fs->buf, n); 718 } 719 return 0; 720 } 721 722 /* 723 * Buffered sector-based I/O primitive 724 */ 725 static int 726 iobuf(DOS_FS * fs, u_int lsec) 727 { 728 int err; 729 730 if (fs->bufsec != lsec) { 731 if ((err = ioget(fs->fd, lsec, fs->buf, 1))) 732 return err; 733 fs->bufsec = lsec; 734 } 735 return 0; 736 } 737 738 /* 739 * Sector-based I/O primitive 740 */ 741 static int 742 ioget(struct open_file * fd, u_int lsec, void *buf, u_int nsec) 743 { 744 size_t rsize; 745 int err; 746 747 #ifndef LIBSA_NO_TWIDDLE 748 twiddle(); 749 #endif 750 err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec, 751 secbyt(nsec), buf, &rsize); 752 return (err); 753 } 754