2021-07-26 12:55:13 +00:00
|
|
|
#include <k/atapi.h>
|
|
|
|
#include <string.h>
|
2021-07-28 23:45:52 +00:00
|
|
|
#include <stdio.h>
|
2021-07-26 12:55:13 +00:00
|
|
|
|
|
|
|
#include "atapi.h"
|
|
|
|
#include "io.h"
|
|
|
|
|
2021-07-28 23:45:52 +00:00
|
|
|
static struct atapi_drive a_drive = { 0 };
|
2021-07-26 12:55:13 +00:00
|
|
|
|
2021-07-28 21:46:18 +00:00
|
|
|
/* wait functions */
|
2021-07-26 12:55:13 +00:00
|
|
|
static void busy_wait(uint16_t drive)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
while (res & BSY)
|
|
|
|
res = inb(ATA_REG_STATUS(drive));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_device_selection(uint16_t drive)
|
|
|
|
{
|
2022-04-23 16:27:44 +00:00
|
|
|
for (int i = 0; i < 4000; ++i)
|
|
|
|
//printf("wait\r\n");
|
2021-07-26 12:55:13 +00:00
|
|
|
inb(drive);
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:46:18 +00:00
|
|
|
static void wait_packet_request(uint16_t drive)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
while (res & BSY && !(res & DRQ))
|
|
|
|
res = inb(ATA_REG_STATUS(drive));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drive discover functions */
|
|
|
|
static void select_drive(uint16_t bus, uint8_t slave)
|
|
|
|
{
|
|
|
|
outb(ATA_REG_DRIVE(bus), slave);
|
|
|
|
}
|
|
|
|
|
2021-07-26 13:00:23 +00:00
|
|
|
static uint8_t is_atapi_drive(uint16_t bus, uint8_t slave)
|
2021-07-26 12:55:13 +00:00
|
|
|
{
|
|
|
|
static uint8_t atapi_sig[] = { ATAPI_SIG_SC, ATAPI_SIG_LBA_LO,
|
|
|
|
ATAPI_SIG_LBA_MI, ATAPI_SIG_LBA_HI };
|
|
|
|
uint8_t sig[4];
|
|
|
|
|
|
|
|
sig[0] = inb(ATA_REG_SECTOR_COUNT(bus));
|
|
|
|
sig[1] = inb(ATA_REG_LBA_LO(bus));
|
|
|
|
sig[2] = inb(ATA_REG_LBA_MI(bus));
|
|
|
|
sig[3] = inb(ATA_REG_LBA_HI(bus));
|
|
|
|
|
|
|
|
if (!memcmp(sig, atapi_sig, 4))
|
|
|
|
{
|
|
|
|
struct atapi_drive d = { bus, slave };
|
2021-07-28 23:45:52 +00:00
|
|
|
memcpy(&a_drive, &d, sizeof(struct atapi_drive));
|
2021-07-26 12:55:13 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void discover_atapi_drive(void)
|
|
|
|
{
|
2021-07-26 13:00:23 +00:00
|
|
|
/* primary_reg */
|
2021-07-26 12:55:13 +00:00
|
|
|
outb(PRIMARY_DCR, SRST);
|
|
|
|
outb(PRIMARY_DCR, INTERRUPT_DISABLE);
|
|
|
|
|
2021-07-26 13:00:23 +00:00
|
|
|
// master_port
|
2021-07-26 12:55:13 +00:00
|
|
|
select_drive(PRIMARY_REG, ATA_PORT_MASTER);
|
|
|
|
wait_device_selection(PRIMARY_REG);
|
2021-07-28 23:45:52 +00:00
|
|
|
if (is_atapi_drive(PRIMARY_REG, ATA_PORT_MASTER))
|
|
|
|
return;
|
2021-07-26 12:55:13 +00:00
|
|
|
|
2021-07-26 13:00:23 +00:00
|
|
|
// slave port
|
|
|
|
select_drive(PRIMARY_REG, ATA_PORT_SLAVE);
|
|
|
|
wait_device_selection(PRIMARY_REG);
|
2021-07-28 23:45:52 +00:00
|
|
|
if (is_atapi_drive(PRIMARY_REG, ATA_PORT_SLAVE))
|
|
|
|
return;
|
2021-07-26 13:00:23 +00:00
|
|
|
|
|
|
|
/* secondary_reg */
|
2021-07-26 12:55:13 +00:00
|
|
|
outb(SECONDARY_DCR, SRST);
|
|
|
|
outb(SECONDARY_DCR, INTERRUPT_DISABLE);
|
2021-07-26 13:00:23 +00:00
|
|
|
|
|
|
|
// master_port
|
|
|
|
select_drive(SECONDARY_REG, ATA_PORT_MASTER);
|
|
|
|
wait_device_selection(SECONDARY_REG);
|
2021-07-28 23:45:52 +00:00
|
|
|
if (is_atapi_drive(SECONDARY_REG, ATA_PORT_MASTER))
|
|
|
|
return;
|
2021-07-26 13:00:23 +00:00
|
|
|
|
|
|
|
// slave port
|
|
|
|
select_drive(SECONDARY_REG, ATA_PORT_SLAVE);
|
|
|
|
wait_device_selection(SECONDARY_REG);
|
2021-07-28 23:45:52 +00:00
|
|
|
if (is_atapi_drive(SECONDARY_REG, ATA_PORT_SLAVE))
|
|
|
|
return;
|
2021-07-26 12:55:13 +00:00
|
|
|
}
|
2021-07-28 21:46:18 +00:00
|
|
|
|
|
|
|
/* IO functions */
|
2021-07-28 23:45:52 +00:00
|
|
|
static int send_packet(struct SCSI_packet *pkt, uint16_t drive, uint16_t size)
|
2021-07-28 21:46:18 +00:00
|
|
|
{
|
|
|
|
// cpy SCSI_packet into uint16_t array
|
|
|
|
uint16_t packet[PACKET_SZ / 2];
|
|
|
|
memcpy(packet, pkt, PACKET_SZ * sizeof(uint8_t));
|
|
|
|
|
|
|
|
// wait for a packet to be requested
|
|
|
|
busy_wait(drive);
|
|
|
|
outb(ATA_REG_FEATURES(drive), 0);
|
|
|
|
outb(ATA_REG_SECTOR_COUNT(drive), 0);
|
|
|
|
outb(ATA_REG_LBA_MI(drive), size);
|
|
|
|
outb(ATA_REG_LBA_HI(drive), size >> 8);
|
|
|
|
outb(ATA_REG_COMMAND(drive), PACKET);
|
|
|
|
wait_packet_request(drive);
|
|
|
|
|
|
|
|
// write SCSI packet
|
|
|
|
for (int i = 0; i < PACKET_SZ / 2; ++i)
|
|
|
|
outw(ATA_REG_DATA(drive), packet[i]);
|
|
|
|
uint8_t read = 0;
|
|
|
|
while (read != PACKET_DATA_TRANSMIT)
|
|
|
|
read = inb(ATA_REG_SECTOR_COUNT(drive));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char block[CD_BLOCK_SZ];
|
|
|
|
|
2021-07-28 23:45:52 +00:00
|
|
|
void *read_block(size_t lba)
|
2021-07-28 21:46:18 +00:00
|
|
|
{
|
2021-07-28 23:45:52 +00:00
|
|
|
select_drive(a_drive.reg, a_drive.drive);
|
|
|
|
wait_device_selection(a_drive.reg);
|
|
|
|
|
2021-07-28 21:46:18 +00:00
|
|
|
struct SCSI_packet pkt = { 0 };
|
|
|
|
pkt.op_code = READ_12;
|
|
|
|
pkt.lba_lo = lba & 0xff;
|
|
|
|
pkt.lba_milo = (lba >> 0x8) & 0xff;
|
|
|
|
pkt.lba_mihi = (lba >> 0x10) & 0xff;
|
|
|
|
pkt.lba_hi = (lba >> 0x18) & 0xff;
|
|
|
|
pkt.transfer_length_lo = 1;
|
|
|
|
|
2021-07-29 01:57:05 +00:00
|
|
|
send_packet(&pkt, a_drive.reg, CD_BLOCK_SZ);
|
2021-07-28 21:46:18 +00:00
|
|
|
|
|
|
|
// we can now read
|
2021-07-28 23:45:52 +00:00
|
|
|
uint16_t *buf = (uint16_t *)block;
|
2021-07-28 21:46:18 +00:00
|
|
|
for (int i = 0; i < CD_BLOCK_SZ / 2; ++i)
|
2021-07-28 23:45:52 +00:00
|
|
|
buf[i] = inw(ATA_REG_DATA(a_drive.reg));
|
2021-07-28 21:46:18 +00:00
|
|
|
|
|
|
|
uint8_t read = 0;
|
|
|
|
while (read != PACKET_COMMAND_COMPLETE)
|
2021-07-28 23:45:52 +00:00
|
|
|
read = inb(ATA_REG_SECTOR_COUNT(a_drive.reg));
|
|
|
|
|
|
|
|
return block;
|
2021-07-28 21:46:18 +00:00
|
|
|
}
|