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