lba读硬盘

4 views
Skip to first unread message

my12...@gmail.com

unread,
Oct 30, 2005, 6:32:21 AM10/30/05
to java home
硬盘读写程序
raw_read:
push ebp
mov ebp, esp
push ebx
push edi
mov ebx, [ebp+16] ; al will be written to 1F6
and ebx, 0x1
shl ebx, 0x4
or ebx, 0x40
mov dx, 0x1F2 ; get sector number
mov al, 1
out dx, al
mov eax, [ebp+12] ; eax = offset
inc dx
out dx, al ; LBA low
shr eax, 8
inc dx
out dx, al ; LBA mid
shr eax, 8
inc dx
out dx, al ; LBA high
shr eax, 8
and eax, 0xF
; or al, bl
or al, 0xE0
mov dx, 0x1F6
out dx, al ; bl calculated above, plus LBA veryhigh
mov al, 0x20 ; READ
inc dx
out dx, al


.chkstat:
mov ecx, 1000000 ; Timeout of about one second (I/O
usually takes one
usec)
in al, dx ; Status
and al, 0x88
jz .return
cmp al, 0x80
jnz .next
loop .chkstat


;; print a message saying we timed out
jmp .return


.next:
mov dx, 0x1f0
mov ecx, 256 ; read 256 * number of sectors words
mov edi, [ebp + 8] ; buffer pointer
rep insw


;; print a message saying we're done


.return:
pop edi
pop ebx
mov esp, ebp
pop ebp
ret


# include<stdio.h>
# include<dos.h>
void rwsisk(int cmd,unsigned char *buff1, int head1, int track1, int
sector1 )
{ unsigned cahr buff[512]; int head, track, sector;unsinged long lba
;unsigned seg,off;
unsigned char *p; unsigned char a1,a2,a3,a4;
int i;
unsigned int flag2;
unsigned char flag;
p=buff;
outportb(0x1f6,0xa0);
outportb(0x1f7,0xec);
while(flag!=0x58)
flag=inportb(0x1f7);
for(i=0;i<256;i++) /*读出256字节的信息*/
{ flag2=inport(0x1f0);
*p++=(flag2<<8)>>8; /*把且出的字分割成两个字节*/
*p++=(flag2>>8);
}
track=(int)(buff[3]*256+buff[2]);
head=(int)(buff[7]*256+buff[6]);
sector=(int)(buff[0x0d]*256+buff[0x0c]);
lba=track1*track*sector+head1*sector+sector1-1;/*计算出lab的直*/
seg=FP_SEG(lba);
off=FP_OFF(lba);
a1=(unsigned char)peekb(seg,off);
a2=(unsigned char)peekb(seg,off+1);
a3=(unsigned char)peekb(seg,off+2);
a4=(unsigned char)peekb(seg,off+3);
a4=
p=buff1;
a4=(a4+0xf)|0xe0;
outportb(0x1f3,a1);
outportb(0x1f4,a2);
outportb(0x1f5,a3);
outportb(0x1f6,a4;
outportb(0x1f7,0x20);
while(1)
{flag=inportb(0x1f7);
if(flag==0x88)
return;
if(flag!=0x80)
break;}
if(cmd==2)
{
for(i=0;i<256;i++) /*读出256字节的信息*/
{ flag2=inport(0x1f0);
*p++=(flag2<<8)>>8; /*把且出的字分割成两个字节*/
*p++=(flag2>>8);
}
}
if(cmd==3)
{for(i=0;i<256;i++) /*读出256字节的信息*/
{p=p+1;
flag2=(unsigned int)(*p)*256+*(p-1);
p=p+1;
outport(0x1f0,flag2);
}
}

}

Reply all
Reply to author
Forward
0 new messages