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