Home | History | Annotate | Line # | Download | only in lib
bios_disk.S revision 1.15
      1 /*	$NetBSD: bios_disk.S,v 1.15 2005/06/22 15:29:48 junyoung Exp $	*/
      2 
      3 /*
      4  * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
      5  *
      6  * Mach Operating System
      7  * Copyright (c) 1992, 1991 Carnegie Mellon University
      8  * All Rights Reserved.
      9  *
     10  * Permission to use, copy, modify and distribute this software and its
     11  * documentation is hereby granted, provided that both the copyright
     12  * notice and this permission notice appear in all copies of the
     13  * software, derivative works or modified versions, and any portions
     14  * thereof, and that both notices appear in supporting documentation.
     15  *
     16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     18  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     19  *
     20  * Carnegie Mellon requests users of this software to return to
     21  *
     22  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     23  *  School of Computer Science
     24  *  Carnegie Mellon University
     25  *  Pittsburgh PA 15213-3890
     26  *
     27  * any improvements or extensions that they make and grant Carnegie Mellon
     28  * the rights to redistribute these changes.
     29  */
     30 
     31 /*
     32   Copyright 1988, 1989, 1990, 1991, 1992
     33    by Intel Corporation, Santa Clara, California.
     34 
     35                 All Rights Reserved
     36 
     37 Permission to use, copy, modify, and distribute this software and
     38 its documentation for any purpose and without fee is hereby
     39 granted, provided that the above copyright notice appears in all
     40 copies and that both the copyright notice and this permission notice
     41 appear in supporting documentation, and that the name of Intel
     42 not be used in advertising or publicity pertaining to distribution
     43 of the software without specific, written prior permission.
     44 
     45 INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
     46 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
     47 IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
     48 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     49 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
     50 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
     51 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     52 */
     53 
     54 /* extracted from netbsd:sys/arch/i386/boot/bios.S */
     55 
     56 #include <machine/asm.h>
     57 
     58 /*
     59  * BIOS call "INT 0x13 Function 0x0" to reset the disk subsystem
     60  *	Call with	%ah = 0x0
     61  *			%dl = drive (0x80 for hard disk, 0x0 for floppy disk)
     62  *	Return:
     63  *			%al = 0x0 on success; err code on failure
     64  */
     65 ENTRY(biosdiskreset)
     66 	pushl	%ebp
     67 	movl	%esp, %ebp
     68 	pushl	%ebx
     69 	push	%edx
     70 	push	%edi
     71 
     72 	movb	8(%ebp), %dl	# device
     73 
     74 	call	_C_LABEL(prot_to_real)	# enter real mode
     75 	.code16
     76 
     77 	movb	$0x0, %ah	# subfunction
     78 	int	$0x13
     79 	setc	%bl
     80 	movb	%ah, %bh	# save error code
     81 
     82 	calll	_C_LABEL(real_to_prot) # back to protected mode
     83 	.code32
     84 
     85 	xorl	%eax, %eax
     86 	movw	%bx, %ax	# return value in %ax
     87 
     88 	pop	%edi
     89 	pop	%edx
     90 	popl	%ebx
     91 	popl	%ebp
     92 	ret
     93 
     94 /*
     95  * BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
     96  *	Call with	%ah = 0x2
     97  *			%al = number of sectors
     98  *			%ch = cylinder
     99  *			%cl = sector
    100  *			%dh = head
    101  *			%dl = drive (0x80 for hard disk, 0x0 for floppy disk)
    102  *			%es:%bx = segment:offset of buffer
    103  *	Return:
    104  *			%al = 0x0 on success; err code on failure
    105  *
    106  * biosread( dev, cyl, head, sect, count, buff_addr );
    107  *
    108  *  Note: On failure, you must reset the disk with biosdiskreset() before
    109  *        sending another command.
    110  */
    111 ENTRY(biosread)
    112 	pushl	%ebp
    113 	movl	%esp, %ebp
    114 	pushl	%ebx
    115 	push	%ecx
    116 	push	%edx
    117 	push	%esi
    118 	push	%edi
    119 
    120 	movb	16(%ebp), %dh
    121 	movw	12(%ebp), %cx
    122 	xchgb	%ch, %cl	# cylinder; the highest 2 bits of cyl is in %cl
    123 	rorb	$2, %cl
    124 	movb	20(%ebp), %al
    125 	orb	%al, %cl
    126 	incb	%cl		# sector; sec starts from 1, not 0
    127 	movb	8(%ebp), %dl	# device
    128 	movl	28(%ebp), %ebx	# buffer address (may be >64k)
    129 	movb	24(%ebp), %al	# number of sectors
    130 
    131 	call	_C_LABEL(prot_to_real)	# enter real mode
    132 	.code16
    133 
    134 	push	%bx
    135 	shrl	$4, %ebx	# max segment
    136 	mov	%ds, %si
    137 	add	%si, %bx
    138 	mov	%bx, %es	# %es:%bx now valid buffer address
    139 	pop	%bx
    140 	and	$0xf, %bx	# and min offset - to avoid overrun
    141 
    142 	movb	$0x2, %ah	# subfunction
    143 	int	$0x13
    144 	setc	%al		# error code is in %ah
    145 
    146 	calll	_C_LABEL(real_to_prot) # back to protected mode
    147 	.code32
    148 
    149 	andl	$0xffff, %eax
    150 
    151 	pop	%edi
    152 	pop	%esi
    153 	pop	%edx
    154 	pop	%ecx
    155 	popl	%ebx
    156 	popl	%ebp
    157 	ret
    158 
    159 /*
    160  *
    161  * get_diskinfo():  return a word that represents the
    162  *	max number of sectors, heads and cylinders for this device
    163  *
    164  */
    165 ENTRY(get_diskinfo)
    166 	pushl	%ebp
    167 	movl	%esp, %ebp
    168 	push	%es
    169 	pushl	%ebx
    170 	push	%ecx
    171 	push	%edx
    172 	push	%esi
    173 	push	%edi
    174 
    175 	movb	8(%ebp), %dl		# diskinfo(drive #)
    176 
    177 	call	_C_LABEL(prot_to_real)	# enter real mode
    178 	.code16
    179 
    180 	movb	$0x08, %ah		# ask for disk info
    181 	int	$0x13
    182 	jnc	ok
    183 
    184 	/*
    185 	 * Urk.  Call failed.  It is not supported for floppies by old BIOS's.
    186 	 * Guess it's a 15-sector floppy.  Initialize all the registers for
    187 	 * documentation, although we only need head and sector counts.
    188 	 */
    189 #	subb	%ah, %ah		# %ax = 0
    190 #	movb	%ah, %bh		# %bh = 0
    191 #	movb	$2, %bl			# %bl bits 0-3 = drive type, 2 = 1.2M
    192 	movb	$79, %ch		# max track
    193 	movb	$15, %cl		# max sector
    194 	movb	$1, %dh			# max head
    195 #	movb	$1, %dl			# # floppy drives installed
    196 	# es:di = parameter table
    197 	# carry = 0
    198 
    199 ok:
    200 	calll	_C_LABEL(real_to_prot)	# back to protected mode
    201 	.code32
    202 
    203 	/* form a longword representing all this gunk */
    204 	shll	$8, %ecx
    205 	movb	%dh, %cl
    206 
    207 	movl	%ecx, %eax
    208 
    209 	pop	%edi
    210 	pop	%esi
    211 	pop	%edx
    212 	pop	%ecx
    213 	popl	%ebx
    214 	pop	%es
    215 	popl	%ebp
    216 	ret
    217 
    218 /*
    219  * int13_extension: check for availibility of int13 extensions.
    220  */
    221 ENTRY(int13_extension)
    222 	pushl	%ebp
    223 	movl	%esp, %ebp
    224 	pushl	%ebx
    225 	pushl	%ecx
    226 	pushl	%edx
    227 	pushl	%esi
    228 	pushl	%edi
    229 
    230 	movb	8(%ebp), %dl		# drive #
    231 	movw	$0x55aa, %bx
    232 
    233 	call	_C_LABEL(prot_to_real)	# enter real mode
    234 	.code16
    235 
    236 	movb	$0x41, %ah		# ask for disk info
    237 	int	$0x13
    238 	setnc	%dl
    239 
    240 	calll	_C_LABEL(real_to_prot)	# switch back
    241 	.code32
    242 
    243 	xorl	%eax, %eax
    244 	movb	%dl, %al	# return value in %ax
    245 
    246 	cmpw	$0xaa55, %bx
    247 	sete	%dl
    248 	andb	%dl, %al
    249 
    250 	andb	%cl, %al
    251 
    252 	popl	%edi
    253 	popl	%esi
    254 	popl	%edx
    255 	popl	%ecx
    256 	popl	%ebx
    257 	popl	%ebp
    258 	ret
    259 
    260 /*
    261  * BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
    262  *	Call with	%ah = 0x42
    263  *			%ds:%si = parameter block (data buffer address
    264  *				must be a real mode physical address).
    265  *			%dl = drive (0x80 for hard disk, 0x0 for floppy disk)
    266  *	Return:
    267  *			%al = 0x0 on success; err code on failure
    268  */
    269 ENTRY(biosextread)
    270 	pushl	%ebp
    271 	movl	%esp, %ebp
    272 	pushl	%ebx
    273 	push	%ecx
    274 	push	%edx
    275 	push	%esi
    276 	push	%edi
    277 
    278 	movb	8(%ebp), %dl	# device
    279 	movl	12(%ebp), %esi	# parameter block
    280 
    281 	call	_C_LABEL(prot_to_real)	# enter real mode
    282 	.code16
    283 
    284 	movl	%esi, %eax
    285 	shrl	$4, %eax
    286 	movw	%ds, %bx
    287 	addw	%bx, %ax
    288 	movw	%ax, %ds
    289 	andw	$0xf, %si
    290 
    291 	movb	$0x42, %ah	# subfunction
    292 	int	$0x13
    293 	setc	%bl
    294 	movb	%ah, %bh	# save error code
    295 
    296 	calll	_C_LABEL(real_to_prot) # back to protected mode
    297 	.code32
    298 
    299 	xorl	%eax, %eax
    300 	movw	%bx, %ax	# return value in %ax
    301 
    302 	pop	%edi
    303 	pop	%esi
    304 	pop	%edx
    305 	pop	%ecx
    306 	popl	%ebx
    307 	popl	%ebp
    308 	ret
    309 
    310 ENTRY(int13_getextinfo)
    311 	pushl	%ebp
    312 	movl	%esp, %ebp
    313 	pushl	%ebx
    314 	push	%ecx
    315 	push	%edx
    316 	push	%esi
    317 	push	%edi
    318 
    319 	movb	8(%ebp), %dl	# device
    320 	movl	12(%ebp), %esi	# parameter block
    321 
    322 	call	_C_LABEL(prot_to_real)	# enter real mode
    323 	.code16
    324 
    325 	movl	%esi, %eax
    326 	shrl	$4, %eax
    327 	andw	$0xf, %si
    328 	movw	%ds, %bx
    329 	addw	%bx, %ax
    330 	movw	%ax, %ds
    331 
    332 	movb	$0x48, %ah	# subfunction
    333 	int	$0x13
    334 	setc	%bl
    335 
    336 	calll	_C_LABEL(real_to_prot) # back to protected mode
    337 	.code32
    338 
    339 	xorl	%eax, %eax
    340 	movb	%bl, %al	# return value in %ax
    341 
    342 	pop	%edi
    343 	pop	%esi
    344 	pop	%edx
    345 	pop	%ecx
    346 	popl	%ebx
    347 	popl	%ebp
    348 	ret
    349