md_root.c revision 1.20.24.1 1 /* NetBSD: md_root.c,v 1.17 2002/05/23 14:59:28 leo Exp $ */
2
3 /*
4 * Copyright (c) 1996 Leo Weppelman.
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: md_root.c,v 1.20.24.1 2008/11/18 01:19:54 snj Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/device.h>
43 #include <sys/ioctl.h>
44 #include <sys/fcntl.h>
45 #include <sys/conf.h>
46 #include <sys/disklabel.h>
47 #include <sys/disk.h>
48 #include <sys/dkbad.h>
49
50 #include <dev/cons.h>
51 #include <dev/md.h>
52
53 #include <atari/atari/device.h>
54
55 /*
56 * Misc. defines:
57 */
58 #define RAMD_CHUNK (9 * 512) /* Chunk-size for auto-load */
59 #define RAMD_NDEV 3 /* Number of devices configured */
60
61 struct ramd_info {
62 u_long ramd_size; /* Size of disk in bytes */
63 u_long ramd_flag; /* see defs below */
64 dev_t ramd_dev; /* device to load from */
65 };
66
67 /*
68 * ramd_flag:
69 */
70 #define RAMD_LOAD 0x01 /* Auto load when first opened */
71 #define RAMD_LCOMP 0x02 /* Input is compressed */
72
73 struct ramd_info rd_info[RAMD_NDEV] = {
74 {
75 1105920, /* 1Mb in 2160 sectors */
76 RAMD_LOAD, /* auto-load this device */
77 MAKEDISKDEV(2, 0, 2), /* XXX: This is crap! (720Kb flop) */
78 },
79 {
80 1474560, /* 1.44Mb in 2880 sectors */
81 RAMD_LOAD, /* auto-load this device */
82 MAKEDISKDEV(2, 0, 2), /* XXX: This is crap! (720Kb flop) */
83 },
84 {
85 1474560, /* 1.44Mb in 2880 sectors */
86 RAMD_LOAD, /* auto-load this device */
87 MAKEDISKDEV(2, 0, 3), /* XXX: This is crap! (1.44Mb flop) */
88 }
89 };
90
91 struct read_info {
92 struct buf *bp; /* buffer for strategy function */
93 long nbytes; /* total number of bytes to read */
94 long offset; /* offset in input medium */
95 caddr_t bufp; /* current output buffer */
96 caddr_t ebufp; /* absolute maximum for bufp */
97 int chunk; /* chunk size on input medium */
98 int media_sz; /* size of input medium */
99 void (*strat)(struct buf *); /* strategy function for read */
100 };
101
102
103 static int loaddisk __P((struct md_conf *, dev_t ld_dev, struct lwp *));
104 static int ramd_norm_read __P((struct read_info *));
105
106 #ifdef support_compression
107 static int cpy_uncompressed __P((caddr_t, int, struct read_info *));
108 static int md_compressed __P((caddr_t, int, struct read_info *));
109 #endif
110
111 /*
112 * This is called during autoconfig.
113 */
114 void
115 md_attach_hook(unit, md)
116 int unit;
117 struct md_conf *md;
118 {
119 if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
120 printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit,
121 rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "",
122 rd_info[unit].ramd_size);
123 md->md_type = MD_UNCONFIGURED; /* Paranoia... */
124 }
125 }
126
127 void
128 md_open_hook(unit, md)
129 int unit;
130 struct md_conf *md;
131 {
132 struct ramd_info *ri;
133
134 if(unit >= RAMD_NDEV)
135 return;
136
137 ri = &rd_info[unit];
138 if (md->md_type != MD_UNCONFIGURED)
139 return; /* Only configure once */
140 md->md_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
141 md->md_size = ri->ramd_size;
142 if(md->md_addr == NULL)
143 return;
144 if(ri->ramd_flag & RAMD_LOAD) {
145 if (loaddisk(md, ri->ramd_dev, curlwp)) {
146 free(md->md_addr, M_DEVBUF);
147 md->md_addr = NULL;
148 return;
149 }
150 }
151 md->md_type = MD_KMEM_ALLOCATED;
152 }
153
154 static int
155 loaddisk(md, ld_dev, lwp)
156 struct md_conf *md;
157 dev_t ld_dev;
158 struct lwp *lwp;
159 {
160 struct buf buf;
161 int error;
162 const struct bdevsw *bdp;
163 struct disklabel dl;
164 struct read_info rs;
165
166 bdp = bdevsw_lookup(ld_dev);
167 if (bdp == NULL)
168 return (ENXIO);
169
170 /*
171 * Initialize our buffer header:
172 */
173 memset(&buf, 0, sizeof(buf));
174 buf.b_vnbufs.le_next = NOLIST;
175 buf.b_flags = B_BUSY;
176 buf.b_dev = ld_dev;
177 buf.b_error = 0;
178 buf.b_proc = lwp->l_proc;
179
180 /*
181 * Setup read_info:
182 */
183 rs.bp = &buf;
184 rs.nbytes = md->md_size;
185 rs.offset = 0;
186 rs.bufp = md->md_addr;
187 rs.ebufp = md->md_addr + md->md_size;
188 rs.chunk = RAMD_CHUNK;
189 rs.media_sz = md->md_size;
190 rs.strat = bdp->d_strategy;
191
192 /*
193 * Open device and try to get some statistics.
194 */
195 if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, lwp)) != 0)
196 return(error);
197 if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, lwp) == 0) {
198 /* Read on a cylinder basis */
199 rs.chunk = dl.d_secsize * dl.d_secpercyl;
200 rs.media_sz = dl.d_secperunit * dl.d_secsize;
201 }
202
203 #ifdef support_compression
204 if(ri->ramd_flag & RAMD_LCOMP)
205 error = decompress(cpy_uncompressed, md_compressed, &rs);
206 else
207 #endif /* support_compression */
208 error = ramd_norm_read(&rs);
209
210 bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, lwp);
211 return(error);
212 }
213
214 static int
215 ramd_norm_read(rsp)
216 struct read_info *rsp;
217 {
218 long bytes_left;
219 int done, error;
220 struct buf *bp;
221 int s;
222 int dotc = 0;
223
224 bytes_left = rsp->nbytes;
225 bp = rsp->bp;
226 error = 0;
227
228 while(bytes_left > 0) {
229 s = splbio();
230 bp->b_flags = B_BUSY | B_PHYS | B_READ;
231 splx(s);
232 bp->b_blkno = btodb(rsp->offset);
233 bp->b_bcount = rsp->chunk;
234 bp->b_data = rsp->bufp;
235
236 /* Initiate read */
237 (*rsp->strat)(bp);
238
239 /* Wait for results */
240 s = splbio();
241 while ((bp->b_flags & B_DONE) == 0)
242 tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
243 if (bp->b_flags & B_ERROR)
244 error = (bp->b_error ? bp->b_error : EIO);
245 splx(s);
246
247 /* Dot counter */
248 printf(".");
249 if(!(++dotc % 40))
250 printf("\n");
251
252 done = bp->b_bcount - bp->b_resid;
253 brelse(bp, 0);
254
255 bytes_left -= done;
256 rsp->offset += done;
257 rsp->bufp += done;
258
259 if(error || !done)
260 break;
261
262 if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
263 printf("\nInsert next media and hit any key...");
264 cngetc();
265 printf("\n");
266 rsp->offset = 0;
267 }
268 }
269 printf("\n");
270 return(error);
271 }
272
273 #ifdef support_compression
274 /*
275 * Functions supporting uncompression:
276 */
277 /*
278 * Copy from the uncompression buffer to the ramdisk
279 */
280 static int
281 cpy_uncompressed(buf, nbyte, rsp)
282 caddr_t buf;
283 struct read_info *rsp;
284 int nbyte;
285 {
286 if((rsp->bufp + nbyte) >= rsp->ebufp)
287 return(0);
288 bcopy(buf, rsp->bufp, nbyte);
289 rsp->bufp += nbyte;
290 return(0);
291 }
292
293 /*
294 * Read a maximum of 'nbyte' bytes into 'buf'.
295 */
296 static int
297 md_compressed(buf, nbyte, rsp)
298 caddr_t buf;
299 struct read_info *rsp;
300 int nbyte;
301 {
302 static int dotc = 0;
303 struct buf *bp;
304 int nread = 0;
305 int s;
306 int done, error;
307
308
309 error = 0;
310 bp = rsp->bp;
311 nbyte &= ~(DEV_BSIZE - 1);
312
313 while(nbyte > 0) {
314 s = splbio();
315 bp->b_flags = B_BUSY | B_PHYS | B_READ;
316 splx(s);
317 bp->b_blkno = btodb(rsp->offset);
318 bp->b_bcount = min(rsp->chunk, nbyte);
319 bp->b_data = buf;
320
321 /* Initiate read */
322 (*rsp->strat)(bp);
323
324 /* Wait for results */
325 s = splbio();
326 while ((bp->b_flags & B_DONE) == 0)
327 tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
328 if (bp->b_flags & B_ERROR)
329 error = (bp->b_error ? bp->b_error : EIO);
330 splx(s);
331
332 /* Dot counter */
333 printf(".");
334 if(!(++dotc % 40))
335 printf("\n");
336
337 done = bp->b_bcount - bp->b_resid;
338 brelse(bp, 0);
339
340 nbyte -= done;
341 nread += done;
342 rsp->offset += done;
343
344 if(error || !done)
345 break;
346
347 if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
348 printf("\nInsert next media and hit any key...");
349 if(cngetc() != '\n')
350 printf("\n");
351 rsp->offset = 0;
352 }
353 }
354 s = splbio();
355 splx(s);
356 return(nread);
357 }
358 #endif /* support_compression */
359