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