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