we can reaad CDROMS

This commit is contained in:
2026-06-24 13:30:23 -05:00
parent ae4b6863bf
commit 68dbdbb2c8
7 changed files with 133 additions and 57 deletions

View File

@@ -1,4 +1,4 @@
#include "drivers/atapi/ata.h" #include "ide.h"
#include "lib/print.h" #include "lib/print.h"
@@ -60,18 +60,7 @@ static int wait_not_bsy_drq(uint16_t bus) {
return 0; return 0;
} }
static int ide_irq_invoked = 0; static int select_drive(uint16_t bus, uint8_t drive) {
static void ide_irq_wait() {
while (!ide_irq_invoked)
;
ide_irq_invoked = 0;
}
void ide_invoke_irq() {
ide_irq_invoked = 1;
}
int select_drive(uint16_t bus, uint8_t drive) {
if (bus == IDE_PRIMARY_IO_BASE && selected_drive_primary == drive) { if (bus == IDE_PRIMARY_IO_BASE && selected_drive_primary == drive) {
return 1; return 1;
} }
@@ -105,7 +94,7 @@ int select_drive(uint16_t bus, uint8_t drive) {
return 1; return 1;
} }
void ident_device(uint16_t bus, uint8_t drive, uint32_t slot) { static void ident_device(uint16_t bus, uint8_t drive, uint32_t slot) {
ide_dev_type type = NO_DEVICE; ide_dev_type type = NO_DEVICE;
uint8_t stat = inb(bus + IO_STATUS_REG); uint8_t stat = inb(bus + IO_STATUS_REG);
@@ -176,24 +165,10 @@ void ident_device(uint16_t bus, uint8_t drive, uint32_t slot) {
} else { } else {
ide_devices[slot].size = *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA); ide_devices[slot].size = *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA);
} }
char* serial_buf = (char*)(ide_info_buffer + ATA_IDENT_SERIAL);
fix_ata_string(serial_buf, 8);
// ide_devices[slot].serial = serial_buf;
char* model_buf = (char*)(ide_info_buffer + ATA_IDENT_MODEL);
fix_ata_string(model_buf, ATA_IDENT_MODEL_SZ);
// ide_devices[slot].model = model_buf;
kprintf("Found device 0x%x on bus 0x%x:\n", drive, bus);
kprintf("\tModel Number: %s\n", model_buf);
kprintf("\tSerial Number: %s\n", serial_buf);
kprintf("\tSize %dB\n", *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA));
} }
uint8_t temp_sector_buf[2048]; static int atapi_read_sector(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf) {
void atapi_read_sector(uint8_t drive_num, uint32_t lba, uint32_t sectors, void* buf) { ide_device_t drive = *dev;
ide_device_t drive = ide_devices[drive_num];
// enable irqs // enable irqs
uint16_t ctrl_reg = fetch_ctrl_reg(drive.bus); uint16_t ctrl_reg = fetch_ctrl_reg(drive.bus);
@@ -214,7 +189,7 @@ void atapi_read_sector(uint8_t drive_num, uint32_t lba, uint32_t sectors, void*
pkt[11] = 0x0; pkt[11] = 0x0;
if (!select_drive(drive.bus, drive.device)) { if (!select_drive(drive.bus, drive.device)) {
return; return 0;
} }
outb(drive.bus + IO_FEATURE_REG, 0x00); // PIO mode outb(drive.bus + IO_FEATURE_REG, 0x00); // PIO mode
@@ -224,42 +199,41 @@ void atapi_read_sector(uint8_t drive_num, uint32_t lba, uint32_t sectors, void*
outb(drive.bus + IO_LBA_HI_REG, (words * 2) >> 8); outb(drive.bus + IO_LBA_HI_REG, (words * 2) >> 8);
outb(drive.bus + IO_CMD_REG, ATA_CMD_PACKET); outb(drive.bus + IO_CMD_REG, ATA_CMD_PACKET);
// poll for drq // poll for drq
int err = wait_not_bsy_drq(drive.bus); int err = wait_not_bsy_drq(drive.bus);
if (err) { if (err) {
kprintf("failed sending ata packet command\n"); return 0;
return;
} }
outsw(drive.bus + IO_DATA_REG, pkt, 6); outsw(drive.bus + IO_DATA_REG, pkt, 6);
uint32_t ptr = (uint32_t)buf; uint32_t ptr = (uint32_t)buf;
for (int i = 0; i < sectors; i++) { for (int i = 0; i < sectors; i++) {
ide_irq_wait();
err = wait_not_bsy_drq(drive.bus); err = wait_not_bsy_drq(drive.bus);
if (err) { if (err) {
kprintf("failed reading atapi sector\n"); return 0;
return;
} }
insw(drive.bus + IO_DATA_REG, (void*)ptr, 1024); insw(drive.bus + IO_DATA_REG, (void*)ptr, 1024);
ptr += 2048; ptr += 2048;
} }
ide_irq_wait(); return 1;
while(inb(drive.bus + IO_STATUS_REG) & (STATUS_REG_BSY | STATUS_REG_DRQ));
kprintf("read %d sectors from drive %d\n", sectors, drive_num);
} }
void scan_drives() { void scan_drives() {
ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, 0); ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, 0);
}
ide_device_t boot_drive = ide_devices[0];
if (!(boot_drive.capabilities & 0x200)) { ide_device_t* get_device(uint8_t slot) {
kprintf("boot drive does not support LBA addressing"); if (slot > 3) return NULL;
return; return &ide_devices[slot];
}
int ide_read_blks(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf) {
if (dev->type == ATAPI) {
return atapi_read_sector(dev, lba, sectors, buf);
} }
return 0;
atapi_read_sector(0, 16, 1, temp_sector_buf);
} }

