1 /* $NetBSD: rawfs.c,v 1.1 2001/06/14 12:57:17 fredette Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Gordon W. Ross 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 the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 4. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Gordon W. Ross 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Raw file system - for stream devices like tapes. 35 * No random access, only sequential read allowed. 36 * This exists only to allow upper level code to be 37 * shielded from the fact that the device must be 38 * read only with whole block position and size. 39 */ 40 41 #include <sys/param.h> 42 #include <stand.h> 43 44 #include "rawfs.h" 45 46 extern int debug; 47 48 /* Our devices are generally willing to do 8K transfers. */ 49 #define RAWFS_BSIZE 0x2000 50 51 /* 52 * In-core open file. 53 */ 54 struct file { 55 daddr_t fs_nextblk; /* block number to read next */ 56 int fs_len; /* amount left in f_buf */ 57 char * fs_ptr; /* read pointer into f_buf */ 58 char fs_buf[RAWFS_BSIZE]; 59 }; 60 61 static int rawfs_get_block __P((struct open_file *)); 62 63 int 64 rawfs_open(path, f) 65 char *path; 66 struct open_file *f; 67 { 68 struct file *fs; 69 70 /* 71 * The actual PROM driver has already been opened. 72 * Just allocate the I/O buffer, etc. 73 */ 74 fs = alloc(sizeof(struct file)); 75 fs->fs_nextblk = 0; 76 fs->fs_len = 0; 77 fs->fs_ptr = fs->fs_buf; 78 79 #ifdef DEBUG_RAWFS 80 printf("rawfs_open: fs=0x%x\n", fs); 81 #endif 82 83 f->f_fsdata = fs; 84 return (0); 85 } 86 87 int 88 rawfs_close(f) 89 struct open_file *f; 90 { 91 struct file *fs; 92 93 fs = (struct file *) f->f_fsdata; 94 f->f_fsdata = NULL; 95 96 #ifdef DEBUG_RAWFS 97 if (debug) { 98 printf("rawfs_close: breakpoint...", fs->fs_buf); 99 __asm (" trap #0"); 100 } 101 #endif 102 103 if (fs != NULL) 104 free(fs, sizeof(*fs)); 105 106 return (0); 107 } 108 109 int 110 rawfs_read(f, start, size, resid) 111 struct open_file *f; 112 void *start; 113 u_int size; 114 u_int *resid; 115 { 116 struct file *fs = (struct file *)f->f_fsdata; 117 char *addr = start; 118 int error = 0; 119 size_t csize; 120 121 while (size != 0) { 122 123 if (fs->fs_len == 0) 124 if ((error = rawfs_get_block(f)) != 0) 125 break; 126 127 if (fs->fs_len <= 0) 128 break; /* EOF */ 129 130 csize = size; 131 if (csize > fs->fs_len) 132 csize = fs->fs_len; 133 134 bcopy(fs->fs_ptr, addr, csize); 135 fs->fs_ptr += csize; 136 fs->fs_len -= csize; 137 addr += csize; 138 size -= csize; 139 } 140 if (resid) 141 *resid = size; 142 return (error); 143 } 144 145 int 146 rawfs_write(f, start, size, resid) 147 struct open_file *f; 148 void *start; 149 size_t size; 150 size_t *resid; /* out */ 151 { 152 #ifdef DEBUG_RAWFS 153 panic("rawfs_write"); 154 #endif 155 return (EROFS); 156 } 157 158 off_t 159 rawfs_seek(f, offset, where) 160 struct open_file *f; 161 off_t offset; 162 int where; 163 { 164 #ifdef DEBUG_RAWFS 165 panic("rawfs_seek"); 166 #endif 167 return (EFTYPE); 168 } 169 170 int 171 rawfs_stat(f, sb) 172 struct open_file *f; 173 struct stat *sb; 174 { 175 #ifdef DEBUG_RAWFS 176 panic("rawfs_stat"); 177 #endif 178 return (EFTYPE); 179 } 180 181 182 /* 183 * Read a block from the underlying stream device 184 * (In our case, a tape drive.) 185 */ 186 static int 187 rawfs_get_block(f) 188 struct open_file *f; 189 { 190 struct file *fs; 191 int error, len; 192 193 fs = (struct file *)f->f_fsdata; 194 fs->fs_ptr = fs->fs_buf; 195 196 twiddle(); 197 error = f->f_dev->dv_strategy(f->f_devdata, F_READ, 198 fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len); 199 200 if (!error) { 201 fs->fs_len = len; 202 fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE); 203 } 204 205 return (error); 206 } 207 208