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