Skip to content

Commit

Permalink
feat(kernel): probe ata disk
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Jul 22, 2024
1 parent e84e98f commit 8a8f27c
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 4 deletions.
1 change: 1 addition & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SRCS = kernel.asm \
dev/at/kbd.inc \
dev/at/com.inc \
dev/at/ne2k.inc \
dev/at/ata.inc \
dev/at/floppy.inc \
dev/dev.inc \
fs/fat.inc \
Expand Down
128 changes: 128 additions & 0 deletions kernel/dev/at/ata.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
;; File: ata.inc
;;
;; <ATA PIO Mode at https://wiki.osdev.org/ATA_PIO_Mode>
;; <IDE spec at https://www.cpcwiki.eu/imgs/a/a2/IDE_SPEC.PDF>

ATA_PRIMARY_IO = 0x1F0
ATA_PRIMARY_IO_CTRL = 0x3F6
ATA_SECONDARY_IO = 0x170
ATA_SECONDARY_IO_CTRL = 0x376

ATA_DATA = 0x0
;; Constant: ATA_ERROR
;;
ATA_ERROR = 0x1
ATA_ERROR_AMNF = 0x01
ATA_ERROR_TKZNF = 0x02
ATA_ERROR_ABRT = 0x04
ATA_ERROR_MCR = 0x08
ATA_ERROR_IDNF = 0x10
ATA_ERROR_MC = 0x20
ATA_ERROR_UNC = 0x40
ATA_ERROR_BBK = 0x80

ATA_FEATURES = 0x1
ATA_SECCOUNT = 0x2
ATA_SECNUM = 0x3
ATA_CYLLO = 0x4
ATA_CYLHI = 0x5
;; Constant: ATA_DRVHEAD
;; Drive/Head register
;;
ATA_DRVHEAD = 0x6
ATA_DRVHEAD_DRV = 0x10
ATA_DRVHEAD_LBA = 0x40

ATA_COMMAND = 0x7

;; Constant: ATA_STATUS
;;
ATA_STATUS = 0x7
ATA_STATUS_ERR = 0x01
ATA_STATUS_IDX = 0x02
ATA_STATUS_CORR = 0x04
ATA_STATUS_DRQ = 0x08
ATA_STATUS_SRV = 0x10
ATA_STATUS_DF = 0x20
ATA_STATUS_RDY = 0x40
ATA_STATUS_BSY = 0x80

;; Constant: ATA_CTRL
;; Device control register (Control base + 0)
ATA_CTRL = 0x0
ATA_CTRL_nIEN = 0x02
ATA_CTRL_SRST = 0x04
ATA_CTRL_HOB = 0x80

ATA_DRVADDR = 0x1
ATA_DRVADDR_DS0 = 0x01
ATA_DRVADDR_DS1 = 0x02
ATA_DRVADDR_WTG = 0x40

ATA_CMD_RESTORE = 0x10
ATA_CMD_DIAGNOSTIC = 0x90
ATA_CMD_IDENTIFY = 0xA0

;; Function: ata_wait
ata_wait:
mov dx, ATA_PRIMARY_IO + ATA_STATUS
in al, dx
and al, ATA_STATUS_BSY
jnz ata_wait
ret

;; Function: ata_probe
ata_probe:
xor ecx, ecx
.loop:
call ata_wait
mov dx, ATA_DRVHEAD
mov al, cl

shl al, 4
or al, 0xa0
call ata_wait
mov dx, ATA_COMMAND
mov al, ATA_CMD_DIAGNOSTIC
out dx, al
call ata_wait
mov dx, ATA_STATUS
in al, dx
and al, ATA_STATUS_ERR or ATA_STATUS_DRQ
jnz @f
call ata_wait
mov dx, ATA_COMMAND
mov al, ATA_CMD_RESTORE
out dx, al
call ata_wait
mov dx, ATA_STATUS
in al, dx
and al, ATA_STATUS_ERR or ATA_STATUS_DRQ
jnz @f
push ecx
push ecx
mov esi, szMsgAtaFound
call klog
pop ecx
@@:
inc cl
cmp cl, 2
jb .loop
ret

;; Function: ata_init
ata_init:
call ata_probe
ret

ata_primary_irq:
iret

ata_secondary_irq:
iret

ata_device:
db "ata", 0, 0, 0, 0, 0
dd ata_init

szMsgAtaFound db "ATA: hd%u found", 0
15 changes: 15 additions & 0 deletions kernel/dev/at/com.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
;; File: com.inc
;; UART 8250, 16750 driver
;;
;; Usefull links:
;; - <PC16550D datasheet at https://www.scs.stanford.edu/10wi-cs140/pintos/specs/pc16550d.pdf>
Expand All @@ -14,8 +15,22 @@ UART8250_RBR = 0x0
UART8250_THR = 0x0

UART8250_IER = 0x1
UART8250_IER_RDA = 0x1
UART8250_IER_THRE = 0x2
UART8250_IER_RLSRC = 0x4
UART8250_IER_MSRC = 0x8
UART16750_IER_SLEEP = 0x10
UART16750_IER_LPM = 0x20

UART8250_IIR = 0x2

UART8250_FCR = 0x2
UART8250_FCR_EN = 0x1
UART8250_FCR_CLSR = 0x2
UART8250_FCR_CLST = 0x4
UART8250_FCR_DMA = 0x8
UART16750_FCR_64EN = 0x20

UART8250_LCR = 0x3
UART8250_MCR = 0x4
UART8250_MCR_OUT2 = 0x08
Expand Down
4 changes: 0 additions & 4 deletions kernel/dev/at/ide.inc

This file was deleted.

5 changes: 5 additions & 0 deletions kernel/dev/at/ne2k.inc
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,15 @@ ne2k_probe:
ret

ne2k_irq:
pusha
mov esi, szMsgNe2kIRQ
call klog
popa
iret

ne2k_device:
db 'ne2k', 0, 0, 0, 0
dd ne2k_init

szMsgNe2kfound db "NE2K: found", 0
szMsgNe2kIRQ db "NE2K: IRQ", 0
2 changes: 2 additions & 0 deletions kernel/dev/dev.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
include 'at/pit.inc'
include 'at/kbd.inc'
include 'at/cga.inc'
include 'at/ata.inc'
include 'at/floppy.inc'

struc Device {
Expand Down Expand Up @@ -50,6 +51,7 @@ aDevices:
dd console_device
dd com_device
dd ne2k_device
dd ata_device
.end:

dev_init:
Expand Down
Empty file added kernel/dev/random.inc
Empty file.
8 changes: 8 additions & 0 deletions kernel/idt.inc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ idt_setup:
mov eax, ne2k_irq
call idt_set_gate

mov ecx, 46
mov eax, ata_primary_irq
call idt_set_gate

mov ecx, 47
mov eax, ata_secondary_irq
call idt_set_gate

mov ecx, 0x42
mov eax, isr_syscall
call idt_set_gate
Expand Down

0 comments on commit 8a8f27c

Please sign in to comment.