1 /* $NetBSD: md_root.c,v 1.28 2008/12/28 23:00:39 tsutsui 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.28 2008/12/28 23:00:39 tsutsui 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 char *bufp; /* current output buffer */ 96 char *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 lwp *)); 104 static int ramd_norm_read __P((struct read_info *)); 105 106 #ifdef support_compression 107 static int cpy_uncompressed __P((void *, int, struct read_info *)); 108 static int md_compressed __P((void *, 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, curlwp)) { 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, lwp) 156 struct md_conf *md; 157 dev_t ld_dev; 158 struct lwp *lwp; 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 buf = getiobuf(NULL, false); 174 buf->b_cflags = BC_BUSY; 175 buf->b_dev = ld_dev; 176 buf->b_error = 0; 177 buf->b_proc = lwp->l_proc; 178 179 /* 180 * Setup read_info: 181 */ 182 rs.bp = buf; 183 rs.nbytes = md->md_size; 184 rs.offset = 0; 185 rs.bufp = md->md_addr; 186 rs.ebufp = (char *)md->md_addr + md->md_size; 187 rs.chunk = RAMD_CHUNK; 188 rs.media_sz = md->md_size; 189 rs.strat = bdp->d_strategy; 190 191 /* 192 * Open device and try to get some statistics. 193 */ 194 if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, lwp)) != 0) { 195 putiobuf(buf); 196 return(error); 197 } 198 if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (void *)&dl, FREAD, lwp) == 0) { 199 /* Read on a cylinder basis */ 200 rs.chunk = dl.d_secsize * dl.d_secpercyl; 201 rs.media_sz = dl.d_secperunit * dl.d_secsize; 202 } 203 204 #ifdef support_compression 205 if(ri->ramd_flag & RAMD_LCOMP) 206 error = decompress(cpy_uncompressed, md_compressed, &rs); 207 else 208 #endif /* support_compression */ 209 error = ramd_norm_read(&rs); 210 211 bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, lwp); 212 putiobuf(buf); 213 return(error); 214 } 215 216 static int 217 ramd_norm_read(rsp) 218 struct read_info *rsp; 219 { 220 long bytes_left; 221 int done, error; 222 struct buf *bp; 223 int dotc = 0; 224 225 bytes_left = rsp->nbytes; 226 bp = rsp->bp; 227 error = 0; 228 229 while(bytes_left > 0) { 230 bp->b_cflags = BC_BUSY; 231 bp->b_flags = B_PHYS | B_READ; 232 bp->b_oflags &= ~BO_DONE; 233 bp->b_blkno = btodb(rsp->offset); 234 bp->b_bcount = min(rsp->chunk, bytes_left); 235 bp->b_data = rsp->bufp; 236 bp->b_error = 0; 237 238 /* Initiate read */ 239 (*rsp->strat)(bp); 240 241 /* Wait for results */ 242 biowait(bp); 243 error = bp->b_error; 244 245 /* Dot counter */ 246 printf("."); 247 if(!(++dotc % 40)) 248 printf("\n"); 249 250 done = bp->b_bcount - bp->b_resid; 251 252 bytes_left -= done; 253 rsp->offset += done; 254 rsp->bufp += done; 255 256 if(error || !done) 257 break; 258 259 if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) { 260 printf("\nInsert next media and hit any key..."); 261 cngetc(); 262 printf("\n"); 263 rsp->offset = 0; 264 } 265 } 266 printf("\n"); 267 return(error); 268 } 269 270 #ifdef support_compression 271 /* 272 * Functions supporting uncompression: 273 */ 274 /* 275 * Copy from the uncompression buffer to the ramdisk 276 */ 277 static int 278 cpy_uncompressed(buf, nbyte, rsp) 279 void * buf; 280 struct read_info *rsp; 281 int nbyte; 282 { 283 if((rsp->bufp + nbyte) >= rsp->ebufp) 284 return(0); 285 bcopy(buf, rsp->bufp, nbyte); 286 rsp->bufp += nbyte; 287 return(0); 288 } 289 290 /* 291 * Read a maximum of 'nbyte' bytes into 'buf'. 292 */ 293 static int 294 md_compressed(buf, nbyte, rsp) 295 void * buf; 296 struct read_info *rsp; 297 int nbyte; 298 { 299 static int dotc = 0; 300 struct buf *bp; 301 int nread = 0; 302 int done, error; 303 304 305 error = 0; 306 bp = rsp->bp; 307 nbyte &= ~(DEV_BSIZE - 1); 308 309 while(nbyte > 0) { 310 bp->b_cflags = BC_BUSY; 311 bp->b_flags = B_PHYS | B_READ; 312 bp->b_oflags &= ~BO_DONE; 313 bp->b_blkno = btodb(rsp->offset); 314 bp->b_bcount = min(rsp->chunk, nbyte); 315 bp->b_data = buf; 316 bp->b_error = 0; 317 318 /* Initiate read */ 319 (*rsp->strat)(bp); 320 321 /* Wait for results */ 322 biowait(bp); 323 error = bp->b_error; 324 325 /* Dot counter */ 326 printf("."); 327 if(!(++dotc % 40)) 328 printf("\n"); 329 330 done = bp->b_bcount - bp->b_resid; 331 332 nbyte -= done; 333 nread += done; 334 rsp->offset += done; 335 336 if(error || !done) 337 break; 338 339 if((rsp->offset == rsp->media_sz) && (nbyte != 0)) { 340 printf("\nInsert next media and hit any key..."); 341 if(cngetc() != '\n') 342 printf("\n"); 343 rsp->offset = 0; 344 } 345 } 346 return(nread); 347 } 348 #endif /* support_compression */ 349