xref: /titanic_52/usr/src/boot/sys/boot/i386/libi386/biosdisk.c (revision ccce536fd7b8e1131c78b93475c49593f3b91675)
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 
30 /*
31  * BIOS disk device handling.
32  *
33  * Ideas and algorithms from:
34  *
35  * - NetBSD libi386/biosdisk.c
36  * - FreeBSD biosboot/disk.c
37  *
38  */
39 
40 #include <sys/disk.h>
41 #include <sys/limits.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_io(struct disk_devdesc *, daddr_t, int, caddr_t, int);
97 static int bd_int13probe(struct bdinfo *bd);
98 
99 static int bd_init(void);
100 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
101     char *buf, size_t *rsize);
102 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
103     char *buf, size_t *rsize);
104 static int bd_open(struct open_file *f, ...);
105 static int bd_close(struct open_file *f);
106 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
107 static int bd_print(int verbose);
108 
109 struct devsw biosdisk = {
110 	"disk",
111 	DEVT_DISK,
112 	bd_init,
113 	bd_strategy,
114 	bd_open,
115 	bd_close,
116 	bd_ioctl,
117 	bd_print,
118 	NULL
119 };
120 
121 /*
122  * Translate between BIOS device numbers and our private unit numbers.
123  */
124 int
125 bd_bios2unit(int biosdev)
126 {
127 	int i;
128 
129 	DEBUG("looking for bios device 0x%x", biosdev);
130 	for (i = 0; i < nbdinfo; i++) {
131 		DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
132 		if (bdinfo[i].bd_unit == biosdev)
133 			return (i);
134 	}
135 	return (-1);
136 }
137 
138 int
139 bd_unit2bios(int unit)
140 {
141 
142 	if ((unit >= 0) && (unit < nbdinfo))
143 		return (bdinfo[unit].bd_unit);
144 	return (-1);
145 }
146 
147 /*
148  * Quiz the BIOS for disk devices, save a little info about them.
149  */
150 static int
151 bd_init(void)
152 {
153 	int base, unit, nfd = 0;
154 
155 	/* sequence 0, 0x80 */
156 	for (base = 0; base <= 0x80; base += 0x80) {
157 		for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
158 #ifndef VIRTUALBOX
159 			/*
160 			 * Check the BIOS equipment list for number
161 			 * of fixed disks.
162 			 */
163 			if(base == 0x80 &&
164 			    (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
165 				break;
166 #endif
167 			bdinfo[nbdinfo].bd_open = 0;
168 			bdinfo[nbdinfo].bd_bcache = NULL;
169 			bdinfo[nbdinfo].bd_unit = unit;
170 			bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
171 			if (!bd_int13probe(&bdinfo[nbdinfo]))
172 				break;
173 
174 #ifndef BOOT2
175 			/* XXX we need "disk aliases" to make this simpler */
176 			printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
177 			    ('A' + unit): ('C' + unit - 0x80), nbdinfo);
178 #endif
179 			nbdinfo++;
180 			if (base == 0x80)
181 				nfd++;
182 		}
183 	}
184 	bcache_add_dev(nbdinfo);
185 	return(0);
186 }
187 
188 /*
189  * Try to detect a device supported by the legacy int13 BIOS
190  */
191 static int
192 bd_int13probe(struct bdinfo *bd)
193 {
194 	struct edd_params params;
195 	int ret = 1;	/* assume success */
196 
197 	v86.ctl = V86_FLAGS;
198 	v86.addr = 0x13;
199 	v86.eax = 0x800;
200 	v86.edx = bd->bd_unit;
201 	v86int();
202 
203 	/* Don't error out if we get bad sector number, try EDD as well */
204 	if (V86_CY(v86.efl) ||	/* carry set */
205 	    (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f))	/* unit # bad */
206 		return (0);	/* skip device */
207 
208 	if ((v86.ecx & 0x3f) == 0)	/* absurd sector number */
209 		ret = 0;	/* set error */
210 
211 	/* Convert max cyl # -> # of cylinders */
212 	bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
213 	/* Convert max head # -> # of heads */
214 	bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
215 	bd->bd_sec = v86.ecx & 0x3f;
216 	bd->bd_type = v86.ebx & 0xff;
217 	bd->bd_flags |= BD_MODEINT13;
218 
219 	/* Calculate sectors count from the geometry */
220 	bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
221 	bd->bd_sectorsize = BIOSDISK_SECSIZE;
222 	DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
223 	    bd->bd_hds, bd->bd_sec);
224 
225 	/* Determine if we can use EDD with this device. */
226 	v86.ctl = V86_FLAGS;
227 	v86.addr = 0x13;
228 	v86.eax = 0x4100;
229 	v86.edx = bd->bd_unit;
230 	v86.ebx = 0x55aa;
231 	v86int();
232 	if (V86_CY(v86.efl) ||	/* carry set */
233 	    (v86.ebx & 0xffff) != 0xaa55 || /* signature */
234 	    (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
235 		return (ret);	/* return code from int13 AH=08 */
236 
237 	/* EDD supported */
238 	bd->bd_flags |= BD_MODEEDD1;
239 	if ((v86.eax & 0xff00) >= 0x3000)
240 		bd->bd_flags |= BD_MODEEDD3;
241 	/* Get disk params */
242 	params.len = sizeof(struct edd_params);
243 	v86.ctl = V86_FLAGS;
244 	v86.addr = 0x13;
245 	v86.eax = 0x4800;
246 	v86.edx = bd->bd_unit;
247 	v86.ds = VTOPSEG(&params);
248 	v86.esi = VTOPOFF(&params);
249 	v86int();
250 	if (!V86_CY(v86.efl)) {
251 		uint64_t total;
252 
253 		/*
254 		 * Sector size must be a multiple of 512 bytes.
255 		 * An alternate test would be to check power of 2,
256 		 * powerof2(params.sector_size).
257 		 */
258 		if (params.sector_size % BIOSDISK_SECSIZE)
259 			bd->bd_sectorsize = BIOSDISK_SECSIZE;
260 		else
261 			bd->bd_sectorsize = params.sector_size;
262 
263 		total = bd->bd_sectorsize * params.sectors;
264 		if (params.sectors != 0) {
265 			/* Only update if we did not overflow. */
266 			if (total > params.sectors)
267 				bd->bd_sectors = params.sectors;
268 		}
269 
270 		total = (uint64_t)params.cylinders *
271 		    params.heads * params.sectors_per_track;
272 		if (bd->bd_sectors < total)
273 			bd->bd_sectors = total;
274 
275 		ret = 1;
276 	}
277 	DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
278 	    bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
279 	return (ret);
280 }
281 
282 /*
283  * Print information about disks
284  */
285 static int
286 bd_print(int verbose)
287 {
288 	static char line[80];
289 	struct disk_devdesc dev;
290 	int i, ret = 0;
291 
292 	if (nbdinfo == 0)
293 		return (0);
294 
295 	printf("%s devices:", biosdisk.dv_name);
296 	if ((ret = pager_output("\n")) != 0)
297 		return (ret);
298 
299 	for (i = 0; i < nbdinfo; i++) {
300 		snprintf(line, sizeof (line),
301 		    "    disk%d:   BIOS drive %c (%ju X %u):\n", i,
302 		    (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
303 		    ('C' + bdinfo[i].bd_unit - 0x80),
304 		    (uintmax_t)bdinfo[i].bd_sectors,
305 		    bdinfo[i].bd_sectorsize);
306 		ret = pager_output(line);
307 		if (ret != 0)
308 			return (ret);
309 
310 		dev.d_dev = &biosdisk;
311 		dev.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 			sprintf(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->d_unit < 0 || dev->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.d_dev = dev->d_dev;
364 	disk.d_type = dev->d_type;
365 	disk.d_unit = dev->d_unit;
366 	disk.d_opendata = NULL;
367 	disk.d_slice = -1;
368 	disk.d_partition = -1;
369 	disk.d_offset = 0;
370 
371 	if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
372 	    BD(dev).bd_sectorsize) == 0) {
373 
374 		if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
375 			size /= BD(dev).bd_sectorsize;
376 			if (size > BD(dev).bd_sectors)
377 				BD(dev).bd_sectors = size;
378 		}
379 		disk_close(&disk);
380 	}
381 
382 	rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
383 	    BD(dev).bd_sectorsize);
384 	if (rc != 0) {
385 		BD(dev).bd_open--;
386 		if (BD(dev).bd_open == 0) {
387 			bcache_free(BD(dev).bd_bcache);
388 			BD(dev).bd_bcache = NULL;
389 		}
390 	}
391 	return (rc);
392 }
393 
394 static int
395 bd_close(struct open_file *f)
396 {
397 	struct disk_devdesc *dev;
398 
399 	dev = (struct disk_devdesc *)f->f_devdata;
400 	BD(dev).bd_open--;
401 	if (BD(dev).bd_open == 0) {
402 	    bcache_free(BD(dev).bd_bcache);
403 	    BD(dev).bd_bcache = NULL;
404 	}
405 	return (disk_close(dev));
406 }
407 
408 static int
409 bd_ioctl(struct open_file *f, u_long cmd, void *data)
410 {
411 	struct disk_devdesc *dev;
412 	int rc;
413 
414 	dev = (struct disk_devdesc *)f->f_devdata;
415 
416 	rc = disk_ioctl(dev, cmd, data);
417 	if (rc != ENOTTY)
418 		return (rc);
419 
420 	switch (cmd) {
421 	case DIOCGSECTORSIZE:
422 		*(u_int *)data = BD(dev).bd_sectorsize;
423 		break;
424 	case DIOCGMEDIASIZE:
425 		*(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
426 		break;
427 	default:
428 		return (ENOTTY);
429 	}
430 	return (0);
431 }
432 
433 static int
434 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
435     char *buf, size_t *rsize)
436 {
437 	struct bcache_devdata bcd;
438 	struct disk_devdesc *dev;
439 
440 	dev = (struct disk_devdesc *)devdata;
441 	bcd.dv_strategy = bd_realstrategy;
442 	bcd.dv_devdata = devdata;
443 	bcd.dv_cache = BD(dev).bd_bcache;
444 
445 	return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
446 	    buf, rsize));
447 }
448 
449 static int
450 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
451     char *buf, size_t *rsize)
452 {
453 	struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
454 	uint64_t disk_blocks, offset;
455 	size_t blks, blkoff, bsize, rest;
456 	caddr_t bbuf;
457 	int rc;
458 
459 	/*
460 	 * First make sure the IO size is a multiple of 512 bytes. While we do
461 	 * process partial reads below, the strategy mechanism is built
462 	 * assuming IO is a multiple of 512B blocks. If the request is not
463 	 * a multiple of 512B blocks, it has to be some sort of bug.
464 	 */
465 	if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
466 		printf("bd_strategy: %d bytes I/O not multiple of %d\n",
467 		    size, BIOSDISK_SECSIZE);
468 		return (EIO);
469 	}
470 
471 	DEBUG("open_disk %p", dev);
472 
473 	offset = dblk * BIOSDISK_SECSIZE;
474 	dblk = offset / BD(dev).bd_sectorsize;
475 	blkoff = offset % BD(dev).bd_sectorsize;
476 
477 	/*
478 	 * Check the value of the size argument. We do have quite small
479 	 * heap (64MB), but we do not know good upper limit, so we check against
480 	 * INT_MAX here. This will also protect us against possible overflows
481 	 * while translating block count to bytes.
482 	 */
483 	if (size > INT_MAX) {
484 		DEBUG("requested read: %zu too large", size);
485 		return (EIO);
486 	}
487 
488 	blks = size / BD(dev).bd_sectorsize;
489 	if (blks == 0 || (size % BD(dev).bd_sectorsize) != 0)
490 		blks++;
491 
492 	if (dblk > dblk + blks)
493 		return (EIO);
494 
495 	if (rsize)
496 		*rsize = 0;
497 
498 	/*
499 	 * Get disk blocks, this value is either for whole disk or for
500 	 * partition.
501 	 */
502 	if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
503 		/* DIOCGMEDIASIZE does return bytes. */
504 		disk_blocks /= BD(dev).bd_sectorsize;
505 	} else {
506 		/* We should not get here. Just try to survive. */
507 		disk_blocks = BD(dev).bd_sectors - dev->d_offset;
508 	}
509 
510 	/* Validate source block address. */
511 	if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
512 		return (EIO);
513 
514 	/*
515 	 * Truncate if we are crossing disk or partition end.
516 	 */
517 	if (dblk + blks >= dev->d_offset + disk_blocks) {
518 		blks = dev->d_offset + disk_blocks - dblk;
519 		size = blks * BD(dev).bd_sectorsize;
520 		DEBUG("short read %d", blks);
521 	}
522 
523 	if (V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize == 0)
524 		panic("BUG: Real mode buffer is too small\n");
525 
526 	bbuf = PTOV(V86_IO_BUFFER);
527 	rest = size;
528 
529 	while (blks > 0) {
530 		int x = min(blks, V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize);
531 
532 		switch (rw & F_MASK) {
533 		case F_READ:
534 			DEBUG("read %d from %lld to %p", x, dblk, buf);
535 			bsize = BD(dev).bd_sectorsize * x - blkoff;
536 			if (rest < bsize)
537 				bsize = rest;
538 
539 			if ((rc = bd_io(dev, dblk, x, bbuf, 0)) != 0)
540 				return (EIO);
541 
542 			bcopy(bbuf + blkoff, buf, bsize);
543 			break;
544 		case F_WRITE :
545 			DEBUG("write %d from %lld to %p", x, dblk, buf);
546 			if (blkoff != 0) {
547 				/*
548 				 * We got offset to sector, read 1 sector to
549 				 * bbuf.
550 				 */
551 				x = 1;
552 				bsize = BD(dev).bd_sectorsize - blkoff;
553 				bsize = min(bsize, rest);
554 				rc = bd_io(dev, dblk, x, bbuf, 0);
555 			} else if (rest < BD(dev).bd_sectorsize) {
556 				/*
557 				 * The remaining block is not full
558 				 * sector. Read 1 sector to bbuf.
559 				 */
560 				x = 1;
561 				bsize = rest;
562 				rc = bd_io(dev, dblk, x, bbuf, 0);
563 			} else {
564 				/* We can write full sector(s). */
565 				bsize = BD(dev).bd_sectorsize * x;
566 			}
567 			/*
568 			 * Put your Data In, Put your Data out,
569 			 * Put your Data In, and shake it all about
570 			 */
571 			bcopy(buf, bbuf + blkoff, bsize);
572 			if ((rc = bd_io(dev, dblk, x, bbuf, 1)) != 0)
573 				return (EIO);
574 
575 			break;
576 		default:
577 			/* DO NOTHING */
578 			return (EROFS);
579 		}
580 
581 		blkoff = 0;
582 		buf += bsize;
583 		rest -= bsize;
584 		blks -= x;
585 		dblk += x;
586 	}
587 
588 	if (rsize != NULL)
589 		*rsize = size;
590 	return (0);
591 }
592 
593 static int
594 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
595     int dowrite)
596 {
597     static struct edd_packet packet;
598 
599     packet.len = sizeof(struct edd_packet);
600     packet.count = blks;
601     packet.off = VTOPOFF(dest);
602     packet.seg = VTOPSEG(dest);
603     packet.lba = dblk;
604     v86.ctl = V86_FLAGS;
605     v86.addr = 0x13;
606     if (dowrite)
607 	/* Should we Write with verify ?? 0x4302 ? */
608 	v86.eax = 0x4300;
609     else
610 	v86.eax = 0x4200;
611     v86.edx = BD(dev).bd_unit;
612     v86.ds = VTOPSEG(&packet);
613     v86.esi = VTOPOFF(&packet);
614     v86int();
615     if (V86_CY(v86.efl))
616 	return (v86.eax >> 8);
617     return (0);
618 }
619 
620 static int
621 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
622     int dowrite)
623 {
624     u_int	x, bpc, cyl, hd, sec;
625 
626     bpc = BD(dev).bd_sec * BD(dev).bd_hds;	/* blocks per cylinder */
627     x = dblk;
628     cyl = x / bpc;			/* block # / blocks per cylinder */
629     x %= bpc;				/* block offset into cylinder */
630     hd = x / BD(dev).bd_sec;		/* offset / blocks per track */
631     sec = x % BD(dev).bd_sec;		/* offset into track */
632 
633     /* correct sector number for 1-based BIOS numbering */
634     sec++;
635 
636     if (cyl > 1023)
637 	/* CHS doesn't support cylinders > 1023. */
638 	return (1);
639 
640     v86.ctl = V86_FLAGS;
641     v86.addr = 0x13;
642     if (dowrite)
643 	v86.eax = 0x300 | blks;
644     else
645 	v86.eax = 0x200 | blks;
646     v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
647     v86.edx = (hd << 8) | BD(dev).bd_unit;
648     v86.es = VTOPSEG(dest);
649     v86.ebx = VTOPOFF(dest);
650     v86int();
651     if (V86_CY(v86.efl))
652 	return (v86.eax >> 8);
653     return (0);
654 }
655 
656 static int
657 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
658     int dowrite)
659 {
660 	u_int result, retry;
661 
662 	/* Just in case some idiot actually tries to read/write -1 blocks... */
663 	if (blks < 0)
664 		return (-1);
665 
666 	/*
667 	 * Loop retrying the operation a couple of times.  The BIOS
668 	 * may also retry.
669 	 */
670 	for (retry = 0; retry < 3; retry++) {
671 		/* if retrying, reset the drive */
672 		if (retry > 0) {
673 			v86.ctl = V86_FLAGS;
674 			v86.addr = 0x13;
675 			v86.eax = 0;
676 			v86.edx = BD(dev).bd_unit;
677 			v86int();
678 		}
679 
680 		if (BD(dev).bd_flags & BD_MODEEDD1)
681 			result = bd_edd_io(dev, dblk, blks, dest, dowrite);
682 		else
683 			result = bd_chs_io(dev, dblk, blks, dest, dowrite);
684 
685 		if (result == 0)
686 			break;
687 	}
688 
689 	/*
690 	 * 0x20 - Controller failure. This is common error when the
691 	 * media is not present.
692 	 */
693 	if (result != 0 && result != 0x20) {
694 		if (dowrite != 0) {
695 			printf("%s%d: Write %d sector(s) from %p (0x%x) "
696 			    "to %lld: 0x%x", dev->d_dev->dv_name, dev->d_unit,
697 			    blks, dest, VTOP(dest), dblk, result);
698 		} else {
699 			printf("%s%d: Read %d sector(s) from %lld to %p "
700 			    "(0x%x): 0x%x", dev->d_dev->dv_name, dev->d_unit,
701 			    blks, dblk, dest, VTOP(dest), result);
702 	}
703 
704 	if (result != 0)
705 	    return (result);
706     }
707 
708     return(0);
709 }
710 
711 /*
712  * Return the BIOS geometry of a given "fixed drive" in a format
713  * suitable for the legacy bootinfo structure.  Since the kernel is
714  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
715  * prefer to get the information directly, rather than rely on being
716  * able to put it together from information already maintained for
717  * different purposes and for a probably different number of drives.
718  *
719  * For valid drives, the geometry is expected in the format (31..0)
720  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
721  * indicated by returning the geometry of a "1.2M" PC-format floppy
722  * disk.  And, incidentally, what is returned is not the geometry as
723  * such but the highest valid cylinder, head, and sector numbers.
724  */
725 u_int32_t
726 bd_getbigeom(int bunit)
727 {
728 
729     v86.ctl = V86_FLAGS;
730     v86.addr = 0x13;
731     v86.eax = 0x800;
732     v86.edx = 0x80 + bunit;
733     v86int();
734     if (V86_CY(v86.efl))
735 	return 0x4f010f;
736     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
737 	   (v86.edx & 0xff00) | (v86.ecx & 0x3f);
738 }
739 
740 /*
741  * Return a suitable dev_t value for (dev).
742  *
743  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
744  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
745  */
746 int
747 bd_getdev(struct i386_devdesc *d)
748 {
749     struct disk_devdesc		*dev;
750     int				biosdev;
751     int 			major;
752     int				rootdev;
753     char			*nip, *cp;
754     int				i, unit;
755 
756     dev = (struct disk_devdesc *)d;
757     biosdev = bd_unit2bios(dev->d_unit);
758     DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
759     if (biosdev == -1)				/* not a BIOS device */
760 	return(-1);
761     if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
762 	BD(dev).bd_sectorsize) != 0)		/* oops, not a viable device */
763 	    return (-1);
764     else
765 	disk_close(dev);
766 
767     if (biosdev < 0x80) {
768 	/* floppy (or emulated floppy) or ATAPI device */
769 	if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
770 	    /* is an ATAPI disk */
771 	    major = WFDMAJOR;
772 	} else {
773 	    /* is a floppy disk */
774 	    major = FDMAJOR;
775 	}
776     } else {
777 	    /* assume an IDE disk */
778 	    major = WDMAJOR;
779     }
780     /* default root disk unit number */
781     unit = biosdev & 0x7f;
782 
783     /* XXX a better kludge to set the root disk unit number */
784     if ((nip = getenv("root_disk_unit")) != NULL) {
785 	i = strtol(nip, &cp, 0);
786 	/* check for parse error */
787 	if ((cp != nip) && (*cp == 0))
788 	    unit = i;
789     }
790 
791     rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
792     DEBUG("dev is 0x%x\n", rootdev);
793     return(rootdev);
794 }
795