xref: /illumos-gate/usr/src/uts/i86pc/os/biosdisk.c (revision afab0816ecb604f0099a09ad8ee398f0d7b77b1c)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/controlregs.h>
29 #include <sys/bootconf.h>
30 #include <sys/bootvfs.h>
31 #include <sys/bootregs.h>
32 #include <sys/bootconf.h>
33 #include <sys/conf.h>
34 #include <sys/promif.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/sunndi.h>
38 #include <sys/biosdisk.h>
39 #include <sys/psw.h>
40 #if defined(__xpv)
41 #include <sys/hypervisor.h>
42 #endif
43 
44 extern int prom_debug;
45 
46 /* hard code realmode memory address for now */
47 #define	BIOS_RES_BUFFER_ADDR		0x7000
48 
49 #define	BIOSDEV_NUM	8
50 #define	STARTING_DRVNUM	0x80
51 #define	FP_OFF(fp) (((uintptr_t)(fp)) & 0xFFFF)
52 #define	FP_SEG(fp) ((((uintptr_t)(fp)) >> 16) & 0xFFFF)
53 
54 #ifdef DEBUG
55 int biosdebug = 0;
56 #define	dprintf(fmt) \
57 	if (biosdebug) \
58 		prom_printf fmt
59 #else
60 #define	dprintf(fmt)
61 #endif
62 
63 biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
64 int dobiosdev = 1;
65 
66 
67 static int bios_check_extension_present(uchar_t);
68 static int get_dev_params(uchar_t);
69 static int read_firstblock(uchar_t drivenum);
70 static int drive_present(uchar_t drivenum);
71 static void reset_disk(uchar_t drivenum);
72 static int is_eltorito(uchar_t drivenum);
73 
74 void
75 startup_bios_disk()
76 {
77 	uchar_t drivenum;
78 	int got_devparams = 0;
79 	int got_first_block = 0;
80 	uchar_t	name[20];
81 	dev_info_t	*devi;
82 	int extensions;
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 		extensions = bios_check_extension_present(drivenum);
98 
99 		/*
100 		 * If we're booting from an Eltorito CD/DVD image, there's
101 		 * no need to get the device parameters or read the first block
102 		 * because we'll never install onto this device.
103 		 */
104 		if (extensions && is_eltorito(drivenum))
105 			continue;
106 
107 		if (extensions && get_dev_params(drivenum))
108 			got_devparams = 1;
109 		else
110 			got_devparams = 0;
111 
112 		if ((got_first_block = read_firstblock(drivenum)) == 0) {
113 			/* retry */
114 			got_first_block = read_firstblock(drivenum);
115 		}
116 
117 		if (got_devparams || got_first_block) {
118 			(void) sprintf((char *)name, "biosdev-0x%x", drivenum);
119 			devi = ddi_root_node();
120 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
121 			    devi, (char *)name,
122 			    (uchar_t *)&biosdev_info[drivenum - 0x80],
123 			    sizeof (biosdev_data_t));
124 		}
125 	}
126 }
127 
128 static int
129 bios_check_extension_present(uchar_t drivenum)
130 {
131 	struct bop_regs rp = {0};
132 	extern struct bootops		*bootops;
133 
134 	rp.eax.word.ax = 0x4100;
135 	rp.ebx.word.bx = 0x55AA;
136 	rp.edx.word.dx = drivenum;
137 
138 	/* make sure we have extension support */
139 	BOP_DOINT(bootops, 0x13, &rp);
140 
141 	if (((rp.eflags & PS_C) != 0) || (rp.ebx.word.bx != 0xAA55)) {
142 		dprintf(("bios_check_extension_present int13 fn 41 "
143 		    "failed %d bx = %x\n", rp.eflags, rp.ebx.word.bx));
144 		return (0);
145 	}
146 
147 	if ((rp.ecx.word.cx & 0x7) == 0) {
148 		dprintf(("bios_check_extension_present get device parameters "
149 		    "not supported cx = %x\n", rp.ecx.word.cx));
150 		return (0);
151 	}
152 
153 	return (1);
154 }
155 
156 static int
157 get_dev_params(uchar_t drivenum)
158 {
159 	struct bop_regs rp = {0};
160 	fn48_t	 *bufp;
161 	extern struct bootops		*bootops;
162 	int i;
163 	int index;
164 	uchar_t *tmp;
165 
166 	dprintf(("In get_dev_params\n"));
167 
168 	bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
169 
170 	/*
171 	 * We cannot use bzero here as we're initializing data
172 	 * at an address below kernel base.
173 	 */
174 	for (i = 0; i < sizeof (*bufp); i++)
175 		((uchar_t *)bufp)[i] = 0;
176 
177 	bufp->buflen = sizeof (*bufp);
178 	rp.eax.word.ax = 0x4800;
179 	rp.edx.byte.dl = drivenum;
180 
181 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
182 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
183 
184 	BOP_DOINT(bootops, 0x13, &rp);
185 
186 	if ((rp.eflags & PS_C) != 0) {
187 		dprintf(("EDD FAILED on drive eflag = %x ah= %x\n",
188 		    rp.eflags, rp.eax.byte.ah));
189 		return (0);
190 	}
191 
192 	index = drivenum - 0x80;
193 	biosdev_info[index].edd_valid = 1;
194 
195 	/*
196 	 * Some compilers turn a structure copy into a call
197 	 * to memcpy.  Since we are copying data below kernel
198 	 * base intentionally, and memcpy asserts that's not
199 	 * the case, we do the copy manually here.
200 	 */
201 	tmp = (uchar_t *)&biosdev_info[index].fn48_dev_params;
202 	for (i = 0; i < sizeof (*bufp); i++)
203 		tmp[i] = ((uchar_t *)bufp)[i];
204 
205 	return (1);
206 }
207 
208 static int
209 drive_present(uchar_t drivenum)
210 {
211 	struct bop_regs rp = {0};
212 
213 	rp.eax.byte.ah = 0x8;	/* get params */
214 	rp.edx.byte.dl = drivenum;
215 
216 	BOP_DOINT(bootops, 0x13, &rp);
217 
218 	if (((rp.eflags & PS_C) != 0) || rp.eax.byte.ah != 0) {
219 		dprintf(("drive not present drivenum %x eflag %x ah %x\n",
220 		    drivenum, rp.eflags, rp.eax.byte.ah));
221 		return (0);
222 	}
223 
224 	dprintf(("drive-present %x\n", drivenum));
225 	return (1);
226 }
227 
228 static void
229 reset_disk(uchar_t drivenum)
230 {
231 	struct bop_regs rp = {0};
232 	int status;
233 
234 	rp.eax.byte.ah = 0x0;   /* reset disk */
235 	rp.edx.byte.dl = drivenum;
236 
237 	BOP_DOINT(bootops, 0x13, &rp);
238 
239 	status = rp.eax.byte.ah;
240 
241 	if (((rp.eflags & PS_C) != 0) || status != 0)
242 		dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
243 		    status));
244 }
245 
246 /* Get first block */
247 static int
248 read_firstblock(uchar_t drivenum)
249 {
250 
251 	struct bop_regs rp = {0};
252 	caddr_t	 bufp;
253 	uchar_t status;
254 	int i, index;
255 
256 
257 	reset_disk(drivenum);
258 	bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
259 
260 
261 	rp.eax.byte.ah = 0x2; 	/* Read disk */
262 	rp.eax.byte.al = 1;	/* nsect */
263 	rp.ecx.byte.ch = 0;	/* cyl & 0xff */
264 	rp.ecx.byte.cl = 1;	/* cyl >> 2 & 0xc0 (sector number) */
265 	rp.edx.byte.dh = 0;	/* head */
266 	rp.edx.byte.dl = drivenum;	/* drivenum */
267 
268 	/* es:bx is buf address */
269 	rp.ebx.word.bx = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
270 	rp.es = FP_SEG((uint_t)(uintptr_t)bufp);
271 
272 	BOP_DOINT(bootops, 0x13, &rp);
273 
274 	status = rp.eax.byte.ah;
275 	if (((rp.eflags & PS_C) != 0) || status != 0) {
276 		dprintf(("read_firstblock AH not clear %x \n", status));
277 		return (0);
278 	}
279 
280 	dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
281 	    *(uint32_t *)(bufp +0x1b8)));
282 
283 	index = drivenum - 0x80;
284 
285 	biosdev_info[index].first_block_valid = 1;
286 	for (i = 0; i < 512; i++)
287 		biosdev_info[index].first_block[i] = *((uchar_t *)bufp + i);
288 
289 	return (1);
290 }
291 
292 static int
293 is_eltorito(uchar_t drivenum)
294 {
295 	struct bop_regs rp = {0};
296 	fn4b_t	 *bufp;
297 	extern struct bootops		*bootops;
298 	int i;
299 
300 	dprintf(("In is_eltorito\n"));
301 
302 	bufp = (fn4b_t *)BIOS_RES_BUFFER_ADDR;
303 
304 	/*
305 	 * We cannot use bzero here as we're initializing data
306 	 * at an address below kernel base.
307 	 */
308 	for (i = 0; i < sizeof (*bufp); i++)
309 		((uchar_t *)bufp)[i] = 0;
310 
311 	bufp->pkt_size = sizeof (*bufp);
312 	rp.eax.word.ax = 0x4b01;
313 	rp.edx.byte.dl = drivenum;
314 
315 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
316 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
317 
318 	BOP_DOINT(bootops, 0x13, &rp);
319 
320 	if ((rp.eflags & PS_C) != 0 || bufp->drivenum != drivenum) {
321 		dprintf(("fn 0x4b01 FAILED on drive "
322 		    "eflags=%x ah=%x drivenum=%x\n",
323 		    rp.eflags, rp.eax.byte.ah, bufp->drivenum));
324 		return (0);
325 	}
326 
327 	if (prom_debug)
328 		prom_printf("INT13 FN4B01 mtype => %x", bufp->boot_mtype);
329 
330 	return (1);
331 }
332