; base port register %define BASE_IO_PORT 0x1f0 %define BASE_CMD_PORT 0x3f4 %define ATA_SR_BSY 0x80 %define ATA_SR_DRDY 0x40 %define ATA_SR_DF 0x20 %define ATA_SR_DSC 0x10 %define ATA_SR_DRQ 0x08 %define ATA_SR_CORR 0x04 %define ATA_SR_IDX 0x02 %define ATA_SR_ERR 0x01 %define ATA_ER_BBK 0x80 %define ATA_ER_UNC 0x40 %define ATA_ER_MC 0x20 %define ATA_ER_IDNF 0x10 %define ATA_ER_MCR 0x08 %define ATA_ER_ABRT 0x04 %define ATA_ER_TK0NF 0x02 %define ATA_ER_AMNF 0x01 %define ATA_CMD_READ_PIO 0x20 %define ATA_CMD_READ_PIO_EXT 0x24 %define ATA_CMD_READ_DMA 0xC8 %define ATA_CMD_READ_DMA_EXT 0x25 %define ATA_CMD_WRITE_PIO 0x30 %define ATA_CMD_WRITE_PIO_EXT 0x34 %define ATA_CMD_WRITE_DMA 0xCA %define ATA_CMD_WRITE_DMA_EXT 0x35 %define ATA_CMD_CACHE_FLUSH 0xE7 %define ATA_CMD_CACHE_FLUSH_EXT 0xEA %define ATA_CMD_PACKET 0xA0 %define ATA_CMD_IDENTIFY_PACKET 0xA1 %define ATA_CMD_IDENTIFY 0xEC %define ATA_MODE 0xe ; * Data Register: BAR0 + 0; // Read-Write ; * Error Register: BAR0 + 1; // Read Only ; * Features Register: BAR0 + 1; // Write Only ; * SECCOUNT0: BAR0 + 2; // Read-Write ; * LBA0: BAR0 + 3; // Read-Write ; * LBA1: BAR0 + 4; // Read-Write ; * LBA2: BAR0 + 5; // Read-Write ; * HDDEVSEL: BAR0 + 6; // Read-Write, used to select a drive in the channel. ; * Command Register: BAR0 + 7; // Write Only. ; * Status Register: BAR0 + 7; // Read Only. ; * Alternate Status Register: BAR1 + 2; // Read Only. ; * Control Register: BAR1 + 2; // Write Only. ; * DEVADDRESS: BAR1 + 2; // I don't know what is the benefit from this register. %define ATA_REG_DATA BASE_IO_PORT+0x00 %define ATA_REG_ERROR BASE_IO_PORT+0x01 %define ATA_REG_FEATURES BASE_IO_PORT+0x01 %define ATA_REG_SECCOUNT0 BASE_IO_PORT+0x02 %define ATA_REG_LBA0 BASE_IO_PORT+0x03 %define ATA_REG_LBA1 BASE_IO_PORT+0x04 %define ATA_REG_LBA2 BASE_IO_PORT+0x05 %define ATA_REG_HDDEVSEL BASE_IO_PORT+0x06 %define ATA_REG_COMMAND BASE_IO_PORT+0x07 %define ATA_REG_STATUS BASE_IO_PORT+0x07 ; %define ATA_REG_SECCOUNT1 BASE_IO_PORT+0x08 ; %define ATA_REG_LBA3 BASE_IO_PORT+0x09 ; %define ATA_REG_LBA4 BASE_IO_PORT+0x0A ; %define ATA_REG_LBA5 BASE_IO_PORT+0x0B %define ATA_REG_CONTROL BASE_CMD_PORT+0x02 %define ATA_REG_ALTSTATUS BASE_CMD_PORT+0x02 ; %define ATA_REG_DEVADDRESS BASE_CMD_PORT+0x02 %define high_io [ebx+4] %define low_io [ebx] cursor: dd 0xc00b8040 putch: push ebx and ax,0x00ff or ax,0x7000 mov ebx,[cursor] add bx,2 mov [ebx],ax mov [cursor],ebx pop ebx ret %macro GETSTATUS 0 xor eax,eax ; mov edx,high_io ; mov dx,ATA_REG_ALTSTATUS ; add dx,2 mov edx,[ebx] add dx,7 in al,dx %endmacro %macro PRINTSTATUS 0 GETSTATUS push eax call printstatus pop eax %endmacro wait_for_busy: GETSTATUS test ax,ATA_SR_BSY jz .end inc word [0xc00b8002] jmp wait_for_busy .end: ret wait_for_drq: GETSTATUS ; push eax ; call printstatus ; pop eax test ax,ATA_SR_DRQ jnz .end inc word [0xc00b8012] jmp wait_for_drq .end: ret wait_for_ready: GETSTATUS test ax,ATA_SR_DRDY jnz .end inc word [0xc00b8004] jmp wait_for_ready .end: ret ; %macro GETADDRESS global ata_issue_identify global ata_read_buffer global ata_28bit_operation global ata_read_test extern printstatus ; param 1, drive no. ; param 2, buffer ; param 3, low io port ; param 4, high io port ; return 1 if not present, 0 otherwise ata_issue_identify: push ebp mov ebp,esp push ebx mov ebx,ebp add ebx,16 ; select drive mov edx,[ebx] ; mov dx,ATA_REG_HDDEVSEL add edx, 6 mov eax, [ebp+8] and al,1 ; do the bit twiddling shl al,4 or al,ATA_MODE out dx,al ; PRINTSTATUS GETSTATUS ; pause a bit GETSTATUS GETSTATUS call wait_for_busy ; wait for rdy ; PRINTSTATUS ; GETSTATUS ; test al,ATA_SR_ERR ; jz .noerror ; xor eax,eax ; ret ; .noerror: GETSTATUS test al,ATA_SR_DRDY jnz .noerror2 xor eax,eax pop ebx pop ebp ret .noerror2: ; call wait_for_ready ; now issue the identify command. mov ax, ATA_CMD_IDENTIFY mov edx,low_io ; mov dx,ATA_REG_COMMAND add dx,7 out dx,al ; wait a few ticks GETSTATUS GETSTATUS GETSTATUS GETSTATUS ; wait for !bsy and drq call wait_for_busy call wait_for_drq ; param 1, buffer (fixed at 512b len) ; param 2, low io port ; param 3, high io port push dword [ebx+4] push dword [ebx] mov eax,[ebp+12] push eax ; buffer ad. call ata_read_buffer add esp,12 ; read into the buffer ; it's possible a string instruction is incorrect though... it works/// ; mov edx,[ebx] ; data reg, no offset ; mov edi, [ebp+12] ; destination buf ; mov ecx,128 ; count (128 dwords = 512b) ; cld ; rep insd ; ; pop ebx pop ebp ret ; param 1, buffer (fixed at 512b len) ; param 2, low io port ; param 3, high io port ata_read_buffer: push ebx mov ebx,esp add ebx,12 push edi GETSTATUS test al,ATA_SR_BSY|ATA_SR_ERR jnz .end test al,ATA_SR_DRQ jz .end ; read into the buffer ; it's possible a string instruction is incorrect though... it works/// mov edx,[ebx] ; data reg, no offset mov edi, [ebx-4] ; destination buf mov ecx,128 ; count (128 dwords = 512b) cld rep insd ; .end: pop edi pop ebx ret extern ide_buf ; param 1 low io ; param 2 high ata_read_test: push ebp mov ebp,esp push ebx mov ebx,ebp add ebx,8 push dword 0 ; loop counter push dword 0 ; read it push dword 1 ; sector push dword [ebx+4] ; high io push dword [ebx] ; low io push dword ide_buf ; buffer push dword 0 ; drive number .loop1: mov [ide_buf],dword 0 mov eax,[esp+24] and eax,0xf shl eax,18 mov [esp+16],eax inc word [0xc00b8024] call ata_28bit_operation call wait_for_busy call wait_for_drq GETSTATUS test eax,ATA_SR_ERR jz .noerror PRINTSTATUS .noerror: push dword [ebx+4] ; high io push dword [ebx] ; low io push dword ide_buf ; buffer call ata_read_buffer add esp,12 mov al, [ide_buf] call putch mov al, [ide_buf+1] call putch mov al, [ide_buf+2] call putch ; mov [0xc00b8040],eax inc dword [esp+24] mov eax,[esp+24] cmp eax,1<<16 ; jle .loop1 add esp,28 pop ebx pop ebp ret ; param 1, drive no. ; param 2, buffer ; param 3, low io port ; param 4, high io port ; param 5, sector number ; param 6, direction 1=write,0=read ; return 1 if not present, 0 otherwise ata_28bit_operation: push ebp mov ebp,esp push ebx mov ebx,ebp add ebx,16 ; param block sub esp,4 mov eax,[ebx+8] mov [esp],al ; lba_0 shr eax,8 mov [esp+1],al ; lba 1 shr eax,8 mov [esp+2],al ; lba 2 shr eax,8; eax=head address and al,0xf ; should be unnecessary mov [esp+3],al call wait_for_busy ; PRINTSTATUS ; select drive mov edx,[ebx] ; mov dx,ATA_REG_HDDEVSEL add edx, 6 mov eax, [ebp+8] and al,1 ; do the bit twiddling shl al,4 or al,0xe0 or al,[esp+3] ; head count out dx,al mov edx,[ebx] add dx,2 mov al,1 out dx,al ; sector count inc dx mov al,[esp] out dx,al ; lba0 inc dx mov al,[esp+1] out dx,al ; lba1 inc dx mov al,[esp+2] out dx,al ; lba2 ; mov eax,[ebx+8] ; test eax,eax ; jnz .writecmd mov al,ATA_CMD_READ_PIO ; jmp .afterdecision ; .writecmd: ; mov al,ATA_CMD_WRITE_PIO ; .afterdecision: mov edx,[ebx] add dx,7 ; command register out dx,al ; PRINTSTATUS ; prologue add esp,4 pop ebx pop ebp ret