xref: /titanic_44/usr/src/uts/i86pc/os/biosdisk.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/controlregs.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/bootregs.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/biosdisk.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/psw.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* hard code realmode memory address for now */
46*7c478bd9Sstevel@tonic-gate #define	BIOS_RES_BUFFER_ADDR		0x7000
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	BIOSDEV_NUM	8
49*7c478bd9Sstevel@tonic-gate #define	STARTING_DRVNUM	0x80
50*7c478bd9Sstevel@tonic-gate #define	FP_OFF(fp) (((uintptr_t)(fp)) & 0xFFFF)
51*7c478bd9Sstevel@tonic-gate #define	FP_SEG(fp) ((((uintptr_t)(fp)) >> 16) & 0xFFFF)
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
54*7c478bd9Sstevel@tonic-gate int biosdebug = 0;
55*7c478bd9Sstevel@tonic-gate #define	dprintf(fmt) \
56*7c478bd9Sstevel@tonic-gate 	if (biosdebug) \
57*7c478bd9Sstevel@tonic-gate 		prom_printf fmt
58*7c478bd9Sstevel@tonic-gate #else
59*7c478bd9Sstevel@tonic-gate #define	dprintf(fmt)
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
63*7c478bd9Sstevel@tonic-gate int dobiosdev = 1;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static int bios_check_extension_present(uchar_t);
67*7c478bd9Sstevel@tonic-gate static int get_dev_params(uchar_t);
68*7c478bd9Sstevel@tonic-gate static int read_firstblock(uchar_t drivenum);
69*7c478bd9Sstevel@tonic-gate static int drive_present(uchar_t drivenum);
70*7c478bd9Sstevel@tonic-gate static void reset_disk(uchar_t drivenum);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate void
74*7c478bd9Sstevel@tonic-gate startup_bios_disk()
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	uchar_t drivenum;
77*7c478bd9Sstevel@tonic-gate 	int got_devparams = 0;
78*7c478bd9Sstevel@tonic-gate 	int got_first_block = 0;
79*7c478bd9Sstevel@tonic-gate 	uchar_t	name[20];
80*7c478bd9Sstevel@tonic-gate 	dev_info_t	*devi;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (dobiosdev == 0)
83*7c478bd9Sstevel@tonic-gate 		return;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	for (drivenum = 0x80; drivenum < (0x80 + BIOSDEV_NUM); drivenum++) {
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		if (!drive_present(drivenum))
88*7c478bd9Sstevel@tonic-gate 			continue;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		got_devparams = get_dev_params(drivenum);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if ((got_first_block = read_firstblock(drivenum)) == 0) {
93*7c478bd9Sstevel@tonic-gate 			/* retry */
94*7c478bd9Sstevel@tonic-gate 			got_first_block = read_firstblock(drivenum);
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		if (got_devparams || got_first_block) {
98*7c478bd9Sstevel@tonic-gate 			(void) sprintf((char *)name, "biosdev-0x%x", drivenum);
99*7c478bd9Sstevel@tonic-gate 			devi = ddi_root_node();
100*7c478bd9Sstevel@tonic-gate 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
101*7c478bd9Sstevel@tonic-gate 			    devi, (char *)name,
102*7c478bd9Sstevel@tonic-gate 			    (uchar_t *)&biosdev_info[drivenum - 0x80],
103*7c478bd9Sstevel@tonic-gate 			    sizeof (biosdev_data_t));
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static int
109*7c478bd9Sstevel@tonic-gate bios_check_extension_present(uchar_t drivenum)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
112*7c478bd9Sstevel@tonic-gate 	extern struct bootops		*bootops;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	rp.eax.word.ax = 0x4100;
115*7c478bd9Sstevel@tonic-gate 	rp.ebx.word.bx = 0x55AA;
116*7c478bd9Sstevel@tonic-gate 	rp.edx.word.dx = drivenum;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	/* make sure we have extension support */
119*7c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || (rp.ebx.word.bx != 0xAA55)) {
122*7c478bd9Sstevel@tonic-gate 		dprintf(("bios_check_extension_present int13 fn 41 "
123*7c478bd9Sstevel@tonic-gate 		    "failed %d bx = %x\n", rp.eflags, rp.ebx.word.bx));
124*7c478bd9Sstevel@tonic-gate 		return (0);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if ((rp.ecx.word.cx & 0x7) == 0) {
128*7c478bd9Sstevel@tonic-gate 		dprintf(("bios_check_extension_present get device parameters "
129*7c478bd9Sstevel@tonic-gate 		    "not supported cx = %x\n", rp.ecx.word.cx));
130*7c478bd9Sstevel@tonic-gate 		return (0);
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	return (1);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate static int
137*7c478bd9Sstevel@tonic-gate get_dev_params(uchar_t drivenum)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
140*7c478bd9Sstevel@tonic-gate 	fn48_t	 *bufp;
141*7c478bd9Sstevel@tonic-gate 	extern struct bootops		*bootops;
142*7c478bd9Sstevel@tonic-gate 	int i;
143*7c478bd9Sstevel@tonic-gate 	int index;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	dprintf(("In get_dev_params\n"));
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (bios_check_extension_present(drivenum) == 0)
148*7c478bd9Sstevel@tonic-gate 		return (0);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (*bufp); i++)
153*7c478bd9Sstevel@tonic-gate 		((uchar_t *)bufp)[i] = 0;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	bufp->buflen = sizeof (*bufp);
156*7c478bd9Sstevel@tonic-gate 	rp.eax.word.ax = 0x4800;
157*7c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
160*7c478bd9Sstevel@tonic-gate 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if ((rp.eflags & PS_C) != 0) {
165*7c478bd9Sstevel@tonic-gate 		dprintf(("EDD FAILED on drive eflag = %x ah= %x\n",
166*7c478bd9Sstevel@tonic-gate 		    rp.eflags, rp.eax.byte.ah));
167*7c478bd9Sstevel@tonic-gate 		return (0);
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	index = drivenum - 0x80;
171*7c478bd9Sstevel@tonic-gate 	biosdev_info[index].edd_valid = 1;
172*7c478bd9Sstevel@tonic-gate 	biosdev_info[index].fn48_dev_params = *bufp;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	return (1);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate static int
179*7c478bd9Sstevel@tonic-gate drive_present(uchar_t drivenum)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x8;	/* get params */
184*7c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || rp.eax.byte.ah != 0) {
189*7c478bd9Sstevel@tonic-gate 		dprintf(("drive not present drivenum %x eflag %x ah %x\n",
190*7c478bd9Sstevel@tonic-gate 		drivenum, rp.eflags, rp.eax.byte.ah));
191*7c478bd9Sstevel@tonic-gate 		return (0);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	dprintf(("drive-present %x\n", drivenum));
195*7c478bd9Sstevel@tonic-gate 	return (1);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate static void
200*7c478bd9Sstevel@tonic-gate reset_disk(uchar_t drivenum)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
203*7c478bd9Sstevel@tonic-gate 	int status;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x0;   /* reset disk */
206*7c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	status = rp.eax.byte.ah;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0)
213*7c478bd9Sstevel@tonic-gate 		dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
214*7c478bd9Sstevel@tonic-gate 		    status));
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate /* Get first block */
218*7c478bd9Sstevel@tonic-gate static int
219*7c478bd9Sstevel@tonic-gate read_firstblock(uchar_t drivenum)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
223*7c478bd9Sstevel@tonic-gate 	caddr_t	 bufp;
224*7c478bd9Sstevel@tonic-gate 	uchar_t status;
225*7c478bd9Sstevel@tonic-gate 	int i, index;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	reset_disk(drivenum);
229*7c478bd9Sstevel@tonic-gate 	bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x2; 	/* Read disk */
233*7c478bd9Sstevel@tonic-gate 	rp.eax.byte.al = 1;	/* nsect */
234*7c478bd9Sstevel@tonic-gate 	rp.ecx.byte.ch = 0;	/* cyl & 0xff */
235*7c478bd9Sstevel@tonic-gate 	rp.ecx.byte.cl = 1;	/* cyl >> 2 & 0xc0 (sector number) */
236*7c478bd9Sstevel@tonic-gate 	rp.edx.byte.dh = 0;	/* head */
237*7c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;	/* drivenum */
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/* es:bx is buf address */
240*7c478bd9Sstevel@tonic-gate 	rp.ebx.word.bx = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
241*7c478bd9Sstevel@tonic-gate 	rp.es = FP_SEG((uint_t)(uintptr_t)bufp);
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	status = rp.eax.byte.ah;
246*7c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0) {
247*7c478bd9Sstevel@tonic-gate 		dprintf(("read_firstblock AH not clear %x \n", status));
248*7c478bd9Sstevel@tonic-gate 		return (0);
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
252*7c478bd9Sstevel@tonic-gate 	    *(uint32_t *)(bufp +0x1b8)));
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	index = drivenum - 0x80;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	biosdev_info[index].first_block_valid = 1;
257*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 512; i++)
258*7c478bd9Sstevel@tonic-gate 		biosdev_info[index].first_block[i] = *((uchar_t *)bufp + i);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	return (1);
261*7c478bd9Sstevel@tonic-gate }
262