xref: /titanic_54/usr/src/boot/sys/boot/i386/libi386/biosdisk.c (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * BIOS disk device handling.
33  *
34  * Ideas and algorithms from:
35  *
36  * - NetBSD libi386/biosdisk.c
37  * - FreeBSD biosboot/disk.c
38  *
39  */
40 
41 #include <sys/disk.h>
42 #include <stand.h>
43 #include <machine/bootinfo.h>
44 #include <stdarg.h>
45 
46 #include <bootstrap.h>
47 #include <btxv86.h>
48 #include <edd.h>
49 #include "disk.h"
50 #include "libi386.h"
51 
52 CTASSERT(sizeof(struct i386_devdesc) >= sizeof(struct disk_devdesc));
53 
54 #define BIOS_NUMDRIVES		0x475
55 #define BIOSDISK_SECSIZE	512
56 #define BUFSIZE			(1 * BIOSDISK_SECSIZE)
57 
58 #define DT_ATAPI		0x10		/* disk type for ATAPI floppies */
59 #define WDMAJOR			0		/* major numbers for devices we frontend for */
60 #define WFDMAJOR		1
61 #define FDMAJOR			2
62 #define DAMAJOR			4
63 
64 #ifdef DISK_DEBUG
65 # define DEBUG(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
66 #else
67 # define DEBUG(fmt, args...)
68 #endif
69 
70 /*
71  * List of BIOS devices, translation from disk unit number to
72  * BIOS unit number.
73  */
74 static struct bdinfo
75 {
76 	int		bd_unit;	/* BIOS unit number */
77 	int		bd_cyl;		/* BIOS geometry */
78 	int		bd_hds;
79 	int		bd_sec;
80 	int		bd_flags;
81 #define	BD_MODEINT13	0x0000
82 #define	BD_MODEEDD1	0x0001
83 #define	BD_MODEEDD3	0x0002
84 #define	BD_MODEMASK	0x0003
85 #define	BD_FLOPPY	0x0004
86 	int		bd_type;	/* BIOS 'drive type' (floppy only) */
87 	uint16_t	bd_sectorsize;	/* Sector size */
88 	uint64_t	bd_sectors;	/* Disk size */
89 	int		bd_open;	/* reference counter */
90 	void		*bd_bcache;	/* buffer cache data */
91 } bdinfo [MAXBDDEV];
92 static int nbdinfo = 0;
93 
94 #define	BD(dev)		(bdinfo[(dev)->d_unit])
95 
96 static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
97     caddr_t dest);
98 static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
99     caddr_t dest);
100 static int bd_int13probe(struct bdinfo *bd);
101 
102 static int bd_init(void);
103 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t offset,
104     size_t size, char *buf, size_t *rsize);
105 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t offset,
106     size_t size, char *buf, size_t *rsize);
107 static int bd_open(struct open_file *f, ...);
108 static int bd_close(struct open_file *f);
109 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
110 static int bd_print(int verbose);
111 static void bd_cleanup(void);
112 
113 struct devsw biosdisk = {
114 	"disk",
115 	DEVT_DISK,
116 	bd_init,
117 	bd_strategy,
118 	bd_open,
119 	bd_close,
120 	bd_ioctl,
121 	bd_print,
122 	bd_cleanup
123 };
124 
125 /*
126  * Translate between BIOS device numbers and our private unit numbers.
127  */
128 int
129 bd_bios2unit(int biosdev)
130 {
131 	int i;
132 
133 	DEBUG("looking for bios device 0x%x", biosdev);
134 	for (i = 0; i < nbdinfo; i++) {
135 		DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
136 		if (bdinfo[i].bd_unit == biosdev)
137 			return (i);
138 	}
139 	return (-1);
140 }
141 
142 int
143 bd_unit2bios(int unit)
144 {
145 
146 	if ((unit >= 0) && (unit < nbdinfo))
147 		return (bdinfo[unit].bd_unit);
148 	return (-1);
149 }
150 
151 /*
152  * Quiz the BIOS for disk devices, save a little info about them.
153  */
154 static int
155 bd_init(void)
156 {
157 	int base, unit, nfd = 0;
158 
159 	/* sequence 0, 0x80 */
160 	for (base = 0; base <= 0x80; base += 0x80) {
161 		for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
162 #ifndef VIRTUALBOX
163 			/*
164 			 * Check the BIOS equipment list for number
165 			 * of fixed disks.
166 			 */
167 			if(base == 0x80 &&
168 			    (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
169 				break;
170 #endif
171 			bdinfo[nbdinfo].bd_open = 0;
172 			bdinfo[nbdinfo].bd_bcache = NULL;
173 			bdinfo[nbdinfo].bd_unit = unit;
174 			bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
175 			if (!bd_int13probe(&bdinfo[nbdinfo]))
176 				break;
177 
178 #ifndef BOOT2
179 			/* XXX we need "disk aliases" to make this simpler */
180 			printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
181 			    ('A' + unit): ('C' + unit - 0x80), nbdinfo);
182 #endif
183 			nbdinfo++;
184 			if (base == 0x80)
185 				nfd++;
186 		}
187 	}
188 	bcache_add_dev(nbdinfo);
189 	return(0);
190 }
191 
192 static void
193 bd_cleanup(void)
194 {
195 
196 	disk_cleanup(&biosdisk);
197 }
198 
199 /*
200  * Try to detect a device supported by the legacy int13 BIOS
201  */
202 static int
203 bd_int13probe(struct bdinfo *bd)
204 {
205 	struct edd_params params;
206 	int ret = 1;	/* assume success */
207 
208 	v86.ctl = V86_FLAGS;
209 	v86.addr = 0x13;
210 	v86.eax = 0x800;
211 	v86.edx = bd->bd_unit;
212 	v86int();
213 
214 	/* Don't error out if we get bad sector number, try EDD as well */
215 	if (V86_CY(v86.efl) ||	/* carry set */
216 	    (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f))	/* unit # bad */
217 		return (0);	/* skip device */
218 
219 	if ((v86.ecx & 0x3f) == 0)	/* absurd sector number */
220 		ret = 0;	/* set error */
221 
222 	/* Convert max cyl # -> # of cylinders */
223 	bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
224 	/* Convert max head # -> # of heads */
225 	bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
226 	bd->bd_sec = v86.ecx & 0x3f;
227 	bd->bd_type = v86.ebx & 0xff;
228 	bd->bd_flags |= BD_MODEINT13;
229 
230 	/* Calculate sectors count from the geometry */
231 	bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
232 	bd->bd_sectorsize = BIOSDISK_SECSIZE;
233 	DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
234 	    bd->bd_hds, bd->bd_sec);
235 
236 	/* Determine if we can use EDD with this device. */
237 	v86.ctl = V86_FLAGS;
238 	v86.addr = 0x13;
239 	v86.eax = 0x4100;
240 	v86.edx = bd->bd_unit;
241 	v86.ebx = 0x55aa;
242 	v86int();
243 	if (V86_CY(v86.efl) ||	/* carry set */
244 	    (v86.ebx & 0xffff) != 0xaa55 || /* signature */
245 	    (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
246 		return (ret);	/* return code from int13 AH=08 */
247 
248 	/* EDD supported */
249 	bd->bd_flags |= BD_MODEEDD1;
250 	if ((v86.eax & 0xff00) >= 0x3000)
251 		bd->bd_flags |= BD_MODEEDD3;
252 	/* Get disk params */
253 	params.len = sizeof(struct edd_params);
254 	v86.ctl = V86_FLAGS;
255 	v86.addr = 0x13;
256 	v86.eax = 0x4800;
257 	v86.edx = bd->bd_unit;
258 	v86.ds = VTOPSEG(&params);
259 	v86.esi = VTOPOFF(&params);
260 	v86int();
261 	if (!V86_CY(v86.efl)) {
262 		uint64_t total;
263 
264 		if (params.sectors != 0)
265 			bd->bd_sectors = params.sectors;
266 
267 		total = (uint64_t)params.cylinders *
268 		    params.heads * params.sectors_per_track;
269 		if (bd->bd_sectors < total)
270 			bd->bd_sectors = total;
271 
272 		bd->bd_sectorsize = params.sector_size;
273 		ret = 1;
274 	}
275 	DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
276 	    bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
277 	return (ret);
278 }
279 
280 /*
281  * Print information about disks
282  */
283 static int
284 bd_print(int verbose)
285 {
286 	static char line[80];
287 	struct disk_devdesc dev;
288 	int i, ret = 0;
289 
290 	for (i = 0; i < nbdinfo; i++) {
291 		sprintf(line, "    disk%d:   BIOS drive %c:\n", i,
292 		    (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
293 		    ('C' + bdinfo[i].bd_unit - 0x80));
294 		ret = pager_output(line);
295 		if (ret != 0)
296 			return (ret);
297 
298 		dev.d_dev = &biosdisk;
299 		dev.d_unit = i;
300 		dev.d_slice = -1;
301 		dev.d_partition = -1;
302 		if (disk_open(&dev,
303 		    bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
304 		    bdinfo[i].bd_sectorsize,
305 		    (bdinfo[i].bd_flags & BD_FLOPPY) ?
306 		    DISK_F_NOCACHE: 0) == 0) {
307 			sprintf(line, "    disk%d", i);
308 			ret = disk_print(&dev, line, verbose);
309 			disk_close(&dev);
310 			if (ret != 0)
311 				return (ret);
312 		}
313 	}
314 	return (ret);
315 }
316 
317 /*
318  * Attempt to open the disk described by (dev) for use by (f).
319  *
320  * Note that the philosophy here is "give them exactly what
321  * they ask for".  This is necessary because being too "smart"
322  * about what the user might want leads to complications.
323  * (eg. given no slice or partition value, with a disk that is
324  *  sliced - are they after the first BSD slice, or the DOS
325  *  slice before it?)
326  */
327 static int
328 bd_open(struct open_file *f, ...)
329 {
330 	struct disk_devdesc *dev;
331 	va_list ap;
332 
333 	va_start(ap, f);
334 	dev = va_arg(ap, struct disk_devdesc *);
335 	va_end(ap);
336 
337 	if (dev->d_unit < 0 || dev->d_unit >= nbdinfo)
338 		return (EIO);
339 	BD(dev).bd_open++;
340 	if (BD(dev).bd_bcache == NULL)
341 	    BD(dev).bd_bcache = bcache_allocate();
342 	return (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
343 	    BD(dev).bd_sectorsize, (BD(dev).bd_flags & BD_FLOPPY) ?
344 	    DISK_F_NOCACHE: 0));
345 }
346 
347 static int
348 bd_close(struct open_file *f)
349 {
350 	struct disk_devdesc *dev;
351 
352 	dev = (struct disk_devdesc *)f->f_devdata;
353 	BD(dev).bd_open--;
354 	if (BD(dev).bd_open == 0) {
355 	    bcache_free(BD(dev).bd_bcache);
356 	    BD(dev).bd_bcache = NULL;
357 	}
358 	return (disk_close(dev));
359 }
360 
361 static int
362 bd_ioctl(struct open_file *f, u_long cmd, void *data)
363 {
364 	struct disk_devdesc *dev;
365 
366 	dev = (struct disk_devdesc *)f->f_devdata;
367 	switch (cmd) {
368 	case DIOCGSECTORSIZE:
369 		*(u_int *)data = BD(dev).bd_sectorsize;
370 		break;
371 	case DIOCGMEDIASIZE:
372 		*(off_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
373 		break;
374 	default:
375 		return (ENOTTY);
376 	}
377 	return (0);
378 }
379 
380 static int
381 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t offset, size_t size,
382     char *buf, size_t *rsize)
383 {
384 	struct bcache_devdata bcd;
385 	struct disk_devdesc *dev;
386 
387 	dev = (struct disk_devdesc *)devdata;
388 	bcd.dv_strategy = bd_realstrategy;
389 	bcd.dv_devdata = devdata;
390 	bcd.dv_cache = BD(dev).bd_bcache;
391 
392 	return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, offset, size,
393 	    buf, rsize));
394 }
395 
396 static int
397 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t offset, size_t size,
398     char *buf, size_t *rsize)
399 {
400     struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
401     int			blks, remaining;
402 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
403     char		fragbuf[BIOSDISK_SECSIZE];
404     size_t		fragsize;
405 
406     fragsize = size % BIOSDISK_SECSIZE;
407 #else
408     if (size % BD(dev).bd_sectorsize)
409 	panic("bd_strategy: %d bytes I/O not multiple of block size", size);
410 #endif
411 
412     DEBUG("open_disk %p", dev);
413     blks = size / BD(dev).bd_sectorsize;
414     if (rsize)
415 	*rsize = 0;
416 
417     /*
418      * Perform partial read to prevent read-ahead crossing
419      * the end of disk - or any 32 bit aliases of the end.
420      * Signed arithmetic is used to handle wrap-around cases
421      * like we do for TCP sequence numbers.
422      */
423     remaining = (int)(BD(dev).bd_sectors - dblk);	/* truncate */
424     if (remaining > 0 && remaining < blks) {
425 	blks = remaining;
426 	size = blks * BD(dev).bd_sectorsize;
427 	DEBUG("short read %d", blks);
428     }
429 
430     switch(rw){
431     case F_READ:
432 	DEBUG("read %d from %lld to %p", blks, dblk, buf);
433 
434 	if (blks && bd_read(dev, dblk, blks, buf)) {
435 	    DEBUG("read error");
436 	    return (EIO);
437 	}
438 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
439 	DEBUG("bd_strategy: frag read %d from %d+%d to %p",
440 	    fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
441 	if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
442 	    DEBUG("frag read error");
443 	    return(EIO);
444 	}
445 	bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
446 #endif
447 	break;
448     case F_WRITE :
449 	DEBUG("write %d from %d to %p", blks, dblk, buf);
450 
451 	if (blks && bd_write(dev, dblk, blks, buf)) {
452 	    DEBUG("write error");
453 	    return (EIO);
454 	}
455 #ifdef BD_SUPPORT_FRAGS
456 	if(fragsize) {
457 	    DEBUG("Attempted to write a frag");
458 	    return (EIO);
459 	}
460 #endif
461 	break;
462     default:
463 	/* DO NOTHING */
464 	return (EROFS);
465     }
466 
467     if (rsize)
468 	*rsize = size;
469     return (0);
470 }
471 
472 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
473 #define FLOPPY_BOUNCEBUF	18
474 
475 static int
476 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
477     int dowrite)
478 {
479     static struct edd_packet packet;
480 
481     packet.len = sizeof(struct edd_packet);
482     packet.count = blks;
483     packet.off = VTOPOFF(dest);
484     packet.seg = VTOPSEG(dest);
485     packet.lba = dblk;
486     v86.ctl = V86_FLAGS;
487     v86.addr = 0x13;
488     if (dowrite)
489 	/* Should we Write with verify ?? 0x4302 ? */
490 	v86.eax = 0x4300;
491     else
492 	v86.eax = 0x4200;
493     v86.edx = BD(dev).bd_unit;
494     v86.ds = VTOPSEG(&packet);
495     v86.esi = VTOPOFF(&packet);
496     v86int();
497     return (V86_CY(v86.efl));
498 }
499 
500 static int
501 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
502     int dowrite)
503 {
504     u_int	x, bpc, cyl, hd, sec;
505 
506     bpc = BD(dev).bd_sec * BD(dev).bd_hds;	/* blocks per cylinder */
507     x = dblk;
508     cyl = x / bpc;			/* block # / blocks per cylinder */
509     x %= bpc;				/* block offset into cylinder */
510     hd = x / BD(dev).bd_sec;		/* offset / blocks per track */
511     sec = x % BD(dev).bd_sec;		/* offset into track */
512 
513     /* correct sector number for 1-based BIOS numbering */
514     sec++;
515 
516     if (cyl > 1023)
517 	/* CHS doesn't support cylinders > 1023. */
518 	return (1);
519 
520     v86.ctl = V86_FLAGS;
521     v86.addr = 0x13;
522     if (dowrite)
523 	v86.eax = 0x300 | blks;
524     else
525 	v86.eax = 0x200 | blks;
526     v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
527     v86.edx = (hd << 8) | BD(dev).bd_unit;
528     v86.es = VTOPSEG(dest);
529     v86.ebx = VTOPOFF(dest);
530     v86int();
531     return (V86_CY(v86.efl));
532 }
533 
534 static int
535 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, int dowrite)
536 {
537     u_int	x, sec, result, resid, retry, maxfer;
538     caddr_t	p, xp, bbuf, breg;
539 
540     /* Just in case some idiot actually tries to read/write -1 blocks... */
541     if (blks < 0)
542 	return (-1);
543 
544     resid = blks;
545     p = dest;
546 
547     /* Decide whether we have to bounce */
548     if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
549 	(VTOP(dest) >> 16) != (VTOP(dest +
550 	blks * BD(dev).bd_sectorsize) >> 16))) {
551 
552 	/*
553 	 * There is a 64k physical boundary somewhere in the
554 	 * destination buffer, or the destination buffer is above
555 	 * first 1MB of physical memory so we have to arrange a
556 	 * suitable bounce buffer.  Allocate a buffer twice as large
557 	 * as we need to.  Use the bottom half unless there is a break
558 	 * there, in which case we use the top half.
559 	 */
560 	x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
561 	bbuf = alloca(x * 2 * BD(dev).bd_sectorsize);
562 	if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
563 	    ((u_int32_t)VTOP(bbuf + x * BD(dev).bd_sectorsize) & 0xffff0000)) {
564 	    breg = bbuf;
565 	} else {
566 	    breg = bbuf + x * BD(dev).bd_sectorsize;
567 	}
568 	maxfer = x;		/* limit transfers to bounce region size */
569     } else {
570 	breg = bbuf = NULL;
571 	maxfer = 0;
572     }
573 
574     while (resid > 0) {
575 	/*
576 	 * Play it safe and don't cross track boundaries.
577 	 * (XXX this is probably unnecessary)
578 	 */
579 	sec = dblk % BD(dev).bd_sec;	/* offset into track */
580 	x = min(BD(dev).bd_sec - sec, resid);
581 	if (maxfer > 0)
582 	    x = min(x, maxfer);		/* fit bounce buffer */
583 
584 	/* where do we transfer to? */
585 	xp = bbuf == NULL ? p : breg;
586 
587 	/*
588 	 * Put your Data In, Put your Data out,
589 	 * Put your Data In, and shake it all about
590 	 */
591 	if (dowrite && bbuf != NULL)
592 	    bcopy(p, breg, x * BD(dev).bd_sectorsize);
593 
594 	/*
595 	 * Loop retrying the operation a couple of times.  The BIOS
596 	 * may also retry.
597 	 */
598 	for (retry = 0; retry < 3; retry++) {
599 	    /* if retrying, reset the drive */
600 	    if (retry > 0) {
601 		v86.ctl = V86_FLAGS;
602 		v86.addr = 0x13;
603 		v86.eax = 0;
604 		v86.edx = BD(dev).bd_unit;
605 		v86int();
606 	    }
607 
608 	    if (BD(dev).bd_flags & BD_MODEEDD1)
609 		result = bd_edd_io(dev, dblk, x, xp, dowrite);
610 	    else
611 		result = bd_chs_io(dev, dblk, x, xp, dowrite);
612 	    if (result == 0)
613 		break;
614 	}
615 
616 	if (dowrite)
617 	    DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
618 		p, VTOP(p), dblk, result ? "failed" : "ok");
619 	else
620 	    DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
621 		dblk, p, VTOP(p), result ? "failed" : "ok");
622 	if (result) {
623 	    return(-1);
624 	}
625 	if (!dowrite && bbuf != NULL)
626 	    bcopy(breg, p, x * BD(dev).bd_sectorsize);
627 	p += (x * BD(dev).bd_sectorsize);
628 	dblk += x;
629 	resid -= x;
630     }
631 
632 /*    hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
633     return(0);
634 }
635 
636 static int
637 bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
638 {
639 
640 	return (bd_io(dev, dblk, blks, dest, 0));
641 }
642 
643 static int
644 bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
645 {
646 
647 	return (bd_io(dev, dblk, blks, dest, 1));
648 }
649 
650 /*
651  * Return the BIOS geometry of a given "fixed drive" in a format
652  * suitable for the legacy bootinfo structure.  Since the kernel is
653  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
654  * prefer to get the information directly, rather than rely on being
655  * able to put it together from information already maintained for
656  * different purposes and for a probably different number of drives.
657  *
658  * For valid drives, the geometry is expected in the format (31..0)
659  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
660  * indicated by returning the geometry of a "1.2M" PC-format floppy
661  * disk.  And, incidentally, what is returned is not the geometry as
662  * such but the highest valid cylinder, head, and sector numbers.
663  */
664 u_int32_t
665 bd_getbigeom(int bunit)
666 {
667 
668     v86.ctl = V86_FLAGS;
669     v86.addr = 0x13;
670     v86.eax = 0x800;
671     v86.edx = 0x80 + bunit;
672     v86int();
673     if (V86_CY(v86.efl))
674 	return 0x4f010f;
675     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
676 	   (v86.edx & 0xff00) | (v86.ecx & 0x3f);
677 }
678 
679 /*
680  * Return a suitable dev_t value for (dev).
681  *
682  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
683  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
684  */
685 int
686 bd_getdev(struct i386_devdesc *d)
687 {
688     struct disk_devdesc		*dev;
689     int				biosdev;
690     int 			major;
691     int				rootdev;
692     char			*nip, *cp;
693     int				i, unit;
694 
695     dev = (struct disk_devdesc *)d;
696     biosdev = bd_unit2bios(dev->d_unit);
697     DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
698     if (biosdev == -1)				/* not a BIOS device */
699 	return(-1);
700     if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
701 	BD(dev).bd_sectorsize,(BD(dev).bd_flags & BD_FLOPPY) ?
702 	DISK_F_NOCACHE: 0) != 0)		/* oops, not a viable device */
703 	    return (-1);
704     else
705 	disk_close(dev);
706 
707     if (biosdev < 0x80) {
708 	/* floppy (or emulated floppy) or ATAPI device */
709 	if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
710 	    /* is an ATAPI disk */
711 	    major = WFDMAJOR;
712 	} else {
713 	    /* is a floppy disk */
714 	    major = FDMAJOR;
715 	}
716     } else {
717 	    /* assume an IDE disk */
718 	    major = WDMAJOR;
719     }
720     /* default root disk unit number */
721     unit = biosdev & 0x7f;
722 
723     /* XXX a better kludge to set the root disk unit number */
724     if ((nip = getenv("root_disk_unit")) != NULL) {
725 	i = strtol(nip, &cp, 0);
726 	/* check for parse error */
727 	if ((cp != nip) && (*cp == 0))
728 	    unit = i;
729     }
730 
731     rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
732     DEBUG("dev is 0x%x\n", rootdev);
733     return(rootdev);
734 }
735