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