IDE通道1,读写0x1f0-0x1f7号端口
IDE通道2,读写0x170-0x17f号端口
CHS方式:
写0x1f1: 0
写0x1f2: 要读的扇区数
写0x1f3: 扇区号
写0x1f4: 柱面的低8位
写0x1f5: 柱面的高8位
写0x1f6: 7~5位,101,第4位0表示主盘,1表示从盘,3~0位,磁头号
写0x1f7: 0x20为读, 0x30为写
读0x1f7: 第4位为0表示读写完成,否则要一直循环等待
读0x1f0: 每次读取1个word,反复循环,直到读完所有数据
24-bit LBA方式:
写0x1f1: 0
写0x1f2: 要读的扇区数
写0x1f3: LBA参数的0~7位
写0x1f4: LBA参数的8~15位
写0x1f5: LBA参数的16~23位
写0x1f6: 7~5位,111,第4位0表示主盘,1表示从盘,3~0位,LBA参数的24~27位
写0x1f7: 0x20为读, 0x30为写
读0x1f7: 第4位为0表示读写完成,否则要一直循环等待
读0x1f0: 每次读取1个word,反复循环,直到读完所有数据
48-bit LBA方式:
写两次0x1f1端口: 0
写两次0x1f2端口: 第一次要读的扇区数的高8位,第二次低8位
写0x1f3: LBA参数的24~31位
写0x1f3: LBA参数的0~7位
写0x1f4: LBA参数的32~39位
写0x1f4: LBA参数的8~15位
写0x1f5: LBA参数的40~47位
写0x1f5: LBA参数的16~23位
写0x1f6: 7~5位,010,第4位0表示主盘,1表示从盘,3~0位,0
写0x1f7: 0x24为读, 0x34为写
CHS Mode
- check_ready:
- mov dx, 0x1f7
- in al, dx
- test al, 0x40
- jz check_ready
- ret
- check_read_complete:
- mov dx, 0x1f7
- in al, dx
- test al, 0x08
- jz check_ready
- ret
- pio_delay:
- nop
- nop
- nop
- nop
- ret
- ;
- ; read sectors to buffer
- ; void
- ; read_sectors (
- ; int sector_count,
- ; int sector_number,
- ; int cylinder,
- ; int driver,
- ; int head,
- ; int buffer);
- ;
- ; parameters:
- ; sector_count: the count of sectors to read
- ; sector_number: the start sector to read
- ; cylinder: the cylinder of sector to read
- ; driver: 0: 1st desk, 1: 2nd desk
- ; head: 0~15
- ; buffer: the address of buffer
- ;
- _read_sectors:
- push ebp
- mov ebp, esp
- push eax
- push ebx
- push ecx
- push edx
- push edi
- call check_ready
- mov dx, 0x1f2
- mov eax, dword [ebp+8]
- ;mov al, 1 ; sector count=1
- out dx, al
- call pio_delay
- mov dx, 0x1f3
- mov eax, dword [ebp+12]
- ;mov al, 1 ; sector number=1
- out dx, al
- call pio_delay
- mov ebx, dword [ebp+16]
- and bx, 0x03ff
- mov dx, 0x1f4
- mov al, bl ; cylinder low 8bits=0
- out dx, al
- call pio_delay
- mov dx, 0x1f5
- mov al, bh ; cylinder high 2bits=0
- out dx, al
- call pio_delay
- mov ebx, dword [ebp+20]
- and ebx, 1
- shl bl, 4
- mov al, 0xa0 ; reserve=101, driver=0, head=0
- or al, bl
- mov ebx, dword [ebp+24]
- and bl, 0x0f
- or al, bl
- mov dx, 0x1f6
- out dx, al
- mov dx, 0x1f7
- mov al, 0x20 ; read until succeed
- out dx, al
- call check_read_complete
- mov eax, 256
- mov ebx, dword [ebp+8]
- mul bx
- mov cx, ax
- mov edi, dword [ebp+28]
- mov dx, 0x1f0
- rep insw
- pop edi
- pop edx
- pop ecx
- pop ebx
- pop eax
- pop ebp
- xor eax, eax
- ret
24-bit LBA Mode
- ;
- ; read sectors to buffer
- ; void
- ; read_sectors_lba24 (
- ; int sector_number,
- ; int sector_count,
- ; int driver,
- ; int buffer);
- ;
- ; parameters:
- ; sector_number: the start sector to read
- ; sector_count: the count of sectors to read
- ; driver: 0: 1st desk, 1: 2nd desk
- ; buffer: the address of buffer
- ;
- _read_sectors_lba24:
- push ebp
- mov ebp, esp
- push eax
- push ebx
- push ecx
- push edx
- push edi
- call check_ready
- mov dx, 0x1f2
- mov eax, dword [ebp+12]
- out dx, al ; sector count
- call pio_delay
- mov dx, 0x1f3
- mov eax, dword [ebp+8]
- out dx, al ; 0~7 bit
- call pio_delay
- mov dx, 0x1f4
- mov eax, dword [ebp+8]
- shr eax, 8 ; 8~15 bit
- out dx, al
- call pio_delay
- mov dx, 0x1f5
- mov eax, dword [ebp+8]
- shr eax, 16 ; 16~23 bit
- out dx, al
- call pio_delay
- mov ebx, dword [ebp+16]
- and ebx, 1
- shl bl, 4
- mov al, 0xe0 ; 5~7: 111, 4: driver, 0~3: 24~27 bit
- or al, bl
- mov ebx, dword [ebp+8]
- shr ebx, 24 ; 24~27 bit
- and bl, 0x0f
- or al, bl
- mov dx, 0x1f6
- out dx, al
- mov dx, 0x1f7
- mov al, 0x20 ; read until succeed
- out dx, al
- call check_read_complete
- mov eax, 256
- mov ebx, dword [ebp+12]
- mul bx
- mov cx, ax
- mov edi, dword [ebp+20]
- mov dx, 0x1f0
- rep insw
- pop edi
- pop edx
- pop ecx
- pop ebx
- pop eax
- pop ebp
- xor eax, eax
- ret
48-bit LBA Mode 实现过程类似,不给出具体代码了。。。