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