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