xref: /freebsd/stand/i386/libi386/biosdisk.c (revision 19261079)
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 <sys/queue.h>
44 #include <stand.h>
45 #include <machine/bootinfo.h>
46 #include <stdarg.h>
47 #include <stdbool.h>
48 
49 #include <bootstrap.h>
50 #include <btxv86.h>
51 #include <edd.h>
52 #include "disk.h"
53 #include "libi386.h"
54 
55 #define	BIOS_NUMDRIVES		0x475
56 #define	BIOSDISK_SECSIZE	512
57 #define	BUFSIZE			(1 * BIOSDISK_SECSIZE)
58 
59 #define	DT_ATAPI	0x10	/* disk type for ATAPI floppies */
60 #define	WDMAJOR		0	/* major numbers for devices we frontend for */
61 #define	WFDMAJOR	1
62 #define	FDMAJOR		2
63 #define	DAMAJOR		4
64 #define	ACDMAJOR	117
65 #define	CDMAJOR		15
66 
67 /*
68  * INT13 commands
69  */
70 #define	CMD_RESET	0x0000
71 #define	CMD_READ_CHS	0x0200
72 #define	CMD_WRITE_CHS	0x0300
73 #define	CMD_READ_PARAM	0x0800
74 #define	CMD_DRIVE_TYPE	0x1500
75 #define	CMD_CHECK_EDD	0x4100
76 #define	CMD_READ_LBA	0x4200
77 #define	CMD_WRITE_LBA	0x4300
78 #define	CMD_EXT_PARAM	0x4800
79 #define	CMD_CD_GET_STATUS 0x4b01
80 
81 #define	DISK_BIOS	0x13
82 
83 #ifdef DISK_DEBUG
84 #define	DPRINTF(fmt, args...)	printf("%s: " fmt "\n", __func__, ## args)
85 #else
86 #define	DPRINTF(fmt, args...)	((void)0)
87 #endif
88 
89 struct specification_packet {
90 	uint8_t		sp_size;
91 	uint8_t		sp_bootmedia;
92 	uint8_t		sp_drive;
93 	uint8_t		sp_controller;
94 	uint32_t	sp_lba;
95 	uint16_t	sp_devicespec;
96 	uint16_t	sp_buffersegment;
97 	uint16_t	sp_loadsegment;
98 	uint16_t	sp_sectorcount;
99 	uint16_t	sp_cylsec;
100 	uint8_t		sp_head;
101 	uint8_t		sp_dummy[16];	/* Avoid memory corruption */
102 };
103 
104 /*
105  * List of BIOS devices, translation from disk unit number to
106  * BIOS unit number.
107  */
108 typedef struct bdinfo
109 {
110 	STAILQ_ENTRY(bdinfo)	bd_link;	/* link in device list */
111 	int		bd_unit;	/* BIOS unit number */
112 	int		bd_cyl;		/* BIOS geometry */
113 	int		bd_hds;
114 	int		bd_sec;
115 	int		bd_flags;
116 #define	BD_MODEINT13	0x0000
117 #define	BD_MODEEDD1	0x0001
118 #define	BD_MODEEDD3	0x0002
119 #define	BD_MODEEDD	(BD_MODEEDD1 | BD_MODEEDD3)
120 #define	BD_MODEMASK	0x0003
121 #define	BD_FLOPPY	0x0004
122 #define	BD_CDROM	0x0008
123 #define	BD_NO_MEDIA	0x0010
124 	int		bd_type;	/* BIOS 'drive type' (floppy only) */
125 	uint16_t	bd_sectorsize;	/* Sector size */
126 	uint64_t	bd_sectors;	/* Disk size */
127 	int		bd_open;	/* reference counter */
128 	void		*bd_bcache;	/* buffer cache data */
129 } bdinfo_t;
130 
131 #define	BD_RD		0
132 #define	BD_WR		1
133 
134 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t;
135 static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
136 static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
137 static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
138 
139 static void bd_io_workaround(bdinfo_t *);
140 static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int);
141 static bool bd_int13probe(bdinfo_t *);
142 
143 static int bd_init(void);
144 static int cd_init(void);
145 static int fd_init(void);
146 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
147     char *buf, size_t *rsize);
148 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
149     char *buf, size_t *rsize);
150 static int bd_open(struct open_file *f, ...);
151 static int bd_close(struct open_file *f);
152 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
153 static int bd_print(int verbose);
154 static int cd_print(int verbose);
155 static int fd_print(int verbose);
156 static void bd_reset_disk(int);
157 static int bd_get_diskinfo_std(struct bdinfo *);
158 
159 struct devsw biosfd = {
160 	.dv_name = "fd",
161 	.dv_type = DEVT_FD,
162 	.dv_init = fd_init,
163 	.dv_strategy = bd_strategy,
164 	.dv_open = bd_open,
165 	.dv_close = bd_close,
166 	.dv_ioctl = bd_ioctl,
167 	.dv_print = fd_print,
168 	.dv_cleanup = NULL
169 };
170 
171 struct devsw bioscd = {
172 	.dv_name = "cd",
173 	.dv_type = DEVT_CD,
174 	.dv_init = cd_init,
175 	.dv_strategy = bd_strategy,
176 	.dv_open = bd_open,
177 	.dv_close = bd_close,
178 	.dv_ioctl = bd_ioctl,
179 	.dv_print = cd_print,
180 	.dv_cleanup = NULL
181 };
182 
183 struct devsw bioshd = {
184 	.dv_name = "disk",
185 	.dv_type = DEVT_DISK,
186 	.dv_init = bd_init,
187 	.dv_strategy = bd_strategy,
188 	.dv_open = bd_open,
189 	.dv_close = bd_close,
190 	.dv_ioctl = bd_ioctl,
191 	.dv_print = bd_print,
192 	.dv_cleanup = NULL
193 };
194 
195 static bdinfo_list_t *
196 bd_get_bdinfo_list(struct devsw *dev)
197 {
198 	if (dev->dv_type == DEVT_DISK)
199 		return (&hdinfo);
200 	if (dev->dv_type == DEVT_CD)
201 		return (&cdinfo);
202 	if (dev->dv_type == DEVT_FD)
203 		return (&fdinfo);
204 	return (NULL);
205 }
206 
207 /* XXX this gets called way way too often, investigate */
208 static bdinfo_t *
209 bd_get_bdinfo(struct devdesc *dev)
210 {
211 	bdinfo_list_t *bdi;
212 	bdinfo_t *bd = NULL;
213 	int unit;
214 
215 	bdi = bd_get_bdinfo_list(dev->d_dev);
216 	if (bdi == NULL)
217 		return (bd);
218 
219 	unit = 0;
220 	STAILQ_FOREACH(bd, bdi, bd_link) {
221 		if (unit == dev->d_unit)
222 			return (bd);
223 		unit++;
224 	}
225 	return (bd);
226 }
227 
228 /*
229  * Translate between BIOS device numbers and our private unit numbers.
230  */
231 int
232 bd_bios2unit(int biosdev)
233 {
234 	bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL };
235 	bdinfo_t *bd;
236 	int i, unit;
237 
238 	DPRINTF("looking for bios device 0x%x", biosdev);
239 	for (i = 0; bdi[i] != NULL; i++) {
240 		unit = 0;
241 		STAILQ_FOREACH(bd, bdi[i], bd_link) {
242 			if (bd->bd_unit == biosdev) {
243 				DPRINTF("bd unit %d is BIOS device 0x%x", unit,
244 				    bd->bd_unit);
245 				return (unit);
246 			}
247 			unit++;
248 		}
249 	}
250 	return (-1);
251 }
252 
253 int
254 bd_unit2bios(struct i386_devdesc *dev)
255 {
256 	bdinfo_list_t *bdi;
257 	bdinfo_t *bd;
258 	int unit;
259 
260 	bdi = bd_get_bdinfo_list(dev->dd.d_dev);
261 	if (bdi == NULL)
262 		return (-1);
263 
264 	unit = 0;
265 	STAILQ_FOREACH(bd, bdi, bd_link) {
266 		if (unit == dev->dd.d_unit)
267 			return (bd->bd_unit);
268 		unit++;
269 	}
270 	return (-1);
271 }
272 
273 /*
274  * Use INT13 AH=15 - Read Drive Type.
275  */
276 static int
277 fd_count(void)
278 {
279 	int drive;
280 
281 	for (drive = 0; drive < MAXBDDEV; drive++) {
282 		bd_reset_disk(drive);
283 
284 		v86.ctl = V86_FLAGS;
285 		v86.addr = DISK_BIOS;
286 		v86.eax = CMD_DRIVE_TYPE;
287 		v86.edx = drive;
288 		v86int();
289 
290 		if (V86_CY(v86.efl))
291 			break;
292 
293 		if ((v86.eax & 0x300) == 0)
294 			break;
295 	}
296 
297 	return (drive);
298 }
299 
300 /*
301  * Quiz the BIOS for disk devices, save a little info about them.
302  */
303 static int
304 fd_init(void)
305 {
306 	int unit, numfd;
307 	bdinfo_t *bd;
308 
309 	numfd = fd_count();
310 	for (unit = 0; unit < numfd; unit++) {
311 		if ((bd = calloc(1, sizeof(*bd))) == NULL)
312 			break;
313 
314 		bd->bd_sectorsize = BIOSDISK_SECSIZE;
315 		bd->bd_flags = BD_FLOPPY;
316 		bd->bd_unit = unit;
317 
318 		/* Use std diskinfo for floppy drive */
319 		if (bd_get_diskinfo_std(bd) != 0) {
320 			free(bd);
321 			break;
322 		}
323 		if (bd->bd_sectors == 0)
324 			bd->bd_flags |= BD_NO_MEDIA;
325 
326 		printf("BIOS drive %c: is %s%d\n", ('A' + unit),
327 		    biosfd.dv_name, unit);
328 
329 		STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link);
330 	}
331 
332 	bcache_add_dev(unit);
333 	return (0);
334 }
335 
336 static int
337 bd_init(void)
338 {
339 	int base, unit;
340 	bdinfo_t *bd;
341 
342 	TSENTER();
343 
344 	base = 0x80;
345 	for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
346 		/*
347 		 * Check the BIOS equipment list for number of fixed disks.
348 		 */
349 		if ((bd = calloc(1, sizeof(*bd))) == NULL)
350 			break;
351 		bd->bd_unit = base + unit;
352 		if (!bd_int13probe(bd)) {
353 			free(bd);
354 			break;
355 		}
356 
357 		printf("BIOS drive %c: is %s%d\n", ('C' + unit),
358 		    bioshd.dv_name, unit);
359 
360 		STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
361 	}
362 	bcache_add_dev(unit);
363 	TSEXIT();
364 	return (0);
365 }
366 
367 /*
368  * We can't quiz, we have to be told what device to use, so this function
369  * doesn't do anything.  Instead, the loader calls bc_add() with the BIOS
370  * device number to add.
371  */
372 static int
373 cd_init(void)
374 {
375 
376 	return (0);
377 }
378 
379 /*
380  * Information from bootable CD-ROM.
381  */
382 static int
383 bd_get_diskinfo_cd(struct bdinfo *bd)
384 {
385 	struct specification_packet bc_sp;
386 	int ret = -1;
387 
388 	(void) memset(&bc_sp, 0, sizeof (bc_sp));
389 	/* Set sp_size as per specification. */
390 	bc_sp.sp_size = sizeof (bc_sp) - sizeof (bc_sp.sp_dummy);
391 
392         v86.ctl = V86_FLAGS;
393         v86.addr = DISK_BIOS;
394         v86.eax = CMD_CD_GET_STATUS;
395         v86.edx = bd->bd_unit;
396         v86.ds = VTOPSEG(&bc_sp);
397         v86.esi = VTOPOFF(&bc_sp);
398         v86int();
399 
400 	if ((v86.eax & 0xff00) == 0 &&
401 	    bc_sp.sp_drive == bd->bd_unit) {
402 		bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
403 		    ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
404 		bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
405 		bd->bd_hds = bc_sp.sp_head + 1;
406 		bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
407 
408 		if (bc_sp.sp_bootmedia & 0x0F) {
409 			/* Floppy or hard-disk emulation */
410 			bd->bd_sectorsize = BIOSDISK_SECSIZE;
411 			return (-1);
412 		} else {
413 			bd->bd_sectorsize = 2048;
414 			bd->bd_flags = BD_MODEEDD | BD_CDROM;
415 			ret = 0;
416 		}
417 	}
418 
419 	/*
420 	 * If this is the boot_drive, default to non-emulation bootable CD-ROM.
421 	 */
422 	if (ret != 0 && bd->bd_unit >= 0x88) {
423 		bd->bd_cyl = 0;
424 		bd->bd_hds = 1;
425 		bd->bd_sec = 15;
426 		bd->bd_sectorsize = 2048;
427 		bd->bd_flags = BD_MODEEDD | BD_CDROM;
428 		bd->bd_sectors = 0;
429 		ret = 0;
430 	}
431 
432 	/*
433 	 * Note we can not use bd_get_diskinfo_ext() nor bd_get_diskinfo_std()
434 	 * here - some systems do get hung with those.
435 	 */
436 	/*
437 	 * Still no size? use 7.961GB. The size does not really matter
438 	 * as long as it is reasonably large to make our reads to pass
439 	 * the sector count check.
440 	 */
441 	if (bd->bd_sectors == 0)
442 		bd->bd_sectors = 4173824;
443 
444 	return (ret);
445 }
446 
447 int
448 bc_add(int biosdev)
449 {
450 	bdinfo_t *bd;
451 	int nbcinfo = 0;
452 
453 	if (!STAILQ_EMPTY(&cdinfo))
454                 return (-1);
455 
456 	if ((bd = calloc(1, sizeof(*bd))) == NULL)
457 		return (-1);
458 
459 	bd->bd_unit = biosdev;
460 	if (bd_get_diskinfo_cd(bd) < 0) {
461 		free(bd);
462 		return (-1);
463 	}
464 
465 	STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
466         printf("BIOS CD is cd%d\n", nbcinfo);
467         nbcinfo++;
468         bcache_add_dev(nbcinfo);        /* register cd device in bcache */
469         return(0);
470 }
471 
472 /*
473  * Return EDD version or 0 if EDD is not supported on this drive.
474  */
475 static int
476 bd_check_extensions(int unit)
477 {
478 	/* do not use ext calls for floppy devices */
479 	if (unit < 0x80)
480 		return (0);
481 
482 	/* Determine if we can use EDD with this device. */
483 	v86.ctl = V86_FLAGS;
484 	v86.addr = DISK_BIOS;
485 	v86.eax = CMD_CHECK_EDD;
486 	v86.edx = unit;
487 	v86.ebx = EDD_QUERY_MAGIC;
488 	v86int();
489 
490 	if (V86_CY(v86.efl) ||			/* carry set */
491 	    (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */
492 		return (0);
493 
494 	/* extended disk access functions (AH=42h-44h,47h,48h) supported */
495 	if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
496 		return (0);
497 
498 	return ((v86.eax >> 8) & 0xff);
499 }
500 
501 static void
502 bd_reset_disk(int unit)
503 {
504 	/* reset disk */
505 	v86.ctl = V86_FLAGS;
506 	v86.addr = DISK_BIOS;
507 	v86.eax = CMD_RESET;
508 	v86.edx = unit;
509 	v86int();
510 }
511 
512 /*
513  * Read CHS info. Return 0 on success, error otherwise.
514  */
515 static int
516 bd_get_diskinfo_std(struct bdinfo *bd)
517 {
518 	bzero(&v86, sizeof(v86));
519 	v86.ctl = V86_FLAGS;
520 	v86.addr = DISK_BIOS;
521 	v86.eax = CMD_READ_PARAM;
522 	v86.edx = bd->bd_unit;
523 	v86int();
524 
525 	if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
526 		return ((v86.eax & 0xff00) >> 8);
527 
528 	/* return custom error on absurd sector number */
529 	if ((v86.ecx & 0x3f) == 0)
530 		return (0x60);
531 
532 	bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
533 	/* Convert max head # -> # of heads */
534 	bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
535 	bd->bd_sec = v86.ecx & 0x3f;
536 	bd->bd_type = v86.ebx;
537 	bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
538 
539 	return (0);
540 }
541 
542 /*
543  * Read EDD info. Return 0 on success, error otherwise.
544  *
545  * Avoid stack corruption on some systems by adding extra bytes to
546  * params block.
547  */
548 static int
549 bd_get_diskinfo_ext(struct bdinfo *bd)
550 {
551 	struct disk_params {
552 		struct edd_params head;
553 		struct edd_device_path_v3 device_path;
554 		uint8_t dummy[16];
555 	} __packed dparams;
556 	struct edd_params *params;
557 	uint64_t total;
558 
559 	params = &dparams.head;
560 
561 	/* Get disk params */
562 	bzero(&dparams, sizeof(dparams));
563 	params->len = sizeof(struct edd_params_v3);
564 	v86.ctl = V86_FLAGS;
565 	v86.addr = DISK_BIOS;
566 	v86.eax = CMD_EXT_PARAM;
567 	v86.edx = bd->bd_unit;
568 	v86.ds = VTOPSEG(&dparams);
569 	v86.esi = VTOPOFF(&dparams);
570 	v86int();
571 
572 	if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
573 		return ((v86.eax & 0xff00) >> 8);
574 
575 	/*
576 	 * Sector size must be a multiple of 512 bytes.
577 	 * An alternate test would be to check power of 2,
578 	 * powerof2(params.sector_size).
579 	 * 16K is largest read buffer we can use at this time.
580 	 */
581 	if (params->sector_size >= 512 &&
582 	    params->sector_size <= 16384 &&
583 	    (params->sector_size % BIOSDISK_SECSIZE) == 0)
584 		bd->bd_sectorsize = params->sector_size;
585 
586 	bd->bd_cyl = params->cylinders;
587 	bd->bd_hds = params->heads;
588 	bd->bd_sec = params->sectors_per_track;
589 
590 	if (params->sectors != 0) {
591 		total = params->sectors;
592 	} else {
593 		total = (uint64_t)params->cylinders *
594 		    params->heads * params->sectors_per_track;
595 	}
596 	bd->bd_sectors = total;
597 
598 	return (0);
599 }
600 
601 /*
602  * Try to detect a device supported by the legacy int13 BIOS
603  */
604 static bool
605 bd_int13probe(bdinfo_t *bd)
606 {
607 	int edd, ret;
608 
609 	bd->bd_flags &= ~BD_NO_MEDIA;
610 
611 	if ((bd->bd_flags & BD_CDROM) != 0) {
612 		return (bd_get_diskinfo_cd(bd) == 0);
613 	}
614 
615 	edd = bd_check_extensions(bd->bd_unit);
616 	if (edd == 0)
617 		bd->bd_flags |= BD_MODEINT13;
618 	else if (edd < 0x30)
619 		bd->bd_flags |= BD_MODEEDD1;
620 	else
621 		bd->bd_flags |= BD_MODEEDD3;
622 
623 	/* Default sector size */
624 	if (bd->bd_sectorsize == 0)
625 		bd->bd_sectorsize = BIOSDISK_SECSIZE;
626 
627 	/*
628 	 * Test if the floppy device is present, so we can avoid receiving
629 	 * bogus information from bd_get_diskinfo_std().
630 	 */
631 	if (bd->bd_unit < 0x80) {
632 		/* reset disk */
633 		bd_reset_disk(bd->bd_unit);
634 
635 		/* Get disk type */
636 		v86.ctl = V86_FLAGS;
637 		v86.addr = DISK_BIOS;
638 		v86.eax = CMD_DRIVE_TYPE;
639 		v86.edx = bd->bd_unit;
640 		v86int();
641 		if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
642 			return (false);
643 	}
644 
645 	ret = 1;
646 	if (edd != 0)
647 		ret = bd_get_diskinfo_ext(bd);
648 	if (ret != 0 || bd->bd_sectors == 0)
649 		ret = bd_get_diskinfo_std(bd);
650 
651 	if (ret != 0 && bd->bd_unit < 0x80) {
652 		/* Set defaults for 1.44 floppy */
653 		bd->bd_cyl = 80;
654 		bd->bd_hds = 2;
655 		bd->bd_sec = 18;
656 		bd->bd_sectors = 2880;
657 		/* Since we are there, there most likely is no media */
658 		bd->bd_flags |= BD_NO_MEDIA;
659 		ret = 0;
660 	}
661 
662 	if (ret != 0) {
663 		if (bd->bd_sectors != 0 && edd != 0) {
664 			bd->bd_sec = 63;
665 			bd->bd_hds = 255;
666 			bd->bd_cyl =
667 			    (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
668 			    bd->bd_sec * bd->bd_hds;
669 		} else {
670 			const char *dv_name;
671 
672 			if ((bd->bd_flags & BD_FLOPPY) != 0)
673 				dv_name = biosfd.dv_name;
674 			else
675 				dv_name = bioshd.dv_name;
676 
677 			printf("Can not get information about %s unit %#x\n",
678 			    dv_name, bd->bd_unit);
679 			return (false);
680 		}
681 	}
682 
683 	if (bd->bd_sec == 0)
684 		bd->bd_sec = 63;
685 	if (bd->bd_hds == 0)
686 		bd->bd_hds = 255;
687 
688 	if (bd->bd_sectors == 0)
689 		bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
690 
691 	DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
692 	    bd->bd_hds, bd->bd_sec);
693 
694 	return (true);
695 }
696 
697 static int
698 bd_count(bdinfo_list_t *bdi)
699 {
700 	bdinfo_t *bd;
701 	int i;
702 
703 	i = 0;
704 	STAILQ_FOREACH(bd, bdi, bd_link)
705 		i++;
706 	return (i);
707 }
708 
709 /*
710  * Print information about disks
711  */
712 static int
713 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
714 {
715 	char line[80];
716 	struct disk_devdesc devd;
717 	bdinfo_t *bd;
718 	int i, ret = 0;
719 	char drive;
720 
721 	if (STAILQ_EMPTY(bdi))
722 		return (0);
723 
724 	printf("%s devices:", dev->dv_name);
725 	if ((ret = pager_output("\n")) != 0)
726 		return (ret);
727 
728 	i = -1;
729 	STAILQ_FOREACH(bd, bdi, bd_link) {
730 		i++;
731 
732 		switch (dev->dv_type) {
733 		case DEVT_FD:
734 			drive = 'A';
735 			break;
736 		case DEVT_CD:
737 			drive = 'C' + bd_count(&hdinfo);
738 			break;
739 		default:
740 			drive = 'C';
741 			break;
742 		}
743 
744 		snprintf(line, sizeof(line),
745 		    "    %s%d:   BIOS drive %c (%s%ju X %u):\n",
746 		    dev->dv_name, i, drive + i,
747 		    (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
748 		    "no media, " : "",
749 		    (uintmax_t)bd->bd_sectors,
750 		    bd->bd_sectorsize);
751 		if ((ret = pager_output(line)) != 0)
752 			break;
753 
754 		if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
755 			continue;
756 
757 		if (dev->dv_type != DEVT_DISK)
758 			continue;
759 
760 		devd.dd.d_dev = dev;
761 		devd.dd.d_unit = i;
762 		devd.d_slice = D_SLICENONE;
763 		devd.d_partition = D_PARTNONE;
764 		if (disk_open(&devd,
765 		    bd->bd_sectorsize * bd->bd_sectors,
766 		    bd->bd_sectorsize) == 0) {
767 			snprintf(line, sizeof(line), "    %s%d",
768 			    dev->dv_name, i);
769 			ret = disk_print(&devd, line, verbose);
770 			disk_close(&devd);
771 			if (ret != 0)
772 				break;
773 		}
774 	}
775 	return (ret);
776 }
777 
778 static int
779 fd_print(int verbose)
780 {
781 	return (bd_print_common(&biosfd, &fdinfo, verbose));
782 }
783 
784 static int
785 bd_print(int verbose)
786 {
787 	return (bd_print_common(&bioshd, &hdinfo, verbose));
788 }
789 
790 static int
791 cd_print(int verbose)
792 {
793 	return (bd_print_common(&bioscd, &cdinfo, verbose));
794 }
795 
796 /*
797  * Read disk size from partition.
798  * This is needed to work around buggy BIOS systems returning
799  * wrong (truncated) disk media size.
800  * During bd_probe() we tested if the multiplication of bd_sectors
801  * would overflow so it should be safe to perform here.
802  */
803 static uint64_t
804 bd_disk_get_sectors(struct disk_devdesc *dev)
805 {
806 	bdinfo_t *bd;
807 	struct disk_devdesc disk;
808 	uint64_t size;
809 
810 	bd = bd_get_bdinfo(&dev->dd);
811 	if (bd == NULL)
812 		return (0);
813 
814 	disk.dd.d_dev = dev->dd.d_dev;
815 	disk.dd.d_unit = dev->dd.d_unit;
816 	disk.d_slice = D_SLICENONE;
817 	disk.d_partition = D_PARTNONE;
818 	disk.d_offset = 0;
819 
820 	size = bd->bd_sectors * bd->bd_sectorsize;
821 	if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
822 		(void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
823 		disk_close(&disk);
824 	}
825 	return (size / bd->bd_sectorsize);
826 }
827 
828 /*
829  * Attempt to open the disk described by (dev) for use by (f).
830  *
831  * Note that the philosophy here is "give them exactly what
832  * they ask for".  This is necessary because being too "smart"
833  * about what the user might want leads to complications.
834  * (eg. given no slice or partition value, with a disk that is
835  *  sliced - are they after the first BSD slice, or the DOS
836  *  slice before it?)
837  */
838 static int
839 bd_open(struct open_file *f, ...)
840 {
841 	bdinfo_t *bd;
842 	struct disk_devdesc *dev;
843 	va_list ap;
844 	int rc;
845 
846 	TSENTER();
847 
848 	va_start(ap, f);
849 	dev = va_arg(ap, struct disk_devdesc *);
850 	va_end(ap);
851 
852 	bd = bd_get_bdinfo(&dev->dd);
853 	if (bd == NULL)
854 		return (EIO);
855 
856 	if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
857 		if (!bd_int13probe(bd))
858 			return (EIO);
859 		if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
860 			return (EIO);
861 	}
862 	if (bd->bd_bcache == NULL)
863 	    bd->bd_bcache = bcache_allocate();
864 
865 	if (bd->bd_open == 0)
866 		bd->bd_sectors = bd_disk_get_sectors(dev);
867 	bd->bd_open++;
868 
869 	rc = 0;
870 	if (dev->dd.d_dev->dv_type == DEVT_DISK) {
871 		rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
872 		    bd->bd_sectorsize);
873 		if (rc != 0) {
874 			bd->bd_open--;
875 			if (bd->bd_open == 0) {
876 				bcache_free(bd->bd_bcache);
877 				bd->bd_bcache = NULL;
878 			}
879 		}
880 	}
881 	TSEXIT();
882 	return (rc);
883 }
884 
885 static int
886 bd_close(struct open_file *f)
887 {
888 	struct disk_devdesc *dev;
889 	bdinfo_t *bd;
890 	int rc = 0;
891 
892 	dev = (struct disk_devdesc *)f->f_devdata;
893 	bd = bd_get_bdinfo(&dev->dd);
894 	if (bd == NULL)
895 		return (EIO);
896 
897 	bd->bd_open--;
898 	if (bd->bd_open == 0) {
899 	    bcache_free(bd->bd_bcache);
900 	    bd->bd_bcache = NULL;
901 	}
902 	if (dev->dd.d_dev->dv_type == DEVT_DISK)
903 		rc = disk_close(dev);
904 	return (rc);
905 }
906 
907 static int
908 bd_ioctl(struct open_file *f, u_long cmd, void *data)
909 {
910 	bdinfo_t *bd;
911 	struct disk_devdesc *dev;
912 	int rc;
913 
914 	dev = (struct disk_devdesc *)f->f_devdata;
915 	bd = bd_get_bdinfo(&dev->dd);
916 	if (bd == NULL)
917 		return (EIO);
918 
919 	if (dev->dd.d_dev->dv_type == DEVT_DISK) {
920 		rc = disk_ioctl(dev, cmd, data);
921 		if (rc != ENOTTY)
922 			return (rc);
923 	}
924 
925 	switch (cmd) {
926 	case DIOCGSECTORSIZE:
927 		*(uint32_t *)data = bd->bd_sectorsize;
928 		break;
929 	case DIOCGMEDIASIZE:
930 		*(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
931 		break;
932 	default:
933 		return (ENOTTY);
934 	}
935 	return (0);
936 }
937 
938 static int
939 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
940     char *buf, size_t *rsize)
941 {
942 	bdinfo_t *bd;
943 	struct bcache_devdata bcd;
944 	struct disk_devdesc *dev;
945 	daddr_t offset;
946 
947 	dev = (struct disk_devdesc *)devdata;
948 	bd = bd_get_bdinfo(&dev->dd);
949 	if (bd == NULL)
950 		return (EINVAL);
951 
952 	bcd.dv_strategy = bd_realstrategy;
953 	bcd.dv_devdata = devdata;
954 	bcd.dv_cache = bd->bd_bcache;
955 
956 	offset = 0;
957 	if (dev->dd.d_dev->dv_type == DEVT_DISK) {
958 
959 		offset = dev->d_offset * bd->bd_sectorsize;
960 		offset /= BIOSDISK_SECSIZE;
961 	}
962 	return (bcache_strategy(&bcd, rw, dblk + offset, size,
963 	    buf, rsize));
964 }
965 
966 static int
967 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
968     char *buf, size_t *rsize)
969 {
970 	struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
971 	bdinfo_t *bd;
972 	uint64_t disk_blocks, offset, d_offset;
973 	size_t blks, blkoff, bsize, bio_size, rest;
974 	caddr_t bbuf = NULL;
975 	int rc;
976 
977 	bd = bd_get_bdinfo(&dev->dd);
978 	if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
979 		return (EIO);
980 
981 	/*
982 	 * First make sure the IO size is a multiple of 512 bytes. While we do
983 	 * process partial reads below, the strategy mechanism is built
984 	 * assuming IO is a multiple of 512B blocks. If the request is not
985 	 * a multiple of 512B blocks, it has to be some sort of bug.
986 	 */
987 	if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
988 		printf("bd_strategy: %d bytes I/O not multiple of %d\n",
989 		    size, BIOSDISK_SECSIZE);
990 		return (EIO);
991 	}
992 
993 	DPRINTF("open_disk %p", dev);
994 
995 	offset = dblk * BIOSDISK_SECSIZE;
996 	dblk = offset / bd->bd_sectorsize;
997 	blkoff = offset % bd->bd_sectorsize;
998 
999 	/*
1000 	 * Check the value of the size argument. We do have quite small
1001 	 * heap (64MB), but we do not know good upper limit, so we check against
1002 	 * INT_MAX here. This will also protect us against possible overflows
1003 	 * while translating block count to bytes.
1004 	 */
1005 	if (size > INT_MAX) {
1006 		DPRINTF("too large I/O: %zu bytes", size);
1007 		return (EIO);
1008 	}
1009 
1010 	blks = size / bd->bd_sectorsize;
1011 	if (blks == 0 || (size % bd->bd_sectorsize) != 0)
1012 		blks++;
1013 
1014 	if (dblk > dblk + blks)
1015 		return (EIO);
1016 
1017 	if (rsize)
1018 		*rsize = 0;
1019 
1020 	/*
1021 	 * Get disk blocks, this value is either for whole disk or for
1022 	 * partition.
1023 	 */
1024 	d_offset = 0;
1025 	disk_blocks = 0;
1026 	if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1027 		if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1028 			/* DIOCGMEDIASIZE does return bytes. */
1029 			disk_blocks /= bd->bd_sectorsize;
1030 		}
1031 		d_offset = dev->d_offset;
1032 	}
1033 	if (disk_blocks == 0)
1034 		disk_blocks = bd->bd_sectors - d_offset;
1035 
1036 	/* Validate source block address. */
1037 	if (dblk < d_offset || dblk >= d_offset + disk_blocks)
1038 		return (EIO);
1039 
1040 	/*
1041 	 * Truncate if we are crossing disk or partition end.
1042 	 */
1043 	if (dblk + blks >= d_offset + disk_blocks) {
1044 		blks = d_offset + disk_blocks - dblk;
1045 		size = blks * bd->bd_sectorsize;
1046 		DPRINTF("short I/O %d", blks);
1047 	}
1048 
1049 	bio_size = min(BIO_BUFFER_SIZE, size);
1050 	while (bio_size > bd->bd_sectorsize) {
1051 		bbuf = bio_alloc(bio_size);
1052 		if (bbuf != NULL)
1053 			break;
1054 		bio_size -= bd->bd_sectorsize;
1055 	}
1056 	if (bbuf == NULL) {
1057 		bio_size = V86_IO_BUFFER_SIZE;
1058 		if (bio_size / bd->bd_sectorsize == 0)
1059 			panic("BUG: Real mode buffer is too small");
1060 
1061 		/* Use alternate 4k buffer */
1062 		bbuf = PTOV(V86_IO_BUFFER);
1063 	}
1064 	rest = size;
1065 	rc = 0;
1066 	while (blks > 0) {
1067 		int x = min(blks, bio_size / bd->bd_sectorsize);
1068 
1069 		switch (rw & F_MASK) {
1070 		case F_READ:
1071 			DPRINTF("read %d from %lld to %p", x, dblk, buf);
1072 			bsize = bd->bd_sectorsize * x - blkoff;
1073 			if (rest < bsize)
1074 				bsize = rest;
1075 
1076 			if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) {
1077 				rc = EIO;
1078 				goto error;
1079 			}
1080 
1081 			bcopy(bbuf + blkoff, buf, bsize);
1082 			break;
1083 		case F_WRITE :
1084 			DPRINTF("write %d from %lld to %p", x, dblk, buf);
1085 			if (blkoff != 0) {
1086 				/*
1087 				 * We got offset to sector, read 1 sector to
1088 				 * bbuf.
1089 				 */
1090 				x = 1;
1091 				bsize = bd->bd_sectorsize - blkoff;
1092 				bsize = min(bsize, rest);
1093 				rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1094 			} else if (rest < bd->bd_sectorsize) {
1095 				/*
1096 				 * The remaining block is not full
1097 				 * sector. Read 1 sector to bbuf.
1098 				 */
1099 				x = 1;
1100 				bsize = rest;
1101 				rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1102 			} else {
1103 				/* We can write full sector(s). */
1104 				bsize = bd->bd_sectorsize * x;
1105 			}
1106 			/*
1107 			 * Put your Data In, Put your Data out,
1108 			 * Put your Data In, and shake it all about
1109 			 */
1110 			bcopy(buf, bbuf + blkoff, bsize);
1111 			if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) {
1112 				rc = EIO;
1113 				goto error;
1114 			}
1115 
1116 			break;
1117 		default:
1118 			/* DO NOTHING */
1119 			rc = EROFS;
1120 			goto error;
1121 		}
1122 
1123 		blkoff = 0;
1124 		buf += bsize;
1125 		rest -= bsize;
1126 		blks -= x;
1127 		dblk += x;
1128 	}
1129 
1130 	if (rsize != NULL)
1131 		*rsize = size;
1132 error:
1133 	if (bbuf != PTOV(V86_IO_BUFFER))
1134 		bio_free(bbuf, bio_size);
1135 	return (rc);
1136 }
1137 
1138 static int
1139 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1140     int dowrite)
1141 {
1142 	static struct edd_packet packet;
1143 
1144 	TSENTER();
1145 
1146 	packet.len = sizeof(struct edd_packet);
1147 	packet.count = blks;
1148 	packet.off = VTOPOFF(dest);
1149 	packet.seg = VTOPSEG(dest);
1150 	packet.lba = dblk;
1151 	v86.ctl = V86_FLAGS;
1152 	v86.addr = DISK_BIOS;
1153 	if (dowrite == BD_WR)
1154 		v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */
1155 	else
1156 		v86.eax = CMD_READ_LBA;
1157 	v86.edx = bd->bd_unit;
1158 	v86.ds = VTOPSEG(&packet);
1159 	v86.esi = VTOPOFF(&packet);
1160 	v86int();
1161 	if (V86_CY(v86.efl))
1162 		return (v86.eax >> 8);
1163 
1164 	TSEXIT();
1165 	return (0);
1166 }
1167 
1168 static int
1169 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1170     int dowrite)
1171 {
1172 	uint32_t x, bpc, cyl, hd, sec;
1173 
1174 	TSENTER();
1175 
1176 	bpc = bd->bd_sec * bd->bd_hds;	/* blocks per cylinder */
1177 	x = dblk;
1178 	cyl = x / bpc;			/* block # / blocks per cylinder */
1179 	x %= bpc;				/* block offset into cylinder */
1180 	hd = x / bd->bd_sec;		/* offset / blocks per track */
1181 	sec = x % bd->bd_sec;		/* offset into track */
1182 
1183 	/* correct sector number for 1-based BIOS numbering */
1184 	sec++;
1185 
1186 	if (cyl > 1023) {
1187 		/* CHS doesn't support cylinders > 1023. */
1188 		return (1);
1189 	}
1190 
1191 	v86.ctl = V86_FLAGS;
1192 	v86.addr = DISK_BIOS;
1193 	if (dowrite == BD_WR)
1194 		v86.eax = CMD_WRITE_CHS | blks;
1195 	else
1196 		v86.eax = CMD_READ_CHS | blks;
1197 	v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1198 	v86.edx = (hd << 8) | bd->bd_unit;
1199 	v86.es = VTOPSEG(dest);
1200 	v86.ebx = VTOPOFF(dest);
1201 	v86int();
1202 	if (V86_CY(v86.efl))
1203 		return (v86.eax >> 8);
1204 	TSEXIT();
1205 	return (0);
1206 }
1207 
1208 static void
1209 bd_io_workaround(bdinfo_t *bd)
1210 {
1211 	uint8_t buf[8 * 1024];
1212 
1213 	bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1214 }
1215 
1216 static int
1217 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1218     caddr_t dest, int dowrite)
1219 {
1220 	int result, retry;
1221 
1222 	TSENTER();
1223 
1224 	/* Just in case some idiot actually tries to read/write -1 blocks... */
1225 	if (blks < 0)
1226 		return (-1);
1227 
1228 	/*
1229 	 * Workaround for a problem with some HP ProLiant BIOS failing to work
1230 	 * out the boot disk after installation. hrs and kuriyama discovered
1231 	 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1232 	 * discovered that an int13h call seems to cause a buffer overrun in
1233 	 * the bios. The problem is alleviated by doing an extra read before
1234 	 * the buggy read. It is not immediately known whether other models
1235 	 * are similarly affected.
1236 	 * Loop retrying the operation a couple of times.  The BIOS
1237 	 * may also retry.
1238 	 */
1239 	if (dowrite == BD_RD && dblk >= 0x100000000)
1240 		bd_io_workaround(bd);
1241 	for (retry = 0; retry < 3; retry++) {
1242 		if (bd->bd_flags & BD_MODEEDD)
1243 			result = bd_edd_io(bd, dblk, blks, dest, dowrite);
1244 		else
1245 			result = bd_chs_io(bd, dblk, blks, dest, dowrite);
1246 
1247 		if (result == 0) {
1248 			if (bd->bd_flags & BD_NO_MEDIA)
1249 				bd->bd_flags &= ~BD_NO_MEDIA;
1250 			break;
1251 		}
1252 
1253 		bd_reset_disk(bd->bd_unit);
1254 
1255 		/*
1256 		 * Error codes:
1257 		 * 20h	controller failure
1258 		 * 31h	no media in drive (IBM/MS INT 13 extensions)
1259 		 * 80h	no media in drive, VMWare (Fusion)
1260 		 * There is no reason to repeat the IO with errors above.
1261 		 */
1262 		if (result == 0x20 || result == 0x31 || result == 0x80) {
1263 			bd->bd_flags |= BD_NO_MEDIA;
1264 			break;
1265 		}
1266 	}
1267 
1268 	if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
1269 		if (dowrite == BD_WR) {
1270 			printf("%s%d: Write %d sector(s) from %p (0x%x) "
1271 			    "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1272 			    dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1273 			    result);
1274 		} else {
1275 			printf("%s%d: Read %d sector(s) from %lld to %p "
1276 			    "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
1277 			    dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
1278 			    result);
1279 		}
1280 	}
1281 
1282 	TSEXIT();
1283 
1284 	return (result);
1285 }
1286 
1287 /*
1288  * Return the BIOS geometry of a given "fixed drive" in a format
1289  * suitable for the legacy bootinfo structure.  Since the kernel is
1290  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1291  * prefer to get the information directly, rather than rely on being
1292  * able to put it together from information already maintained for
1293  * different purposes and for a probably different number of drives.
1294  *
1295  * For valid drives, the geometry is expected in the format (31..0)
1296  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1297  * indicated by returning the geometry of a "1.2M" PC-format floppy
1298  * disk.  And, incidentally, what is returned is not the geometry as
1299  * such but the highest valid cylinder, head, and sector numbers.
1300  */
1301 uint32_t
1302 bd_getbigeom(int bunit)
1303 {
1304 
1305 	v86.ctl = V86_FLAGS;
1306 	v86.addr = DISK_BIOS;
1307 	v86.eax = CMD_READ_PARAM;
1308 	v86.edx = 0x80 + bunit;
1309 	v86int();
1310 	if (V86_CY(v86.efl))
1311 		return (0x4f010f);
1312 	return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1313 	    (v86.edx & 0xff00) | (v86.ecx & 0x3f));
1314 }
1315 
1316 /*
1317  * Return a suitable dev_t value for (dev).
1318  *
1319  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1320  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
1321  */
1322 int
1323 bd_getdev(struct i386_devdesc *d)
1324 {
1325 	struct disk_devdesc *dev;
1326 	bdinfo_t *bd;
1327 	int	biosdev;
1328 	int	major;
1329 	int	rootdev;
1330 	char	*nip, *cp;
1331 	int	i, unit, slice, partition;
1332 
1333 	/* XXX: Assume partition 'a'. */
1334 	slice = 0;
1335 	partition = 0;
1336 
1337 	dev = (struct disk_devdesc *)d;
1338 	bd = bd_get_bdinfo(&dev->dd);
1339 	if (bd == NULL)
1340 		return (-1);
1341 
1342 	biosdev = bd_unit2bios(d);
1343 	DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1344 	if (biosdev == -1)			/* not a BIOS device */
1345 		return (-1);
1346 
1347 	if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1348 		if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1349 		    bd->bd_sectorsize) != 0)	/* oops, not a viable device */
1350 			return (-1);
1351 		else
1352 			disk_close(dev);
1353 		slice = dev->d_slice + 1;
1354 		partition = dev->d_partition;
1355 	}
1356 
1357 	if (biosdev < 0x80) {
1358 		/* floppy (or emulated floppy) or ATAPI device */
1359 		if (bd->bd_type == DT_ATAPI) {
1360 			/* is an ATAPI disk */
1361 			major = WFDMAJOR;
1362 		} else {
1363 			/* is a floppy disk */
1364 			major = FDMAJOR;
1365 		}
1366 	} else {
1367 		/* assume an IDE disk */
1368 		major = WDMAJOR;
1369 	}
1370 	/* default root disk unit number */
1371 	unit = biosdev & 0x7f;
1372 
1373 	if (dev->dd.d_dev->dv_type == DEVT_CD) {
1374 		/*
1375 		 * XXX: Need to examine device spec here to figure out if
1376 		 * SCSI or ATAPI.  No idea on how to figure out device number.
1377 		 * All we can really pass to the kernel is what bus and device
1378 		 * on which bus we were booted from, which dev_t isn't well
1379 		 * suited to since those number don't match to unit numbers
1380 		 * very well.  We may just need to engage in a hack where
1381 		 * we pass -C to the boot args if we are the boot device.
1382 		 */
1383 		major = ACDMAJOR;
1384 		unit = 0;       /* XXX */
1385 	}
1386 
1387 	/* XXX a better kludge to set the root disk unit number */
1388 	if ((nip = getenv("root_disk_unit")) != NULL) {
1389 		i = strtol(nip, &cp, 0);
1390 		/* check for parse error */
1391 		if ((cp != nip) && (*cp == 0))
1392 			unit = i;
1393 	}
1394 
1395 	rootdev = MAKEBOOTDEV(major, slice, unit, partition);
1396 	DPRINTF("dev is 0x%x\n", rootdev);
1397 	return (rootdev);
1398 }
1399