xref: /freebsd/stand/i386/libi386/biosdisk.c (revision 6044931335ca0fe7ecf61d2dcebc520c7a557ce6)
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 <sys/limits.h>
43 #include <stand.h>
44 #include <machine/bootinfo.h>
45 #include <stdarg.h>
46 
47 #include <bootstrap.h>
48 #include <btxv86.h>
49 #include <edd.h>
50 #include "disk.h"
51 #include "libi386.h"
52 
53 #define	BIOS_NUMDRIVES		0x475
54 #define	BIOSDISK_SECSIZE	512
55 #define	BUFSIZE			(1 * BIOSDISK_SECSIZE)
56 
57 #define	DT_ATAPI	0x10	/* disk type for ATAPI floppies */
58 #define	WDMAJOR		0	/* major numbers for devices we frontend for */
59 #define	WFDMAJOR	1
60 #define	FDMAJOR		2
61 #define	DAMAJOR		4
62 
63 #ifdef DISK_DEBUG
64 #define	DEBUG(fmt, args...)	printf("%s: " fmt "\n", __func__, ## args)
65 #else
66 #define	DEBUG(fmt, args...)
67 #endif
68 
69 /*
70  * List of BIOS devices, translation from disk unit number to
71  * BIOS unit number.
72  */
73 static struct bdinfo
74 {
75 	int		bd_unit;	/* BIOS unit number */
76 	int		bd_cyl;		/* BIOS geometry */
77 	int		bd_hds;
78 	int		bd_sec;
79 	int		bd_flags;
80 #define	BD_MODEINT13	0x0000
81 #define	BD_MODEEDD1	0x0001
82 #define	BD_MODEEDD3	0x0002
83 #define	BD_MODEMASK	0x0003
84 #define	BD_FLOPPY	0x0004
85 	int		bd_type;	/* BIOS 'drive type' (floppy only) */
86 	uint16_t	bd_sectorsize;	/* Sector size */
87 	uint64_t	bd_sectors;	/* Disk size */
88 	int		bd_open;	/* reference counter */
89 	void		*bd_bcache;	/* buffer cache data */
90 } bdinfo [MAXBDDEV];
91 static int nbdinfo = 0;
92 
93 #define	BD(dev)		(bdinfo[(dev)->dd.d_unit])
94 
95 static void bd_io_workaround(struct disk_devdesc *dev);
96 
97 static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
98     caddr_t dest);
99 static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
100     caddr_t dest);
101 static int bd_int13probe(struct bdinfo *bd);
102 
103 static int bd_init(void);
104 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
105     char *buf, size_t *rsize);
106 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
107     char *buf, size_t *rsize);
108 static int bd_open(struct open_file *f, ...);
109 static int bd_close(struct open_file *f);
110 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
111 static int bd_print(int verbose);
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 	NULL
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 			/* XXX we need "disk aliases" to make this simpler */
179 			printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
180 			    ('A' + unit): ('C' + unit - 0x80), nbdinfo);
181 			nbdinfo++;
182 			if (base == 0x80)
183 				nfd++;
184 		}
185 	}
186 	bcache_add_dev(nbdinfo);
187 	return (0);
188 }
189 
190 /*
191  * Try to detect a device supported by the legacy int13 BIOS
192  */
193 static int
194 bd_int13probe(struct bdinfo *bd)
195 {
196 	struct edd_params params;
197 	int ret = 1;	/* assume success */
198 
199 	v86.ctl = V86_FLAGS;
200 	v86.addr = 0x13;
201 	v86.eax = 0x800;
202 	v86.edx = bd->bd_unit;
203 	v86int();
204 
205 	/* Don't error out if we get bad sector number, try EDD as well */
206 	if (V86_CY(v86.efl) ||	/* carry set */
207 	    (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f))	/* unit # bad */
208 		return (0);	/* skip device */
209 
210 	if ((v86.ecx & 0x3f) == 0)	/* absurd sector number */
211 		ret = 0;	/* set error */
212 
213 	/* Convert max cyl # -> # of cylinders */
214 	bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
215 	/* Convert max head # -> # of heads */
216 	bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
217 	bd->bd_sec = v86.ecx & 0x3f;
218 	bd->bd_type = v86.ebx & 0xff;
219 	bd->bd_flags |= BD_MODEINT13;
220 
221 	/* Calculate sectors count from the geometry */
222 	bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
223 	bd->bd_sectorsize = BIOSDISK_SECSIZE;
224 	DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
225 	    bd->bd_hds, bd->bd_sec);
226 
227 	/* Determine if we can use EDD with this device. */
228 	v86.ctl = V86_FLAGS;
229 	v86.addr = 0x13;
230 	v86.eax = 0x4100;
231 	v86.edx = bd->bd_unit;
232 	v86.ebx = 0x55aa;
233 	v86int();
234 	if (V86_CY(v86.efl) ||	/* carry set */
235 	    (v86.ebx & 0xffff) != 0xaa55 || /* signature */
236 	    (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
237 		return (ret);	/* return code from int13 AH=08 */
238 
239 	/* EDD supported */
240 	bd->bd_flags |= BD_MODEEDD1;
241 	if ((v86.eax & 0xff00) >= 0x3000)
242 		bd->bd_flags |= BD_MODEEDD3;
243 	/* Get disk params */
244 	params.len = sizeof(struct edd_params);
245 	v86.ctl = V86_FLAGS;
246 	v86.addr = 0x13;
247 	v86.eax = 0x4800;
248 	v86.edx = bd->bd_unit;
249 	v86.ds = VTOPSEG(&params);
250 	v86.esi = VTOPOFF(&params);
251 	v86int();
252 	if (!V86_CY(v86.efl)) {
253 		uint64_t total;
254 
255 		/*
256 		 * Sector size must be a multiple of 512 bytes.
257 		 * An alternate test would be to check power of 2,
258 		 * powerof2(params.sector_size).
259 		 */
260 		if (params.sector_size % BIOSDISK_SECSIZE)
261 			bd->bd_sectorsize = BIOSDISK_SECSIZE;
262 		else
263 			bd->bd_sectorsize = params.sector_size;
264 
265 		total = bd->bd_sectorsize * params.sectors;
266 		if (params.sectors != 0) {
267 			/* Only update if we did not overflow. */
268 			if (total > params.sectors)
269 				bd->bd_sectors = params.sectors;
270 		}
271 
272 		total = (uint64_t)params.cylinders *
273 		    params.heads * params.sectors_per_track;
274 		if (total > 0 && bd->bd_sectors > total)
275 			bd->bd_sectors = total;
276 
277 		ret = 1;
278 	}
279 	DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
280 	    bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
281 	return (ret);
282 }
283 
284 /*
285  * Print information about disks
286  */
287 static int
288 bd_print(int verbose)
289 {
290 	static char line[80];
291 	struct disk_devdesc dev;
292 	int i, ret = 0;
293 
294 	if (nbdinfo == 0)
295 		return (0);
296 
297 	printf("%s devices:", biosdisk.dv_name);
298 	if ((ret = pager_output("\n")) != 0)
299 		return (ret);
300 
301 	for (i = 0; i < nbdinfo; i++) {
302 		snprintf(line, sizeof(line),
303 		    "    disk%d:   BIOS drive %c (%ju X %u):\n", i,
304 		    (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
305 		    ('C' + bdinfo[i].bd_unit - 0x80),
306 		    (uintmax_t)bdinfo[i].bd_sectors,
307 		    bdinfo[i].bd_sectorsize);
308 		if ((ret = pager_output(line)) != 0)
309 			break;
310 		dev.dd.d_dev = &biosdisk;
311 		dev.dd.d_unit = i;
312 		dev.d_slice = -1;
313 		dev.d_partition = -1;
314 		if (disk_open(&dev,
315 		    bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
316 		    bdinfo[i].bd_sectorsize) == 0) {
317 			snprintf(line, sizeof(line), "    disk%d", i);
318 			ret = disk_print(&dev, line, verbose);
319 			disk_close(&dev);
320 			if (ret != 0)
321 				return (ret);
322 		}
323 	}
324 	return (ret);
325 }
326 
327 /*
328  * Attempt to open the disk described by (dev) for use by (f).
329  *
330  * Note that the philosophy here is "give them exactly what
331  * they ask for".  This is necessary because being too "smart"
332  * about what the user might want leads to complications.
333  * (eg. given no slice or partition value, with a disk that is
334  *  sliced - are they after the first BSD slice, or the DOS
335  *  slice before it?)
336  */
337 static int
338 bd_open(struct open_file *f, ...)
339 {
340 	struct disk_devdesc *dev;
341 	struct disk_devdesc disk;
342 	va_list ap;
343 	uint64_t size;
344 	int rc;
345 
346 	va_start(ap, f);
347 	dev = va_arg(ap, struct disk_devdesc *);
348 	va_end(ap);
349 
350 	if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo)
351 		return (EIO);
352 	BD(dev).bd_open++;
353 	if (BD(dev).bd_bcache == NULL)
354 	    BD(dev).bd_bcache = bcache_allocate();
355 
356 	/*
357 	 * Read disk size from partition.
358 	 * This is needed to work around buggy BIOS systems returning
359 	 * wrong (truncated) disk media size.
360 	 * During bd_probe() we tested if the mulitplication of bd_sectors
361 	 * would overflow so it should be safe to perform here.
362 	 */
363 	disk.dd.d_dev = dev->dd.d_dev;
364 	disk.dd.d_unit = dev->dd.d_unit;
365 	disk.d_slice = -1;
366 	disk.d_partition = -1;
367 	disk.d_offset = 0;
368 
369 	if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
370 	    BD(dev).bd_sectorsize) == 0) {
371 
372 		if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
373 			size /= BD(dev).bd_sectorsize;
374 			if (size > BD(dev).bd_sectors)
375 				BD(dev).bd_sectors = size;
376 		}
377 		disk_close(&disk);
378 	}
379 
380 	rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
381 	    BD(dev).bd_sectorsize);
382 	if (rc != 0) {
383 		BD(dev).bd_open--;
384 		if (BD(dev).bd_open == 0) {
385 			bcache_free(BD(dev).bd_bcache);
386 			BD(dev).bd_bcache = NULL;
387 		}
388 	}
389 
390 	return (rc);
391 }
392 
393 static int
394 bd_close(struct open_file *f)
395 {
396 	struct disk_devdesc *dev;
397 
398 	dev = (struct disk_devdesc *)f->f_devdata;
399 	BD(dev).bd_open--;
400 	if (BD(dev).bd_open == 0) {
401 	    bcache_free(BD(dev).bd_bcache);
402 	    BD(dev).bd_bcache = NULL;
403 	}
404 	return (disk_close(dev));
405 }
406 
407 static int
408 bd_ioctl(struct open_file *f, u_long cmd, void *data)
409 {
410 	struct disk_devdesc *dev;
411 	int rc;
412 
413 	dev = (struct disk_devdesc *)f->f_devdata;
414 
415 	rc = disk_ioctl(dev, cmd, data);
416 	if (rc != ENOTTY)
417 		return (rc);
418 
419 	switch (cmd) {
420 	case DIOCGSECTORSIZE:
421 		*(uint32_t *)data = BD(dev).bd_sectorsize;
422 		break;
423 	case DIOCGMEDIASIZE:
424 		*(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
425 		break;
426 	default:
427 		return (ENOTTY);
428 	}
429 	return (0);
430 }
431 
432 static int
433 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
434     char *buf, size_t *rsize)
435 {
436 	struct bcache_devdata bcd;
437 	struct disk_devdesc *dev;
438 
439 	dev = (struct disk_devdesc *)devdata;
440 	bcd.dv_strategy = bd_realstrategy;
441 	bcd.dv_devdata = devdata;
442 	bcd.dv_cache = BD(dev).bd_bcache;
443 	return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
444 	    buf, rsize));
445 }
446 
447 static int
448 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
449     char *buf, size_t *rsize)
450 {
451 	struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
452 	uint64_t		disk_blocks;
453 	int			blks, rc;
454 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
455 	char		fragbuf[BIOSDISK_SECSIZE];
456 	size_t		fragsize;
457 
458 	fragsize = size % BIOSDISK_SECSIZE;
459 #else
460 	if (size % BD(dev).bd_sectorsize) {
461 		panic("bd_strategy: %d bytes I/O not multiple of block size",
462 		    size);
463 	}
464 #endif
465 
466 	DEBUG("open_disk %p", dev);
467 
468 	/*
469 	 * Check the value of the size argument. We do have quite small
470 	 * heap (64MB), but we do not know good upper limit, so we check against
471 	 * INT_MAX here. This will also protect us against possible overflows
472 	 * while translating block count to bytes.
473 	 */
474 	if (size > INT_MAX) {
475 		DEBUG("too large read: %zu bytes", size);
476 		return (EIO);
477 	}
478 
479 	blks = size / BD(dev).bd_sectorsize;
480 	if (dblk > dblk + blks)
481 		return (EIO);
482 
483 	if (rsize)
484 		*rsize = 0;
485 
486 	/*
487 	 * Get disk blocks, this value is either for whole disk or for
488 	 * partition.
489 	 */
490 	if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
491 		/* DIOCGMEDIASIZE returns bytes. */
492 		disk_blocks /= BD(dev).bd_sectorsize;
493 	} else {
494 		/* We should not get here. Just try to survive. */
495 		disk_blocks = BD(dev).bd_sectors - dev->d_offset;
496 	}
497 
498 	/* Validate source block address. */
499 	if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
500 		return (EIO);
501 
502 	/*
503 	 * Truncate if we are crossing disk or partition end.
504 	 */
505 	if (dblk + blks >= dev->d_offset + disk_blocks) {
506 		blks = dev->d_offset + disk_blocks - dblk;
507 		size = blks * BD(dev).bd_sectorsize;
508 		DEBUG("short read %d", blks);
509 	}
510 
511 	switch (rw & F_MASK) {
512 	case F_READ:
513 		DEBUG("read %d from %lld to %p", blks, dblk, buf);
514 
515 		if (blks && (rc = bd_read(dev, dblk, blks, buf))) {
516 			/* Filter out floppy controller errors */
517 			if (BD(dev).bd_flags != BD_FLOPPY || rc != 0x20) {
518 				printf("read %d from %lld to %p, error: 0x%x\n",
519 				    blks, dblk, buf, rc);
520 			}
521 			return (EIO);
522 		}
523 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
524 		DEBUG("bd_strategy: frag read %d from %d+%d to %p",
525 		    fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
526 		if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
527 			DEBUG("frag read error");
528 			return (EIO);
529 		}
530 		bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
531 #endif
532 		break;
533 	case F_WRITE :
534 		DEBUG("write %d from %lld to %p", blks, dblk, buf);
535 
536 		if (blks && bd_write(dev, dblk, blks, buf)) {
537 			DEBUG("write error");
538 			return (EIO);
539 		}
540 #ifdef BD_SUPPORT_FRAGS
541 		if (fragsize) {
542 			DEBUG("Attempted to write a frag");
543 			return (EIO);
544 		}
545 #endif
546 		break;
547 	default:
548 		/* DO NOTHING */
549 		return (EROFS);
550 	}
551 
552 	if (rsize)
553 		*rsize = size;
554 	return (0);
555 }
556 
557 static int
558 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
559     int dowrite)
560 {
561 	static struct edd_packet packet;
562 
563 	packet.len = sizeof(struct edd_packet);
564 	packet.count = blks;
565 	packet.off = VTOPOFF(dest);
566 	packet.seg = VTOPSEG(dest);
567 	packet.lba = dblk;
568 	v86.ctl = V86_FLAGS;
569 	v86.addr = 0x13;
570 	/* Should we Write with verify ?? 0x4302 ? */
571 	if (dowrite)
572 		v86.eax = 0x4300;
573 	else
574 		v86.eax = 0x4200;
575 	v86.edx = BD(dev).bd_unit;
576 	v86.ds = VTOPSEG(&packet);
577 	v86.esi = VTOPOFF(&packet);
578 	v86int();
579 	if (V86_CY(v86.efl))
580 		return (v86.eax >> 8);
581 	return (0);
582 }
583 
584 static int
585 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
586     int dowrite)
587 {
588 	uint32_t x, bpc, cyl, hd, sec;
589 
590 	bpc = BD(dev).bd_sec * BD(dev).bd_hds;	/* blocks per cylinder */
591 	x = dblk;
592 	cyl = x / bpc;			/* block # / blocks per cylinder */
593 	x %= bpc;				/* block offset into cylinder */
594 	hd = x / BD(dev).bd_sec;		/* offset / blocks per track */
595 	sec = x % BD(dev).bd_sec;		/* offset into track */
596 
597 	/* correct sector number for 1-based BIOS numbering */
598 	sec++;
599 
600 	if (cyl > 1023) {
601 		/* CHS doesn't support cylinders > 1023. */
602 		return (1);
603 	}
604 
605 	v86.ctl = V86_FLAGS;
606 	v86.addr = 0x13;
607 	if (dowrite)
608 		v86.eax = 0x300 | blks;
609 	else
610 		v86.eax = 0x200 | blks;
611 	v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
612 	v86.edx = (hd << 8) | BD(dev).bd_unit;
613 	v86.es = VTOPSEG(dest);
614 	v86.ebx = VTOPOFF(dest);
615 	v86int();
616 	if (V86_CY(v86.efl))
617 		return (v86.eax >> 8);
618 	return (0);
619 }
620 
621 static void
622 bd_io_workaround(struct disk_devdesc *dev)
623 {
624 	uint8_t buf[8 * 1024];
625 
626 	bd_edd_io(dev, 0xffffffff, 1, (caddr_t)buf, 0);
627 }
628 
629 
630 static int
631 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
632     int dowrite)
633 {
634 	u_int	x, sec, result, resid, retry, maxfer;
635 	caddr_t	p, xp, bbuf;
636 
637 	/* Just in case some idiot actually tries to read/write -1 blocks... */
638 	if (blks < 0)
639 		return (-1);
640 
641 	resid = blks;
642 	p = dest;
643 
644 	/*
645 	 * Workaround for a problem with some HP ProLiant BIOS failing to work
646 	 * out the boot disk after installation. hrs and kuriyama discovered
647 	 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
648 	 * discovered that an int13h call seems to cause a buffer overrun in
649 	 * the bios. The problem is alleviated by doing an extra read before
650 	 * the buggy read. It is not immediately known whether other models
651 	 * are similarly affected.
652 	 */
653 	if (dblk >= 0x100000000)
654 		bd_io_workaround(dev);
655 
656 	/* Decide whether we have to bounce */
657 	if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
658 	    (VTOP(dest) >> 16) !=
659 	    (VTOP(dest + blks * BD(dev).bd_sectorsize) >> 16))) {
660 
661 		/*
662 		 * There is a 64k physical boundary somewhere in the
663 		 * destination buffer, or the destination buffer is above
664 		 * first 1MB of physical memory so we have to arrange a
665 		 * suitable bounce buffer.  Allocate a buffer twice as large
666 		 * as we need to.  Use the bottom half unless there is a break
667 		 * there, in which case we use the top half.
668 		 */
669 		x = V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize;
670 		x = min(x, (unsigned)blks);
671 		bbuf = PTOV(V86_IO_BUFFER);
672 		maxfer = x;	/* limit transfers to bounce region size */
673 	} else {
674 		bbuf = NULL;
675 		maxfer = 0;
676 	}
677 
678 	while (resid > 0) {
679 		/*
680 		 * Play it safe and don't cross track boundaries.
681 		 * (XXX this is probably unnecessary)
682 		 */
683 		sec = dblk % BD(dev).bd_sec;	/* offset into track */
684 		x = min(BD(dev).bd_sec - sec, resid);
685 		if (maxfer > 0)
686 			x = min(x, maxfer);		/* fit bounce buffer */
687 
688 		/* where do we transfer to? */
689 		xp = bbuf == NULL ? p : bbuf;
690 
691 		/*
692 		 * Put your Data In, Put your Data out,
693 		 * Put your Data In, and shake it all about
694 		 */
695 		if (dowrite && bbuf != NULL)
696 			bcopy(p, bbuf, x * BD(dev).bd_sectorsize);
697 
698 		/*
699 		 * Loop retrying the operation a couple of times.  The BIOS
700 		 * may also retry.
701 		 */
702 		for (retry = 0; retry < 3; retry++) {
703 			/* if retrying, reset the drive */
704 			if (retry > 0) {
705 				v86.ctl = V86_FLAGS;
706 				v86.addr = 0x13;
707 				v86.eax = 0;
708 				v86.edx = BD(dev).bd_unit;
709 				v86int();
710 			}
711 
712 			if (BD(dev).bd_flags & BD_MODEEDD1)
713 				result = bd_edd_io(dev, dblk, x, xp, dowrite);
714 			else
715 				result = bd_chs_io(dev, dblk, x, xp, dowrite);
716 			if (result == 0)
717 				break;
718 		}
719 
720 		if (dowrite)
721 			DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
722 			    p, VTOP(p), dblk, result ? "failed" : "ok");
723 		else
724 			DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
725 			    dblk, p, VTOP(p), result ? "failed" : "ok");
726 		if (result) {
727 			return (result);
728 		}
729 		if (!dowrite && bbuf != NULL)
730 			bcopy(bbuf, p, x * BD(dev).bd_sectorsize);
731 		p += (x * BD(dev).bd_sectorsize);
732 		dblk += x;
733 		resid -= x;
734 	}
735 
736 	return (0);
737 }
738 
739 static int
740 bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
741 {
742 
743 	return (bd_io(dev, dblk, blks, dest, 0));
744 }
745 
746 static int
747 bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
748 {
749 
750 	return (bd_io(dev, dblk, blks, dest, 1));
751 }
752 
753 /*
754  * Return the BIOS geometry of a given "fixed drive" in a format
755  * suitable for the legacy bootinfo structure.  Since the kernel is
756  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
757  * prefer to get the information directly, rather than rely on being
758  * able to put it together from information already maintained for
759  * different purposes and for a probably different number of drives.
760  *
761  * For valid drives, the geometry is expected in the format (31..0)
762  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
763  * indicated by returning the geometry of a "1.2M" PC-format floppy
764  * disk.  And, incidentally, what is returned is not the geometry as
765  * such but the highest valid cylinder, head, and sector numbers.
766  */
767 uint32_t
768 bd_getbigeom(int bunit)
769 {
770 
771 	v86.ctl = V86_FLAGS;
772 	v86.addr = 0x13;
773 	v86.eax = 0x800;
774 	v86.edx = 0x80 + bunit;
775 	v86int();
776 	if (V86_CY(v86.efl))
777 		return (0x4f010f);
778 	return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
779 	    (v86.edx & 0xff00) | (v86.ecx & 0x3f));
780 }
781 
782 /*
783  * Return a suitable dev_t value for (dev).
784  *
785  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
786  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
787  */
788 int
789 bd_getdev(struct i386_devdesc *d)
790 {
791 	struct disk_devdesc *dev;
792 	int	biosdev;
793 	int	major;
794 	int	rootdev;
795 	char	*nip, *cp;
796 	int	i, unit;
797 
798 	dev = (struct disk_devdesc *)d;
799 	biosdev = bd_unit2bios(dev->dd.d_unit);
800 	DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
801 	if (biosdev == -1)			/* not a BIOS device */
802 		return (-1);
803 	if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
804 	    BD(dev).bd_sectorsize) != 0)	/* oops, not a viable device */
805 		return (-1);
806 	else
807 		disk_close(dev);
808 
809 	if (biosdev < 0x80) {
810 		/* floppy (or emulated floppy) or ATAPI device */
811 		if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
812 			/* is an ATAPI disk */
813 			major = WFDMAJOR;
814 		} else {
815 			/* is a floppy disk */
816 			major = FDMAJOR;
817 		}
818 	} else {
819 		/* assume an IDE disk */
820 		major = WDMAJOR;
821 	}
822 	/* default root disk unit number */
823 	unit = biosdev & 0x7f;
824 
825 	/* XXX a better kludge to set the root disk unit number */
826 	if ((nip = getenv("root_disk_unit")) != NULL) {
827 		i = strtol(nip, &cp, 0);
828 		/* check for parse error */
829 		if ((cp != nip) && (*cp == 0))
830 			unit = i;
831 	}
832 
833 	rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
834 	DEBUG("dev is 0x%x\n", rootdev);
835 	return (rootdev);
836 }
837