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