Home | History | Annotate | Line # | Download | only in bootst
rawfs.c revision 1.2.6.1
      1  1.2.6.1  thorpej /*	$NetBSD: rawfs.c,v 1.2.6.1 2001/11/12 21:17:17 thorpej Exp $	*/
      2      1.1    chuck 
      3      1.1    chuck /*
      4      1.1    chuck  * Copyright (c) 1995 Gordon W. Ross
      5      1.1    chuck  * All rights reserved.
      6      1.1    chuck  *
      7      1.1    chuck  * Redistribution and use in source and binary forms, with or without
      8      1.1    chuck  * modification, are permitted provided that the following conditions
      9      1.1    chuck  * are met:
     10      1.1    chuck  * 1. Redistributions of source code must retain the above copyright
     11      1.1    chuck  *    notice, this list of conditions and the following disclaimer.
     12      1.1    chuck  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    chuck  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    chuck  *    documentation and/or other materials provided with the distribution.
     15      1.1    chuck  * 3. The name of the author may not be used to endorse or promote products
     16      1.1    chuck  *    derived from this software without specific prior written permission.
     17      1.1    chuck  * 4. All advertising materials mentioning features or use of this software
     18      1.1    chuck  *    must display the following acknowledgement:
     19      1.1    chuck  *      This product includes software developed by Gordon W. Ross
     20      1.1    chuck  *
     21      1.1    chuck  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1    chuck  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1    chuck  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1    chuck  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1    chuck  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1    chuck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1    chuck  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1    chuck  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1    chuck  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1    chuck  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1    chuck  */
     32      1.1    chuck 
     33      1.1    chuck /*
     34      1.1    chuck  * Raw file system - for stream devices like tapes.
     35      1.1    chuck  * No random access, only sequential read allowed.
     36      1.1    chuck  * This exists only to allow upper level code to be
     37      1.1    chuck  * shielded from the fact that the device must be
     38      1.1    chuck  * read only with whole block position and size.
     39      1.1    chuck  */
     40      1.1    chuck 
     41      1.1    chuck #include <sys/param.h>
     42      1.1    chuck #include <stand.h>
     43      1.1    chuck #include <rawfs.h>
     44      1.1    chuck 
     45      1.1    chuck extern int debug;
     46      1.1    chuck 
     47  1.2.6.1  thorpej #define	RAWFS_BSIZE	8192
     48      1.1    chuck 
     49      1.1    chuck /*
     50      1.1    chuck  * In-core open file.
     51      1.1    chuck  */
     52      1.1    chuck struct file {
     53      1.1    chuck 	daddr_t		fs_nextblk;	/* block number to read next */
     54  1.2.6.1  thorpej 	daddr_t		fs_curblk;	/* block number currently in buffer */
     55      1.1    chuck 	int		fs_len;		/* amount left in f_buf */
     56      1.1    chuck 	char *		fs_ptr;		/* read pointer into f_buf */
     57      1.1    chuck 	char		fs_buf[RAWFS_BSIZE];
     58      1.1    chuck };
     59      1.1    chuck 
     60      1.1    chuck static int
     61      1.1    chuck rawfs_get_block __P((struct open_file *));
     62      1.1    chuck 
     63      1.1    chuck int	rawfs_open(path, f)
     64      1.1    chuck 	char *path;
     65      1.1    chuck 	struct open_file *f;
     66      1.1    chuck {
     67      1.1    chuck 	struct file *fs;
     68      1.1    chuck 
     69      1.1    chuck 	/*
     70      1.1    chuck 	 * The actual PROM driver has already been opened.
     71      1.1    chuck 	 * Just allocate the I/O buffer, etc.
     72      1.1    chuck 	 */
     73      1.1    chuck 	fs = alloc(sizeof(struct file));
     74      1.1    chuck 	fs->fs_nextblk = 0;
     75  1.2.6.1  thorpej 	fs->fs_curblk = -1;
     76      1.1    chuck 	fs->fs_len = 0;
     77      1.1    chuck 	fs->fs_ptr = fs->fs_buf;
     78      1.1    chuck 
     79      1.1    chuck 	f->f_fsdata = fs;
     80      1.1    chuck 	return (0);
     81      1.1    chuck }
     82      1.1    chuck 
     83      1.1    chuck int	rawfs_close(f)
     84      1.1    chuck 	struct open_file *f;
     85      1.1    chuck {
     86      1.1    chuck 	struct file *fs;
     87      1.1    chuck 
     88      1.1    chuck 	fs = (struct file *) f->f_fsdata;
     89      1.1    chuck 	f->f_fsdata = (void *)0;
     90      1.1    chuck 
     91      1.1    chuck 	if (fs != (struct file *)0)
     92      1.1    chuck 		free(fs, sizeof(*fs));
     93      1.1    chuck 
     94      1.1    chuck 	return (0);
     95      1.1    chuck }
     96      1.1    chuck 
     97      1.1    chuck int	rawfs_read(f, start, size, resid)
     98      1.1    chuck 	struct open_file *f;
     99      1.1    chuck 	void *start;
    100      1.1    chuck 	u_int size;
    101      1.1    chuck 	u_int *resid;
    102      1.1    chuck {
    103      1.1    chuck 	struct file *fs = (struct file *)f->f_fsdata;
    104      1.1    chuck 	char *addr = start;
    105      1.1    chuck 	int error = 0;
    106      1.1    chuck 	size_t csize;
    107      1.1    chuck 
    108      1.1    chuck 	while (size != 0) {
    109      1.1    chuck 
    110      1.1    chuck 		if (fs->fs_len == 0)
    111      1.1    chuck 			if ((error = rawfs_get_block(f)) != 0)
    112      1.1    chuck 				break;
    113      1.1    chuck 
    114      1.1    chuck 		if (fs->fs_len <= 0)
    115      1.1    chuck 			break;	/* EOF */
    116      1.1    chuck 
    117      1.1    chuck 		csize = size;
    118      1.1    chuck 		if (csize > fs->fs_len)
    119      1.1    chuck 			csize = fs->fs_len;
    120      1.1    chuck 
    121      1.2      scw 		memcpy(addr, fs->fs_ptr, csize);
    122      1.1    chuck 		fs->fs_ptr += csize;
    123      1.1    chuck 		fs->fs_len -= csize;
    124      1.1    chuck 		addr += csize;
    125      1.1    chuck 		size -= csize;
    126      1.1    chuck 	}
    127      1.1    chuck 	if (resid)
    128      1.1    chuck 		*resid = size;
    129  1.2.6.1  thorpej 
    130  1.2.6.1  thorpej 	if (error) {
    131  1.2.6.1  thorpej 		errno = error;
    132  1.2.6.1  thorpej 		error = -1;
    133  1.2.6.1  thorpej 	}
    134  1.2.6.1  thorpej 
    135      1.1    chuck 	return (error);
    136      1.1    chuck }
    137      1.1    chuck 
    138      1.1    chuck int	rawfs_write(f, start, size, resid)
    139      1.1    chuck 	struct open_file *f;
    140      1.1    chuck 	void *start;
    141      1.1    chuck 	size_t size;
    142      1.1    chuck 	size_t *resid;	/* out */
    143      1.1    chuck {
    144  1.2.6.1  thorpej 	errno = EROFS;
    145  1.2.6.1  thorpej 	return (-1);
    146      1.1    chuck }
    147      1.1    chuck 
    148      1.1    chuck off_t	rawfs_seek(f, offset, where)
    149      1.1    chuck 	struct open_file *f;
    150      1.1    chuck 	off_t offset;
    151      1.1    chuck 	int where;
    152      1.1    chuck {
    153  1.2.6.1  thorpej 	struct file *fs = (struct file *)f->f_fsdata;
    154  1.2.6.1  thorpej 	daddr_t curblk, targblk;
    155  1.2.6.1  thorpej 	off_t newoff;
    156  1.2.6.1  thorpej 	int err, idx;
    157  1.2.6.1  thorpej 
    158  1.2.6.1  thorpej 	/*
    159  1.2.6.1  thorpej 	 * We support a very minimal feature set for lseek(2); just
    160  1.2.6.1  thorpej 	 * enough to allow loadfile() to work with the parameters
    161  1.2.6.1  thorpej 	 * we pass to it on boot.
    162  1.2.6.1  thorpej 	 *
    163  1.2.6.1  thorpej 	 * In all cases, we can't seek back past the start of the
    164  1.2.6.1  thorpej 	 * current block.
    165  1.2.6.1  thorpej 	 */
    166  1.2.6.1  thorpej 	curblk = (fs->fs_curblk < 0) ? 0 : fs->fs_curblk;
    167  1.2.6.1  thorpej 
    168  1.2.6.1  thorpej 	/*
    169  1.2.6.1  thorpej 	 * Only support SEEK_SET and SEEK_CUR which result in offsets
    170  1.2.6.1  thorpej 	 * which don't require seeking backwards.
    171  1.2.6.1  thorpej 	 */
    172  1.2.6.1  thorpej 	switch (where) {
    173  1.2.6.1  thorpej 	case SEEK_SET:
    174  1.2.6.1  thorpej 		newoff = offset;
    175  1.2.6.1  thorpej 		break;
    176  1.2.6.1  thorpej 
    177  1.2.6.1  thorpej 	case SEEK_CUR:
    178  1.2.6.1  thorpej 		if (fs->fs_curblk < 0)
    179  1.2.6.1  thorpej 			newoff = 0;
    180  1.2.6.1  thorpej 		else {
    181  1.2.6.1  thorpej 			newoff = fs->fs_curblk * RAWFS_BSIZE;
    182  1.2.6.1  thorpej 			newoff += RAWFS_BSIZE - fs->fs_len;
    183  1.2.6.1  thorpej 		}
    184  1.2.6.1  thorpej 		newoff += offset;
    185  1.2.6.1  thorpej 		break;
    186  1.2.6.1  thorpej 
    187  1.2.6.1  thorpej 	default:
    188  1.2.6.1  thorpej 		errno = EINVAL;
    189  1.2.6.1  thorpej 		return (-1);
    190  1.2.6.1  thorpej 	}
    191  1.2.6.1  thorpej 
    192  1.2.6.1  thorpej 	if (newoff < (curblk * RAWFS_BSIZE)) {
    193  1.2.6.1  thorpej 		errno = EINVAL;
    194  1.2.6.1  thorpej 		return (-1);
    195  1.2.6.1  thorpej 	}
    196  1.2.6.1  thorpej 
    197  1.2.6.1  thorpej 	targblk = newoff / RAWFS_BSIZE;
    198  1.2.6.1  thorpej 
    199  1.2.6.1  thorpej 	/*
    200  1.2.6.1  thorpej 	 * If necessary, skip blocks until we hit the required target
    201  1.2.6.1  thorpej 	 */
    202  1.2.6.1  thorpej 	err = 0;
    203  1.2.6.1  thorpej 	while (fs->fs_curblk != targblk && (err = rawfs_get_block(f)) == 0)
    204  1.2.6.1  thorpej 		;
    205  1.2.6.1  thorpej 
    206  1.2.6.1  thorpej 	if (err) {
    207  1.2.6.1  thorpej 		errno = err;
    208  1.2.6.1  thorpej 		return (-1);
    209  1.2.6.1  thorpej 	}
    210  1.2.6.1  thorpej 
    211  1.2.6.1  thorpej 	/*
    212  1.2.6.1  thorpej 	 * Update the index within the loaded block
    213  1.2.6.1  thorpej 	 */
    214  1.2.6.1  thorpej 	idx = newoff % RAWFS_BSIZE;
    215  1.2.6.1  thorpej 	fs->fs_len = RAWFS_BSIZE - idx;
    216  1.2.6.1  thorpej 	fs->fs_ptr = &fs->fs_buf[idx];
    217  1.2.6.1  thorpej 
    218  1.2.6.1  thorpej 	return (newoff);
    219      1.1    chuck }
    220      1.1    chuck 
    221      1.1    chuck int	rawfs_stat(f, sb)
    222      1.1    chuck 	struct open_file *f;
    223      1.1    chuck 	struct stat *sb;
    224      1.1    chuck {
    225  1.2.6.1  thorpej 	errno = EFTYPE;
    226  1.2.6.1  thorpej 	return (-1);
    227      1.1    chuck }
    228      1.1    chuck 
    229      1.1    chuck 
    230      1.1    chuck /*
    231      1.1    chuck  * Read a block from the underlying stream device
    232      1.1    chuck  * (In our case, a tape drive.)
    233      1.1    chuck  */
    234      1.1    chuck static int
    235      1.1    chuck rawfs_get_block(f)
    236      1.1    chuck 	struct open_file *f;
    237      1.1    chuck {
    238      1.1    chuck 	struct file *fs;
    239      1.1    chuck 	int error, len;
    240      1.1    chuck 
    241      1.1    chuck 	fs = (struct file *)f->f_fsdata;
    242      1.1    chuck 	fs->fs_ptr = fs->fs_buf;
    243      1.1    chuck 
    244      1.1    chuck 	twiddle();
    245      1.1    chuck 	error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
    246  1.2.6.1  thorpej 		fs->fs_nextblk * (RAWFS_BSIZE / DEV_BSIZE),
    247  1.2.6.1  thorpej 		RAWFS_BSIZE, fs->fs_buf, &len);
    248      1.1    chuck 
    249      1.1    chuck 	if (!error) {
    250      1.1    chuck 		fs->fs_len = len;
    251  1.2.6.1  thorpej 		fs->fs_curblk = fs->fs_nextblk;
    252  1.2.6.1  thorpej 		fs->fs_nextblk += 1;
    253  1.2.6.1  thorpej 	} else {
    254  1.2.6.1  thorpej 		errno = error;
    255  1.2.6.1  thorpej 		error = -1;
    256      1.1    chuck 	}
    257      1.1    chuck 
    258      1.1    chuck 	return (error);
    259      1.1    chuck }
    260