md_root.c revision 1.33.44.1 1 /* $NetBSD: md_root.c,v 1.33.44.1 2016/07/20 23:50:54 pgoyette 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: md_root.c,v 1.33.44.1 2016/07/20 23:50:54 pgoyette Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/buf.h>
36 #include <sys/proc.h>
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 #include <sys/fcntl.h>
40 #include <sys/conf.h>
41 #include <sys/disklabel.h>
42 #include <sys/disk.h>
43 #include <sys/dkbad.h>
44
45 #include <dev/cons.h>
46 #include <dev/md.h>
47
48 #include <atari/atari/device.h>
49
50 /*
51 * Misc. defines:
52 */
53 #define RAMD_CHUNK (9 * 512) /* Chunk-size for auto-load */
54 #define RAMD_NDEV 3 /* Number of devices configured */
55
56 struct ramd_info {
57 u_long ramd_size; /* Size of disk in bytes */
58 u_long ramd_flag; /* see defs below */
59 dev_t ramd_dev; /* device to load from */
60 };
61
62 /*
63 * ramd_flag:
64 */
65 #define RAMD_LOAD 0x01 /* Auto load when first opened */
66 #define RAMD_LCOMP 0x02 /* Input is compressed */
67
68 struct ramd_info rd_info[RAMD_NDEV] = {
69 {
70 1105920, /* 1Mb in 2160 sectors */
71 RAMD_LOAD, /* auto-load this device */
72 MAKEDISKDEV(2, 0, 2), /* XXX: This is crap! (720Kb flop) */
73 },
74 {
75 1474560, /* 1.44Mb in 2880 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, 3), /* XXX: This is crap! (1.44Mb flop) */
83 }
84 };
85
86 struct read_info {
87 struct buf *bp; /* buffer for strategy function */
88 long nbytes; /* total number of bytes to read */
89 long offset; /* offset in input medium */
90 char *bufp; /* current output buffer */
91 char *ebufp; /* absolute maximum for bufp */
92 int chunk; /* chunk size on input medium */
93 int media_sz; /* size of input medium */
94 void (*strat)(struct buf *); /* strategy function for read */
95 };
96
97
98 static int loaddisk(struct md_conf *, dev_t ld_dev, struct lwp *);
99 static int ramd_norm_read(struct read_info *);
100
101 #ifdef support_compression
102 static int cpy_uncompressed(void *, int, struct read_info *);
103 static int md_compressed(void *, int, struct read_info *);
104 #endif
105
106 /*
107 * This is called during autoconfig.
108 */
109 void
110 md_attach_hook(int unit, struct md_conf *md)
111 {
112
113 if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
114 printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit,
115 rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "",
116 rd_info[unit].ramd_size);
117 md->md_type = MD_UNCONFIGURED; /* Paranoia... */
118 }
119 }
120
121 void
122 md_open_hook(int unit, 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, curlwp)) {
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(struct md_conf *md, dev_t ld_dev, struct lwp *lwp)
148 {
149 struct buf *buf;
150 int error;
151 const struct bdevsw *bdp;
152 struct disklabel dl;
153 struct read_info rs;
154
155 bdp = bdevsw_lookup_acquire(ld_dev);
156 if (bdp == NULL)
157 return ENXIO;
158
159 /*
160 * Initialize our buffer header:
161 */
162 buf = getiobuf(NULL, false);
163 buf->b_cflags = BC_BUSY;
164 buf->b_dev = ld_dev;
165 buf->b_error = 0;
166 buf->b_proc = lwp->l_proc;
167
168 /*
169 * Setup read_info:
170 */
171 rs.bp = buf;
172 rs.nbytes = md->md_size;
173 rs.offset = 0;
174 rs.bufp = md->md_addr;
175 rs.ebufp = (char *)md->md_addr + md->md_size;
176 rs.chunk = RAMD_CHUNK;
177 rs.media_sz = md->md_size;
178 rs.strat = bdp->d_strategy;
179
180 /*
181 * Open device and try to get some statistics.
182 */
183 if ((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, lwp)) != 0) {
184 putiobuf(buf);
185 bdevsw_release(bdp);
186 return error;
187 }
188 if (bdp->d_ioctl(ld_dev, DIOCGDINFO, (void *)&dl, FREAD, lwp) == 0) {
189 /* Read on a cylinder basis */
190 rs.chunk = dl.d_secsize * dl.d_secpercyl;
191 rs.media_sz = dl.d_secperunit * dl.d_secsize;
192 }
193
194 #ifdef support_compression
195 if (ri->ramd_flag & RAMD_LCOMP)
196 error = decompress(cpy_uncompressed, md_compressed, &rs);
197 else
198 #endif /* support_compression */
199 error = ramd_norm_read(&rs);
200
201 bdp->d_close(ld_dev, FREAD | FNONBLOCK, 0, lwp);
202 putiobuf(buf);
203 bdevsw_release(bdp);
204 return error;
205 }
206
207 static int
208 ramd_norm_read(struct read_info *rsp)
209 {
210 long bytes_left;
211 int done, error;
212 struct buf *bp;
213 int dotc = 0;
214
215 bytes_left = rsp->nbytes;
216 bp = rsp->bp;
217 error = 0;
218
219 while (bytes_left > 0) {
220 bp->b_cflags = BC_BUSY;
221 bp->b_flags = B_PHYS | B_READ;
222 bp->b_oflags &= ~BO_DONE;
223 bp->b_blkno = btodb(rsp->offset);
224 bp->b_bcount = min(rsp->chunk, bytes_left);
225 bp->b_data = rsp->bufp;
226 bp->b_error = 0;
227
228 /* Initiate read */
229 (*rsp->strat)(bp);
230
231 /* Wait for results */
232 biowait(bp);
233 error = bp->b_error;
234
235 /* Dot counter */
236 printf(".");
237 if (!(++dotc % 40))
238 printf("\n");
239
240 done = bp->b_bcount - bp->b_resid;
241
242 bytes_left -= done;
243 rsp->offset += done;
244 rsp->bufp += done;
245
246 if (error || !done)
247 break;
248
249 if ((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
250 printf("\nInsert next media and hit any key...");
251 cngetc();
252 printf("\n");
253 rsp->offset = 0;
254 }
255 }
256 printf("\n");
257 return error;
258 }
259
260 #ifdef support_compression
261 /*
262 * Functions supporting uncompression:
263 */
264 /*
265 * Copy from the uncompression buffer to the ramdisk
266 */
267 static int
268 cpy_uncompressed(void * buf, int nbyte, struct read_info *rsp)
269 {
270
271 if ((rsp->bufp + nbyte) >= rsp->ebufp)
272 return 0;
273 memcpy(rsp->bufp, buf, nbyte);
274 rsp->bufp += nbyte;
275 return 0;
276 }
277
278 /*
279 * Read a maximum of 'nbyte' bytes into 'buf'.
280 */
281 static int
282 md_compressed(void * buf, int nbyte, struct read_info *rsp)
283 {
284 static int dotc = 0;
285 struct buf *bp;
286 int nread = 0;
287 int done, error;
288
289
290 error = 0;
291 bp = rsp->bp;
292 nbyte &= ~(DEV_BSIZE - 1);
293
294 while (nbyte > 0) {
295 bp->b_cflags = BC_BUSY;
296 bp->b_flags = B_PHYS | B_READ;
297 bp->b_oflags &= ~BO_DONE;
298 bp->b_blkno = btodb(rsp->offset);
299 bp->b_bcount = min(rsp->chunk, nbyte);
300 bp->b_data = buf;
301 bp->b_error = 0;
302
303 /* Initiate read */
304 (*rsp->strat)(bp);
305
306 /* Wait for results */
307 biowait(bp);
308 error = bp->b_error;
309
310 /* Dot counter */
311 printf(".");
312 if (!(++dotc % 40))
313 printf("\n");
314
315 done = bp->b_bcount - bp->b_resid;
316
317 nbyte -= done;
318 nread += done;
319 rsp->offset += done;
320
321 if (error || !done)
322 break;
323
324 if ((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
325 printf("\nInsert next media and hit any key...");
326 if (cngetc() != '\n')
327 printf("\n");
328 rsp->offset = 0;
329 }
330 }
331 return nread;
332 }
333 #endif /* support_compression */
334