1 /* $NetBSD: biosdisk_ll.c,v 1.13 2002/12/04 18:26:57 jdolecek 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 * If the drive is 2.88 floppy drive, check that we can actually 93 * read sector >= 18. If not, assume 1.44 floppy disk. 94 */ 95 if (d->dev == 0 && SPT(diskinfo) == 36) { 96 if (biosread(d->dev, 0, 0, 18, 1, diskbuf)) { 97 d->sec = 18; 98 d->chs_sectors /= 2; 99 } 100 } 101 102 /* 103 * get_diskinfo assumes floppy if BIOS call fails. Check at least 104 * "valid" geometry. 105 */ 106 return (!d->sec || !d->head); 107 } 108 109 /* 110 * Global shared "diskbuf" is used as read ahead buffer. For reading from 111 * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that 112 * this buffer doesn't cross a 64K DMA boundary. 113 */ 114 #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE) 115 static int ra_dev; 116 static int ra_end; 117 static int ra_first; 118 119 static int 120 do_read(d, dblk, num, buf) 121 struct biosdisk_ll *d; 122 int dblk, num; 123 char *buf; 124 { 125 int cyl, head, sec, nsec, spc; 126 struct { 127 int8_t size; 128 int8_t resvd; 129 int16_t cnt; 130 int16_t off; 131 int16_t seg; 132 int64_t sec; 133 } ext; 134 135 if ((d->dev & 0x80) && (dblk + num) >= d->chs_sectors) { 136 if (!(d->flags & BIOSDISK_EXT13)) 137 return -1; 138 ext.size = sizeof(ext); 139 ext.resvd = 0; 140 ext.cnt = num; 141 ext.off = (int32_t)buf; 142 ext.seg = ourseg; 143 ext.sec = dblk; 144 145 if (biosextread(d->dev, &ext)) { 146 (void)biosdiskreset(d->dev); 147 return -1; 148 } 149 150 return ext.cnt; 151 } else { 152 spc = d->head * d->sec; 153 cyl = dblk / spc; 154 head = (dblk % spc) / d->sec; 155 sec = dblk % d->sec; 156 nsec = d->sec - sec; 157 158 if (nsec > num) 159 nsec = num; 160 161 if (biosread(d->dev, cyl, head, sec, nsec, buf)) { 162 (void)biosdiskreset(d->dev); 163 return -1; 164 } 165 166 return nsec; 167 } 168 } 169 170 int 171 readsects(d, dblk, num, buf, cold) /* reads ahead if (!cold) */ 172 struct biosdisk_ll *d; 173 int dblk, num; 174 char *buf; 175 int cold; /* don't use data segment or bss, don't call 176 * library functions */ 177 { 178 while (num) { 179 int nsec; 180 181 /* check for usable data in read-ahead buffer */ 182 if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev 183 || dblk < ra_first || dblk >= ra_end) { 184 185 /* no, read from disk */ 186 char *trbuf; 187 int maxsecs; 188 int retries = BIOSDISK_RETRIES; 189 190 if (cold) { 191 /* transfer directly to buffer */ 192 trbuf = buf; 193 maxsecs = num; 194 } else { 195 /* fill read-ahead buffer */ 196 trbuf = diskbuf; 197 maxsecs = RA_SECTORS; 198 diskbuf_user = 0; /* not yet valid */ 199 } 200 201 while ((nsec = do_read(d, dblk, maxsecs, trbuf)) < 0) { 202 #ifdef DISK_DEBUG 203 if (!cold) 204 printf("read error dblk %d-%d\n", dblk, 205 dblk + maxsecs - 1); 206 #endif 207 if (--retries >= 0) 208 continue; 209 return (-1); /* XXX cannot output here if 210 * (cold) */ 211 } 212 if (!cold) { 213 ra_dev = d->dev; 214 ra_first = dblk; 215 ra_end = dblk + nsec; 216 diskbuf_user = &ra_dev; 217 } 218 } else /* can take blocks from end of read-ahead 219 * buffer */ 220 nsec = ra_end - dblk; 221 222 if (!cold) { 223 /* copy data from read-ahead to user buffer */ 224 if (nsec > num) 225 nsec = num; 226 memcpy(buf, 227 diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE, 228 nsec * BIOSDISK_SECSIZE); 229 } 230 buf += nsec * BIOSDISK_SECSIZE; 231 num -= nsec; 232 dblk += nsec; 233 } 234 235 return (0); 236 } 237