summaryrefslogtreecommitdiff
path: root/sys/arch/i386/stand/lib/dos_file.S
blob: 1c5547d6516b06d49e568f5292e48d236c47b647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*	$NetBSD: dos_file.S,v 1.6 2009/11/21 11:52:57 dsl Exp $	*/
	
/* extracted from Tor Egge's patches for NetBSD boot */

#include <machine/asm.h>

/*
# MSDOS call "INT 0x21 Function 0x3d" to open a file.
# Call with	%ah = 0x3d
# 		%al = 0x0  (access and sharing modes)
#		%ds:%dx = ASCIZ filename
#		%cl = attribute mask of files to look for
*/

	.globl _C_LABEL(doserrno)
_C_LABEL(doserrno):	.long 1

ENTRY(dosopen)
	.code32
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%edx
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	
	movl	0x8(%ebp), %edx		# File name.

	call	_C_LABEL(prot_to_real)	# enter real mode
	.code16

	push	%ds
	movl	%edx, %eax
	shrl	$4, %eax
	mov	%ds, %si
	add	%si, %ax
	mov	%ax, %ds
	and	$0xf, %dx

	movb	$0x3d, %ah		# Open existing file.
	movb	$0x0 , %al		# ro

	sti
	int	$0x21
	cli
	pop	%ds

	jnc	ok1
	mov	%ax, _C_LABEL(doserrno)
	movl	$-1, %edx
	jmp err1
ok1:
	movl	$0,%edx
	mov	%ax, %dx
err1:
	calll	_C_LABEL(real_to_prot) # back to protected mode
	.code32

	movl	%edx, %eax		# return value in %eax

	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%edx
	popl	%ebp
	ret

ENTRY(dosread)
	.code32
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	pushl	%ecx
	pushl	%edx
	pushl	%esi
	pushl	%edi
	
	movl	0x8(%ebp), %ebx		# File handle
	movl	0xc(%ebp), %edx		# Buffer.
	movl	0x10(%ebp), %ecx	# Bytes to read

	call	_C_LABEL(prot_to_real)	# enter real mode
	.code16

	push	%ds
	movl	%edx, %eax
	shrl	$4, %eax
	mov	%ds, %si
	add	%si, %ax
	mov	%ax, %ds
	and	$0xf, %dx

	movb	$0x3f, %ah		# Read from file or device

	sti
	int	$0x21
	cli
	pop	%ds

	jnc	ok2
	mov	%ax, _C_LABEL(doserrno)
	movl	$-1, %edx
	jmp	err2
ok2:
	movl	$0,%edx
	mov	%ax, %dx
err2:
	calll	_C_LABEL(real_to_prot)	# back to protected mode
	.code32

	movl	%edx, %eax		# return value in %eax

	popl	%edi
	popl	%esi
	popl	%edx
	popl	%ecx
	popl	%ebx
	popl	%ebp
	ret

ENTRY(dosclose)
	.code32
	pushl %ebp
	movl  %esp, %ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	
	movl	0x8(%ebp), %ebx		# File handle

	call	_C_LABEL(prot_to_real)	# enter real mode
	.code16

	movb	$0x3e, %ah		# Close file.

	sti
	int	$0x21
	cli

	jnc	ok3
	mov	%ax, _C_LABEL(doserrno)
	movl	$-1, %ebx
	jmp	err3
ok3:
	movl	$0, %ebx
err3:
	calll	_C_LABEL(real_to_prot) # back to protected mode
	.code32

	movl	%ebx, %eax		# return value in %eax

	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret

ENTRY(dosseek)
	.code32
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	pushl	%ecx
	pushl	%edx
	pushl	%esi
	pushl	%edi
	
	movl	0x8(%ebp), %ebx		# File handle
	movl	0xc(%ebp), %ecx		# Offset
	movl	0x10(%ebp) , %edx	# whence

	call	_C_LABEL(prot_to_real)	# enter real mode
	.code16

	movb	$0x42, %ah		# Seek
	movb	%dl, %al		# whence
	mov	%cx, %dx		#offs lo
	shrl	$0x10, %ecx		#offs hi

	sti
	int	$0x21
	cli

	jnc	ok4
	mov	%ax, _C_LABEL(doserrno)
	movl	$-1, %edx
	jmp	err4
ok4:
	shll	$0x10, %edx	#new ofs hi
	mov	%ax, %dx	#new ofs lo
err4:
	calll	_C_LABEL(real_to_prot) # back to protected mode
	.code32

	movl	%edx, %eax		# return value in %eax

	popl	%edi
	popl	%esi
	popl	%edx
	popl	%ecx
	popl	%ebx
	popl	%ebp
	ret