rawfs.c revision 1.2 1 1.2 scw /* $NetBSD: rawfs.c,v 1.2 2001/07/07 09:06:43 scw 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.1 chuck #define RAWFS_BSIZE 512
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.1 chuck int fs_len; /* amount left in f_buf */
55 1.1 chuck char * fs_ptr; /* read pointer into f_buf */
56 1.1 chuck char fs_buf[RAWFS_BSIZE];
57 1.1 chuck };
58 1.1 chuck
59 1.1 chuck static int
60 1.1 chuck rawfs_get_block __P((struct open_file *));
61 1.1 chuck
62 1.1 chuck int rawfs_open(path, f)
63 1.1 chuck char *path;
64 1.1 chuck struct open_file *f;
65 1.1 chuck {
66 1.1 chuck struct file *fs;
67 1.1 chuck
68 1.1 chuck /*
69 1.1 chuck * The actual PROM driver has already been opened.
70 1.1 chuck * Just allocate the I/O buffer, etc.
71 1.1 chuck */
72 1.1 chuck fs = alloc(sizeof(struct file));
73 1.1 chuck fs->fs_nextblk = 0;
74 1.1 chuck fs->fs_len = 0;
75 1.1 chuck fs->fs_ptr = fs->fs_buf;
76 1.1 chuck
77 1.1 chuck f->f_fsdata = fs;
78 1.1 chuck return (0);
79 1.1 chuck }
80 1.1 chuck
81 1.1 chuck int rawfs_close(f)
82 1.1 chuck struct open_file *f;
83 1.1 chuck {
84 1.1 chuck struct file *fs;
85 1.1 chuck
86 1.1 chuck fs = (struct file *) f->f_fsdata;
87 1.1 chuck f->f_fsdata = (void *)0;
88 1.1 chuck
89 1.1 chuck if (fs != (struct file *)0)
90 1.1 chuck free(fs, sizeof(*fs));
91 1.1 chuck
92 1.1 chuck return (0);
93 1.1 chuck }
94 1.1 chuck
95 1.1 chuck int rawfs_read(f, start, size, resid)
96 1.1 chuck struct open_file *f;
97 1.1 chuck void *start;
98 1.1 chuck u_int size;
99 1.1 chuck u_int *resid;
100 1.1 chuck {
101 1.1 chuck struct file *fs = (struct file *)f->f_fsdata;
102 1.1 chuck char *addr = start;
103 1.1 chuck int error = 0;
104 1.1 chuck size_t csize;
105 1.1 chuck
106 1.1 chuck while (size != 0) {
107 1.1 chuck
108 1.1 chuck if (fs->fs_len == 0)
109 1.1 chuck if ((error = rawfs_get_block(f)) != 0)
110 1.1 chuck break;
111 1.1 chuck
112 1.1 chuck if (fs->fs_len <= 0)
113 1.1 chuck break; /* EOF */
114 1.1 chuck
115 1.1 chuck csize = size;
116 1.1 chuck if (csize > fs->fs_len)
117 1.1 chuck csize = fs->fs_len;
118 1.1 chuck
119 1.2 scw memcpy(addr, fs->fs_ptr, csize);
120 1.1 chuck fs->fs_ptr += csize;
121 1.1 chuck fs->fs_len -= csize;
122 1.1 chuck addr += csize;
123 1.1 chuck size -= csize;
124 1.1 chuck }
125 1.1 chuck if (resid)
126 1.1 chuck *resid = size;
127 1.1 chuck return (error);
128 1.1 chuck }
129 1.1 chuck
130 1.1 chuck int rawfs_write(f, start, size, resid)
131 1.1 chuck struct open_file *f;
132 1.1 chuck void *start;
133 1.1 chuck size_t size;
134 1.1 chuck size_t *resid; /* out */
135 1.1 chuck {
136 1.1 chuck return (EROFS);
137 1.1 chuck }
138 1.1 chuck
139 1.1 chuck off_t rawfs_seek(f, offset, where)
140 1.1 chuck struct open_file *f;
141 1.1 chuck off_t offset;
142 1.1 chuck int where;
143 1.1 chuck {
144 1.1 chuck return (EFTYPE);
145 1.1 chuck }
146 1.1 chuck
147 1.1 chuck int rawfs_stat(f, sb)
148 1.1 chuck struct open_file *f;
149 1.1 chuck struct stat *sb;
150 1.1 chuck {
151 1.1 chuck return (EFTYPE);
152 1.1 chuck }
153 1.1 chuck
154 1.1 chuck
155 1.1 chuck /*
156 1.1 chuck * Read a block from the underlying stream device
157 1.1 chuck * (In our case, a tape drive.)
158 1.1 chuck */
159 1.1 chuck static int
160 1.1 chuck rawfs_get_block(f)
161 1.1 chuck struct open_file *f;
162 1.1 chuck {
163 1.1 chuck struct file *fs;
164 1.1 chuck int error, len;
165 1.1 chuck
166 1.1 chuck fs = (struct file *)f->f_fsdata;
167 1.1 chuck fs->fs_ptr = fs->fs_buf;
168 1.1 chuck
169 1.1 chuck twiddle();
170 1.1 chuck error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
171 1.1 chuck fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
172 1.1 chuck
173 1.1 chuck if (!error) {
174 1.1 chuck fs->fs_len = len;
175 1.1 chuck fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
176 1.1 chuck }
177 1.1 chuck
178 1.1 chuck return (error);
179 1.1 chuck }
180 1.1 chuck
181