Home | History | Annotate | Line # | Download | only in lib
biosdisk_ll.c revision 1.18.2.4
      1  1.18.2.4     skrll /*	$NetBSD: biosdisk_ll.c,v 1.18.2.4 2005/11/10 13:56:54 skrll Exp $	 */
      2  1.18.2.4     skrll 
      3  1.18.2.4     skrll /*-
      4  1.18.2.4     skrll  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.18.2.4     skrll  * All rights reserved.
      6  1.18.2.4     skrll  *
      7  1.18.2.4     skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18.2.4     skrll  * by Bang Jun-Young.
      9  1.18.2.4     skrll  *
     10  1.18.2.4     skrll  * Redistribution and use in source and binary forms, with or without
     11  1.18.2.4     skrll  * modification, are permitted provided that the following conditions
     12  1.18.2.4     skrll  * are met:
     13  1.18.2.4     skrll  * 1. Redistributions of source code must retain the above copyright
     14  1.18.2.4     skrll  *    notice, this list of conditions and the following disclaimer.
     15  1.18.2.4     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18.2.4     skrll  *    notice, this list of conditions and the following disclaimer in the
     17  1.18.2.4     skrll  *    documentation and/or other materials provided with the distribution.
     18  1.18.2.4     skrll  * 3. All advertising materials mentioning features or use of this software
     19  1.18.2.4     skrll  *    must display the following acknowledgement:
     20  1.18.2.4     skrll  *	This product includes software developed by the NetBSD
     21  1.18.2.4     skrll  *	Foundation, Inc. and its contributors.
     22  1.18.2.4     skrll  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.18.2.4     skrll  *    contributors may be used to endorse or promote products derived
     24  1.18.2.4     skrll  *    from this software without specific prior written permission.
     25  1.18.2.4     skrll  *
     26  1.18.2.4     skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.18.2.4     skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.18.2.4     skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.18.2.4     skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.18.2.4     skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.18.2.4     skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.18.2.4     skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.18.2.4     skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.18.2.4     skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.18.2.4     skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.18.2.4     skrll  * POSSIBILITY OF SUCH DAMAGE.
     37  1.18.2.4     skrll  */
     38       1.1     perry 
     39       1.1     perry /*
     40       1.1     perry  * Copyright (c) 1996
     41       1.1     perry  * 	Matthias Drochner.  All rights reserved.
     42       1.1     perry  * Copyright (c) 1996
     43       1.1     perry  * 	Perry E. Metzger.  All rights reserved.
     44       1.1     perry  *
     45       1.1     perry  * Redistribution and use in source and binary forms, with or without
     46       1.1     perry  * modification, are permitted provided that the following conditions
     47       1.1     perry  * are met:
     48       1.1     perry  * 1. Redistributions of source code must retain the above copyright
     49       1.1     perry  *    notice, this list of conditions and the following disclaimer.
     50       1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     51       1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     52       1.1     perry  *    documentation and/or other materials provided with the distribution.
     53       1.1     perry  * 3. All advertising materials mentioning features or use of this software
     54       1.1     perry  *    must display the following acknowledgements:
     55       1.1     perry  *	This product includes software developed for the NetBSD Project
     56       1.1     perry  *	by Matthias Drochner.
     57       1.1     perry  *	This product includes software developed for the NetBSD Project
     58       1.1     perry  *	by Perry E. Metzger.
     59       1.1     perry  * 4. The names of the authors may not be used to endorse or promote products
     60       1.1     perry  *    derived from this software without specific prior written permission.
     61       1.1     perry  *
     62       1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     63       1.1     perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     64       1.1     perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     65       1.1     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     66       1.1     perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     67       1.1     perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     68       1.1     perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     69       1.1     perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     70       1.1     perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     71       1.1     perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     72       1.1     perry  */
     73       1.1     perry 
     74       1.2   thorpej /*
     75       1.3  drochner  * shared by bootsector startup (bootsectmain) and biosdisk.c
     76       1.3  drochner  * needs lowlevel parts from bios_disk.S
     77       1.2   thorpej  */
     78       1.1     perry 
     79       1.1     perry #include <lib/libsa/stand.h>
     80       1.1     perry 
     81       1.1     perry #include "biosdisk_ll.h"
     82       1.1     perry #include "diskbuf.h"
     83  1.18.2.1     skrll #include "libi386.h"
     84       1.1     perry 
     85      1.17       dsl static int do_read(struct biosdisk_ll *, daddr_t, int, char *);
     86       1.1     perry 
     87       1.7  drochner /*
     88       1.7  drochner  * we get from get_diskinfo():
     89  1.18.2.4     skrll  *      %ah      %ch      %cl      %dh (registers after int13/8), ie
     90  1.18.2.4     skrll  * xxxxxxxx cccccccc CCssssss hhhhhhhh
     91       1.7  drochner  */
     92  1.18.2.4     skrll #define STATUS(di)	((di)>>24)
     93  1.18.2.4     skrll #define SPT(di)		(((di)>>8)&0x3f)
     94  1.18.2.4     skrll #define HEADS(di)	(((di)&0xff)+1)
     95       1.7  drochner #define CYL(di)		(((((di)>>16)&0xff)|(((di)>>6)&0x300))+1)
     96       1.1     perry 
     97       1.4  drochner #ifndef BIOSDISK_RETRIES
     98       1.4  drochner #define BIOSDISK_RETRIES 5
     99       1.4  drochner #endif
    100       1.4  drochner 
    101  1.18.2.4     skrll int
    102  1.18.2.4     skrll set_geometry(struct biosdisk_ll *d, struct biosdisk_extinfo *ed)
    103       1.1     perry {
    104       1.7  drochner 	int diskinfo;
    105       1.1     perry 
    106  1.18.2.4     skrll 	diskinfo = biosdisk_getinfo(d->dev);
    107       1.7  drochner 	d->sec = SPT(diskinfo);
    108       1.7  drochner 	d->head = HEADS(diskinfo);
    109       1.7  drochner 	d->cyl = CYL(diskinfo);
    110       1.8      fvdl 	d->chs_sectors = d->sec * d->head * d->cyl;
    111       1.1     perry 
    112  1.18.2.4     skrll 	if (d->dev >= 0x80 + get_harddrives()) {
    113  1.18.2.4     skrll 		d->secsize = 2048;
    114  1.18.2.4     skrll 		d->type = BIOSDISK_TYPE_CD;
    115  1.18.2.4     skrll 	} else {
    116  1.18.2.4     skrll 		d->secsize = 512;
    117  1.18.2.4     skrll 		if (d->dev & 0x80)
    118  1.18.2.4     skrll 			d->type = BIOSDISK_TYPE_HD;
    119  1.18.2.4     skrll 		else
    120  1.18.2.4     skrll 			d->type = BIOSDISK_TYPE_FD;
    121  1.18.2.4     skrll 	}
    122  1.18.2.4     skrll 
    123  1.18.2.4     skrll 	/*
    124  1.18.2.4     skrll 	 * Some broken BIOSes such as one found on Soltek SL-75DRV2 report
    125  1.18.2.4     skrll 	 * that they don't support int13 extension for CD-ROM drives while
    126  1.18.2.4     skrll 	 * they actually do. As a workaround, if the boot device is a CD we
    127  1.18.2.4     skrll 	 * assume that the extension is available. Note that only very old
    128  1.18.2.4     skrll 	 * BIOSes don't support the extended mode, and they don't work with
    129  1.18.2.4     skrll 	 * ATAPI CD-ROM drives, either. So there's no problem.
    130  1.18.2.4     skrll 	 */
    131       1.5        ws 	d->flags = 0;
    132  1.18.2.4     skrll 	if (d->type == BIOSDISK_TYPE_CD ||
    133  1.18.2.4     skrll 	    (d->type == BIOSDISK_TYPE_HD && biosdisk_int13ext(d->dev))) {
    134  1.18.2.4     skrll 		d->flags |= BIOSDISK_INT13EXT;
    135      1.17       dsl 		if (ed != NULL) {
    136  1.18.2.4     skrll 			ed->size = sizeof(*ed);
    137  1.18.2.4     skrll 			biosdisk_getextinfo(d->dev, ed);
    138      1.17       dsl 		}
    139      1.13  jdolecek 	}
    140      1.13  jdolecek 
    141      1.13  jdolecek 	/*
    142  1.18.2.4     skrll 	 * If the drive is 2.88MB floppy drive, check that we can actually
    143  1.18.2.4     skrll 	 * read sector >= 18. If not, assume 1.44MB floppy disk.
    144      1.13  jdolecek 	 */
    145  1.18.2.4     skrll 	if (d->type == BIOSDISK_TYPE_FD && SPT(diskinfo) == 36) {
    146  1.18.2.4     skrll 		char buf[512];
    147  1.18.2.4     skrll 
    148  1.18.2.4     skrll 		if (biosdisk_read(d->dev, 0, 0, 18, 1, buf)) {
    149      1.13  jdolecek 			d->sec = 18;
    150      1.13  jdolecek 			d->chs_sectors /= 2;
    151  1.18.2.4     skrll 		}
    152       1.6      fvdl 	}
    153       1.1     perry 
    154  1.18.2.4     skrll 	return 0;
    155       1.1     perry }
    156       1.1     perry 
    157       1.2   thorpej /*
    158       1.2   thorpej  * Global shared "diskbuf" is used as read ahead buffer.  For reading from
    159       1.2   thorpej  * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
    160       1.2   thorpej  * this buffer doesn't cross a 64K DMA boundary.
    161       1.1     perry  */
    162       1.2   thorpej static int      ra_dev;
    163       1.2   thorpej static int      ra_end;
    164       1.2   thorpej static int      ra_first;
    165       1.2   thorpej 
    166      1.18   thorpej /*
    167      1.18   thorpej  * Because some older BIOSes have bugs in their int13 extensions, we
    168      1.18   thorpej  * only try to use the extended read if the I/O request can't be addressed
    169      1.18   thorpej  * using CHS.
    170      1.18   thorpej  *
    171      1.18   thorpej  * Of course, some BIOSes have bugs in ths CHS read, such as failing to
    172      1.18   thorpej  * function properly if the MBR table has a different geometry than the
    173      1.18   thorpej  * BIOS would generate internally for the device in question, and so we
    174      1.18   thorpej  * provide a way to force the extended on hard disks via a compile-time
    175      1.18   thorpej  * option.
    176      1.18   thorpej  */
    177      1.18   thorpej #if defined(FORCE_INT13EXT)
    178      1.18   thorpej #define	NEED_INT13EXT(d, dblk, num)				\
    179      1.18   thorpej 	(((d)->dev & 0x80) != 0)
    180      1.18   thorpej #else
    181      1.18   thorpej #define	NEED_INT13EXT(d, dblk, num)				\
    182  1.18.2.4     skrll 	(((d)->type == BIOSDISK_TYPE_CD) ||                     \
    183  1.18.2.4     skrll 	 ((d)->type == BIOSDISK_TYPE_HD &&			\
    184  1.18.2.4     skrll 	  ((dblk) + (num)) >= (d)->chs_sectors))
    185      1.18   thorpej #endif
    186      1.18   thorpej 
    187       1.5        ws static int
    188      1.17       dsl do_read(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf)
    189       1.5        ws {
    190       1.5        ws 
    191      1.18   thorpej 	if (NEED_INT13EXT(d, dblk, num)) {
    192  1.18.2.4     skrll 		struct {
    193  1.18.2.4     skrll 			int8_t size;
    194  1.18.2.4     skrll 			int8_t resvd;
    195  1.18.2.4     skrll 			int16_t cnt;
    196  1.18.2.4     skrll 			int16_t off;
    197  1.18.2.4     skrll 			int16_t seg;
    198  1.18.2.4     skrll 			int64_t sec;
    199  1.18.2.4     skrll 		} ext;
    200  1.18.2.4     skrll 
    201  1.18.2.4     skrll 		if (!(d->flags & BIOSDISK_INT13EXT))
    202       1.8      fvdl 			return -1;
    203       1.5        ws 		ext.size = sizeof(ext);
    204       1.5        ws 		ext.resvd = 0;
    205       1.5        ws 		ext.cnt = num;
    206      1.14       dsl 		/* seg:off of physical address */
    207      1.14       dsl 		ext.off = (int)buf & 0xf;
    208      1.14       dsl 		ext.seg = vtophys(buf) >> 4;
    209       1.5        ws 		ext.sec = dblk;
    210       1.5        ws 
    211  1.18.2.4     skrll 		if (biosdisk_extread(d->dev, &ext)) {
    212  1.18.2.4     skrll 			(void)biosdisk_reset(d->dev);
    213       1.5        ws 			return -1;
    214      1.12    dyoung 		}
    215       1.5        ws 
    216       1.5        ws 		return ext.cnt;
    217       1.5        ws 	} else {
    218  1.18.2.4     skrll 		int cyl, head, sec, nsec, spc, dblk32;
    219  1.18.2.4     skrll 
    220      1.16      fvdl 		dblk32 = (int)dblk;
    221       1.7  drochner 		spc = d->head * d->sec;
    222      1.16      fvdl 		cyl = dblk32 / spc;
    223      1.16      fvdl 		head = (dblk32 % spc) / d->sec;
    224      1.16      fvdl 		sec = dblk32 % d->sec;
    225       1.6      fvdl 		nsec = d->sec - sec;
    226       1.5        ws 
    227       1.5        ws 		if (nsec > num)
    228       1.5        ws 			nsec = num;
    229       1.5        ws 
    230  1.18.2.4     skrll 		if (biosdisk_read(d->dev, cyl, head, sec, nsec, buf)) {
    231  1.18.2.4     skrll 			(void)biosdisk_reset(d->dev);
    232       1.5        ws 			return -1;
    233      1.12    dyoung 		}
    234       1.5        ws 
    235       1.5        ws 		return nsec;
    236       1.5        ws 	}
    237       1.5        ws }
    238       1.5        ws 
    239  1.18.2.4     skrll /*
    240  1.18.2.4     skrll  * NB if 'cold' is set below not all of the program is loaded, so
    241  1.18.2.4     skrll  * mustn't use data segment, bss, call library functions or do read-ahead.
    242      1.14       dsl  */
    243  1.18.2.4     skrll int
    244      1.17       dsl readsects(struct biosdisk_ll *d, daddr_t dblk, int num, char *buf, int cold)
    245       1.1     perry {
    246      1.17       dsl #ifdef BOOTXX
    247      1.17       dsl #define cold 1		/* collapse out references to diskbufp */
    248      1.17       dsl #endif
    249       1.2   thorpej 	while (num) {
    250  1.18.2.4     skrll 		int nsec;
    251       1.1     perry 
    252       1.2   thorpej 		/* check for usable data in read-ahead buffer */
    253       1.2   thorpej 		if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
    254       1.2   thorpej 		    || dblk < ra_first || dblk >= ra_end) {
    255       1.2   thorpej 
    256       1.2   thorpej 			/* no, read from disk */
    257  1.18.2.4     skrll 			char *trbuf;
    258       1.7  drochner 			int maxsecs;
    259       1.4  drochner 			int retries = BIOSDISK_RETRIES;
    260       1.2   thorpej 
    261       1.2   thorpej 			if (cold) {
    262       1.2   thorpej 				/* transfer directly to buffer */
    263       1.2   thorpej 				trbuf = buf;
    264       1.7  drochner 				maxsecs = num;
    265       1.2   thorpej 			} else {
    266       1.2   thorpej 				/* fill read-ahead buffer */
    267      1.14       dsl 				trbuf = alloc_diskbuf(0); /* no data yet */
    268  1.18.2.4     skrll 				maxsecs = DISKBUFSIZE / d->secsize;
    269       1.2   thorpej 			}
    270       1.2   thorpej 
    271       1.7  drochner 			while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) {
    272       1.4  drochner #ifdef DISK_DEBUG
    273       1.4  drochner 				if (!cold)
    274      1.10  drochner 					printf("read error dblk %d-%d\n", dblk,
    275      1.10  drochner 					       dblk + maxsecs - 1);
    276       1.4  drochner #endif
    277       1.5        ws 				if (--retries >= 0)
    278       1.4  drochner 					continue;
    279  1.18.2.4     skrll 				return -1;	/* XXX cannot output here if
    280       1.2   thorpej 						 * (cold) */
    281       1.5        ws 			}
    282       1.5        ws 			if (!cold) {
    283       1.5        ws 				ra_dev = d->dev;
    284       1.5        ws 				ra_first = dblk;
    285       1.5        ws 				ra_end = dblk + nsec;
    286       1.5        ws 				diskbuf_user = &ra_dev;
    287       1.2   thorpej 			}
    288       1.2   thorpej 		} else		/* can take blocks from end of read-ahead
    289       1.2   thorpej 				 * buffer */
    290       1.2   thorpej 			nsec = ra_end - dblk;
    291       1.2   thorpej 
    292       1.2   thorpej 		if (!cold) {
    293       1.2   thorpej 			/* copy data from read-ahead to user buffer */
    294       1.2   thorpej 			if (nsec > num)
    295       1.2   thorpej 				nsec = num;
    296      1.11     perry 			memcpy(buf,
    297  1.18.2.4     skrll 			       diskbufp + (dblk - ra_first) * d->secsize,
    298  1.18.2.4     skrll 			       nsec * d->secsize);
    299       1.2   thorpej 		}
    300  1.18.2.4     skrll 		buf += nsec * d->secsize;
    301       1.2   thorpej 		num -= nsec;
    302       1.2   thorpej 		dblk += nsec;
    303       1.1     perry 	}
    304       1.1     perry 
    305  1.18.2.4     skrll 	return 0;
    306  1.18.2.4     skrll }
    307  1.18.2.4     skrll 
    308  1.18.2.4     skrll /*
    309  1.18.2.4     skrll  * Return the number of hard disk drives.
    310  1.18.2.4     skrll  */
    311  1.18.2.4     skrll int
    312  1.18.2.4     skrll get_harddrives(void)
    313  1.18.2.4     skrll {
    314  1.18.2.4     skrll 	/*
    315  1.18.2.4     skrll 	 * Some BIOSes are buggy so that they return incorrect number
    316  1.18.2.4     skrll 	 * of hard drives with int13/ah=8. We read a byte at 0040:0075
    317  1.18.2.4     skrll 	 * instead, which is known to be always correct.
    318  1.18.2.4     skrll 	 */
    319  1.18.2.4     skrll 	int n = 0;
    320  1.18.2.4     skrll 
    321  1.18.2.4     skrll 	pvbcopy((void *)0x475, &n, 1);
    322  1.18.2.4     skrll 
    323  1.18.2.4     skrll 	return n;
    324       1.1     perry }
    325