Home | History | Annotate | Line # | Download | only in lib
biosdisk_ll.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: biosdisk_ll.c,v 1.2 1997/03/22 01:41:36 thorpej Exp $	 */
      2  1.1    perry 
      3  1.1    perry /*
      4  1.1    perry  * Copyright (c) 1996
      5  1.1    perry  * 	Matthias Drochner.  All rights reserved.
      6  1.1    perry  * Copyright (c) 1996
      7  1.1    perry  * 	Perry E. Metzger.  All rights reserved.
      8  1.1    perry  *
      9  1.1    perry  * Redistribution and use in source and binary forms, with or without
     10  1.1    perry  * modification, are permitted provided that the following conditions
     11  1.1    perry  * are met:
     12  1.1    perry  * 1. Redistributions of source code must retain the above copyright
     13  1.1    perry  *    notice, this list of conditions and the following disclaimer.
     14  1.1    perry  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1    perry  *    notice, this list of conditions and the following disclaimer in the
     16  1.1    perry  *    documentation and/or other materials provided with the distribution.
     17  1.1    perry  * 3. All advertising materials mentioning features or use of this software
     18  1.1    perry  *    must display the following acknowledgements:
     19  1.1    perry  *	This product includes software developed for the NetBSD Project
     20  1.1    perry  *	by Matthias Drochner.
     21  1.1    perry  *	This product includes software developed for the NetBSD Project
     22  1.1    perry  *	by Perry E. Metzger.
     23  1.1    perry  * 4. The names of the authors may not be used to endorse or promote products
     24  1.1    perry  *    derived from this software without specific prior written permission.
     25  1.1    perry  *
     26  1.1    perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  1.1    perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  1.1    perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  1.1    perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  1.1    perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31  1.1    perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32  1.1    perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33  1.1    perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34  1.1    perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35  1.1    perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36  1.1    perry  */
     37  1.1    perry 
     38  1.2  thorpej /*
     39  1.2  thorpej  * shared by bootsector startup (bootsectmain) and biosdisk.c needs lowlevel
     40  1.2  thorpej  * parts from bios_disk.S
     41  1.2  thorpej  */
     42  1.1    perry 
     43  1.1    perry #include <lib/libsa/stand.h>
     44  1.1    perry 
     45  1.1    perry #include "biosdisk_ll.h"
     46  1.1    perry #include "diskbuf.h"
     47  1.1    perry 
     48  1.1    perry extern int get_diskinfo __P((int));
     49  1.2  thorpej extern int biosread __P((int, int, int, int, int, char *));
     50  1.1    perry 
     51  1.1    perry #define	SPT(di)		((di)&0xff)
     52  1.1    perry #define	HEADS(di)	((((di)>>8)&0xff)+1)
     53  1.1    perry 
     54  1.2  thorpej int
     55  1.2  thorpej set_geometry(d)
     56  1.2  thorpej 	struct biosdisk_ll *d;
     57  1.1    perry {
     58  1.2  thorpej 	int             diskinfo;
     59  1.1    perry 
     60  1.2  thorpej 	diskinfo = get_diskinfo(d->dev);
     61  1.1    perry 
     62  1.2  thorpej 	d->spc = (d->spt = SPT(diskinfo)) * HEADS(diskinfo);
     63  1.1    perry 
     64  1.2  thorpej 	/*
     65  1.2  thorpej 	 * get_diskinfo assumes floppy if BIOS call fails. Check at least
     66  1.2  thorpej 	 * "valid" geometry.
     67  1.2  thorpej 	 */
     68  1.2  thorpej 	return (!d->spc || !d->spt);
     69  1.1    perry }
     70  1.1    perry 
     71  1.2  thorpej /*
     72  1.2  thorpej  * Global shared "diskbuf" is used as read ahead buffer.  For reading from
     73  1.2  thorpej  * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
     74  1.2  thorpej  * this buffer doesn't cross a 64K DMA boundary.
     75  1.1    perry  */
     76  1.1    perry #define RA_SECTORS      (DISKBUFSIZE / BIOSDISK_SECSIZE)
     77  1.2  thorpej static int      ra_dev;
     78  1.2  thorpej static int      ra_end;
     79  1.2  thorpej static int      ra_first;
     80  1.2  thorpej 
     81  1.2  thorpej int
     82  1.2  thorpej readsects(d, dblk, num, buf, cold)	/* reads ahead if (!cold) */
     83  1.2  thorpej 	struct biosdisk_ll *d;
     84  1.2  thorpej 	int             dblk, num;
     85  1.2  thorpej 	char           *buf;
     86  1.2  thorpej 	int             cold;	/* don't use data segment or bss, don't call
     87  1.2  thorpej 				 * library functions */
     88  1.1    perry {
     89  1.2  thorpej 	while (num) {
     90  1.2  thorpej 		int             nsec;
     91  1.1    perry 
     92  1.2  thorpej 		/* check for usable data in read-ahead buffer */
     93  1.2  thorpej 		if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
     94  1.2  thorpej 		    || dblk < ra_first || dblk >= ra_end) {
     95  1.2  thorpej 
     96  1.2  thorpej 			/* no, read from disk */
     97  1.2  thorpej 			int             cyl, head, sec;
     98  1.2  thorpej 			char           *trbuf;
     99  1.2  thorpej 
    100  1.2  thorpej 			cyl = dblk / d->spc;
    101  1.2  thorpej 			head = (dblk % d->spc) / d->spt;
    102  1.2  thorpej 			sec = dblk % d->spt;
    103  1.2  thorpej 			nsec = d->spt - sec;
    104  1.2  thorpej 
    105  1.2  thorpej 			if (cold) {
    106  1.2  thorpej 				/* transfer directly to buffer */
    107  1.2  thorpej 				trbuf = buf;
    108  1.2  thorpej 				if (nsec > num)
    109  1.2  thorpej 					nsec = num;
    110  1.2  thorpej 			} else {
    111  1.2  thorpej 				/* fill read-ahead buffer */
    112  1.2  thorpej 				trbuf = diskbuf;
    113  1.2  thorpej 				if (nsec > RA_SECTORS)
    114  1.2  thorpej 					nsec = RA_SECTORS;
    115  1.2  thorpej 
    116  1.2  thorpej 				ra_dev = d->dev;
    117  1.2  thorpej 				ra_first = dblk;
    118  1.2  thorpej 				ra_end = dblk + nsec;
    119  1.2  thorpej 				diskbuf_user = &ra_dev;
    120  1.2  thorpej 			}
    121  1.2  thorpej 
    122  1.2  thorpej 			if (biosread(d->dev, cyl, head, sec, nsec, trbuf)) {
    123  1.2  thorpej 				if (!cold)
    124  1.2  thorpej 					diskbuf_user = 0; /* mark invalid */
    125  1.2  thorpej 				return (-1);	/* XXX cannot output here if
    126  1.2  thorpej 						 * (cold) */
    127  1.2  thorpej 			}
    128  1.2  thorpej 		} else		/* can take blocks from end of read-ahead
    129  1.2  thorpej 				 * buffer */
    130  1.2  thorpej 			nsec = ra_end - dblk;
    131  1.2  thorpej 
    132  1.2  thorpej 		if (!cold) {
    133  1.2  thorpej 			/* copy data from read-ahead to user buffer */
    134  1.2  thorpej 			if (nsec > num)
    135  1.2  thorpej 				nsec = num;
    136  1.2  thorpej 			bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
    137  1.2  thorpej 			    buf, nsec * BIOSDISK_SECSIZE);
    138  1.2  thorpej 		}
    139  1.2  thorpej 		buf += nsec * BIOSDISK_SECSIZE;
    140  1.2  thorpej 		num -= nsec;
    141  1.2  thorpej 		dblk += nsec;
    142  1.1    perry 	}
    143  1.1    perry 
    144  1.2  thorpej 	return (0);
    145  1.1    perry }
    146