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