1 /* $NetBSD: md_root.c,v 1.31 2009/03/18 17:06:43 cegger 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.31 2009/03/18 17:06:43 cegger 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(struct md_conf *, dev_t ld_dev, struct lwp *); 104 static int ramd_norm_read(struct read_info *); 105 106 #ifdef support_compression 107 static int cpy_uncompressed(void *, int, struct read_info *); 108 static int md_compressed(void *, int, struct read_info *); 109 #endif 110 111 /* 112 * This is called during autoconfig. 113 */ 114 void 115 md_attach_hook(int unit, struct md_conf *md) 116 { 117 if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) { 118 printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit, 119 rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "", 120 rd_info[unit].ramd_size); 121 md->md_type = MD_UNCONFIGURED; /* Paranoia... */ 122 } 123 } 124 125 void 126 md_open_hook(int unit, struct md_conf *md) 127 { 128 struct ramd_info *ri; 129 130 if(unit >= RAMD_NDEV) 131 return; 132 133 ri = &rd_info[unit]; 134 if (md->md_type != MD_UNCONFIGURED) 135 return; /* Only configure once */ 136 md->md_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK); 137 md->md_size = ri->ramd_size; 138 if(md->md_addr == NULL) 139 return; 140 if(ri->ramd_flag & RAMD_LOAD) { 141 if (loaddisk(md, ri->ramd_dev, curlwp)) { 142 free(md->md_addr, M_DEVBUF); 143 md->md_addr = NULL; 144 return; 145 } 146 } 147 md->md_type = MD_KMEM_ALLOCATED; 148 } 149 150 static int 151 loaddisk(struct md_conf *md, dev_t ld_dev, struct lwp *lwp) 152 { 153 struct buf *buf; 154 int error; 155 const struct bdevsw *bdp; 156 struct disklabel dl; 157 struct read_info rs; 158 159 bdp = bdevsw_lookup(ld_dev); 160 if (bdp == NULL) 161 return (ENXIO); 162 163 /* 164 * Initialize our buffer header: 165 */ 166 buf = getiobuf(NULL, false); 167 buf->b_cflags = BC_BUSY; 168 buf->b_dev = ld_dev; 169 buf->b_error = 0; 170 buf->b_proc = lwp->l_proc; 171 172 /* 173 * Setup read_info: 174 */ 175 rs.bp = buf; 176 rs.nbytes = md->md_size; 177 rs.offset = 0; 178 rs.bufp = md->md_addr; 179 rs.ebufp = (char *)md->md_addr + md->md_size; 180 rs.chunk = RAMD_CHUNK; 181 rs.media_sz = md->md_size; 182 rs.strat = bdp->d_strategy; 183 184 /* 185 * Open device and try to get some statistics. 186 */ 187 if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, lwp)) != 0) { 188 putiobuf(buf); 189 return(error); 190 } 191 if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (void *)&dl, FREAD, lwp) == 0) { 192 /* Read on a cylinder basis */ 193 rs.chunk = dl.d_secsize * dl.d_secpercyl; 194 rs.media_sz = dl.d_secperunit * dl.d_secsize; 195 } 196 197 #ifdef support_compression 198 if(ri->ramd_flag & RAMD_LCOMP) 199 error = decompress(cpy_uncompressed, md_compressed, &rs); 200 else 201 #endif /* support_compression */ 202 error = ramd_norm_read(&rs); 203 204 bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, lwp); 205 putiobuf(buf); 206 return(error); 207 } 208 209 static int 210 ramd_norm_read(struct read_info *rsp) 211 { 212 long bytes_left; 213 int done, error; 214 struct buf *bp; 215 int dotc = 0; 216 217 bytes_left = rsp->nbytes; 218 bp = rsp->bp; 219 error = 0; 220 221 while(bytes_left > 0) { 222 bp->b_cflags = BC_BUSY; 223 bp->b_flags = B_PHYS | B_READ; 224 bp->b_oflags &= ~BO_DONE; 225 bp->b_blkno = btodb(rsp->offset); 226 bp->b_bcount = min(rsp->chunk, bytes_left); 227 bp->b_data = rsp->bufp; 228 bp->b_error = 0; 229 230 /* Initiate read */ 231 (*rsp->strat)(bp); 232 233 /* Wait for results */ 234 biowait(bp); 235 error = bp->b_error; 236 237 /* Dot counter */ 238 printf("."); 239 if(!(++dotc % 40)) 240 printf("\n"); 241 242 done = bp->b_bcount - bp->b_resid; 243 244 bytes_left -= done; 245 rsp->offset += done; 246 rsp->bufp += done; 247 248 if(error || !done) 249 break; 250 251 if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) { 252 printf("\nInsert next media and hit any key..."); 253 cngetc(); 254 printf("\n"); 255 rsp->offset = 0; 256 } 257 } 258 printf("\n"); 259 return(error); 260 } 261 262 #ifdef support_compression 263 /* 264 * Functions supporting uncompression: 265 */ 266 /* 267 * Copy from the uncompression buffer to the ramdisk 268 */ 269 static int 270 cpy_uncompressed(void * buf, int nbyte, struct read_info *rsp) 271 { 272 if((rsp->bufp + nbyte) >= rsp->ebufp) 273 return(0); 274 memcpy( rsp->bufp, buf, nbyte); 275 rsp->bufp += nbyte; 276 return(0); 277 } 278 279 /* 280 * Read a maximum of 'nbyte' bytes into 'buf'. 281 */ 282 static int 283 md_compressed(void * buf, int nbyte, struct read_info *rsp) 284 { 285 static int dotc = 0; 286 struct buf *bp; 287 int nread = 0; 288 int done, error; 289 290 291 error = 0; 292 bp = rsp->bp; 293 nbyte &= ~(DEV_BSIZE - 1); 294 295 while(nbyte > 0) { 296 bp->b_cflags = BC_BUSY; 297 bp->b_flags = B_PHYS | B_READ; 298 bp->b_oflags &= ~BO_DONE; 299 bp->b_blkno = btodb(rsp->offset); 300 bp->b_bcount = min(rsp->chunk, nbyte); 301 bp->b_data = buf; 302 bp->b_error = 0; 303 304 /* Initiate read */ 305 (*rsp->strat)(bp); 306 307 /* Wait for results */ 308 biowait(bp); 309 error = bp->b_error; 310 311 /* Dot counter */ 312 printf("."); 313 if(!(++dotc % 40)) 314 printf("\n"); 315 316 done = bp->b_bcount - bp->b_resid; 317 318 nbyte -= done; 319 nread += done; 320 rsp->offset += done; 321 322 if(error || !done) 323 break; 324 325 if((rsp->offset == rsp->media_sz) && (nbyte != 0)) { 326 printf("\nInsert next media and hit any key..."); 327 if(cngetc() != '\n') 328 printf("\n"); 329 rsp->offset = 0; 330 } 331 } 332 return(nread); 333 } 334 #endif /* support_compression */ 335