Home | History | Annotate | Line # | Download | only in lib
biosdisk_ll.c revision 1.18
      1 /*	$NetBSD: biosdisk_ll.c,v 1.18 2003/06/25 04:21:51 thorpej 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 int get_diskinfo(int);
     49 extern void int13_getextinfo(int, struct biosdisk_ext13info *);
     50 extern int int13_extension(int);
     51 extern int biosread(int, int, int, int, int, void *);
     52 extern int biosdiskreset(int);
     53 extern int biosextread(int, void *);
     54 static int do_read(struct biosdisk_ll *, daddr_t, int, char *);
     55 extern u_int vtophys(void *);
     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(struct biosdisk_ll *d, struct biosdisk_ext13info *ed)
     72 {
     73 	int diskinfo;
     74 	char buf[512];
     75 
     76 	diskinfo = get_diskinfo(d->dev);
     77 	d->sec = SPT(diskinfo);
     78 	d->head = HEADS(diskinfo);
     79 	d->cyl = CYL(diskinfo);
     80 	d->chs_sectors = d->sec * d->head * d->cyl;
     81 
     82 	d->flags = 0;
     83 	if ((d->dev & 0x80) && int13_extension(d->dev)) {
     84 		d->flags |= BIOSDISK_EXT13;
     85 		if (ed != NULL) {
     86 			ed->size = sizeof *ed;
     87 			int13_getextinfo(d->dev, ed);
     88 		}
     89 	}
     90 
     91 	/*
     92 	 * If the drive is 2.88 floppy drive, check that we can actually
     93 	 * read sector >= 18. If not, assume 1.44 floppy disk.
     94 	 */
     95 	if (d->dev == 0 && SPT(diskinfo) == 36) {
     96 		if (biosread(d->dev, 0, 0, 18, 1, buf)) {
     97 			d->sec = 18;
     98 			d->chs_sectors /= 2;
     99 		}
    100 	}
    101 
    102 	/*
    103 	 * get_diskinfo assumes floppy if BIOS call fails. Check at least
    104 	 * "valid" geometry.
    105 	 */
    106 	return (!d->sec || !d->head);
    107 }
    108 
    109 /*
    110  * Global shared "diskbuf" is used as read ahead buffer.  For reading from
    111  * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
    112  * this buffer doesn't cross a 64K DMA boundary.
    113  */
    114 #define RA_SECTORS      (DISKBUFSIZE / BIOSDISK_SECSIZE)
    115 static int      ra_dev;
    116 static int      ra_end;
    117 static int      ra_first;
    118 
    119 /*
    120  * Because some older BIOSes have bugs in their int13 extensions, we
    121  * only try to use the extended read if the I/O request can't be addressed
    122  * using CHS.
    123  *
    124  * Of course, some BIOSes have bugs in ths CHS read, such as failing to
    125  * function properly if the MBR table has a different geometry than the
    126  * BIOS would generate internally for the device in question, and so we
    127  * provide a way to force the extended on hard disks via a compile-time
    128  * option.
    129  */
    130 #if defined(FORCE_INT13EXT)
    131 #define	NEED_INT13EXT(d, dblk, num)				\
    132 	(((d)->dev & 0x80) != 0)
    133 #else
    134 #define	NEED_INT13EXT(d, dblk, num)				\
    135 	(((d)->dev & 0x80) != 0 && ((dblk) + (num)) >= (d)->chs_sectors)
    136 #endif
    137 
    138 static int
    139 do_read(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf)
    140 {
    141 	int		cyl, head, sec, nsec, spc, dblk32;
    142 	struct {
    143 		int8_t	size;
    144 		int8_t	resvd;
    145 		int16_t	cnt;
    146 		int16_t	off;
    147 		int16_t	seg;
    148 		int64_t	sec;
    149 	}		ext;
    150 
    151 	if (NEED_INT13EXT(d, dblk, num)) {
    152 		if (!(d->flags & BIOSDISK_EXT13))
    153 			return -1;
    154 		ext.size = sizeof(ext);
    155 		ext.resvd = 0;
    156 		ext.cnt = num;
    157 		/* seg:off of physical address */
    158 		ext.off = (int)buf & 0xf;
    159 		ext.seg = vtophys(buf) >> 4;
    160 		ext.sec = dblk;
    161 
    162 		if (biosextread(d->dev, &ext)) {
    163 			(void)biosdiskreset(d->dev);
    164 			return -1;
    165 		}
    166 
    167 		return ext.cnt;
    168 	} else {
    169 		dblk32 = (int)dblk;
    170 		spc = d->head * d->sec;
    171 		cyl = dblk32 / spc;
    172 		head = (dblk32 % spc) / d->sec;
    173 		sec = dblk32 % d->sec;
    174 		nsec = d->sec - sec;
    175 
    176 		if (nsec > num)
    177 			nsec = num;
    178 
    179 		if (biosread(d->dev, cyl, head, sec, nsec, buf)) {
    180 			(void)biosdiskreset(d->dev);
    181 			return -1;
    182 		}
    183 
    184 		return nsec;
    185 	}
    186 }
    187 
    188 /* NB if 'cold' is set below not all of the program is loaded, so
    189  * musn't use data segment, bss, call library functions or do
    190  * read-ahead.
    191  */
    192 
    193 int
    194 readsects(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf, int cold)
    195 {
    196 #ifdef BOOTXX
    197 #define cold 1		/* collapse out references to diskbufp */
    198 #endif
    199 	while (num) {
    200 		int             nsec;
    201 
    202 		/* check for usable data in read-ahead buffer */
    203 		if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
    204 		    || dblk < ra_first || dblk >= ra_end) {
    205 
    206 			/* no, read from disk */
    207 			char           *trbuf;
    208 			int maxsecs;
    209 			int retries = BIOSDISK_RETRIES;
    210 
    211 			if (cold) {
    212 				/* transfer directly to buffer */
    213 				trbuf = buf;
    214 				maxsecs = num;
    215 			} else {
    216 				/* fill read-ahead buffer */
    217 				trbuf = alloc_diskbuf(0); /* no data yet */
    218 				maxsecs = RA_SECTORS;
    219 			}
    220 
    221 			while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) {
    222 #ifdef DISK_DEBUG
    223 				if (!cold)
    224 					printf("read error dblk %d-%d\n", dblk,
    225 					       dblk + maxsecs - 1);
    226 #endif
    227 				if (--retries >= 0)
    228 					continue;
    229 				return (-1);	/* XXX cannot output here if
    230 						 * (cold) */
    231 			}
    232 			if (!cold) {
    233 				ra_dev = d->dev;
    234 				ra_first = dblk;
    235 				ra_end = dblk + nsec;
    236 				diskbuf_user = &ra_dev;
    237 			}
    238 		} else		/* can take blocks from end of read-ahead
    239 				 * buffer */
    240 			nsec = ra_end - dblk;
    241 
    242 		if (!cold) {
    243 			/* copy data from read-ahead to user buffer */
    244 			if (nsec > num)
    245 				nsec = num;
    246 			memcpy(buf,
    247 			       diskbufp + (dblk - ra_first) * BIOSDISK_SECSIZE,
    248 			       nsec * BIOSDISK_SECSIZE);
    249 		}
    250 		buf += nsec * BIOSDISK_SECSIZE;
    251 		num -= nsec;
    252 		dblk += nsec;
    253 	}
    254 
    255 	return (0);
    256 }
    257