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