9597 Want hypervisor API for FPU management
[unleashed.git] / usr / src / uts / i86pc / os / biosdisk.c
blob14f25f96ac5c8962e10dd561f77049d297cc606e
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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
44 extern int prom_debug;
46 /* hard code realmode memory address for now */
47 #define BIOS_RES_BUFFER_ADDR 0x7000
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)
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
63 biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
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);
73 #if !defined(__xpv)
74 void
75 startup_bios_disk()
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;
84 for (drivenum = 0x80; drivenum < (0x80 + BIOSDEV_NUM); drivenum++) {
86 if (!drive_present(drivenum))
87 continue;
89 extensions = bios_check_extension_present(drivenum);
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.
96 if (extensions && is_eltorito(drivenum))
97 continue;
99 if (extensions && get_dev_params(drivenum))
100 got_devparams = 1;
101 else
102 got_devparams = 0;
104 if ((got_first_block = read_firstblock(drivenum)) == 0) {
105 /* retry */
106 got_first_block = read_firstblock(drivenum);
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));
119 #endif
121 static int
122 bios_check_extension_present(uchar_t drivenum)
124 struct bop_regs rp = {0};
125 extern struct bootops *bootops;
127 rp.eax.word.ax = 0x4100;
128 rp.ebx.word.bx = 0x55AA;
129 rp.edx.word.dx = drivenum;
131 /* make sure we have extension support */
132 BOP_DOINT(bootops, 0x13, &rp);
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);
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);
146 return (1);
149 static int
150 get_dev_params(uchar_t drivenum)
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;
159 dprintf(("In get_dev_params\n"));
161 bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
164 * We cannot use bzero here as we're initializing data
165 * at an address below kernel base.
167 for (i = 0; i < sizeof (*bufp); i++)
168 ((uchar_t *)bufp)[i] = 0;
170 bufp->buflen = sizeof (*bufp);
171 rp.eax.word.ax = 0x4800;
172 rp.edx.byte.dl = drivenum;
174 rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
175 rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
177 BOP_DOINT(bootops, 0x13, &rp);
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);
185 index = drivenum - 0x80;
186 biosdev_info[index].edd_valid = 1;
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.
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];
198 return (1);
201 static int
202 drive_present(uchar_t drivenum)
204 struct bop_regs rp = {0};
206 rp.eax.byte.ah = 0x8; /* get params */
207 rp.edx.byte.dl = drivenum;
209 BOP_DOINT(bootops, 0x13, &rp);
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);
217 dprintf(("drive-present %x\n", drivenum));
218 return (1);
221 static void
222 reset_disk(uchar_t drivenum)
224 struct bop_regs rp = {0};
225 int status;
227 rp.eax.byte.ah = 0x0; /* reset disk */
228 rp.edx.byte.dl = drivenum;
230 BOP_DOINT(bootops, 0x13, &rp);
232 status = rp.eax.byte.ah;
234 if (((rp.eflags & PS_C) != 0) || status != 0)
235 dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
236 status));
239 /* Get first block */
240 static int
241 read_firstblock(uchar_t drivenum)
244 struct bop_regs rp = {0};
245 caddr_t bufp;
246 uchar_t status;
247 int i, index;
250 reset_disk(drivenum);
251 bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
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 */
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);
265 BOP_DOINT(bootops, 0x13, &rp);
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);
273 dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
274 *(uint32_t *)(bufp +0x1b8)));
276 index = drivenum - 0x80;
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);
282 return (1);
285 static int
286 is_eltorito(uchar_t drivenum)
288 struct bop_regs rp = {0};
289 fn4b_t *bufp;
290 extern struct bootops *bootops;
291 int i;
293 dprintf(("In is_eltorito\n"));
295 bufp = (fn4b_t *)BIOS_RES_BUFFER_ADDR;
298 * We cannot use bzero here as we're initializing data
299 * at an address below kernel base.
301 for (i = 0; i < sizeof (*bufp); i++)
302 ((uchar_t *)bufp)[i] = 0;
304 bufp->pkt_size = sizeof (*bufp);
305 rp.eax.word.ax = 0x4b01;
306 rp.edx.byte.dl = drivenum;
308 rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
309 rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
311 BOP_DOINT(bootops, 0x13, &rp);
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);
320 if (prom_debug)
321 prom_printf("INT13 FN4B01 mtype => %x", bufp->boot_mtype);
323 return (1);