xref: /illumos-gate/usr/src/uts/i86pc/os/biosdisk.c (revision fdbb9738)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5843e1988Sjohnlev  * Common Development and Distribution License (the "License").
6843e1988Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22721df51dSSeth Goldberg  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/controlregs.h>
297c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
307c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
317c478bd9Sstevel@tonic-gate #include <sys/bootregs.h>
327c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/promif.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
387c478bd9Sstevel@tonic-gate #include <sys/biosdisk.h>
397c478bd9Sstevel@tonic-gate #include <sys/psw.h>
40843e1988Sjohnlev #if defined(__xpv)
41843e1988Sjohnlev #include <sys/hypervisor.h>
42843e1988Sjohnlev #endif
437c478bd9Sstevel@tonic-gate 
44721df51dSSeth Goldberg extern int prom_debug;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* hard code realmode memory address for now */
477c478bd9Sstevel@tonic-gate #define	BIOS_RES_BUFFER_ADDR		0x7000
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	BIOSDEV_NUM	8
507c478bd9Sstevel@tonic-gate #define	STARTING_DRVNUM	0x80
517c478bd9Sstevel@tonic-gate #define	FP_OFF(fp) (((uintptr_t)(fp)) & 0xFFFF)
527c478bd9Sstevel@tonic-gate #define	FP_SEG(fp) ((((uintptr_t)(fp)) >> 16) & 0xFFFF)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef DEBUG
557c478bd9Sstevel@tonic-gate int biosdebug = 0;
567c478bd9Sstevel@tonic-gate #define	dprintf(fmt) \
577c478bd9Sstevel@tonic-gate 	if (biosdebug) \
587c478bd9Sstevel@tonic-gate 		prom_printf fmt
597c478bd9Sstevel@tonic-gate #else
607c478bd9Sstevel@tonic-gate #define	dprintf(fmt)
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate biosdev_data_t biosdev_info[BIOSDEV_NUM]; /* from 0x80 to 0x87 */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int bios_check_extension_present(uchar_t);
677c478bd9Sstevel@tonic-gate static int get_dev_params(uchar_t);
687c478bd9Sstevel@tonic-gate static int read_firstblock(uchar_t drivenum);
697c478bd9Sstevel@tonic-gate static int drive_present(uchar_t drivenum);
707c478bd9Sstevel@tonic-gate static void reset_disk(uchar_t drivenum);
71721df51dSSeth Goldberg static int is_eltorito(uchar_t drivenum);
727c478bd9Sstevel@tonic-gate 
73*63d0f4f1SToomas Soome #if !defined(__xpv)
747c478bd9Sstevel@tonic-gate void
startup_bios_disk()757c478bd9Sstevel@tonic-gate startup_bios_disk()
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	uchar_t drivenum;
787c478bd9Sstevel@tonic-gate 	int got_devparams = 0;
797c478bd9Sstevel@tonic-gate 	int got_first_block = 0;
807c478bd9Sstevel@tonic-gate 	uchar_t	name[20];
817c478bd9Sstevel@tonic-gate 	dev_info_t	*devi;
82721df51dSSeth Goldberg 	int extensions;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	for (drivenum = 0x80; drivenum < (0x80 + BIOSDEV_NUM); drivenum++) {
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		if (!drive_present(drivenum))
877c478bd9Sstevel@tonic-gate 			continue;
887c478bd9Sstevel@tonic-gate 
89721df51dSSeth Goldberg 		extensions = bios_check_extension_present(drivenum);
90721df51dSSeth Goldberg 
91721df51dSSeth Goldberg 		/*
92721df51dSSeth Goldberg 		 * If we're booting from an Eltorito CD/DVD image, there's
93721df51dSSeth Goldberg 		 * no need to get the device parameters or read the first block
94721df51dSSeth Goldberg 		 * because we'll never install onto this device.
95721df51dSSeth Goldberg 		 */
96721df51dSSeth Goldberg 		if (extensions && is_eltorito(drivenum))
97721df51dSSeth Goldberg 			continue;
98721df51dSSeth Goldberg 
99721df51dSSeth Goldberg 		if (extensions && get_dev_params(drivenum))
100721df51dSSeth Goldberg 			got_devparams = 1;
101721df51dSSeth Goldberg 		else
102721df51dSSeth Goldberg 			got_devparams = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		if ((got_first_block = read_firstblock(drivenum)) == 0) {
1057c478bd9Sstevel@tonic-gate 			/* retry */
1067c478bd9Sstevel@tonic-gate 			got_first_block = read_firstblock(drivenum);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		if (got_devparams || got_first_block) {
1107c478bd9Sstevel@tonic-gate 			(void) sprintf((char *)name, "biosdev-0x%x", drivenum);
1117c478bd9Sstevel@tonic-gate 			devi = ddi_root_node();
1127c478bd9Sstevel@tonic-gate 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1137c478bd9Sstevel@tonic-gate 			    devi, (char *)name,
1147c478bd9Sstevel@tonic-gate 			    (uchar_t *)&biosdev_info[drivenum - 0x80],
1157c478bd9Sstevel@tonic-gate 			    sizeof (biosdev_data_t));
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate }
119*63d0f4f1SToomas Soome #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static int
bios_check_extension_present(uchar_t drivenum)1227c478bd9Sstevel@tonic-gate bios_check_extension_present(uchar_t drivenum)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
1257c478bd9Sstevel@tonic-gate 	extern struct bootops		*bootops;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	rp.eax.word.ax = 0x4100;
1287c478bd9Sstevel@tonic-gate 	rp.ebx.word.bx = 0x55AA;
1297c478bd9Sstevel@tonic-gate 	rp.edx.word.dx = drivenum;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* make sure we have extension support */
1327c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || (rp.ebx.word.bx != 0xAA55)) {
1357c478bd9Sstevel@tonic-gate 		dprintf(("bios_check_extension_present int13 fn 41 "
1367c478bd9Sstevel@tonic-gate 		    "failed %d bx = %x\n", rp.eflags, rp.ebx.word.bx));
1377c478bd9Sstevel@tonic-gate 		return (0);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((rp.ecx.word.cx & 0x7) == 0) {
1417c478bd9Sstevel@tonic-gate 		dprintf(("bios_check_extension_present get device parameters "
1427c478bd9Sstevel@tonic-gate 		    "not supported cx = %x\n", rp.ecx.word.cx));
1437c478bd9Sstevel@tonic-gate 		return (0);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return (1);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static int
get_dev_params(uchar_t drivenum)1507c478bd9Sstevel@tonic-gate get_dev_params(uchar_t drivenum)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
1537c478bd9Sstevel@tonic-gate 	fn48_t	 *bufp;
1547c478bd9Sstevel@tonic-gate 	extern struct bootops		*bootops;
1557c478bd9Sstevel@tonic-gate 	int i;
1567c478bd9Sstevel@tonic-gate 	int index;
15703859504Sjg 	uchar_t *tmp;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	dprintf(("In get_dev_params\n"));
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	bufp = (fn48_t *)BIOS_RES_BUFFER_ADDR;
1627c478bd9Sstevel@tonic-gate 
16303859504Sjg 	/*
16403859504Sjg 	 * We cannot use bzero here as we're initializing data
16503859504Sjg 	 * at an address below kernel base.
16603859504Sjg 	 */
1677c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (*bufp); i++)
1687c478bd9Sstevel@tonic-gate 		((uchar_t *)bufp)[i] = 0;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	bufp->buflen = sizeof (*bufp);
1717c478bd9Sstevel@tonic-gate 	rp.eax.word.ax = 0x4800;
1727c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
1757c478bd9Sstevel@tonic-gate 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((rp.eflags & PS_C) != 0) {
1807c478bd9Sstevel@tonic-gate 		dprintf(("EDD FAILED on drive eflag = %x ah= %x\n",
1817c478bd9Sstevel@tonic-gate 		    rp.eflags, rp.eax.byte.ah));
1827c478bd9Sstevel@tonic-gate 		return (0);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	index = drivenum - 0x80;
1867c478bd9Sstevel@tonic-gate 	biosdev_info[index].edd_valid = 1;
1877c478bd9Sstevel@tonic-gate 
18803859504Sjg 	/*
18903859504Sjg 	 * Some compilers turn a structure copy into a call
19003859504Sjg 	 * to memcpy.  Since we are copying data below kernel
19103859504Sjg 	 * base intentionally, and memcpy asserts that's not
19203859504Sjg 	 * the case, we do the copy manually here.
19303859504Sjg 	 */
19403859504Sjg 	tmp = (uchar_t *)&biosdev_info[index].fn48_dev_params;
19503859504Sjg 	for (i = 0; i < sizeof (*bufp); i++)
19603859504Sjg 		tmp[i] = ((uchar_t *)bufp)[i];
1977c478bd9Sstevel@tonic-gate 
19803859504Sjg 	return (1);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static int
drive_present(uchar_t drivenum)2027c478bd9Sstevel@tonic-gate drive_present(uchar_t drivenum)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x8;	/* get params */
2077c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || rp.eax.byte.ah != 0) {
2127c478bd9Sstevel@tonic-gate 		dprintf(("drive not present drivenum %x eflag %x ah %x\n",
213843e1988Sjohnlev 		    drivenum, rp.eflags, rp.eax.byte.ah));
2147c478bd9Sstevel@tonic-gate 		return (0);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	dprintf(("drive-present %x\n", drivenum));
2187c478bd9Sstevel@tonic-gate 	return (1);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static void
reset_disk(uchar_t drivenum)2227c478bd9Sstevel@tonic-gate reset_disk(uchar_t drivenum)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
2257c478bd9Sstevel@tonic-gate 	int status;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x0;   /* reset disk */
2287c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0)
2357c478bd9Sstevel@tonic-gate 		dprintf(("Bad disk reset driv %x, status %x\n", drivenum,
2367c478bd9Sstevel@tonic-gate 		    status));
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /* Get first block */
2407c478bd9Sstevel@tonic-gate static int
read_firstblock(uchar_t drivenum)2417c478bd9Sstevel@tonic-gate read_firstblock(uchar_t drivenum)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	struct bop_regs rp = {0};
2457c478bd9Sstevel@tonic-gate 	caddr_t	 bufp;
2467c478bd9Sstevel@tonic-gate 	uchar_t status;
2477c478bd9Sstevel@tonic-gate 	int i, index;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	reset_disk(drivenum);
2517c478bd9Sstevel@tonic-gate 	bufp = (caddr_t)BIOS_RES_BUFFER_ADDR;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	rp.eax.byte.ah = 0x2; 	/* Read disk */
2557c478bd9Sstevel@tonic-gate 	rp.eax.byte.al = 1;	/* nsect */
2567c478bd9Sstevel@tonic-gate 	rp.ecx.byte.ch = 0;	/* cyl & 0xff */
2577c478bd9Sstevel@tonic-gate 	rp.ecx.byte.cl = 1;	/* cyl >> 2 & 0xc0 (sector number) */
2587c478bd9Sstevel@tonic-gate 	rp.edx.byte.dh = 0;	/* head */
2597c478bd9Sstevel@tonic-gate 	rp.edx.byte.dl = drivenum;	/* drivenum */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* es:bx is buf address */
2627c478bd9Sstevel@tonic-gate 	rp.ebx.word.bx = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
2637c478bd9Sstevel@tonic-gate 	rp.es = FP_SEG((uint_t)(uintptr_t)bufp);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	BOP_DOINT(bootops, 0x13, &rp);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	status = rp.eax.byte.ah;
2687c478bd9Sstevel@tonic-gate 	if (((rp.eflags & PS_C) != 0) || status != 0) {
2697c478bd9Sstevel@tonic-gate 		dprintf(("read_firstblock AH not clear %x \n", status));
2707c478bd9Sstevel@tonic-gate 		return (0);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	dprintf(("drivenum %x uid at 0x1b8 is %x\n", drivenum,
2747c478bd9Sstevel@tonic-gate 	    *(uint32_t *)(bufp +0x1b8)));
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	index = drivenum - 0x80;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	biosdev_info[index].first_block_valid = 1;
2797c478bd9Sstevel@tonic-gate 	for (i = 0; i < 512; i++)
2807c478bd9Sstevel@tonic-gate 		biosdev_info[index].first_block[i] = *((uchar_t *)bufp + i);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (1);
2837c478bd9Sstevel@tonic-gate }
284721df51dSSeth Goldberg 
285721df51dSSeth Goldberg static int
is_eltorito(uchar_t drivenum)286721df51dSSeth Goldberg is_eltorito(uchar_t drivenum)
287721df51dSSeth Goldberg {
288721df51dSSeth Goldberg 	struct bop_regs rp = {0};
289721df51dSSeth Goldberg 	fn4b_t	 *bufp;
290721df51dSSeth Goldberg 	extern struct bootops		*bootops;
291721df51dSSeth Goldberg 	int i;
292721df51dSSeth Goldberg 
293721df51dSSeth Goldberg 	dprintf(("In is_eltorito\n"));
294721df51dSSeth Goldberg 
295721df51dSSeth Goldberg 	bufp = (fn4b_t *)BIOS_RES_BUFFER_ADDR;
296721df51dSSeth Goldberg 
297721df51dSSeth Goldberg 	/*
298721df51dSSeth Goldberg 	 * We cannot use bzero here as we're initializing data
299721df51dSSeth Goldberg 	 * at an address below kernel base.
300721df51dSSeth Goldberg 	 */
301721df51dSSeth Goldberg 	for (i = 0; i < sizeof (*bufp); i++)
302721df51dSSeth Goldberg 		((uchar_t *)bufp)[i] = 0;
303721df51dSSeth Goldberg 
304721df51dSSeth Goldberg 	bufp->pkt_size = sizeof (*bufp);
305721df51dSSeth Goldberg 	rp.eax.word.ax = 0x4b01;
306721df51dSSeth Goldberg 	rp.edx.byte.dl = drivenum;
307721df51dSSeth Goldberg 
308721df51dSSeth Goldberg 	rp.esi.word.si = (uint16_t)FP_OFF((uint_t)(uintptr_t)bufp);
309721df51dSSeth Goldberg 	rp.ds = FP_SEG((uint_t)(uintptr_t)bufp);
310721df51dSSeth Goldberg 
311721df51dSSeth Goldberg 	BOP_DOINT(bootops, 0x13, &rp);
312721df51dSSeth Goldberg 
313721df51dSSeth Goldberg 	if ((rp.eflags & PS_C) != 0 || bufp->drivenum != drivenum) {
314721df51dSSeth Goldberg 		dprintf(("fn 0x4b01 FAILED on drive "
315721df51dSSeth Goldberg 		    "eflags=%x ah=%x drivenum=%x\n",
316721df51dSSeth Goldberg 		    rp.eflags, rp.eax.byte.ah, bufp->drivenum));
317721df51dSSeth Goldberg 		return (0);
318721df51dSSeth Goldberg 	}
319721df51dSSeth Goldberg 
320721df51dSSeth Goldberg 	if (prom_debug)
321721df51dSSeth Goldberg 		prom_printf("INT13 FN4B01 mtype => %x", bufp->boot_mtype);
322721df51dSSeth Goldberg 
323721df51dSSeth Goldberg 	return (1);
324721df51dSSeth Goldberg }
325