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.19 2003/07/15 01:19:51 lukem 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 proc *)); 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, curproc)) { 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, proc) 156 struct md_conf *md; 157 dev_t ld_dev; 158 struct proc *proc; 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 = 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, proc)) != 0) 196 return(error); 197 if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 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, proc); 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 bytes_left -= done; 254 rsp->offset += done; 255 rsp->bufp += done; 256 257 if(error || !done) 258 break; 259 260 if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) { 261 printf("\nInsert next media and hit any key..."); 262 cngetc(); 263 printf("\n"); 264 rsp->offset = 0; 265 } 266 } 267 printf("\n"); 268 return(error); 269 } 270 271 #ifdef support_compression 272 /* 273 * Functions supporting uncompression: 274 */ 275 /* 276 * Copy from the uncompression buffer to the ramdisk 277 */ 278 static int 279 cpy_uncompressed(buf, nbyte, rsp) 280 caddr_t buf; 281 struct read_info *rsp; 282 int nbyte; 283 { 284 if((rsp->bufp + nbyte) >= rsp->ebufp) 285 return(0); 286 bcopy(buf, rsp->bufp, nbyte); 287 rsp->bufp += nbyte; 288 return(0); 289 } 290 291 /* 292 * Read a maximum of 'nbyte' bytes into 'buf'. 293 */ 294 static int 295 md_compressed(buf, nbyte, rsp) 296 caddr_t buf; 297 struct read_info *rsp; 298 int nbyte; 299 { 300 static int dotc = 0; 301 struct buf *bp; 302 int nread = 0; 303 int s; 304 int done, error; 305 306 307 error = 0; 308 bp = rsp->bp; 309 nbyte &= ~(DEV_BSIZE - 1); 310 311 while(nbyte > 0) { 312 s = splbio(); 313 bp->b_flags = B_BUSY | B_PHYS | B_READ; 314 splx(s); 315 bp->b_blkno = btodb(rsp->offset); 316 bp->b_bcount = min(rsp->chunk, nbyte); 317 bp->b_data = buf; 318 319 /* Initiate read */ 320 (*rsp->strat)(bp); 321 322 /* Wait for results */ 323 s = splbio(); 324 while ((bp->b_flags & B_DONE) == 0) 325 tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0); 326 if (bp->b_flags & B_ERROR) 327 error = (bp->b_error ? bp->b_error : EIO); 328 splx(s); 329 330 /* Dot counter */ 331 printf("."); 332 if(!(++dotc % 40)) 333 printf("\n"); 334 335 done = bp->b_bcount - bp->b_resid; 336 nbyte -= done; 337 nread += done; 338 rsp->offset += done; 339 340 if(error || !done) 341 break; 342 343 if((rsp->offset == rsp->media_sz) && (nbyte != 0)) { 344 if(rsp->offset == rsp->media_sz) { 345 printf("\nInsert next media and hit any key..."); 346 if(cngetc() != '\n') 347 printf("\n"); 348 rsp->offset = 0; 349 } 350 } 351 s = splbio(); 352 splx(s); 353 return(nread); 354 } 355 #endif /* support_compression */ 356