biosdisk_ll.c revision 1.14 1 /* $NetBSD: biosdisk_ll.c,v 1.14 2003/02/01 14:48:17 dsl 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
48 extern int get_diskinfo(int);
49 extern void int13_getextinfo(int, struct biosdisk_ext13info *);
50 extern int int13_extension(int);
51 extern int biosread(int, int, int, int, int, void *);
52 extern int biosdiskreset(int);
53 extern int biosextread(int, void *);
54 static int do_read(struct biosdisk_ll *, int, int, char *);
55 extern u_int vtophys(void *);
56
57 /*
58 * we get from get_diskinfo():
59 * xxxx %ch %cl %dh (registers after int13/8), ie
60 * xxxx cccc Csss hhhh
61 */
62 #define SPT(di) (((di)>>8)&0x3f)
63 #define HEADS(di) (((di)&0xff)+1)
64 #define CYL(di) (((((di)>>16)&0xff)|(((di)>>6)&0x300))+1)
65
66 #ifndef BIOSDISK_RETRIES
67 #define BIOSDISK_RETRIES 5
68 #endif
69
70 int
71 set_geometry(struct biosdisk_ll *d, struct biosdisk_ext13info *ed)
72 {
73 int diskinfo;
74
75 diskinfo = get_diskinfo(d->dev);
76 d->sec = SPT(diskinfo);
77 d->head = HEADS(diskinfo);
78 d->cyl = CYL(diskinfo);
79 d->chs_sectors = d->sec * d->head * d->cyl;
80
81 d->flags = 0;
82 if ((d->dev & 0x80) && int13_extension(d->dev)) {
83 d->flags |= BIOSDISK_EXT13;
84 if (ed != NULL)
85 int13_getextinfo(d->dev, ed);
86 }
87
88 /*
89 * If the drive is 2.88 floppy drive, check that we can actually
90 * read sector >= 18. If not, assume 1.44 floppy disk.
91 */
92 if (d->dev == 0 && SPT(diskinfo) == 36) {
93 if (biosread(d->dev, 0, 0, 18, 1, alloc_diskbuf(0))) {
94 d->sec = 18;
95 d->chs_sectors /= 2;
96 }
97 }
98
99 /*
100 * get_diskinfo assumes floppy if BIOS call fails. Check at least
101 * "valid" geometry.
102 */
103 return (!d->sec || !d->head);
104 }
105
106 /*
107 * Global shared "diskbuf" is used as read ahead buffer. For reading from
108 * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
109 * this buffer doesn't cross a 64K DMA boundary.
110 */
111 #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE)
112 static int ra_dev;
113 static int ra_end;
114 static int ra_first;
115
116 static int
117 do_read(struct biosdisk_ll *d, int dblk, int num, char *buf)
118 {
119 int cyl, head, sec, nsec, spc;
120 struct {
121 int8_t size;
122 int8_t resvd;
123 int16_t cnt;
124 int16_t off;
125 int16_t seg;
126 int64_t sec;
127 } ext;
128
129 if ((d->dev & 0x80) && (dblk + num) >= d->chs_sectors) {
130 if (!(d->flags & BIOSDISK_EXT13))
131 return -1;
132 ext.size = sizeof(ext);
133 ext.resvd = 0;
134 ext.cnt = num;
135 /* seg:off of physical address */
136 ext.off = (int)buf & 0xf;
137 ext.seg = vtophys(buf) >> 4;
138 ext.sec = dblk;
139
140 if (biosextread(d->dev, &ext)) {
141 (void)biosdiskreset(d->dev);
142 return -1;
143 }
144
145 return ext.cnt;
146 } else {
147 spc = d->head * d->sec;
148 cyl = dblk / spc;
149 head = (dblk % spc) / d->sec;
150 sec = dblk % d->sec;
151 nsec = d->sec - sec;
152
153 if (nsec > num)
154 nsec = num;
155
156 if (biosread(d->dev, cyl, head, sec, nsec, buf)) {
157 (void)biosdiskreset(d->dev);
158 return -1;
159 }
160
161 return nsec;
162 }
163 }
164
165 /* NB if 'cold' is set below not all of the program is loaded, so
166 * musn't use data segment, bss, call library functions or do
167 * read-ahead.
168 */
169
170 int
171 readsects(struct biosdisk_ll *d, int dblk, int num, char *buf, int cold)
172 {
173 while (num) {
174 int nsec;
175
176 /* check for usable data in read-ahead buffer */
177 if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
178 || dblk < ra_first || dblk >= ra_end) {
179
180 /* no, read from disk */
181 char *trbuf;
182 int maxsecs;
183 int retries = BIOSDISK_RETRIES;
184
185 if (cold) {
186 /* transfer directly to buffer */
187 trbuf = buf;
188 maxsecs = num;
189 } else {
190 /* fill read-ahead buffer */
191 trbuf = alloc_diskbuf(0); /* no data yet */
192 maxsecs = RA_SECTORS;
193 }
194
195 while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) {
196 #ifdef DISK_DEBUG
197 if (!cold)
198 printf("read error dblk %d-%d\n", dblk,
199 dblk + maxsecs - 1);
200 #endif
201 if (--retries >= 0)
202 continue;
203 return (-1); /* XXX cannot output here if
204 * (cold) */
205 }
206 if (!cold) {
207 ra_dev = d->dev;
208 ra_first = dblk;
209 ra_end = dblk + nsec;
210 diskbuf_user = &ra_dev;
211 }
212 } else /* can take blocks from end of read-ahead
213 * buffer */
214 nsec = ra_end - dblk;
215
216 if (!cold) {
217 /* copy data from read-ahead to user buffer */
218 if (nsec > num)
219 nsec = num;
220 memcpy(buf,
221 diskbufp + (dblk - ra_first) * BIOSDISK_SECSIZE,
222 nsec * BIOSDISK_SECSIZE);
223 }
224 buf += nsec * BIOSDISK_SECSIZE;
225 num -= nsec;
226 dblk += nsec;
227 }
228
229 return (0);
230 }
231