1/* $MirOS: src/sys/arch/i386/stand/libsa/dosfslo.S,v 1.1 2009/12/26 15:09:02 tg Exp $ */
2
3/*-
4 * Copyright (c) 2009
5 *	Thorsten Glaser <tg@mirbsd.org>
6 *
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
12 *
13 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 * the utmost extent permitted by applicable law, neither express nor
15 * implied; without malicious intent or gross negligence. In no event
16 * may a licensor, author or contributor be held liable for indirect,
17 * direct, other damage, loss, or other issues arising in any way out
18 * of dealing in the work, even if advised of the possibility of such
19 * damage or existence of a defect, except proven that it results out
20 * of said person's immediate fault when using the work as intended.
21 */
22
23	.intel_syntax noprefix
24	.text
25	.code32
26
27	/* extern */
28	.globl	bounce_buf
29
30/* *bounce_buf = ASCIZ filename; returns effff e=error f=flags */
31/* uint32_t dosfs__stat(void); */
32
33	.globl	dosfs__stat
34dosfs__stat:
35	push	ebp
36	mov	ebp,esp
37	pushfd
38	push	ebx
39	push	esi
40	push	edi
41	/* assert: bounce_buf is PARA aligned */
42	mov	ebx,offset bounce_buf
43	xor	edx,edx
44	shr	ebx,4
45
46	/** state:
47	 * ELF i386 ABI registers saved
48	 * bx:edx = seg:ofs bounce_buf
49	 */
50
51	call	prot_to_real
52	.code16
53	sti
54
55	mov	ds,bx
56	mov	ax,0x4300
57	xor	bx,bx
58	push	bx
59	int	0x21
60	pop	bx
61	/** state:
62	 * bx = 0000
63	 * CY = error?
64	 * ax = error code (if CY)
65	 * cx = attributes (if NC)
66	 */
67	jnc	1f
68	inc	bx
69	mov	cx,ax
701:	shl	ebx,16
71	mov	bx,cx
72
73	/** state:
74	 * ebx = 000evvvv
75	 * + e = 1 if error, 0 otherwise
76	 * + v = error code if error, attributes otherwise
77	 */
78
79	call	real_to_prot
80	.code32
81
82	mov	eax,ebx
83	pop	edi
84	pop	esi
85	pop	ebx
86	popfd
87	pop	ebp
88	ret
89
90
91/* *bounce_buf = ASCIZ filename; returns ehhhh e=error h=handle */
92/* uint32_t dosfs__open(void); */
93
94	.globl	dosfs__open
95dosfs__open:
96	push	ebp
97	mov	ebp,esp
98	pushfd
99	push	ebx
100	push	esi
101	push	edi
102	/* assert: bounce_buf is PARA aligned */
103	mov	ebx,offset bounce_buf
104	xor	edx,edx
105	shr	ebx,4
106
107	/** state:
108	 * ELF i386 ABI registers saved
109	 * bx:edx = seg:ofs bounce_buf
110	 */
111
112	call	prot_to_real
113	.code16
114	sti
115
116	mov	ds,bx
117	mov	ax,0x3D00
118	xor	cx,cx
119	push	cx
120	int	0x21
121	pop	bx
122	/** state:
123	 * bx = 0000
124	 * CY = error?
125	 * ax = handle (if NC) or error code (if CY)
126	 */
127	adc	bx,0		/* add 0 + CF to bx */
128	shl	ebx,16
129	mov	bx,ax
130
131	/** state:
132	 * ebx = 000evvvv
133	 * + e = 1 if error, 0 otherwise
134	 * + v = error code if error, handle otherwise
135	 */
136
137	call	real_to_prot
138	.code32
139
140	mov	eax,ebx
141	pop	edi
142	pop	esi
143	pop	ebx
144	popfd
145	pop	ebp
146	ret
147
148
149/* void dosfs__close(uint32_t handle); */
150
151	.globl	dosfs__close
152dosfs__close:
153	push	ebp
154	mov	ebp,esp
155	pushfd
156	pushad
157	mov	ebx,[ebp+8]
158
159	/** state:
160	 * all registers saved
161	 * bx = handle
162	 */
163
164	call	prot_to_real
165	.code16
166	sti
167
168	mov	ah,0x3E
169	int	0x21
170
171	call	real_to_prot
172	.code32
173
174	popad
175	popfd
176	pop	ebp
177	ret
178
179
180/* uint32_t dosfs__read(uint32_t handle, void *buf, uint32_t n); */
181
182	.globl	dosfs__read
183dosfs__read:
184	push	ebp
185	mov	ebp,esp
186	pushfd
187	push	ebx
188	push	esi
189	push	edi
190	mov	ebx,[ebp+8]
191	mov	edx,[ebp+12]
192	mov	ecx,[ebp+16]
193
194	/** state:
195	 * ELF i386 ABI registers saved
196	 * bx = handle
197	 * cx = count
198	 * edx = flat offset buf
199	 */
200
201	call	prot_to_real
202	.code16
203	sti
204
205	push	dx
206	shr	edx,4
207	mov	ds,dx
208	pop	dx
209	and	dx,0x000F
210	/** state:
211	 * bx = handle
212	 * cx = count
213	 * ds = seg buf
214	 * edx = ofs buf (bit 4..31 cleared)
215	 */
216	mov	ah,0x3F
217	int	0x21
218	jnc	1f
219	xor	ax,ax
220	dec	ax
2211:	movzx	ebx,ax
222
223	/** state:
224	 * ebx = 0x0000FFFF (error) or count
225	 */
226
227	call	real_to_prot
228	.code32
229
230	mov	eax,ebx
231	pop	edi
232	pop	esi
233	pop	ebx
234	popfd
235	pop	ebp
236	ret
237
238
239/* int32_t dosfs__seek(uint32_t handle, uint32_t whence, int32_t ofs); */
240
241	.globl	dosfs__seek
242dosfs__seek:
243	push	ebp
244	mov	ebp,esp
245	pushfd
246	push	ebx
247	push	esi
248	push	edi
249	mov	ebx,[ebp+8]
250	mov	ecx,[ebp+12]
251	mov	edx,[ebp+16]
252
253	/** state:
254	 * ELF i386 ABI registers saved
255	 * bx = handle
256	 * ecx = signed 32-bit file offset
257	 * dx = whence
258	 */
259
260	call	prot_to_real
261	.code16
262	sti
263
264	mov	ah,0x42
265	mov	al,dl
266	mov	dx,cx
267	shr	ecx,16
268	/** state:
269	 * al = whence
270	 * bx = handle
271	 * cx:dx = signed 16+16 bit file offset
272	 */
273	int	0x21
274	jnc	1f
275	xor	ax,ax
276	dec	ax
277	mov	dx,ax
2781:	shl	edx,16
279	mov	dx,ax
280
281	/** state:
282	 * edx = signed 32-bit file offset, or (-1) on error
283	 */
284
285	call	real_to_prot
286	.code32
287
288	mov	eax,edx
289	pop	edi
290	pop	esi
291	pop	ebx
292	popfd
293	pop	ebp
294	ret
295