forked from AbleOS/ableos
134 lines
2.6 KiB
Plaintext
134 lines
2.6 KiB
Plaintext
.{memory, buffer, log} := @use("../../../libraries/stn/src/lib.hb");
|
|
.{inb, outb} := memory
|
|
|
|
regs := @use("regs.hb");
|
|
.{ATA_PRIMARY_IO, ATA_PRIMARY_DEVCTL, ATA_SECONDARY_DEVCTL, ATA_REG_STAT, ATA_SR_ERR, ATA_SR_BSY, ATA_SR_DF, ATA_SR_DRQ} := regs
|
|
|
|
sloop := fn(i: u8): void {
|
|
// idx := 0xFFFFFFF
|
|
// loop {
|
|
// if idx == 0 {
|
|
// break
|
|
// } else {
|
|
// idx -= 1
|
|
// }
|
|
// }
|
|
|
|
loop {
|
|
if i == 0 {
|
|
break
|
|
} else {
|
|
a := inb(ATA_SECONDARY_DEVCTL)
|
|
|
|
i -= 1
|
|
}
|
|
}
|
|
}
|
|
|
|
// identify := fn(): u8 {
|
|
// a := inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_DRIVE_HEAD, 0xA0)
|
|
// log.info("Primary drive head set.\0")
|
|
|
|
// a = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_SECCOUNT, 0)
|
|
// a = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_LBA_LO, 0)
|
|
// a = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_LBA_MID, 0)
|
|
// a = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_LBA_HI, 0)
|
|
// a = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// outb(ATA_PRIMARY_COMM_REGSTAT, 0xEC)
|
|
// outb(ATA_PRIMARY_COMM_REGSTAT, 0xE7)
|
|
|
|
// status := inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// log.info("Waiting for status.\0")
|
|
// loop {
|
|
// if (status & STAT_BSY) == 0 {
|
|
// log.info("Status got.\0")
|
|
// break
|
|
// } else {
|
|
// sloop()
|
|
// }
|
|
// status = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// }
|
|
// if status == 0 {
|
|
// log.error("No drive detected.\0")
|
|
|
|
// return 0
|
|
// }
|
|
// log.info("Status indicates presence of a drive. Polling while STAT_BSY... \0")
|
|
// loop {
|
|
// if (status & STAT_BSY) == 0 {
|
|
// log.info("Status got.\0")
|
|
// break
|
|
// } else {
|
|
// sloop()
|
|
// }
|
|
// status = inb(ATA_PRIMARY_COMM_REGSTAT)
|
|
// }
|
|
|
|
// mid := inb(ATA_PRIMARY_LBA_MID)
|
|
// hi := inb(ATA_PRIMARY_LBA_HI)
|
|
|
|
// if (mid | hi) == 0 {
|
|
// log.error("The drive is not ATA. (Who knows what it is.)\0")
|
|
// return 0
|
|
// }
|
|
|
|
// return 0
|
|
// }
|
|
|
|
drive_reset := fn(): void {
|
|
log.warn("Drive resetting.\0")
|
|
outb(ATA_PRIMARY_DEVCTL, 0x4)
|
|
sloop(4)
|
|
outb(ATA_PRIMARY_DEVCTL, 0x0)
|
|
}
|
|
|
|
poll := fn(): u8 {
|
|
sloop(4)
|
|
status := 0
|
|
// busy loop waiting for status
|
|
loop {
|
|
status = inb(ATA_PRIMARY_IO + ATA_REG_STAT)
|
|
if (status & ATA_SR_BSY) == 0 {
|
|
break
|
|
}
|
|
}
|
|
|
|
status = inb(ATA_PRIMARY_IO + ATA_REG_STAT)
|
|
if (status & ATA_SR_DF) == 0 {
|
|
log.error("ATA PRIMARY DRIVE FAULT\0")
|
|
return 1
|
|
}
|
|
|
|
if (status & ATA_SR_ERR) == 0 {
|
|
log.error("ATA PRIMARY ERR\0")
|
|
return 2
|
|
}
|
|
|
|
if (status & ATA_SR_DRQ) == 0 {
|
|
log.error("ATA PRIMARY DRQ\0")
|
|
return 3
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
main := fn(): int {
|
|
// shuts down ableOS
|
|
// outb(0xF400, 0)
|
|
|
|
a := inb(0x4600)
|
|
b := inb(0x4700)
|
|
|
|
// c := buffer.search("XNumber\0")
|
|
// c := identify()
|
|
|
|
d := drive_reset()
|
|
e := poll()
|
|
|
|
return 0
|
|
} |