biosdisk_ll.c revision 1.6 1 /* $NetBSD: biosdisk_ll.c,v 1.6 1999/03/08 00:09:25 fvdl 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 long ourseg;
49
50 extern void get_diskinfo __P((struct biosdisk_ll *));
51 extern void int13_getextinfo __P((int, struct biosdisk_ext13info *));
52 extern int int13_extension __P((int));
53 extern int biosread __P((int, int, int, int, int, char *));
54 extern int biosextread __P((int, void *));
55 static int do_read __P((struct biosdisk_ll *, int, int, char *));
56
57 #define SPT(di) ((di)&0xff)
58 #define HEADS(di) ((((di)>>8)&0xff)+1)
59 #define CYL(di)
60
61 #ifndef BIOSDISK_RETRIES
62 #define BIOSDISK_RETRIES 5
63 #endif
64
65 int
66 set_geometry(d, ed)
67 struct biosdisk_ll *d;
68 struct biosdisk_ext13info *ed;
69 {
70 d->sec = d->head = 0;
71
72 get_diskinfo(d);
73
74 d->flags = 0;
75 if ((d->dev&0x80) && int13_extension(d->dev)) {
76 d->flags |= BIOSDISK_EXT13;
77 if (ed != NULL)
78 int13_getextinfo(d->dev, ed);
79 }
80
81 /*
82 * get_diskinfo assumes floppy if BIOS call fails. Check at least
83 * "valid" geometry.
84 */
85 return (!d->sec || !d->head || !d->cyl);
86 }
87
88 /*
89 * Global shared "diskbuf" is used as read ahead buffer. For reading from
90 * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
91 * this buffer doesn't cross a 64K DMA boundary.
92 */
93 #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE)
94 static int ra_dev;
95 static int ra_end;
96 static int ra_first;
97
98 static int
99 do_read(d, dblk, num, buf)
100 struct biosdisk_ll *d;
101 int dblk, num;
102 char *buf;
103 {
104 int cyl, head, sec, nsec, spc;
105 struct {
106 int8_t size;
107 int8_t resvd;
108 int16_t cnt;
109 int16_t off;
110 int16_t seg;
111 int64_t sec;
112 } ext;
113
114 if (d->flags & BIOSDISK_EXT13) {
115 ext.size = sizeof(ext);
116 ext.resvd = 0;
117 ext.cnt = num;
118 ext.off = (int32_t)buf;
119 ext.seg = ourseg;
120 ext.sec = dblk;
121
122 if (biosextread(d->dev, &ext))
123 return -1;
124
125 return ext.cnt;
126 } else {
127 spc = (d->head + 1) * d->sec;
128 cyl = dblk / spc;
129 head = (dblk % spc) / d->sec;
130 sec = dblk % d->sec;
131 nsec = d->sec - sec;
132
133 if (nsec > num)
134 nsec = num;
135
136 if (biosread(d->dev, cyl, head, sec, nsec, buf))
137 return -1;
138
139 return nsec;
140 }
141 }
142
143 int
144 readsects(d, dblk, num, buf, cold) /* reads ahead if (!cold) */
145 struct biosdisk_ll *d;
146 int dblk, num;
147 char *buf;
148 int cold; /* don't use data segment or bss, don't call
149 * library functions */
150 {
151 while (num) {
152 int nsec;
153
154 /* check for usable data in read-ahead buffer */
155 if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
156 || dblk < ra_first || dblk >= ra_end) {
157
158 /* no, read from disk */
159 char *trbuf;
160 int retries = BIOSDISK_RETRIES;
161
162 nsec = num;
163
164 if (cold) {
165 /* transfer directly to buffer */
166 trbuf = buf;
167 } else {
168 /* fill read-ahead buffer */
169 trbuf = diskbuf;
170 if (nsec > RA_SECTORS)
171 nsec = RA_SECTORS;
172
173 }
174
175 while ((nsec = do_read(d, dblk, nsec, trbuf)) < 0) {
176 #ifdef DISK_DEBUG
177 if (!cold)
178 printf("read error C:%d H:%d S:%d-%d\n",
179 cyl, head, sec, sec + nsec - 1);
180 #endif
181 if (--retries >= 0)
182 continue;
183 if (!cold)
184 diskbuf_user = 0; /* mark invalid */
185 return (-1); /* XXX cannot output here if
186 * (cold) */
187 }
188 if (!cold) {
189 ra_dev = d->dev;
190 ra_first = dblk;
191 ra_end = dblk + nsec;
192 diskbuf_user = &ra_dev;
193 }
194 } else /* can take blocks from end of read-ahead
195 * buffer */
196 nsec = ra_end - dblk;
197
198 if (!cold) {
199 /* copy data from read-ahead to user buffer */
200 if (nsec > num)
201 nsec = num;
202 bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
203 buf, nsec * BIOSDISK_SECSIZE);
204 }
205 buf += nsec * BIOSDISK_SECSIZE;
206 num -= nsec;
207 dblk += nsec;
208 }
209
210 return (0);
211 }
212