Home | History | Annotate | Line # | Download | only in bootst
rawfs.c revision 1.6.2.1
      1  1.6.2.1      yamt /*	$NetBSD: rawfs.c,v 1.6.2.1 2006/06/21 14:54:01 yamt 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.6  junyoung #include <lib/libsa/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.3       scw #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.3       scw 	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.5        he 	const 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.3       scw 	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.6.2.1      yamt 		dealloc(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.3       scw 
    130      1.3       scw 	if (error) {
    131      1.3       scw 		errno = error;
    132      1.3       scw 		error = -1;
    133      1.3       scw 	}
    134      1.3       scw 
    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.3       scw 	errno = EROFS;
    145      1.3       scw 	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.3       scw 	struct file *fs = (struct file *)f->f_fsdata;
    154      1.3       scw 	daddr_t curblk, targblk;
    155      1.3       scw 	off_t newoff;
    156      1.3       scw 	int err, idx;
    157      1.3       scw 
    158      1.3       scw 	/*
    159      1.3       scw 	 * We support a very minimal feature set for lseek(2); just
    160      1.3       scw 	 * enough to allow loadfile() to work with the parameters
    161      1.3       scw 	 * we pass to it on boot.
    162      1.3       scw 	 *
    163      1.3       scw 	 * In all cases, we can't seek back past the start of the
    164      1.3       scw 	 * current block.
    165      1.3       scw 	 */
    166      1.4       scw 	curblk = (fs->fs_curblk < 0) ? 0 : fs->fs_curblk;
    167      1.3       scw 
    168      1.3       scw 	/*
    169      1.3       scw 	 * Only support SEEK_SET and SEEK_CUR which result in offsets
    170      1.3       scw 	 * which don't require seeking backwards.
    171      1.3       scw 	 */
    172      1.3       scw 	switch (where) {
    173      1.3       scw 	case SEEK_SET:
    174      1.3       scw 		newoff = offset;
    175      1.3       scw 		break;
    176      1.3       scw 
    177      1.3       scw 	case SEEK_CUR:
    178      1.3       scw 		if (fs->fs_curblk < 0)
    179      1.3       scw 			newoff = 0;
    180      1.3       scw 		else {
    181      1.3       scw 			newoff = fs->fs_curblk * RAWFS_BSIZE;
    182      1.3       scw 			newoff += RAWFS_BSIZE - fs->fs_len;
    183      1.3       scw 		}
    184      1.3       scw 		newoff += offset;
    185      1.3       scw 		break;
    186      1.3       scw 
    187      1.3       scw 	default:
    188      1.3       scw 		errno = EINVAL;
    189      1.3       scw 		return (-1);
    190      1.3       scw 	}
    191      1.3       scw 
    192      1.3       scw 	if (newoff < (curblk * RAWFS_BSIZE)) {
    193      1.3       scw 		errno = EINVAL;
    194      1.3       scw 		return (-1);
    195      1.3       scw 	}
    196      1.3       scw 
    197      1.3       scw 	targblk = newoff / RAWFS_BSIZE;
    198      1.3       scw 
    199      1.3       scw 	/*
    200      1.3       scw 	 * If necessary, skip blocks until we hit the required target
    201      1.3       scw 	 */
    202      1.3       scw 	err = 0;
    203      1.3       scw 	while (fs->fs_curblk != targblk && (err = rawfs_get_block(f)) == 0)
    204      1.3       scw 		;
    205      1.3       scw 
    206      1.3       scw 	if (err) {
    207      1.3       scw 		errno = err;
    208      1.3       scw 		return (-1);
    209      1.3       scw 	}
    210      1.3       scw 
    211      1.3       scw 	/*
    212      1.3       scw 	 * Update the index within the loaded block
    213      1.3       scw 	 */
    214      1.3       scw 	idx = newoff % RAWFS_BSIZE;
    215      1.3       scw 	fs->fs_len = RAWFS_BSIZE - idx;
    216      1.3       scw 	fs->fs_ptr = &fs->fs_buf[idx];
    217      1.3       scw 
    218      1.3       scw 	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.3       scw 	errno = EFTYPE;
    226      1.3       scw 	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.6.2.1      yamt 	int error;
    240  1.6.2.1      yamt 	size_t len;
    241      1.1     chuck 
    242      1.1     chuck 	fs = (struct file *)f->f_fsdata;
    243      1.1     chuck 	fs->fs_ptr = fs->fs_buf;
    244      1.1     chuck 
    245      1.1     chuck 	twiddle();
    246      1.1     chuck 	error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
    247      1.3       scw 		fs->fs_nextblk * (RAWFS_BSIZE / DEV_BSIZE),
    248      1.3       scw 		RAWFS_BSIZE, fs->fs_buf, &len);
    249      1.1     chuck 
    250      1.1     chuck 	if (!error) {
    251      1.1     chuck 		fs->fs_len = len;
    252      1.3       scw 		fs->fs_curblk = fs->fs_nextblk;
    253      1.3       scw 		fs->fs_nextblk += 1;
    254      1.3       scw 	} else {
    255      1.3       scw 		errno = error;
    256      1.3       scw 		error = -1;
    257      1.1     chuck 	}
    258      1.1     chuck 
    259      1.1     chuck 	return (error);
    260      1.1     chuck }
    261