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