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