biosdisk_ll.c revision 1.5 1 1.5 ws /* $NetBSD: biosdisk_ll.c,v 1.5 1998/10/15 15:28:22 ws Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1996
5 1.1 perry * Matthias Drochner. All rights reserved.
6 1.1 perry * Copyright (c) 1996
7 1.1 perry * Perry E. Metzger. All rights reserved.
8 1.1 perry *
9 1.1 perry * Redistribution and use in source and binary forms, with or without
10 1.1 perry * modification, are permitted provided that the following conditions
11 1.1 perry * are met:
12 1.1 perry * 1. Redistributions of source code must retain the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer.
14 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perry * notice, this list of conditions and the following disclaimer in the
16 1.1 perry * documentation and/or other materials provided with the distribution.
17 1.1 perry * 3. All advertising materials mentioning features or use of this software
18 1.1 perry * must display the following acknowledgements:
19 1.1 perry * This product includes software developed for the NetBSD Project
20 1.1 perry * by Matthias Drochner.
21 1.1 perry * This product includes software developed for the NetBSD Project
22 1.1 perry * by Perry E. Metzger.
23 1.1 perry * 4. The names of the authors may not be used to endorse or promote products
24 1.1 perry * derived from this software without specific prior written permission.
25 1.1 perry *
26 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 1.1 perry */
37 1.1 perry
38 1.2 thorpej /*
39 1.3 drochner * shared by bootsector startup (bootsectmain) and biosdisk.c
40 1.3 drochner * needs lowlevel parts from bios_disk.S
41 1.2 thorpej */
42 1.1 perry
43 1.1 perry #include <lib/libsa/stand.h>
44 1.1 perry
45 1.1 perry #include "biosdisk_ll.h"
46 1.1 perry #include "diskbuf.h"
47 1.1 perry
48 1.5 ws extern long ourseg;
49 1.5 ws
50 1.1 perry extern int get_diskinfo __P((int));
51 1.5 ws extern int int13_extension __P((int));
52 1.2 thorpej extern int biosread __P((int, int, int, int, int, char *));
53 1.5 ws extern int biosextread __P((int, void *));
54 1.5 ws static int do_read __P((struct biosdisk_ll *, int, int, char *));
55 1.1 perry
56 1.1 perry #define SPT(di) ((di)&0xff)
57 1.1 perry #define HEADS(di) ((((di)>>8)&0xff)+1)
58 1.1 perry
59 1.4 drochner #ifndef BIOSDISK_RETRIES
60 1.4 drochner #define BIOSDISK_RETRIES 5
61 1.4 drochner #endif
62 1.4 drochner
63 1.2 thorpej int
64 1.2 thorpej set_geometry(d)
65 1.2 thorpej struct biosdisk_ll *d;
66 1.1 perry {
67 1.2 thorpej int diskinfo;
68 1.1 perry
69 1.2 thorpej diskinfo = get_diskinfo(d->dev);
70 1.1 perry
71 1.5 ws d->flags = 0;
72 1.5 ws if ((d->dev&0x80) && int13_extension(d->dev))
73 1.5 ws d->flags |= BIOSDISK_EXT13;
74 1.5 ws
75 1.2 thorpej d->spc = (d->spt = SPT(diskinfo)) * HEADS(diskinfo);
76 1.1 perry
77 1.2 thorpej /*
78 1.2 thorpej * get_diskinfo assumes floppy if BIOS call fails. Check at least
79 1.2 thorpej * "valid" geometry.
80 1.2 thorpej */
81 1.2 thorpej return (!d->spc || !d->spt);
82 1.1 perry }
83 1.1 perry
84 1.2 thorpej /*
85 1.2 thorpej * Global shared "diskbuf" is used as read ahead buffer. For reading from
86 1.2 thorpej * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
87 1.2 thorpej * this buffer doesn't cross a 64K DMA boundary.
88 1.1 perry */
89 1.1 perry #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE)
90 1.2 thorpej static int ra_dev;
91 1.2 thorpej static int ra_end;
92 1.2 thorpej static int ra_first;
93 1.2 thorpej
94 1.5 ws static int
95 1.5 ws do_read(d, dblk, num, buf)
96 1.5 ws struct biosdisk_ll *d;
97 1.5 ws int dblk, num;
98 1.5 ws char *buf;
99 1.5 ws {
100 1.5 ws int cyl, head, sec, nsec;
101 1.5 ws struct {
102 1.5 ws int8_t size;
103 1.5 ws int8_t resvd;
104 1.5 ws int16_t cnt;
105 1.5 ws int16_t off;
106 1.5 ws int16_t seg;
107 1.5 ws int64_t sec;
108 1.5 ws } ext;
109 1.5 ws
110 1.5 ws if (d->flags & BIOSDISK_EXT13) {
111 1.5 ws ext.size = sizeof(ext);
112 1.5 ws ext.resvd = 0;
113 1.5 ws ext.cnt = num;
114 1.5 ws ext.off = (int32_t)buf;
115 1.5 ws ext.seg = ourseg;
116 1.5 ws ext.sec = dblk;
117 1.5 ws
118 1.5 ws if (biosextread(d->dev, &ext))
119 1.5 ws return -1;
120 1.5 ws
121 1.5 ws return ext.cnt;
122 1.5 ws } else {
123 1.5 ws
124 1.5 ws cyl = dblk / d->spc;
125 1.5 ws head = (dblk % d->spc) / d->spt;
126 1.5 ws sec = dblk % d->spt;
127 1.5 ws nsec = d->spt - sec;
128 1.5 ws
129 1.5 ws if (nsec > num)
130 1.5 ws nsec = num;
131 1.5 ws
132 1.5 ws if (biosread(d->dev, cyl, head, sec, nsec, buf))
133 1.5 ws return -1;
134 1.5 ws
135 1.5 ws return nsec;
136 1.5 ws }
137 1.5 ws }
138 1.5 ws
139 1.2 thorpej int
140 1.2 thorpej readsects(d, dblk, num, buf, cold) /* reads ahead if (!cold) */
141 1.2 thorpej struct biosdisk_ll *d;
142 1.2 thorpej int dblk, num;
143 1.2 thorpej char *buf;
144 1.2 thorpej int cold; /* don't use data segment or bss, don't call
145 1.2 thorpej * library functions */
146 1.1 perry {
147 1.2 thorpej while (num) {
148 1.2 thorpej int nsec;
149 1.1 perry
150 1.2 thorpej /* check for usable data in read-ahead buffer */
151 1.2 thorpej if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
152 1.2 thorpej || dblk < ra_first || dblk >= ra_end) {
153 1.2 thorpej
154 1.2 thorpej /* no, read from disk */
155 1.2 thorpej char *trbuf;
156 1.4 drochner int retries = BIOSDISK_RETRIES;
157 1.2 thorpej
158 1.5 ws nsec = num;
159 1.2 thorpej
160 1.2 thorpej if (cold) {
161 1.2 thorpej /* transfer directly to buffer */
162 1.2 thorpej trbuf = buf;
163 1.2 thorpej } else {
164 1.2 thorpej /* fill read-ahead buffer */
165 1.2 thorpej trbuf = diskbuf;
166 1.2 thorpej if (nsec > RA_SECTORS)
167 1.2 thorpej nsec = RA_SECTORS;
168 1.2 thorpej
169 1.2 thorpej }
170 1.2 thorpej
171 1.5 ws while ((nsec = do_read(d, dblk, nsec, trbuf)) < 0) {
172 1.4 drochner #ifdef DISK_DEBUG
173 1.4 drochner if (!cold)
174 1.4 drochner printf("read error C:%d H:%d S:%d-%d\n",
175 1.4 drochner cyl, head, sec, sec + nsec - 1);
176 1.4 drochner #endif
177 1.5 ws if (--retries >= 0)
178 1.4 drochner continue;
179 1.2 thorpej if (!cold)
180 1.2 thorpej diskbuf_user = 0; /* mark invalid */
181 1.2 thorpej return (-1); /* XXX cannot output here if
182 1.2 thorpej * (cold) */
183 1.5 ws }
184 1.5 ws if (!cold) {
185 1.5 ws ra_dev = d->dev;
186 1.5 ws ra_first = dblk;
187 1.5 ws ra_end = dblk + nsec;
188 1.5 ws diskbuf_user = &ra_dev;
189 1.2 thorpej }
190 1.2 thorpej } else /* can take blocks from end of read-ahead
191 1.2 thorpej * buffer */
192 1.2 thorpej nsec = ra_end - dblk;
193 1.2 thorpej
194 1.2 thorpej if (!cold) {
195 1.2 thorpej /* copy data from read-ahead to user buffer */
196 1.2 thorpej if (nsec > num)
197 1.2 thorpej nsec = num;
198 1.2 thorpej bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
199 1.2 thorpej buf, nsec * BIOSDISK_SECSIZE);
200 1.2 thorpej }
201 1.2 thorpej buf += nsec * BIOSDISK_SECSIZE;
202 1.2 thorpej num -= nsec;
203 1.2 thorpej dblk += nsec;
204 1.1 perry }
205 1.1 perry
206 1.2 thorpej return (0);
207 1.1 perry }
208