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