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