Home | History | Annotate | Line # | Download | only in dev
md_root.c revision 1.1
      1  1.1  leo /*	$NetBSD: md_root.c,v 1.1 1996/03/14 21:41:51 leo Exp $	*/
      2  1.1  leo 
      3  1.1  leo /*
      4  1.1  leo  * Copyright (c) 1996 Leo Weppelman.
      5  1.1  leo  * All rights reserved.
      6  1.1  leo  *
      7  1.1  leo  * Redistribution and use in source and binary forms, with or without
      8  1.1  leo  * modification, are permitted provided that the following conditions
      9  1.1  leo  * are met:
     10  1.1  leo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  leo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  leo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  leo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  leo  *    documentation and/or other materials provided with the distribution.
     15  1.1  leo  * 3. All advertising materials mentioning features or use of this software
     16  1.1  leo  *    must display the following acknowledgement:
     17  1.1  leo  *      This product includes software developed by Leo Weppelman.
     18  1.1  leo  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  leo  *    derived from this software without specific prior written permission
     20  1.1  leo  *
     21  1.1  leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  leo  */
     32  1.1  leo 
     33  1.1  leo #include <sys/param.h>
     34  1.1  leo #include <sys/systm.h>
     35  1.1  leo #include <sys/kernel.h>
     36  1.1  leo #include <sys/malloc.h>
     37  1.1  leo #include <sys/buf.h>
     38  1.1  leo #include <sys/proc.h>
     39  1.1  leo #include <sys/device.h>
     40  1.1  leo #include <sys/ioctl.h>
     41  1.1  leo #include <sys/fcntl.h>
     42  1.1  leo #include <sys/conf.h>
     43  1.1  leo #include <sys/disklabel.h>
     44  1.1  leo #include <sys/disk.h>
     45  1.1  leo #include <sys/dkbad.h>
     46  1.1  leo 
     47  1.1  leo #include <dev/ramdisk.h>
     48  1.1  leo 
     49  1.1  leo /*
     50  1.1  leo  * Misc. defines:
     51  1.1  leo  */
     52  1.1  leo #define	RAMD_CHUNK	(9 * 512)	/* Chunk-size for auto-load	*/
     53  1.1  leo #define	RAMD_NDEV	2		/* Number of devices configured	*/
     54  1.1  leo 
     55  1.1  leo struct   ramd_info {
     56  1.1  leo 	u_long	ramd_size;  /* Size of disk in bytes			*/
     57  1.1  leo 	u_long	ramd_flag;  /* see defs below				*/
     58  1.1  leo 	dev_t	ramd_dev;   /* device to load from			*/
     59  1.1  leo };
     60  1.1  leo 
     61  1.1  leo /*
     62  1.1  leo  * ramd_flag:
     63  1.1  leo  */
     64  1.1  leo #define	RAMD_LOAD	0x01	/* Auto load when first opened	*/
     65  1.1  leo #define	RAMD_LCOMP	0x02	/* Input is compressed		*/
     66  1.1  leo 
     67  1.1  leo struct ramd_info rd_info[RAMD_NDEV] = {
     68  1.1  leo     {
     69  1.1  leo 	1105920,		/*	1Mb in 2160 sectors		*/
     70  1.1  leo 	RAMD_LOAD,		/* auto-load this device		*/
     71  1.1  leo 	MAKEDISKDEV(2, 0, 1),	/* XXX: This is crap! (720Kb flop)	*/
     72  1.1  leo     },
     73  1.1  leo     {
     74  1.1  leo 	1474560,		/*	1.44Mb in 2880 sectors		*/
     75  1.1  leo 	RAMD_LOAD,		/* auto-load this device		*/
     76  1.1  leo 	MAKEDISKDEV(2, 0, 1),	/* XXX: This is crap! (720Kb flop)	*/
     77  1.1  leo     }
     78  1.1  leo };
     79  1.1  leo 
     80  1.1  leo struct read_info {
     81  1.1  leo     struct buf	*bp;		/* buffer for strategy function		*/
     82  1.1  leo     long	nbytes;		/* total number of bytes to read	*/
     83  1.1  leo     long	offset;		/* offset in input medium		*/
     84  1.1  leo     caddr_t	bufp;		/* current output buffer		*/
     85  1.1  leo     caddr_t	ebufp;		/* absolute maximum for bufp		*/
     86  1.1  leo     int		chunk;		/* chunk size on input medium		*/
     87  1.1  leo     int		media_sz;	/* size of input medium			*/
     88  1.1  leo     void	(*strat)(struct buf *);	/* strategy function for read	*/
     89  1.1  leo };
     90  1.1  leo 
     91  1.1  leo 
     92  1.1  leo static int  loaddisk __P((struct  rd_conf *, dev_t ld_dev, struct proc *));
     93  1.1  leo static int  ramd_norm_read __P((struct read_info *));
     94  1.1  leo static int  cpy_uncompressed __P((caddr_t, int, struct read_info *));
     95  1.1  leo static int  rd_compressed __P((caddr_t, int, struct read_info *));
     96  1.1  leo 
     97  1.1  leo /*
     98  1.1  leo  * This is called during autoconfig.
     99  1.1  leo  */
    100  1.1  leo int
    101  1.1  leo rd_match_hook(parent, self, aux)
    102  1.1  leo 	struct device	*parent;
    103  1.1  leo 	void	*self;
    104  1.1  leo 	void	*aux;
    105  1.1  leo {
    106  1.1  leo 	if(strcmp("rd", aux) && strcmp("*", aux))
    107  1.1  leo 		return(0);
    108  1.1  leo 	return(1);
    109  1.1  leo }
    110  1.1  leo 
    111  1.1  leo void
    112  1.1  leo rd_attach_hook(unit, rd)
    113  1.1  leo int		unit;
    114  1.1  leo struct rd_conf	*rd;
    115  1.1  leo {
    116  1.1  leo 	extern int	atari_realconfig;
    117  1.1  leo 
    118  1.1  leo 	if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
    119  1.1  leo 		printf (":%sauto-load on open. Size %d bytes.",
    120  1.1  leo 		    rd_info[unit].ramd_flag & RAMD_LCOMP ? " decompress/" : "",
    121  1.1  leo 		    rd_info[unit].ramd_size);
    122  1.1  leo 		rd->rd_type = RD_UNCONFIGURED; /* Paranoia... */
    123  1.1  leo 	}
    124  1.1  leo }
    125  1.1  leo 
    126  1.1  leo void
    127  1.1  leo rd_open_hook(unit, rd)
    128  1.1  leo int		unit;
    129  1.1  leo struct rd_conf	*rd;
    130  1.1  leo {
    131  1.1  leo 	struct ramd_info *ri;
    132  1.1  leo 	int		 s;
    133  1.1  leo 	int		 error = 0;
    134  1.1  leo 
    135  1.1  leo 	if(unit >= RAMD_NDEV)
    136  1.1  leo 		return;
    137  1.1  leo 
    138  1.1  leo 	ri = &rd_info[unit];
    139  1.1  leo 	if (rd->rd_type != RD_UNCONFIGURED)
    140  1.1  leo 		return;	/* Only configure once */
    141  1.1  leo 	rd->rd_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
    142  1.1  leo 	rd->rd_size = ri->ramd_size;
    143  1.1  leo 	if(rd->rd_addr == NULL)
    144  1.1  leo 		return;
    145  1.1  leo 	if(ri->ramd_flag & RAMD_LOAD) {
    146  1.1  leo 		if (loaddisk(rd, ri->ramd_dev, curproc)) {
    147  1.1  leo 			free(rd->rd_addr, M_DEVBUF);
    148  1.1  leo 			rd->rd_addr = NULL;
    149  1.1  leo 			return;
    150  1.1  leo 		}
    151  1.1  leo 	}
    152  1.1  leo 	rd->rd_type = RD_KMEM_ALLOCATED;
    153  1.1  leo }
    154  1.1  leo 
    155  1.1  leo static int
    156  1.1  leo loaddisk(rd, ld_dev, proc)
    157  1.1  leo struct rd_conf		*rd;
    158  1.1  leo dev_t			ld_dev;
    159  1.1  leo struct proc		*proc;
    160  1.1  leo {
    161  1.1  leo 	struct buf		buf;
    162  1.1  leo 	int			error;
    163  1.1  leo 	struct bdevsw		*bdp = &bdevsw[major(ld_dev)];
    164  1.1  leo 	struct disklabel	dl;
    165  1.1  leo 	struct read_info	rs;
    166  1.1  leo 
    167  1.1  leo 	/*
    168  1.1  leo 	 * Initialize our buffer header:
    169  1.1  leo 	 */
    170  1.1  leo 	buf.b_actf  = NULL;
    171  1.1  leo 	buf.b_rcred = buf.b_wcred = proc->p_ucred;
    172  1.1  leo 	buf.b_vnbufs.le_next = NOLIST;
    173  1.1  leo 	buf.b_flags = B_BUSY;
    174  1.1  leo 	buf.b_dev   = ld_dev;
    175  1.1  leo 	buf.b_error = 0;
    176  1.1  leo 	buf.b_proc  = proc;
    177  1.1  leo 
    178  1.1  leo 	/*
    179  1.1  leo 	 * Setup read_info:
    180  1.1  leo 	 */
    181  1.1  leo 	rs.bp       = &buf;
    182  1.1  leo 	rs.nbytes   = rd->rd_size;
    183  1.1  leo 	rs.offset   = 0;
    184  1.1  leo 	rs.bufp     = rd->rd_addr;
    185  1.1  leo 	rs.ebufp    = rd->rd_addr + rd->rd_size;
    186  1.1  leo 	rs.chunk    = RAMD_CHUNK;
    187  1.1  leo 	rs.media_sz = rd->rd_size;
    188  1.1  leo 	rs.strat    = bdp->d_strategy;
    189  1.1  leo 
    190  1.1  leo 	/*
    191  1.1  leo 	 * Open device and try to get some statistics.
    192  1.1  leo 	 */
    193  1.1  leo 	if(error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, proc))
    194  1.1  leo 		return(error);
    195  1.1  leo 	if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 0) {
    196  1.1  leo 		/* Read on a cylinder basis */
    197  1.1  leo 		rs.chunk    = dl.d_secsize * dl.d_secpercyl;
    198  1.1  leo 		rs.media_sz = dl.d_secperunit * dl.d_secsize;
    199  1.1  leo 	}
    200  1.1  leo 
    201  1.1  leo #ifdef support_compression
    202  1.1  leo 	if(ri->ramd_flag & RAMD_LCOMP)
    203  1.1  leo 		error = decompress(cpy_uncompressed, rd_compressed, &rs);
    204  1.1  leo 	else
    205  1.1  leo #endif /* support_compression */
    206  1.1  leo 		error = ramd_norm_read(&rs);
    207  1.1  leo 
    208  1.1  leo 	bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, proc);
    209  1.1  leo 	return(error);
    210  1.1  leo }
    211  1.1  leo 
    212  1.1  leo static int
    213  1.1  leo ramd_norm_read(rsp)
    214  1.1  leo struct read_info	*rsp;
    215  1.1  leo {
    216  1.1  leo 	long		bytes_left;
    217  1.1  leo 	int		done, error;
    218  1.1  leo 	struct buf	*bp;
    219  1.1  leo 	int		s;
    220  1.1  leo 	int		dotc = 0;
    221  1.1  leo 
    222  1.1  leo 	bytes_left = rsp->nbytes;
    223  1.1  leo 	bp         = rsp->bp;
    224  1.1  leo 	error      = 0;
    225  1.1  leo 
    226  1.1  leo 	while(bytes_left > 0) {
    227  1.1  leo 		s = splbio();
    228  1.1  leo 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    229  1.1  leo 		splx(s);
    230  1.1  leo 		bp->b_blkno  = btodb(rsp->offset);
    231  1.1  leo 		bp->b_bcount = rsp->chunk;
    232  1.1  leo 		bp->b_data   = rsp->bufp;
    233  1.1  leo 
    234  1.1  leo 		/* Initiate read */
    235  1.1  leo 		(*rsp->strat)(bp);
    236  1.1  leo 
    237  1.1  leo 		/* Wait for results	*/
    238  1.1  leo 		s = splbio();
    239  1.1  leo 		while ((bp->b_flags & B_DONE) == 0)
    240  1.1  leo 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    241  1.1  leo 		if (bp->b_flags & B_ERROR)
    242  1.1  leo 			error = (bp->b_error ? bp->b_error : EIO);
    243  1.1  leo 		splx(s);
    244  1.1  leo 
    245  1.1  leo 		/* Dot counter */
    246  1.1  leo 		printf(".");
    247  1.1  leo 		if(!(++dotc % 40))
    248  1.1  leo 			printf("\n");
    249  1.1  leo 
    250  1.1  leo 		done = bp->b_bcount - bp->b_resid;
    251  1.1  leo 		bytes_left   -= done;
    252  1.1  leo 		rsp->offset  += done;
    253  1.1  leo 		rsp->bufp    += done;
    254  1.1  leo 
    255  1.1  leo 		if(error || !done)
    256  1.1  leo 			break;
    257  1.1  leo 
    258  1.1  leo 		if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
    259  1.1  leo 			printf("\nInsert next media and hit any key...");
    260  1.1  leo 			cngetc();
    261  1.1  leo 			printf("\n");
    262  1.1  leo 			rsp->offset = 0;
    263  1.1  leo 		}
    264  1.1  leo 	}
    265  1.1  leo 	printf("\n");
    266  1.1  leo 	return(error);
    267  1.1  leo }
    268  1.1  leo 
    269  1.1  leo #ifdef support_compression
    270  1.1  leo /*
    271  1.1  leo  * Functions supporting uncompression:
    272  1.1  leo  */
    273  1.1  leo /*
    274  1.1  leo  * Copy from the uncompression buffer to the ramdisk
    275  1.1  leo  */
    276  1.1  leo static int
    277  1.1  leo cpy_uncompressed(buf, nbyte, rsp)
    278  1.1  leo caddr_t			buf;
    279  1.1  leo struct read_info	*rsp;
    280  1.1  leo int			nbyte;
    281  1.1  leo {
    282  1.1  leo 	if((rsp->bufp + nbyte) >= rsp->ebufp)
    283  1.1  leo 		return(0);
    284  1.1  leo 	bcopy(buf, rsp->bufp, nbyte);
    285  1.1  leo 	rsp->bufp += nbyte;
    286  1.1  leo 	return(0);
    287  1.1  leo }
    288  1.1  leo 
    289  1.1  leo /*
    290  1.1  leo  * Read a maximum of 'nbyte' bytes into 'buf'.
    291  1.1  leo  */
    292  1.1  leo static int
    293  1.1  leo rd_compressed(buf, nbyte, rsp)
    294  1.1  leo caddr_t			buf;
    295  1.1  leo struct read_info	*rsp;
    296  1.1  leo int			nbyte;
    297  1.1  leo {
    298  1.1  leo 	static int	dotc = 0;
    299  1.1  leo 	struct buf	*bp;
    300  1.1  leo 	       int	nread = 0;
    301  1.1  leo 	       int	s;
    302  1.1  leo 	       int	done, error;
    303  1.1  leo 
    304  1.1  leo 
    305  1.1  leo 	error  = 0;
    306  1.1  leo 	bp     = rsp->bp;
    307  1.1  leo 	nbyte &= ~(DEV_BSIZE - 1);
    308  1.1  leo 
    309  1.1  leo 	while(nbyte > 0) {
    310  1.1  leo 		s = splbio();
    311  1.1  leo 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    312  1.1  leo 		splx(s);
    313  1.1  leo 		bp->b_blkno  = btodb(rsp->offset);
    314  1.1  leo 		bp->b_bcount = min(rsp->chunk, nbyte);
    315  1.1  leo 		bp->b_data   = buf;
    316  1.1  leo 
    317  1.1  leo 		/* Initiate read */
    318  1.1  leo 		(*rsp->strat)(bp);
    319  1.1  leo 
    320  1.1  leo 		/* Wait for results	*/
    321  1.1  leo 		s = splbio();
    322  1.1  leo 		while ((bp->b_flags & B_DONE) == 0)
    323  1.1  leo 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    324  1.1  leo 		if (bp->b_flags & B_ERROR)
    325  1.1  leo 			error = (bp->b_error ? bp->b_error : EIO);
    326  1.1  leo 		splx(s);
    327  1.1  leo 
    328  1.1  leo 		/* Dot counter */
    329  1.1  leo 		printf(".");
    330  1.1  leo 		if(!(++dotc % 40))
    331  1.1  leo 			printf("\n");
    332  1.1  leo 
    333  1.1  leo 		done = bp->b_bcount - bp->b_resid;
    334  1.1  leo 		nbyte        -= done;
    335  1.1  leo 		nread        += done;
    336  1.1  leo 		rsp->offset  += done;
    337  1.1  leo 
    338  1.1  leo 		if(error || !done)
    339  1.1  leo 			break;
    340  1.1  leo 
    341  1.1  leo 		if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
    342  1.1  leo 		if(rsp->offset == rsp->media_sz) {
    343  1.1  leo 			printf("\nInsert next media and hit any key...");
    344  1.1  leo 			if(cngetc() != '\n')
    345  1.1  leo 				printf("\n");
    346  1.1  leo 			rsp->offset = 0;
    347  1.1  leo 		}
    348  1.1  leo 	}
    349  1.1  leo 	s = splbio();
    350  1.1  leo 	splx(s);
    351  1.1  leo 	return(nread);
    352  1.1  leo }
    353  1.1  leo #endif /* support_compression */
    354