rawfs.c revision 1.10 1 1.10 tsutsui /* $NetBSD: rawfs.c,v 1.10 2008/01/12 09:54:30 tsutsui 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.10 tsutsui 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.10 tsutsui static int rawfs_get_block(struct open_file *);
61 1.1 chuck
62 1.10 tsutsui int rawfs_open(const char *path, struct open_file *f)
63 1.1 chuck {
64 1.1 chuck struct file *fs;
65 1.1 chuck
66 1.1 chuck /*
67 1.1 chuck * The actual PROM driver has already been opened.
68 1.1 chuck * Just allocate the I/O buffer, etc.
69 1.1 chuck */
70 1.1 chuck fs = alloc(sizeof(struct file));
71 1.1 chuck fs->fs_nextblk = 0;
72 1.3 scw fs->fs_curblk = -1;
73 1.1 chuck fs->fs_len = 0;
74 1.1 chuck fs->fs_ptr = fs->fs_buf;
75 1.1 chuck
76 1.1 chuck f->f_fsdata = fs;
77 1.10 tsutsui return 0;
78 1.1 chuck }
79 1.1 chuck
80 1.10 tsutsui int rawfs_close(struct open_file *f)
81 1.1 chuck {
82 1.1 chuck struct file *fs;
83 1.1 chuck
84 1.10 tsutsui fs = (struct file *)f->f_fsdata;
85 1.10 tsutsui f->f_fsdata = NULL;
86 1.1 chuck
87 1.10 tsutsui if (fs != NULL)
88 1.8 christos dealloc(fs, sizeof(*fs));
89 1.1 chuck
90 1.10 tsutsui return 0;
91 1.1 chuck }
92 1.1 chuck
93 1.10 tsutsui int rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
94 1.1 chuck {
95 1.1 chuck struct file *fs = (struct file *)f->f_fsdata;
96 1.1 chuck char *addr = start;
97 1.1 chuck int error = 0;
98 1.1 chuck size_t csize;
99 1.1 chuck
100 1.1 chuck while (size != 0) {
101 1.1 chuck
102 1.1 chuck if (fs->fs_len == 0)
103 1.1 chuck if ((error = rawfs_get_block(f)) != 0)
104 1.1 chuck break;
105 1.1 chuck
106 1.1 chuck if (fs->fs_len <= 0)
107 1.1 chuck break; /* EOF */
108 1.1 chuck
109 1.1 chuck csize = size;
110 1.1 chuck if (csize > fs->fs_len)
111 1.1 chuck csize = fs->fs_len;
112 1.1 chuck
113 1.2 scw memcpy(addr, fs->fs_ptr, csize);
114 1.1 chuck fs->fs_ptr += csize;
115 1.1 chuck fs->fs_len -= csize;
116 1.1 chuck addr += csize;
117 1.1 chuck size -= csize;
118 1.1 chuck }
119 1.1 chuck if (resid)
120 1.1 chuck *resid = size;
121 1.3 scw
122 1.3 scw if (error) {
123 1.3 scw errno = error;
124 1.3 scw error = -1;
125 1.3 scw }
126 1.3 scw
127 1.10 tsutsui return error;
128 1.1 chuck }
129 1.1 chuck
130 1.10 tsutsui int rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
131 1.1 chuck {
132 1.10 tsutsui
133 1.3 scw errno = EROFS;
134 1.10 tsutsui return -1;
135 1.1 chuck }
136 1.1 chuck
137 1.10 tsutsui off_t rawfs_seek(struct open_file *f, off_t offset, int where)
138 1.1 chuck {
139 1.3 scw struct file *fs = (struct file *)f->f_fsdata;
140 1.3 scw daddr_t curblk, targblk;
141 1.3 scw off_t newoff;
142 1.3 scw int err, idx;
143 1.3 scw
144 1.3 scw /*
145 1.3 scw * We support a very minimal feature set for lseek(2); just
146 1.3 scw * enough to allow loadfile() to work with the parameters
147 1.3 scw * we pass to it on boot.
148 1.3 scw *
149 1.3 scw * In all cases, we can't seek back past the start of the
150 1.3 scw * current block.
151 1.3 scw */
152 1.4 scw curblk = (fs->fs_curblk < 0) ? 0 : fs->fs_curblk;
153 1.3 scw
154 1.3 scw /*
155 1.3 scw * Only support SEEK_SET and SEEK_CUR which result in offsets
156 1.3 scw * which don't require seeking backwards.
157 1.3 scw */
158 1.3 scw switch (where) {
159 1.3 scw case SEEK_SET:
160 1.3 scw newoff = offset;
161 1.3 scw break;
162 1.3 scw
163 1.3 scw case SEEK_CUR:
164 1.3 scw if (fs->fs_curblk < 0)
165 1.3 scw newoff = 0;
166 1.3 scw else {
167 1.3 scw newoff = fs->fs_curblk * RAWFS_BSIZE;
168 1.3 scw newoff += RAWFS_BSIZE - fs->fs_len;
169 1.3 scw }
170 1.3 scw newoff += offset;
171 1.3 scw break;
172 1.3 scw
173 1.3 scw default:
174 1.3 scw errno = EINVAL;
175 1.10 tsutsui return -1;
176 1.3 scw }
177 1.3 scw
178 1.3 scw if (newoff < (curblk * RAWFS_BSIZE)) {
179 1.3 scw errno = EINVAL;
180 1.10 tsutsui return -1;
181 1.3 scw }
182 1.3 scw
183 1.3 scw targblk = newoff / RAWFS_BSIZE;
184 1.3 scw
185 1.3 scw /*
186 1.3 scw * If necessary, skip blocks until we hit the required target
187 1.3 scw */
188 1.3 scw err = 0;
189 1.3 scw while (fs->fs_curblk != targblk && (err = rawfs_get_block(f)) == 0)
190 1.3 scw ;
191 1.3 scw
192 1.3 scw if (err) {
193 1.3 scw errno = err;
194 1.10 tsutsui return -1;
195 1.3 scw }
196 1.3 scw
197 1.3 scw /*
198 1.3 scw * Update the index within the loaded block
199 1.3 scw */
200 1.3 scw idx = newoff % RAWFS_BSIZE;
201 1.3 scw fs->fs_len = RAWFS_BSIZE - idx;
202 1.3 scw fs->fs_ptr = &fs->fs_buf[idx];
203 1.3 scw
204 1.10 tsutsui return newoff;
205 1.1 chuck }
206 1.1 chuck
207 1.10 tsutsui int rawfs_stat(struct open_file *f, struct stat *sb)
208 1.1 chuck {
209 1.10 tsutsui
210 1.3 scw errno = EFTYPE;
211 1.10 tsutsui return -1;
212 1.1 chuck }
213 1.1 chuck
214 1.1 chuck
215 1.1 chuck /*
216 1.1 chuck * Read a block from the underlying stream device
217 1.1 chuck * (In our case, a tape drive.)
218 1.1 chuck */
219 1.1 chuck static int
220 1.10 tsutsui rawfs_get_block(struct open_file *f)
221 1.1 chuck {
222 1.1 chuck struct file *fs;
223 1.9 mrg int error;
224 1.9 mrg size_t len;
225 1.1 chuck
226 1.1 chuck fs = (struct file *)f->f_fsdata;
227 1.1 chuck fs->fs_ptr = fs->fs_buf;
228 1.1 chuck
229 1.1 chuck twiddle();
230 1.1 chuck error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
231 1.10 tsutsui fs->fs_nextblk * (RAWFS_BSIZE / DEV_BSIZE),
232 1.10 tsutsui RAWFS_BSIZE, fs->fs_buf, &len);
233 1.1 chuck
234 1.10 tsutsui if (error == 0) {
235 1.1 chuck fs->fs_len = len;
236 1.3 scw fs->fs_curblk = fs->fs_nextblk;
237 1.3 scw fs->fs_nextblk += 1;
238 1.3 scw } else {
239 1.3 scw errno = error;
240 1.3 scw error = -1;
241 1.1 chuck }
242 1.1 chuck
243 1.10 tsutsui return error;
244 1.1 chuck }
245