xref: /onnv-gate/usr/src/uts/i86pc/os/biosdisk.c (revision 8135:a8055d14e4de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55084Sjohnlev  * Common Development and Distribution License (the "License").
65084Sjohnlev  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8135SSeth.Goldberg@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/controlregs.h>
290Sstevel@tonic-gate #include <sys/bootconf.h>
300Sstevel@tonic-gate #include <sys/bootvfs.h>
310Sstevel@tonic-gate #include <sys/bootregs.h>
320Sstevel@tonic-gate #include <sys/bootconf.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/promif.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/sunndi.h>
380Sstevel@tonic-gate #include <sys/biosdisk.h>
390Sstevel@tonic-gate #include <sys/psw.h>
405084Sjohnlev #if defined(__xpv)
415084Sjohnlev #include <sys/hypervisor.h>
425084Sjohnlev #endif
430Sstevel@tonic-gate 
44*8135SSeth.Goldberg@Sun.COM extern int prom_debug;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /* hard code realmode memory address for now */
470Sstevel@tonic-gate #define	BIOS_RES_BUFFER_ADDR		0x7000
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	BIOSDEV_NUM	8
500Sstevel@tonic-gate #define	STARTING_DRVNUM	0x80
510Sstevel@tonic-gate #define	FP_OFF(fp) (((uintptr_t)(fp)) & 0xFFFF)
520Sstevel@tonic-gate #define	FP_SEG(fp) ((((uintptr_t)(fp)) >> 16) & 0xFFFF)
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef DEBUG
550Sstevel@tonic-gate int biosdebug = 0;
560Sstevel@tonic-gate #define	dprintf(fmt) \
570Sstevel@tonic-gate 	if (biosdebug) \
580Sstevel@tonic-gate 		prom_printf fmt
590Sstevel@tonic-gate #else
600Sstevel@tonic-gate #define	dprintf(fmt)
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate 
630Sstevel@tonic-gate biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
640Sstevel@tonic-gate int dobiosdev = 1;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int bios_check_extension_present(uchar_t);
680Sstevel@tonic-gate static int get_dev_params(uchar_t);
690Sstevel@tonic-gate static int read_firstblock(uchar_t drivenum);
700Sstevel@tonic-gate static int drive_present(uchar_t drivenum);
710Sstevel@tonic-gate static void reset_disk(uchar_t drivenum);
72*8135SSeth.Goldberg@Sun.COM static int is_eltorito(uchar_t drivenum);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate void
startup_bios_disk()750Sstevel@tonic-gate startup_bios_disk()
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	uchar_t drivenum;
780Sstevel@tonic-gate 	int got_devparams = 0;
790Sstevel@tonic-gate 	int got_first_block = 0;
800Sstevel@tonic-gate 	uchar_t	name[20];
810Sstevel@tonic-gate 	dev_info_t	*devi;
82*8135SSeth.Goldberg@Sun.COM 	int extensions;
830Sstevel@tonic-gate 
845084Sjohnlev #if defined(__xpv)
855084Sjohnlev 	if (!DOMAIN_IS_INITDOMAIN(xen_info))
865084Sjohnlev 		return;
875084Sjohnlev #endif
885084Sjohnlev 
890Sstevel@tonic-gate 	if (dobiosdev == 0)
900Sstevel@tonic-gate 		return;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	for (drivenum = 0x80; drivenum < (0x80 + BIOSDEV_NUM); drivenum++) {
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		if (!drive_present(drivenum))
950Sstevel@tonic-gate 			continue;
960Sstevel@tonic-gate 
97*8135SSeth.Goldberg@Sun.COM 		extensions = bios_check_extension_present(drivenum);
98*8135SSeth.Goldberg@Sun.COM 
99*8135SSeth.Goldberg@Sun.COM 		/*
100*8135SSeth.Goldberg@Sun.COM 		 * If we're booting from an Eltorito CD/DVD image, there's
101*8135SSeth.Goldberg@Sun.COM 		 * no need to get the device parameters or read the first block
102*8135SSeth.Goldberg@Sun.COM 		 * because we'll never install onto this device.
103*8135SSeth.Goldberg@Sun.COM 		 */
104*8135SSeth.Goldberg@Sun.COM 		if (extensions && is_eltorito(drivenum))
105*8135SSeth.Goldberg@Sun.COM 			continue;
106*8135SSeth.Goldberg@Sun.COM 
107*8135SSeth.Goldberg@Sun.COM 		if (extensions && get_dev_params(drivenum))
108*8135SSeth.Goldberg@Sun.COM 			got_devparams = 1;
109*8135SSeth.Goldberg@Sun.COM 		else
110*8135SSeth.Goldberg@Sun.COM 			got_devparams = 0;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		if ((got_first_block = read_firstblock(drivenum)) == 0) {
1130Sstevel@tonic-gate 			/* retry */
1140Sstevel@tonic-gate 			got_first_block = read_firstblock(drivenum);
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		if (got_devparams || got_first_block) {
1180Sstevel@tonic-gate 			(void) sprintf((char *)name, "biosdev-0x%x", drivenum);
1190Sstevel@tonic-gate 			devi = ddi_root_node();
1200Sstevel@tonic-gate 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1210Sstevel@tonic-gate 			    devi, (char *)name,
1220Sstevel@tonic-gate 			    (uchar_t *)&biosdev_info[drivenum - 0x80],
1230Sstevel@tonic-gate 			    sizeof (biosdev_data_t));
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static int
bios_check_extension_present(uchar_t drivenum)1290Sstevel@tonic-gate bios_check_extension_present(uchar_t drivenum)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	struct bop_regs rp = {0};
1320Sstevel@tonic-gate 	extern struct bootops		*bootops;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	rp.eax.word.ax = 0x4100;
1350Sstevel@tonic-gate 	rp.ebx.word.bx = 0x55AA;
1360Sstevel@tonic-gate 	rp.edx.word.dx = drivenum;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/* make sure we have extension support */
1390Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || (rp.ebx.word.bx != 0xAA55)) {
1420Sstevel@tonic-gate 		dprintf(("bios_check_extension_present int13 fn 41 "
1430Sstevel@tonic-gate 		    "failed %d bx = %x\n", rp.eflags, rp.ebx.word.bx));
1440Sstevel@tonic-gate 		return (0);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((rp.ecx.word.cx & 0x7) == 0) {
1480Sstevel@tonic-gate 		dprintf(("bios_check_extension_present get device parameters "
1490Sstevel@tonic-gate 		    "not supported cx = %x\n", rp.ecx.word.cx));
1500Sstevel@tonic-gate 		return (0);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	return (1);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static int
get_dev_params(uchar_t drivenum)1570Sstevel@tonic-gate get_dev_params(uchar_t drivenum)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	struct bop_regs rp = {0};
1600Sstevel@tonic-gate 	fn48_t	 *bufp;
1610Sstevel@tonic-gate 	extern struct bootops		*bootops;
1620Sstevel@tonic-gate 	int i;
1630Sstevel@tonic-gate 	int index;
164175Sjg 	uchar_t *tmp;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	dprintf(("In get_dev_params\n"));
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
1690Sstevel@tonic-gate 
170175Sjg 	/*
171175Sjg 	 * We cannot use bzero here as we're initializing data
172175Sjg 	 * at an address below kernel base.
173175Sjg 	 */
1740Sstevel@tonic-gate 	for (i = 0; i < sizeof (*bufp); i++)
1750Sstevel@tonic-gate 		((uchar_t *)bufp)[i] = 0;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	bufp->buflen = sizeof (*bufp);
1780Sstevel@tonic-gate 	rp.eax.word.ax = 0x4800;
1790Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
1820Sstevel@tonic-gate 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((rp.eflags & PS_C) != 0) {
1870Sstevel@tonic-gate 		dprintf(("EDD FAILED on drive eflag = %x ah= %x\n",
1880Sstevel@tonic-gate 		    rp.eflags, rp.eax.byte.ah));
1890Sstevel@tonic-gate 		return (0);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	index = drivenum - 0x80;
1930Sstevel@tonic-gate 	biosdev_info[index].edd_valid = 1;
194175Sjg 
195175Sjg 	/*
196175Sjg 	 * Some compilers turn a structure copy into a call
197175Sjg 	 * to memcpy.  Since we are copying data below kernel
198175Sjg 	 * base intentionally, and memcpy asserts that's not
199175Sjg 	 * the case, we do the copy manually here.
200175Sjg 	 */
201175Sjg 	tmp = (uchar_t *)&biosdev_info[index].fn48_dev_params;
202175Sjg 	for (i = 0; i < sizeof (*bufp); i++)
203175Sjg 		tmp[i] = ((uchar_t *)bufp)[i];
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	return (1);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static int
drive_present(uchar_t drivenum)2090Sstevel@tonic-gate drive_present(uchar_t drivenum)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	struct bop_regs rp = {0};
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	rp.eax.byte.ah = 0x8;	/* get params */
2140Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || rp.eax.byte.ah != 0) {
2190Sstevel@tonic-gate 		dprintf(("drive not present drivenum %x eflag %x ah %x\n",
2205084Sjohnlev 		    drivenum, rp.eflags, rp.eax.byte.ah));
2210Sstevel@tonic-gate 		return (0);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	dprintf(("drive-present %x\n", drivenum));
2250Sstevel@tonic-gate 	return (1);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static void
reset_disk(uchar_t drivenum)2290Sstevel@tonic-gate reset_disk(uchar_t drivenum)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	struct bop_regs rp = {0};
2320Sstevel@tonic-gate 	int status;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	rp.eax.byte.ah = 0x0;   /* reset disk */
2350Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0)
2420Sstevel@tonic-gate 		dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
2430Sstevel@tonic-gate 		    status));
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /* Get first block */
2470Sstevel@tonic-gate static int
read_firstblock(uchar_t drivenum)2480Sstevel@tonic-gate read_firstblock(uchar_t drivenum)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	struct bop_regs rp = {0};
2520Sstevel@tonic-gate 	caddr_t	 bufp;
2530Sstevel@tonic-gate 	uchar_t status;
2540Sstevel@tonic-gate 	int i, index;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	reset_disk(drivenum);
2580Sstevel@tonic-gate 	bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	rp.eax.byte.ah = 0x2; 	/* Read disk */
2620Sstevel@tonic-gate 	rp.eax.byte.al = 1;	/* nsect */
2630Sstevel@tonic-gate 	rp.ecx.byte.ch = 0;	/* cyl & 0xff */
2640Sstevel@tonic-gate 	rp.ecx.byte.cl = 1;	/* cyl >> 2 & 0xc0 (sector number) */
2650Sstevel@tonic-gate 	rp.edx.byte.dh = 0;	/* head */
2660Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;	/* drivenum */
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/* es:bx is buf address */
2690Sstevel@tonic-gate 	rp.ebx.word.bx = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
2700Sstevel@tonic-gate 	rp.es = FP_SEG((uint_t)(uintptr_t)bufp);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2750Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0) {
2760Sstevel@tonic-gate 		dprintf(("read_firstblock AH not clear %x \n", status));
2770Sstevel@tonic-gate 		return (0);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
2810Sstevel@tonic-gate 	    *(uint32_t *)(bufp +0x1b8)));
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	index = drivenum - 0x80;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	biosdev_info[index].first_block_valid = 1;
2860Sstevel@tonic-gate 	for (i = 0; i < 512; i++)
2870Sstevel@tonic-gate 		biosdev_info[index].first_block[i] = *((uchar_t *)bufp + i);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (1);
2900Sstevel@tonic-gate }
291*8135SSeth.Goldberg@Sun.COM 
292*8135SSeth.Goldberg@Sun.COM static int
is_eltorito(uchar_t drivenum)293*8135SSeth.Goldberg@Sun.COM is_eltorito(uchar_t drivenum)
294*8135SSeth.Goldberg@Sun.COM {
295*8135SSeth.Goldberg@Sun.COM 	struct bop_regs rp = {0};
296*8135SSeth.Goldberg@Sun.COM 	fn4b_t	 *bufp;
297*8135SSeth.Goldberg@Sun.COM 	extern struct bootops		*bootops;
298*8135SSeth.Goldberg@Sun.COM 	int i;
299*8135SSeth.Goldberg@Sun.COM 
300*8135SSeth.Goldberg@Sun.COM 	dprintf(("In is_eltorito\n"));
301*8135SSeth.Goldberg@Sun.COM 
302*8135SSeth.Goldberg@Sun.COM 	bufp = (fn4b_t *)BIOS_RES_BUFFER_ADDR;
303*8135SSeth.Goldberg@Sun.COM 
304*8135SSeth.Goldberg@Sun.COM 	/*
305*8135SSeth.Goldberg@Sun.COM 	 * We cannot use bzero here as we're initializing data
306*8135SSeth.Goldberg@Sun.COM 	 * at an address below kernel base.
307*8135SSeth.Goldberg@Sun.COM 	 */
308*8135SSeth.Goldberg@Sun.COM 	for (i = 0; i < sizeof (*bufp); i++)
309*8135SSeth.Goldberg@Sun.COM 		((uchar_t *)bufp)[i] = 0;
310*8135SSeth.Goldberg@Sun.COM 
311*8135SSeth.Goldberg@Sun.COM 	bufp->pkt_size = sizeof (*bufp);
312*8135SSeth.Goldberg@Sun.COM 	rp.eax.word.ax = 0x4b01;
313*8135SSeth.Goldberg@Sun.COM 	rp.edx.byte.dl = drivenum;
314*8135SSeth.Goldberg@Sun.COM 
315*8135SSeth.Goldberg@Sun.COM 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
316*8135SSeth.Goldberg@Sun.COM 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
317*8135SSeth.Goldberg@Sun.COM 
318*8135SSeth.Goldberg@Sun.COM 	BOP_DOINT(bootops, 0x13, &rp);
319*8135SSeth.Goldberg@Sun.COM 
320*8135SSeth.Goldberg@Sun.COM 	if ((rp.eflags & PS_C) != 0 || bufp->drivenum != drivenum) {
321*8135SSeth.Goldberg@Sun.COM 		dprintf(("fn 0x4b01 FAILED on drive "
322*8135SSeth.Goldberg@Sun.COM 		    "eflags=%x ah=%x drivenum=%x\n",
323*8135SSeth.Goldberg@Sun.COM 		    rp.eflags, rp.eax.byte.ah, bufp->drivenum));
324*8135SSeth.Goldberg@Sun.COM 		return (0);
325*8135SSeth.Goldberg@Sun.COM 	}
326*8135SSeth.Goldberg@Sun.COM 
327*8135SSeth.Goldberg@Sun.COM 	if (prom_debug)
328*8135SSeth.Goldberg@Sun.COM 		prom_printf("INT13 FN4B01 mtype => %x", bufp->boot_mtype);
329*8135SSeth.Goldberg@Sun.COM 
330*8135SSeth.Goldberg@Sun.COM 	return (1);
331*8135SSeth.Goldberg@Sun.COM }
332