View File

@@ -114,11 +114,9 @@ typedef struct __attribute__((packed)) {
uint32_t size; uint32_t size;
} ide_device_t; } ide_device_t;
int select_drive(uint16_t bus, uint8_t drive);
void ident_device(uint16_t bus, uint8_t drive, uint32_t slot);
void scan_drives(); void scan_drives();
ide_device_t* get_device(uint8_t slot);
void ide_invoke_irq(); int ide_read_blks(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf);
#endif #endif

40
kernel/drivers/iso/iso.c Normal file
View File

@@ -0,0 +1,40 @@
#include "iso.h"
#include "drivers/ide/ide.h"
#include "lib/print.h"
#include "lib/memory.h"
uint8_t sector_buf[2048];
void read_iso(ide_device_t* dev) {
kprintf("Reading ISO Filesystem from CDROM Disk\n");
ide_read_blks(dev, 16, 1, sector_buf);
if (sector_buf[0] != 1) {
kprintf("Couldn't find PVD\n");
return;
}
iso_pvd_t pvd = *(iso_pvd_t*)sector_buf;
char pvd_identifier[6];
memcpy(pvd.header.identifier, pvd_identifier, 5);
pvd_identifier[5] = 0;
if (strcmp(pvd_identifier, "CD001") > 0) {
kprintf("Invalid PVD identifier\n");
return;
}
char system_identifier[33];
memcpy(&pvd.sys_ident[0], system_identifier, 32);
system_identifier[32] = 0;
if (strlen(system_identifier) > 0) {
kprintf("System Identifier: %s\n", system_identifier);
}
char volume_identifier[33];
memcpy(&pvd.volume_ident[0], volume_identifier, 32);
volume_identifier[32] = 0;
kprintf("Volume Identifier: %s\n", volume_identifier);
}

33
kernel/drivers/iso/iso.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef KISO_H
#define KISO_H
#include "drivers/ide/ide.h"
typedef struct __attribute__((packed)) {
uint8_t type;
char identifier[5];
uint8_t version;
} vd_header_t;
typedef struct __attribute__((packed)) {
vd_header_t header;
uint8_t unused;
uint8_t sys_ident[32];
uint8_t volume_ident[32];
uint8_t unused_1[8];
uint32_t volume_space_sz_le;
uint32_t volume_space_sz_be;
uint8_t unused_2[32];
uint16_t vol_set_sz_le;
uint16_t vol_set_sz_be;
uint16_t vol_seq_num_le;
uint16_t vol_seq_num_be;
uint16_t logical_blk_sz_le;
uint16_t logical_blk_sz_be;
uint32_t path_table_sz_le;
uint32_t path_table_sz_be;
} iso_pvd_t;
void read_iso(ide_device_t* dev);
#endif

View File

@@ -1,8 +1,6 @@
#include "lib/print.h" #include "lib/print.h"
#include <stdint.h> #include <stdint.h>
#include "drivers/atapi/ata.h"
extern void send_eoi_master(void); extern void send_eoi_master(void);
extern void send_eoi_slave(void); extern void send_eoi_slave(void);
extern uint8_t read_isr_master(void); extern uint8_t read_isr_master(void);
@@ -71,14 +69,10 @@ void common_interrupt_handler(struct registers *args) {
break; break;
} }
case 46: { // IDE PRIMARY case 46: { // IDE PRIMARY
kprintf("got interrupt\n");
ide_invoke_irq();
send_eoi_slave(); send_eoi_slave();
break; break;
} }
case 47: { // IDE SLAVE case 47: { // IDE SLAVE
kprintf("got interrupt\n");
ide_invoke_irq();
send_eoi_slave(); send_eoi_slave();
break; break;
} }

24
kernel/lib/memory.c Normal file
View File

@@ -0,0 +1,24 @@
#include "memory.h"
void memcpy(const void* src, void* dst, size_t sz) {
for (size_t i = 0; i < sz; i++) {
((uint8_t*)dst)[i] = ((uint8_t*)src)[i];
}
}
int strcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const uint8_t*)s1 - *(const uint8_t*)s2;
}
int strlen(const char* str) {
int len = 0;
while(*str != 0) {
str++;
len++;
}
return len;
}

13
kernel/lib/memory.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef KMEMORY_H
#define KMEMORY_H
#include <stdint.h>
#include <stddef.h>
void memcpy(const void* src, void* dst, size_t sz);
void memset(const void* dst, uint8_t val, size_t sz);
int strcmp(const char *s1, const char *s2);
int strlen(const char* str);
#endif