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