Home | History | Annotate | Line # | Download | only in lib
biosdisk_ll.c revision 1.5
      1 /*	$NetBSD: biosdisk_ll.c,v 1.5 1998/10/15 15:28:22 ws 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 int int13_extension __P((int));
     52 extern int biosread __P((int, int, int, int, int, char *));
     53 extern int biosextread __P((int, void *));
     54 static int do_read __P((struct biosdisk_ll *, int, int, char *));
     55 
     56 #define	SPT(di)		((di)&0xff)
     57 #define	HEADS(di)	((((di)>>8)&0xff)+1)
     58 
     59 #ifndef BIOSDISK_RETRIES
     60 #define BIOSDISK_RETRIES 5
     61 #endif
     62 
     63 int
     64 set_geometry(d)
     65 	struct biosdisk_ll *d;
     66 {
     67 	int             diskinfo;
     68 
     69 	diskinfo = get_diskinfo(d->dev);
     70 
     71 	d->flags = 0;
     72 	if ((d->dev&0x80) && int13_extension(d->dev))
     73 		d->flags |= BIOSDISK_EXT13;
     74 
     75 	d->spc = (d->spt = SPT(diskinfo)) * HEADS(diskinfo);
     76 
     77 	/*
     78 	 * get_diskinfo assumes floppy if BIOS call fails. Check at least
     79 	 * "valid" geometry.
     80 	 */
     81 	return (!d->spc || !d->spt);
     82 }
     83 
     84 /*
     85  * Global shared "diskbuf" is used as read ahead buffer.  For reading from
     86  * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
     87  * this buffer doesn't cross a 64K DMA boundary.
     88  */
     89 #define RA_SECTORS      (DISKBUFSIZE / BIOSDISK_SECSIZE)
     90 static int      ra_dev;
     91 static int      ra_end;
     92 static int      ra_first;
     93 
     94 static int
     95 do_read(d, dblk, num, buf)
     96 	struct		biosdisk_ll *d;
     97 	int		dblk, num;
     98 	char	       *buf;
     99 {
    100 	int		cyl, head, sec, nsec;
    101 	struct {
    102 		int8_t	size;
    103 		int8_t	resvd;
    104 		int16_t	cnt;
    105 		int16_t	off;
    106 		int16_t	seg;
    107 		int64_t	sec;
    108 	}		ext;
    109 
    110 	if (d->flags & BIOSDISK_EXT13) {
    111 		ext.size = sizeof(ext);
    112 		ext.resvd = 0;
    113 		ext.cnt = num;
    114 		ext.off = (int32_t)buf;
    115 		ext.seg = ourseg;
    116 		ext.sec = dblk;
    117 
    118 		if (biosextread(d->dev, &ext))
    119 			return -1;
    120 
    121 		return ext.cnt;
    122 	} else {
    123 
    124 		cyl = dblk / d->spc;
    125 		head = (dblk % d->spc) / d->spt;
    126 		sec = dblk % d->spt;
    127 		nsec = d->spt - sec;
    128 
    129 		if (nsec > num)
    130 			nsec = num;
    131 
    132 		if (biosread(d->dev, cyl, head, sec, nsec, buf))
    133 			return -1;
    134 
    135 		return nsec;
    136 	}
    137 }
    138 
    139 int
    140 readsects(d, dblk, num, buf, cold)	/* reads ahead if (!cold) */
    141 	struct biosdisk_ll *d;
    142 	int             dblk, num;
    143 	char           *buf;
    144 	int             cold;	/* don't use data segment or bss, don't call
    145 				 * library functions */
    146 {
    147 	while (num) {
    148 		int             nsec;
    149 
    150 		/* check for usable data in read-ahead buffer */
    151 		if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
    152 		    || dblk < ra_first || dblk >= ra_end) {
    153 
    154 			/* no, read from disk */
    155 			char           *trbuf;
    156 			int retries = BIOSDISK_RETRIES;
    157 
    158 			nsec = num;
    159 
    160 			if (cold) {
    161 				/* transfer directly to buffer */
    162 				trbuf = buf;
    163 			} else {
    164 				/* fill read-ahead buffer */
    165 				trbuf = diskbuf;
    166 				if (nsec > RA_SECTORS)
    167 					nsec = RA_SECTORS;
    168 
    169 			}
    170 
    171 			while ((nsec = do_read(d, dblk, nsec, trbuf)) < 0) {
    172 #ifdef DISK_DEBUG
    173 				if (!cold)
    174 					printf("read error C:%d H:%d S:%d-%d\n",
    175 					       cyl, head, sec, sec + nsec - 1);
    176 #endif
    177 				if (--retries >= 0)
    178 					continue;
    179 				if (!cold)
    180 					diskbuf_user = 0; /* mark invalid */
    181 				return (-1);	/* XXX cannot output here if
    182 						 * (cold) */
    183 			}
    184 			if (!cold) {
    185 				ra_dev = d->dev;
    186 				ra_first = dblk;
    187 				ra_end = dblk + nsec;
    188 				diskbuf_user = &ra_dev;
    189 			}
    190 		} else		/* can take blocks from end of read-ahead
    191 				 * buffer */
    192 			nsec = ra_end - dblk;
    193 
    194 		if (!cold) {
    195 			/* copy data from read-ahead to user buffer */
    196 			if (nsec > num)
    197 				nsec = num;
    198 			bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
    199 			    buf, nsec * BIOSDISK_SECSIZE);
    200 		}
    201 		buf += nsec * BIOSDISK_SECSIZE;
    202 		num -= nsec;
    203 		dblk += nsec;
    204 	}
    205 
    206 	return (0);
    207 }
    208