Home | History | Annotate | Line # | Download | only in lib
biosdisk_ll.c revision 1.11.4.2
      1  1.11.4.2  perry /*	$NetBSD: biosdisk_ll.c,v 1.11.4.2 2001/07/07 22:57:58 perry Exp $	 */
      2  1.11.4.2  perry 
      3  1.11.4.2  perry /*
      4  1.11.4.2  perry  * Copyright (c) 1996
      5  1.11.4.2  perry  * 	Matthias Drochner.  All rights reserved.
      6  1.11.4.2  perry  * Copyright (c) 1996
      7  1.11.4.2  perry  * 	Perry E. Metzger.  All rights reserved.
      8  1.11.4.2  perry  *
      9  1.11.4.2  perry  * Redistribution and use in source and binary forms, with or without
     10  1.11.4.2  perry  * modification, are permitted provided that the following conditions
     11  1.11.4.2  perry  * are met:
     12  1.11.4.2  perry  * 1. Redistributions of source code must retain the above copyright
     13  1.11.4.2  perry  *    notice, this list of conditions and the following disclaimer.
     14  1.11.4.2  perry  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.11.4.2  perry  *    notice, this list of conditions and the following disclaimer in the
     16  1.11.4.2  perry  *    documentation and/or other materials provided with the distribution.
     17  1.11.4.2  perry  * 3. All advertising materials mentioning features or use of this software
     18  1.11.4.2  perry  *    must display the following acknowledgements:
     19  1.11.4.2  perry  *	This product includes software developed for the NetBSD Project
     20  1.11.4.2  perry  *	by Matthias Drochner.
     21  1.11.4.2  perry  *	This product includes software developed for the NetBSD Project
     22  1.11.4.2  perry  *	by Perry E. Metzger.
     23  1.11.4.2  perry  * 4. The names of the authors may not be used to endorse or promote products
     24  1.11.4.2  perry  *    derived from this software without specific prior written permission.
     25  1.11.4.2  perry  *
     26  1.11.4.2  perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  1.11.4.2  perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  1.11.4.2  perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  1.11.4.2  perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  1.11.4.2  perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31  1.11.4.2  perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32  1.11.4.2  perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33  1.11.4.2  perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34  1.11.4.2  perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35  1.11.4.2  perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36  1.11.4.2  perry  */
     37  1.11.4.2  perry 
     38  1.11.4.2  perry /*
     39  1.11.4.2  perry  * shared by bootsector startup (bootsectmain) and biosdisk.c
     40  1.11.4.2  perry  * needs lowlevel parts from bios_disk.S
     41  1.11.4.2  perry  */
     42  1.11.4.2  perry 
     43  1.11.4.2  perry #include <lib/libsa/stand.h>
     44  1.11.4.2  perry 
     45  1.11.4.2  perry #include "biosdisk_ll.h"
     46  1.11.4.2  perry #include "diskbuf.h"
     47  1.11.4.2  perry 
     48  1.11.4.2  perry extern long ourseg;
     49  1.11.4.2  perry 
     50  1.11.4.2  perry extern int get_diskinfo __P((int));
     51  1.11.4.2  perry extern void int13_getextinfo __P((int, struct biosdisk_ext13info *));
     52  1.11.4.2  perry extern int int13_extension __P((int));
     53  1.11.4.2  perry extern int biosread __P((int, int, int, int, int, char *));
     54  1.11.4.2  perry extern int biosextread __P((int, void *));
     55  1.11.4.2  perry static int do_read __P((struct biosdisk_ll *, int, int, char *));
     56  1.11.4.2  perry 
     57  1.11.4.2  perry /*
     58  1.11.4.2  perry  * we get from get_diskinfo():
     59  1.11.4.2  perry  * xxxx  %ch  %cl  %dh (registers after int13/8), ie
     60  1.11.4.2  perry  * xxxx cccc Csss hhhh
     61  1.11.4.2  perry  */
     62  1.11.4.2  perry #define	SPT(di)		(((di)>>8)&0x3f)
     63  1.11.4.2  perry #define	HEADS(di)	(((di)&0xff)+1)
     64  1.11.4.2  perry #define CYL(di)		(((((di)>>16)&0xff)|(((di)>>6)&0x300))+1)
     65  1.11.4.2  perry 
     66  1.11.4.2  perry #ifndef BIOSDISK_RETRIES
     67  1.11.4.2  perry #define BIOSDISK_RETRIES 5
     68  1.11.4.2  perry #endif
     69  1.11.4.2  perry 
     70  1.11.4.2  perry int
     71  1.11.4.2  perry set_geometry(d, ed)
     72  1.11.4.2  perry 	struct biosdisk_ll *d;
     73  1.11.4.2  perry 	struct biosdisk_ext13info *ed;
     74  1.11.4.2  perry {
     75  1.11.4.2  perry 	int diskinfo;
     76  1.11.4.2  perry 
     77  1.11.4.2  perry 	diskinfo = get_diskinfo(d->dev);
     78  1.11.4.2  perry 	d->sec = SPT(diskinfo);
     79  1.11.4.2  perry 	d->head = HEADS(diskinfo);
     80  1.11.4.2  perry 	d->cyl = CYL(diskinfo);
     81  1.11.4.2  perry 	d->chs_sectors = d->sec * d->head * d->cyl;
     82  1.11.4.2  perry 
     83  1.11.4.2  perry 	d->flags = 0;
     84  1.11.4.2  perry 	if ((d->dev & 0x80) && int13_extension(d->dev)) {
     85  1.11.4.2  perry 		d->flags |= BIOSDISK_EXT13;
     86  1.11.4.2  perry 		if (ed != NULL)
     87  1.11.4.2  perry 			int13_getextinfo(d->dev, ed);
     88  1.11.4.2  perry 	}
     89  1.11.4.2  perry 
     90  1.11.4.2  perry 	/*
     91  1.11.4.2  perry 	 * get_diskinfo assumes floppy if BIOS call fails. Check at least
     92  1.11.4.2  perry 	 * "valid" geometry.
     93  1.11.4.2  perry 	 */
     94  1.11.4.2  perry 	return (!d->sec || !d->head);
     95  1.11.4.2  perry }
     96  1.11.4.2  perry 
     97  1.11.4.2  perry /*
     98  1.11.4.2  perry  * Global shared "diskbuf" is used as read ahead buffer.  For reading from
     99  1.11.4.2  perry  * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
    100  1.11.4.2  perry  * this buffer doesn't cross a 64K DMA boundary.
    101  1.11.4.2  perry  */
    102  1.11.4.2  perry #define RA_SECTORS      (DISKBUFSIZE / BIOSDISK_SECSIZE)
    103  1.11.4.2  perry static int      ra_dev;
    104  1.11.4.2  perry static int      ra_end;
    105  1.11.4.2  perry static int      ra_first;
    106  1.11.4.2  perry 
    107  1.11.4.2  perry static int
    108  1.11.4.2  perry do_read(d, dblk, num, buf)
    109  1.11.4.2  perry 	struct		biosdisk_ll *d;
    110  1.11.4.2  perry 	int		dblk, num;
    111  1.11.4.2  perry 	char	       *buf;
    112  1.11.4.2  perry {
    113  1.11.4.2  perry 	int		cyl, head, sec, nsec, spc;
    114  1.11.4.2  perry 	struct {
    115  1.11.4.2  perry 		int8_t	size;
    116  1.11.4.2  perry 		int8_t	resvd;
    117  1.11.4.2  perry 		int16_t	cnt;
    118  1.11.4.2  perry 		int16_t	off;
    119  1.11.4.2  perry 		int16_t	seg;
    120  1.11.4.2  perry 		int64_t	sec;
    121  1.11.4.2  perry 	}		ext;
    122  1.11.4.2  perry 
    123  1.11.4.2  perry 	if ((d->dev & 0x80) && (dblk + num) >= d->chs_sectors) {
    124  1.11.4.2  perry 		if (!(d->flags & BIOSDISK_EXT13))
    125  1.11.4.2  perry 			return -1;
    126  1.11.4.2  perry 		ext.size = sizeof(ext);
    127  1.11.4.2  perry 		ext.resvd = 0;
    128  1.11.4.2  perry 		ext.cnt = num;
    129  1.11.4.2  perry 		ext.off = (int32_t)buf;
    130  1.11.4.2  perry 		ext.seg = ourseg;
    131  1.11.4.2  perry 		ext.sec = dblk;
    132  1.11.4.2  perry 
    133  1.11.4.2  perry 		if (biosextread(d->dev, &ext))
    134  1.11.4.2  perry 			return -1;
    135  1.11.4.2  perry 
    136  1.11.4.2  perry 		return ext.cnt;
    137  1.11.4.2  perry 	} else {
    138  1.11.4.2  perry 		spc = d->head * d->sec;
    139  1.11.4.2  perry 		cyl = dblk / spc;
    140  1.11.4.2  perry 		head = (dblk % spc) / d->sec;
    141  1.11.4.2  perry 		sec = dblk % d->sec;
    142  1.11.4.2  perry 		nsec = d->sec - sec;
    143  1.11.4.2  perry 
    144  1.11.4.2  perry 		if (nsec > num)
    145  1.11.4.2  perry 			nsec = num;
    146  1.11.4.2  perry 
    147  1.11.4.2  perry 		if (biosread(d->dev, cyl, head, sec, nsec, buf))
    148  1.11.4.2  perry 			return -1;
    149  1.11.4.2  perry 
    150  1.11.4.2  perry 		return nsec;
    151  1.11.4.2  perry 	}
    152  1.11.4.2  perry }
    153  1.11.4.2  perry 
    154  1.11.4.2  perry int
    155  1.11.4.2  perry readsects(d, dblk, num, buf, cold)	/* reads ahead if (!cold) */
    156  1.11.4.2  perry 	struct biosdisk_ll *d;
    157  1.11.4.2  perry 	int             dblk, num;
    158  1.11.4.2  perry 	char           *buf;
    159  1.11.4.2  perry 	int             cold;	/* don't use data segment or bss, don't call
    160  1.11.4.2  perry 				 * library functions */
    161  1.11.4.2  perry {
    162  1.11.4.2  perry 	while (num) {
    163  1.11.4.2  perry 		int             nsec;
    164  1.11.4.2  perry 
    165  1.11.4.2  perry 		/* check for usable data in read-ahead buffer */
    166  1.11.4.2  perry 		if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
    167  1.11.4.2  perry 		    || dblk < ra_first || dblk >= ra_end) {
    168  1.11.4.2  perry 
    169  1.11.4.2  perry 			/* no, read from disk */
    170  1.11.4.2  perry 			char           *trbuf;
    171  1.11.4.2  perry 			int maxsecs;
    172  1.11.4.2  perry 			int retries = BIOSDISK_RETRIES;
    173  1.11.4.2  perry 
    174  1.11.4.2  perry 			if (cold) {
    175  1.11.4.2  perry 				/* transfer directly to buffer */
    176  1.11.4.2  perry 				trbuf = buf;
    177  1.11.4.2  perry 				maxsecs = num;
    178  1.11.4.2  perry 			} else {
    179  1.11.4.2  perry 				/* fill read-ahead buffer */
    180  1.11.4.2  perry 				trbuf = diskbuf;
    181  1.11.4.2  perry 				maxsecs = RA_SECTORS;
    182  1.11.4.2  perry 				diskbuf_user = 0; /* not yet valid */
    183  1.11.4.2  perry 			}
    184  1.11.4.2  perry 
    185  1.11.4.2  perry 			while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) {
    186  1.11.4.2  perry #ifdef DISK_DEBUG
    187  1.11.4.2  perry 				if (!cold)
    188  1.11.4.2  perry 					printf("read error dblk %d-%d\n", dblk,
    189  1.11.4.2  perry 					       dblk + maxsecs - 1);
    190  1.11.4.2  perry #endif
    191  1.11.4.2  perry 				if (--retries >= 0)
    192  1.11.4.2  perry 					continue;
    193  1.11.4.2  perry 				return (-1);	/* XXX cannot output here if
    194  1.11.4.2  perry 						 * (cold) */
    195  1.11.4.2  perry 			}
    196  1.11.4.2  perry 			if (!cold) {
    197  1.11.4.2  perry 				ra_dev = d->dev;
    198  1.11.4.2  perry 				ra_first = dblk;
    199  1.11.4.2  perry 				ra_end = dblk + nsec;
    200  1.11.4.2  perry 				diskbuf_user = &ra_dev;
    201  1.11.4.2  perry 			}
    202  1.11.4.2  perry 		} else		/* can take blocks from end of read-ahead
    203  1.11.4.2  perry 				 * buffer */
    204  1.11.4.2  perry 			nsec = ra_end - dblk;
    205  1.11.4.2  perry 
    206  1.11.4.2  perry 		if (!cold) {
    207  1.11.4.2  perry 			/* copy data from read-ahead to user buffer */
    208  1.11.4.2  perry 			if (nsec > num)
    209  1.11.4.2  perry 				nsec = num;
    210  1.11.4.2  perry 			memcpy(buf,
    211  1.11.4.2  perry 			       diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
    212  1.11.4.2  perry 			       nsec * BIOSDISK_SECSIZE);
    213  1.11.4.2  perry 		}
    214  1.11.4.2  perry 		buf += nsec * BIOSDISK_SECSIZE;
    215  1.11.4.2  perry 		num -= nsec;
    216  1.11.4.2  perry 		dblk += nsec;
    217  1.11.4.2  perry 	}
    218  1.11.4.2  perry 
    219  1.11.4.2  perry 	return (0);
    220  1.11.4.2  perry }
    221