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