xref: /netbsd/sys/arch/i386/stand/lib/bios_disk.S (revision bf9ec67e)
1/*	$NetBSD: bios_disk.S,v 1.8 2000/05/21 16:59:28 perry Exp $	*/
2
3/*
4 * Ported to boot 386BSD by Julian Elischer (julian@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@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
37Permission to use, copy, modify, and distribute this software and
38its documentation for any purpose and without fee is hereby
39granted, provided that the above copyright notice appears in all
40copies and that both the copyright notice and this permission notice
41appear in supporting documentation, and that the name of Intel
42not be used in advertising or publicity pertaining to distribution
43of the software without specific, written prior permission.
44
45INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
46INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
47IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
48CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
49LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
50NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
51WITH 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#define	addr32	.byte 0x67
59#define	data32	.byte 0x66
60
61/*
62# BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
63#	Call with	%ah = 0x2
64#			%al = number of sectors
65#			%ch = cylinder
66#			%cl = sector
67#			%dh = head
68#			%dl = drive (0x80 for hard disk, 0x0 for floppy disk)
69#			%es:%bx = segment:offset of buffer
70#	Return:
71#			%al = 0x0 on success; err code on failure
72*/
73ENTRY(biosread)
74	pushl	%ebp
75	movl	%esp, %ebp
76	pushl	%ebx
77	push	%ecx
78	push	%edx
79	push	%esi
80	push	%edi
81
82	movb	16(%ebp), %dh
83	movw	12(%ebp), %cx
84	xchgb	%ch, %cl	# cylinder; the highest 2 bits of cyl is in %cl
85	rorb	$2, %cl
86	movb	20(%ebp), %al
87	orb	%al, %cl
88	incb	%cl		# sector; sec starts from 1, not 0
89	movb	8(%ebp), %dl	# device
90	movl	28(%ebp), %ebx	# offset
91				# prot_to_real will set %es to BOOTSEG
92
93	call	_C_LABEL(prot_to_real)	# enter real mode
94
95	movb	$0x2, %ah	# subfunction
96	addr32
97	movb	24(%ebp), %al	# number of sectors
98	int	$0x13
99	setc	%bl
100	movb	%ah, %bh	# save error code
101
102	data32
103	call	_C_LABEL(real_to_prot) # back to protected mode
104
105	xorl	%eax, %eax
106	movw	%bx, %ax	# return value in %ax
107
108	pop	%edi
109	pop	%esi
110	pop	%edx
111	pop	%ecx
112	popl	%ebx
113	popl	%ebp
114	ret
115
116/*
117#
118# get_diskinfo():  return a word that represents the
119#	max number of sectors, heads and cylinders for this device
120#
121*/
122
123ENTRY(get_diskinfo)
124	pushl	%ebp
125	movl	%esp, %ebp
126	push	%es
127	pushl	%ebx
128	push	%ecx
129	push	%edx
130	push	%esi
131	push	%edi
132
133	movb	8(%ebp), %dl		# diskinfo(drive #)
134
135	call	_C_LABEL(prot_to_real)	# enter real mode
136
137	movb	$0x08, %ah		# ask for disk info
138	int	$0x13
139	jnc	ok
140
141	/*
142	 * Urk.  Call failed.  It is not supported for floppies by old BIOS's.
143	 * Guess it's a 15-sector floppy.  Initialize all the registers for
144	 * documentation, although we only need head and sector counts.
145	 */
146#	subb	%ah, %ah		# %ax = 0
147#	movb	%ah, %bh		# %bh = 0
148#	movb	$2, %bl			# %bl bits 0-3 = drive type, 2 = 1.2M
149	movb	$79, %ch		# max track
150	movb	$15, %cl		# max sector
151	movb	$1, %dh			# max head
152#	movb	$1, %dl			# # floppy drives installed
153	# es:di = parameter table
154	# carry = 0
155
156ok:
157	data32
158	call	_C_LABEL(real_to_prot)	# back to protected mode
159
160	/* form a longword representing all this gunk */
161	shll	$8, %ecx
162	movb	%dh, %cl
163
164	movl	%ecx, %eax
165
166	pop	%edi
167	pop	%esi
168	pop	%edx
169	pop	%ecx
170	popl	%ebx
171	pop	%es
172	popl	%ebp
173	ret
174
175/*
176# int13_extension: check for availibility of int13 extensions.
177*/
178
179ENTRY(int13_extension)
180	pushl	%ebp
181	movl	%esp, %ebp
182	pushl	%ebx
183	pushl	%ecx
184	pushl	%edx
185	pushl	%esi
186	pushl	%edi
187
188	movb	8(%ebp), %dl		# drive #
189	movw	$0x55aa, %bx
190
191	call	_C_LABEL(prot_to_real)	# enter real mode
192
193	movb	$0x41, %ah		# ask for disk info
194	int	$0x13
195	setnc	%dl
196
197	data32
198	CALL	_C_LABEL(real_to_prot)	# switch back
199
200	xorl	%eax, %eax
201	movb	%dl, %al	# return value in %ax
202
203	cmpw	$0xaa55, %bx
204	sete	%dl
205	andb	%dl, %al
206
207	andb	%cl, %al
208
209	popl	%edi
210	popl	%esi
211	popl	%edx
212	popl	%ecx
213	popl	%ebx
214	popl	%ebp
215	ret
216
217/*
218# BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
219#	Call with	%ah = 0x42
220#			%ds:%si = parameter block
221#			%dl = drive (0x80 for hard disk, 0x0 for floppy disk)
222#	Return:
223#			%al = 0x0 on success; err code on failure
224*/
225ENTRY(biosextread)
226	pushl	%ebp
227	movl	%esp, %ebp
228	pushl	%ebx
229	push	%ecx
230	push	%edx
231	push	%esi
232	push	%edi
233
234	movb	8(%ebp), %dl	# device
235	movl	12(%ebp), %esi	# parameter block
236
237	call	_C_LABEL(prot_to_real)	# enter real mode
238
239	movb	$0x42, %ah	# subfunction
240	int	$0x13
241	setc	%bl
242	movb	%ah, %bh	# save error code
243
244	data32
245	call	_C_LABEL(real_to_prot) # back to protected mode
246
247	xorl	%eax, %eax
248	movw	%bx, %ax	# return value in %ax
249
250	pop	%edi
251	pop	%esi
252	pop	%edx
253	pop	%ecx
254	popl	%ebx
255	popl	%ebp
256	ret
257
258ENTRY(int13_getextinfo)
259	pushl	%ebp
260	movl	%esp, %ebp
261	pushl	%ebx
262	push	%ecx
263	push	%edx
264	push	%esi
265	push	%edi
266
267	movb	8(%ebp), %dl	# device
268	movl	12(%ebp), %esi	# parameter block
269	movl	$0x01a, (%esi)	# length (v 1.x)
270
271	call	_C_LABEL(prot_to_real)	# enter real mode
272
273	movb	$0x48, %ah	# subfunction
274	int	$0x13
275	setc	%bl
276
277	data32
278	call	_C_LABEL(real_to_prot) # back to protected mode
279
280	xorl	%eax, %eax
281	movb	%bl, %al	# return value in %ax
282
283	pop	%edi
284	pop	%esi
285	pop	%edx
286	pop	%ecx
287	popl	%ebx
288	popl	%ebp
289	ret
290