xref: /onnv-gate/usr/src/uts/i86pc/os/biosdisk.c (revision 175:fb740727ec2a)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/controlregs.h>
320Sstevel@tonic-gate #include <sys/bootconf.h>
330Sstevel@tonic-gate #include <sys/bootvfs.h>
340Sstevel@tonic-gate #include <sys/bootregs.h>
350Sstevel@tonic-gate #include <sys/bootconf.h>
360Sstevel@tonic-gate #include <sys/conf.h>
370Sstevel@tonic-gate #include <sys/promif.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/sunndi.h>
410Sstevel@tonic-gate #include <sys/biosdisk.h>
420Sstevel@tonic-gate #include <sys/psw.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* hard code realmode memory address for now */
460Sstevel@tonic-gate #define	BIOS_RES_BUFFER_ADDR		0x7000
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	BIOSDEV_NUM	8
490Sstevel@tonic-gate #define	STARTING_DRVNUM	0x80
500Sstevel@tonic-gate #define	FP_OFF(fp) (((uintptr_t)(fp)) & 0xFFFF)
510Sstevel@tonic-gate #define	FP_SEG(fp) ((((uintptr_t)(fp)) >> 16) & 0xFFFF)
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #ifdef DEBUG
540Sstevel@tonic-gate int biosdebug = 0;
550Sstevel@tonic-gate #define	dprintf(fmt) \
560Sstevel@tonic-gate 	if (biosdebug) \
570Sstevel@tonic-gate 		prom_printf fmt
580Sstevel@tonic-gate #else
590Sstevel@tonic-gate #define	dprintf(fmt)
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
630Sstevel@tonic-gate int dobiosdev = 1;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static int bios_check_extension_present(uchar_t);
670Sstevel@tonic-gate static int get_dev_params(uchar_t);
680Sstevel@tonic-gate static int read_firstblock(uchar_t drivenum);
690Sstevel@tonic-gate static int drive_present(uchar_t drivenum);
700Sstevel@tonic-gate static void reset_disk(uchar_t drivenum);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate void
740Sstevel@tonic-gate startup_bios_disk()
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	uchar_t drivenum;
770Sstevel@tonic-gate 	int got_devparams = 0;
780Sstevel@tonic-gate 	int got_first_block = 0;
790Sstevel@tonic-gate 	uchar_t	name[20];
800Sstevel@tonic-gate 	dev_info_t	*devi;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (dobiosdev == 0)
830Sstevel@tonic-gate 		return;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	for (drivenum = 0x80; drivenum < (0x80 + BIOSDEV_NUM); drivenum++) {
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 		if (!drive_present(drivenum))
880Sstevel@tonic-gate 			continue;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 		got_devparams = get_dev_params(drivenum);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 		if ((got_first_block = read_firstblock(drivenum)) == 0) {
930Sstevel@tonic-gate 			/* retry */
940Sstevel@tonic-gate 			got_first_block = read_firstblock(drivenum);
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 		if (got_devparams || got_first_block) {
980Sstevel@tonic-gate 			(void) sprintf((char *)name, "biosdev-0x%x", drivenum);
990Sstevel@tonic-gate 			devi = ddi_root_node();
1000Sstevel@tonic-gate 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1010Sstevel@tonic-gate 			    devi, (char *)name,
1020Sstevel@tonic-gate 			    (uchar_t *)&biosdev_info[drivenum - 0x80],
1030Sstevel@tonic-gate 			    sizeof (biosdev_data_t));
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static int
1090Sstevel@tonic-gate bios_check_extension_present(uchar_t drivenum)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	struct bop_regs rp = {0};
1120Sstevel@tonic-gate 	extern struct bootops		*bootops;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	rp.eax.word.ax = 0x4100;
1150Sstevel@tonic-gate 	rp.ebx.word.bx = 0x55AA;
1160Sstevel@tonic-gate 	rp.edx.word.dx = drivenum;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/* make sure we have extension support */
1190Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || (rp.ebx.word.bx != 0xAA55)) {
1220Sstevel@tonic-gate 		dprintf(("bios_check_extension_present int13 fn 41 "
1230Sstevel@tonic-gate 		    "failed %d bx = %x\n", rp.eflags, rp.ebx.word.bx));
1240Sstevel@tonic-gate 		return (0);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if ((rp.ecx.word.cx & 0x7) == 0) {
1280Sstevel@tonic-gate 		dprintf(("bios_check_extension_present get device parameters "
1290Sstevel@tonic-gate 		    "not supported cx = %x\n", rp.ecx.word.cx));
1300Sstevel@tonic-gate 		return (0);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	return (1);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int
1370Sstevel@tonic-gate get_dev_params(uchar_t drivenum)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	struct bop_regs rp = {0};
1400Sstevel@tonic-gate 	fn48_t	 *bufp;
1410Sstevel@tonic-gate 	extern struct bootops		*bootops;
1420Sstevel@tonic-gate 	int i;
1430Sstevel@tonic-gate 	int index;
144*175Sjg 	uchar_t *tmp;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	dprintf(("In get_dev_params\n"));
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (bios_check_extension_present(drivenum) == 0)
1490Sstevel@tonic-gate 		return (0);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
1520Sstevel@tonic-gate 
153*175Sjg 	/*
154*175Sjg 	 * We cannot use bzero here as we're initializing data
155*175Sjg 	 * at an address below kernel base.
156*175Sjg 	 */
1570Sstevel@tonic-gate 	for (i = 0; i < sizeof (*bufp); i++)
1580Sstevel@tonic-gate 		((uchar_t *)bufp)[i] = 0;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	bufp->buflen = sizeof (*bufp);
1610Sstevel@tonic-gate 	rp.eax.word.ax = 0x4800;
1620Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
1650Sstevel@tonic-gate 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if ((rp.eflags & PS_C) != 0) {
1700Sstevel@tonic-gate 		dprintf(("EDD FAILED on drive eflag = %x ah= %x\n",
1710Sstevel@tonic-gate 		    rp.eflags, rp.eax.byte.ah));
1720Sstevel@tonic-gate 		return (0);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	index = drivenum - 0x80;
1760Sstevel@tonic-gate 	biosdev_info[index].edd_valid = 1;
177*175Sjg 
178*175Sjg 	/*
179*175Sjg 	 * Some compilers turn a structure copy into a call
180*175Sjg 	 * to memcpy.  Since we are copying data below kernel
181*175Sjg 	 * base intentionally, and memcpy asserts that's not
182*175Sjg 	 * the case, we do the copy manually here.
183*175Sjg 	 */
184*175Sjg 	tmp = (uchar_t *)&biosdev_info[index].fn48_dev_params;
185*175Sjg 	for (i = 0; i < sizeof (*bufp); i++)
186*175Sjg 		tmp[i] = ((uchar_t *)bufp)[i];
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	return (1);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static int
1920Sstevel@tonic-gate drive_present(uchar_t drivenum)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	struct bop_regs rp = {0};
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	rp.eax.byte.ah = 0x8;	/* get params */
1970Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || rp.eax.byte.ah != 0) {
2020Sstevel@tonic-gate 		dprintf(("drive not present drivenum %x eflag %x ah %x\n",
2030Sstevel@tonic-gate 		drivenum, rp.eflags, rp.eax.byte.ah));
2040Sstevel@tonic-gate 		return (0);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	dprintf(("drive-present %x\n", drivenum));
2080Sstevel@tonic-gate 	return (1);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static void
2130Sstevel@tonic-gate reset_disk(uchar_t drivenum)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	struct bop_regs rp = {0};
2160Sstevel@tonic-gate 	int status;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	rp.eax.byte.ah = 0x0;   /* reset disk */
2190Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0)
2260Sstevel@tonic-gate 		dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
2270Sstevel@tonic-gate 		    status));
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /* Get first block */
2310Sstevel@tonic-gate static int
2320Sstevel@tonic-gate read_firstblock(uchar_t drivenum)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	struct bop_regs rp = {0};
2360Sstevel@tonic-gate 	caddr_t	 bufp;
2370Sstevel@tonic-gate 	uchar_t status;
2380Sstevel@tonic-gate 	int i, index;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	reset_disk(drivenum);
2420Sstevel@tonic-gate 	bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	rp.eax.byte.ah = 0x2; 	/* Read disk */
2460Sstevel@tonic-gate 	rp.eax.byte.al = 1;	/* nsect */
2470Sstevel@tonic-gate 	rp.ecx.byte.ch = 0;	/* cyl & 0xff */
2480Sstevel@tonic-gate 	rp.ecx.byte.cl = 1;	/* cyl >> 2 & 0xc0 (sector number) */
2490Sstevel@tonic-gate 	rp.edx.byte.dh = 0;	/* head */
2500Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;	/* drivenum */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/* es:bx is buf address */
2530Sstevel@tonic-gate 	rp.ebx.word.bx = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
2540Sstevel@tonic-gate 	rp.es = FP_SEG((uint_t)(uintptr_t)bufp);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2590Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0) {
2600Sstevel@tonic-gate 		dprintf(("read_firstblock AH not clear %x \n", status));
2610Sstevel@tonic-gate 		return (0);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
2650Sstevel@tonic-gate 	    *(uint32_t *)(bufp +0x1b8)));
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	index = drivenum - 0x80;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	biosdev_info[index].first_block_valid = 1;
2700Sstevel@tonic-gate 	for (i = 0; i < 512; i++)
2710Sstevel@tonic-gate 		biosdev_info[index].first_block[i] = *((uchar_t *)bufp + i);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	return (1);
2740Sstevel@tonic-gate }
275