md_root.c revision 1.3 1 1.3 leo /* $NetBSD: md_root.c,v 1.3 1996/04/18 08:52:09 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1996 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <sys/param.h>
34 1.1 leo #include <sys/systm.h>
35 1.1 leo #include <sys/kernel.h>
36 1.1 leo #include <sys/malloc.h>
37 1.1 leo #include <sys/buf.h>
38 1.1 leo #include <sys/proc.h>
39 1.1 leo #include <sys/device.h>
40 1.1 leo #include <sys/ioctl.h>
41 1.1 leo #include <sys/fcntl.h>
42 1.1 leo #include <sys/conf.h>
43 1.1 leo #include <sys/disklabel.h>
44 1.1 leo #include <sys/disk.h>
45 1.1 leo #include <sys/dkbad.h>
46 1.1 leo
47 1.1 leo #include <dev/ramdisk.h>
48 1.1 leo
49 1.1 leo /*
50 1.1 leo * Misc. defines:
51 1.1 leo */
52 1.1 leo #define RAMD_CHUNK (9 * 512) /* Chunk-size for auto-load */
53 1.1 leo #define RAMD_NDEV 2 /* Number of devices configured */
54 1.1 leo
55 1.1 leo struct ramd_info {
56 1.1 leo u_long ramd_size; /* Size of disk in bytes */
57 1.1 leo u_long ramd_flag; /* see defs below */
58 1.1 leo dev_t ramd_dev; /* device to load from */
59 1.1 leo };
60 1.1 leo
61 1.1 leo /*
62 1.1 leo * ramd_flag:
63 1.1 leo */
64 1.1 leo #define RAMD_LOAD 0x01 /* Auto load when first opened */
65 1.1 leo #define RAMD_LCOMP 0x02 /* Input is compressed */
66 1.1 leo
67 1.1 leo struct ramd_info rd_info[RAMD_NDEV] = {
68 1.1 leo {
69 1.1 leo 1105920, /* 1Mb in 2160 sectors */
70 1.1 leo RAMD_LOAD, /* auto-load this device */
71 1.1 leo MAKEDISKDEV(2, 0, 1), /* XXX: This is crap! (720Kb flop) */
72 1.1 leo },
73 1.1 leo {
74 1.1 leo 1474560, /* 1.44Mb in 2880 sectors */
75 1.1 leo RAMD_LOAD, /* auto-load this device */
76 1.1 leo MAKEDISKDEV(2, 0, 1), /* XXX: This is crap! (720Kb flop) */
77 1.1 leo }
78 1.1 leo };
79 1.1 leo
80 1.1 leo struct read_info {
81 1.1 leo struct buf *bp; /* buffer for strategy function */
82 1.1 leo long nbytes; /* total number of bytes to read */
83 1.1 leo long offset; /* offset in input medium */
84 1.1 leo caddr_t bufp; /* current output buffer */
85 1.1 leo caddr_t ebufp; /* absolute maximum for bufp */
86 1.1 leo int chunk; /* chunk size on input medium */
87 1.1 leo int media_sz; /* size of input medium */
88 1.1 leo void (*strat)(struct buf *); /* strategy function for read */
89 1.1 leo };
90 1.1 leo
91 1.1 leo
92 1.1 leo static int loaddisk __P((struct rd_conf *, dev_t ld_dev, struct proc *));
93 1.1 leo static int ramd_norm_read __P((struct read_info *));
94 1.3 leo
95 1.3 leo #ifdef support_compression
96 1.1 leo static int cpy_uncompressed __P((caddr_t, int, struct read_info *));
97 1.1 leo static int rd_compressed __P((caddr_t, int, struct read_info *));
98 1.3 leo #endif
99 1.1 leo
100 1.1 leo /*
101 1.1 leo * This is called during autoconfig.
102 1.1 leo */
103 1.1 leo void
104 1.1 leo rd_attach_hook(unit, rd)
105 1.1 leo int unit;
106 1.1 leo struct rd_conf *rd;
107 1.1 leo {
108 1.1 leo extern int atari_realconfig;
109 1.1 leo
110 1.1 leo if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
111 1.2 leo printf ("rd%d:%sauto-load on open. Size %ld bytes.\n", unit,
112 1.1 leo rd_info[unit].ramd_flag & RAMD_LCOMP ? " decompress/" : "",
113 1.1 leo rd_info[unit].ramd_size);
114 1.1 leo rd->rd_type = RD_UNCONFIGURED; /* Paranoia... */
115 1.1 leo }
116 1.1 leo }
117 1.1 leo
118 1.1 leo void
119 1.1 leo rd_open_hook(unit, rd)
120 1.1 leo int unit;
121 1.1 leo struct rd_conf *rd;
122 1.1 leo {
123 1.1 leo struct ramd_info *ri;
124 1.1 leo
125 1.1 leo if(unit >= RAMD_NDEV)
126 1.1 leo return;
127 1.1 leo
128 1.1 leo ri = &rd_info[unit];
129 1.1 leo if (rd->rd_type != RD_UNCONFIGURED)
130 1.1 leo return; /* Only configure once */
131 1.1 leo rd->rd_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
132 1.1 leo rd->rd_size = ri->ramd_size;
133 1.1 leo if(rd->rd_addr == NULL)
134 1.1 leo return;
135 1.1 leo if(ri->ramd_flag & RAMD_LOAD) {
136 1.1 leo if (loaddisk(rd, ri->ramd_dev, curproc)) {
137 1.1 leo free(rd->rd_addr, M_DEVBUF);
138 1.1 leo rd->rd_addr = NULL;
139 1.1 leo return;
140 1.1 leo }
141 1.1 leo }
142 1.1 leo rd->rd_type = RD_KMEM_ALLOCATED;
143 1.1 leo }
144 1.1 leo
145 1.1 leo static int
146 1.1 leo loaddisk(rd, ld_dev, proc)
147 1.1 leo struct rd_conf *rd;
148 1.1 leo dev_t ld_dev;
149 1.1 leo struct proc *proc;
150 1.1 leo {
151 1.1 leo struct buf buf;
152 1.1 leo int error;
153 1.1 leo struct bdevsw *bdp = &bdevsw[major(ld_dev)];
154 1.1 leo struct disklabel dl;
155 1.1 leo struct read_info rs;
156 1.1 leo
157 1.1 leo /*
158 1.1 leo * Initialize our buffer header:
159 1.1 leo */
160 1.1 leo buf.b_actf = NULL;
161 1.1 leo buf.b_rcred = buf.b_wcred = proc->p_ucred;
162 1.1 leo buf.b_vnbufs.le_next = NOLIST;
163 1.1 leo buf.b_flags = B_BUSY;
164 1.1 leo buf.b_dev = ld_dev;
165 1.1 leo buf.b_error = 0;
166 1.1 leo buf.b_proc = proc;
167 1.1 leo
168 1.1 leo /*
169 1.1 leo * Setup read_info:
170 1.1 leo */
171 1.1 leo rs.bp = &buf;
172 1.1 leo rs.nbytes = rd->rd_size;
173 1.1 leo rs.offset = 0;
174 1.1 leo rs.bufp = rd->rd_addr;
175 1.1 leo rs.ebufp = rd->rd_addr + rd->rd_size;
176 1.1 leo rs.chunk = RAMD_CHUNK;
177 1.1 leo rs.media_sz = rd->rd_size;
178 1.1 leo rs.strat = bdp->d_strategy;
179 1.1 leo
180 1.1 leo /*
181 1.1 leo * Open device and try to get some statistics.
182 1.1 leo */
183 1.3 leo if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, proc)) != 0)
184 1.1 leo return(error);
185 1.1 leo if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 0) {
186 1.1 leo /* Read on a cylinder basis */
187 1.1 leo rs.chunk = dl.d_secsize * dl.d_secpercyl;
188 1.1 leo rs.media_sz = dl.d_secperunit * dl.d_secsize;
189 1.1 leo }
190 1.1 leo
191 1.1 leo #ifdef support_compression
192 1.1 leo if(ri->ramd_flag & RAMD_LCOMP)
193 1.1 leo error = decompress(cpy_uncompressed, rd_compressed, &rs);
194 1.1 leo else
195 1.1 leo #endif /* support_compression */
196 1.1 leo error = ramd_norm_read(&rs);
197 1.1 leo
198 1.1 leo bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, proc);
199 1.1 leo return(error);
200 1.1 leo }
201 1.1 leo
202 1.1 leo static int
203 1.1 leo ramd_norm_read(rsp)
204 1.1 leo struct read_info *rsp;
205 1.1 leo {
206 1.1 leo long bytes_left;
207 1.1 leo int done, error;
208 1.1 leo struct buf *bp;
209 1.1 leo int s;
210 1.1 leo int dotc = 0;
211 1.1 leo
212 1.1 leo bytes_left = rsp->nbytes;
213 1.1 leo bp = rsp->bp;
214 1.1 leo error = 0;
215 1.1 leo
216 1.1 leo while(bytes_left > 0) {
217 1.1 leo s = splbio();
218 1.1 leo bp->b_flags = B_BUSY | B_PHYS | B_READ;
219 1.1 leo splx(s);
220 1.1 leo bp->b_blkno = btodb(rsp->offset);
221 1.1 leo bp->b_bcount = rsp->chunk;
222 1.1 leo bp->b_data = rsp->bufp;
223 1.1 leo
224 1.1 leo /* Initiate read */
225 1.1 leo (*rsp->strat)(bp);
226 1.1 leo
227 1.1 leo /* Wait for results */
228 1.1 leo s = splbio();
229 1.1 leo while ((bp->b_flags & B_DONE) == 0)
230 1.1 leo tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
231 1.1 leo if (bp->b_flags & B_ERROR)
232 1.1 leo error = (bp->b_error ? bp->b_error : EIO);
233 1.1 leo splx(s);
234 1.1 leo
235 1.1 leo /* Dot counter */
236 1.1 leo printf(".");
237 1.1 leo if(!(++dotc % 40))
238 1.1 leo printf("\n");
239 1.1 leo
240 1.1 leo done = bp->b_bcount - bp->b_resid;
241 1.1 leo bytes_left -= done;
242 1.1 leo rsp->offset += done;
243 1.1 leo rsp->bufp += done;
244 1.1 leo
245 1.1 leo if(error || !done)
246 1.1 leo break;
247 1.1 leo
248 1.1 leo if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
249 1.1 leo printf("\nInsert next media and hit any key...");
250 1.1 leo cngetc();
251 1.1 leo printf("\n");
252 1.1 leo rsp->offset = 0;
253 1.1 leo }
254 1.1 leo }
255 1.1 leo printf("\n");
256 1.1 leo return(error);
257 1.1 leo }
258 1.1 leo
259 1.1 leo #ifdef support_compression
260 1.1 leo /*
261 1.1 leo * Functions supporting uncompression:
262 1.1 leo */
263 1.1 leo /*
264 1.1 leo * Copy from the uncompression buffer to the ramdisk
265 1.1 leo */
266 1.1 leo static int
267 1.1 leo cpy_uncompressed(buf, nbyte, rsp)
268 1.1 leo caddr_t buf;
269 1.1 leo struct read_info *rsp;
270 1.1 leo int nbyte;
271 1.1 leo {
272 1.1 leo if((rsp->bufp + nbyte) >= rsp->ebufp)
273 1.1 leo return(0);
274 1.1 leo bcopy(buf, rsp->bufp, nbyte);
275 1.1 leo rsp->bufp += nbyte;
276 1.1 leo return(0);
277 1.1 leo }
278 1.1 leo
279 1.1 leo /*
280 1.1 leo * Read a maximum of 'nbyte' bytes into 'buf'.
281 1.1 leo */
282 1.1 leo static int
283 1.1 leo rd_compressed(buf, nbyte, rsp)
284 1.1 leo caddr_t buf;
285 1.1 leo struct read_info *rsp;
286 1.1 leo int nbyte;
287 1.1 leo {
288 1.1 leo static int dotc = 0;
289 1.1 leo struct buf *bp;
290 1.1 leo int nread = 0;
291 1.1 leo int s;
292 1.1 leo int done, error;
293 1.1 leo
294 1.1 leo
295 1.1 leo error = 0;
296 1.1 leo bp = rsp->bp;
297 1.1 leo nbyte &= ~(DEV_BSIZE - 1);
298 1.1 leo
299 1.1 leo while(nbyte > 0) {
300 1.1 leo s = splbio();
301 1.1 leo bp->b_flags = B_BUSY | B_PHYS | B_READ;
302 1.1 leo splx(s);
303 1.1 leo bp->b_blkno = btodb(rsp->offset);
304 1.1 leo bp->b_bcount = min(rsp->chunk, nbyte);
305 1.1 leo bp->b_data = buf;
306 1.1 leo
307 1.1 leo /* Initiate read */
308 1.1 leo (*rsp->strat)(bp);
309 1.1 leo
310 1.1 leo /* Wait for results */
311 1.1 leo s = splbio();
312 1.1 leo while ((bp->b_flags & B_DONE) == 0)
313 1.1 leo tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
314 1.1 leo if (bp->b_flags & B_ERROR)
315 1.1 leo error = (bp->b_error ? bp->b_error : EIO);
316 1.1 leo splx(s);
317 1.1 leo
318 1.1 leo /* Dot counter */
319 1.1 leo printf(".");
320 1.1 leo if(!(++dotc % 40))
321 1.1 leo printf("\n");
322 1.1 leo
323 1.1 leo done = bp->b_bcount - bp->b_resid;
324 1.1 leo nbyte -= done;
325 1.1 leo nread += done;
326 1.1 leo rsp->offset += done;
327 1.1 leo
328 1.1 leo if(error || !done)
329 1.1 leo break;
330 1.1 leo
331 1.1 leo if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
332 1.1 leo if(rsp->offset == rsp->media_sz) {
333 1.1 leo printf("\nInsert next media and hit any key...");
334 1.1 leo if(cngetc() != '\n')
335 1.1 leo printf("\n");
336 1.1 leo rsp->offset = 0;
337 1.1 leo }
338 1.1 leo }
339 1.1 leo s = splbio();
340 1.1 leo splx(s);
341 1.1 leo return(nread);
342 1.1 leo }
343 1.1 leo #endif /* support_compression */
344