1*58a2b000SEvgeniy Ivanov/* $NetBSD: dos_file.S,v 1.6 2009/11/21 11:52:57 dsl Exp $ */ 2*58a2b000SEvgeniy Ivanov 3*58a2b000SEvgeniy Ivanov/* extracted from Tor Egge's patches for NetBSD boot */ 4*58a2b000SEvgeniy Ivanov 5*58a2b000SEvgeniy Ivanov#include <machine/asm.h> 6*58a2b000SEvgeniy Ivanov 7*58a2b000SEvgeniy Ivanov/* 8*58a2b000SEvgeniy Ivanov# MSDOS call "INT 0x21 Function 0x3d" to open a file. 9*58a2b000SEvgeniy Ivanov# Call with %ah = 0x3d 10*58a2b000SEvgeniy Ivanov# %al = 0x0 (access and sharing modes) 11*58a2b000SEvgeniy Ivanov# %ds:%dx = ASCIZ filename 12*58a2b000SEvgeniy Ivanov# %cl = attribute mask of files to look for 13*58a2b000SEvgeniy Ivanov*/ 14*58a2b000SEvgeniy Ivanov 15*58a2b000SEvgeniy Ivanov .globl _C_LABEL(doserrno) 16*58a2b000SEvgeniy Ivanov_C_LABEL(doserrno): .long 1 17*58a2b000SEvgeniy Ivanov 18*58a2b000SEvgeniy IvanovENTRY(dosopen) 19*58a2b000SEvgeniy Ivanov .code32 20*58a2b000SEvgeniy Ivanov pushl %ebp 21*58a2b000SEvgeniy Ivanov movl %esp, %ebp 22*58a2b000SEvgeniy Ivanov pushl %edx 23*58a2b000SEvgeniy Ivanov pushl %ebx 24*58a2b000SEvgeniy Ivanov pushl %esi 25*58a2b000SEvgeniy Ivanov pushl %edi 26*58a2b000SEvgeniy Ivanov 27*58a2b000SEvgeniy Ivanov movl 0x8(%ebp), %edx # File name. 28*58a2b000SEvgeniy Ivanov 29*58a2b000SEvgeniy Ivanov call _C_LABEL(prot_to_real) # enter real mode 30*58a2b000SEvgeniy Ivanov .code16 31*58a2b000SEvgeniy Ivanov 32*58a2b000SEvgeniy Ivanov push %ds 33*58a2b000SEvgeniy Ivanov movl %edx, %eax 34*58a2b000SEvgeniy Ivanov shrl $4, %eax 35*58a2b000SEvgeniy Ivanov mov %ds, %si 36*58a2b000SEvgeniy Ivanov add %si, %ax 37*58a2b000SEvgeniy Ivanov mov %ax, %ds 38*58a2b000SEvgeniy Ivanov and $0xf, %dx 39*58a2b000SEvgeniy Ivanov 40*58a2b000SEvgeniy Ivanov movb $0x3d, %ah # Open existing file. 41*58a2b000SEvgeniy Ivanov movb $0x0 , %al # ro 42*58a2b000SEvgeniy Ivanov 43*58a2b000SEvgeniy Ivanov sti 44*58a2b000SEvgeniy Ivanov int $0x21 45*58a2b000SEvgeniy Ivanov cli 46*58a2b000SEvgeniy Ivanov pop %ds 47*58a2b000SEvgeniy Ivanov 48*58a2b000SEvgeniy Ivanov jnc ok1 49*58a2b000SEvgeniy Ivanov mov %ax, _C_LABEL(doserrno) 50*58a2b000SEvgeniy Ivanov movl $-1, %edx 51*58a2b000SEvgeniy Ivanov jmp err1 52*58a2b000SEvgeniy Ivanovok1: 53*58a2b000SEvgeniy Ivanov movl $0,%edx 54*58a2b000SEvgeniy Ivanov mov %ax, %dx 55*58a2b000SEvgeniy Ivanoverr1: 56*58a2b000SEvgeniy Ivanov calll _C_LABEL(real_to_prot) # back to protected mode 57*58a2b000SEvgeniy Ivanov .code32 58*58a2b000SEvgeniy Ivanov 59*58a2b000SEvgeniy Ivanov movl %edx, %eax # return value in %eax 60*58a2b000SEvgeniy Ivanov 61*58a2b000SEvgeniy Ivanov popl %edi 62*58a2b000SEvgeniy Ivanov popl %esi 63*58a2b000SEvgeniy Ivanov popl %ebx 64*58a2b000SEvgeniy Ivanov popl %edx 65*58a2b000SEvgeniy Ivanov popl %ebp 66*58a2b000SEvgeniy Ivanov ret 67*58a2b000SEvgeniy Ivanov 68*58a2b000SEvgeniy IvanovENTRY(dosread) 69*58a2b000SEvgeniy Ivanov .code32 70*58a2b000SEvgeniy Ivanov pushl %ebp 71*58a2b000SEvgeniy Ivanov movl %esp, %ebp 72*58a2b000SEvgeniy Ivanov pushl %ebx 73*58a2b000SEvgeniy Ivanov pushl %ecx 74*58a2b000SEvgeniy Ivanov pushl %edx 75*58a2b000SEvgeniy Ivanov pushl %esi 76*58a2b000SEvgeniy Ivanov pushl %edi 77*58a2b000SEvgeniy Ivanov 78*58a2b000SEvgeniy Ivanov movl 0x8(%ebp), %ebx # File handle 79*58a2b000SEvgeniy Ivanov movl 0xc(%ebp), %edx # Buffer. 80*58a2b000SEvgeniy Ivanov movl 0x10(%ebp), %ecx # Bytes to read 81*58a2b000SEvgeniy Ivanov 82*58a2b000SEvgeniy Ivanov call _C_LABEL(prot_to_real) # enter real mode 83*58a2b000SEvgeniy Ivanov .code16 84*58a2b000SEvgeniy Ivanov 85*58a2b000SEvgeniy Ivanov push %ds 86*58a2b000SEvgeniy Ivanov movl %edx, %eax 87*58a2b000SEvgeniy Ivanov shrl $4, %eax 88*58a2b000SEvgeniy Ivanov mov %ds, %si 89*58a2b000SEvgeniy Ivanov add %si, %ax 90*58a2b000SEvgeniy Ivanov mov %ax, %ds 91*58a2b000SEvgeniy Ivanov and $0xf, %dx 92*58a2b000SEvgeniy Ivanov 93*58a2b000SEvgeniy Ivanov movb $0x3f, %ah # Read from file or device 94*58a2b000SEvgeniy Ivanov 95*58a2b000SEvgeniy Ivanov sti 96*58a2b000SEvgeniy Ivanov int $0x21 97*58a2b000SEvgeniy Ivanov cli 98*58a2b000SEvgeniy Ivanov pop %ds 99*58a2b000SEvgeniy Ivanov 100*58a2b000SEvgeniy Ivanov jnc ok2 101*58a2b000SEvgeniy Ivanov mov %ax, _C_LABEL(doserrno) 102*58a2b000SEvgeniy Ivanov movl $-1, %edx 103*58a2b000SEvgeniy Ivanov jmp err2 104*58a2b000SEvgeniy Ivanovok2: 105*58a2b000SEvgeniy Ivanov movl $0,%edx 106*58a2b000SEvgeniy Ivanov mov %ax, %dx 107*58a2b000SEvgeniy Ivanoverr2: 108*58a2b000SEvgeniy Ivanov calll _C_LABEL(real_to_prot) # back to protected mode 109*58a2b000SEvgeniy Ivanov .code32 110*58a2b000SEvgeniy Ivanov 111*58a2b000SEvgeniy Ivanov movl %edx, %eax # return value in %eax 112*58a2b000SEvgeniy Ivanov 113*58a2b000SEvgeniy Ivanov popl %edi 114*58a2b000SEvgeniy Ivanov popl %esi 115*58a2b000SEvgeniy Ivanov popl %edx 116*58a2b000SEvgeniy Ivanov popl %ecx 117*58a2b000SEvgeniy Ivanov popl %ebx 118*58a2b000SEvgeniy Ivanov popl %ebp 119*58a2b000SEvgeniy Ivanov ret 120*58a2b000SEvgeniy Ivanov 121*58a2b000SEvgeniy IvanovENTRY(dosclose) 122*58a2b000SEvgeniy Ivanov .code32 123*58a2b000SEvgeniy Ivanov pushl %ebp 124*58a2b000SEvgeniy Ivanov movl %esp, %ebp 125*58a2b000SEvgeniy Ivanov pushl %ebx 126*58a2b000SEvgeniy Ivanov pushl %esi 127*58a2b000SEvgeniy Ivanov pushl %edi 128*58a2b000SEvgeniy Ivanov 129*58a2b000SEvgeniy Ivanov movl 0x8(%ebp), %ebx # File handle 130*58a2b000SEvgeniy Ivanov 131*58a2b000SEvgeniy Ivanov call _C_LABEL(prot_to_real) # enter real mode 132*58a2b000SEvgeniy Ivanov .code16 133*58a2b000SEvgeniy Ivanov 134*58a2b000SEvgeniy Ivanov movb $0x3e, %ah # Close file. 135*58a2b000SEvgeniy Ivanov 136*58a2b000SEvgeniy Ivanov sti 137*58a2b000SEvgeniy Ivanov int $0x21 138*58a2b000SEvgeniy Ivanov cli 139*58a2b000SEvgeniy Ivanov 140*58a2b000SEvgeniy Ivanov jnc ok3 141*58a2b000SEvgeniy Ivanov mov %ax, _C_LABEL(doserrno) 142*58a2b000SEvgeniy Ivanov movl $-1, %ebx 143*58a2b000SEvgeniy Ivanov jmp err3 144*58a2b000SEvgeniy Ivanovok3: 145*58a2b000SEvgeniy Ivanov movl $0, %ebx 146*58a2b000SEvgeniy Ivanoverr3: 147*58a2b000SEvgeniy Ivanov calll _C_LABEL(real_to_prot) # back to protected mode 148*58a2b000SEvgeniy Ivanov .code32 149*58a2b000SEvgeniy Ivanov 150*58a2b000SEvgeniy Ivanov movl %ebx, %eax # return value in %eax 151*58a2b000SEvgeniy Ivanov 152*58a2b000SEvgeniy Ivanov popl %edi 153*58a2b000SEvgeniy Ivanov popl %esi 154*58a2b000SEvgeniy Ivanov popl %ebx 155*58a2b000SEvgeniy Ivanov popl %ebp 156*58a2b000SEvgeniy Ivanov ret 157*58a2b000SEvgeniy Ivanov 158*58a2b000SEvgeniy IvanovENTRY(dosseek) 159*58a2b000SEvgeniy Ivanov .code32 160*58a2b000SEvgeniy Ivanov pushl %ebp 161*58a2b000SEvgeniy Ivanov movl %esp, %ebp 162*58a2b000SEvgeniy Ivanov pushl %ebx 163*58a2b000SEvgeniy Ivanov pushl %ecx 164*58a2b000SEvgeniy Ivanov pushl %edx 165*58a2b000SEvgeniy Ivanov pushl %esi 166*58a2b000SEvgeniy Ivanov pushl %edi 167*58a2b000SEvgeniy Ivanov 168*58a2b000SEvgeniy Ivanov movl 0x8(%ebp), %ebx # File handle 169*58a2b000SEvgeniy Ivanov movl 0xc(%ebp), %ecx # Offset 170*58a2b000SEvgeniy Ivanov movl 0x10(%ebp) , %edx # whence 171*58a2b000SEvgeniy Ivanov 172*58a2b000SEvgeniy Ivanov call _C_LABEL(prot_to_real) # enter real mode 173*58a2b000SEvgeniy Ivanov .code16 174*58a2b000SEvgeniy Ivanov 175*58a2b000SEvgeniy Ivanov movb $0x42, %ah # Seek 176*58a2b000SEvgeniy Ivanov movb %dl, %al # whence 177*58a2b000SEvgeniy Ivanov mov %cx, %dx #offs lo 178*58a2b000SEvgeniy Ivanov shrl $0x10, %ecx #offs hi 179*58a2b000SEvgeniy Ivanov 180*58a2b000SEvgeniy Ivanov sti 181*58a2b000SEvgeniy Ivanov int $0x21 182*58a2b000SEvgeniy Ivanov cli 183*58a2b000SEvgeniy Ivanov 184*58a2b000SEvgeniy Ivanov jnc ok4 185*58a2b000SEvgeniy Ivanov mov %ax, _C_LABEL(doserrno) 186*58a2b000SEvgeniy Ivanov movl $-1, %edx 187*58a2b000SEvgeniy Ivanov jmp err4 188*58a2b000SEvgeniy Ivanovok4: 189*58a2b000SEvgeniy Ivanov shll $0x10, %edx #new ofs hi 190*58a2b000SEvgeniy Ivanov mov %ax, %dx #new ofs lo 191*58a2b000SEvgeniy Ivanoverr4: 192*58a2b000SEvgeniy Ivanov calll _C_LABEL(real_to_prot) # back to protected mode 193*58a2b000SEvgeniy Ivanov .code32 194*58a2b000SEvgeniy Ivanov 195*58a2b000SEvgeniy Ivanov movl %edx, %eax # return value in %eax 196*58a2b000SEvgeniy Ivanov 197*58a2b000SEvgeniy Ivanov popl %edi 198*58a2b000SEvgeniy Ivanov popl %esi 199*58a2b000SEvgeniy Ivanov popl %edx 200*58a2b000SEvgeniy Ivanov popl %ecx 201*58a2b000SEvgeniy Ivanov popl %ebx 202*58a2b000SEvgeniy Ivanov popl %ebp 203*58a2b000SEvgeniy Ivanov ret 204