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